Skip to content

Commit 4d3a776

Browse files
kartikkabadilidge-jun
authored andcommitted
feat(usage): record streamAborted on mid-stream-death attempts
Port the codex-router #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.
1 parent 420db62 commit 4d3a776

4 files changed

Lines changed: 313 additions & 6 deletions

File tree

‎src/server/relay.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ export function trackSseForRequestLog(
366366
): ReadableStream<Uint8Array> {
367367
const reader = body.getReader();
368368
let terminalReported = false;
369+
let cancelled = false;
369370

370371
const reportTerminal = (status: ResponsesTerminalStatus) => {
371372
if (terminalReported) return;
@@ -394,12 +395,19 @@ export function trackSseForRequestLog(
394395
inspector.feed(value);
395396
controller.enqueue(value);
396397
} catch (err) {
398+
// The upstream read rejected: the 200 body died mid-flight. Client
399+
// cancellation is the caller's separate 499 path, so a cancel-drained
400+
// pending read (cancelled=true) must not carry the truncation marker.
401+
if (!cancelled && !terminalReported && logCtx?.activeAttempt) {
402+
logCtx.activeAttempt.streamAborted = true;
403+
}
397404
if (!terminalReported) reportTerminal("incomplete");
398405
inspector.dispose();
399406
try { controller.error(err); } catch { /* already torn down */ }
400407
}
401408
},
402409
cancel(reason) {
410+
cancelled = true;
403411
inspector.dispose();
404412
onCancel();
405413
reader.cancel(reason).catch(() => {});
@@ -1122,6 +1130,10 @@ export function consumeForInspection(
11221130
if (logCtx) {
11231131
logCtx.transportPhase = "mid_stream";
11241132
logCtx.terminalSource = "synthetic";
1133+
// A truncated 200 body must not meter as a success the client never
1134+
// received; the router's equivalent turn carries 502 + streamAborted
1135+
// (codex-router #139).
1136+
if (logCtx.activeAttempt) logCtx.activeAttempt.streamAborted = true;
11251137
}
11261138
onTerminal("failed", 502);
11271139
}

‎src/usage/log.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ export interface PersistedUsageAttempt {
3636
adapter: string;
3737
status: number;
3838
durationMs: number;
39+
/**
40+
* True only when the upstream stream died after its 200 head was committed,
41+
* so the row must not meter as a success the client never received.
42+
* Absent on ordinary attempts so old rows keep their exact shape.
43+
*/
44+
streamAborted?: boolean;
3945
/** TTFT relative to THIS attempt's start (WP4); unset for non-streaming/tool-only. */
4046
firstOutputMs?: number;
4147
sendCount: number;
@@ -277,6 +283,8 @@ function normalizeUsageAttempt(raw: unknown): PersistedUsageAttempt | null {
277283
adapter: attempt.adapter,
278284
status: attempt.status,
279285
durationMs: attempt.durationMs,
286+
// Absent by default; only the literal `true` marker survives the round trip.
287+
...(attempt.streamAborted === true ? { streamAborted: true } : {}),
280288
...(isNonNegativeFiniteNumber(attempt.firstOutputMs)
281289
? { firstOutputMs: attempt.firstOutputMs }
282290
: {}),
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2+
import { mkdtempSync, rmSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
import { consumeForInspection, trackSseForRequestLog } from "../src/server/relay";
6+
import {
7+
addFinalRequestLog,
8+
addRequestLog,
9+
beginRequestAttempt,
10+
httpStatusForRequestLogTerminal,
11+
type RequestLogContext,
12+
} from "../src/server/request-log";
13+
import {
14+
readUsageEntries,
15+
resetUsageReadCacheForTests,
16+
type PersistedUsageAttempt,
17+
} from "../src/usage/log";
18+
19+
// Port of codex-router #139's streamAborted metering marker: an upstream stream
20+
// that dies after its 200 head was committed must meter as a truncated turn
21+
// (synthetic 502 + streamAborted), while a client cancellation keeps opencodex's
22+
// own 499 client_cancel semantics and never carries the marker.
23+
24+
const encoder = new TextEncoder();
25+
26+
let testDir = "";
27+
let previousHome: string | undefined;
28+
29+
beforeEach(() => {
30+
previousHome = process.env.OPENCODEX_HOME;
31+
testDir = mkdtempSync(join(tmpdir(), "ocx-stream-aborted-"));
32+
process.env.OPENCODEX_HOME = testDir;
33+
resetUsageReadCacheForTests();
34+
});
35+
36+
afterEach(() => {
37+
if (previousHome === undefined) delete process.env.OPENCODEX_HOME;
38+
else process.env.OPENCODEX_HOME = previousHome;
39+
if (testDir) rmSync(testDir, { recursive: true, force: true });
40+
});
41+
42+
function makeLogCtx(): { logCtx: RequestLogContext; attempt: PersistedUsageAttempt } {
43+
const attempt = beginRequestAttempt(1, "openai", "gpt-test", "openai-responses");
44+
const logCtx: RequestLogContext = {
45+
provider: "openai",
46+
model: "gpt-test",
47+
activeAttempt: attempt,
48+
activeAttemptStartedAt: Date.now(),
49+
attempts: [attempt],
50+
};
51+
return { logCtx, attempt };
52+
}
53+
54+
/** Enqueues one event (a 200 head is committed), then the upstream read dies. */
55+
function streamThatFailsMidStream(): ReadableStream<Uint8Array> {
56+
let reads = 0;
57+
return new ReadableStream<Uint8Array>({
58+
pull(controller) {
59+
reads += 1;
60+
if (reads === 1) {
61+
controller.enqueue(encoder.encode('data: {"type":"response.output_text.delta","delta":"hel"}\n\n'));
62+
} else {
63+
controller.error(new Error("socket reset"));
64+
}
65+
},
66+
});
67+
}
68+
69+
/** A stream whose read never resolves on its own; only cancel or abort ends it. */
70+
function pendingStream(): ReadableStream<Uint8Array> {
71+
return new ReadableStream<Uint8Array>({ start() {}, pull() { /* producer is test-controlled */ } });
72+
}
73+
74+
describe("streamAborted marker (codex-router #139)", () => {
75+
test("mid-stream death after a 200 head meters as 502 + streamAborted", async () => {
76+
const { logCtx, attempt } = makeLogCtx();
77+
const terminalReported = Promise.withResolvers<void>();
78+
const terminals: Array<[string, number | undefined]> = [];
79+
let cancels = 0;
80+
consumeForInspection(
81+
streamThatFailsMidStream(),
82+
(status, httpStatusOverride) => {
83+
terminals.push([status, httpStatusOverride]);
84+
terminalReported.resolve();
85+
},
86+
undefined,
87+
() => {},
88+
logCtx,
89+
() => { cancels += 1; },
90+
);
91+
await terminalReported.promise;
92+
expect(terminals).toEqual([["failed", 502]]);
93+
expect(cancels).toBe(0);
94+
expect(attempt.streamAborted).toBe(true);
95+
96+
// Finalize exactly like the native-passthrough terminal path in index.ts.
97+
addFinalRequestLog(
98+
"ocx-stream-aborted-e2e",
99+
Date.now(),
100+
logCtx,
101+
httpStatusForRequestLogTerminal("failed", logCtx),
102+
{ terminalStatus: "failed", closeReason: "terminal" },
103+
addRequestLog,
104+
);
105+
const [row] = readUsageEntries();
106+
expect(row?.status).toBe(502);
107+
expect(row?.attempts?.[0]?.status).toBe(502);
108+
expect(row?.attempts?.[0]?.streamAborted).toBe(true);
109+
});
110+
111+
test("client cancellation keeps 499 and never sets streamAborted", async () => {
112+
const { logCtx, attempt } = makeLogCtx();
113+
const ac = new AbortController();
114+
const cancelFired = Promise.withResolvers<void>();
115+
const doneFired = Promise.withResolvers<void>();
116+
const terminals: string[] = [];
117+
let cancels = 0;
118+
consumeForInspection(
119+
pendingStream(),
120+
status => { terminals.push(status); },
121+
ac.signal,
122+
() => doneFired.resolve(),
123+
logCtx,
124+
() => {
125+
cancels += 1;
126+
cancelFired.resolve();
127+
},
128+
);
129+
ac.abort();
130+
await Promise.all([cancelFired.promise, doneFired.promise]);
131+
expect(terminals).toEqual([]);
132+
expect(cancels).toBe(1);
133+
expect(attempt.streamAborted).toBeUndefined();
134+
135+
addFinalRequestLog(
136+
"ocx-cancel-e2e",
137+
Date.now(),
138+
logCtx,
139+
499,
140+
{ closeReason: "client_cancel" },
141+
addRequestLog,
142+
);
143+
const [row] = readUsageEntries();
144+
expect(row?.status).toBe(499);
145+
expect(row?.attempts?.[0]?.streamAborted).toBeUndefined();
146+
});
147+
148+
test("translated SSE read failure marks the attempt streamAborted", async () => {
149+
const { logCtx, attempt } = makeLogCtx();
150+
const terminals: string[] = [];
151+
let cancels = 0;
152+
const relayed = trackSseForRequestLog(
153+
streamThatFailsMidStream(),
154+
status => { terminals.push(status); },
155+
() => { cancels += 1; },
156+
logCtx,
157+
);
158+
await expect(new Response(relayed).text()).rejects.toThrow("socket reset");
159+
expect(terminals).toEqual(["incomplete"]);
160+
expect(cancels).toBe(0);
161+
expect(attempt.streamAborted).toBe(true);
162+
});
163+
164+
test("translated SSE client cancel never sets the truncation marker", async () => {
165+
const { logCtx, attempt } = makeLogCtx();
166+
const cancelFired = Promise.withResolvers<void>();
167+
const terminals: string[] = [];
168+
let cancels = 0;
169+
const relayed = trackSseForRequestLog(
170+
pendingStream(),
171+
status => { terminals.push(status); },
172+
() => {
173+
cancels += 1;
174+
cancelFired.resolve();
175+
},
176+
logCtx,
177+
);
178+
await relayed.getReader().cancel(new DOMException("client closed", "AbortError"));
179+
await cancelFired.promise;
180+
expect(cancels).toBe(1);
181+
expect(terminals).toEqual([]);
182+
expect(attempt.streamAborted).toBeUndefined();
183+
184+
addFinalRequestLog(
185+
"ocx-cancel-translated",
186+
Date.now(),
187+
logCtx,
188+
499,
189+
{ closeReason: "client_cancel" },
190+
addRequestLog,
191+
);
192+
const [row] = readUsageEntries();
193+
expect(row?.status).toBe(499);
194+
expect(row?.attempts?.[0]?.streamAborted).toBeUndefined();
195+
});
196+
});

‎tests/usage-log.test.ts‎

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,115 @@ describe("usage log", () => {
9595
provider: "blsc",
9696
model: "blsc/DeepSeek-V4-Flash",
9797
status: 429,
98-
durationMs: 4,
99-
usageStatus: "reported",
98+
durationMs: 1,
99+
usageStatus: "unreported",
100100
attempts: [{
101101
ordinal: 1,
102102
provider: "blsc",
103103
model: "blsc/DeepSeek-V4-Flash",
104104
adapter: "openai-chat",
105105
status: 429,
106-
durationMs: 4,
107-
sendCount: 2,
108-
recoveryKinds: ["rate-limit-429", "rate-limit-429"],
109-
usageStatus: "reported",
106+
durationMs: 1,
107+
sendCount: 1,
108+
recoveryKinds: ["rate-limit-429"],
109+
usageStatus: "unreported",
110110
}],
111111
};
112112
appendUsageEntry(entry);
113113
expect(readUsageEntries()[0]?.attempts?.[0]?.recoveryKinds).toEqual(["rate-limit-429"]);
114114
});
115115

116+
test("persists the streamAborted marker on truncated-stream attempts", () => {
117+
appendUsageEntry({
118+
requestId: "ocx-stream-aborted",
119+
timestamp: 1,
120+
provider: "openai",
121+
model: "gpt-test",
122+
status: 502,
123+
durationMs: 3,
124+
usageStatus: "unreported",
125+
terminalStatus: "failed",
126+
closeReason: "terminal",
127+
attempts: [{
128+
ordinal: 1,
129+
provider: "openai",
130+
model: "gpt-test",
131+
adapter: "openai-responses",
132+
status: 502,
133+
durationMs: 3,
134+
sendCount: 1,
135+
recoveryKinds: [],
136+
usageStatus: "unreported",
137+
streamAborted: true,
138+
}],
139+
});
140+
const raw = readFileSync(usageLogPath(), "utf-8");
141+
expect(raw).toContain('"streamAborted":true');
142+
expect(readUsageEntries()[0]?.attempts?.[0]?.streamAborted).toBe(true);
143+
});
144+
145+
test("ordinary attempts omit the streamAborted marker (backward compatible)", () => {
146+
const attempt: PersistedUsageAttempt = {
147+
ordinal: 1,
148+
provider: "openai",
149+
model: "gpt-test",
150+
adapter: "openai-responses",
151+
status: 200,
152+
durationMs: 3,
153+
sendCount: 1,
154+
recoveryKinds: [],
155+
usageStatus: "reported",
156+
};
157+
const base: PersistedUsageEntry = {
158+
requestId: "ocx-stream-aborted-absent",
159+
timestamp: 1,
160+
provider: "openai",
161+
model: "gpt-test",
162+
status: 200,
163+
durationMs: 3,
164+
usageStatus: "reported",
165+
attempts: [attempt],
166+
};
167+
for (const marker of [undefined, false]) {
168+
const normalized = normalizeUsageEntryForTest({
169+
...base,
170+
attempts: [{ ...attempt, ...(marker === undefined ? {} : { streamAborted: marker }) }],
171+
});
172+
expect(normalized.attempts?.[0]).not.toHaveProperty("streamAborted");
173+
}
174+
const marked = normalizeUsageEntryForTest({
175+
...base,
176+
attempts: [{ ...attempt, streamAborted: true }],
177+
});
178+
expect(marked.attempts?.[0]?.streamAborted).toBe(true);
179+
});
180+
181+
test("legacy attempts without streamAborted stay readable and unset", () => {
182+
writeFileSync(usageLogPath(), `${JSON.stringify({
183+
requestId: "legacy-no-marker",
184+
timestamp: 1,
185+
provider: "openai",
186+
model: "gpt-test",
187+
status: 200,
188+
durationMs: 3,
189+
usageStatus: "reported",
190+
attempts: [{
191+
ordinal: 1,
192+
provider: "openai",
193+
model: "gpt-test",
194+
adapter: "openai-responses",
195+
status: 200,
196+
durationMs: 3,
197+
sendCount: 1,
198+
recoveryKinds: [],
199+
usageStatus: "reported",
200+
}],
201+
})}\n`);
202+
const attempt = readUsageEntries()[0]?.attempts?.[0];
203+
expect(attempt?.ordinal).toBe(1);
204+
expect(attempt).not.toHaveProperty("streamAborted");
205+
});
206+
116207
/** Build one minimal persisted-usage JSONL line for the given request id. */
117208
const persistedLine = (requestId: string) => JSON.stringify({
118209
requestId,

0 commit comments

Comments
 (0)