Merge pull request #20 from snufkintomo/fix/done-sentinel-hang-and-orphaned-tool-results

fix: end the stream at [DONE] and drop orphaned tool results
This commit is contained in:
Simon Shi
2026-08-31 23:42:52 +08:00
committed by GitHub
4 changed files with 148 additions and 2 deletions
+54
View File
@@ -290,4 +290,58 @@ describe("streamQoder", () => {
expect(msg.content.find((c) => c.type === "toolCall")).toBeUndefined();
expect(msg.stopReason).toBe("stop");
});
it("finishes when the gateway sends [DONE] but keeps the body open", async () => {
// Qoder's gateway does not always close the HTTP body after the sentinel.
// The read loop used to keep awaiting reader.read() until the socket went
// away, so a fully streamed reply never produced a done event and the
// agent appeared to hang with no error.
const sse = sseEnvelope(chunk({ content: "OK", role: "assistant" })) + sseEnvelope(finishChunk("stop")) + DONE_SSE;
let cancelled = false;
const body = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(new TextEncoder().encode(sse));
// Deliberately never call controller.close().
},
cancel() {
cancelled = true;
},
});
globalThis.fetch = vi.fn(
async () => new Response(body, { status: 200, headers: { "content-type": "text/event-stream" } }),
) as unknown as typeof fetch;
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 even though the body stayed open").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");
// The reader is released rather than left holding the connection.
expect(cancelled).toBe(true);
});
it("finishes on a bare 'data: [DONE]' line with the body left open", async () => {
// Same sentinel, unwrapped.
const sse = `${sseEnvelope(chunk({ content: "hi", role: "assistant" }))}data: [DONE]\n\n`;
const body = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(new TextEncoder().encode(sse));
},
});
globalThis.fetch = vi.fn(
async () => new Response(body, { status: 200, headers: { "content-type": "text/event-stream" } }),
) as unknown as typeof fetch;
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 for the bare sentinel").toBeDefined();
const msg = (done as { message: AssistantMessage }).message;
const text = msg.content.find((c) => c.type === "text");
expect(text && "text" in text ? text.text : "").toBe("hi");
});
});
+52
View File
@@ -348,4 +348,56 @@ describe("transformMessagesForQoder", () => {
expect(tool.role).toBe("tool");
expect(tool.tool_call_id).toBe("call_abc123");
});
it("drops the tool result whose assistant turn was aborted", () => {
// Cancelling a turn mid-tool used to drop only the assistant message and
// keep its tool result, leaving a `tool` message with no matching
// tool_calls. Upstreams reject that with "tool must follow a message with
// tool_calls", so every later request in the session failed.
const msgs = [
{ role: "user", content: "go" },
{
role: "assistant",
stopReason: "aborted",
content: [{ type: "toolCall", id: "call_gone", name: "bash", arguments: {} }],
},
{ role: "toolResult", toolCallId: "call_gone", content: "output" },
{ role: "user", content: "again" },
] as unknown as Message[];
const result = transformMessagesForQoder(msgs);
expect(result.map((m) => (m as { role: string }).role)).toEqual(["user", "user"]);
expect(result.some((m) => (m as { role: string }).role === "tool")).toBe(false);
});
it("keeps a healthy tool round-trip while dropping an aborted one", () => {
const msgs = [
{
role: "assistant",
stopReason: "error",
content: [{ type: "toolCall", id: "call_bad", name: "x", arguments: {} }],
},
{ role: "toolResult", toolCallId: "call_bad", content: "r1" },
{
role: "assistant",
content: [{ type: "toolCall", id: "call_good", name: "y", arguments: {} }],
},
{ role: "toolResult", toolCallId: "call_good", content: "r2" },
] as unknown as Message[];
const result = transformMessagesForQoder(msgs);
expect(result).toHaveLength(2);
const asst = result[0] as { tool_calls?: Array<{ id: string }> };
const tool = result[1] as { role: string; tool_call_id: string };
expect(asst.tool_calls?.map((t) => t.id)).toEqual(["call_good"]);
expect(tool.tool_call_id).toBe("call_good");
// Every tool message must have a declaring tool_calls entry.
const declared = new Set(
result.flatMap((m) => ((m as { tool_calls?: Array<{ id: string }> }).tool_calls ?? []).map((t) => t.id)),
);
for (const m of result) {
const tm = m as { role: string; tool_call_id?: string };
if (tm.role === "tool") expect(declared.has(tm.tool_call_id as string)).toBe(true);
}
});
});