|
| 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 | +}); |
0 commit comments