Skip to content

fix: rebuild from history past reads the provider answered itself - #174

Merged
oakimov merged 2 commits into
mainfrom
fix/rebuild-provider-answers-172
Nov 9, 2026
Merged

oakimov merged 2 commits into
mainfrom
fix/rebuild-provider-answers-172

Conversation

@oakimov

@oakimov oakimov commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

Fixes #172.

Problem

When Cursor cuts a Run right after a tool result is written, the only safe recovery is a rebuild from host history. #164 let that rebuild skip AwaitShell's terminal-record polls, but it still needed a host result for every other exec. So any other read the provider answers itself refused the rebuild with missing-host-results.

@nkoynov, in your host logs for Nov 2–9, 25 of 82 Runs rebuilt after a post-result cut had such reads. In 22 of them the only one was the empty read before a new file's edit. The edit's write always went to the host, and its result was in the Run.

Fix

  • recordProviderAnswer (src/replay-safety.ts) records which kind of answer the provider gave for each exec. It replaces recordTerminalRead.
  • historyCoverage decides at history-check time whether host history holds everything the Run did:
    • terminal-record polls and missing-target reads (typed file_not_found / invalid_file) need no result;
    • the empty read before a new file's edit, and a Task attachment read, need a host exec of the same call whose result is in the rebuilt prompt;
    • refusals, runtime spill or catalog reads, list_mcp_resources and every other exec still need their own result.
  • Unchanged: an answer in the cut attempt itself is still a barrier and ends the turn.
  • The recovery: history-check line also prints missingReads=, editHandshakes= and attachmentReads=.

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, post-result drop matrix:
    • missing-read and new-file-edit now rebuild. On main both fail with missing-host-results execs=3 results=2.
    • missing-read-after-result (answer in the cut attempt) and refused-read (no advertised read, so the provider refuses the call) are still refused.
  • test/replay-safety.test.ts: unit tests for historyCoverage:
    • an edit or Task attachment read counts only with its call's host result in history, which may be on a resumed Run;
    • a read with no call id, or whose only owner is another provider answer, does not count;
    • recordProviderAnswer marks only the current Run's exec.
  • bun run typecheck, bun run build and bun test ./test pass (3387 pass, 5 skip, 0 fail).

@nkoynov, thanks for offering the live check. The new-file cut on OpenCode 2.x against this branch should show reason=completed-tool-results ... editHandshakes=1 and kind=rebase, with the file written once.

A Run Cursor cuts right after a tool result can only recover through a
history rebuild, and that rebuild required a host result for every exec
except AwaitShell's terminal-record polls. Any other read the provider
answers itself refused it with missing-host-results. Live, that was mostly
the empty read before a new file's edit.

Record which kind of answer the provider gave for each exec
(recordProviderAnswer) and decide coverage at history-check time
(historyCoverage):
- terminal-record and missing-target reads need no result;
- a new file's empty edit read and a Task attachment read need their
  call's host result in the rebuilt prompt;
- refusals and every other exec still need their own result, and any
  answer in the cut attempt stays a barrier.

Fixes #172.
@greptile-apps

greptile-apps Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

The PR lets post-result recovery rebuild past more reads the provider answered itself.

  • History rebuilds now account for provider-answered reads.

No actionable issues found. Review used source and test inspection; no tests or live host runs were executed.

oakimov explicitly notes that live validation and an end-to-end Task attachment recovery fixture remain uncovered. The PR covers Task attachment ownership with unit tests.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Run drops after host results] --> B{Cut attempt has a barrier?}
  B -->|Yes| C[End the turn]
  B -->|No| D[Check host history]
  D --> E[Require every host exec result]
  E --> F[Require same-call results for edit and Task reads]
  F --> G{History covers the Run?}
  G -->|Yes| H[Rebuild from host history]
  G -->|No| C
Loading

Reviews (1) · Last reviewed commit: "fix: rebuild from history past reads the..." · Reviewed by Greptile

@codecov

codecov Bot commented Nov 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@nkoynov

nkoynov commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Ran it on 90ff0d5 with the test-only cut on top, plain opencode serve 2.2.6, Opus 5.5, real account. Control was main (2d228b4) with the same cut.

  • New file, cut 30 ms after the write's result: all 3 runs reason=completed-tool-results execs=1 editHandshakes=1, then reopened ... kind=rebase, and the turn finished. One write call, file holds its line once. On main: reason=missing-host-results execs=2, turn ended as "automatic retry unsafe".
  • Read of a missing file, then a shell command, cut 30 ms after the shell result: all 3 runs reason=completed-tool-results execs=1 missingReads=1, kind=rebase, turn finished, the command ran once. On main: missing-host-results execs=2.

So it matches what you expected in both cases.

@oakimov

oakimov commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner Author

@nkoynov, thank you! That's the confirmation we needed.

What you raised in #140, and what fixes it:

@oakimov
oakimov merged commit 41dbd81 into main Nov 9, 2026
10 checks passed
@oakimov
oakimov deleted the fix/rebuild-provider-answers-172 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 has execs the provider answered itself

2 participants