opencode-copilot-chat icon indicating copy to clipboard operation
opencode-copilot-chat copied to clipboard

[BUG] finishReason is never captured from Responses API streams — root cause of the Muse Spark "try again" errors (#193, #197)

Open ltmoerdani opened this issue 3 months ago • 4 comments

Digging into #193 and #197 uncovered a deeper bug that predates both reports.

The short version: the truncation detector checks usageSummary.finishReason, but for Responses API streams that field is never filled. The gateway can send stop_reason perfectly fine and we still don't see it.

The flow, as it runs today:

  1. The engine parses each SSE event and hands the raw JSON to updateRequestUsageSummary (src/transports/engine.ts, the onData callback).
  2. That function reads finishReason from exactly two places: data.delta.stop_reason (Anthropic) and data.choices[0].finish_reason (chat-completions). See src/transports/extract.ts.
  3. A raw Responses event like {"type": "response.completed", "response": {"stop_reason": "completed"}} has neither. No choices array, no delta.
  4. The normalized object from normalizeResponsesStreamEvent (src/core/routing.ts) does carry a mapped finish_reason, but it only feeds extractStreamParts. It never reaches usageSummary.
  5. So isStreamTruncated sees finishReason === undefined on every Responses stream that also lacks data: [DONE].

Why only some models break: GPT-family requests get [DONE] forwarded by the gateway, so sawDone saves them. Muse Spark (upstream Meta) does not send [DONE], so every unterminated-looking stream is flagged as truncated. If nothing extractable arrives first, the engine retries 3 times and then throws the "try again" error. That is the exact signature in both #193 and #197.

This one gap explains the whole Muse Spark saga: #79 (throw after 12 parts), #187 (patched the symptom), #193, and #197.

Proposed fix: in updateRequestUsageSummary, treat data.type === "response.completed" as a terminal signal and set summary.finishReason from response.stop_reason, defaulting to "stop" when absent. The event itself is terminal by definition, so a stream that received it can never be a mid-response cut. A matching ?? "stop" default in the routing normalizer would be a cheap second layer.

One honest caveat: this fix stops the error, but if the Muse gateway also stopped delivering extractable text (the #197 log shows ~907 completion tokens and zero extracted parts), users would get a silent empty reply. That second half needs the raw SSE dump (diag-sse-event-* lines in the OpenCode output channel) from someone who can reproduce before we touch the extractors.

Related: #193, #197, #187, #79

ltmoerdani avatar Aug 27 '26 02:08 ltmoerdani

I can reproduce this on OpenCode Go with gpt-5.6-luna (VS Code 1.134.0, extension 0.7.2, Windows).

The request returns HTTP 200 and the stream includes response.completed with status=completed, but no [DONE] or finish_reason. The extension retries three times and then reports response stream ended before completion.

This appears to be the same Responses API completion-signal bug, and it also affects GPT-5.6 Luna on OpenCode Go, not only Muse Spark.

HanHan666666 avatar Aug 27 '26 02:08 HanHan666666

how to open the diag-sse log? I tried but could not get diag-sse log .

weizhen25 avatar Aug 27 '26 03:08 weizhen25

I try to edit out/transports/engine.js +383 local and reload. reporoduce bug and get the log as below

if (rawSseData.length > 0) { options.output?.appendLine( [diag-empty-response] model=${options.modelId} completionTokens=${String(usageSummary.completionTokens)} totalEvents=${String(totalEvents)} rawSseDataCount=${String(rawSseData.length)}` );

for (let i = 0; i < rawSseData.length; i++) {
    options.output?.appendLine(
        `[diag-sse-event-${String(i)}] ${(0, errors_1.truncateForLog)(JSON.stringify(rawSseData[i]))}`
    );
}

} // if (usageSummary.completionTokens && usageSummary.completionTokens > 0 && extractedPartCount === 0 && rawSseData.length > 0) { // options.output?.appendLine([diag-empty-response] model=${options.modelId} completionTokens=${String(usageSummary.completionTokens)} totalEvents=${String(totalEvents)} rawSseDataCount=${String(rawSseData.length)}); // for (let i = 0; i < rawSseData.length; i++) { // options.output?.appendLine([diag-sse-event-${String(i)}] ${(0, errors_1.truncateForLog)(JSON.stringify(rawSseData[i]))}); // } // } `

opencode-fail-muse.txt

weizhen25 avatar Aug 27 '26 03:08 weizhen25

I don't understand ts grammar. but gpt give me a workaround temp. it seems work for a while. edit local extract.js file. function updateRequestUsageSummary(summary, data) { if (!(0, utils_1.isRecord)(data)) { return; } // Responses API signals successful completion via response.completed. if (data.type === "response.completed") { summary.finishReason = "stop"; } waiting for the author's official release.

weizhen25 avatar Aug 27 '26 06:08 weizhen25