Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(memory): skip the edit-distance retry check on long commands (#4371)
HetCreep59dbe9804248
diff --git a/headroom/memory/traffic_learner.py b/headroom/memory/traffic_learner.pyindex 6382077e8..7d2c95be9 100644--- a/headroom/memory/traffic_learner.py+++ b/headroom/memory/traffic_learner.py@@ -320,6 +320,10 @@ def _paths_related_as_typo(failed: str, success: str) -> bool:return Falseif 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 Falsethreshold = max(2, max(len(a), len(b)) // 3)return _levenshtein(a, b) <= threshold@@ -375,13 +379,21 @@ 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 Truereturn 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 +417,10 @@ def _commands_related_as_retry(failed: str, success: str) -> bool:return Falsemax_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 Truedef _substantive(cmd: str, binary: str) -> set[str]:diff --git a/tests/test_memory/test_traffic_learner.py b/tests/test_memory/test_traffic_learner.pyindex d335cd873..fa7eaecb9 100644--- a/tests/test_memory/test_traffic_learner.py+++ b/tests/test_memory/test_traffic_learner.py@@ -2918,3 +2918,98 @@ class TestPendingStatePersistence:await second.start()assert second.get_stats()["pending_patterns"] == 0await 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")+++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")+++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 == []