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:
snufkintomo
2026-08-21 19:12:00 +08:00
parent 9820845768
commit e4c55f4871
4 changed files with 148 additions and 2 deletions
+21
View File
@@ -72,12 +72,33 @@ export function transformTools(tools: Tool[]): QoderTool[] {
export function transformMessagesForQoder(messages: Message[]): QoderMessage[] {
const normalizedMessages: QoderMessage[] = [];
// Dropping an assistant turn (below) also invalidates its tool calls: the
// result that follows would refer to a tool_calls entry that is no longer in
// the request, and upstreams reject that with "tool must follow a message
// with tool_calls".
const droppedToolCallIds = new Set<string>();
for (const msg of messages) {
// Skip error or aborted messages
if (
msg.role === "assistant" &&
((msg as AssistantMessage).stopReason === "error" || (msg as AssistantMessage).stopReason === "aborted")
) {
const am = msg as AssistantMessage;
if (Array.isArray(am.content)) {
for (const block of am.content) {
if (block.type === "toolCall") {
const id = (block as ToolCall).id;
if (id) droppedToolCallIds.add(id);
}
}
}
continue;
}
// Drop the result too, otherwise it refers to a tool_calls entry that is
// no longer in the request.
if (msg.role === "toolResult" && droppedToolCallIds.has((msg as ToolResultMessage).toolCallId)) {
continue;
}