fix(background-agent): add circuit breaker to prevent subagent infinite loops
Adds a configurable maxToolCalls limit (default: 200) that automatically cancels background tasks when they exceed the threshold. This prevents runaway subagent loops from burning unlimited tokens, as reported in #2571 where a Gemini subagent ran 809 consecutive tool calls over 3.5 hours costing ~$350. The circuit breaker triggers in the existing tool call tracking path (message.part.updated/delta events) and cancels the task with a clear error message explaining what happened. The limit is configurable via background_task.maxToolCalls in oh-my-opencode.jsonc. Fixes #2571
This commit is contained in:
@@ -11,6 +11,8 @@ export const BackgroundTaskConfigSchema = z.object({
|
||||
/** Timeout for tasks that never received any progress update, falling back to startedAt (default: 1800000 = 30 minutes, minimum: 60000 = 1 minute) */
|
||||
messageStalenessTimeoutMs: z.number().min(60000).optional(),
|
||||
syncPollTimeoutMs: z.number().min(60000).optional(),
|
||||
/** Maximum tool calls per subagent task before circuit breaker triggers (default: 200, minimum: 10). Prevents runaway loops from burning unlimited tokens. */
|
||||
maxToolCalls: z.number().int().min(10).optional(),
|
||||
})
|
||||
|
||||
export type BackgroundTaskConfig = z.infer<typeof BackgroundTaskConfigSchema>
|
||||
|
||||
Reference in New Issue
Block a user