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(traffic-learner): tighten matchers and drop contradictions
Garm606131451b41
diff --git a/headroom/memory/traffic_learner.py b/headroom/memory/traffic_learner.pyindex bf9a6ac5e..06476c689 100644--- a/headroom/memory/traffic_learner.py+++ b/headroom/memory/traffic_learner.py@@ -211,6 +211,180 @@ _MODULE_RE = re.compile(r"No module named ['\"]?(\w[\w.]*)['\"]?")_COMMAND_NF_RE = re.compile(r"(\w[\w-]*): command not found")+def _levenshtein(a: str, b: str) -> int:+ """Iterative Levenshtein distance. Pure Python, no deps.++ Bounded use only — callers should keep input sizes reasonable+ (basenames, command strings) to avoid O(n*m) blowups.+ """+ if a == b:+ return 0+ if not a:+ return len(b)+ if not b:+ return len(a)+ if len(a) > len(b):+ a, b = b, a+ prev = list(range(len(a) + 1))+ for j, cb in enumerate(b, 1):+ curr = [j] + [0] * len(a)+ for i, ca in enumerate(a, 1):+ cost = 0 if ca == cb else 1+ curr[i] = min(curr[i - 1] + 1, prev[i] + 1, prev[i - 1] + cost)+ prev = curr+ return prev[-1]+++def _paths_related_as_typo(failed: str, success: str) -> bool:+ """Heuristic: are these two file paths plausibly the same target?++ Two paths are "related as typo recovery" if their basenames are+ identical or close in edit distance. Different basenames in the same+ directory (e.g. `state.rs` vs `lib.rs`) are NOT related — the matcher+ must reject them, otherwise unrelated reads get paired into bogus+ "File X does not exist, use Y" rules.+ """+ if not failed or not success or failed == success:+ return False+ a = failed.rsplit("/", 1)[-1].rsplit("\\", 1)[-1]+ b = success.rsplit("/", 1)[-1].rsplit("\\", 1)[-1]+ if not a or not b:+ return False+ if a == b:+ return True+ threshold = max(2, max(len(a), len(b)) // 3)+ return _levenshtein(a, b) <= threshold+++# Tokens that occur in many unrelated commands and don't, by themselves,+# suggest two commands are related retries.+_COMMAND_NOISE_TOKENS = frozenset(+ {+ "head",+ "tail",+ "cat",+ "grep",+ "awk",+ "sed",+ "sort",+ "uniq",+ "wc",+ "xargs",+ "find",+ }+)+++def _bash_first_binary(cmd: str) -> str | None:+ """Return the first binary name in a Bash command, or None.++ Strips a leading `source <venv> && ` prefix and skips over `VAR=value`+ environment-variable assignments before the binary. Used to gate+ command-recovery pairing: if two commands don't share a binary, they+ are not retries of each other.+ """+ s = cmd.strip()+ m = re.match(r"source\s+\S+\s*&&\s*(.*)", s, re.I)+ if m:+ s = m.group(1)+ for tok in s.split():+ if "=" in tok and tok.split("=", 1)[0].replace("_", "").isalnum():+ continue+ return tok+ return None+++def _bash_binaries_match(a: str, b: str) -> bool:+ """Treat two binaries as 'the same tool' for recovery purposes.++ Equal strings, basename equality (`ruff` vs `.venv/bin/ruff`), and+ short prefix-style versions (`python` vs `python3`) all qualify.+ Different tools (`grep` vs `find`) do not.+ """+ if a == b:+ return True+ a_base = a.rsplit("/", 1)[-1]+ 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+ ) <= 2:+ return True+ return False+++def _commands_related_as_retry(failed: str, success: str) -> bool:+ """Heuristic: is `success` plausibly a corrected retry of `failed`?++ Requires the same binary AND either:+ - low normalized edit distance (≤40% of max length), OR+ - at least one shared substantive token (length ≥ 5, not a flag,+ not a generic shell verb).++ The bar is conservative: noise like `grep <pattern A> <file A>` paired+ with `grep <pattern B> <file B>` shares the `grep` binary but no real+ arguments, and gets rejected. Genuine retries (extra flag, single+ arg edit) pass via the edit-distance path.+ """+ if not failed or not success or failed == success:+ return False+ bin_a = _bash_first_binary(failed)+ bin_b = _bash_first_binary(success)+ if not bin_a or not bin_b or not _bash_binaries_match(bin_a, bin_b):+ return False++ max_len = max(len(failed), len(success))+ if max_len > 0 and _levenshtein(failed, success) / max_len <= 0.40:+ return True++ def _substantive(cmd: str, binary: str) -> set[str]:+ out: set[str] = set()+ for tok in cmd.split():+ if len(tok) < 5 or tok.startswith("-") or tok == binary:+ continue+ if tok.lower() in _COMMAND_NOISE_TOKENS:+ continue+ out.add(tok)+ return out++ return bool(_substantive(failed, bin_a) & _substantive(success, bin_b))+++_FILE_X_DOES_NOT_EXIST_RE = re.compile(+ r"^File `([^`]+)` does not exist\. The correct path is `([^`]+)`\.$"+)+++def _drop_contradictions(patterns: list[ExtractedPattern]) -> list[ExtractedPattern]:+ """Remove A→B and B→A pairs from error_recovery patterns.++ When the matcher emits a "File X does not exist, use Y" rule and the+ inverse "File Y does not exist, use X" rule, both are likely the+ result of opposite-direction typos rather than a stable truth. Drop+ both rather than persisting contradictory advice.+ """+ forward: dict[tuple[str, str], int] = {}+ for idx, p in enumerate(patterns):+ if p.category != PatternCategory.ERROR_RECOVERY:+ continue+ m = _FILE_X_DOES_NOT_EXIST_RE.match(p.content)+ if not m:+ continue+ forward[(m.group(1), m.group(2))] = idx++ drop: set[int] = set()+ for (a, b), idx_ab in forward.items():+ idx_ba = forward.get((b, a))+ if idx_ba is not None:+ drop.add(idx_ab)+ drop.add(idx_ba)++ if not drop:+ return patterns+ return [p for i, p in enumerate(patterns) if i not in drop]++# =============================================================================# Traffic Learner# =============================================================================@@ -404,6 +578,13 @@ class TrafficLearner:if not patterns:return+ # Drop A→B / B→A contradictions among error_recovery patterns.+ # Both directions appearing with enough evidence usually means+ # opposite-direction typos in different sessions, not stable advice.+ patterns = _drop_contradictions(patterns)+ if not patterns:+ return+# Bucket patterns by project.if self._project_roots_cache is None:try:@@ -641,22 +822,26 @@ class TrafficLearner:elif tool == "Read":error_path = error_entry["input"].get("file_path", "")success_path = success_entry["input"].get("file_path", "")- if error_path and success_path and error_path != success_path:- content = (- f"File `{error_path}` does not exist. The correct path is `{success_path}`."- )- return ExtractedPattern(- category=PatternCategory.ERROR_RECOVERY,- content=content,- importance=0.7,- entity_refs=[success_path],- metadata={- "error_category": error_cat,- "tool": "Read",- "error_path": error_path,- "success_path": success_path,- },- )+ # Reject pairs whose basenames don't look like typos of each+ # other — different files in the same directory are unrelated+ # reads, not a recovery, and emitting a rule is actively wrong.+ if not _paths_related_as_typo(error_path, success_path):+ return None+ content = (+ f"File `{error_path}` does not exist. The correct path is `{success_path}`."+ )+ return ExtractedPattern(+ category=PatternCategory.ERROR_RECOVERY,+ content=content,+ importance=0.7,+ entity_refs=[success_path],+ metadata={+ "error_category": error_cat,+ "tool": "Read",+ "error_path": error_path,+ "success_path": success_path,+ },+ )elif tool in ("Grep", "Glob"):error_pattern = error_entry["input"].get("pattern", "")success_pattern = success_entry["input"].get("pattern", "")@@ -684,6 +869,13 @@ class TrafficLearner:if not failed_cmd or not success_cmd or failed_cmd == success_cmd:return None+ # Require the two commands to look like the same operation retried.+ # Without this, any failed Bash followed by any Bash success in the+ # last 5 calls becomes a "use Y instead of X" rule, even when X and+ # Y are unrelated (e.g. two grep calls with different needles and+ # different files).+ if not _commands_related_as_retry(failed_cmd, success_cmd):+ return None# Determine importance based on error categoryimportance = 0.7diff --git a/tests/test_memory/test_traffic_learner.py b/tests/test_memory/test_traffic_learner.pyindex abb337c3e..8168586e5 100644--- a/tests/test_memory/test_traffic_learner.py+++ b/tests/test_memory/test_traffic_learner.py@@ -15,10 +15,13 @@ from headroom.memory.traffic_learner import (PatternCategory,TrafficLearner,_classify_error,+ _commands_related_as_retry,+ _drop_contradictions,_is_error,_load_persisted_patterns_from_sqlite,_normalize_bash_for_hash,_parse_iso_timestamp,+ _paths_related_as_typo,_patterns_to_recommendations,_project_for_pattern,_refine_error_recovery,@@ -56,6 +59,104 @@ class TestErrorClassification:assert not _is_error("short")+# =============================================================================+# Recovery-pair relatedness heuristics+# =============================================================================+++class TestPathsRelatedAsTypo:+ def test_identical_basename_different_dir_is_typo(self):+ # Same file in two locations — common path-typo case.+ assert _paths_related_as_typo("/a/state.rs", "/b/state.rs")++ def test_close_basename_is_typo(self):+ assert _paths_related_as_typo("/a/staet.rs", "/a/state.rs")+ assert _paths_related_as_typo("/a/App.tsx", "/a/app.tsx")++ def test_unrelated_files_in_same_dir_rejected(self):+ # The motivating bug: state.rs and lib.rs are unrelated files,+ # not typos, and should never be paired into a recovery rule.+ assert not _paths_related_as_typo(+ "/src-tauri/src/state.rs", "/src-tauri/src/lib.rs"+ )+ assert not _paths_related_as_typo("/x/models.py", "/x/views.py")++ def test_empty_or_equal_paths_rejected(self):+ assert not _paths_related_as_typo("", "/a/x")+ assert not _paths_related_as_typo("/a/x", "")+ assert not _paths_related_as_typo("/a/x", "/a/x")+++class TestCommandsRelatedAsRetry:+ def test_python_to_python3_is_retry(self):+ assert _commands_related_as_retry("python test.py", "python3 test.py")++ def test_path_prefixed_binary_is_retry(self):+ assert _commands_related_as_retry("ruff check .", ".venv/bin/ruff check .")++ def test_extra_flag_is_retry(self):+ assert _commands_related_as_retry(+ "cargo build", "cargo build --release"+ )++ def test_different_binaries_rejected(self):+ assert not _commands_related_as_retry("grep -n foo bar.rs", "find . -name foo")++ def test_same_binary_unrelated_args_rejected(self):+ # The motivating bug: two grep calls sharing nothing but the+ # binary should not pair up. Different needles, different files.+ failed = (+ 'grep -nE "smoke|HEADROOM_SMOKE_TEST_TIMEOUT|smoke_test" '+ "/Users/x/src-tauri/src/tool_manager.rs 2>&1 | head -20"+ )+ success = (+ 'grep -nE "fn hf_hub_cache_dir|HF_HOME|HUGGINGFACE_HUB_CACHE" '+ "/Users/x/src-tauri/src/state.rs 2>&1 | head -10"+ )+ assert not _commands_related_as_retry(failed, success)++ def test_empty_or_equal_commands_rejected(self):+ assert not _commands_related_as_retry("", "ls")+ assert not _commands_related_as_retry("ls", "")+ assert not _commands_related_as_retry("ls", "ls")+++class TestDropContradictions:+ def _read_recovery(self, failed: str, success: str) -> ExtractedPattern:+ return ExtractedPattern(+ category=PatternCategory.ERROR_RECOVERY,+ content=f"File `{failed}` does not exist. The correct path is `{success}`.",+ importance=0.7,+ entity_refs=[success],+ metadata={"error_category": "file_not_found", "failed_path": failed},+ evidence_count=5,+ )++ def test_drops_inverse_pairs(self):+ a_to_b = self._read_recovery("/x/a.rs", "/x/b.rs")+ b_to_a = self._read_recovery("/x/b.rs", "/x/a.rs")+ keep = self._read_recovery("/x/c.rs", "/x/d.rs")+ cleaned = _drop_contradictions([a_to_b, b_to_a, keep])+ assert keep in cleaned+ assert a_to_b not in cleaned+ assert b_to_a not in cleaned++ def test_passthrough_when_no_inverse(self):+ a_to_b = self._read_recovery("/x/a.rs", "/x/b.rs")+ cleaned = _drop_contradictions([a_to_b])+ assert cleaned == [a_to_b]++ def test_only_filters_error_recovery_category(self):+ env_pattern = ExtractedPattern(+ category=PatternCategory.ENVIRONMENT,+ content="File `x` does not exist. The correct path is `y`.", # text alone+ importance=0.5,+ evidence_count=5,+ )+ cleaned = _drop_contradictions([env_pattern])+ assert cleaned == [env_pattern]++# =============================================================================# Traffic Learner Core Tests# =============================================================================