mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-24 19:52:53 +08:00
fix(proxy): don't take Claude Code's "Tool loaded." note as the user query (#4714)
## Description Claude Code answers a ToolSearch call with `tool_reference` blocks plus a "Tool loaded." text block. `extract_user_query` takes the newest user text, so after an MCP tool is loaded the relevance query is "Tool loaded." and the question never reaches SmartCrusher. It's the same kind of bug as #4628 (`<system-reminder>`). Observed on Claude Code 2.1.319. ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) ## Changes Made - In a turn whose tool_result carries `tool_reference` blocks, drop the exact "Tool loaded." text. Other text in that turn still counts, and "Tool loaded." outside such a turn is unchanged. - The `tool_reference` check is the stable signal; the exact string follows Claude Code's current wording. Happy to drop all text in such a turn instead if you prefer. ## Testing - [x] New tests: ToolSearch turn, ToolSearch turn with real user text, plain "Tool loaded." text. The first two fail without the fix. - [x] `ruff check`, `ruff format --check`; tests for `extract_user_query` callers pass. Fork CI: green. ### Test Output ``` $ pytest -q tests/test_utils.py 38 passed in 0.33s $ pytest -q tests/test_utils.py -k "tool_search or tool_loaded" 3 passed, 35 deselected in 0.18s $ ruff check headroom/utils.py tests/test_utils.py && ruff format --check headroom/utils.py tests/test_utils.py All checks passed! 2 files already formatted ``` ## Real Behavior Proof - Environment: macOS arm64, Claude Code (Sonnet) through `headroom proxy` (PyPI 0.43.1 vs source), an MCP tool returning 100 of this repo's issues. - Exact command / steps: 12 questions "Who opened issue #N, and is it open or closed?"; scripts and answers: https://github.com/cherylaurelia/headroom-demo/tree/main/bench - Observed result: the logged query was "Tool loaded." on 0.43.1, and the question with this fix. Accuracy 2/12 → 12/12 together with the SmartCrusher fix in `fix/keep-query-matches` (2/12 with that fix alone). - Not tested: agents other than Claude Code. ## Runtime Rollout Safety - Rollout-managed feature(s): n/a - Minimum rollout channel: n/a - Stable/default behavior changed: only for ToolSearch result turns - Kill switch / disable path: n/a - Unsafe override required: no - Qualification impact: none - Rollback path: revert ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] I did **not** edit `CHANGELOG.md` Written with Claude Code, which also ran the tests and measurements above. <!-- devin-review-badge-begin --> --- <a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4714" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a> <!-- devin-review-badge-end --> Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
+22
-1
@@ -120,6 +120,25 @@ def _strip_system_reminders(text: str) -> str:
|
||||
return "".join(parts).strip()
|
||||
|
||||
|
||||
_TOOL_LOADED_NOTE = "Tool loaded."
|
||||
|
||||
|
||||
def _is_tool_search_load(content: list[Any]) -> bool:
|
||||
"""A tool_result turn answering Claude Code's ToolSearch: its
|
||||
``tool_reference`` blocks come with a "Tool loaded." text block that is a
|
||||
harness note, not the user's question."""
|
||||
return any(
|
||||
isinstance(block, dict)
|
||||
and block.get("type") == "tool_result"
|
||||
and isinstance(block.get("content"), list)
|
||||
and any(
|
||||
isinstance(ref, dict) and ref.get("type") == "tool_reference"
|
||||
for ref in block["content"]
|
||||
)
|
||||
for block in content
|
||||
)
|
||||
|
||||
|
||||
def extract_user_query(
|
||||
messages: list[dict[str, Any]], *, latest_user_turn_only: bool = False
|
||||
) -> str:
|
||||
@@ -146,12 +165,14 @@ def extract_user_query(
|
||||
if user_text:
|
||||
return user_text
|
||||
if isinstance(content, list):
|
||||
# Drop only the ToolSearch note; other text in that turn still counts.
|
||||
load_note = _TOOL_LOADED_NOTE if _is_tool_search_load(content) else None
|
||||
user_text = "\n".join(
|
||||
text
|
||||
for block in content
|
||||
if isinstance(block, dict) and block.get("type") == "text"
|
||||
for text in (_strip_system_reminders(str(block.get("text", ""))),)
|
||||
if text
|
||||
if text and text != load_note
|
||||
)
|
||||
if user_text:
|
||||
return user_text
|
||||
|
||||
@@ -142,6 +142,62 @@ def test_extract_user_query_latest_user_turn_only() -> None:
|
||||
assert utils.extract_user_query(messages, latest_user_turn_only=True) == ""
|
||||
|
||||
|
||||
def test_extract_user_query_skips_tool_search_load_note() -> None:
|
||||
"""Claude Code answers a ToolSearch call with ``tool_reference`` blocks plus a
|
||||
"Tool loaded." text block; that note is not the user's question."""
|
||||
messages = [
|
||||
{"role": "user", "content": "Who opened issue #4475?"},
|
||||
{"role": "assistant", "content": [{"type": "tool_use", "id": "t1", "name": "ToolSearch"}]},
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": "tool_result",
|
||||
"tool_use_id": "t1",
|
||||
"content": [{"type": "tool_reference", "tool_name": "mcp__gh__list_issues"}],
|
||||
},
|
||||
{"type": "text", "text": "Tool loaded."},
|
||||
],
|
||||
},
|
||||
{"role": "assistant", "content": [{"type": "tool_use", "id": "t2", "name": "list"}]},
|
||||
{
|
||||
"role": "user",
|
||||
"content": [{"type": "tool_result", "tool_use_id": "t2", "content": "[]"}],
|
||||
},
|
||||
]
|
||||
|
||||
assert utils.extract_user_query(messages) == "Who opened issue #4475?"
|
||||
assert utils.extract_user_query(messages, latest_user_turn_only=True) == ""
|
||||
|
||||
|
||||
def test_extract_user_query_keeps_user_text_beside_tool_search_load() -> None:
|
||||
"""Only the "Tool loaded." note is dropped; real text in that turn still counts."""
|
||||
messages = [
|
||||
{"role": "user", "content": "older question"},
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": "tool_result",
|
||||
"tool_use_id": "t1",
|
||||
"content": [{"type": "tool_reference", "tool_name": "mcp__gh__list_issues"}],
|
||||
},
|
||||
{"type": "text", "text": "Tool loaded."},
|
||||
{"type": "text", "text": "only open issues please"},
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
assert utils.extract_user_query(messages) == "only open issues please"
|
||||
|
||||
|
||||
def test_extract_user_query_keeps_tool_loaded_text_without_tool_reference() -> None:
|
||||
"""Outside a ToolSearch turn, "Tool loaded." is ordinary user text."""
|
||||
messages = [{"role": "user", "content": [{"type": "text", "text": "Tool loaded."}]}]
|
||||
|
||||
assert utils.extract_user_query(messages) == "Tool loaded."
|
||||
|
||||
|
||||
def test_extract_user_query_latest_turn_only_agrees_on_normal_turns() -> None:
|
||||
"""With text present on the newest user turn, the flag changes nothing."""
|
||||
messages = [
|
||||
|
||||
Reference in New Issue
Block a user