Refactor: extract defineInlineTextRedactRule factory#170
Merged
Conversation
Three text-walker rules — pii-redact, secrets-redact, and encoded-payload-redact — duplicated the same five-primitive lifecycle around a one-line difference in `collectMatches`: a ReusableAbortController + lazy subscribeRouteChange + createSubtreeWatcher with skipPlaceholderSubtrees + walkTextNodesChunked + a teardown that unwinds all of them. The new `defineInlineTextRedactRule` in `lib/inline-text-redact.ts` owns that scaffolding. Each rule file now declares its detection logic (regex patterns, decoding, entropy scoring) and hands `id`, `label`, `description`, `minLength`, and `collectMatches` to the factory. Net -52 LOC across the four files, and lifecycle state now lives in a closure rather than at module scope. prompt-injection-redact deliberately stays bespoke — it inserts block placeholders against a walked-up container, doesn't use the subtree watcher, and runs a second-pass dedupe via `onComplete`. A future `defineBlockTextRedactRule` could absorb it if more rules adopt that shape. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three text-walker rules —
pii-redact,secrets-redact, andencoded-payload-redact— duplicated the same five-primitive lifecycle around a one-line difference incollectMatches:The new
defineInlineTextRedactRuleinlib/inline-text-redact.tsowns that scaffolding. Each rule file now declares its detection logic — regex patterns (pii-redact), prefix patterns + entropy scoring (secrets-redact), candidate-window decoding (encoded-payload-redact) — and handsid,label,description,minLength, andcollectMatchesto the factory.What this changes
extension/src/lib/inline-text-redact.ts(100 LOC) — factory, plus anInlineTextRedactRuletype that tightensteardownto non-optional so existing tests can keep callingrule.teardown()directly.pii-redact(135 → 81),secrets-redact(202 → 153),encoded-payload-redact(350 → 301).unsubscribeRouteChange.Out of scope
prompt-injection-redactis a text-walker too but a different shape — it inserts a block placeholder against a walked-up container, doesn't use the subtree watcher, and runs a second-pass dedupe viaonCompletewithfilterToOutermost. A futuredefineBlockTextRedactRulecould absorb it if more rules adopt that shape.Rule.id: RuleIdtightening,subtree-watcherdecomposition) are deferred to keep this PR focused. TheRule.idtightening in particular would break existing test fixtures that use synthetic ids, so it needs its own treatment.Test plan
bun run test— 1354 tests pass (was 1354).bun run typecheck— clean.bun run check(Biome + ESLint) — clean.bun run knip— no unused exports.pii-redact,secrets-redact,encoded-payload-redacttest suites (57 tests) — all green, including the property tests.🤖 Generated with Claude Code