diff --git a/src/stream-terminal.ts b/src/stream-terminal.ts index 7bfc69694b..1b08ede935 100644 --- a/src/stream-terminal.ts +++ b/src/stream-terminal.ts @@ -103,7 +103,9 @@ async function observeJsonTerminalState(stream: ReadableStream): Pro } if (typeof body !== "object" || body === null) return "unknown"; const rec = body as Record; - if (rec.error !== undefined) return "failed"; + if (rec.error !== undefined && rec.error !== null) return "failed"; + if (rec.status === "failed" || rec.status === "incomplete" || rec.status === "cancelled") return "failed"; + if (rec.status !== undefined && rec.status !== "completed") return "unknown"; if (Array.isArray(rec.output)) return "completed"; return "unknown"; } diff --git a/tests/native-compact-terminal.test.ts b/tests/native-compact-terminal.test.ts index 9b8a7ca65c..55f60da26b 100644 --- a/tests/native-compact-terminal.test.ts +++ b/tests/native-compact-terminal.test.ts @@ -54,6 +54,11 @@ test("e2e #321 PR-C: rebase only on completed; trigger request forwarded clean", res.end(JSON.stringify({ error: { message: "compact failed" } })); return; } + if (mode?.startsWith("status-")) { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ status: mode.slice(7), error: null, output: [] })); + return; + } if (mode === "empty") { res.writeHead(200, { "content-type": "application/json" }); res.end(); @@ -187,6 +192,17 @@ test("e2e #321 PR-C: rebase only on completed; trigger request forwarded clean", await r5.text(); assert.equal(pendingRebase("term-empty"), undefined, "empty body must not schedule a rebase"); + for (const status of ["failed", "incomplete", "cancelled", "queued", "in_progress"]) { + const label = `term-json-${status}`; + const response = await postResponses(`/responses/compact?mode=status-${status}`, { model: "gpt-resp", session_id: label }); + assert.equal(response.status, 200); + await response.text(); + assert.equal(pendingRebase(label), undefined, `${status} JSON output must not schedule a rebase`); + } + const completed = await postResponses("/responses/compact?mode=status-completed", { model: "gpt-resp", session_id: "term-null-error" }); + await completed.text(); + assert.equal(pendingRebase("term-null-error"), true, "a null error must not suppress completed compaction"); + // 6. Endpoint form, JSON with output → rebase (the #249 happy path). const r6 = await postResponses("/responses/compact", { model: "gpt-resp", session_id: "term-jsonok" }); assert.equal(r6.status, 200); diff --git a/tests/stream-terminal.test.ts b/tests/stream-terminal.test.ts index 9b592f298e..14cb3f8c9e 100644 --- a/tests/stream-terminal.test.ts +++ b/tests/stream-terminal.test.ts @@ -95,6 +95,35 @@ test("JSON: error field present → failed", async () => { assert.equal(await observeResponsesTerminalState(stream, false), "failed"); }); +for (const status of ["failed", "incomplete", "cancelled"]) { + test(`JSON: ${status} output never permits a native-compaction rebase`, async () => { + const stream = sseStream([JSON.stringify({ status, output: [{ type: "compaction" }] })]); + assert.equal(await observeResponsesTerminalState(stream, false), "failed"); + }); +} + +for (const status of ["queued", "in_progress", "future_status", null, 1]) { + test(`JSON: nonterminal or unknown status ${JSON.stringify(status)} stays unknown`, async () => { + const stream = sseStream([JSON.stringify({ status, output: [] })]); + assert.equal(await observeResponsesTerminalState(stream, false), "unknown"); + }); +} + +test("JSON: completed output with a null error is successful", async () => { + const stream = sseStream([JSON.stringify({ status: "completed", error: null, output: [] })]); + assert.equal(await observeResponsesTerminalState(stream, false), "completed"); +}); + +test("JSON: a completed claim carrying a real error still fails", async () => { + const stream = sseStream([JSON.stringify({ status: "completed", error: { message: "boom" }, output: [] })]); + assert.equal(await observeResponsesTerminalState(stream, false), "failed"); +}); + +test("JSON: compact endpoint output without a status remains supported", async () => { + const stream = sseStream([JSON.stringify({ object: "response.compaction", output: [] })]); + assert.equal(await observeResponsesTerminalState(stream, false), "completed"); +}); + test("JSON: unparseable body → unknown", async () => { assert.equal(await observeResponsesTerminalState(sseStream(["502 bad gateway"]), false), "unknown"); });