Skip to content

TD-5449: Scrubber: the sk- rule reads a run once - #309

Merged
captcrock merged 3 commits into
mainfrom
td/5449-sk-rule-linear
Dec 3, 2026
Merged

captcrock merged 3 commits into
mainfrom
td/5449-sk-rule-linear

Conversation

@captcrock

@captcrock captcrock commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

The sk- key rule was quadratic. Its words check (TD-5160) ran at every "sk-" after a lowercase letter and read to the end of the run, so redact_secrets("xsk-" * 8000) took 2.9 s. Every reply's scrub (scrub_wire_json) runs this rule, so any model output, file or Code feed line of that shape froze the daemon. Now each run is read once, and the rule finds exactly the same keys as before.

Was stacked on #306 (TD-5447), which also changes core/tstd/logging.py. #306 is merged, and this branch is rebased onto main: two commits.

What changes

  • core/tstd/logging.py: _SkKeys replaces the sk- regex in SECRET_PATTERNS, with the sub, finditer and search its callers use. The regex is kept as _SkKeys.pattern.
    • The text after "sk-" always starts a segment of its run (split by "-" and "_"). So "the rest of the run reads as words" is the same as "every segment from here to the run's end is a word".
    • One pass over a run finds where its last segment that isn't a word starts. A body reads as words exactly when it starts after that point, so a run keeps one number, and every "sk-" in it uses it (second commit, after the second review).
    • Everything else is as before: the lowercase letter before "sk-", the 16-character minimum, and a key running to the end of its run.
  • ui/src/lib/redact.ts: the UI belt had the same rule and the same cost. skKeySpans reads it the same way.
  • Docs: the TD-5449 backlog entry.

Before / after

redact_secrets("xsk-" * n), one core:

n before (71a29921) after
2,000 0.175 s 0.003 s
4,000 0.736 s 0.005 s
8,000 2.894 s 0.010 s
32,000 — 0.043 s
128,000 — 0.175 s

The UI belt, "xsk-" * n: the old regex in node took 0.014 s at 2,000, 0.225 s at 8,000 and 3.734 s at 32,000. skKeySpans takes 0.002 s at 2,000, 0.038 s at 32,000 and 0.078 s at 128,000.

Same keys as before:

  • 200,000 random texts built from pieces that exercise the rule ("sk-", "Ask-", "task-", separators, words, acronyms, numbers, long runs, mixed case, non-ASCII): 0 differ from the regex, in spans or in sub output (e2e/td5449_sk_equivalence.py, run again on the final commit).
  • The tests run 20,000 of them in Python, and 20,000 in the UI against the old regex compiled from SK_KEY_SOURCE.

New tests:

  • core: the 20,000-text check against the regex;
  • core: a 1 MiB word run peaks under 3× its size in memory (second commit);
  • core: the match interface (group, span, start/end for groups 0 and 1, a function as repl, count, finditer) answers like the regex's;
  • a key after a word run in the same run (xsk-foo-Ask-…, xsk-foo-sk-…) is still caught;
  • 128,000 repeats of xsk-, xsk_ and ask-word- are left alone within 5 s (0.19 s here; the regex would take minutes, est. from its growth);
  • ui: the same check, the TD-5160 word cases and keys, and 100,000 repeats of xsk- within 2 s.

Independent review

A fresh reviewer agent with read-only access, and no access to my conclusions, reviewed the first version and wrote its own scripts, run on one core.

  • Same matches: about 4.4 million Python cases (3.15 million random, over 4 seeds, with word segments at their length limits and bodies of 14–18 characters; plus all 1,111,110 sequences of up to 6 of 10 tokens) and 1.2 million TypeScript cases (the transpiled redact.ts against SK_KEY_SOURCE). Spans, group(), search and sub matched the regex every time.
  • Linear: 16 adversarial shapes each took about 4× the time at 4× the size. 80,000 characters of xsk- took 28 ms through redact_secrets; a 390,000-character word run ending in a key took 50 ms.
  • Its finding, fixed: _SkKeys copied only part of the re.Pattern interface. Every current caller was fine, but group(1), start()/end(), a function as repl and count didn't work, and finditer returned a list. Now they work as the regex's do. A regex template string (\1) is refused rather than put in literally. _SkKeys.pattern keeps the regex's group, and a test checks each of these against it.

Second review (by the manager), on 5b4b7af2: about 3.99 million Python cases against the old regex, 60,000 through TS and Python, and 16.9 million TS-only all gave 0 mismatches. 32 KB of xsk- went from 3.02 s to 0.006 s. One finding, fixed in the second commit:

  • Memory per segment: the first version kept a list and a dict (or Map) entry for every segment of a run. "xsk-" + "a-" * N peaked at 90× a 1 MiB input in Python (tracemalloc), and grew the node heap by 284 MiB at 4 MiB. Now a run keeps one number: the Python peak is 1.0× the input (the output string), and the node heap grows by 4.0 MiB at 4 MiB. A new test holds the peak under 3× the input on two such runs; it fails on 5b4b7af2.
  • Checked again after the change: 200,000 random texts (0 differ), the first reviewer's 1,111,110 exhaustive cases (0 mismatches) and its TS fuzz, 1.2 million cases over 2 seeds (0 mismatches).

Tests

Not run locally: the full suite; the machine is on a CPU hold for other work.
CI: runs on this PR now that its base is main. (Before, while it was stacked, CI was started by hand on the first commit: https://github.com/ThatSimpleTech/TST-Desk/actions/runs/47212126385.)

  • Run, one core, on the rebased branch: the 12 test files that use redact_secrets, scrub_wire_json, secret_spans or SECRET_PATTERNS (including test_security_suite.py and test_pem_redaction.py), and tests/test_steering_proposals.py: 1,246 passed.
  • UI: redact.test.ts and notifications.test.ts pass (59), and tsc --noEmit is clean.

Checks

  • ruff, ruff format and mypy: clean.
  • release_scan --git: 0 hits.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SPdoY5jgXgTw6gUHBMBngR

@captcrock
captcrock changed the base branch from td/5447-pem-escaped-no-end to main December 3, 2026 22:09
captcrock and others added 2 commits December 3, 2026 22:10
The sk- rule's words lookahead (TD-5160) ran at every "sk-" after a
lowercase letter and read to the end of the run, so "xsk-" * n took
n^2/2 steps: redact_secrets("xsk-" * 8000) took 2.9 s, and every
reply's scrub runs this rule.

_SkKeys finds exactly the regex's matches in one pass: the text after
"sk-" starts a segment of its run, so "the rest reads as words" is
"every segment from here reads as a word", known for every segment
start from one pass from the right. The regex is kept as
_SkKeys.pattern and the two are checked against each other. The UI
belt (redact.ts) had the same rule and gets the same reading.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SPdoY5jgXgTw6gUHBMBngR
Review of #309: _sk_words_from (and wordsFrom in redact.ts) kept a list
and a dict entry per segment of a run, so "xsk-" + "a-" * N peaked at
90x a 1 MiB input in Python and grew the node heap by 284 MiB at 4 MiB.
A body reads as words exactly when it starts after the run's last
segment that isn't a word, so that one number is all a run keeps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SPdoY5jgXgTw6gUHBMBngR
@captcrock
captcrock force-pushed the td/5449-sk-rule-linear branch from 5b4b7af to 3d9a6cc Compare December 3, 2026 22:12
…49 sections)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@captcrock
captcrock merged commit ad0ce27 into main Dec 3, 2026
10 checks passed
@captcrock
captcrock deleted the td/5449-sk-rule-linear branch December 3, 2026 22:56
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.

1 participant