Skip to content

fix: rebuild from history past AwaitShell's terminal-record polls (follow-up to #162) - #164

Merged
oakimov merged 2 commits into
mainfrom
fix/history-rebuild-await-polls-159
Nov 8, 2026
Merged

oakimov merged 2 commits into
mainfrom
fix/history-rebuild-await-polls-159

Conversation

@oakimov

@oakimov oakimov commented Nov 7, 2026

Copy link
Copy Markdown
Owner

Follow-up to #162. Fixes #159.

Problem

#162 let a Run that Cursor cuts right after a tool result rebuild from host history past echoes of its own steers and notes. @nkoynov's live check confirmed that for notes and steers, but found one case still refused: a steer taken while Cursor waits on a background shell (AwaitShell), promoted, then a cut after the next shell result. The trace was history-check reason=missing-host-results execs=60 results=2.

AwaitShell watches the shell by reading its terminal record over and over. The provider answers each of those reads itself (runtimeExecMessages), so none of them gets a host result. The history check wanted a host result for every exec the Run saw, so the 58 polls made it refuse. #162's tests had no Cursor-side wait, so they never hit this.

Fix

  • isTerminalRecordRead (src/runtime-files.ts) recognises a native read_args of a host shell's terminal record. Spill reads and writes, other names under terminals/ and paths outside the runtime root don't count.
  • After the provider answers such a read, AttemptReplaySafety.recordTerminalRead marks that exec. The history check leaves marked execs out when it asks for host results. They still claim their display call as before.
  • Unchanged: a poll in the cut attempt itself still blocks the rebuild. Other execs the provider answers itself (e.g. list_mcp_resources) still refuse, and so do missing host results.
  • The recovery: history-check line now also prints terminalReads=N.

Nothing new is sent on normal turns, and a rebuild seeds host history as before, so prompt caching is unaffected. Both majors share this path.

Verification

  • test/run-error-host.test.ts: terminal-poll (three polls between two host shells, then a cut) now rebuilds. Before the fix it failed with missing-host-results execs=5 results=2. terminal-poll-after-result (a poll in the cut attempt) is still refused.
  • test/early-steer.test.ts: the reported case, a promoted steer during AwaitShell polls followed by a cut. It rebuilds with the steer sent once and nothing re-injected. It fails without the fix.
  • test/runtime-files.test.ts: unit test for isTerminalRecordRead.
  • bun run typecheck, bun run build and bun test pass (3297 tests, 0 failures).

Not covered:

  • No live run yet. @nkoynov, repro-162.py steer-await against this branch would confirm it.
  • A terminal-record read through an advertised MCP read (mcp_args) is not exempted. I have no wire evidence that AwaitShell uses that path.

Follow-up to #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 #159.
@codecov

codecov Bot commented Nov 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@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. Read the recovery diff, its callers in the shared pump, and the in-scope review rules. The behavioral claims match the code. No actionable findings.
  • description-accuracy: passed. The description matches the diff. It names isTerminalRecordRead, recordTerminalRead, and the history-check terminalReads count. It says a poll in the cut attempt and other self-answered execs such as list_mcp_resources still refuse, which the scripted runs confirmed. It names terminal-poll, terminal-poll-after-result, the promoted-steer case, and the isTerminalRecordRead unit test. Its 3297 passing tests equal this Linux suite's 3302 minus five tests in test/review-scripts.test.ts that are skipped off Linux. Its note that a live steer-await run was still open is what the live A/B below answers.
  • adversarial: passed. Validated the cut-attempt, list_mcp_resources, false-green, cache, and MCP-read candidates, then checked later uses of the mark and both forced recovery outcomes.
  • install: passed (exit 0). Isolated bun install --frozen-lockfile --ignore-scripts on the reviewed commit installed 45 packages. The same step passed on the earlier isolated runs.
  • build: passed (exit 0). Isolated bun run build (tsc) passed on the reviewed commit.
  • typecheck: passed (exit 0). Isolated bun run typecheck passed for the source project and tsconfig.test.json.
  • unit-tests: passed (exit 0). Isolated bun test on the reviewed commit, both the rerun and the --live suite: 3302 pass, 0 fail, exit 0 (18665 expects, 126 files on the --live suite). Main on this machine was 3298 pass, 0 fail, exit 0. The description's 3297 equals this Linux count minus five tests skipped off Linux.
  • isolated-suite-first: failed (exit 1). The first isolated bun test on the reviewed commit was 3301 pass, 1 fail, exit 1. The failure was test/model-selection-run.test.ts, the unchanged scenario for 1m with fast=true and max=false: expected max_mode false and received true. That file is outside the diff.
    Failure cause: environment. test/model-selection-run.test.ts is outside the diff. A second isolated run and the --live suite of this commit both passed 3302 tests with 0 failures, and main passed 3298 with 0 failures. The assertion matches a parallel clash on the process-wide model cache.
  • live: passed (exit 0). Harness live test on the reviewed commit: OpenCode 2.2.6, plugin-opencode2.js 0.8.6, PLUGIN_INIT_COUNT=1, LIVE_TEST=passed. The assistant reply was pong, model cursor/composer-2.5, opencode_exit=0.
  • live-ab: passed (exit 0). @nkoynov, thank you for the repro. This is the live confirmation you asked for. The driver was your repro-162.py steer-await mode from the gist linked on #162, unchanged, sha256 ab28087c4900dff72d478a31615e1a3655ca92bb55f6a98742b4266cddd11048, with the test-only cut from bb73b91 applied in throwaway worktrees and not committed. OpenCode 2.2.6 loaded plugin-opencode2.js 0.8.6 with PLUGIN_INIT_COUNT=1. Main e1066a9 refused twice. First: recovery: history-check reason=missing-host-results execs=83 results=2 pending=0; recovery: suppressed; automatic retry unsafe (attempt 1/14). Second: the same check with execs=84 results=2, then suppressed. This commit rebuilt twice. First: recovery: history-check reason=completed-tool-results execs=2 terminalReads=71 results=2 pending=0; recovery: reopened kind=rebase reason=completed-tool-results. Second: the same check with terminalReads=61, then kind=rebase reason=completed-tool-results. On each of those four runs the steer was sent once (1 trailing tool result before 1 early steer) and was not injected again after the cut. The two rebuilds kept a single S2 in counter.txt and the reply included HERON-58. AwaitShell polls were in the log: variant=await_tool_call and runtime metadata read_args, 81 and 82 reads on the two refusals and 71 and 61 on the two rebuilds. Other runs resumed from an unrevoked checkpoint because of the 30 ms cut race and did not reach this path. Those PR checks still logged terminalReads of 79, 82, 82, 69, 71, 79, and 72.

Limitations:

  • An MCP read of a terminal record (mcp_args) is answered and is not marked terminalRead, so it still counts as a missing host result. That non-exemption matches the description and is a note, not a finding. There is no wire evidence that AwaitShell uses that path.
  • The 30 ms test cut sometimes lands while a checkpoint is still valid. Those runs resume and do not exercise the history rebuild. The two refusals and two rebuilds above are the runs that reached it.
  • The harness pong loaded the isolation archive of this commit. The A/B loaded the same two commits from throwaway worktrees inside the isolation boundary. CURSOR_API_KEY stayed inside that boundary.

Runtime: linux.
Reviewed #164 at f58ba60.

@nkoynov

nkoynov commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

I ran it on f58ba60 with the cut patch on top, plain opencode serve 2.2.5, real account. repro-162.py steer-await rebuilt from history all 3 times (kind=rebase reason=completed-tool-results execs=2, with terminalReads=48, 39 and 46), where e1066a9 had refused it with missing-host-results execs=60. Each time the steer was injected once, its echo matched, nothing was injected again after the cut, the counter command ran once, and the reply had the steer's codeword. note and the promoted steer case also still rebuild, with terminalReads=0.

On mcp_args: our host logs from Nov 2 to 8 have 798 AwaitShell calls and about 773,000 terminal or spill reads the provider answered itself, all native read_args, plus 3 MCP read calls, none on a terminal record or near an AwaitShell. The VM run had none. An MCP read would go to the host and get a host result anyway.

Small thing: in src/runtime-files.ts the new function went in between runtimeExecMessages and its doc comment, so "Native internal filesystem requests…" now sits above isTerminalRecordRead.

@oakimov

oakimov commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner Author

@nkoynov Fixed in f075826. Moved the “Native internal filesystem requests…” comment directly above runtimeExecMessages, keeping isTerminalRecordRead’s own comment directly above it. The diff only moves that one line in src/runtime-files.ts.

@oakimov
oakimov merged commit e580aab into main Nov 8, 2026
10 checks passed
@oakimov
oakimov deleted the fix/history-rebuild-await-polls-159 branch November 9, 2026 18:15
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