Reject approvals for tool calls a server-held history isn't waiting on under `manage_message_history='server'`

Merged
DO
1mo ago
+163-13

Posted by Claude Code on Douwe's behalf.

Why

With manage_message_history='server' (#11431), message_history is the conversation, and the adapter already keeps a client tool result only when it answers a call the stored history is waiting on. Approvals and denials were the one client answer the adapter didn't check itself: it passed them to the agent and relied on the agent's own check on resume. That check does stop them today (it requires the answers to match the unprocessed calls of the last response exactly), but its error doesn't help a UI client, and it's the check that partial DeferredToolResults would relax.

This makes the server-held mode check it at the adapter boundary: every approval or denial in the request has to answer a tool call that the stored history is still waiting on. A request with an approval for a call that isn't pending, such as a tool call the model didn't make or one that was already answered, is rejected with a protocol error event before the run starts, and no tool runs. The default 'client' mode is unchanged.

What it does today, and with this change

Tested through both adapters' public APIs with a server-held history that ends with an approval-gated delete_file call (del-1):

RequestmainThis PR
(a) An approval for a call that isn't pending (del-1 again, after it was approved and ran)Not run; agent error: "Tool call results were provided, but the message history does not contain any unprocessed tool calls."Not run; adapter error naming 'del-1'
(b) A tool call the model didn't make (del-2) plus its approval, alone or beside an approval of the pending del-1Not run; agent error: "Tool call results need to be provided for all deferred tool calls. Expected: {'del-1'}, got: {...}"Not run; adapter error naming 'del-2'
(c) An approval or denial for the pending del-1Runs (or is denied)Unchanged

In (b), the client's assistant message carrying the call was already discarded under 'server', since only the requests after the client's last response are used, so the model never saw it either way. Neither version calls the model for (a) or (b).

The new error reads: "The request answers 1 tool call(s) that message_history is not waiting on: 'del-2'. With manage_message_history='server', an approval or denial can only answer a tool call that is still pending at the end of the stored history. If the conversation changed since the client loaded it, reload it from the server."

Changes

  • ui/_adapter.py: _take_new_turn now also returns the ids of the calls the new turn leaves unanswered, and _reject_answers_for_calls_not_pending raises a UserError for any id in deferred_tool_results.approvals or .calls outside them. It runs inside run_stream_native's existing rejection path, so it reaches the client as Vercel AI's error chunk or AG-UI's RUN_ERROR under a 200, as a rejected regenerate does. No request the agent accepted before is rejected now: the agent already required the same ids.
  • Docs: the trust-model warning in docs/ui/overview.md, its "Tool approvals" bullet under "Keeping the history on the server", and the approval sections in vercel-ai.md and ag-ui.md now recommend server-held history for approval-gated tools and say what's rejected. THREAT_MODEL.md's server-held paragraph says how a tool result and an approval for a call that isn't pending are each handled.

Scope: matching against the pending calls only. No store (#11031), no change to client-held mode (#8954), no new approval type or answerer record. Binding an approval to the call's arguments (an args digest checked at execution) is a separate follow-up; this check keys on tool_call_id only and doesn't stand in its way. AG-UI's editedArgs still replaces the arguments of the pending call it approves, as documented.

Verification

  • tests/test_vercel_ai.py::test_server_message_history_rejects_an_approval_for_a_call_not_pending and tests/test_ag_ui.py::test_server_message_history_rejects_an_approval_for_a_call_not_pending, each with three cases (a call already answered, a call the model didn't make, and that call beside the pending one): no tool runs, the model isn't called, and the error event is the adapter's. All three fail on main on the error text only.
  • (c) is covered by the existing test_server_message_history_tool_approval (Vercel AI, approved and denied) and test_server_message_history_resume_interrupt (AG-UI), which still pass.

Open questions for Douwe

  1. Error or warn-and-drop. I went with rejecting the request, matching the rejected regenerate and AG-UI's invalid resume[] payload (both a UserError surfaced as a protocol error event), rather than the warn-and-drop used for an unmatched client tool result. Dropping works for tool results because the rest of the turn is still valid and a client that sends its whole history resends results it already sent. An approval is the request's whole point: with it dropped, the run would have no new input and would either pause again or call the model with nothing new, and clients don't resend answered approvals (approval-responded parts become output-available, and resume[] is per run). The alternative is to drop unmatched approvals with a warning and only reject when nothing valid is left.
  2. Kind of answer. The check matches ids, not what the call is waiting for, since a stored ToolCallPart doesn't record whether its call waits on an approval or on an external result. So a tool result for a pending approval-gated call is still accepted as that call's result (nothing runs), and an approval for a pending external call still reaches the agent. Recording that on the call would be a "kind on deferred calls" primitive, out of scope here.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.

šŸ¤– Generated with Claude Code


Summary by cubic

Rejects approvals, denials, and deferred tool results for tool calls the stored history isn't waiting on under manage_message_history='server', returning a protocol error event before any tool runs instead of the agent's less specific error. The default 'client' mode is unchanged, and no request the agent accepted before is rejected now — it already required the same tool call ids.

  • Records which calls the new turn leaves unanswered in _take_new_turn and raises a UserError for any approval, denial, or deferred tool result keyed to a tool_call_id outside them.
  • Surfaces as Vercel AI's error chunk or AG-UI's RUN_ERROR under a 200, naming the offending call ids and reloading advice.
  • Updates the trust-model warning in the UI docs and THREAT_MODEL.md; adds tests for already-answered and model-never-made calls, including one beside a pending call.