mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(ccr): return a mixed turn's continuation, not the turn it replaced
The continuation is the model's newer decision, made with the retrieved content in hand. Handing back the original turn when it withdrew or changed a client call delivered an action the model had cancelled. Only a failed continuation request hands the original turn back now, since the model made no newer decision; a continuation that succeeded supersedes it even if a later retrieval round fails. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -29,8 +29,6 @@ from .tool_calls import (
|
||||
has_ccr_tool_calls,
|
||||
is_ccr_tool_call,
|
||||
parse_ccr_tool_calls,
|
||||
reissues_tool_calls,
|
||||
tool_call_signature,
|
||||
)
|
||||
from .tool_injection import CCR_TOOL_NAME
|
||||
|
||||
@@ -471,10 +469,6 @@ class CCRResponseHandler:
|
||||
current_response = response
|
||||
current_messages = list(messages) # Copy to avoid mutation
|
||||
rounds = 0
|
||||
# A mixed turn whose client calls were dropped, and those calls, until
|
||||
# a continuation re-issues them.
|
||||
mixed_turn: dict[str, Any] | None = None
|
||||
reissue: list[str] = []
|
||||
|
||||
while rounds < self.config.max_retrieval_rounds:
|
||||
# Check for CCR tool calls
|
||||
@@ -489,12 +483,14 @@ class CCRResponseHandler:
|
||||
# headroom_retrieve", so the model never gets the content), and a
|
||||
# continuation needs a tool_result for every tool_use. The client
|
||||
# calls have not run yet, so drop them and serve the retrieval now;
|
||||
# the model re-issues them, content in hand, in the continuation.
|
||||
# They count as delivered only if it re-issues every one unchanged;
|
||||
# otherwise the client gets this turn back below. A CCR-named call
|
||||
# without a valid hash, or a provider where dropping a sibling is
|
||||
# not safe (see drop_tool_calls), keeps the turn as it is for the
|
||||
# client to resolve.
|
||||
# the model decides again, content in hand, in the continuation.
|
||||
# That continuation replaces this turn whatever it does: re-issue
|
||||
# the calls, change them or drop them. Only a failed continuation
|
||||
# hands this turn back (below), since the model made no newer
|
||||
# decision. A CCR-named call without a valid hash, or a provider
|
||||
# where dropping a sibling is not safe (see drop_tool_calls), keeps
|
||||
# the turn as it is for the client to resolve.
|
||||
mixed_turn: dict[str, Any] | None = None
|
||||
if other_calls:
|
||||
trimmed = (
|
||||
current_response
|
||||
@@ -516,7 +512,6 @@ class CCRResponseHandler:
|
||||
len(other_calls),
|
||||
)
|
||||
mixed_turn = current_response
|
||||
reissue = [tool_call_signature(c) for c in other_calls]
|
||||
current_response = trimmed
|
||||
|
||||
rounds += 1
|
||||
@@ -570,22 +565,13 @@ class CCRResponseHandler:
|
||||
# entirely (#3129).
|
||||
logger.error("CCR: Continuation API call failed: %s: %r", type(e).__name__, e)
|
||||
# Return the response we had (with unhandled CCR calls)
|
||||
# The client will see the tool_use and might handle it differently
|
||||
# The client will see the tool_use and might handle it differently.
|
||||
# For a mixed turn that is the model's own turn, client calls
|
||||
# included (#839), never the trimmed one that lost them.
|
||||
if mixed_turn is not None:
|
||||
current_response = mixed_turn
|
||||
break
|
||||
|
||||
if reissue:
|
||||
_, reissued = self._parse_ccr_tool_calls(current_response, provider)
|
||||
if not reissues_tool_calls(reissued, reissue):
|
||||
break
|
||||
reissue = []
|
||||
|
||||
if reissue and mixed_turn is not None:
|
||||
logger.info(
|
||||
"CCR: the client call(s) dropped for retrieval were not re-issued; "
|
||||
"returning the model's own turn for the client to resolve"
|
||||
)
|
||||
return mixed_turn
|
||||
|
||||
if rounds >= self.config.max_retrieval_rounds:
|
||||
logger.warning(
|
||||
f"CCR: Hit max retrieval rounds ({self.config.max_retrieval_rounds}), "
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from collections import Counter
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
@@ -102,26 +100,6 @@ def drop_tool_calls(
|
||||
return response
|
||||
|
||||
|
||||
def tool_call_signature(tool_call: dict[str, Any]) -> str:
|
||||
"""Name and arguments of an Anthropic or OpenAI chat tool call, without its id."""
|
||||
function = tool_call.get("function")
|
||||
if isinstance(function, dict):
|
||||
name, arguments = function.get("name"), function.get("arguments")
|
||||
if isinstance(arguments, str):
|
||||
try:
|
||||
arguments = json.loads(arguments)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
name, arguments = tool_call.get("name"), tool_call.get("input")
|
||||
return json.dumps([name, arguments], sort_keys=True, default=str)
|
||||
|
||||
|
||||
def reissues_tool_calls(calls: list[dict[str, Any]], signatures: list[str]) -> bool:
|
||||
"""Do ``calls`` include every call in ``signatures``, unchanged?"""
|
||||
return not (Counter(signatures) - Counter(tool_call_signature(c) for c in calls))
|
||||
|
||||
|
||||
def is_ccr_tool_call(tool_call: dict[str, Any]) -> bool:
|
||||
"""Return true when a provider-native tool call names the CCR retrieval tool."""
|
||||
return (
|
||||
|
||||
@@ -613,7 +613,6 @@ class TestCCRResponseHandling:
|
||||
}
|
||||
]
|
||||
}
|
||||
# Re-issued with its own id and differently spaced arguments: same call.
|
||||
final = {
|
||||
"choices": [
|
||||
{
|
||||
@@ -663,45 +662,59 @@ class TestCCRResponseHandling:
|
||||
@pytest.mark.parametrize(
|
||||
"continuation",
|
||||
[
|
||||
# Answers without re-issuing the client call.
|
||||
{"content": [{"type": "text", "text": "Done"}], "stop_reason": "end_turn"},
|
||||
# Re-issues it with different input: not the call the model made.
|
||||
# Withdraws the client call once it has the content.
|
||||
{
|
||||
"content": [{"type": "text", "text": "Do not overwrite old.txt."}],
|
||||
"stop_reason": "end_turn",
|
||||
},
|
||||
# Changes it.
|
||||
{
|
||||
"content": [
|
||||
{
|
||||
"type": "tool_use",
|
||||
"id": "user_call_2",
|
||||
"name": "write_file",
|
||||
"input": {"path": "/tmp/other"},
|
||||
"input": {"path": "new.txt", "content": "safe output"},
|
||||
}
|
||||
],
|
||||
"stop_reason": "tool_use",
|
||||
},
|
||||
# Re-issues it unchanged.
|
||||
{
|
||||
"content": [
|
||||
{
|
||||
"type": "tool_use",
|
||||
"id": "user_call_2",
|
||||
"name": "write_file",
|
||||
"input": {"path": "old.txt", "content": "overwrite"},
|
||||
}
|
||||
],
|
||||
"stop_reason": "tool_use",
|
||||
},
|
||||
],
|
||||
ids=["not_reissued", "reissued_changed"],
|
||||
ids=["withdrawn", "changed", "reissued"],
|
||||
)
|
||||
async def test_handle_response_mixed_tools_returns_turn_when_not_reissued(self, continuation):
|
||||
"""A dropped client call is delivered only by an unchanged re-issue.
|
||||
Otherwise the client gets the model's own turn back, as before (#839),
|
||||
so the write still runs."""
|
||||
async def test_handle_response_mixed_tools_returns_the_continuation(self, continuation):
|
||||
"""The continuation is the model's newer decision, made with the
|
||||
retrieved content: the client gets it, never the dropped call."""
|
||||
store = get_compression_store()
|
||||
hash_key = store.store(original="[1,2,3]", compressed="[]")
|
||||
mixed_response = self._mixed_turn(hash_key, {"path": "/tmp/out"})
|
||||
sent = []
|
||||
mixed_response = self._mixed_turn(hash_key, {"path": "old.txt", "content": "overwrite"})
|
||||
|
||||
async def mock_api_call(messages, tools):
|
||||
sent.append(messages)
|
||||
return continuation
|
||||
|
||||
result = await CCRResponseHandler().handle_response(
|
||||
mixed_response, [], None, mock_api_call, "anthropic"
|
||||
)
|
||||
|
||||
assert len(sent) == 1
|
||||
assert result is mixed_response
|
||||
assert result is continuation
|
||||
assert "user_call" not in [b.get("id") for b in result["content"]]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_response_mixed_tools_failed_continuation_returns_turn(self):
|
||||
"""No newer decision: the client gets the model's own turn, client
|
||||
call included, as before (#839)."""
|
||||
store = get_compression_store()
|
||||
hash_key = store.store(original="[1,2,3]", compressed="[]")
|
||||
mixed_response = self._mixed_turn(hash_key, {"path": "/tmp/out"})
|
||||
@@ -715,6 +728,40 @@ class TestCCRResponseHandling:
|
||||
|
||||
assert result is mixed_response
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_response_mixed_tools_later_failure_keeps_the_continuation(self):
|
||||
"""A continuation that succeeded supersedes the mixed turn even when a
|
||||
later retrieval round fails."""
|
||||
store = get_compression_store()
|
||||
hash_key = store.store(original="[1,2,3]", compressed="[]")
|
||||
mixed_response = self._mixed_turn(hash_key, {"path": "/tmp/out"})
|
||||
retrieve_again = {
|
||||
"content": [
|
||||
{
|
||||
"type": "tool_use",
|
||||
"id": "ccr_call_2",
|
||||
"name": CCR_TOOL_NAME,
|
||||
"input": {"hash": hash_key},
|
||||
}
|
||||
],
|
||||
"stop_reason": "tool_use",
|
||||
}
|
||||
calls = 0
|
||||
|
||||
async def mock_api_call(messages, tools):
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
if calls > 1:
|
||||
raise RuntimeError("upstream down")
|
||||
return retrieve_again
|
||||
|
||||
result = await CCRResponseHandler().handle_response(
|
||||
mixed_response, [], None, mock_api_call, "anthropic"
|
||||
)
|
||||
|
||||
assert calls == 2
|
||||
assert result is retrieve_again
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_response_malformed_ccr_call_keeps_turn(self):
|
||||
"""A headroom_retrieve without a valid hash is not a client call to
|
||||
|
||||
@@ -6,9 +6,7 @@ from headroom.ccr.tool_calls import (
|
||||
extract_tool_calls,
|
||||
has_ccr_tool_calls,
|
||||
parse_ccr_tool_calls,
|
||||
reissues_tool_calls,
|
||||
tool_call_id_for_provider,
|
||||
tool_call_signature,
|
||||
)
|
||||
from headroom.ccr.tool_injection import CCR_TOOL_NAME
|
||||
|
||||
@@ -147,34 +145,3 @@ def test_drop_tool_calls_returns_unrecognized_shapes_unchanged() -> None:
|
||||
({"candidates": []}, "google"),
|
||||
]:
|
||||
assert drop_tool_calls(response, provider, []) is response
|
||||
|
||||
|
||||
def test_tool_call_signature_ignores_ids_and_argument_formatting() -> None:
|
||||
def openai(call_id: str, arguments: object) -> dict:
|
||||
return {"id": call_id, "function": {"name": "Read", "arguments": arguments}}
|
||||
|
||||
assert tool_call_signature(openai("a", '{"p": 1, "q": 2}')) == tool_call_signature(
|
||||
openai("b", '{"q":2,"p":1}')
|
||||
)
|
||||
assert tool_call_signature(openai("a", {"p": 1})) == tool_call_signature(
|
||||
openai("b", '{"p": 1}')
|
||||
)
|
||||
# Unparseable arguments compare as the raw string.
|
||||
assert tool_call_signature(openai("a", "{oops")) == tool_call_signature(openai("b", "{oops"))
|
||||
assert tool_call_signature(openai("a", "{oops")) != tool_call_signature(openai("a", "{}"))
|
||||
|
||||
def anthropic(call_id: str, value: int) -> dict:
|
||||
return {"type": "tool_use", "id": call_id, "name": "Read", "input": {"p": value}}
|
||||
|
||||
assert tool_call_signature(anthropic("a", 1)) == tool_call_signature(anthropic("b", 1))
|
||||
assert tool_call_signature(anthropic("a", 1)) != tool_call_signature(anthropic("a", 2))
|
||||
|
||||
|
||||
def test_reissues_tool_calls_is_a_multiset_check() -> None:
|
||||
def call(path: str) -> dict:
|
||||
return {"type": "tool_use", "id": path, "name": "Read", "input": {"path": path}}
|
||||
|
||||
wanted = [tool_call_signature(call("a")), tool_call_signature(call("a"))]
|
||||
assert reissues_tool_calls([call("a"), call("a"), call("b")], wanted)
|
||||
assert not reissues_tool_calls([call("a"), call("b")], wanted)
|
||||
assert reissues_tool_calls([], [])
|
||||
|
||||
@@ -84,8 +84,9 @@ def _message_response(content: list[dict], *, stop_reason: str = "end_turn") ->
|
||||
|
||||
|
||||
class _ContinuationClient:
|
||||
def __init__(self, response_json: dict) -> None:
|
||||
def __init__(self, response_json: dict, status_code: int = 200) -> None:
|
||||
self.response_json = response_json
|
||||
self.status_code = status_code
|
||||
self.post_calls: list[dict] = []
|
||||
|
||||
async def post(self, url, *, content=None, headers=None, timeout=None): # noqa: ANN001
|
||||
@@ -97,7 +98,7 @@ class _ContinuationClient:
|
||||
"timeout": timeout,
|
||||
}
|
||||
)
|
||||
return httpx.Response(200, json=self.response_json)
|
||||
return httpx.Response(self.status_code, json=self.response_json)
|
||||
|
||||
async def aclose(self) -> None:
|
||||
return None
|
||||
@@ -348,10 +349,12 @@ def test_mixed_ccr_and_client_tool_serves_retrieval_then_streams_reissued_tool()
|
||||
assert "retrieved answer" in json.dumps(tool_result)
|
||||
|
||||
|
||||
def test_mixed_turn_not_reissued_streams_both_blocks_as_sse() -> None:
|
||||
"""LEGAL mixed turn (#839, #2089) whose continuation does not re-issue the
|
||||
client tool: the client gets the model's own turn as a 200 SSE stream with
|
||||
BOTH tool_use blocks, so the client call still runs. No 502."""
|
||||
@pytest.mark.parametrize("status_code", [200, 500], ids=["withdrawn", "failed"])
|
||||
def test_mixed_turn_streams_the_continuation_or_the_turn_on_failure(status_code: int) -> None:
|
||||
"""LEGAL mixed turn (#839, #2089). A continuation that withdraws the client
|
||||
tool is what the client gets: the dropped call never reaches it. If the
|
||||
continuation fails, the model made no newer decision, so the client gets
|
||||
the model's own turn as a 200 SSE stream with BOTH tool_use blocks. No 502."""
|
||||
config = _make_config()
|
||||
store = get_compression_store()
|
||||
hash_key = store.store(
|
||||
@@ -385,7 +388,7 @@ def test_mixed_turn_not_reissued_streams_both_blocks_as_sse() -> None:
|
||||
side_effect=AssertionError("live streaming path should not be used")
|
||||
)
|
||||
continuation_client = _ContinuationClient(
|
||||
_message_response([{"type": "text", "text": "all done"}])
|
||||
_message_response([{"type": "text", "text": "all done"}]), status_code
|
||||
)
|
||||
proxy.http_client = continuation_client
|
||||
|
||||
@@ -416,9 +419,13 @@ def test_mixed_turn_not_reissued_streams_both_blocks_as_sse() -> None:
|
||||
|
||||
assert resp.status_code == 200, resp.text
|
||||
assert "text/event-stream" in resp.headers["content-type"]
|
||||
assert "toolu_ccr" in resp.text
|
||||
assert "toolu_client" in resp.text
|
||||
assert "all done" not in resp.text
|
||||
if status_code == 200:
|
||||
assert "all done" in resp.text
|
||||
assert "toolu_client" not in resp.text
|
||||
assert "headroom_retrieve" not in resp.text
|
||||
else:
|
||||
assert "toolu_ccr" in resp.text
|
||||
assert "toolu_client" in resp.text
|
||||
assert "Unable to safely complete streamed CCR retrieval" not in resp.text
|
||||
assert len(continuation_client.post_calls) == 1
|
||||
|
||||
|
||||
Reference in New Issue
Block a user