perf: fix O(n²) catastrophic backtracking in redact regex#4779
Closed
acsezen wants to merge 1 commit intoNousResearch:mainfrom
Closed
perf: fix O(n²) catastrophic backtracking in redact regex#4779acsezen wants to merge 1 commit intoNousResearch:mainfrom
acsezen wants to merge 1 commit intoNousResearch:mainfrom
Conversation
…ile read guard
Two pre-existing issues causing test_file_read_guards timeouts on CI:
1. agent/redact.py: _ENV_ASSIGN_RE used unbounded [A-Z_]* with
IGNORECASE, matching any letter/underscore to end-of-string at
each position → O(n²) backtracking on 100K+ char inputs.
Bounded to {0,50} since env var names are never that long.
2. tools/file_tools.py: redact_sensitive_text() ran BEFORE the
character-count guard, so oversized content (that would be rejected
anyway) went through the expensive regex first. Reordered to check
size limit before redaction.
4dcf7a9 to
f999bb0
Compare
Contributor
|
Merged via PR #4962. Cherry-picked onto current main with authorship preserved. Clean fix — 81 tests now pass in 0.20s instead of timing out. Thanks! |
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.
Problem
Three tests in
test_file_read_guards.pyconsistently time out on CI (30s limit):TestCharacterCountGuard::test_content_under_limit_passesTestCharacterCountGuard::test_oversized_read_rejectedTestConfigOverride::test_custom_config_raises_limitThese are pre-existing issues unrelated to any feature PR.
Root Cause
1. O(n²) regex in
agent/redact.py_ENV_ASSIGN_REuses unbounded[A-Z_]*withIGNORECASE, which matches any letter or underscore. On 100K+ character uniform strings (like the test fixtures), the engine tries every start position, greedily matches to end-of-string, then backtracks character-by-character looking forAPI_KEY/TOKEN/etc. This is classic catastrophic backtracking.Empirical timing: 5K chars → 0.2s, 10K → 0.6s, 20K → 2.4s, 100K → ~60s (extrapolated).
2. Guard ordering in
tools/file_tools.pyredact_sensitive_text()ran BEFORE the character-count guard, so oversized content (that would be rejected anyway) went through the expensive regex first.Fix
agent/redact.py: Bound[A-Z_]*to[A-Z_]{0,50}— env var names are never 50K+ chars. Same matching for real-world input, eliminates pathological backtracking.tools/file_tools.py: Move character-count guard before redaction. Content exceeding the limit is rejected immediately without touching the regex. Redaction only runs on content that passes the size check.Result
All 19 tests in
test_file_read_guards.pypass in 2.6s (previously 3 timed out at 30s+).