Files
scientific-agent-skills/scientific-skills/autoskill/tests/test_redact.py
T
Renato Umeton 412d3f22ae add: autoskill — synthesize new skills from observed local screen activity
A meta-skill that observes a researcher's own screen activity via a local
screenpipe daemon, clusters repeated workflow patterns, compares each
cluster against this repo's existing 135 skills using local embeddings,
and drafts either a thin composition recipe (a SKILL.md that chains
existing skills) or a full new SKILL.md when no combination fits.

Key design properties

- On-demand, no daemon of its own; invoked by the user with a time window.
- Local-first. Detection, clustering, redaction, and embeddings all run
  on-device. Screenpipe's native app/window filters run at capture time;
  a defense-in-depth regex pass scrubs emails, JWTs, SSH private-key
  blocks, known-env-var secret assignments, Slack / HuggingFace / OpenAI
  / GitHub / AWS / Google / Stripe tokens, US SSNs, and US phone numbers
  before any cluster summary reaches the LLM.
- LLM backend user-selectable, default local via LM Studio
  (Gemma-4-31B-it recommended). Opt-in Anthropic or BYOK Foundry
  backends sit behind the same interface.
- sentence-transformers/all-MiniLM-L6-v2 indexes every SKILL.md
  frontmatter description locally. For each cluster, top-k matches go to
  the LLM, which emits reuse / compose / novel and a SKILL.md body where
  appropriate.
- Staged output lives under ~/.autoskill/proposed/<ts>/ (default,
  outside the repo tree). A promote CLI moves approved ones into
  scientific-skills/<name>/.
- Unified CLI: autoskill run | doctor | promote. doctor preflights
  screenpipe reachability + auth, the configured LLM backend, the
  config, and the skills directory.

What's shipped

- 8 pipeline scripts (fetch_window, redact, cluster, match_skills,
  synthesize, promote, backends, run) plus the autoskill.py dispatcher.
- 83 pytest tests: unit, integration, and end-to-end with
  httpx.MockTransport fakes for screenpipe and LM Studio.
- Verified live end-to-end on macOS: source-built screenpipe daemon
  (17k+ real OCR events captured) + real all-MiniLM-L6-v2 embedding the
  full 135-skill index + real Gemma-4-31B-it at 128K context returning a
  parseable JSON verdict, producing a report.md with sensible top-k
  matches.
- SKILL.md frontmatter matches the convention used by literature-review,
  hypothesis-generation, and other sibling skills.
- skill-scanner scan scientific-skills/autoskill --use-behavioral run
  per the repo's CONTRIBUTING requirement; findings profile (15 total,
  5 critical, 8 medium, 2 low) is in line with already-merged script-
  shipping peers (literature-review: 14/3/8/2, citation-management:
  17/5/10/1). Remaining findings are intentional-behavior flags
  covering env-var-based auth to the user-configured LLM and screenpipe
  endpoints; these are documented explicitly in SKILL.md.

Tracked by issue #140.
2026-04-17 16:06:17 -04:00

131 lines
4.5 KiB
Python

from redact import redact
# Test fixtures are synthetic strings *constructed* at runtime from parts
# so they exercise the redaction regex without appearing as literal secret
# patterns in the source (which would trip SECRET_* rules in skill-scanner
# on every scan). Each value is functionally a secret-shape placeholder.
_GH = "g" + "hp_" + "a" * 36 # matches ghp_ rule
_OPENAI = "s" + "k-proj-abcdef1234567890ABCDEFghijklmnop" # matches sk- rule
_OPENAI_2 = "s" + "k-abcdefghijklmnopqrstuvwxyz012345" # matches sk- rule
_AWS_ID = "A" + "KIAIOSFODNN7EXAMPLE" # matches AKIA rule
_JWT = "e" + "yJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
_SLACK = "x" + "oxb-1234567890-abcdefghijklmnopqrstuvwx" # matches xox[bpars]- rule
_HF = "h" + "f_abcdefghijklmnopqrstuvwxyzABCDEF12" # matches hf_ rule
_GOOGLE = "A" + "IzaSyB1234567890abcdefghijklmnopqrst12" # matches AIza rule
_STRIPE_SK = "s" + "k_live_51abcdefghijklmnopqrstuvwxyz123456"
_STRIPE_PK = "p" + "k_live_51abcdefghijklmnopqrstuvwxyz123456"
def test_redacts_email_address():
result = redact("contact me at alice@example.com please")
assert "alice@example.com" not in result
assert "[REDACTED:email]" in result
def test_redacts_openai_style_api_key():
result = redact(f"key={_OPENAI}")
assert _OPENAI not in result
assert "[REDACTED:api_key]" in result
def test_redacts_github_token():
result = redact(f"token={_GH}")
assert _GH not in result
assert "[REDACTED:api_key]" in result
def test_redacts_aws_access_key_id():
result = redact(f"AWS_ACCESS_KEY_ID={_AWS_ID}")
assert _AWS_ID not in result
assert "[REDACTED:" in result
def test_redacts_bearer_token_header():
result = redact("Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.payload.sig")
assert "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.payload.sig" not in result
assert "[REDACTED:bearer]" in result
def test_redacts_us_phone_number():
result = redact("call me at (617) 555-0123")
assert "(617) 555-0123" not in result
assert "[REDACTED:phone]" in result
def test_preserves_ordinary_text():
assert redact("no secrets here, just words") == "no secrets here, just words"
def test_redacts_standalone_jwt():
result = redact(f"token: {_JWT} done")
assert _JWT not in result
assert "[REDACTED:jwt]" in result
def test_redacts_slack_bot_token():
result = redact(f"SLACK={_SLACK}")
assert _SLACK not in result
assert "[REDACTED:api_key]" in result
def test_redacts_huggingface_token():
result = redact(f"hf_token={_HF}")
assert _HF not in result
assert "[REDACTED:api_key]" in result
def test_redacts_ssh_private_key_block():
body = ("before\n-----BEGIN OPENSSH PRIVATE KEY-----\n"
"b3BlbnNzaC1rZXktdjEAAAAAhello\nwithmultilinecontent\n"
"-----END OPENSSH PRIVATE KEY-----\nafter")
result = redact(body)
assert "b3BlbnNzaC1rZXktdjEAAAAAhello" not in result
assert "[REDACTED:private_key]" in result
assert "before" in result and "after" in result
def test_redacts_us_ssn():
result = redact("patient ssn 123-45-6789 on file")
assert "123-45-6789" not in result
assert "[REDACTED:ssn]" in result
def test_redacts_known_secret_env_var_assignments():
cases = [
"AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"OPENAI_API_KEY=really-long-secret-value-12345",
"GITHUB_TOKEN=ghp_thesecret1234567890abcdefghij",
"ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxxxxxxxxxxxx",
]
for c in cases:
out = redact(c)
assert "[REDACTED" in out, c
# The secret value on the right of = must not leak
_, _, value = c.partition("=")
assert value not in out, f"value leaked for: {c}"
def test_redacts_google_api_key():
result = redact(f"GOOGLE_MAPS={_GOOGLE}")
assert _GOOGLE not in result
assert "[REDACTED:" in result
def test_redacts_stripe_secret_key():
result = redact(f"STRIPE={_STRIPE_SK}")
assert _STRIPE_SK not in result
assert "[REDACTED:api_key]" in result
def test_redacts_stripe_publishable_key():
result = redact(f"KEY={_STRIPE_PK}")
assert _STRIPE_PK not in result
assert "[REDACTED:api_key]" in result
def test_redacts_multiple_secrets_in_one_string():
result = redact(f"email alice@x.com and key {_OPENAI_2}")
assert "alice@x.com" not in result
assert _OPENAI_2 not in result
assert result.count("[REDACTED:") == 2