Repository navigation
fix: hide gateway calls on terminal Messages rounds - #346
Merged
franciscojavierarceo merged 1 commit intoSep 20, 2026
Merged
franciscojavierarceo merged 1 commit into
franciscojavierarceo merged 1 commit into
Conversation
A /v1/messages round can end while a gateway-owned tool_use is present, whenever the stop reason is not a tool-call stop: a max_tokens truncation mid-call, or an end_turn the request context does not accept. The loop returned that message unchanged, so the client received a tool_use for a tool it had declared for the gateway to execute. A native web_search_20250305 declaration is rewritten into an ordinary function tool for upstream, so the surfaced call names a tool the client never agreed to run. The streaming accumulator suppresses these blocks on every round, so the two transports disagreed on the same upstream turn. Strip gateway-owned tool_use blocks in deliver, the terminal return for every assistant message, rather than only on a round that also carries a client call. The call is not executed and the assistant turn fed back to the model still carries it; a round with no gateway block is unchanged. Signed-off-by: Chuyue Wang <[email protected]>
StevenWang-CY
requested review from
bbrowning,
franciscojavierarceo,
haoshan98,
jiahuei,
leseb,
maralbahari,
noobHappylife,
qandrew and
tjtanaa
as code owners
September 20, 2026 00:33
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The focused change has matching regression coverage and no unresolved blocking issues.
Review effort: Lite
Findings: None
What changed in this PR
Fixes parity between streaming and non-streaming Messages responses by hiding unexecutable gateway tool calls on terminal rounds.
Changes:
- Filters gateway calls during terminal delivery.
- Adds regression tests for terminal-round behavior.
- Documents the updated Messages lifecycle contract.
| File | Summary |
|---|---|
docs/design/claude-code-integration.md |
Documents terminal-round gateway-call suppression. |
crates/agentic-server-core/tests/messages_stream_test.rs |
Adds streaming parity coverage. |
crates/agentic-server-core/tests/messages_loop_test.rs |
Tests truncated and unaccepted terminal rounds. |
crates/agentic-server-core/src/executor/messages_loop.rs |
Filters gateway calls before returning terminal messages. |
ARCHITECTURE.md |
Updates the Messages lifecycle behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
franciscojavierarceo
approved these changes
Sep 20, 2026
franciscojavierarceo
added a commit
that referenced
this pull request
Sep 29, 2026
## Summary Reject incomplete or malformed upstream Messages streams before built-in tool dispatch or successful completion. Every round now requires `message_stop` and a `content_block_stop` for every started block, including hidden built-in tool blocks; EOF or `[DONE]` without it produces an Anthropic SSE `api_error` instead of synthetic completion. Invalid JSON in an SSE data payload also terminates the loop with a fixed error that does not echo upstream content. Previously, malformed events were silently ignored and could be followed by tool execution. Block events require unsigned integer indexes. Index reuse, duplicate completion, and deltas or stops outside an open block fail before tool dispatch or completion. Duplicate message starts within one upstream round fail before usage updates or dispatch. Valid starts in later tool rounds remain supported. Message-start IDs and tool-use IDs/names must be non-empty strings; malformed identifiers fail before dispatch or completion. Part of #313 and Enterprise Readiness #316. The existing accumulator remains the single ingestion path. Move unchanged content-block reconstruction into a private module and remove the now-unneeded file-size baseline exception. Well-formed unknown events, SSE comments, upstream error forwarding, named-tool compatibility, and valid completion before HTTP EOF retain their behavior. Pass-through requests are unchanged. Remaining event-schema/message-level lifecycle validation, aggregate retained-data budgets, and bounded tool scheduling remain open. The branch includes main's terminal hidden-call fix (#346). ## Test Plan - Regression tests failed before each fix: premature EOF reported success, and malformed JSON or an unclosed block still dispatched a search. - Core library: 753 passed, 5 ignored. Messages stream integration: 15 passed, covering ten incomplete-stream scenarios, malformed data before a tool dispatch and after a completed tool round, no upstream-data echo, well-formed unknown events, and individually omitted block-closing events in both recorded rounds. Tests count actual search and inference requests. An additional 18-case matrix rejects missing/invalid indexes, index reuse, duplicate starts/stops, and deltas or stops outside an open block. - Identifier regression coverage includes 12 cases for missing, null, numeric, and empty message IDs, tool IDs, and tool names. The missing-message-ID case reproduced tool execution before the fix. - Duplicate-start regression covers early and pre-terminal duplicates in both recorded rounds; the early duplicate reproduced tool execution before the fix. - HTTP Messages completion: 12 passed, including immediate malformed-data rejection, fragmented UTF-8, and completion without HTTP EOF. HTTP tool choice: 13 passed. - Workspace all-target clippy with warnings denied, all-files pre-commit hooks, formatting, and diff checks passed. - Validation uses deterministic local servers and existing cassette replay, not live inference. Linux builds used an existing local static OpenSSL installation. GitHub CI reruns for the updated head. Signed-off-by: Francisco Javier Arceo <[email protected]> Signed-off-by: Francisco Javier Arceo <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A
/v1/messagesgateway round can end while a gateway-ownedtool_useis still present. That happens whenever the stop reason is not a tool-call stop: amax_tokenstruncation mid-call, or anend_turnthe request context does not accept as a tool stop. The call is correctly not executed, but the loop returned the message unchanged, so the client received atool_usefor a tool it had declared for the gateway to run. A nativeweb_search_20250305declaration is rewritten into an ordinary function tool for upstream, so the surfaced call names a tool the client never agreed to execute.The streaming accumulator suppresses gateway blocks on every round, so the two transports disagreed on the same upstream turn:
stream: falseonmainstream: trueonmainweb_searchtool_use,stop_reason: max_tokenstool_usesurfacesstrip_gateway_tool_usealready exists for this contract ("the client never sees a call it cannot execute"), but the loop applied it only on a round that also carried a client call. This moves it intodeliver, the terminal return for every assistant message, so hide-the-call holds on every terminal round. It is also what the Anthropic contract promises for a server tool: the API "executes the tool internally and incorporates the results directly into its response without requiring additional user interaction".Behavior notes:
pause_turncontinuation stays open (Open Q1inmessages_loop.rs). Anthropic resumes a paused turn by resending the response with its pendingserver_tool_useblock; the gateway has no client-facing representation of a pending hidden call on either transport, and a resent client-facingtool_usewith no result would not have resumed it either. This PR only stops the message from advertising a call the client cannot run.Related: #314 (equivalent behavior across execution paths), #313 (one public Messages lifecycle).
Test Plan
Red on unmodified
main(747cb62), green with this change, using a separate worktree with only the test files applied:messages_loop_hides_a_gateway_call_on_a_terminal_rounddrives a truncated round carrying text plus aweb_searchcall overmax_tokens,stop_sequence,pause_turn,refusal, and an unknown future stop reason. Onmainit fails with the call surfaced; it also asserts the visible text, the terminal reason, and usage are unchanged.messages_loop_hides_an_unexecuted_gateway_call_labelled_end_turncovers anend_turnthe request context does not accept as a tool stop (vLLM's labelling of a named call, here withtool_choiceunset), where the loop declines to execute the call. Onmainthe call surfaces as the only content block.messages_stream_hides_a_gateway_call_on_a_terminal_roundis the streaming parity test for the same upstream turn. It passes onmainand locks that behavior against regression.repro_f5), the model-facing history that must keep the call (repro_f3), and the tool-choice and Dynamo/vLLM cassette replays.pre-commit run --all-files(includingrust-file-sizeswithRUST_FILE_SIZE_BASE=origin/main),cargo fmt -- --check,cargo clippy --all-targets -- -D warnings, andcargo test --no-fail-fastacross the workspace pass on the exact commit from a clean worktree.