Skip to content

fix: hide gateway calls on terminal Messages rounds - #346

Merged
franciscojavierarceo merged 1 commit into
vllm-project:mainfrom
StevenWang-CY:fix-messages-hide-terminal-gateway-call
Sep 20, 2026
Merged

franciscojavierarceo merged 1 commit into
vllm-project:mainfrom
StevenWang-CY:fix-messages-hide-terminal-gateway-call

Conversation

@StevenWang-CY

Copy link
Copy Markdown
Contributor

Summary

A /v1/messages gateway round can end while a gateway-owned tool_use is still present. That happens 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 as a tool stop. The call is correctly not executed, but the loop returned the message unchanged, so the client received a tool_use for a tool it had declared for the gateway to run. 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 execute.

The streaming accumulator suppresses gateway blocks on every round, so the two transports disagreed on the same upstream turn:

Upstream turn stream: false on main stream: true on main
text + web_search tool_use, stop_reason: max_tokens tool_use surfaces suppressed

strip_gateway_tool_use already 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 into deliver, 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:

  • The call is not executed, and the assistant turn fed back to the model still carries it. Only the client-facing message changes.
  • A round with no gateway-owned block is returned untouched, so requests without gateway tools are unaffected.
  • When the hidden call was the only block, the content array is empty, which is what the streaming path already produces for that turn.
  • An operator-aliased client tool is gateway-owned, so it is hidden on a terminal round too. That matches the streaming path and the gateway's ownership of its execution.
  • pause_turn continuation stays open (Open Q1 in messages_loop.rs). Anthropic resumes a paused turn by resending the response with its pending server_tool_use block; the gateway has no client-facing representation of a pending hidden call on either transport, and a resent client-facing tool_use with 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_round drives a truncated round carrying text plus a web_search call over max_tokens, stop_sequence, pause_turn, refusal, and an unknown future stop reason. On main it 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_turn covers an end_turn the request context does not accept as a tool stop (vLLM's labelling of a named call, here with tool_choice unset), where the loop declines to execute the call. On main the call surfaces as the only content block.
  • messages_stream_hides_a_gateway_call_on_a_terminal_round is the streaming parity test for the same upstream turn. It passes on main and locks that behavior against regression.
  • Existing coverage is unchanged: the mixed client-plus-gateway round (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 (including rust-file-sizes with RUST_FILE_SIZE_BASE=origin/main), cargo fmt -- --check, cargo clippy --all-targets -- -D warnings, and cargo test --no-fail-fast across the workspace pass on the exact commit from a clean worktree.

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]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
franciscojavierarceo merged commit 32f32c8 into vllm-project:main Sep 20, 2026
19 checks passed
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants