Skip to content

Commit 9ef44b7

Browse files
local: cap unknown-finish retries
SessionPrompt.run's loop-exit check treats an "unknown" finish reason the same as "tool-calls" (there might be more work to do, keep going), which is right for a one-off glitch but had no upper bound: a provider whose finish_reason never maps to a recognized FinishReason caused the loop to re-send the same request forever with no backoff. Cap consecutive unresolved ("unknown", no tool calls) attempts at MAX_UNKNOWN_FINISH_RETRIES before giving up. Not sent upstream (anomalyco#49414 draft PR withdrawn) — kept as a local-only patch on top of the vendor baseline.
1 parent 5202020 commit 9ef44b7

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ globalThis.AI_SDK_LOG_WARNINGS = false
6363
const decodeMessageInfo = Schema.decodeUnknownExit(SessionV1.Info)
6464
const decodeMessagePart = Schema.decodeUnknownExit(SessionV1.Part)
6565
const MAX_MCP_RESOURCE_BLOB_BYTES = 10 * 1024 * 1024
66+
// How many times in a row we'll silently retry a turn that ends with an
67+
// unrecognized ("unknown") finish reason and no tool calls before giving up.
68+
// A provider occasionally dropping its finish signal is expected to resolve
69+
// within a request or two; a provider that never resolves it (e.g. a
70+
// finish_reason value this build's FinishReason schema doesn't recognize)
71+
// must not retry forever. See anomalyco/opencode#49414.
72+
const MAX_UNKNOWN_FINISH_RETRIES = 3
6673
const SUPPORTED_MCP_RESOURCE_ATTACHMENT_MIMES = new Set([
6774
"application/pdf",
6875
"image/gif",
@@ -1083,6 +1090,12 @@ const layer = Layer.effect(
10831090
const ctx = yield* InstanceState.context
10841091
let structured: unknown
10851092
let step = 0
1093+
// "unknown" finish reasons are treated as possibly-transient (see below) and the
1094+
// loop retries by design (e.g. a provider that ends its stream without a proper
1095+
// finish signal). If a provider deterministically never resolves to a recognized
1096+
// finish reason, that retry has no bound today and re-sends the same request
1097+
// forever - see anomalyco/opencode#49414. Cap it.
1098+
let consecutiveUnknownFinishes = 0
10861099
const session = yield* sessions.get(sessionID).pipe(Effect.orDie)
10871100

10881101
while (true) {
@@ -1108,6 +1121,10 @@ const layer = Layer.effect(
11081121
(part) => part.type === "tool" && !part.metadata?.providerExecuted && !isOrphanedInterruptedTool(part),
11091122
) ?? false
11101123

1124+
const isUnresolvedFinish =
1125+
lastAssistant?.finish === "unknown" && !hasToolCalls && lastAssistant.parentID === lastUser.id
1126+
consecutiveUnknownFinishes = isUnresolvedFinish ? consecutiveUnknownFinishes + 1 : 0
1127+
11111128
if (
11121129
lastAssistant?.finish &&
11131130
!["tool-calls", "unknown"].includes(lastAssistant.finish) &&
@@ -1129,6 +1146,20 @@ const layer = Layer.effect(
11291146
break
11301147
}
11311148

1149+
// An "unknown" finish reason is treated above as possibly-transient and
1150+
// retried like "tool-calls", but with no tool calls to resolve there's
1151+
// nothing that will change about the next request. If a provider never
1152+
// resolves to a recognized finish reason, don't retry forever - see
1153+
// anomalyco/opencode#49414.
1154+
if (isUnresolvedFinish && consecutiveUnknownFinishes > MAX_UNKNOWN_FINISH_RETRIES) {
1155+
yield* Effect.logWarning("loop exit after repeated unrecognized finish reason", {
1156+
"session.id": sessionID,
1157+
messageID: lastAssistant?.id,
1158+
attempts: consecutiveUnknownFinishes,
1159+
})
1160+
break
1161+
}
1162+
11321163
step++
11331164
if (step === 1)
11341165
yield* title({

‎packages/opencode/test/session/prompt.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,47 @@ noLLMServer.instance(
460460
{ config: cfg },
461461
)
462462

463+
it.instance(
464+
// Regression test for #49414: "loop continues when finish is unknown"
465+
// (below) intentionally retries once when a provider ends a stream
466+
// without a proper finish signal - that's a reasonable way to recover
467+
// from a one-off glitch. But if a provider *never* resolves to a
468+
// recognized finish reason (e.g. its finish_reason string never maps to
469+
// one of our known values), the same retry had no upper bound and kept
470+
// re-sending the same request forever. This asserts it's now capped.
471+
"loop gives up after repeated unrecognized finish reason instead of retrying forever",
472+
() =>
473+
Effect.gen(function* () {
474+
const { llm } = yield* useServerConfig(providerCfg)
475+
const prompt = yield* SessionPrompt.Service
476+
const sessions = yield* Session.Service
477+
const session = yield* sessions.create({
478+
title: "Pinned",
479+
permission: [{ permission: "*", pattern: "*", action: "allow" }],
480+
})
481+
yield* prompt.prompt({
482+
sessionID: session.id,
483+
agent: "build",
484+
noReply: true,
485+
parts: [{ type: "text", text: "hello" }],
486+
})
487+
// Every reply ends its stream without a finish line, so every attempt
488+
// maps to "unknown" - never a real "stop". Push more than the retry
489+
// cap; the loop must give up before exhausting the queue.
490+
for (let i = 0; i < 10; i++) {
491+
yield* llm.push(reply().text(`attempt ${i}`))
492+
}
493+
494+
const result = yield* prompt.loop({ sessionID: session.id })
495+
expect(result.info.role).toBe("assistant")
496+
if (result.info.role === "assistant") expect(result.info.finish).toBe("unknown")
497+
// 1 initial attempt + MAX_UNKNOWN_FINISH_RETRIES retries, then it must
498+
// stop instead of consuming the rest of the queued replies.
499+
expect(yield* llm.calls).toBe(4)
500+
}),
501+
{ config: cfg },
502+
)
503+
463504
noLLMServer.instance(
464505
"loop exits for a completed parent turn with nonmonotonic message IDs",
465506
() =>

0 commit comments

Comments
 (0)