Skip to content

Fix native Responses recovery after prelude-only EOF - #8292

Merged
lidge-jun merged 27 commits into
devfrom
codex/8231-prelude-eof
Nov 12, 2026
Merged

lidge-jun merged 27 commits into
devfrom
codex/8231-prelude-eof

Conversation

@lidge-jun

@lidge-jun lidge-jun commented Nov 7, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Recover eligible native Responses streams that end after an uncommitted lifecycle prelude, using the existing replay grant and send budget.
  • Preserve output and error snapshots, conflicting SSE events and unfinished frames; abort recovery on downstream cancellation.
  • Select replacement response metadata before headers are committed, bind quota evidence to the published attempt, and keep inactivity accounting tied to real upstream reads.

Closes #8231

Verification

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

@coderabbitai

coderabbitai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

✅ Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added the bug Something isn't working label Nov 7, 2026
@lidge-jun lidge-jun added the ci:full Run the four full Ubuntu test shards on this pull request label Nov 8, 2026
Native passthrough reset recovery returned the original HTTP head at once and
then refused any replacement whose status or headers differed. Per-send quota
headers, request ids and x-codex-turn-state always differ, so an ordinary
pre-output reset or clean EOF could no longer recover.

Dispatch now starts the protocol preflight immediately and waits for the first
of selection, abort, or one heartbeat interval capped by the stall timeout. A
selection that finishes first publishes the selected response, including a
replacement with its own status, headers and body. When the deadline wins, the
original head is returned and the same preflight continues behind it; a
replacement must then match that head, checked at acceptance and on arrival.

The published head is bound to the main quota proof of the attempt that
produced it, so a renewed context proof cannot move ownership. The OpenGateway
completion hold is shortened by the time selection already held the head.
Abort ends selection or the hold at once without awaiting source cleanup.
Pre-head reset selection holds a canonical buffered turn for up to one
heartbeat interval (capped by the stall budget) before the collector starts.
The collector then began a fresh first-byte clock and treated any delivered
byte as activity, so a silent upstream with a 5 s stall budget failed after
about 10 s, a prelude read during selection and replayed later reset the
clock, and a quiet replacement behind a committed head inherited the
original's last activity and was cut short.

Guard the original body and any accepted replacement at their upstream reads
for the canonical buffered consumer, as OpenGateway already is, so each
physical attempt's stall budget runs from its own head. The collector reports
the guard's failure as a stall timeout, which the reset gate never resends.
The held stream also reports a replacement head that lands behind a committed
head, so the collector's own byte clock never ends a turn before the guard.
Native reset selection held a direct streaming client's response head for
up to one 15-second heartbeat interval while it inspected the original
upstream. A client with a shorter header timeout failed on a healthy slow
turn, including requests the resend gate could never replace.

A request that is not self-contained now skips the preflight entirely,
so its head and prelude are relayed as for a provider without reset
replay. A direct stream commits the original head once the original's own
inspection has run for 250 ms; inspection continues behind that head and
a replacement must then match it exactly. A failure observed inside the
window still waits for the replacement's own head, bounded by the
existing deadline. Bun's server writes a head only with the first body
bytes, so the window commit owes the client an SSE comment at once, as
the deadline commit already did. Canonical buffered JSON and combo
children keep the deadline because their consumers publish no head first.

The physical replay guard memoizes its verdict for the body that left, so
the eligibility check does not parse a large outbound body twice.
The quota observation case for a head committed before a reset used a
one-second stall budget as its commitment timer. The same budget arms the
upstream inactivity guard on the original body, and a guard timeout is
never replayed, so on a slow runner the guard could win and the case saw
one send instead of two.

The case now arms the fixture clock to capture the direct-stream
inspection window and fires it itself before releasing the reset, while
the inactivity guard keeps the default 300-second budget. The window
moves from 250 ms to 200 ms because the fixture clock already claims
every 250 ms timer as the quota persistence debounce, which had silently
swallowed the window in this file.
cpa added 2 commits November 9, 2026 10:05
# Conflicts:
#	src/server/responses/combo-stream-preflight.ts
#	structure/transports/responses-combo-boundaries.md
#	tests/routing/combo-stream-preflight.test.ts
@lidge-jun

Copy link
Copy Markdown
Owner Author

Maintainer judgment: hold (not merging yet)

Taken over from the unreachable lane. At cfe670c the branch is merged with dev (conflicts with #8152 resolved; Codex metadata frames stay non-committing control frames), and an independent review passed with nits. One finding is a regression for existing users, so this stays a draft until the policy below is decided.

Regression: post-head reset recovery becomes unreachable. On dev, a direct native stream that resets mid-body behind an already-returned head is replaced whenever the replacement is an event stream (acceptResponse in passthrough-dispatch.ts). This PR returns the head after a 200 ms inspection window and, from then on, accepts a replacement only if its status, status text and complete header set equal the committed head (sameDeferredResponseMetadata). Real origins vary date, x-request-id, x-codex-turn-state and quota headers on every attempt (the reviewer measured date differing one second apart), so in practice every reset more than 200 ms into a stream, which is where most resets happen, now keeps the error. Users with retryOnReset lose recovery they have today.

I tried relaxing the check to status plus media type. It breaks 23 tests that deliberately pin refusal on a different model, quota, turn state, request id or set-cookie header, including "never publishes replacement quota" in the quota observation suite. Those encode a deliberate identity contract, so choosing which headers define the head's identity is a design decision, not a fix I should make inside a takeover:

  • (a) Keep the strict contract and document that post-head recovery no longer applies; this removes a shipped behavior.
  • (b) Compare status, media type and identity headers only (served model, account/credential identity), and ignore per-attempt ones (date, request ids, turn state, quota readings). The client keeps the original head's values, as on dev.
  • (c) Keep dev's post-head acceptance unchanged, and apply the strict check only to the new prelude-only EOF path this PR adds.

Fixed at cfe670c: a replacement authorization (createPhysicalReplayGuard) is now released when its send fails before leaving or is refused, so the next ordinary send is not judged as a replacement. New unit cases cover the guard, including hosted and provider-executed snapshots; two fail on the previous head.

Verification at cfe670c (isolated homes, one file at a time): responses-reset-replay 40/0, combo-stream-preflight 429/0, responses-reset-selection-http 6/0, responses-canonical-nonstream 82/0, responses-main-quota-observation 39/0, empty-completion-core 48/0, responses-passthrough-transient-policy 9/0; typecheck clean. NOT RUN: full suite, live provider streams.

…carry

# Conflicts:
#	src/server/responses/combo-stream-preflight.ts
#	src/server/responses/passthrough-dispatch.ts
#	src/server/responses/reset-replay.ts
#	structure/transports/responses-failover.md
#	tests/fixtures/test-layout-expected.json
#	tests/routing/combo-stream-preflight.test.ts
…relude

A native Responses SSE body that closes cleanly after only lifecycle events
left the client with a head and no output. The recovery lane already replaced
that body, but only when the replacement's complete header set matched the
committed one. Real origins vary `date`, `x-request-id`, `x-codex-turn-state`
and the quota headers per attempt, so post-head recovery was unreachable, and
the same check also narrowed the reset lane, which had always accepted any
replacement after a mid-stream reset.

Acceptance now follows how the stream ended. A read error keeps the reset
lane's behaviour unchanged. A clean pre-output EOF behind a committed head
compares a normalized identity subset instead: status, status text,
content-type media type, `openai-model` and `x-codex-turn-state`. The first is
the model the origin actually served; the second is this proxy's own turn
handle, bound at issuance and redeemed only at a matching physical send.
Volatile and hop-by-hop headers are ignored because the client already holds
the original's values.

The physical replay guard moved to `native-replay-guard.ts` so dispatch stays
under its line cap, and `record` clears a pending authorization in `finally`
so a send that throws before leaving cannot fence a later unrelated send.

Covered by real local HTTP tests for both post-window outcomes and by unit
tests for the guard's throwing, hosted and provider-executed paths.
…nventory

The replay guard extracted from passthrough dispatch is reached from core.ts, so the source-oracle inventory must list it (responses-core-modules).
@lidge-jun

Copy link
Copy Markdown
Owner Author

Maintainer judgment: merge

Head reviewed: ad91d21e22f17219ea83437178fe8bd40cb6c2fb. This is the original lane's commits through cfe670c, carried after that lane went offline. The carry adds dev merges 387e6db1b and 74940b8ba, the option (c) fix 933499df1, and the module-inventory fix ad91d21e2.

Value. Fixes #8231. A native Responses SSE body that closed cleanly after only its lifecycle events left the client with a response head and no output. The proxy now recovers such a stream with one bounded replacement. While the head is still open, which lasts up to 200 ms, the replacement publishes its own head. After the head is committed, the replacement is accepted only if it matches the original's identity. No other open PR covers this.

Owner decision applied (option c). dev's reset recovery is unchanged: after a mid-stream read error, any replacement is accepted, as before. Only the new clean pre-output EOF path compares a normalized identity subset: status, status text, the content-type media type, openai-model and x-codex-turn-state. Volatile and hop-by-hop headers (date, request ids, rate-limit headers, content-length, and so on) are ignored. The earlier rule of complete header equality made post-head recovery unreachable on real origins and also narrowed dev's reset lane.

User-visible change. For a reset inside the 200 ms window, before the head is committed, the client now receives the replacement's own head. Only the replacement attempt's quota is published, under that attempt's proof (99% versus the arrival attempt's 17% in the fixture). dev's own test comment called replacement-header publication "a follow-up". That is the one dev test that changes; the review confirmed it is correct and safe. An unbound response still falls back to the prior dispatch. Behind a committed head, an upstream that rotates x-codex-turn-state on every attempt is not recovered. Publishing body B under attempt A's token would bind the client's next turn to the wrong state, so declining is the conservative outcome. The pre-head window still recovers that case. Config, defaults and CLI output are unchanged. Docs: docs-site/.../reference/configuration/providers.md (plus 7 locales), structure/transports/responses-combo-boundaries.md and responses-failover.md.

No regression for existing users. I ran dev's own versions of the reset-replay, canonical-nonstream, combo-stream-preflight and chained-turn (#8453) suites against this tree, and they pass unchanged. The #8453 fix (chained turns are not rejected as replacement targets) is preserved through both merges. The physical replay guard now clears its pending authorization in finally, so a send that throws cannot fence a later unrelated send. passthrough-dispatch.ts is 1,999 of its 2,000 lines; the guard moved to native-replay-guard.ts, which is registered in the core module inventory.

Windows. No platform-specific code changed.

Evidence.

  • Fresh independent review of this exact head (DeepSeek via combo/deepseek-flash-pool): PASS-WITH-NITS. It ran 7 real Bun.serve upstream cases: post-window EOF differing only in date or x-request-id is recovered; an openai-model mismatch is refused; a turn-state mismatch is refused with no third send; a read error after the head accepts arbitrary headers; there is no replay after delivered output; abort settles in about 220 ms; and a chained previous_response_id EOF is recovered. Mutations: replacing the identity check with full-header equality fails the real-HTTP recovery case plus 10 replay cases, and removing the finally fails 4 guard cases. Its focused suite: 974 pass / 0 fail. Its three nits are left as follow-ups: the x-codex-turn-state comment describes the WS-lane handle although this lane relays the raw upstream token; the docs do not yet say that an upstream rotating that token does not recover after the head is committed; and deferProtocolSafeResetRecovery is now test-only.
  • My own runs in an isolated home: 1,234 pass / 0 fail across 13 suites on 74940b8; 138 pass / 0 fail across the 7 inventory readers on ad91d21; typecheck and structure:check clean.
  • CI: all 32 checks pass on this head. The earlier test 5/5 failure on 74940b8 was the missing inventory entry, fixed in ad91d21.

NOT RUN: full bun run test; macOS and Windows lanes.

@lidge-jun
lidge-jun marked this pull request as ready for review November 12, 2026 07:28
@lidge-jun
lidge-jun requested a review from Ingwannu as a code owner November 12, 2026 07:28
@lidge-jun
lidge-jun merged commit aa30ea9 into dev Nov 12, 2026
32 checks passed
@lidge-jun
lidge-jun deleted the codex/8231-prelude-eof branch November 12, 2026 07:28
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T07:32:17.835501Z ad91d21 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

lidge-jun added a commit that referenced this pull request Nov 12, 2026
#8292 landed on dev as aa30ea9, so this branch takes dev's tree (the final #8292) and
re-applies only its own change on top. The provider docs keep #8292's post-head rules and
replace the #8347 sentence about when a replacement supplies its own head.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ci:full Run the four full Ubuntu test shards on this pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] retryOnReset does not replace a native prelude-only clean EOF

1 participant