mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(transforms): leave results of unreadable tool calls untouched
A tool call whose "function" is not an object has no readable name, so its result could not be classified and was compressed like any other output (an 18,690-char log result became 103 chars). Pass such results through like a verbatim-excluded tool, which is what happened before these calls stopped raising out of apply(). Read maturation now skips a call whose file cannot be read from its arguments (non-object arguments or input, non-string path) instead of recording it under an empty path, where quiet turns matured it. The tests assert the tool result is unchanged, and the maturation test runs eight quiet turns. Refs #4458 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
e310d83c7c
commit
42ddb602f8
@@ -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.
|
||||
|
||||
@@ -6142,6 +6160,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
|
||||
@@ -6536,7 +6557,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
|
||||
@@ -7665,7 +7686,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:
|
||||
|
||||
@@ -294,11 +294,11 @@ class ReadMaturationManager:
|
||||
args = json.loads(func.get("arguments", "{}"))
|
||||
except (ValueError, TypeError):
|
||||
args = {}
|
||||
if not isinstance(args, dict): # arguments may decode to a list/str/number
|
||||
args = {}
|
||||
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): # used as a dict key below
|
||||
fp = ""
|
||||
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:
|
||||
@@ -313,11 +313,11 @@ class ReadMaturationManager:
|
||||
if name not in _TOUCH_TOOLS:
|
||||
continue
|
||||
inp = b.get("input")
|
||||
if not isinstance(inp, dict):
|
||||
inp = {}
|
||||
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): # used as a dict key below
|
||||
fp = ""
|
||||
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:
|
||||
|
||||
@@ -47,8 +47,13 @@ MALFORMED = {
|
||||
}
|
||||
|
||||
|
||||
def _conversation(assistant: dict) -> list[dict]:
|
||||
result = "line of output\n" * 200
|
||||
# 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:
|
||||
@@ -56,7 +61,15 @@ def _conversation(assistant: dict) -> list[dict]:
|
||||
"role": "user",
|
||||
"content": [{"type": "tool_result", "tool_use_id": "c1", "content": result}],
|
||||
}
|
||||
return [{"role": "user", "content": "go"}, assistant, tool_msg]
|
||||
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"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("assistant", MALFORMED.values(), ids=MALFORMED.keys())
|
||||
@@ -68,8 +81,11 @@ def test_read_lifecycle_tolerates_malformed_tool_calls(assistant: dict) -> None:
|
||||
|
||||
@pytest.mark.parametrize("assistant", MALFORMED.values(), ids=MALFORMED.keys())
|
||||
def test_read_maturation_tolerates_malformed_tool_calls(assistant: dict) -> None:
|
||||
messages = _conversation(assistant)
|
||||
ReadMaturationManager(ReadMaturationConfig(enabled=True)).apply(messages)
|
||||
# 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
|
||||
|
||||
|
||||
@pytest.mark.parametrize("assistant", MALFORMED.values(), ids=MALFORMED.keys())
|
||||
@@ -77,3 +93,4 @@ def test_content_router_apply_tolerates_malformed_tool_calls(assistant: dict) ->
|
||||
messages = _conversation(assistant)
|
||||
result = ContentRouter().apply(messages, EstimatingTokenCounter())
|
||||
assert len(result.messages) == len(messages)
|
||||
assert _tool_result_text(result.messages) == RESULT
|
||||
|
||||
Reference in New Issue
Block a user