Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(agent-core): send a pause_turn continuation only as served, and log refused request shapes (#3101)

Call Whitea81b421f933c
diff --git a/core/agent_core/ai/anthropic.py b/core/agent_core/ai/anthropic.py
index 3dffb2ccb..68ab0d787 100644
--- a/core/agent_core/ai/anthropic.py
+++ b/core/agent_core/ai/anthropic.py
@@ -80,6 +80,8 @@ MAX_PAUSE_CONTINUATIONS = 5
PAUSE_LIMIT_MESSAGE = f"the provider paused the turn more than {MAX_PAUSE_CONTINUATIONS} times"
#: A paused response continues only on the Source that served it, pinned there (§4.3 as implemented).
PAUSE_UNSERVED_MESSAGE = "the Source that served the paused turn cannot continue it"
+#: A continuation goes out only as the first send plus the paused response exactly as served (§4.3).
+PAUSE_ALTERED_MESSAGE = "the paused turn cannot be sent back as it was served"
_MAX_CACHE_BREAKPOINTS = 4
# A Claude route caches its static prefix, the tools and then the system prompt, for an hour (``_prefix_ttl``); the
# conversation's breakpoints keep the default five minutes. Anthropic evaluates breakpoints as tools, system, messages
@@ -863,6 +865,9 @@ class AnthropicAdapter(ProviderAdapter):
yield terminal
sending = request
continuations = 0
+ # The first send's prepared messages and images: every continuation must send exactly them plus the
+ # response so far, as served (§4.3).
+ first_sent: tuple[tuple[Any, ...], Mapping[str, LoadedImage]] | None = None
while True:
# The lifetime ``wire_request`` gives the prefix: the default once this route has rejected the field.
prefix_ttl = _prefix_ttl(sending)
@@ -897,6 +902,8 @@ class AnthropicAdapter(ProviderAdapter):
rejected = True
break
if isinstance(event, ProviderError):
+ if event.status == 400:
+ _log_refused_request_shape(sending, wire.body, continuations)
repair = search_replay_refusal(event, sending, wire.body)
if repair != event.refusal:
event = replace(event, refusal=repair)
@@ -942,6 +949,8 @@ class AnthropicAdapter(ProviderAdapter):
if terminal is not None:
yield terminal
return
+ if first_sent is None:
+ first_sent = (transformed_messages, loaded_images)
continuations += 1
response_usage = None
carried_counts, response_counts = _summed(carried_counts, response_counts), {}
@@ -971,12 +980,67 @@ class AnthropicAdapter(ProviderAdapter):
yield terminal
return
transformed_messages, loaded_images = prepared
+ if first_sent is not None and _binding_prefix(
+ sending, transformed_messages, loaded_images
+ ) != _binding_prefix(request, (*first_sent[0], so_far), first_sent[1]):
+ # The provider continues the paused response from exactly what it sent and served: anything this
+ # render changed (a hosted call or thinking the Session can no longer send back, an earlier
+ # message rendered differently) would make it a different request, or an assistant prefill.
+ # The response ends with what it has instead (§4.3).
+ terminal = assembler.terminal(assembler.error(PAUSE_ALTERED_MESSAGE, kind="interrupted"))
+ if terminal is not None:
+ yield terminal
+ return
except Exception as exc:
terminal = assembler.terminal(assembler.exception(exc))
if terminal is not None:
yield terminal
+def _binding_prefix(
+ request: ModelRequest, messages: tuple[Any, ...], loaded_images: Mapping[str, LoadedImage] | None
+) -> dict[str, Any]:
+ """What a Messages request's replayed content is bound to: its ``system``, ``tools`` and ``messages``, without
+ cache markers, which are not part of it. ``messages`` go through the payload builder unchanged, so an untransformed
+ response among them renders exactly as served."""
+
+ payload = build_messages_payload(replace(request, cache="none"), messages, loaded_images=loaded_images)
+ return {key: payload.get(key) for key in ("system", "tools", "messages")}
+
+
+def _block_types(content: Any) -> list[str]:
+ if not isinstance(content, list):
+ return ["text"]
+ return [str(block.get("type")) if isinstance(block, Mapping) else "?" for block in content]
+
+
+def _log_refused_request_shape(request: ModelRequest, body: Mapping[str, Any], continuation: int) -> None:
+ """Log the structure of a request the provider refused with HTTP 400, never its content.
+
+ A refusal that depends on how the conversation ends (an assistant prefill, a tool pairing) cannot be diagnosed
+ from the error alone. Roles and block types, counts, and the hop name the shape without any text, id, or input.
+ """
+
+ messages = body.get("messages")
+ if not isinstance(messages, list):
+ return
+ last = next((index for index in range(len(messages) - 1, -1, -1) if messages[index].get("role") == "assistant"), None)
+ after = messages[last + 1 :] if last is not None else []
+ tail = " ".join(f"{message.get('role')}[{','.join(_block_types(message.get('content')))}]" for message in messages[-4:])
+ thinking = body.get("thinking")
+ logger.warning(
+ "Anthropic refused a request with HTTP 400: source=%s model=%s continuation=%s messages=%s thinking=%s "
+ "tail=%s results_only_after_assistant=%s",
+ request.endpoint.source_id,
+ request.endpoint.model_id,
+ continuation,
+ len(messages),
+ thinking.get("type") if isinstance(thinking, Mapping) else None,
+ tail,
+ bool(after) and all(set(_block_types(message.get("content"))) == {"tool_result"} for message in after),
+ )
+
+
def _continues_unthought_tool_loop(messages: list[dict[str, Any]]) -> bool:
"""Whether a request continues a tool loop (tool results follow its last assistant message) whose assistant message
does not start with a thinking block, which Anthropic refuses under manual thinking. Anthropic combines consecutive
diff --git a/docs/plans/vibey-hosted-tools.md b/docs/plans/vibey-hosted-tools.md
index 506f23eae..cb0c557f0 100644
--- a/docs/plans/vibey-hosted-tools.md
+++ b/docs/plans/vibey-hosted-tools.md
@@ -526,8 +526,13 @@ never dropped.
**`pause_turn`.** Unchanged from v1 as implemented (orchestrator decision, 2026-10-14):
- The Anthropic adapter continues a paused response internally, at most 5 continuations, each pinned to the Source
that served the paused response. It never re-renders a continuation in portable form.
-- A continuation that Source answers `pin_unavailable`, a pause of a send that carried no pin, or a 6th pause ends
- the response with one terminal `ProviderError(kind="interrupted")` whose partial keeps all assembled content.
+- A continuation goes out only as the first send plus the paused response exactly as served: same `system`, `tools`,
+ and earlier messages, cache markers aside. A render that changes any of it (a hosted call or thinking the Session can
+ no longer send back after a replay refusal, or an earlier message rendered differently) is not sent, since it would
+ be a different request whose assistant tail the provider takes as a prefill.
+- A continuation that Source answers `pin_unavailable`, a pause of a send that carried no pin, a continuation that
+ cannot go out as served, or a 6th pause ends the response with one terminal `ProviderError(kind="interrupted")`
+ whose partial keeps all assembled content.
- The merged message carries the final continuation's usage; `hosted_summary` sums counts and cost and deduplicates
`cited_sources` by URL. Pending calls carry over by tool-call id, so each call stays one block.
- The merged message is verified only when every response in it was verified and reported the same origin.
diff --git a/tests/agent_core/ai/test_anthropic_prompt_cache.py b/tests/agent_core/ai/test_anthropic_prompt_cache.py
index b366b28a8..8b4ba5d73 100644
--- a/tests/agent_core/ai/test_anthropic_prompt_cache.py
+++ b/tests/agent_core/ai/test_anthropic_prompt_cache.py
@@ -243,7 +243,7 @@ async def test_a_rejected_ttl_is_retried_once_without_it_and_remembered_for_the_
assert [type(event) for event in terminals] == [Done if retry_accepted else ProviderError]
assert retry_accepted or terminals[0].status == 400
# Logged once per route, not once per request.
- assert len([record for record in caplog.records if record.name == "core.agent_core.ai.anthropic"]) == 3
+ assert len([record for record in caplog.records if "prompt-cache TTL" in record.getMessage()]) == 3
@pytest.mark.parametrize(
diff --git a/tests/agent_core/ai/test_hosted_pause_turn.py b/tests/agent_core/ai/test_hosted_pause_turn.py
index 3ab3eaad4..71820d7ab 100644
--- a/tests/agent_core/ai/test_hosted_pause_turn.py
+++ b/tests/agent_core/ai/test_hosted_pause_turn.py
@@ -16,7 +16,12 @@ import httpx
import pytest
from core.agent_core.ai._common import PIN_MODEL_HEADER, PIN_SOURCE_HEADER, portable_endpoint
-from core.agent_core.ai.anthropic import MAX_PAUSE_CONTINUATIONS, PAUSE_LIMIT_MESSAGE, PAUSE_UNSERVED_MESSAGE
+from core.agent_core.ai.anthropic import (
+ MAX_PAUSE_CONTINUATIONS,
+ PAUSE_ALTERED_MESSAGE,
+ PAUSE_LIMIT_MESSAGE,
+ PAUSE_UNSERVED_MESSAGE,
+)
from core.agent_core.ai.provider import Done, HostedToolRoute, ModelEndpoint, ModelRequest, ProviderError
from core.agent_core.ai.registry import create_adapter
from core.agent_core.cancel import CancelToken
@@ -241,6 +246,35 @@ async def test_a_response_paused_past_the_bound_ends_interrupted_and_keeps_every
assert sum(isinstance(event, (Done, ProviderError)) for event in events) == 1
[email protected]("refused", ["hosted", "signature"])
+async def test_a_paused_response_the_session_cannot_send_back_as_served_ends_interrupted_without_a_continuation(
+ refused,
+):
+ """§4.3: a continuation is the first send plus the paused response exactly as served. When the Session can no
+ longer send part of it back, because the provider refused that identity's hosted payloads or thinking signatures,
+ the continuation would be a different request ending in an assistant turn the provider takes as a prefill. The
+ response ends ``interrupted`` with what it has, and nothing more is sent."""
+
+ identity = "sha256:served"
+ request = _request()
+ request = replace(
+ request,
+ endpoint=replace(request.endpoint, source_identity=identity, opaque_refused=frozenset({(identity, refused)})),
+ )
+
+ def answer(number: int) -> httpx.Response:
+ body = _response(_thinking(), _use(), stop_reason="pause_turn", input_tokens=100, output_tokens=10, searches=1)
+ return httpx.Response(200, headers=_served(), text=body)
+
+ sent, events = await _stream(answer, request)
+
+ assert len(sent) == 1
+ error = events[-1]
+ assert isinstance(error, ProviderError) and error.kind == "interrupted" and error.message == PAUSE_ALTERED_MESSAGE
+ assert [type(block) for block in error.partial.content] == [ThinkingBlock, HostedToolBlock]
+ assert sum(isinstance(event, (Done, ProviderError)) for event in events) == 1
+
+
@pytest.mark.parametrize(
("unavailable", "reported", "portable", "sends"),
[(2, True, False, 2), (None, True, True, 1), (None, False, False, 1)],
diff --git a/tests/agent_core/ai/test_search_history_recovery.py b/tests/agent_core/ai/test_search_history_recovery.py
index 07d658fb9..a6e366dc5 100644
--- a/tests/agent_core/ai/test_search_history_recovery.py
+++ b/tests/agent_core/ai/test_search_history_recovery.py
@@ -133,6 +133,24 @@ async def test_prefill_is_not_an_unconditional_search_refusal(case, transport):
assert error.refusal is None or error.refusal.kind != "search_history"
+async def test_a_refused_request_logs_its_shape_and_none_of_its_content(caplog):
+ """A 400 that depends on how the conversation ends (a prefill) cannot be diagnosed from the error alone: the
+ adapter logs the request's roles and block types, never a text, id, signature, input, or result."""
+
+ request = mixed_request()
+ response = httpx.Response(400, json=replay._anthropic_refusal(PREFILL, via_gateway=False))
+
+ with caplog.at_level("WARNING", logger="core.agent_core.ai.anthropic"):
+ await replay._send(request, lambda _: response)
+
+ (record,) = [record for record in caplog.records if "refused a request with HTTP 400" in record.getMessage()]
+ logged = record.getMessage()
+ assert "tail=" in logged and "assistant[thinking,server_tool_use,tool_use] user[tool_result]" in logged
+ assert "results_only_after_assistant=True" in logged and f"source={replay.SOURCE}" in logged
+ for content in ("read first", "live-signature", "next release", "toolu_read", "srvtoolu_live", "saved result"):
+ assert content not in logged
+
+
def mixed_stream(*, completed=False):
events = (
{"type": "message_start", "message": {"usage": {"input_tokens": 5}}},
diff --git a/tests/scenarios/model_hub/catalog.yaml b/tests/scenarios/model_hub/catalog.yaml
index cfd78c5f4..44f96a79f 100644
--- a/tests/scenarios/model_hub/catalog.yaml
+++ b/tests/scenarios/model_hub/catalog.yaml
@@ -457,6 +457,7 @@ scenarios:
related_tests:
- tests/agent_core/ai/test_hosted_pause_turn.py::test_a_response_paused_past_the_bound_ends_interrupted_and_keeps_everything_it_assembled
- tests/agent_core/ai/test_hosted_pause_turn.py::test_a_paused_response_its_source_cannot_continue_ends_interrupted_and_keeps_what_it_has
+ - tests/agent_core/ai/test_hosted_pause_turn.py::test_a_paused_response_the_session_cannot_send_back_as_served_ends_interrupted_without_a_continuation
- tests/agent_core/ai/test_hosted_pause_turn.py::test_a_continued_response_is_verified_only_when_every_response_in_it_was_from_one_origin
- tests/agent_core/ai/test_hosted_pause_turn.py::test_a_counter_past_the_bound_in_any_pause_stays_absent_and_the_response_completes
- tests/agent_core/ai/test_anthropic_deferred.py::test_mixed_pause_preserves_exact_assistant_and_result_only_user_group