Skip to content

FC-4: request deadline budget for the Anthropic chat path — stop fixing per-hop timeouts retail #9835

Closed

Description

@Git-on-my-level

Problem

The provider timeout class has been fixed four times, each time by moving a per-hop timeout: #8640, #8911, #9135 ("stop cutting off long-context Gemini calls at 90s"), #9644 ("restore Gemini platform deadline"). #9533's own body names the root cause: "those fixes only moved a per-attempt timeout to the current infrastructure boundary." This is registry class FC-4 (docs/product/failure-classes.md, #9822).

Current state, precisely:

  • The Gemini proxy path already has a request-level deadline (240s absolute inside the 300s platform bound, routes/proxy.rs:54) — the class is closed there, ad hoc.
  • The Anthropic chat path (routes/chat_completions.rs) does not. Pre-first-byte work — auth/JWK refresh (auth.rs, 5s per wait), paywall/BYOK lookups in the PaywalledAuthUser extractor, body extraction, Anthropic retries, and up to three server-tool continuations — has no shared budget; each stage owns a constant (10s connect at :847, 120s non-streaming total at :929).
  • A related live gap: the stream idle timer resets on every raw upstream byte (chat_completions.rs:1287), and Anthropic ping events emit nothing downstream (:1535) — a provider can ping indefinitely while the client has received no first byte and nothing times out.

What is already correct and must not change: streaming has no total response timeout by design (chat_completions.rs:101 — "a long answer is not an error"). #9135 was exactly the bug of a total timeout cutting a producing stream; the budget must never reintroduce it.

Fix: a RequestDeadline type on the Anthropic chat path, one PR

1. Admission in middleware, not the handler. RequestDeadline (wrapping tokio::time::Instant; API: remaining(), derive(cap) -> Duration, expired()) is created in route middleware before extractors and body extraction and stored in request extensions — the PaywalledAuthUser extractor's auth/paywall waits are inside the budget (their per-request waits become min(stage cap, remaining)).

2. Budget semantics. The deadline governs everything up to the first client-visible semantic SSE event (message_start, text delta, or tool delta) — not provider headers, pings, or keepalives, which do not count as progress. This includes retries and server-tool continuation round-trips that occur before that first visible event. After the first visible event: the budget is done; a separate semantic-progress idle timer (visible-event-based, replacing the raw-byte reset at :1287 — closing the infinite-ping gap) governs stalls. Public-web pause-turn synthesis is pre-first-visible-byte work and stays budgeted. Retry/continuation attempts start only if remaining() exceeds a floor; backoff sleeps are truncated at the budget edge.

3. Deadline is for request-owned awaits only. Policy clocks are explicitly out: the unknown-kid cooldown (auth.rs:21 — the wait may be min(5s, remaining), the shared cooldown stays independent), cache TTLs (BYOK/paywall/rate-limit), the idle-timer policy value, and startup retries (main.rs:144 is startup-only, not a request seam). Detached work spawned during a request (e.g. the Firestore usage write on message_stop, :1514) must never inherit the request deadline — it outlives the response and uses its own bounded background policy.

4. Enforcement. Provider sends go through a narrow send_with_deadline helper taking &RequestDeadline; a diff-scoped checker (manifest-registered, local + ci lanes) flags direct outbound sends in routes/ bypassing it — signatures alone don't cover raw reqwest builders. Per #9822's rule, the checker cites #8640/#8911/#9135/#9644 as the caught class.

5. Failure surface, stated honestly. Before the response starts: a typed HTTP timeout error, returned inside a verified platform bound (deployment-config verification is a proof item, not an assumption — the 300s figure is code-side and Cloud Run applies its request deadline to streaming connections too). After HTTP 200: only a terminal SSE error event is possible (as today on idle failure); clients must still handle platform/client disconnects. This shrinks the :1612 spinning-bubble class to post-response platform kills; it does not make it "impossible."

Proof

Cut list

  • Gemini proxy unification is a named follow-up, not this PR: proxy.rs already has a working ad hoc deadline; migrating it onto RequestDeadline is mechanical once the type exists, and bundling it here makes the PR unreviewable.
  • No retry-policy redesign (counts, backoff shape) — only budget-awareness of the existing policy.
  • No changes to policy clocks: cooldowns, cache TTLs, startup retries.
  • No per-user/per-model configurable budgets — one admission constant per route class until evidence demands more.
  • No streaming total timeout — explicitly preserved as absent, by design.

Sizing

One PR, honestly medium-large (this is a behavioral change, not an 80-line type): middleware + extensions plumbing, extractor budget coverage, semantic-progress redefinition of the idle timer, send_with_deadline seam + checker, deterministic fake-upstream test module, registry update. All within routes/chat_completions.rs, auth.rs, and one new module.

Sequencing

Lands before the chat_completions.rs split (companion issue): this is the behavioral change; the split is then a pure move-code refactor with no behavior change hiding inside it.

Activity

  1. added 7 commits that reference this issue on Jul 24, 2026
    fb2db82
    15d5eb1
    09a5bbf
    2d256cd
    03d14b4
    b858ade
    04850c5
  2. added a commit that references this issue on Jul 28, 2026
    5e9c0d5
  3. added a commit that references this issue on Jul 29, 2026
    d2366fc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions