Skip to content

Commit 0e1d1bd

Browse files
committed
fix(session): detect truncated tool calls and auto-continue on length (anomalyco#18108)
1 parent 978b172 commit 0e1d1bd

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,19 @@ export namespace LLM {
147147
},
148148
)
149149

150-
const maxOutputTokens =
151-
isCodex || provider.id.includes("github-copilot") ? undefined : ProviderTransform.maxOutputTokens(input.model)
150+
// Coordinate maxOutputTokens with thinking budget
151+
// For Anthropic: budgetTokens must be < max_tokens, and both share the output pool
152+
// Ensure enough room for both thinking and actual output
153+
const MIN_OUTPUT_SPACE = 16_000
154+
const limit = isCodex || provider.id.includes("github-copilot") ? undefined : ProviderTransform.maxOutputTokens(input.model)
155+
const budget = Number(
156+
options.thinking?.budgetTokens ??
157+
options.reasoningConfig?.budgetTokens ??
158+
0,
159+
)
160+
const maxOutputTokens = limit !== undefined && budget > 0
161+
? Math.max(limit, budget + MIN_OUTPUT_SPACE)
162+
: limit
152163

153164
const tools = await resolveTools(input)
154165

@@ -190,6 +201,22 @@ export namespace LLM {
190201
toolName: lower,
191202
}
192203
}
204+
// Known tool but args truncated (JSON parse failure)
205+
if (tools[failed.toolCall.toolName] || tools[lower]) {
206+
return {
207+
...failed.toolCall,
208+
input: JSON.stringify({
209+
tool: failed.toolCall.toolName,
210+
error:
211+
"Tool call arguments were truncated because output exceeded the token limit. " +
212+
"Do NOT retry with the same approach. " +
213+
"For large file writes, split content into multiple smaller Write calls " +
214+
"(write first portion, then use Edit to append remaining sections).",
215+
}),
216+
toolName: "invalid",
217+
}
218+
}
219+
// Genuinely unknown tool
193220
return {
194221
...failed.toolCall,
195222
input: JSON.stringify({

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ export namespace SessionPrompt {
293293
let structuredOutput: unknown | undefined
294294

295295
let step = 0
296+
let lengthCount = 0
297+
const MAX_LENGTH_CONTINUES = 3
296298
const session = await Session.get(sessionID)
297299
while (true) {
298300
SessionStatus.set(sessionID, { type: "busy" })
@@ -695,6 +697,20 @@ export namespace SessionPrompt {
695697
break
696698
}
697699

700+
// Handle finishReason: "length" (output truncated by token limit)
701+
if (processor.message.finish === "length") {
702+
lengthCount++
703+
if (lengthCount >= MAX_LENGTH_CONTINUES) {
704+
processor.message.error = new MessageV2.OutputLengthError({}).toObject()
705+
await Session.updateMessage(processor.message)
706+
break
707+
}
708+
// Don't treat as finished — continue the loop
709+
continue
710+
}
711+
// Reset counter on non-length finish
712+
lengthCount = 0
713+
698714
// Check if model finished (finish reason is not "tool-calls" or "unknown")
699715
const modelFinished = processor.message.finish && !["tool-calls", "unknown"].includes(processor.message.finish)
700716

0 commit comments

Comments
 (0)