Skip to content

opencode: recover the task after the repetition guard trips instead of abandoning the turn #738

Closed

Description

@agenteinstinct1-max

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)

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:

  1. When RepetitionDetectedError ends 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).
  2. 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.
  3. 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.
  4. 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

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions