Repository navigation
Closed
Description
Symptom
Reported by the project owner from a real TUI session: the repetition guard tripped mid-generation ("the model streamed the same 96-character fragment 3 times in a row... Aborted the stream") and the turn simply died - the session went idle with the error and the task was abandoned until the user manually retried. The owner's ask: recover from this somehow, instead of just canceling the session's work.
Current mechanism (source at tip 3d4c83f0)
RepetitionGuard.guard(packages/opencode/src/session/llm/repetition-guard.ts) watches unfenced text deltas and fails the stream withRepetitionDetectedError(identical-line rule, issue feat(harness): detect and break out of repetitive text-generation loops #94; fragment-unit rule).SessionRetry.retryable(retry.ts:180) deliberately excludes the repetition message - retrying would replay the same looping stream. This exclusion must stay.processor.tshalt()setsassistantMessage.error, publishes the error, and the run loop returns "stop" (ctx.assistantMessage.errorcheck). The partial text streamed before the abort IS persisted (text deltas persist viaupdatePartDeltaas they stream), but nothing uses it - the turn just ends.- Prior art for "nudge, then deterministic backstop" recovery philosophy: review-stagnation (Prevent Review subagent doomloops on repeated literal output #171/Add a hard backstop for review stagnation recovery #176).
Direction (owner-approved intent)
Keep the guard's abort (it stops paying for the loop) and keep the retry exclusion. Add a bounded automatic recovery at the processor/run-loop level:
- When
RepetitionDetectedErrorends a generation, auto-continue the turn ONCE with a synthetic recovery message instead of going idle: state that the previous response was cut off by the repetition guard (with the repeated-fragment detail), that the text produced before the cutoff is preserved in the transcript, and instruct the model not to repeat that fragment and to proceed with the task (emit the pending tool call, or continue with different/shorter output). - Hard bound: the guard stays armed during the recovery generation; a second trip in the recovery generation surfaces the error and stops. No unbounded recovery loop. A configurable budget is fine (default 1) but the default path must terminate deterministically.
- NOT in scope: disabling or weakening the guard, raising the default thresholds, overriding via
OPENCODE_EXPERIMENTAL_REPETITION_*, or making the guard's abort retryable. Recovery resumes from persisted state; the harness never replays the aborted stream, so no side effect is re-executed by the harness. - Side-effect scoping (same discipline as opencode: recover truncated write tool calls into a guided continuation instead of a bare invalid-call error #736): the recovery message must not claim "nothing happened" - tool calls earlier in the aborted generation may have executed. Claim only what is known: the repeated text was discarded, prior persisted state stands.
Relations / constraints
- feat(harness): detect and break out of repetitive text-generation loops #94 (guard origin), fix(opencode): Bun segfaults during pathological M3 agent generation (reproducible) #89 (generation cap - the analogous terminal guard).
- feat(session): auto-resume replay-safe after stream interruption with tool calls #730 / feat(session): auto-resume replay-safe after stream interruption (Refs #730) #733 (stream-interruption auto-resume): different trigger (provider/stream failure vs. deliberate guard abort). If feat(session): auto-resume replay-safe after stream interruption (Refs #730) #733's replay-safe resume machinery lands first, reuse it where it genuinely fits, but this outcome is a separate front - do not duplicate patch ownership.
- fix(opencode): doom_loop permission false-triggers on parallel/same-batch tool calls and non-failure statuses #725 / fix(opencode): keep doom-loop guard to real failures across steps #728 (doom-loop guard): a bounded recovery nudge must not trip the doom-loop detector; verify against its real-failure definition.
- opencode: recover truncated write tool calls into a guided continuation instead of a bare invalid-call error #736 (truncated write-call recovery): sibling recovery front in
llm.ts's repair hook; disjoint locus (that one repairs tool-call args, this one resumes a guard-aborted turn).
Acceptance
- One bounded auto-recovery after a guard trip, with the recovery message contents above; the session proceeds without user intervention when the model cooperates.
- Guard still trips during recovery generation; second trip terminates with the error surfaced (deterministic bound, test proves no infinite loop).
- Existing behavior unchanged at equal strength: guard thresholds/defaults, retry exclusion, error message text, fence suppression.
- Tests: recovery message issued once with fragment detail; second trip stops; persisted partial text referenced honestly; doom-loop guard not tripped by a single recovery; run the touched test files plus typecheck and report exact pass counts.
Metadata
Metadata
Assignees
Labels
No labels
Activity
fix(session): keep the transcript for an in-flight finish nudge (Refs #…
test(tui): wait for diff request readiness (Refs #789)
fix(session): keep finish continuations with their prompt root (Refs #…
fix(session): satisfy lineage types and keep ordering lookup bounded …
fix(session): bound repetition recovery and keep turns with the corre…