fix: [#834] cap todowrite per session to break runaway loops

Closed
AN
4mo ago
+325-1

What does this PR do?

Caps todowrite at 50 calls per session with a warning at 25. Telemetry-2026-05-21 showed 29,562 total todowrite calls in 14 days with one machine hitting 9,139 calls across 28 sessions (~325 per session). The existing doom-loop detector in session/processor.ts catches per-message bursts but not the slow-burn pattern of 10-15 calls per turn across many turns.

Implementation: tool-level per-session counter inside TodoWriteTool. Two thresholds:

  • 25 calls → warn: emit telemetry + insert a warning into the tool output that the LLM will read. Todos still persist.
  • 50 calls → refuse: emit telemetry + refuse the update with a structured message telling the agent to stop. Existing todos preserved.

Extracted into a testable helper recordTodoWriteCall(sessionID) returning { action, count }.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Issue for this PR

Closes #834

How did you verify your code works?

  • tsgo --noEmit: pass
  • bun test test/tool/todowrite-loop.test.ts test/session/todo.test.ts: 15 pass / 0 fail (12 new + 3 existing)
  • 12 new tests pin every boundary: counter mechanics, action=ok below warn, action=warn exactly at threshold, action=ok between warn and refuse, action=refuse at and beyond, refusal repeats, threshold constants sane
  • Marker check: pass — altimate_change markers applied to all changed lines in upstream-shared files

Checklist

  • My code follows the project's code style
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective
  • New and existing unit tests pass locally with my changes
  • Marker check passes
  • Linked to issue #834

Files changed (2):

  • packages/opencode/src/tool/todo.ts — per-session counter + recordTodoWriteCall helper + warn/refuse branches in TodoWriteTool.execute
  • packages/opencode/test/tool/todowrite-loop.test.ts — 12 tests covering every boundary

Expected telemetry impact:

  • todowrite call volume per session capped at ~50 (down from 9,000+ peaks)
  • doom_loop_detected events fire on every session that crosses threshold (previously invisible to the per-message detector)
  • Reduced token/context burn from runaway todo updates

P0 series progress (from telemetry-analysis-2026-05-21):

  • #827 / #828: finops_* 100% failure
  • #830 / #831: project_scan 32% errors
  • #832 / #833: build agent 0% completion
  • #834 / this PR: todowrite runaway

Summary by cubic

Caps todowrite per session (warn at 25, block at 50) to stop slow-burn loops and cut token/context waste. Addresses #834 by tracking repeat tool use across turns, and clarifies that the warning means future calls will be refused while the current list is preserved.

  • Bug Fixes
    • Add per-session counter via recordTodoWriteCall(sessionID); emit doom_loop_detected on warn and once on the first refusal to avoid telemetry spam.
    • At 25 calls: show an in-output warning that future updates will be refused and todos are preserved; at 50+: refuse with clear guidance; include call_count in metadata.
    • Clear counters on Session.Event.Deleted (lazy, best-effort); lazy subscription now retries if Bus.subscribe fails; add clearTodoWriteCounter(sessionID) to unblock legitimate long sessions; tests cover thresholds, isolation, transition-only telemetry, and the escape hatch.

Summary by CodeRabbit

  • New Features

    • Added per-session runaway detection for todo writes with warning and refusal thresholds; warnings surface a notice but still complete the write, refusals block further writes and include call-count metadata.
  • Tests

    • Added comprehensive tests covering per-session counting, threshold behavior, counter reset/unblock behavior, and isolation across sessions.

Review Change Stack