feat(usage): record streamAborted on mid-stream-death attempts - #1652
feat(usage): record streamAborted on mid-stream-death attempts#1652kartikkabadi wants to merge 1 commit into
Conversation
Port the codex-router lidge-jun#139 streamAborted metering marker: when an upstream stream dies after its 200 head was committed, the persisted usage attempt now carries streamAborted: true alongside the synthetic 502 terminal so the turn never meters as a success the client did not receive. - usage/log.ts: optional streamAborted on PersistedUsageAttempt, whitelisted in normalizeUsageAttempt (only the literal true marker survives the round trip; ordinary and legacy rows keep their exact shape). - relay.ts: set the marker on consumeForInspection's mid-stream read-failure path (failed/502) and on trackSseForRequestLog's read-failure path (incomplete/502). Client-cancel (499) semantics are untouched and never carry the marker. - tests: usage-log.test.ts gains persistence/backward-compat coverage; stream-aborted-marker.test.ts drives the relay paths end to end through addFinalRequestLog to the persisted JSONL row.
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
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. Comment |
|
✅ Deterministic PR hygiene checks passed. |
⏳ DRAFT
What to do
Review readiness checklist
0/4 boxes ticked. This PR stays in draft until every box above is ticked. |
Summary
Ports codex-router PR #139's
streamAbortedmetering marker into opencodex: when an upstream stream dies after its 200 head was already committed, the persisted usage attempt must not meter as a success the client never received. The turn is recorded with the synthetic 502 terminal plusstreamAborted: true; client cancellations keep opencodex's own 499client_cancelsemantics and never carry the marker.Changes
src/usage/log.ts— optionalstreamAborted?: booleanonPersistedUsageAttempt, whitelisted innormalizeUsageAttemptso only the literaltruemarker survives the round trip. Backward compatible: ordinary and legacy rows keep their exact JSONL shape.src/server/relay.ts— set the marker on both mid-stream read-failure paths:consumeForInspectiononReadError(native passthrough):onTerminal("failed", 502)plusstreamAborted: trueon the active attempt.trackSseForRequestLogread failure (translated responses):incomplete/502 terminal plus the marker, guarded so a cancel-drained pending read never pollutes the 499 row.tests/usage-log.test.ts— 4 new tests: marker persistence, backward-compatible omission (undefined/false), legacy rows stay readable, and raw JSONL round trip.tests/stream-aborted-marker.test.ts(new) — drives both relay paths end to end throughaddFinalRequestLogto the persisted JSONL row: mid-stream death meters 502 +streamAborted: true; client cancel meters 499 with no marker.Verification
bun test tests/usage-log.test.ts— 34 pass (30 pre-existing + 4 new).bun test tests/stream-aborted-marker.test.ts— 4 pass (new file).tests/consume-for-inspection-cancel.test.ts,tests/sse-inspector-bounds.test.ts— 34 pass.bunx tsc --noEmit— clean for the files in this PR (remaining errors are peers' in-flight edits in other branches' files:src/lib/token-estimate.ts,src/server/responses/empty-completion-guard.ts).Coordination notes
src/server/request-log.tswas not modified (TokenCapPort owns it).addFinalRequestLog's existing...attemptspread carries the marker into the persisted row; the only shared assumption islogCtx.activeAttemptmutation, which relay.ts already uses fortransportPhase/terminalSource.tests/usage-log.test.tsalso carries a small pre-existing working-tree edit to the "persists the rate-limit-429 recovery kind" test (durationMs4→1,sendCount2→1, dedupedrecoveryKinds,usageStatusreported→unreported) that was present before this branch's work and was kept as-is per main-agent direction; it is not authored by this PR.relay-eager.tsonSynthetic"failed", wired inresponses/core.ts) synthesizes the same 502 terminal but does not yet setstreamAborted. Follow-up recommended.Review readiness checklist
This PR stays in draft until every box below is ticked. Tick all four boxes once the requirements are met:
All CI tests are green on my local testing.
I pushed my PR to the latest dev commit.
I resolved all correct Codex and CodeRabbit findings.
My PR is ready for review.