Skip to content

fix: rebuild from history past echoes of the Run's own steers and notes - #162

Merged
oakimov merged 4 commits into
mainfrom
fix/history-rebuild-echoes-159
Nov 7, 2026
Merged

oakimov merged 4 commits into
mainfrom
fix/history-rebuild-echoes-159

Conversation

@oakimov

@oakimov oakimov commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Fixes #159.

Problem

When Cursor cuts a Run right after a tool result is written, the only safe recovery is a rebuild from host history. That rebuild was refused (opaque-activity) whenever the Run had earlier delivered an early steer or a host note. Cursor answers every delivered inject_context_action with user_message_appended, and that frame marked the whole Run as history-unsafe. The two refused Runs in the report were caused by host notes (background shell notices), not steers, so this covers both.

Fix

  • Match the echo to the Run's own injection: same message_id and text, Cursor reported it delivered, and the UserMessage holds only text, id and Cursor's echo metadata (fix(opencode2): support stable 2.0.x without ctx.catalog #24–HTTP/2 Run stream ignores HTTPS_PROXY / NO_PROXY (connect timeout behind an HTTP proxy) #26). Each injection matches once.
  • Keep matched echoes as plain replay state and decide at history-check time:
    • a steer needs OpenCode's promoted copy in the prompt, at the boundary it was promoted at;
    • a note needs its text in the seeded history, or the Run must still track it, in which case the rebuild carries it over unless the seed already has it.
  • Unmatched, duplicate, undelivered or extra-field echoes keep today's refusal. Transport replay and checkpoint resume still treat every echo as a stateful barrier.
  • A failed attempt whose only barriers were matched echoes may still reach the history check (replayBarrierEchoesOnly). Text, tool rows or any other frame in that attempt keep it final.
  • Every cause that makes history unsafe is now traced as a recovery: line (history-unsafe, appended-echo, appended-unmatched, appended-not-in-history), so it shows with CURSOR_PROVIDER_RECOVERY_DEBUG=1.

Nothing new is sent on normal turns; the injection's message_id was already sent and is now remembered. A rebuild seeds host history as before. Both majors share this path: 1.x has host notes but no early steers.

Verification

  • Unit tests for echo parsing, matching and the attempt flag (test/replay-safety.test.ts).
  • Host notes, end to end (test/run-error-host.test.ts): rebuilds when the note is still tracked, or checkpointed but still in history. Refuses for a wrong id, a duplicate echo, an extra field, an echo before delivered, and a note dropped from history.
  • Early steers, end to end (test/early-steer.test.ts): a promoted steer rebuilds with the steer sent once and nothing re-injected; an unpromoted one is refused.
  • Typecheck and build pass. On macOS, this branch alone shows the 6 review-script failures main already has there; fix: run the full checks on Linux, macOS and Windows hosts #161 fixes those.

Not yet done: the live OpenCode 2.x check from the issue (steer mid-Run, then force a cut after a later result write); the reporter offered to run it. The #24–#26 echo fields are accepted only as varints, per the recorded frame described in the issue; a different live wire type fails closed and shows up as appended-unmatched layout=….

Cursor answers every delivered inject_context_action with a
user_message_appended frame that echoes its message_id. That frame marked
the whole Run opaque, so once a Run had delivered an early steer or a host
note, a cut right after a tool result write could no longer rebuild from
host history and the turn ended as "automatic retry unsafe".

Match the echo to the Run's own delivered injection (same message_id and
text, nothing else) and keep it as plain replay state. At history-check
time the rebuild goes ahead only when the seeded prompt holds each one: a
promoted steer's copy at its promotion boundary, or a note's text, or a
note the Run still tracks and carries over. Unmatched, duplicate,
undelivered or extra-field echoes keep the refusal. Transport replay and
checkpoint resume still treat the echo as a stateful barrier; a failed
attempt whose only barriers were such echoes may reach the history check.

Every cause that makes history unsafe is now a recovery: trace line.

Fixes #159.
The fixture linked /usr/bin and /bin into a private PATH and skipped a
name only when fs.existsSync saw it. existsSync follows links, so a broken
link in /usr/bin (the current ubuntu-latest image ships one) read as
missing and the /bin pass linked the same name again: EEXIST. The setup
also ran outside try, so the failure left its temp directory behind.

Dedupe by name and set up inside try.
@codecov

codecov Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

analyzeReplayFrame reads an echo only from a frame validInteractionUpdateWire
accepted, which already proved update #6 holds exactly one length-delimited
user_message #1. appendedEcho checked that again, and its fallback branch
could never run (Codecov: the one uncovered patch line). State the
precondition instead, and cover an empty user_message.

@caeliq-reviewer caeliq-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No actionable findings.

Checks:

  • diff: passed. The diff matches the description and issue 159. A matched echo of this Run's delivered steer or host note stays a stateful barrier, and a history rebuild proceeds only when the seeded prompt holds it. Unmatched, duplicate, undelivered, and extra-field echoes still refuse. The description's statement that fields 24-26 are accepted only as varints matches appendedEcho.
  • harness: passed. Scripts ran from harness commit 6915cbb. There were no earlier review threads on this pull request.
  • isolated-check: passed. Install, build, and typecheck passed. The new echo, steer, and host-note rebuild tests passed. The wrapper exited 1 with 3264 pass, 10 skip, and 11 fail, all in review-eval or isolate-run self-tests: the archive has no git metadata, and nested user namespaces are refused. Required CI Typecheck, test, build passed on this head. On a normal checkout those self-tests passed except one opencode tool-path case the archive did not mount and therefore skipped.
  • native-runner: skipped. test-harness.py --diagnose reported Linux, backend docker, not ready: node is not installed and no local Docker engine is available. The native runner does not apply. Contributor execution used isolated-check.sh.
  • adversarial: passed. Candidate, omission, and failed-work retry passes finished. The forced credential failure during a note rebuild left the note deferred, and the ordinary retry seeded it once.
  • live: skipped. The harness forwards CURSOR_API_KEY only after isolated-check.sh exits 0. The wrapper exited 1 on archive self-tests, so the live gate did not run and the key was not passed. The cut after a result write was not observed on a live Cursor Run.

Limitations:

  • No live OpenCode prompt was run. The installed CLI is 2.2.5, and it was not given a prompt or the API key.
  • Fields 24-26 were checked as varints with independent fixture bytes, not a frame captured from this run. Another wire type fails closed.
  • Echo state is in-memory session state. A process restart was not exercised.

Runtime: linux.
Reviewed #162 at d013c97.

@oakimov
oakimov merged commit e1066a9 into main Nov 7, 2026
10 checks passed
@nkoynov

nkoynov commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

I ran the 2.x live check on e1066a9 (OpenCode 2.2.5 through T3, Opus 5.5, a real Cursor account), with a test-only patch that cuts the Run 30 ms after a shell result once a host note of that Run was delivered. With a nested AGENTS.md note, Cursor's echo matched the note (appended-echo kind=note) and the cut Run rebuilt from host history (completed-tool-results): the shell command ran once, the note was not sent again, and the final answer still used it. Our fork on top of e1066a9 did the same for a background shell completion notice, the case from #140; before #162 both cuts were refused (opaque-activity). I couldn't get T3 to deliver a steer inside a running Run on this setup (it arrived as the next turn), so the steer path is covered only by your tests.

@oakimov

oakimov commented Nov 7, 2026

Copy link
Copy Markdown
Owner Author

@nkoynov I'll reopen the issue. As I do not use T3, can you share a little more info how to reproduce?

@nkoynov

nkoynov commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Sure, no T3 needed. I redid it with plain opencode serve (2.2.5) and a small script.

Patch: branch test/cut-after-result-e1066a9 on my fork (bb73b91), one test-only commit on e1066a9. It cuts the Run with an RST_STREAM CANCEL and is off unless one of these is set:

  • CURSOR_PROVIDER_TEST_CUT_AFTER_RESULT_MS=30: 30 ms after a shell result, once an injection of that Run was delivered (note, promoted steer)
  • CURSOR_PROVIDER_TEST_CUT_AFTER_DELIVERED_MS=30: 30 ms after Cursor reports an injection delivered (unpromoted steer)

Steps (script: https://gist.github.com/nkoynov/f2ebd14a766894999cfe4d22f7cd221c):

# build the branch, point OpenCode 2.x's cursor plugin at it, then:
CURSOR_PROVIDER_DEBUG=1 CURSOR_PROVIDER_DEBUG_FILE=$PWD/cut.log CURSOR_PROVIDER_TEST_CUT_AFTER_RESULT_MS=30 \
  OPENCODE_PASSWORD=pw opencode serve --port 4096
export OPENCODE_PASSWORD=pw CURSOR_PROVIDER_DEBUG_FILE=$PWD/cut.log
python3 repro-162.py note r-note
python3 repro-162.py steer r-steer
# restart serve with CURSOR_PROVIDER_TEST_CUT_AFTER_DELIVERED_MS=30 instead, then: python3 repro-162.py steer r-steer2

The steer is a second POST /api/session/{id}/prompt with "delivery": "steer". The provider injects it only while it is streaming the Run, so the script sends it 1 s after the read result, while the model writes a list. If the log says left for the next step, it missed; run it again. Our T3 run missed the same way: the steer arrived after the turn, and on time it would have landed while the host ran sleep, when no Run is streaming.

What we saw:

  • Note: echo matched, cut after the shell result, reopened ... kind=rebase reason=completed-tool-results, the command ran once.
  • Steer, promoted: injected and delivered during the text, echo matched, promoted after the next shell result (before 1 early steer(s) the held Run already has), cut right there, rebuilt from history. Both commands ran once, the steer is in the session once and the reply followed it.
  • Steer, unpromoted: the Run had a checkpoint from after the read, so it resumed that (kind=resume) and your check never came into it. The steer wasn't injected again; OpenCode's own copy reached the model at the next boundary, once. I didn't get appended-not-in-history live; that needs a cut with no checkpoint to resume.
  • Also: a steer taken while Cursor waits on a background job (AwaitShell), promoted, then cut after the next shell result, was refused with history-check reason=missing-host-results execs=60 results=2. The await's polls seem to count as execs without host results, so that one ended as "automatic retry unsafe". repro-162.py steer-await shows it.

Lines to look for (need CURSOR_PROVIDER_DEBUG=1; CURSOR_PROVIDER_RECOVERY_DEBUG=1 alone keeps only the recovery: ones): steer: ... injected into the pumping Run, context_injection_state: ... state=delivered, recovery: appended-echo kind=steer|note, early steer(s) the held Run already has, TEST: cutting, recovery: history-check ... reason=, recovery: reopened ... kind=, recovery: appended-not-in-history.

@nkoynov

nkoynov commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Follow-up on the AwaitShell case: our fork rebuilds it. Its history check counts only execs that went to the host (hostExecs, from c11480f), so the await polls the provider answers itself don't count as missing results. On e1066a9 they do (execs=60 results=2). With the same steer-during-await cut, the fork on top of e1066a9 rebuilt 2 of 2 times, with each tool run once and the steer sent once.

@oakimov
oakimov deleted the fix/history-rebuild-echoes-159 branch November 9, 2026 18:15
thaoran pushed a commit to thaoran/cursor-opencode-provider that referenced this pull request Nov 24, 2026
Follow-up to oakimov#162. A Run cut right after a tool result could still not
rebuild from host history when Cursor had earlier waited on a background
shell: each AwaitShell poll of the shell's terminal record is an exec the
provider answers itself, and the history check wanted a host result for
every exec (`missing-host-results`). Mark those reads and leave them out of
that check. A poll in the cut attempt itself stays a barrier.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rebuild from history after a cut when the Run delivered an early steer

2 participants