Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/stream-terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ async function observeJsonTerminalState(stream: ReadableStream<Uint8Array>): Pro
}
if (typeof body !== "object" || body === null) return "unknown";
const rec = body as Record<string, unknown>;
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";
}
16 changes: 16 additions & 0 deletions tests/native-compact-terminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
29 changes: 29 additions & 0 deletions tests/stream-terminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(["<html>502 bad gateway</html>"]), false), "unknown");
});
Expand Down
Loading