Repository navigation
Codex on a ChatGPT account: finished turns are no longer cancelled, and their usage is read - #310
Merged
Merged
Conversation
…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>
This was referenced Oct 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/responsesover HTTP and stops reading as soon as it hasresponse.completed, dropping the connection without waiting for the stream to end (codexcodex-api/src/sse/responses.rs, testemits_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 byEnding's Drop. The ChatGPT backend often ends its stream a little afterresponse.completed, so most turns became "cancelled".tw_dialect::convert::ends_answer: Responsesresponse.completed/incomplete/failed, Anthropicmessage_stop, Chat[DONE]) and marks the endingdelivered(). Dropped after that, the request is recorded as finished (status, usage, cost, timings), or as the upstream's failure forresponse.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
usagecarries a per-itemattributionthat 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_requestanda_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 --workspacepass locally.Not confirmed against the real backend: the exact shape/size of
attribution, and the stream ending shortly afterresponse.completed(inferred from the report).🤖 Generated with Claude Code