Skip to content

Commit a266d02

Browse files
fix(opencode): recover from truncated tool calls instead of failing silently
When a model's output hits the token limit mid-tool-call, the JSON arguments are truncated and tool parsing fails with 'expected string, received undefined'. Two fixes: 1. experimental_repairToolCall now detects truncation: when the tool name is a valid registered tool but args failed to parse, return an actionable error telling the model to split its operation into smaller pieces. Previously all parse failures were routed to a generic 'invalid tool' handler with no recovery guidance. 2. The session loop now handles finishReason 'length': instead of silently exiting when the model is cut off by the token limit, inject a synthetic continuation message so the model can resume where it left off. Fixes anomalyco#18108 Fixes anomalyco#17471 Refs anomalyco#14519, anomalyco#13102
1 parent 46f243f commit a266d02

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,28 @@ export namespace LLM {
329329
toolName: lower,
330330
}
331331
}
332+
// When the tool name is valid but args failed to parse, the output
333+
// was likely truncated by the token limit. Give the model a clear
334+
// signal so it can retry with smaller input instead of looping.
335+
if (tools[failed.toolCall.toolName] || tools[lower]) {
336+
l.warn("truncated tool call detected", {
337+
tool: failed.toolCall.toolName,
338+
error: failed.error.message,
339+
})
340+
return {
341+
...failed.toolCall,
342+
input: JSON.stringify({
343+
tool: failed.toolCall.toolName,
344+
error:
345+
"Your output was truncated because it exceeded the token limit. " +
346+
"The tool arguments were cut off and could not be parsed. " +
347+
"Split your operation into smaller pieces and try again. " +
348+
"For file writes, use the Edit tool for targeted changes or write smaller sections. " +
349+
"For bash commands, break long heredocs into multiple shorter commands.",
350+
}),
351+
toolName: "invalid",
352+
}
353+
}
332354
return {
333355
...failed.toolCall,
334356
input: JSON.stringify({

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,35 @@ NOTE: At any point in time through this workflow you should feel free to ask the
13761376
const hasToolCalls =
13771377
lastAssistantMsg?.parts.some((part) => part.type === "tool" && !part.metadata?.providerExecuted) ?? false
13781378

1379+
// When the model hit the output token limit (finishReason: "length"),
1380+
// its response was truncated. Instead of silently exiting, inject a
1381+
// continuation message so the model can finish its work.
1382+
if (lastAssistant?.finish === "length" && lastUser.id < lastAssistant.id) {
1383+
log.warn("output truncated by token limit, auto-continuing", { sessionID })
1384+
const continuationMsg: MessageV2.User = {
1385+
id: MessageID.ascending(),
1386+
sessionID,
1387+
role: "user",
1388+
time: { created: Date.now() },
1389+
agent: lastUser.agent,
1390+
model: lastUser.model,
1391+
}
1392+
yield* sessions.updateMessage(continuationMsg)
1393+
yield* sessions.updatePart({
1394+
id: PartID.ascending(),
1395+
messageID: continuationMsg.id,
1396+
sessionID,
1397+
type: "text",
1398+
text:
1399+
"Your previous response was truncated because it exceeded the output token limit. " +
1400+
"If you were writing a file, split it into smaller pieces. " +
1401+
"If you were using a tool, use smaller arguments. " +
1402+
"Continue where you left off.",
1403+
synthetic: true,
1404+
})
1405+
continue
1406+
}
1407+
13791408
if (
13801409
lastAssistant?.finish &&
13811410
!["tool-calls"].includes(lastAssistant.finish) &&

0 commit comments

Comments
 (0)