← Back to Pull requests

fix: hide gateway calls on terminal Messages rounds #346

Closed
unknown closed this pull request without merging on Sep 20, 2026 13:42 UTC
View on GitHub
unknown unknown commented on Sep 20, 2026 00:33 UTC
Author

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.
  • Diff: 498 lines (+373 / -125) across 6 files Reviewer: franciscojavierarceo Review duration: 789m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.47) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.47 · 498 lines diff across 6 files
    0.47
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.372
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #346"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.5% (MEDIUM) · Higher = larger blast radius
    47.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%