Skip to content

perf: fix O(n²) catastrophic backtracking in redact regex (salvage #4779)#4962

Merged
teknium1 merged 1 commit intomainfrom
hermes/hermes-a82207c8
Apr 4, 2026
Merged

perf: fix O(n²) catastrophic backtracking in redact regex (salvage #4779)#4962
teknium1 merged 1 commit intomainfrom
hermes/hermes-a82207c8

Conversation

@teknium1
Copy link
Copy Markdown
Contributor

@teknium1 teknium1 commented Apr 4, 2026

Salvage of PR #4779 by @acsezen. Cherry-picked with authorship preserved.

Fixes

  1. Regex bound (agent/redact.py): [A-Z_]*[A-Z_]{0,50} in _ENV_ASSIGN_RE. Eliminates O(n²) catastrophic backtracking on large uniform strings. Benefits all callers (file_tools, terminal_tool, browser_tool, code_execution_tool).

  2. Guard reorder (tools/file_tools.py): Character-count guard now runs before redact_sensitive_text(). Oversized content rejected immediately without touching the regex.

Results

  • 81 redact + file_tools tests pass in 0.20s (3 previously timed out at 30s+)
  • No behavioral change for real-world input (env var names are never 50+ chars)

Closes #4779

…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.
@teknium1 teknium1 merged commit 831067c into main Apr 4, 2026
3 of 4 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