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(learn): derive loop signature from tool input identity, not display summary (#3461)
Nicholas Chaef6b9fc09e33a
diff --git a/headroom/learn/loops.py b/headroom/learn/loops.pyindex 3c48d0928..f2949fd18 100644--- a/headroom/learn/loops.py+++ b/headroom/learn/loops.py@@ -24,11 +24,19 @@ weight at all.from __future__ import annotations+import hashlib+import jsonimport refrom dataclasses import dataclass, fieldfrom .models import Recommendation, SessionData, ToolCall+# Upper bound on the signature body kept for fuzzy rule matching. Matches the+# width that ``input_summary`` previously imposed, so the majority-overlap rule+# in ``apply_loop_weighting`` keeps behaving exactly as it did before identity+# stopped going through that truncation.+_SIGNATURE_MATCH_LIMIT = 100+# Minimum repetitions of one signature before it counts as a loop. Three is the# smallest count that distinguishes a loop ("again, and again") from a one-off# retry ("that failed once, try once more") — matching the analyzer's existing@@ -60,7 +68,6 @@ _PAGINATION_RE = re.compile("|".join(_PAGINATION_PATTERNS), re.IGNORECASE)# Collapse any remaining bare integers so e.g. line numbers / byte offsets in# otherwise identical commands do not split a loop into singletons._INT_RE = re.compile(r"\b\d+\b")-_WS_RE = re.compile(r"\s+")@dataclass@@ -85,21 +92,108 @@ class LoopPattern:return "error-loop" if self.is_error_loop else "refetch-loop"+# Input fields that identify a call, per tool. ``ToolCall.input_summary`` reads+# the same fields but is a *display* helper — it cuts Bash commands at 100 chars+# and renders any tool missing from this table as ``str(input_data)[:80]``. Two+# distinct calls sharing a long prefix survive that truncation as equal strings,+# so identity is taken from the untruncated input here instead.+#+# Because identity no longer reads ``input_summary``, a field that distinguishes+# two calls has to be named here to count — adding one to the summary alone+# changes the display and leaves the grouping merged.+#+# Adding a *second* field to a tool listed here also moves it to the structured+# identity form in ``_identity_input``, which ``_PAGINATION_RE`` below is not+# written against — so a second field on ``bash``/``shell`` would stop+# re-fetch variants of one command collapsing.+_IDENTITY_FIELDS: dict[str, tuple[str, ...]] = {+ "bash": ("command",),+ "shell": ("command",),+ "read": ("file_path",),+ # A search is identified by pattern *and* path: the same pattern swept+ # across three service directories is three searches, not a loop. #3455+ # pins this on the display side; identity has to agree or one undoes the+ # other depending on merge order.+ "grep": ("pattern", "path"),+ "glob": ("pattern", "path"),+ "edit": ("file_path",),+ "write": ("file_path",),+}+++def _identity_input(tc: ToolCall) -> str:+ """Render a tool call's input in full, for identity comparison.++ Falls back to the whole input mapping — key-sorted so ordering cannot split+ a group — rather than to a truncated ``repr``. The fallback also covers a+ tool that *is* in :data:`_IDENTITY_FIELDS` but carries none of its fields:+ ``normalize_tool_name`` maps provider tools onto the builtin names without+ normalizing their input schema, and an empty identity would collapse every+ such call into one loop — the merge this function exists to prevent.+ """+ data = tc.input_data if isinstance(tc.input_data, dict) else {}+ fields = _IDENTITY_FIELDS.get(tc.name.lower())+ if fields is not None:+ parts = [str(data.get(field, "")) for field in fields]+ if any(parts):+ # A lone field is already unambiguous, and leaving it bare is what+ # the shell pagination normalization below matches against. Two or+ # more have to carry their own boundaries: joined on a space,+ # ("error in src", "logs") and ("error", "in src logs") are two+ # different searches rendering one string — and so one phantom loop.+ # Escaping stays off: ``\u00e9`` would reach _signature_tokens as a+ # token no recommendation can contain, diluting the majority overlap+ # apply_loop_weighting needs to credit the loop.+ return parts[0] if len(parts) == 1 else json.dumps(parts, ensure_ascii=False)+ try:+ return json.dumps(tc.input_data, sort_keys=True, default=repr, ensure_ascii=False)+ except TypeError:+ # Unorderable keys — not reachable from parsed JSON, but identity is+ # derived from files the user did not write, so it must not raise.+ return str(tc.input_data)++def _canonical_signature(tc: ToolCall) -> str:"""Collapse a tool call to a signature stable across re-fetch variants.For shell commands this strips pagination/limit fragments and bareintegers so output-limit variants of the same command map together.- For other tools the input summary is normalized on whitespace only.+ For other tools the identity input is normalized on whitespace only."""- raw = tc.input_summary.strip()+ raw = _identity_input(tc).strip()if tc.name.lower() in ("bash", "shell"):raw = _PAGINATION_RE.sub(" ", raw)raw = _INT_RE.sub("N", raw)- raw = _WS_RE.sub(" ", raw).strip().lower()+ # ``" ".join(split())`` collapses whitespace runs and strips, identically to+ # ``re.sub(r"\s+", " ", raw).strip()`` but without the regex engine walking the+ # whole input — the dominant cost now that the signature is the untruncated one.+ raw = " ".join(raw.split()).lower()return f"{tc.name.lower()}::{raw}"+def _group_key(signature: str) -> str:+ """Fixed-size grouping key for a canonical signature.++ The signature is lossless, so it can be as large as the tool input itself.+ Hashing keeps the grouping tables bounded regardless of input size; the+ digest is never surfaced, only used to bucket identical signatures.+ """+ return hashlib.blake2b(signature.encode("utf-8", "replace"), digest_size=16).hexdigest()+++def _fuzzy_signature(signature: str) -> str:+ """Bounded form of a canonical signature, for fuzzy rule matching.++ ``LoopPattern.signature`` is consumed only by :func:`_signature_tokens`,+ which requires a *majority* of its tokens to appear in a recommendation. An+ unbounded signature would put that threshold out of reach for large inputs+ and silently strip real loops of their measured-waste boost, so the stored+ value is capped. Identity still comes from the full signature above.+ """+ name, sep, body = signature.partition("::")+ return f"{name}{sep}{body[:_SIGNATURE_MATCH_LIMIT]}"++def _tokens(tc: ToolCall) -> int:"""Token estimate for a single call's output."""nbytes = tc.output_bytes or len(tc.output)@@ -158,22 +252,26 @@ def detect_loops(no conversation ran."""groups: dict[str, list[ToolCall]] = {}+ signatures: dict[str, str] = {}seen_ids: dict[str, set[str]] = {}for session in sessions:per_session: dict[str, list[ToolCall]] = {}for tc in session.tool_calls:- per_session.setdefault(_canonical_signature(tc), []).append(tc)+ sig = _canonical_signature(tc)+ key = _group_key(sig)+ signatures.setdefault(key, sig)+ per_session.setdefault(key, []).append(tc)# Merge each session's qualifying groups into the global view keyed by# signature so cross-session recurrence of the SAME loop accumulates.- for sig, calls in per_session.items():+ for key, calls in per_session.items():distinct = _without_replays(calls, set())if len(distinct) < min_occurrences:continue- bucket = groups.setdefault(sig, [])- bucket.extend(_without_replays(distinct, seen_ids.setdefault(sig, set())))+ bucket = groups.setdefault(key, [])+ bucket.extend(_without_replays(distinct, seen_ids.setdefault(key, set())))loops: list[LoopPattern] = []- for sig, calls in groups.items():+ for key, calls in groups.items():# No post-merge threshold re-check: every group here was seeded by a# session that cleared the bar on its own distinct calls, and merging# only ever adds.@@ -191,7 +289,7 @@ def detect_loops(loops.append(LoopPattern(tool=calls[0].name,- signature=sig,+ signature=_fuzzy_signature(signatures[key]),sample_input=calls[0].input_summary[:120],count=count,is_error_loop=is_error_loop,diff --git a/tests/test_learn/test_loop_weighting.py b/tests/test_learn/test_loop_weighting.pyindex 0ac01d5e4..789948f05 100644--- a/tests/test_learn/test_loop_weighting.py+++ b/tests/test_learn/test_loop_weighting.py@@ -25,7 +25,10 @@ from headroom.learn.fixtures import (refetch_loop_session,)from headroom.learn.loops import (+ _IDENTITY_FIELDS,_canonical_signature,+ _identity_input,+ _signature_tokens,apply_loop_weighting,detect_loops,)@@ -220,6 +223,342 @@ def _call(name: str, tool_call_id: str, input_data: dict, *, msg_index: int) ->)+class TestSignatureIsIdentityNotDisplay:+ """The loop key must be a full identity, not the truncated display summary.++ ``ToolCall.input_summary`` is documented as being "for display": it cuts Bash+ commands at 100 chars and renders any non-builtin tool as+ ``str(input_data)[:80]``. Grouping on it makes distinct calls that share a+ long prefix compare equal, so unrelated work is reported as one loop.+ """++ def test_distinct_custom_tool_calls_are_not_one_loop(self):+ # Five patches to five different files under one long shared prefix.+ prefix = "/home/user/worktrees/feature-branch-alpha/services/api/internal/handlers"+ calls = [+ _call(+ "apply_patch",+ f"call_{i}",+ {"input": f"*** Update File: {prefix}/handler_{i}.py\n@@\n-old\n+new\n"},+ msg_index=i,+ )+ for i in range(5)+ ]+ assert len({_canonical_signature(c) for c in calls}) == 5+ assert detect_loops([SessionData(session_id="s", tool_calls=calls)]) == []++ def test_distinct_long_bash_commands_are_not_one_loop(self):+ # Commands diverge only after the 100-char display cutoff.+ base = (+ "rg --no-heading --line-number --color never "+ "'TimeoutError|ValueError|KeyError' /srv/app/services/ingest/pipeline/"+ )+ calls = [+ _call("Bash", f"call_{i}", {"command": f"{base}module_{i}/handlers.py"}, msg_index=i)+ for i in range(4)+ ]+ assert len(calls[0].input_summary) < len(calls[0].input_data["command"])+ assert len({_canonical_signature(c) for c in calls}) == 4+ assert detect_loops([SessionData(session_id="s", tool_calls=calls)]) == []++ def test_pagination_variants_of_one_long_command_still_collapse(self):+ # The re-fetch collapse must survive the identity change: one command,+ # growing output limits, past the 100-char display cutoff.+ base = (+ "rg --no-heading --line-number --color never "+ "'TimeoutError|ValueError|KeyError' /srv/app/services/ingest/pipeline/handlers.py"+ )+ calls = [+ _call("Bash", f"call_{i}", {"command": f"{base} | head -{50 * (i + 1)}"}, msg_index=i)+ for i in range(4)+ ]+ assert len({_canonical_signature(c) for c in calls}) == 1+ loops = detect_loops([SessionData(session_id="s", tool_calls=calls)])+ assert len(loops) == 1+ assert loops[0].count == 4+++class TestSignatureIsSizeIndependent:+ """Detection quality must not degrade as a tool input gets larger.++ The identity key is lossless, so it grows with the input. Everything derived+ from it must stay size-independent, because the only consumer of+ ``LoopPattern.signature`` — ``_signature_tokens`` in ``apply_loop_weighting``+ — requires a *majority* of the signature's tokens to appear in a+ recommendation. A signature that grows without bound pushes that threshold+ out of reach, and a real loop silently loses its measured-waste boost.+ """++ BASE = "pytest tests/test_learn/test_loops.py -v --maxfail 1 --tb=short"+ REC = (+ "Avoid rerunning pytest on tests/test_learn/test_loops.py with"+ " -v --maxfail 1 --tb=short; reuse the prior run."+ )++ def _command(self, extra_args: int) -> str:+ tail = " ".join(f"--unrelated-opt-{i}=value-{i}" for i in range(extra_args))+ return f"{self.BASE} {tail}".strip()++ def _loop(self, extra_args: int):+ calls = [+ _call("Bash", f"call_b{i}", {"command": self._command(extra_args)}, msg_index=i)+ for i in range(5)+ ]+ return detect_loops([SessionData(session_id="s", tool_calls=calls)])++ @pytest.mark.parametrize("extra_args", [0, 20, 200, 2000])+ def test_weighting_holds_however_long_the_input_is(self, extra_args):+ loops = self._loop(extra_args)+ rec = Recommendation(+ target=RecommendationTarget.CONTEXT_FILE,+ section="Tooling",+ content=self.REC,+ estimated_tokens_saved=100,+ )++ apply_loop_weighting([rec], loops)++ assert rec.is_loop_guardrail is True+ assert rec.estimated_tokens_saved == loops[0].wasted_tokens++ def test_signature_size_does_not_track_input_size(self):+ big = self._loop(500)[0].signature+ ten_times_bigger = self._loop(5000)[0].signature++ assert big == ten_times_bigger+ assert len(big) < len(self._command(500)) // 10++ def test_oversized_inputs_are_still_told_apart(self):+ """Bounding the signature must not reintroduce the merge it was added to survive.++ Identity is the *unbounded* key; only the stored signature is capped. Two+ calls sharing a long prefix must therefore stay distinct even when their+ bounded signatures are identical.+ """+ shared = "*** Update File: /repo/services/ingest/handler.py\n" + "-old\n+new\n" * 400+ calls = [+ _call("apply_patch", f"call_p{i}", {"input": f"{shared}# variant {i}"}, msg_index=i)+ for i in range(5)+ ]++ assert len({_canonical_signature(c) for c in calls}) == 5+ assert detect_loops([SessionData(session_id="s", tool_calls=calls)]) == []+++class TestGroupingInvariants:+ """Properties that must hold for any input, not just the pinned examples."""++ def _mixed_calls(self) -> list[ToolCall]:+ return [+ _call("Read", "call_r0", {"file_path": "/repo/a.py"}, msg_index=0),+ _call("Read", "call_r1", {"file_path": "/repo/a.py"}, msg_index=1),+ _call("Read", "call_r2", {"file_path": "/repo/a.py"}, msg_index=2),+ _call("Bash", "call_b0", {"command": "rg alpha /repo"}, msg_index=3),+ _call("Bash", "call_b1", {"command": "rg alpha /repo"}, msg_index=4),+ _call("Bash", "call_b2", {"command": "rg alpha /repo"}, msg_index=5),+ _call("Grep", "call_g0", {"pattern": "beta"}, msg_index=6),+ ]++ def test_detection_does_not_depend_on_call_order(self):+ calls = self._mixed_calls()+ shuffled = list(reversed(calls))++ def summarize(cs):+ loops = detect_loops([SessionData(session_id="s", tool_calls=cs)])+ return sorted((lp.tool, lp.count, lp.wasted_tokens) for lp in loops)++ assert summarize(shuffled) == summarize(calls)++ def test_whitespace_variants_share_one_signature(self):+ """Whitespace normalization survives the split/join implementation.++ The collapse is done with ``" ".join(str.split())`` rather than a regex+ for speed; this pins that tabs, newlines and repeated spaces still fold+ to the same signature.+ """+ variants = [+ "rg --line-number 'def handler' /repo/app.py",+ "rg --line-number\t'def handler' /repo/app.py",+ " rg --line-number\n'def handler' /repo/app.py ",+ ]+ calls = [+ _call("Bash", f"call_w{i}", {"command": cmd}, msg_index=i)+ for i, cmd in enumerate(variants)+ ]++ assert len({_canonical_signature(c) for c in calls}) == 1+++class TestIdentityFallsBackWhenTheSchemaIsUnknown:+ """``_identity_input`` must render the input in full, as it documents.++ ``_IDENTITY_FIELDS`` guesses which field identifies a call from the tool's+ name. ``normalize_tool_name`` maps provider tools onto those same names+ without normalizing their input schema, so the guess can miss every field —+ at which point an empty identity collapses unrelated calls into one loop,+ the exact failure this signature change exists to prevent.+ """++ def test_a_builtin_name_carrying_a_foreign_schema_stays_distinct(self):+ # `codebase_search` / `search_text` normalize to Grep but carry `query`.+ calls = [+ _call("Grep", f"call_{i}", {"query": f"distinct query number {i}"}, msg_index=i)+ for i in range(5)+ ]++ assert len({_canonical_signature(c) for c in calls}) == 5+ assert detect_loops([SessionData(session_id="s", tool_calls=calls)]) == []++ def test_a_partially_matching_schema_still_uses_the_known_field(self):+ # `file_path` is present, so identity comes from it and the rest is+ # ignored — the pinned builtin behaviour is unchanged.+ calls = [+ _call("Read", f"call_{i}", {"file_path": "/repo/a.md", "offset": i}, msg_index=i)+ for i in range(3)+ ]++ loops = detect_loops([SessionData(session_id="s", tool_calls=calls)])++ assert [lp.count for lp in loops] == [3]++ @pytest.mark.parametrize("payload", [None, [], "raw string", {1: "a", "b": 2}])+ def test_an_unexpected_input_shape_does_not_raise(self, payload):+ # Identity is derived during a scan of files the user did not write;+ # a malformed input must not take down `headroom learn`.+ call = _call("Bash", "call_0", payload, msg_index=0)++ assert _canonical_signature(call).startswith("bash::")+++class TestSearchIdentityIncludesPath:+ """The same pattern in different trees is three searches, not a loop.++ Identity now comes from the input fields rather than ``input_summary``, so+ every field that distinguishes a search has to be named here — a pattern+ alone would merge a sweep across three service directories into one loop+ worth 20,000 tokens of phantom waste. Pins the same behavior #3455 pins on+ the display side, on the path identity actually reads.+ """++ def _search(self, name: str, pattern: str, path: str, index: int) -> ToolCall:+ return _call(name, f"{name}-{index}", {"pattern": pattern, "path": path}, msg_index=index)++ @pytest.mark.parametrize(("name", "pattern"), [("Grep", "TimeoutError"), ("Glob", "**/*.py")])+ def test_distinct_paths_are_distinct_signatures(self, name: str, pattern: str):+ calls = [+ self._search(name, pattern, path, i)+ for i, path in enumerate(("/srv/ingest", "/srv/billing", "/srv/web"))+ ]++ assert len({_canonical_signature(c) for c in calls}) == 3++ @pytest.mark.parametrize(("name", "pattern"), [("Grep", "TimeoutError"), ("Glob", "**/*.py")])+ def test_the_same_pattern_in_three_trees_is_not_a_loop(self, name: str, pattern: str):+ calls = [+ self._search(name, pattern, path, i)+ for i, path in enumerate(("/srv/ingest", "/srv/billing", "/srv/web"))+ ]++ assert detect_loops([SessionData(session_id=name, tool_calls=calls)]) == []++ @pytest.mark.parametrize(("name", "pattern"), [("Grep", "TimeoutError"), ("Glob", "**/*.py")])+ def test_repeating_one_search_is_still_a_loop(self, name: str, pattern: str):+ # Adding path to identity must not stop a real re-search loop counting.+ calls = [self._search(name, pattern, "/srv/ingest", i) for i in range(3)]++ assert detect_loops([SessionData(session_id=name, tool_calls=calls)])[0].count == 3++ def test_a_search_without_a_path_still_has_identity(self, name: str = "Grep"):+ # Claude Code omits path when searching the cwd; identity must not+ # collapse to the empty fallback, nor split from a differing path.+ rooted = _call(name, "g-1", {"pattern": "TimeoutError", "path": "/srv"}, msg_index=1)+ bare = _call(name, "g-0", {"pattern": "TimeoutError"}, msg_index=0)++ assert _canonical_signature(bare) != _canonical_signature(rooted)+++class TestIdentityPreservesFieldBoundaries:+ """Two identity fields must not run together into one flat string.++ ``pattern`` and ``path`` joined on a space lose the boundary between them:+ ``("error in src", "logs")`` and ``("error", "in src logs")`` are different+ searches that render the same text, and merged into one loop they report+ 20,000 tokens of waste nobody spent. Only tools with more than one identity+ field need the structured form, so Bash keeps the bare command its+ pagination normalization is written against.+ """++ def _search(self, pattern: str, path: str, index: int) -> ToolCall:+ return _call("Grep", f"g-{index}", {"pattern": pattern, "path": path}, msg_index=index)++ # Same characters in the same order, split between the fields three ways.+ _AMBIGUOUS = (("error in src", "logs"), ("error in", "src logs"), ("error", "in src logs"))++ def test_field_splits_of_one_string_are_distinct_signatures(self):+ calls = [+ self._search(pattern, path, i) for i, (pattern, path) in enumerate(self._AMBIGUOUS)+ ]++ assert len({_canonical_signature(c) for c in calls}) == 3++ def test_field_splits_of_one_string_are_not_a_loop(self):+ calls = [+ self._search(pattern, path, i) for i, (pattern, path) in enumerate(self._AMBIGUOUS)+ ]++ assert detect_loops([SessionData(session_id="s", tool_calls=calls)]) == []++ def test_a_path_containing_spaces_still_loops_when_repeated(self):+ # Spaces are not themselves a reason to split a group.+ calls = [self._search("error in src", "/srv/my logs", i) for i in range(3)]++ assert detect_loops([SessionData(session_id="s", tool_calls=calls)])[0].count == 3++ @pytest.mark.parametrize(+ ("pattern", "path"),+ [('a", "b', "c"), ("a", '", "b", "c'), ("a\\", '"b'), ('["a"]', "b")],+ )+ def test_a_pattern_that_mimics_the_encoding_stays_distinct(self, pattern: str, path: str):+ # Whatever the encoding is, a search cannot forge another search's+ # identity by embedding the separator in its own pattern.+ mimic = self._search(pattern, path, 0)+ plain = self._search("a", "b", 1)++ assert _canonical_signature(mimic) != _canonical_signature(plain)++ def test_pagination_variants_of_one_command_still_collapse(self):+ # The structured form must not reach single-field tools: Bash identity+ # stays the bare command, so _PAGINATION_RE keeps matching it.+ base = 'rg --no-heading "TimeoutError" /srv/app/services/ingest/pipeline/handlers.py'+ calls = [+ _call("Bash", f"call_{i}", {"command": f"{base} | head -{50 * (i + 1)}"}, msg_index=i)+ for i in range(4)+ ]++ assert len({_canonical_signature(c) for c in calls}) == 1++ def test_a_non_ascii_pattern_does_not_leak_escape_tokens(self):+ # apply_loop_weighting requires a *majority* of a signature's tokens to+ # appear in a recommendation. An escape artifact is a token no+ # recommendation can ever contain, so it only raises that bar and costs+ # a real loop its measured-waste boost.+ call = self._search("café latte", "/srv/menu", 0)++ assert "u00e9" not in _signature_tokens(_canonical_signature(call))++ @pytest.mark.parametrize(+ ("name", "field"),+ [(n, f[0]) for n, f in sorted(_IDENTITY_FIELDS.items()) if len(f) == 1],+ )+ def test_a_single_field_tool_keeps_its_bare_value(self, name: str, field: str):+ # Guards the carve-out above: give one of these tools a second identity+ # field and it silently moves to the structured form, where+ # _PAGINATION_RE no longer matches the command it was written for.+ call = _call(name, "c-0", {field: "a b c"}, msg_index=0)++ assert _identity_input(call) == "a b c"++def _mixed_calls() -> list[ToolCall]:return [_call("Read", "call_r0", {"file_path": "/repo/a.py"}, msg_index=0),