fix: end the stream at [DONE] and drop orphaned tool results
Two independent bugs made Qoder turns hang or fail outright. 1. The SSE read loop never terminated on the sentinel. `data: [DONE]` broke only the inner line loop, so the outer loop kept awaiting reader.read() until the socket closed. Qoder's gateway holds the body open after the sentinel, so a reply that had already streamed in full produced no `done` event: the agent sat there with the complete answer buffered and no error, until something else timed the connection out. The sentinel also arrives wrapped in an envelope (`body: "[DONE]"`), which likewise only did `continue`, so both forms now end the loop. The reader is cancelled once the reply is complete instead of being left holding the connection. 2. Aborting a turn produced an invalid request. Assistant messages with stopReason error/aborted are skipped, but the tool result that followed was still emitted, leaving a `tool` message whose tool_call_id had no matching tool_calls entry. Upstreams reject that with "tool must follow a message with tool_calls" -- the same failure the null-content workaround above it already guards against -- so after one cancelled tool call, every later request in that session failed. Tool calls belonging to a dropped turn are now recorded and their results dropped with them. Both fixes are covered by regression tests that fail on the current code: the two stream tests time out at the 8s limit, and the transform tests produce an orphaned tool message.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user