Skip to content

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.

fix(transforms): tolerate malformed assistant tool calls in ContentRouter.apply (#4459)

HetCreepcddd2fd60ed7
diff --git a/headroom/transforms/content_router.py b/headroom/transforms/content_router.py
index 038fe1529..88c2eef13 100644
--- a/headroom/transforms/content_router.py
+++ b/headroom/transforms/content_router.py
@@ -596,6 +596,24 @@ def _external_compressor_matches(descriptor: CompressorDescriptor, content_mime:
return any(d in ("*", "*/*") or d == type_wildcard for d in declared)
+def _malformed_tool_call_ids(messages: list[dict[str, Any]]) -> set[str]:
+ """Ids of OpenAI tool calls whose ``function`` is not an object.
+
+ Such a call has no readable tool name, so its result cannot be classified;
+ it is passed through like a verbatim-excluded tool rather than compressed.
+ """
+ ids: set[str] = set()
+ for msg in messages:
+ if msg.get("role") != "assistant":
+ continue
+ for tc in msg.get("tool_calls") or []:
+ if isinstance(tc, dict) and not isinstance(tc.get("function"), dict):
+ tc_id = tc.get("id")
+ if isinstance(tc_id, str) and tc_id:
+ ids.add(tc_id)
+ return ids
+
+
def _tool_call_args_text(raw: Any) -> str:
"""Compact, query-usable text from a tool call's args.
@@ -5832,7 +5850,9 @@ class ContentRouter(Transform):
for tc in msg.get("tool_calls") or []:
if isinstance(tc, dict):
tc_id = tc.get("id", "")
- fn = tc.get("function", {})
+ fn = tc.get("function")
+ if not isinstance(fn, dict): # "function": null / non-object
+ continue
name = fn.get("name", "")
call_args = fn.get("arguments")
if name:
@@ -6184,6 +6204,9 @@ class ContentRouter(Transform):
for tool_id, name in tool_name_map.items()
if is_tool_excluded(name, exclude_tools)
}
+ # A call with no readable name keeps its result untouched, as it was
+ # before such calls stopped raising out of apply().
+ excluded_tool_ids |= _malformed_tool_call_ids(messages)
# CCR-retrieve tool IDs, precomputed once (mirrors excluded_tool_ids
# above, rather than calling is_tool_excluded() per message/block). A
@@ -6579,7 +6602,7 @@ class ContentRouter(Transform):
if role == "tool":
if tool_call_id in excluded_tool_ids:
tool_name = tool_name_map.get(tool_call_id, "")
- if tool_name and is_tool_excluded(tool_name, DEFAULT_VERBATIM_EXCLUDE_TOOLS):
+ if not tool_name or is_tool_excluded(tool_name, DEFAULT_VERBATIM_EXCLUDE_TOOLS):
result_slots[i] = message
transforms_applied.append("router:excluded:tool")
route_counts["excluded_tool"] += 1
@@ -7753,7 +7776,7 @@ class ContentRouter(Transform):
continue
if tool_use_id in excluded_tool_ids:
tool_name = tool_name_map.get(tool_use_id, "") if tool_name_map else ""
- if tool_name and is_tool_excluded(tool_name, DEFAULT_VERBATIM_EXCLUDE_TOOLS):
+ if not tool_name or is_tool_excluded(tool_name, DEFAULT_VERBATIM_EXCLUDE_TOOLS):
new_blocks.append(block)
transforms_applied.append("router:excluded:tool")
if route_counts is not None:
diff --git a/headroom/transforms/read_lifecycle.py b/headroom/transforms/read_lifecycle.py
index 66e8672e4..71baa8e86 100644
--- a/headroom/transforms/read_lifecycle.py
+++ b/headroom/transforms/read_lifecycle.py
@@ -181,7 +181,9 @@ class ReadLifecycleManager:
if not isinstance(tc, dict):
continue
tc_id = tc.get("id", "")
- func = tc.get("function", {})
+ func = tc.get("function")
+ if not isinstance(func, dict): # "function": null / non-object
+ continue
name = func.get("name", "")
if not tc_id or not name:
continue
@@ -191,11 +193,15 @@ class ReadLifecycleManager:
limit = None
try:
args = json.loads(func.get("arguments", "{}"))
+ except (json.JSONDecodeError, TypeError):
+ args = None
+ if isinstance(args, dict): # arguments may decode to a list/str/number
file_path = args.get("file_path") or args.get("path")
offset = args.get("offset")
limit = args.get("limit")
- except (json.JSONDecodeError, TypeError):
- pass
+ # file_path is a dict key downstream; an unhashable value must not reach it.
+ if not isinstance(file_path, str):
+ file_path = None
metadata[tc_id] = (name, file_path, offset, limit)
# Anthropic format: content blocks with type=tool_use
@@ -218,6 +224,8 @@ class ReadLifecycleManager:
file_path = inp.get("file_path") or inp.get("path")
offset = inp.get("offset")
limit = inp.get("limit")
+ if not isinstance(file_path, str):
+ file_path = None
metadata[tc_id] = (name, file_path, offset, limit)
return metadata
diff --git a/headroom/transforms/read_maturation.py b/headroom/transforms/read_maturation.py
index cd40c4d14..40d3e6bad 100644
--- a/headroom/transforms/read_maturation.py
+++ b/headroom/transforms/read_maturation.py
@@ -284,15 +284,21 @@ class ReadMaturationManager:
for tc in msg.get("tool_calls", []) or []:
if not isinstance(tc, dict):
continue
- func = tc.get("function", {})
+ func = tc.get("function")
+ if not isinstance(func, dict): # "function": null / non-object
+ continue
name = func.get("name", "")
if name not in _TOUCH_TOOLS:
continue
try:
args = json.loads(func.get("arguments", "{}"))
- except (ValueError, TypeError):
- args = {}
+ except (ValueError, TypeError): # not JSON: unknown file
+ continue
+ if not isinstance(args, dict): # list/str/number: unknown file
+ continue
fp = args.get("file_path") or args.get("path") or ""
+ if not isinstance(fp, str): # unknown file: never mature this call
+ continue
if fp:
act.file_last_touch[fp] = turn
if name in _READ_TOOLS:
@@ -306,8 +312,12 @@ class ReadMaturationManager:
name = b.get("name", "")
if name not in _TOUCH_TOOLS:
continue
- inp = b.get("input") or {}
+ inp = b.get("input")
+ if not isinstance(inp, dict): # unknown file: never mature this call
+ continue
fp = inp.get("file_path") or inp.get("path") or ""
+ if not isinstance(fp, str): # unknown file: never mature this call
+ continue
if fp:
act.file_last_touch[fp] = turn
if name in _READ_TOOLS:
diff --git a/tests/test_transforms/test_malformed_tool_calls.py b/tests/test_transforms/test_malformed_tool_calls.py
new file mode 100755
index 000000000..985a30e0b
--- /dev/null
+++ b/tests/test_transforms/test_malformed_tool_calls.py
@@ -0,0 +1,97 @@
+"""Malformed assistant tool calls must not raise out of ContentRouter.apply.
+
+A request body can carry ``"function": null``, ``arguments`` that decode to a
+non-object, or a ``tool_use`` whose ``file_path`` is not a string. Those used to
+raise out of ``ReadLifecycleManager`` / ``ReadMaturationManager`` /
+``ContentRouter._build_tool_name_map``; ``TransformPipeline.apply`` re-raised
+and counted a circuit-breaker failure, so three such requests in a row switched
+compression off for the breaker cooldown.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+from headroom.config import ReadLifecycleConfig, ReadMaturationConfig
+from headroom.tokenizers import EstimatingTokenCounter
+from headroom.transforms.content_router import ContentRouter
+from headroom.transforms.read_lifecycle import ReadLifecycleManager
+from headroom.transforms.read_maturation import ReadMaturationManager
+
+
+def _openai(function: object) -> dict:
+ return {
+ "role": "assistant",
+ "content": None,
+ "tool_calls": [{"id": "c1", "type": "function", "function": function}],
+ }
+
+
+def _anthropic(tool_input: object) -> dict:
+ return {
+ "role": "assistant",
+ "content": [{"type": "tool_use", "id": "c1", "name": "Read", "input": tool_input}],
+ }
+
+
+MALFORMED = {
+ "function_null": _openai(None),
+ "function_string": _openai("Read"),
+ "arguments_list": _openai({"name": "Read", "arguments": "[1]"}),
+ "arguments_string": _openai({"name": "Read", "arguments": '"s"'}),
+ "arguments_number": _openai({"name": "Read", "arguments": "5"}),
+ "arguments_invalid_json": _openai({"name": "Read", "arguments": "{"}),
+ "arguments_file_path_list": _openai({"name": "Read", "arguments": '{"file_path": ["a"]}'}),
+ "tool_use_file_path_list": _anthropic({"file_path": ["a"]}),
+ "tool_use_path_dict": _anthropic({"path": {"a": 1}}),
+ "tool_use_input_list": _anthropic([1]),
+}
+
+
+# Compressible log text: a well-formed call to an unknown tool would be compressed,
+# so an unchanged result shows the malformed call's output was left alone.
+RESULT = "".join(f"2026-10-24 INFO worker {i % 7} processed item {i} ok\n" for i in range(400))
+
+
+def _conversation(assistant: dict, quiet_turns: int = 0) -> list[dict]:
+ result = RESULT
+ if "tool_calls" in assistant:
+ tool_msg = {"role": "tool", "tool_call_id": "c1", "content": result}
+ else:
+ tool_msg = {
+ "role": "user",
+ "content": [{"type": "tool_result", "tool_use_id": "c1", "content": result}],
+ }
+ messages = [{"role": "user", "content": "go"}, assistant, tool_msg]
+ for _ in range(quiet_turns):
+ messages += [{"role": "assistant", "content": "ok"}, {"role": "user", "content": "next"}]
+ return messages
+
+
+def _tool_result_text(messages: list[dict]) -> str:
+ content = messages[2]["content"]
+ return content if isinstance(content, str) else content[0]["content"]
+
+
[email protected]("assistant", MALFORMED.values(), ids=MALFORMED.keys())
+def test_read_lifecycle_tolerates_malformed_tool_calls(assistant: dict) -> None:
+ messages = _conversation(assistant)
+ result = ReadLifecycleManager(ReadLifecycleConfig(enabled=True)).apply(messages)
+ assert result.messages == messages
+
+
[email protected]("assistant", MALFORMED.values(), ids=MALFORMED.keys())
+def test_read_maturation_tolerates_malformed_tool_calls(assistant: dict) -> None:
+ # Enough quiet turns that a well-formed Read would mature; a call whose file
+ # cannot be read from its arguments must never be matured.
+ messages = _conversation(assistant, quiet_turns=8)
+ result = ReadMaturationManager(ReadMaturationConfig(enabled=True)).apply(messages)
+ assert _tool_result_text(result.messages) == RESULT
+
+
[email protected]("assistant", MALFORMED.values(), ids=MALFORMED.keys())
+def test_content_router_apply_tolerates_malformed_tool_calls(assistant: dict) -> None:
+ messages = _conversation(assistant)
+ result = ContentRouter().apply(messages, EstimatingTokenCounter())
+ assert len(result.messages) == len(messages)
+ assert _tool_result_text(result.messages) == RESULT