Repository navigation
Conversation
…1670 fix (#2398) py-spy caught this 14 times across three agents (cmo, copywriter, cornelius), every dump on the same stack: <genexpr> (credential_sanitizer.py) <- the any(...) in this function _is_sensitive_kv_key _redact_kv_match -> sanitize_text -> sanitize_dict (recursive) sanitize_subprocess_line -> read_stdout (headless_executor) #1670 made the LINE scan linear (#1661) and left the KEY test quadratic: re.search(r'(?:.*TOKEN.*)\Z', key) retries the leading .* from every offset, O(n^2) in key length. Measured on 3.13: 4 KB = 0.32s, 44 KB = 40s — for ONE key, per match, per line, on a thread holding the GIL. That is the pegged core. What made a quadratic test look harmless was a wrong docstring. It called the key "a short KEY= name". It is not: the key is whatever preceded an "=" in _KV_LINE_RE's unbounded capture — and the stack above reaches it from stream-json tool RESULTS via recursive sanitize_dict, so multi-KB "keys" are the normal case rather than an adversarial one. Every pattern is ".*LITERAL.*" or "LITERAL.*", and under .search() with a trailing \Z both mean exactly "the key CONTAINS this literal" — so the test is substring containment. Linear, C-level, no backtracking. VERDICTS ARE UNCHANGED, and that is the part that mattered to get right: a faster check that redacts LESS is a credential leak. Pinned against the old implementation as an oracle — 5,420 keys on the backend copy, 9,607 on the agent copy, zero disagreements — including the deliberate #1670 suffix semantics (DB_ still redacts MY_DB_PASSWORD). The literals are DERIVED from the patterns, and the derivation REFUSES anything that is not a plain literal, so a future pattern needing real regex fails loudly at import rather than being silently reduced to a check that redacts less. BOTH copies (Invariant #5). The two are vendored in shape, not byte-identically — the agent list is SENSITIVE_VAR_PATTERNS with 30 entries to the backend's 13 — and every py-spy dump was taken in the AGENT copy, so a backend-only fix would have left all 14 burns exactly where they were. backend copy: 44 KB key 40s -> 0.00035s (112,000x) agent-server copy: 44 KB key 44s -> 0.00066s ( 67,000x) 727 passed across the sanitizer, #1661 and #1670 suites. Related to #2398 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Contributor
Author
|
Superseded by #2406, which targets main. The burn is on client instances and they run The two files have diverged between main and dev, so #2406 is written against main's tree and measured there (7,566 + 9,606 keys, 0 disagreements; 704 tests) rather than cherry-picked from this branch.
|
Merged
3 of 5 tasks
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
This is my regression. #1670 fixed the catastrophic backtracking in the credential sanitizer's line scan (#1661) and left the identical shape one call deeper, in
_is_sensitive_kv_key. Every one of the 14 py-spy dumps is on that stack.The cost
re.search(r'(?:.*TOKEN.*)\Z', key)retries the leading.*from every offset — O(n²) in key length. Measured on 3.13:Forty seconds of CPU for one key, per match, per line, on a thread holding the GIL. That is the pegged core, and it is why the event loop stopped accepting.
Why a quadratic test looked harmless
A wrong docstring. It described
keyas "a shortKEY=name". It is not — the key is whatever preceded an=in_KV_LINE_RE's unbounded([^\s"'=]+). And the stack above reaches it from stream-json tool results through recursivesanitize_dict, so multi-KB "keys" are the normal case, not an adversarial one. That single wrong assumption is what let the quadratic survive the #1670 review.The fix
Every pattern is
.*LITERAL.*orLITERAL.*. Under.search()with a trailing\Z, both mean exactly "the key contains this literal" — so the test is substring containment: linear, C-level, no backtracking.Verdicts are unchanged, and that was the part to get right — a faster check that redacts less is a credential leak. Pinned against the old implementation as an oracle: 5,420 keys on the backend copy and 9,607 on the agent copy, zero disagreements, including the deliberate #1670 suffix semantics (
DB_still redactsMY_DB_PASSWORD).The literals are derived from the patterns and the derivation refuses anything that is not a plain literal — so a future pattern needing real regex fails loudly at import instead of being silently reduced to a check that redacts less. That is the guard #1670 should have had.
Both copies — and the agent one is the one that matters
Invariant #5. The two files are vendored in shape, not byte-identically: the agent list is
SENSITIVE_VAR_PATTERNSwith 30 entries to the backend's 13. Every py-spy dump was taken in the agent copy, so a backend-only fix would have left all 14 burns exactly where they were. A test asserts the agent copy carries the fix.What this does and does not close
schedule_executionsrows or the missingHEALTHCHECKon the agent image (no auto-recovery). Those are real and separate; the burn was making them visible.max_parallel_tasks10→5,execution_timeout3600→1800) cap blast radius and are still worth keeping until this ships to the fleet — note the agent copy means a base-image rebuild is required for existing agents.Test Plan
tests/unit/test_2398_sanitizer_key_redos.py— verdict equivalence against the old implementation, the fix(sanitizer): linear KEY=value redaction — the #1661 CPU spin is the regex, not the pipe #1670 suffix semantics, linearity at 64 KB, end-to-endsanitize_texton a 44 KB token, the derivation's refusal of a non-literal pattern, and the agent-copy parity assertion727 passedacross the sanitizer, bug: agent-server leaks a busy-spinning reader thread (100% CPU) on stdout-pipe-race / drain deadlock — #728 fix incomplete #1661 and fix(sanitizer): linear KEY=value redaction — the #1661 CPU spin is the regex, not the pipe #1670 suitesRelated to #2398 · follow-up to #1670 / #1661