Skip to content

fix(session): bound repetition-guard recovery to its own caller (Refs #738) - #777

Closed
agenteinstinct1-max wants to merge 1 commit into
dev-vibecodedfrom
arena/47d94bed-silvercode
Closed

agenteinstinct1-max wants to merge 1 commit into
dev-vibecodedfrom
arena/47d94bed-silvercode

Conversation

@agenteinstinct1-max

@agenteinstinct1-max agenteinstinct1-max commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

Issue for this PR

Refs #738 (not Closes: the report describes a symptom we can now bound and prove, but the underlying prompt/run architecture keeps the door open for adjacent ordering bugs; #738 lists four acceptance points and all four have dedicated tests here).

This PR replaces the superseded draft #769. It is a clean replay of the six-file #738 change on top of the current accepted tip, not a rebase or a revert of anything accepted.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

A repetition-guard abort cuts a generation off mid-stream. #738 asks that the aborted prompt get exactly one bounded continuation, that the continuation stay with that prompt even when another prompt is queued/created/persisted while it runs, that a silent injection never suppress or steal it, and that nothing be re-executed or re-requested.

Base and head

Files (7)

  1. packages/opencode/src/session/llm/repetition-guard.ts — persists a stable data.ref marker on the guard abort. Recovery now identifies its own abort by typed identity, not by message text, so an HTTP 400 that happens to quote the guard text is a different failure (test included).
  2. packages/opencode/src/session/message-v2.ts — records that marker, and keeps the parts a guard abort had already persisted. Because of this a completed tool call stays visible to the continuation request, so the model builds on it instead of issuing it again (test asserts one call, recorded output, no second execution). A provider HTTP error cannot carry the marker, so nothing else changes classification.
  3. packages/opencode/src/session/subagent-result.ts — the recovery message declares { promptID, attempt } in its synthetic text-part metadata, and only that exact continuation counts as a continuation of its declared prompt. A synthetic-only user message that is anything else (shell marker, task summary, bridge text) is still a boundary, and a recovery whose declared prompt is not its immediate predecessor is treated as a boundary instead of borrowing neighbour lineage.
  4. packages/opencode/src/session/prompt.ts — the behavioural core:
    • generationTarget chooses the message a generation belongs to from the transcript and the live set of callers currently waiting: (1) an unanswered recovery continuation the runner already injected, (2) the newest prompt whose caller is still waiting and unanswered, (3) a message persisted after the latest assistant, (4) the owner of the turn that produced the latest assistant, (5) newest replyable / newest message.
    • rule (2) needs live state because persistence cannot prove anyone is waiting: after cancel, a released queued prompt stays persisted and unanswered forever. A caller registers in a per-session refcount while it is inside the run.
    • the recovery budget is scoped to the prompt lineage (REPETITION_RECOVERY_MAX = 1), so a second trip surfaces the error instead of looping, and a spent lineage is never regenerated.
    • each caller settles on the answer to its own prompt; a stored answer for another prompt is never returned to it.
    • expectsReply respects the persisted no-reply intent, so a silent marker cannot suppress the recovery the aborted prompt is owed.
    • recovery wording corrected: earlier copies of the repeated fragment stay in the transcript, only the output past the cutoff is missing, and the instruction is "Do not repeat that fragment again".
  5. packages/schema/src/v1/session.ts — seventh file, added deliberately: User.noReply is the persisted form of PromptInput.noReply. Previously the intent existed only in memory, so a later run could not tell a silent injection from a caller that legitimately submitted synthetic parts. Part shape cannot stand in for it, which is exactly the gap that let a silent marker suppress the P1 recovery. Optional field, no migration needed.
  6. packages/opencode/test/session/prompt.test.ts — the eight opencode: recover the task after the repetition guard trips instead of abandoning the turn #738 guard tests (Q1/Q2/Q3, HTTP-400-quote, adversarial cases) plus the controlled-runtime coverage described below; each test has its own prompt/call/parent/idle/text assertions.
  7. packages/opencode/test/session/subagent-result.test.ts — lineage helper unit tests, including the "another synthetic message is still a boundary" and "marker readable only from a synthetic text part" directions.

Behaviour covered by new/rewritten tests (all against the real prompt runtime with a scripted provider, not a stub of the logic):

  • a recovery is injected once and the turn continues; a second trip terminates with the guard error surfaced (1 recovery, 0 further requests).
  • a completed tool call stays visible to the recovery request and runs once.
  • a synthetic message carrying a real caller's priority is still answered exactly once.
  • an actual no-reply marker plus a synthetic task marker during a guard trip does not suppress the sole recovery: the aborted prompt gets its answer, the marker gets no answer, no text is attributed to it.
  • B1 seeded [P1, A1 trip, r1, P2]: the later P2 does not steal the generation, and r1 does not dangle on a P1 guard error.
  • B2 earlier-created/later-persisted P2 with async parts: an answered newer r1 cannot turn the terminal fallback into UnknownError / parts: [] with no generation.
  • a prompt queued before the trip, and a prompt queued during a recovery that trips again, each get their own turn; cancel releases a queued prompt and the next prompt runs once.
  • a stored answer for another prompt is never returned to an unanswered caller.

No budget, timeout or assertion was relaxed; no test was retried into green.

How did you verify your code works?

Bun 1.3.14, packages/opencode, this sandbox (2 vCPU / 4 GB).

  • bun test --timeout 30000 test/session/prompt.test.ts → 151 pass / 1 skip / 0 fail, 888 expect() calls, 258.9 s (skip is the pre-existing "v2 projector disabled" case).
  • guard subset -t "repetition-guard" → 12 pass / 0 fail.
  • bun test --timeout 30000 test/session/subagent-result.test.ts test/session/llm/repetition-guard.test.ts test/session/llm-repair.test.ts → 63 pass / 0 fail.
  • bun test --timeout 30000 test/permission test/permission-task.test.ts → 126 pass / 0 fail (test(opencode): preload config before permission waits (Refs #489, #732) #751 surface, untouched by this PR, re-run as a cross-check).
  • bun test --timeout 30000 test/session (36 files) → 878 pass / 8 skip / 1 todo / 1 fail; the failure is the pre-existing timing test model switch during long backoff cancels retry promptly (not after full delay) at 2953 ms under full-directory load. Re-run standalone, test/session/model-switch-retry.test.ts → 5 pass / 0 fail (same test 1197 ms). See unresolved below — I am not claiming this is fixed.
  • NODE_OPTIONS=--max-old-space-size=3072 bun run typecheck → clean. The plain script OOMs in this 4 GB sandbox (FATAL ERROR: Ineffective mark-compacts, ~1.9 GB heap) because tsc over the whole package needs more than the default ceiling; this is sandbox memory, not a code error, and no type errors are reported.
  • prettier 3.6.2 with the repo config on all 7 touched files → clean. Note: both base versions of prompt.ts / prompt.test.ts were not prettier-clean, so a small number of whitespace-only lines appear in the diff; git diff --ignore-all-space shows the semantic change only.

Positive mutants (each one re-broken on the green tree, then restored)

  • message-v2.ts: replacing the RepetitionGuard.isStoredError(...) clause with false fails 2 guard tests — the recovery injection and the "completed tool call stays visible".
  • prompt.ts expectsReply: dropping noReply !== true fails "a silent no-reply marker does not suppress the one recovery".
  • prompt.ts target rule 2: dropping waiting.has(...) fails "queued prompts are released by cancel and the next prompt runs once".

Scope

git diff --stat a241c84..HEAD is exactly the 7 files above (2,024 insertions, 229 deletions), of which ~1,780 are tests. llm.ts, llm-repair.test.ts, the llm-server.ts toolRaw helpers, the #751 permission tests, the #735 activity code/tests and both CLI retry fixtures are byte-identical to a241c84 — no file outside the list is touched.

Unresolved / not claimed

  • The <1500 ms model-switch timing test fails only under full-directory load in this sandbox (2953 ms) and passes standalone (1197 ms). This was already unexplained in the previous attempt; it is not addressed here and not caused by the guard changes (no retry/backoff file is touched).
  • fix(opencode): keep repetition recovery results with their callers (Refs #738) #769 also reported a Windows-CI state-840/kv.json readiness race and a one-off mime-prep failure. Neither is reproduced or explained here.
  • Old technical approvals from the rejected attempt are not treated as a waiver of these gaps.
  • The broad live post-snapshot probes from the earlier attempt were 4/4 safe, but the pre-snapshot <1m ss-liver case was never hit live, so this PR does not claim it as verified.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…738)

A repetition-guard abort was handled by a single "is this the newest user
message" check, so a recovery instruction could be consumed by, or dangle
behind, a prompt queued while it ran, and a silent injection could suppress
the one recovery the aborted prompt was owed.

This replay applies the #738 work on top of the current accepted tip instead
of the stale tree PR #769 was based on:

- repetition-guard: persist a stable `data.ref` marker on the guard abort so
  recovery reads identity, not message text (an HTTP 400 that quotes the
  guard text is a different failure).
- message-v2: keep the parts a guard abort already persisted, so a completed
  tool call stays visible to the recovery request and is not re-executed.
- subagent-result: declare a recovery's prompt lineage in its synthetic part
  metadata and treat exactly that continuation as a continuation.
- prompt: choose the generation target from the transcript plus the live set
  of waiting callers, scope the recovery budget per prompt lineage, settle
  each caller on its own answer, and preserve a persisted no-reply intent.
- schema: store `PromptInput.noReply` on the user message, because part shape
  cannot distinguish a silent injection from a caller that submitted
  synthetic parts.

Recovery wording now states that earlier copies of the repeated fragment
stay in the transcript and only the output past the cutoff is missing.

Refs #738

Co-authored-by: arena-agent <[email protected]>
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant