Skip to content

Codex on a ChatGPT account: finished turns are no longer cancelled, and their usage is read - #310

Merged
fylorn merged 1 commit into
mainfrom
fix/codex-chatgpt-endings
Oct 10, 2026
Merged

fylorn merged 1 commit into
mainfrom
fix/codex-chatgpt-endings

Conversation

@fylorn

@fylorn fylorn commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Reported by a user: with the Codex desktop app on an upstream signed in with a ChatGPT account, the traffic list shows the turns as cancelled although they complete, and even the occasional 200 has no tokens or cost.

1. Finished turns recorded as cancelled. With the config Lite writes for Codex (supports_websockets = false), Codex streams /v1/responses over HTTP and stops reading as soon as it has response.completed, dropping the connection without waiting for the stream to end (codex codex-api/src/sse/responses.rs, test emits_completed_without_stream_end). Core only reported the ending when the upstream stream reached its end, so a client hanging up first was reported as a cancellation by Ending's Drop. The ChatGPT backend often ends its stream a little after response.completed, so most turns became "cancelled".

  • The relay now recognises the answer's last frame in what it sends to the client (tw_dialect::convert::ends_answer: Responses response.completed / incomplete / failed, Anthropic message_stop, Chat [DONE]) and marks the ending delivered(). Dropped after that, the request is recorded as finished (status, usage, cost, timings), or as the upstream's failure for response.failed. A client that leaves before the last frame is still cancelled; a manual abort still records as aborted; a stream that runs to its end is unchanged.

2. No usage on longer Codex sessions. The ChatGPT backend's usage carries a per-item attribution that grows with the conversation. The usage reader only accepted objects up to 8 KB and couldn't rejoin one split more than 4 KB back. It now reads usage objects up to 1 MiB, continues a split object instead of rescanning, skips anything nested inside a usage object once read, and no longer misses the key when chunks are shorter than "usage.

Tests: codex_closing_right_after_response_completed_is_a_finished_request and a_long_attribution_in_the_usage_does_not_hide_the_tokens (both failed before), plus unit tests for the reader (whole, 16 KB / 1000 / 7-byte chunks, byte by byte, an unclosed object), ends_answer, and drop-after-delivery. cargo fmt --check, cargo clippy --workspace --all-targets -D warnings, cargo test --workspace pass locally.

Not confirmed against the real backend: the exact shape/size of attribution, and the stream ending shortly after response.completed (inferred from the report).

🤖 Generated with Claude Code

…nd their usage is read

Two bugs made every Codex turn through a ChatGPT-account upstream show up as
cancelled, with no tokens or cost even on the rare row that ended with 200.

1. Codex hangs up as soon as it has `response.completed`. Its SSE reader
   (openai/codex codex-api/src/sse/responses.rs, process_sse_with_treatment)
   returns right after handing over `Completed` and drops the byte stream, so
   the connection closes without waiting for the stream to end
   (`emits_completed_without_stream_end` pins this). The gateway reported a
   request's ending only when the upstream stream reached its end; when the
   end arrived after the last event, the client was already gone and Drop
   reported a cancellation.

   The relay now watches what it sends to the client for the answer's last
   frame (Responses `response.completed`/`response.incomplete`/
   `response.failed`, Anthropic `message_stop`, Chat `[DONE]`; new
   `tw_dialect::convert::ends_answer`). Once that frame has been handed over,
   `Ending::delivered` is set, and a later drop reports the request as
   finished (or as the upstream's failure, for `response.failed`) with its
   usage, instead of cancelled. A request whose stream runs to its end is
   reported exactly as before.

2. The Codex backend's `usage` carries an `attribution` breakdown per item,
   which grows with the conversation. The usage sniffer only read objects up
   to 8 KB (and could only rejoin an object cut more than 4 KB back), so a
   longer Codex conversation recorded no tokens at all. Objects up to 1 MiB
   are now read, and an object cut across chunks is resumed where the brace
   matching stopped instead of rescanned. A parsed usage object is skipped as
   a whole, so nothing nested inside it is merged as a second usage. Chunks
   shorter than the `"usage` needle now extend the carry window instead of
   replacing it (a usage key cut over three or more chunks was missed).

Tests: a fake Codex backend that keeps the stream open after
`response.completed` and a client that hangs up like Codex
(tests/chatgpt.rs), a usage with a long attribution, and unit tests for the
sniffer, `ends_answer` and the delivered drop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fylorn
fylorn merged commit 5d51a96 into main Oct 10, 2026
5 checks passed
@fylorn
fylorn deleted the fix/codex-chatgpt-endings branch October 10, 2026 03:21
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.

1 participant