fix(memory): bound the remaining edit-distance paths in retry matching

_bash_binaries_match ran the O(n*m) _levenshtein on the first-token
basenames whenever one was a prefix of the other, with no length bound,
so a very long first token reached it before the command-length cap.
When one name is a prefix of the other the edit distance equals the
length difference, so compare lengths instead (same result, linear).

_paths_related_as_typo ran the same edit distance on traffic-derived
file basenames with no bound either. No real file name comes near
_RETRY_EDIT_DISTANCE_MAX_CHARS, so a basename past it is treated as
unrelated instead of being compared.

Fixes #4367

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HetCreep
2026-10-24 09:14:52 +07:00
co-authored by Claude Sonnet 5.5
parent c5681e2214
commit 2707c5b82b
2 changed files with 79 additions and 2 deletions
+9 -2
View File
@@ -320,6 +320,10 @@ def _paths_related_as_typo(failed: str, success: str) -> bool:
return False
if a == b:
return True
# Basenames come from tool traffic; no real file name comes near this cap,
# so skip the quadratic edit distance rather than run it on an oversized one.
if max(len(a), len(b)) > _RETRY_EDIT_DISTANCE_MAX_CHARS:
return False
threshold = max(2, max(len(a), len(b)) // 3)
return _levenshtein(a, b) <= threshold
@@ -375,8 +379,11 @@ def _bash_binaries_match(a: str, b: str) -> bool:
b_base = b.rsplit("/", 1)[-1]
if a_base == b_base:
return True
if (a_base.startswith(b_base) or b_base.startswith(a_base)) and _levenshtein(
a_base, b_base
# One name is a prefix of the other, so their edit distance is exactly the
# length difference: compare lengths instead of running _levenshtein (O(n*m))
# on an unbounded, traffic-derived token.
if (a_base.startswith(b_base) or b_base.startswith(a_base)) and abs(
len(a_base) - len(b_base)
) <= 2:
return True
return False
+70
View File
@@ -2943,3 +2943,73 @@ def test_long_commands_skip_the_quadratic_edit_distance(monkeypatch):
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")
def _record_oversized_levenshtein(monkeypatch):
"""Wrap _levenshtein and record every call on an input past the cap."""
import headroom.memory.traffic_learner as tl
real = tl._levenshtein
oversized: list[tuple[int, int]] = []
def _recording(a, b):
if max(len(a), len(b)) > tl._RETRY_EDIT_DISTANCE_MAX_CHARS:
oversized.append((len(a), len(b)))
return real(a, b)
monkeypatch.setattr(tl, "_levenshtein", _recording)
return oversized
def test_oversized_first_token_skips_the_quadratic_edit_distance(monkeypatch):
"""Review of #4371: a long executable token must not reach _levenshtein either."""
import headroom.memory.traffic_learner as tl
oversized = _record_oversized_levenshtein(monkeypatch)
head = "x" * 2000
# the first tokens (2000 and 2001 chars) are prefix-related; "execute" is shared
assert tl._commands_related_as_retry(f"{head} execute", f"{head}3 execute")
assert tl._bash_binaries_match(head, f"{head}3")
assert not tl._bash_binaries_match(head, f"{head}333")
assert oversized == []
def test_short_binary_names_keep_the_prefix_tolerance():
assert _bash_binaries_match("python", "python3")
assert _bash_binaries_match("python3", "python")
assert _bash_binaries_match("python", "python32")
assert not _bash_binaries_match("python", "python321")
assert not _bash_binaries_match("pyth", "python3")
@pytest.mark.asyncio
async def test_oversized_first_token_through_on_tool_result(monkeypatch):
"""Same case through the entry point the proxy handlers await."""
oversized = _record_oversized_levenshtein(monkeypatch)
learner = TrafficLearner(backend=None, user_id="test-user", min_evidence=1)
head = "x" * 2000
await learner.on_tool_result(
tool_name="Bash",
tool_input={"command": f"{head} execute"},
tool_output="Error: execute failed with status 2",
is_error=True,
)
await learner.on_tool_result(
tool_name="Bash",
tool_input={"command": f"{head}3 execute"},
tool_output="done",
is_error=False,
)
assert oversized == []
assert learner.get_stats()["patterns_extracted"] >= 1
def test_oversized_path_basename_skips_the_quadratic_edit_distance(monkeypatch):
"""The file-path typo check runs the same edit distance on traffic basenames."""
import headroom.memory.traffic_learner as tl
oversized = _record_oversized_levenshtein(monkeypatch)
name = "y" * 2000
assert not tl._paths_related_as_typo(f"/a/{name}.rs", f"/a/{name}x.rs")
assert tl._paths_related_as_typo("/a/staet.rs", "/a/state.rs")
assert oversized == []