fix(memory): skip the edit-distance retry check on long commands

_commands_related_as_retry compared every failed/successful Bash command
pair with _levenshtein, an O(n*m) pure-Python loop that runs on the
request path, with no bound on the command length. The helper's own
docstring says callers must keep input sizes reasonable.

Commands longer than _RETRY_EDIT_DISTANCE_MAX_CHARS (512) now skip the
edit-distance path and are judged by the existing linear shared-token
check, so ordinary short retries behave exactly as before.

Fixes #4367

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HetCreep
2026-10-22 11:09:24 +07:00
co-authored by Claude Opus 5.5
parent 4c7acba593
commit c5681e2214
2 changed files with 34 additions and 1 deletions
+9 -1
View File
@@ -382,6 +382,11 @@ def _bash_binaries_match(a: str, b: str) -> bool:
return False
# _levenshtein is O(n*m) pure Python and runs on the request path; longer
# commands skip it and are judged by the linear token-overlap check below.
_RETRY_EDIT_DISTANCE_MAX_CHARS = 512
def _commands_related_as_retry(failed: str, success: str) -> bool:
"""Heuristic: is `success` plausibly a corrected retry of `failed`?
@@ -405,7 +410,10 @@ def _commands_related_as_retry(failed: str, success: str) -> bool:
return False
max_len = max(len(failed), len(success))
if max_len > 0 and _levenshtein(failed, success) / max_len <= 0.40:
if (
0 < max_len <= _RETRY_EDIT_DISTANCE_MAX_CHARS
and _levenshtein(failed, success) / max_len <= 0.40
):
return True
def _substantive(cmd: str, binary: str) -> set[str]:
+25
View File
@@ -2918,3 +2918,28 @@ class TestPendingStatePersistence:
await second.start()
assert second.get_stats()["pending_patterns"] == 0
await second.stop()
def test_long_commands_skip_the_quadratic_edit_distance(monkeypatch):
"""#4367: commands past the cap are judged without _levenshtein."""
import headroom.memory.traffic_learner as tl
real = tl._levenshtein
def _guarded(a, b):
# binary-name matching may still use it on short names
if max(len(a), len(b)) > tl._RETRY_EDIT_DISTANCE_MAX_CHARS:
raise AssertionError("edit distance computed on an oversized command")
return real(a, b)
monkeypatch.setattr(tl, "_levenshtein", _guarded)
body = "x" * (tl._RETRY_EDIT_DISTANCE_MAX_CHARS + 1)
# shared substantive token -> still related, via the linear path
assert tl._commands_related_as_retry(f"python {body} run", f"python3 {body} run")
# no shared substantive token -> unrelated
assert not tl._commands_related_as_retry(f"python {body}", "python other.py")
def test_short_commands_still_use_edit_distance():
"""Commands within the cap keep the existing edit-distance behaviour."""
assert _commands_related_as_retry("cargo build", "cargo build --release")