Files
OmniRoute/tests/unit/responses-continuation-store.test.ts
Markus Hartung 955b28ef5c feat(dashboard): badge a conversation that never reached a clean stop (#12717)
Uma conversa que nunca chegou a parada limpa e não sinaliza nada é o pior estado possível de UI: indistinguível de uma que terminou. O incidente que você cita no comentário do teste — stream pesado em reasoning estourando o cap do coletor no meio, deixando a conversa presa sem sinal — é exatamente o caso que justifica o badge.

Separar `resolveTurnCompletionState` de `resolveConversationStalledState` também está certo: `tool_call_pending` é um estado legítimo em voo, não uma conversa travada.

Revalidei sobre o tip: **29/29**, typecheck:core limpo.

**Nota de integração.** O `tests/unit/responses-continuation-store.test.ts` conflitou com o #12854, que anexa a própria bateria ao mesmo arquivo. Reconstruí o arquivo como append limpo — versão do tip mais o seu bloco de 184 linhas, verificado por `esbuild` antes de rodar. Registro por que importa: na primeira tentativa eu apenas retirei os marcadores de conflito, e isso enfiou os seus testes **dentro** de um objeto literal não terminado do #12854. Compilava como erro de transform, não como conflito — só apareceu ao rodar. Resolver JSON e teste "aditivamente" sem verificar a sintaxe depois é armadilha; ficou a lição.
2026-09-10 10:52:03 -03:00

790 lines
31 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
// OmniRoute-native `previous_response_id` virtualization: resolvePreviousResponseState
// resolves a response id back to the full input/output a prior call produced by
// reading the already-persisted call-log artifact, so a later request can be
// reconstructed to full history server-side without duplicating conversation
// content into a second store.
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-responses-continuation-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../src/lib/db/core.ts");
const store = await import("../../src/lib/db/responsesContinuationStore.ts");
const callLogs = await import("../../src/lib/usage/callLogs.ts");
test.after(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
function insertCallLog(row: {
id: string;
responseId: string | null;
apiKeyId: string | null;
detailState: string;
artifactRelPath: string | null;
videoContentRemoved?: 0 | 1;
}) {
const db = core.getDbInstance();
db.prepare(
`INSERT INTO call_logs
(id, timestamp, method, path, status, model, provider, account, duration,
tokens_in, tokens_out, api_key_id, detail_state, artifact_relpath, response_id,
video_content_removed)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
).run(
row.id,
new Date().toISOString(),
"POST",
"/v1/responses",
200,
"gpt-5.4-pro",
"openai",
"acc1",
100,
10,
20,
row.apiKeyId,
row.detailState,
row.artifactRelPath,
row.responseId,
row.videoContentRemoved ?? 0
);
}
function writeArtifact(relPath: string, pipeline: Record<string, unknown>) {
const absPath = path.join(TEST_DATA_DIR, "call_logs", relPath);
fs.mkdirSync(path.dirname(absPath), { recursive: true });
fs.writeFileSync(
absPath,
JSON.stringify({
schemaVersion: 5,
summary: {},
requestBody: null,
responseBody: null,
error: null,
pipeline,
})
);
}
test("resolvePreviousResponseState reconstructs input/output from the call-log artifact", () => {
insertCallLog({
id: "log-1",
responseId: "resp_abc",
apiKeyId: "key-1",
detailState: "ready",
artifactRelPath: "2026-01-01/log-1.json",
});
writeArtifact("2026-01-01/log-1.json", {
clientRawRequest: { body: { input: [{ type: "message", role: "user", content: "hi" }] } },
providerRequest: { body: { input: [{ type: "message", role: "user", content: "hi" }] } },
clientResponse: {
id: "resp_abc",
output: [{ type: "message", role: "assistant", content: "hello" }],
},
});
const result = store.resolvePreviousResponseState("resp_abc", "key-1");
assert.deepEqual(result, {
input: [{ type: "message", role: "user", content: "hi" }],
output: [{ type: "message", role: "assistant", content: "hello" }],
});
});
test("resolvePreviousResponseState reads output from a wrapped (streaming) clientResponse shape", () => {
// A streaming reply's clientResponse is clientPayloadCollector.build()'s output,
// which always nests the caller-supplied summary under `.summary` (see
// createStructuredSSECollector in streamPayloadCollector.ts) rather than
// carrying `output` at the top level like a non-streaming reply does. This
// must resolve exactly like the unwrapped shape above -- it was the actual
// cause of previous_response_id continuation always failing for a streaming
// Responses-API passthrough connection (fixed alongside the clientPayload
// builder gap in open-sse/utils/stream.ts).
insertCallLog({
id: "log-1-streamed",
responseId: "resp_streamed",
apiKeyId: "key-1",
detailState: "ready",
artifactRelPath: "2026-01-01/log-1-streamed.json",
});
writeArtifact("2026-01-01/log-1-streamed.json", {
clientRawRequest: { body: { input: [{ type: "message", role: "user", content: "hi" }] } },
providerRequest: { body: { input: [{ type: "message", role: "user", content: "hi" }] } },
clientResponse: {
_streamed: true,
_format: "sse-json",
_eventCount: 1,
summary: {
id: "resp_streamed",
object: "response",
output: [{ type: "message", role: "assistant", content: "hello" }],
},
},
});
const result = store.resolvePreviousResponseState("resp_streamed", "key-1");
assert.deepEqual(result, {
input: [{ type: "message", role: "user", content: "hi" }],
output: [{ type: "message", role: "assistant", content: "hello" }],
});
});
test("resolvePreviousResponseState chains off effectiveInput, not the pre-reconstruction clientRawRequest.body", () => {
// Live incident (2026-09-03): clientRawRequest.body is deliberately captured
// BEFORE chat.ts's own previous_response_id reconstruction runs
// (captureDeferredClientRawBody's whole point -- it must reflect the raw
// client bytes for audit/guardrail purposes, not what OmniRoute rewrote the
// request into). For a turn that was ITSELF a continuation, body.input is
// just the client's own trimmed delta -- a handful of tool-call items with
// no leading system/user message. Chaining a LATER continuation off that
// instead of the request's real effective input compounds into a
// progressively truncated reconstruction, which the upstream provider then
// rejects outright ("Please ensure that function call turn comes
// immediately after a user turn..."). effectiveInput is captured AFTER
// reconstruction and must be what this function chains off.
insertCallLog({
id: "log-continued-turn",
responseId: "resp_continued",
apiKeyId: "key-1",
detailState: "ready",
artifactRelPath: "2026-01-01/log-continued-turn.json",
});
writeArtifact("2026-01-01/log-continued-turn.json", {
clientRawRequest: {
// What the client actually sent this turn: just the new delta, relying
// on OmniRoute to have reconstructed full history server-side.
body: {
input: [{ type: "function_call_output", call_id: "call_1", output: "42" }],
},
// What this request ACTUALLY dispatched with, after chat.ts's own
// reconstruction expanded the prior turn's stored input+output back in.
effectiveInput: [
{ type: "message", role: "user", content: "hi" },
{ type: "message", role: "assistant", content: "calling a tool" },
{ type: "function_call", call_id: "call_1", name: "get_answer", arguments: "{}" },
{ type: "function_call_output", call_id: "call_1", output: "42" },
],
},
providerRequest: { body: { input: [] } },
clientResponse: {
id: "resp_continued",
output: [{ type: "message", role: "assistant", content: "the answer is 42" }],
},
});
const result = store.resolvePreviousResponseState("resp_continued", "key-1");
assert.deepEqual(result, {
input: [
{ type: "message", role: "user", content: "hi" },
{ type: "message", role: "assistant", content: "calling a tool" },
{ type: "function_call", call_id: "call_1", name: "get_answer", arguments: "{}" },
{ type: "function_call_output", call_id: "call_1", output: "42" },
],
output: [{ type: "message", role: "assistant", content: "the answer is 42" }],
});
});
test("resolvePreviousResponseState falls back to clientRawRequest.body.input when effectiveInput is absent (pre-fix artifacts)", () => {
insertCallLog({
id: "log-legacy-no-effective-input",
responseId: "resp_legacy",
apiKeyId: "key-1",
detailState: "ready",
artifactRelPath: "2026-01-01/log-legacy-no-effective-input.json",
});
writeArtifact("2026-01-01/log-legacy-no-effective-input.json", {
clientRawRequest: { body: { input: [{ type: "message", role: "user", content: "hi" }] } },
providerRequest: { body: { input: [{ type: "message", role: "user", content: "hi" }] } },
clientResponse: {
id: "resp_legacy",
output: [{ type: "message", role: "assistant", content: "hello" }],
},
});
const result = store.resolvePreviousResponseState("resp_legacy", "key-1");
assert.deepEqual(result, {
input: [{ type: "message", role: "user", content: "hi" }],
output: [{ type: "message", role: "assistant", content: "hello" }],
});
});
test("resolvePreviousResponseState returns null for an unknown response id", () => {
const result = store.resolvePreviousResponseState("resp_does_not_exist", "key-1");
assert.equal(result, null);
});
test("resolvePreviousResponseState never crosses tenants (scoped by api_key_id)", () => {
insertCallLog({
id: "log-2",
responseId: "resp_tenant_a",
apiKeyId: "key-a",
detailState: "ready",
artifactRelPath: "2026-01-01/log-2.json",
});
writeArtifact("2026-01-01/log-2.json", {
clientRawRequest: { body: { input: [{ role: "user", content: "secret" }] } },
providerRequest: { body: { input: [{ role: "user", content: "secret" }] } },
clientResponse: { id: "resp_tenant_a", output: [{ role: "assistant", content: "reply" }] },
});
assert.equal(store.resolvePreviousResponseState("resp_tenant_a", "key-b"), null);
assert.equal(store.resolvePreviousResponseState("resp_tenant_a", null), null);
assert.notEqual(store.resolvePreviousResponseState("resp_tenant_a", "key-a"), null);
});
test("resolvePreviousResponseState returns null when the artifact is missing on disk", () => {
insertCallLog({
id: "log-3",
responseId: "resp_missing_file",
apiKeyId: "key-1",
detailState: "ready",
artifactRelPath: "2026-01-01/does-not-exist.json",
});
assert.equal(store.resolvePreviousResponseState("resp_missing_file", "key-1"), null);
});
test("resolvePreviousResponseState fails closed when the pipeline payload was size-limit-omitted", () => {
insertCallLog({
id: "log-4",
responseId: "resp_omitted",
apiKeyId: "key-1",
detailState: "ready",
artifactRelPath: "2026-01-01/log-4.json",
});
// A size-limit-omitted payload is replaced with a placeholder string, not
// an object -- resolvePreviousResponseState must never try to reconstruct
// from it and silently drop history.
writeArtifact("2026-01-01/log-4.json", {
clientRawRequest: { body: "[omitted: call log artifact size limit exceeded]" },
clientResponse: { id: "resp_omitted", output: [] },
});
assert.equal(store.resolvePreviousResponseState("resp_omitted", "key-1"), null);
});
test("resolvePreviousResponseState resolves input from clientRawRequest when providerRequest was translated to a different upstream wire shape", () => {
// Real shape from a live auto-routed free-tier connection: OmniRoute
// translates the client's Responses-API request into Chat Completions
// (`messages`, no `input` at all) before forwarding upstream. Reading
// `input` from providerRequest.body made this permanently unresolvable --
// previous_response_not_found on every attempt -- for any connection where
// the selected upstream isn't itself a native Responses-API passthrough.
// The client's own request is always Responses-API shaped (this store only
// fires for sourceFormat === OPENAI_RESPONSES, see chat.ts), so
// clientRawRequest is the correct source regardless of upstream shape.
insertCallLog({
id: "log-6",
responseId: "resp_gen-translate-mode",
apiKeyId: "key-1",
detailState: "ready",
artifactRelPath: "2026-01-01/log-6.json",
});
writeArtifact("2026-01-01/log-6.json", {
clientRawRequest: { body: { input: [{ type: "message", role: "user", content: "hi" }] } },
providerRequest: {
body: { model: "laguna-s-2.1-free", messages: [{ role: "user", content: "hi" }] },
},
clientResponse: {
summary: {
id: "resp_gen-translate-mode",
output: [{ type: "message", role: "assistant", content: "hello" }],
},
},
});
const result = store.resolvePreviousResponseState("resp_gen-translate-mode", "key-1");
assert.deepEqual(result, {
input: [{ type: "message", role: "user", content: "hi" }],
output: [{ type: "message", role: "assistant", content: "hello" }],
});
});
test("resolvePreviousResponseState fails closed when the stored input array was log-truncated", () => {
// Real production shape: cloneBoundedChatLogPayload (chatCore/logTruncation.ts)
// and cloneBoundedForLog (utils/requestLogger.ts) both prepend an
// `_omniroute_truncated_array` sentinel in place of the items they dropped
// once a logged array exceeds their tail-item cap (~24 items) -- routine
// for any conversation that's been going a while, not an edge case. Reading
// that sentinel back as a real Responses-API item and forwarding it upstream
// produced a live 400: "input item type 'missing' cannot be represented in
// Chat Completions" -- worse than the plain cache-miss this function is
// otherwise designed to fail into.
insertCallLog({
id: "log-7",
responseId: "resp_gen-truncated-history",
apiKeyId: "key-1",
detailState: "ready",
artifactRelPath: "2026-01-01/log-7.json",
});
writeArtifact("2026-01-01/log-7.json", {
clientRawRequest: {
body: {
input: [
{ _omniroute_truncated_array: true, originalLength: 26, retainedTailItems: 24 },
{ type: "function_call_output", call_id: "call_1", output: "ok" },
],
},
},
providerRequest: { body: { input: [] } },
clientResponse: {
summary: {
id: "resp_gen-truncated-history",
output: [{ type: "message", role: "assistant", content: "hello" }],
},
},
});
assert.equal(store.resolvePreviousResponseState("resp_gen-truncated-history", "key-1"), null);
});
test("resolvePreviousResponseState fails closed when the streaming collector truncated the response", () => {
// Live incident (2026-09-02): a huge/reasoning-heavy response blew past
// createStructuredSSECollector's own event-count cap mid-stream. The
// stored clientResponse then carries `_truncated: true` and
// `summary.status: "in_progress"` (never reached "completed") with a
// genuinely empty `summary.output` -- not a bounded array with an
// `_omniroute_truncated_array` sentinel (that only covers an array capped
// mid-array, not a collector that stopped before populating output at
// all). The empty array previously passed every check here and got
// merged into the next turn's request as this response's entire
// contribution -- reconstructing to zero real messages, which the
// upstream provider then rejected outright ("Input required: specify
// prompt or messages"), breaking the conversation. Measured live: ~22%
// of a sample of recent successful Ping responses carried this flag.
insertCallLog({
id: "log-8",
responseId: "resp_gen-collector-truncated",
apiKeyId: "key-1",
detailState: "ready",
artifactRelPath: "2026-01-01/log-8.json",
});
writeArtifact("2026-01-01/log-8.json", {
clientRawRequest: { body: { input: [{ type: "message", role: "user", content: "hi" }] } },
providerRequest: { body: { input: [{ type: "message", role: "user", content: "hi" }] } },
clientResponse: {
_streamed: true,
_truncated: true,
_droppedEvents: 24,
summary: { id: "resp_gen-collector-truncated", status: "in_progress", output: [] },
},
});
assert.equal(store.resolvePreviousResponseState("resp_gen-collector-truncated", "key-1"), null);
});
test("resolvePreviousResponseState fails closed on an empty output array even without the _truncated flag", () => {
// Belt-and-suspenders for the same failure class when the collector
// truncated without ever setting `_truncated` (or for a non-streaming
// response that somehow logged zero output items): a response the
// client actually received as real/successful always has at least one
// output item, so an empty array here is never a legitimate prior turn
// to reconstruct from.
insertCallLog({
id: "log-9",
responseId: "resp_gen-empty-output",
apiKeyId: "key-1",
detailState: "ready",
artifactRelPath: "2026-01-01/log-9.json",
});
writeArtifact("2026-01-01/log-9.json", {
clientRawRequest: { body: { input: [{ type: "message", role: "user", content: "hi" }] } },
providerRequest: { body: { input: [{ type: "message", role: "user", content: "hi" }] } },
clientResponse: { id: "resp_gen-empty-output", output: [] },
});
assert.equal(store.resolvePreviousResponseState("resp_gen-empty-output", "key-1"), null);
});
test("resolvePreviousResponseState fails closed when the row had video content removed (#12150 P2)", () => {
// #12150 P2 surface 2: the persisted clientRawRequest snapshot had its video
// transcript cues structurally redacted to [redacted-video-transcript] before
// storage (videoBridgeSnapshotRedaction). The stored input therefore no longer
// carries the client's real cue text -- reconstructing a continuation off it
// would forward the placeholder upstream as if it were genuine history. When the
// owning row is marked video_content_removed=1 this must fail closed (return
// null) so the client resends full history, exactly like previous_response_not_found,
// even though the artifact itself is otherwise a perfectly resolvable 'ready' row.
insertCallLog({
id: "log-video-removed",
responseId: "resp_video_removed",
apiKeyId: "key-1",
detailState: "ready",
artifactRelPath: "2026-01-01/log-video-removed.json",
videoContentRemoved: 1,
});
writeArtifact("2026-01-01/log-video-removed.json", {
clientRawRequest: {
body: {
input: [{ type: "message", role: "user", content: "[redacted-video-transcript]" }],
},
},
providerRequest: { body: { input: [] } },
clientResponse: {
id: "resp_video_removed",
output: [{ type: "message", role: "assistant", content: "hello" }],
},
});
assert.equal(store.resolvePreviousResponseState("resp_video_removed", "key-1"), null);
});
test("resolvePreviousResponseState still resolves a normal row (video_content_removed=0)", () => {
// Guard the fail-closed above does not over-fire: an ordinary row (the default
// 0) resolves exactly as before.
insertCallLog({
id: "log-video-notremoved",
responseId: "resp_video_notremoved",
apiKeyId: "key-1",
detailState: "ready",
artifactRelPath: "2026-01-01/log-video-notremoved.json",
videoContentRemoved: 0,
});
writeArtifact("2026-01-01/log-video-notremoved.json", {
clientRawRequest: { body: { input: [{ type: "message", role: "user", content: "hi" }] } },
providerRequest: { body: { input: [{ type: "message", role: "user", content: "hi" }] } },
clientResponse: {
id: "resp_video_notremoved",
output: [{ type: "message", role: "assistant", content: "hello" }],
},
});
assert.deepEqual(store.resolvePreviousResponseState("resp_video_notremoved", "key-1"), {
input: [{ type: "message", role: "user", content: "hi" }],
output: [{ type: "message", role: "assistant", content: "hello" }],
});
});
test("resolvePreviousResponseState returns null when detail logging was never captured for this row", () => {
insertCallLog({
id: "log-5",
responseId: "resp_no_detail",
apiKeyId: "key-1",
detailState: "none",
artifactRelPath: null,
});
assert.equal(store.resolvePreviousResponseState("resp_no_detail", "key-1"), null);
});
// Proven live in production (2026-09-06, nvidia/nemotron-3.5-lightning:free via
// OpenRouter): a client that fires its next turn immediately after receiving a
// response id -- normal behavior in a tight tool-calling loop -- can reach
// resolvePreviousResponseState before saveCallLog's own artifact write (queued,
// see writeCallArtifactAsync) has landed and flipped detail_state to "ready".
// Before the pending-continuation bridge, OmniRoute answered a well-formed 400
// previous_response_not_found for an id it minted seconds earlier; the wire
// capture showed the client recovering by resending full history, exactly like
// a real OpenAI-issued rejection -- but every one of those resends was an
// avoidable full-history resend, not a genuine unknown id. This exercises the
// real saveCallLog pipeline end to end, not a pre-inserted "ready" row.
test("resolvePreviousResponseState resolves via the pending bridge while saveCallLog's artifact write is still queued", async () => {
const save = callLogs.saveCallLog({
method: "POST",
path: "/v1/responses",
status: 200,
model: "nvidia/nemotron-3.5-lightning:free",
provider: "openrouter",
apiKeyId: "key-1",
duration: 8169,
responseId: "resp_gen-race-abc123",
requestBody: { input: [{ type: "message", role: "user", content: "hi" }], store: true },
responseBody: { id: "resp_gen-race-abc123" },
pipeline: {
clientRawRequest: { body: { input: [{ type: "message", role: "user", content: "hi" }] } },
clientResponse: {
id: "resp_gen-race-abc123",
output: [{ type: "message", role: "assistant", content: "hello" }],
},
},
});
// The client's next turn can arrive before the queued artifact write below
// has even started -- the bridge, seeded synchronously inside saveCallLog
// before this call returns, must already answer correctly.
assert.deepEqual(store.resolvePreviousResponseState("resp_gen-race-abc123", "key-1"), {
input: [{ type: "message", role: "user", content: "hi" }],
output: [{ type: "message", role: "assistant", content: "hello" }],
});
await save;
// Once the durable row lands, the same id must still resolve -- now from
// call_logs/the artifact, with the bridge entry already cleared.
assert.deepEqual(store.resolvePreviousResponseState("resp_gen-race-abc123", "key-1"), {
input: [{ type: "message", role: "user", content: "hi" }],
output: [{ type: "message", role: "assistant", content: "hello" }],
});
});
test("resolvePreviousResponseState never lets the pending bridge cross tenants", async () => {
const save = callLogs.saveCallLog({
method: "POST",
path: "/v1/responses",
status: 200,
model: "nvidia/nemotron-3.5-lightning:free",
provider: "openrouter",
apiKeyId: "key-a",
duration: 4000,
responseId: "resp_gen-tenant-bridge",
pipeline: {
clientRawRequest: { body: { input: [{ role: "user", content: "secret" }] } },
clientResponse: { id: "resp_gen-tenant-bridge", output: [{ role: "assistant", content: "reply" }] },
},
});
assert.equal(store.resolvePreviousResponseState("resp_gen-tenant-bridge", "key-b"), null);
assert.equal(store.resolvePreviousResponseState("resp_gen-tenant-bridge", null), null);
assert.notEqual(store.resolvePreviousResponseState("resp_gen-tenant-bridge", "key-a"), null);
await save;
});
test("resolvePreviousResponseState does not bridge a response id that saveCallLog never seeded (no-log or no pipeline)", async () => {
// noLog: the entry is redacted before it would ever reach the bridge.
await callLogs.saveCallLog({
method: "POST",
path: "/v1/responses",
status: 200,
model: "gpt-5.4-pro",
provider: "openai",
apiKeyId: "key-1",
noLog: true,
responseId: "resp_gen-nolog",
pipeline: {
clientRawRequest: { body: { input: [{ role: "user", content: "hi" }] } },
clientResponse: { id: "resp_gen-nolog", output: [{ role: "assistant", content: "hi" }] },
},
});
assert.equal(store.resolvePreviousResponseState("resp_gen-nolog", "key-1"), null);
// No pipeline payload at all -- nothing to reconstruct from.
await callLogs.saveCallLog({
method: "POST",
path: "/v1/responses",
status: 200,
model: "gpt-5.4-pro",
provider: "openai",
apiKeyId: "key-1",
responseId: "resp_gen-no-pipeline",
});
assert.equal(store.resolvePreviousResponseState("resp_gen-no-pipeline", "key-1"), null);
});
test("the pending bridge shares the durable path's fail-closed rules (video-redacted turns never bridge)", async () => {
const save = callLogs.saveCallLog({
method: "POST",
path: "/v1/responses",
status: 200,
model: "gpt-5.4-pro",
provider: "openai",
apiKeyId: "key-1",
duration: 100,
responseId: "resp_gen-video-bridge",
videoContentRemoved: true,
pipeline: {
clientRawRequest: { body: { input: [{ role: "user", content: "[redacted-video-transcript]" }] } },
clientResponse: { id: "resp_gen-video-bridge", output: [{ role: "assistant", content: "ok" }] },
},
});
// Even mid-flight (bridge-only, durable row not yet written), a
// video-redacted turn must fail closed exactly like the durable path does.
assert.equal(store.resolvePreviousResponseState("resp_gen-video-bridge", "key-1"), null);
await save;
assert.equal(store.resolvePreviousResponseState("resp_gen-video-bridge", "key-1"), null);
});
// resolveTurnCompletionState / resolveConversationStalledState -- backs the
// /dashboard/conversations "stalled" badge (live incident 2026-09-04: a
// reasoning-heavy stream blew past the SSE collector's cap mid-stream,
// leaving a conversation permanently stuck at an unanswered state with no
// client-visible signal that anything had gone wrong).
test("resolveTurnCompletionState returns 'stop' for a clean final assistant reply (no function_call)", () => {
writeArtifact("2026-01-01/turn-stop.json", {
clientResponse: {
summary: {
status: "completed",
output: [{ type: "message", role: "assistant", content: "final answer" }],
},
},
});
assert.equal(store.resolveTurnCompletionState("2026-01-01/turn-stop.json"), "stop");
});
test("resolveTurnCompletionState returns 'tool_call_pending' for a completed stream ending in an unanswered function_call", () => {
writeArtifact("2026-01-01/turn-tool-call.json", {
clientResponse: {
summary: {
status: "completed",
output: [
{ type: "message", role: "assistant", content: "calling a tool" },
{ type: "function_call", call_id: "call_1", name: "get_answer", arguments: "{}" },
],
},
},
});
assert.equal(store.resolveTurnCompletionState("2026-01-01/turn-tool-call.json"), "tool_call_pending");
});
test("resolveTurnCompletionState returns 'incomplete' for a collector-truncated stream (_truncated: true)", () => {
// Exact live-incident shape: createStructuredSSECollector's own event-count
// cap stopped mid-stream, so status never reached "completed" and output
// stayed empty -- see responses-continuation-store.test.ts's earlier
// "fails closed when the streaming collector truncated" case for the same
// shape backing resolvePreviousResponseState's own fail-closed behavior.
writeArtifact("2026-01-01/turn-truncated.json", {
clientResponse: {
_streamed: true,
_truncated: true,
_droppedEvents: 1487,
summary: { status: "in_progress", output: [] },
},
});
assert.equal(store.resolveTurnCompletionState("2026-01-01/turn-truncated.json"), "incomplete");
});
test("resolveTurnCompletionState returns 'incomplete' for a non-'completed' status without the _truncated flag", () => {
writeArtifact("2026-01-01/turn-failed-status.json", {
clientResponse: { summary: { status: "failed", output: [] } },
});
assert.equal(store.resolveTurnCompletionState("2026-01-01/turn-failed-status.json"), "incomplete");
});
test("resolveTurnCompletionState returns 'unknown' for a missing artifact", () => {
assert.equal(store.resolveTurnCompletionState("2026-01-01/does-not-exist.json"), "unknown");
assert.equal(store.resolveTurnCompletionState(null), "unknown");
});
test("resolveConversationStalledState is false while still inside the 5-minute grace period", () => {
writeArtifact("2026-01-01/stall-grace.json", {
clientResponse: {
summary: {
status: "completed",
output: [{ type: "function_call", call_id: "call_1", name: "x", arguments: "{}" }],
},
},
});
const lastSeenAt = new Date(Date.UTC(2026, 0, 1, 12, 0, 0)).toISOString();
const now = Date.parse(lastSeenAt) + 4 * 60 * 1000; // 4 minutes later
assert.equal(
store.resolveConversationStalledState({
artifactRelPath: "2026-01-01/stall-grace.json",
lastSeenAt,
isActive: false,
now,
}),
false
);
});
test("resolveConversationStalledState is true once the grace period elapses with an unanswered tool call", () => {
writeArtifact("2026-01-01/stall-elapsed.json", {
clientResponse: {
summary: {
status: "completed",
output: [{ type: "function_call", call_id: "call_1", name: "x", arguments: "{}" }],
},
},
});
const lastSeenAt = new Date(Date.UTC(2026, 0, 1, 12, 0, 0)).toISOString();
const now = Date.parse(lastSeenAt) + 6 * 60 * 1000; // 6 minutes later
assert.equal(
store.resolveConversationStalledState({
artifactRelPath: "2026-01-01/stall-elapsed.json",
lastSeenAt,
isActive: false,
now,
}),
true
);
});
test("resolveConversationStalledState is true immediately for a genuinely truncated stream, no grace period needed", () => {
// Unlike a bare unanswered tool call, a truncated/failed stream has no
// legitimate "still working on it" interpretation -- it already permanently
// failed the moment the collector gave up.
writeArtifact("2026-01-01/stall-truncated.json", {
clientResponse: { _truncated: true, summary: { status: "in_progress", output: [] } },
});
const lastSeenAt = new Date(Date.UTC(2026, 0, 1, 12, 0, 0)).toISOString();
const now = Date.parse(lastSeenAt) + 60 * 1000; // 1 minute later -- still "elapsed" per the check below
// The grace period still applies uniformly (elapsed-time check is the same
// for both incomplete states) -- assert the boundary explicitly instead of
// assuming: at 1 minute, still within grace; at 6 minutes, stalled.
assert.equal(
store.resolveConversationStalledState({
artifactRelPath: "2026-01-01/stall-truncated.json",
lastSeenAt,
isActive: false,
now,
}),
false
);
assert.equal(
store.resolveConversationStalledState({
artifactRelPath: "2026-01-01/stall-truncated.json",
lastSeenAt,
isActive: false,
now: Date.parse(lastSeenAt) + 6 * 60 * 1000,
}),
true
);
});
test("resolveConversationStalledState is never true while isActive, regardless of completion state or elapsed time", () => {
writeArtifact("2026-01-01/stall-active.json", {
clientResponse: { _truncated: true, summary: { status: "in_progress", output: [] } },
});
const lastSeenAt = new Date(Date.UTC(2026, 0, 1, 12, 0, 0)).toISOString();
assert.equal(
store.resolveConversationStalledState({
artifactRelPath: "2026-01-01/stall-active.json",
lastSeenAt,
isActive: true,
now: Date.parse(lastSeenAt) + 60 * 60 * 1000, // an hour later
}),
false
);
});
test("resolveConversationStalledState is false for a clean 'stop' turn no matter how much time has passed", () => {
writeArtifact("2026-01-01/stall-stopped.json", {
clientResponse: {
summary: {
status: "completed",
output: [{ type: "message", role: "assistant", content: "done" }],
},
},
});
const lastSeenAt = new Date(Date.UTC(2026, 0, 1, 12, 0, 0)).toISOString();
assert.equal(
store.resolveConversationStalledState({
artifactRelPath: "2026-01-01/stall-stopped.json",
lastSeenAt,
isActive: false,
now: Date.parse(lastSeenAt) + 24 * 60 * 60 * 1000, // a day later
}),
false
);
});