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:
+45
-6
@@ -153,7 +153,12 @@ export function streamQoder(
|
||||
}
|
||||
}
|
||||
|
||||
const sessionID = stableHash("qoder-session", userID, qoderModel);
|
||||
// Each request gets a fresh server-side session id. Reusing a stable
|
||||
// session id across back-to-back agentic requests triggers Qoder's
|
||||
// per-session lock and the server replies `406 Session blocked` mid
|
||||
// stream. We send full conversation history on every request, so a unique
|
||||
// session id is safe and avoids the lock entirely.
|
||||
const sessionID = `${stableHash("qoder-session", userID, qoderModel)}-${crypto.randomUUID()}`;
|
||||
|
||||
let maxTokens = 32768;
|
||||
if (maxOutputTokens > 0) {
|
||||
@@ -294,6 +299,22 @@ export function streamQoder(
|
||||
if (!innerStr || innerStr === "[DONE]") continue;
|
||||
|
||||
const inner = JSON.parse(innerStr);
|
||||
if (inner.id) output.responseId = inner.id as string;
|
||||
if (inner.model) output.responseModel = inner.model as string;
|
||||
if (inner.usage) {
|
||||
const u = inner.usage as {
|
||||
prompt_tokens?: number;
|
||||
completion_tokens?: number;
|
||||
total_tokens?: number;
|
||||
completion_tokens_details?: { reasoning_tokens?: number };
|
||||
prompt_tokens_details?: { cacheable_tokens?: number; cached_tokens?: number };
|
||||
};
|
||||
output.usage.input = u.prompt_tokens ?? 0;
|
||||
output.usage.output = u.completion_tokens ?? 0;
|
||||
output.usage.totalTokens = u.total_tokens ?? 0;
|
||||
output.usage.cacheRead = u.prompt_tokens_details?.cached_tokens ?? 0;
|
||||
output.usage.cacheWrite = u.prompt_tokens_details?.cacheable_tokens ?? 0;
|
||||
}
|
||||
if (inner.choices && inner.choices.length > 0) {
|
||||
const choice = inner.choices[0];
|
||||
const delta = choice.delta;
|
||||
@@ -382,10 +403,23 @@ export function streamQoder(
|
||||
}
|
||||
|
||||
if (choice.finish_reason) {
|
||||
output.stopReason = choice.finish_reason;
|
||||
// Preserve the real upstream finish_reason (e.g. "length",
|
||||
// "content_filter") instead of forcing "stop" later.
|
||||
output.stopReason = choice.finish_reason as AssistantMessage["stopReason"];
|
||||
}
|
||||
}
|
||||
} catch {}
|
||||
} catch (e) {
|
||||
// A single malformed SSE line shouldn't kill the stream — skip it.
|
||||
// But a genuine upstream error (thrown below) must propagate to the
|
||||
// outer catch and surface as stopReason="error", not be swallowed.
|
||||
if (e instanceof SyntaxError) {
|
||||
if (process.env.QODER_DEBUG) {
|
||||
console.error("[pi-provider-qoder] skipping malformed SSE line:", dataStr.slice(0, 200));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -428,10 +462,15 @@ export function streamQoder(
|
||||
|
||||
if (toolCallsState.length > 0) {
|
||||
output.stopReason = "toolUse";
|
||||
} else {
|
||||
output.stopReason = "stop";
|
||||
}
|
||||
stream.push({ type: "done", reason: output.stopReason as "stop" | "toolUse", message: output });
|
||||
// Otherwise keep whatever finish_reason set upstream (defaults to "stop").
|
||||
// Never overwrite a meaningful finish_reason ("length", "content_filter",
|
||||
// ...) with "stop".
|
||||
stream.push({
|
||||
type: "done",
|
||||
reason: output.stopReason as Extract<AssistantMessage["stopReason"], "stop" | "length" | "toolUse">,
|
||||
message: output,
|
||||
});
|
||||
stream.end();
|
||||
} catch (e: unknown) {
|
||||
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
||||
|
||||
Reference in New Issue
Block a user