Skip to content

perf: ~15x faster anonymization via cheap substring gates - #21

Merged
armand0e merged 1 commit into
TeichAI:mainfrom
CompactAIOfficial:perf/anonymizer-gates
Jul 10, 2026
Merged

perf: ~15x faster anonymization via cheap substring gates#21
armand0e merged 1 commit into
TeichAI:mainfrom
CompactAIOfficial:perf/anonymizer-gates

Conversation

@CompactAIOfficial

Copy link
Copy Markdown
Contributor

Summary

The anonymization phase of teich extract runs ~35 compiled regexes (several with expensive variable-length lookbehinds, plus assignment patterns that match every key=value) over every string in every message — even plain prose that can't contain a secret. This PR gates each regex family behind a cheap literal substring check so the expensive patterns only run on text that could possibly match:

  • emails only if @ is present; home-path scrubbing only on home/Users
  • each API-key pattern only if its literal prefix (sk-ant-api03-, ghp_, AKIA, …) appears
  • assignment/generic-secret scanners only if a word that could make _is_sensitive_name fire is present (pass, secret, token, key, url, …)
  • JWT/JWE patterns behind a lookaround-free pre-scan
  • private-key blocks only on -----BEGIN ; Bearer only on bearer; credential URLs only on :// + @

Also caches the per-username owner/group regexes, which were recompiled for every string.

Results

3000-line synthetic chat export seeded with real keys/emails/home paths: 7.22s → 0.48s (~15x), output byte-identical to the old code, replacement counts identical. Per-file anonymizer scoping (consistent within a trace, fresh across traces) is unchanged.

Testing

  • pytest tests/test_extract_anonymize_cli.py — 32 passed
  • benchmark output diffed byte-for-byte against pre-change output

🤖 Generated with Claude Code

Run each regex family in TraceAnonymizer only when a cheap literal
substring check says the text could match: emails need '@', home paths
need 'home'/'Users', each API-key pattern needs its literal prefix,
assignment/generic-secret scans need a sensitive word, JWTs need a
lookaround-free pre-scan. Also cache the per-username owner/group
patterns instead of recompiling them for every string.

Output is byte-identical on a 3000-line benchmark (7.2s -> 0.48s);
per-file replacement scoping is unchanged.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@armand0e armand0e self-assigned this Jul 10, 2026
@armand0e
armand0e merged commit ff476f3 into TeichAI:main Jul 10, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants