fix(stream): stop silently swallowing upstream errors and session-lock stalls
Three stability fixes for the streaming layer. Together they eliminate
the "agent silently stops mid-task" failure mode users hit during agentic
loops.
1. Surface upstream errors instead of swallowing them
The SSE parsing loop wrapped every line in try { ... } catch {}, which
silently dropped the throw raised when Qoder returned an error envelope
(e.g. 406 Session blocked, provider_error). The stream then completed
with empty content and stopReason "stop", so pi saw a clean stop instead
of an error and never retried or informed the user.
The catch now only skips genuinely malformed SSE lines (SyntaxError) and
re-throws everything else so the outer handler can set stopReason "error"
+ errorMessage. A QODER_DEBUG env var logs skipped lines for diagnosis.
2. Stop overwriting the real finish_reason with "stop"
At end-of-stream the code unconditionally did:
output.stopReason = toolCalls.length ? "toolUse" : "stop"
clobbering any meaningful finish_reason ("length", "content_filter", ...)
captured earlier. Now only toolUse is forced when tool calls are present;
otherwise the upstream finish_reason is preserved (defaults to "stop").
The done event reason cast now includes "length".
3. Give each request a fresh server-side session id
session_id was stableHash("qoder-session", userID, qoderModel) —
constant across all requests for a given user+model. Qoder enforces a
per-session lock and replies 406 Session blocked when a second request
arrives before the previous one fully drains server-side, which happens
routinely in tight agentic tool loops. Appending crypto.randomUUID()
makes every request use a fresh session. We send full conversation
history on each request, so losing server-side session continuity is
safe.
4. Capture upstream usage, responseId and responseModel
The provider reported all-zero usage and no responseId/responseModel.
Now the finish chunk usage (prompt/completion/total tokens, cache
read/write) is mapped into output.usage, and id/model into
responseId/responseModel, so pi sees real token accounting and can
correlate responses.
Add stream.test.ts with regression coverage for the 406 path, the
finish_reason preservation, the toolUse stop reason, and usage/response
metadata capture.
This commit is contained in:
@@ -0,0 +1,221 @@
|
||||
import type {
|
||||
Api,
|
||||
AssistantMessage,
|
||||
AssistantMessageEvent,
|
||||
AssistantMessageEventStream,
|
||||
Context,
|
||||
Model,
|
||||
} from "@earendil-works/pi-ai";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { streamQoder } from "../stream.js";
|
||||
|
||||
/**
|
||||
* Build a single SSE `data:` line carrying a Qoder envelope:
|
||||
* { headers, body: <JSON string>, statusCodeValue, statusCode }
|
||||
* The server wraps the OpenAI-style chunk inside `body` as a JSON string.
|
||||
*/
|
||||
function sseEnvelope(body: object, statusCodeValue = 200, statusCode = "OK"): string {
|
||||
return (
|
||||
"data:" +
|
||||
JSON.stringify({
|
||||
headers: { "Content-Type": ["application/json"] },
|
||||
body: JSON.stringify(body),
|
||||
statusCodeValue,
|
||||
statusCode,
|
||||
}) +
|
||||
"\n\n"
|
||||
);
|
||||
}
|
||||
|
||||
const DONE_SSE =
|
||||
"data:" +
|
||||
JSON.stringify({
|
||||
headers: { "Content-Type": ["application/json"] },
|
||||
body: "[DONE]",
|
||||
statusCodeValue: 200,
|
||||
statusCode: "OK",
|
||||
}) +
|
||||
"\n\n";
|
||||
|
||||
function chunk(delta: object, extra: object = {}): object {
|
||||
return {
|
||||
choices: [{ delta, index: 0 }],
|
||||
created: 1,
|
||||
id: "test-id",
|
||||
model: "auto",
|
||||
object: "chat.completion.chunk",
|
||||
...extra,
|
||||
};
|
||||
}
|
||||
|
||||
function finishChunk(finish_reason: string, extra: object = {}): object {
|
||||
return {
|
||||
choices: [{ finish_reason, index: 0 }],
|
||||
created: 1,
|
||||
id: "test-id",
|
||||
model: "auto",
|
||||
object: "chat.completion.chunk",
|
||||
usage: { completion_tokens: 1, prompt_tokens: 1, total_tokens: 2 },
|
||||
...extra,
|
||||
};
|
||||
}
|
||||
|
||||
const SUCCESS_SSE =
|
||||
sseEnvelope(chunk({ role: "assistant" })) +
|
||||
sseEnvelope(chunk({ reasoning_content: "The user wants OK.", role: "assistant" })) +
|
||||
sseEnvelope(chunk({ content: "OK", role: "assistant" })) +
|
||||
sseEnvelope(finishChunk("stop")) +
|
||||
DONE_SSE;
|
||||
|
||||
const BLOCKED_SSE = sseEnvelope(
|
||||
{ code: "provider_error", message: "Session blocked", request_id: "r", type: "provider_error" },
|
||||
406,
|
||||
"Not Acceptable",
|
||||
);
|
||||
|
||||
function mockFetch(body: string): typeof fetch {
|
||||
const response = new Response(body, {
|
||||
status: 200,
|
||||
headers: { "content-type": "text/event-stream" },
|
||||
});
|
||||
return vi.fn(async () => response) as unknown as typeof fetch;
|
||||
}
|
||||
|
||||
function makeModel(): Model<Api> {
|
||||
return { id: "ultimate", api: "qoder-api" as Api, provider: "qoder" } as Model<Api>;
|
||||
}
|
||||
|
||||
function makeContext(): Context {
|
||||
return {
|
||||
systemPrompt: "test",
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
tools: [],
|
||||
} as unknown as Context;
|
||||
}
|
||||
|
||||
async function consume(stream: AssistantMessageEventStream): Promise<AssistantMessageEvent[]> {
|
||||
const events: AssistantMessageEvent[] = [];
|
||||
for await (const ev of stream) {
|
||||
events.push(ev);
|
||||
if (ev.type === "done" || ev.type === "error") break;
|
||||
}
|
||||
return events;
|
||||
}
|
||||
|
||||
describe("streamQoder", () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
afterEach(() => {
|
||||
globalThis.fetch = originalFetch;
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("parses a successful SSE stream into text + stop", async () => {
|
||||
globalThis.fetch = mockFetch(SUCCESS_SSE);
|
||||
const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" });
|
||||
const events = await consume(stream);
|
||||
|
||||
const done = events.find((e) => e.type === "done");
|
||||
expect(done, "expected a done event").toBeDefined();
|
||||
const msg = (done as { message: AssistantMessage }).message;
|
||||
expect(msg.stopReason).toBe("stop");
|
||||
const text = msg.content.find((c) => c.type === "text");
|
||||
expect(text && "text" in text ? text.text : "").toBe("OK");
|
||||
});
|
||||
|
||||
it("surfaces an upstream 406 'Session blocked' as an error event, not a silent stop", async () => {
|
||||
globalThis.fetch = mockFetch(BLOCKED_SSE);
|
||||
const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" });
|
||||
const events = await consume(stream);
|
||||
|
||||
const err = events.find((e) => e.type === "error");
|
||||
expect(err, "expected an error event").toBeDefined();
|
||||
const msg = (err as { error: AssistantMessage }).error;
|
||||
expect(msg.stopReason).toBe("error");
|
||||
expect(msg.errorMessage).toMatch(/Session blocked/);
|
||||
expect(msg.errorMessage).toMatch(/406/);
|
||||
// Must NOT emit a silent done/stop.
|
||||
expect(events.find((e) => e.type === "done")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("preserves finish_reason=length instead of overwriting to stop", async () => {
|
||||
const sse =
|
||||
sseEnvelope(chunk({ content: "partial", role: "assistant" })) + sseEnvelope(finishChunk("length")) + DONE_SSE;
|
||||
globalThis.fetch = mockFetch(sse);
|
||||
const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" });
|
||||
const events = await consume(stream);
|
||||
|
||||
const done = events.find((e) => e.type === "done");
|
||||
const msg = (done as { message: AssistantMessage }).message;
|
||||
expect(msg.stopReason).toBe("length");
|
||||
});
|
||||
|
||||
it("captures usage, responseId and responseModel from the finish chunk", async () => {
|
||||
const sse =
|
||||
sseEnvelope(chunk({ content: "OK", role: "assistant" })) +
|
||||
sseEnvelope(
|
||||
finishChunk("stop", {
|
||||
id: "chatcmpl-abc123",
|
||||
model: "qmodel_latest",
|
||||
usage: {
|
||||
prompt_tokens: 42,
|
||||
completion_tokens: 7,
|
||||
total_tokens: 49,
|
||||
completion_tokens_details: { reasoning_tokens: 3 },
|
||||
prompt_tokens_details: { cacheable_tokens: 10, cached_tokens: 5 },
|
||||
},
|
||||
}),
|
||||
) +
|
||||
DONE_SSE;
|
||||
globalThis.fetch = mockFetch(sse);
|
||||
const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" });
|
||||
const events = await consume(stream);
|
||||
|
||||
const done = events.find((e) => e.type === "done");
|
||||
const msg = (done as { message: AssistantMessage }).message;
|
||||
expect(msg.responseId).toBe("chatcmpl-abc123");
|
||||
expect(msg.responseModel).toBe("qmodel_latest");
|
||||
expect(msg.usage.input).toBe(42);
|
||||
expect(msg.usage.output).toBe(7);
|
||||
expect(msg.usage.totalTokens).toBe(49);
|
||||
expect(msg.usage.cacheRead).toBe(5);
|
||||
expect(msg.usage.cacheWrite).toBe(10);
|
||||
});
|
||||
|
||||
it("emits a done event with reason=length when finish_reason is length", async () => {
|
||||
const sse =
|
||||
sseEnvelope(chunk({ content: "partial", role: "assistant" })) + sseEnvelope(finishChunk("length")) + DONE_SSE;
|
||||
globalThis.fetch = mockFetch(sse);
|
||||
const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" });
|
||||
const events = await consume(stream);
|
||||
|
||||
const done = events.find((e) => e.type === "done");
|
||||
expect(done, "expected a done event").toBeDefined();
|
||||
expect((done as { reason: string }).reason).toBe("length");
|
||||
});
|
||||
|
||||
it("reports a tool_use stop reason when the stream emits tool calls", async () => {
|
||||
const sse =
|
||||
sseEnvelope(
|
||||
chunk({
|
||||
tool_calls: [
|
||||
{
|
||||
index: 0,
|
||||
id: "call_1",
|
||||
function: { name: "bash", arguments: '{"command":"ls"}' },
|
||||
},
|
||||
],
|
||||
}),
|
||||
) +
|
||||
sseEnvelope(finishChunk("tool_calls")) +
|
||||
DONE_SSE;
|
||||
globalThis.fetch = mockFetch(sse);
|
||||
const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" });
|
||||
const events = await consume(stream);
|
||||
|
||||
const done = events.find((e) => e.type === "done");
|
||||
const msg = (done as { message: AssistantMessage }).message;
|
||||
expect(msg.stopReason).toBe("toolUse");
|
||||
const toolCall = msg.content.find((c) => c.type === "toolCall");
|
||||
expect(toolCall).toBeDefined();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user