Repository navigation
Description
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 thePaywalledAuthUserextractor, 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 Anthropicpingevents 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
- Behavioral tests with
tokio::time::pauseand a scripted fake upstream: (a) a retry/continuation chain stops at the budget floor rather than starting an attempt it can't finish; (b) backoff sleep truncated at budget edge; (c) a stream emitting visible deltas survives budget expiry; (d) ping-only upstream beforemessage_starthits the budget → typed error (the live gap above); (e) post-first-event stall dies from the semantic idle timer independent of budget; (f) the spawned usage write completes under its own policy after the request deadline expires. - Test-file header cites [codex] Fix Gemini proxy timeout handling #8640/Fix desktop Gemini proxy timeout observability #8911/desktop-backend Gemini proxy returns persistent 504s while /health stays green #9135/fix(desktop): restore Gemini platform deadline #9644 as the caught class.
- Deployed platform timeout config verified and recorded in the PR (not assumed from code constants).
- FC-4 registry clock started at merge.
Cut list
- Gemini proxy unification is a named follow-up, not this PR:
proxy.rsalready has a working ad hoc deadline; migrating it ontoRequestDeadlineis 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
refactor(proxy): migrate the Gemini deadline onto the shared RequestD…
fix(chat): consume one request deadline budget on the Anthropic path
harden(ci): reject chat provider sends that bypass send_with_deadline
fix(chat): consume one request deadline budget on the Anthropic path
harden(ci): reject chat provider sends that bypass send_with_deadline
refactor(proxy): migrate the Gemini deadline onto the shared RequestD…
fix(chat): consume one request deadline budget on the Anthropic path
harden(ci): reject chat provider sends that bypass send_with_deadline
harden(desktop-backend): give the bare Client::new() call sites a bou…
fix(chat): consume one request deadline budget on the Anthropic path
harden(ci): reject chat provider sends that bypass send_with_deadline
refactor(proxy): migrate the Gemini deadline onto the shared RequestD…
refactor(proxy): migrate the Gemini deadline onto the shared RequestD…
fix(chat): consume one request deadline budget on the Anthropic path
harden(ci): reject chat provider sends that bypass send_with_deadline
refactor(proxy): migrate the Gemini deadline onto the shared RequestD…
fix(chat): FC-4 request deadline budget for the Anthropic chat path (B…
fix(chat): FC-4 request deadline budget for the Anthropic chat path (B…