Skip to content

fix(desktop): re-sync a run the GUI settled while the Agent still owned it - #1097

Merged
huichen merged 2 commits into
mainfrom
fix/desktop-terminal-run-ledger-resync
Oct 15, 2026
Merged

huichen merged 2 commits into
mainfrom
fix/desktop-terminal-run-ledger-resync

Conversation

@huichen

@huichen huichen commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator

Why

Nothing drives a terminal run, and the durable writers cannot correct one.

The send pipeline settles its own row and never reads the Agent again, so a row written while the Agent was still working on that run stays wrong forever. The backend cannot fix it either: update_run_status_if_active and the observer's settle are compare-and-sets from a non-terminal state, so once the row is terminal the Agent's own completed marker never reaches it — and the next reload binds the real assistant entry to that stale row (applyRunMetadata, keyed on the canonical run id).

That is the shape of the bug the frontend fix (#1093) addressed for new rows: an accepted-but-queued prompt returns its invoke response at the ack, so the row was marked failed ([RESPONSE_UNCONFIRMED] response did not complete) while the Agent had that very run queued behind another one and answered it ~20 minutes later. #1093 stops new rows being written that way; the rows already written can only be repaired from the outside.

What

The runtime watchdog gains a second pass next to the non-terminal one: re-read the Agent ledger for the rows the GUI settled without the Agent's evidence, and repair the ones the ledger contradicts.

Candidates are exactly two terminal shapes, one SQL fragment shared by the query and both writers:

  • failed — the send pipeline's failure settle;
  • cancelled + error_type='interrupted' — the startup convergence guess.

A user abort (error_type='abort_requested') and a completed row are never candidates: the first is the user's decision, the second already has the Agent's own terminal marker behind it.

For each candidate within a recent window (7 days), one get_run_state:

Ledger says Action
activeRun is that run, or queuedRuns holds it CAS the row back to running (clearing the failure markers), ensure the session observer — the existing re-attach path resumes and the active-run watchdog settles it later from the Agent's own terminal marker
a durable terminal completed settle completed, dropping the failure copy, with the Agent's own durationMs anchored on the row's start (a zero duration keeps the end time the row already had)
anything else (failed/error/incomplete/cancelled/unknown, or no marker at all) leave the row alone

A row confirmed consistent is remembered in an in-process set, so a candidate costs at most one ledger read per launch; a non-answer (the Agent cannot resolve the session) is deliberately not remembered, so a later tick retries. On the write side the pass emits the standard thread-runtime-updated push (the sidebar) plus remote-activity (an open view re-reads that thread's run) — the same signal used when another client drives the session, which is exactly the situation.

No schema change and no wire-contract change: the two new writers are compare-and-set over the shared candidate predicate, and the caller owns the UI notifications (desktop/CLAUDE.md #11).

Tests

  • store::cleanup: the candidate query is shape-scoped and windowed, resolves the Agent session id, and both writers are guarded CAS (reviving twice is a no-op; an aborted and a completed row are untouched; an unknown Agent state is a no-op; the ledger's end time is applied).
  • agent_bridge::tests::watchdog_tests: the pure ledger plan — alive via activeRun (even parked, isStreaming false), alive via queuedRuns, terminal completed with its duration, terminal error, and no-marker/different-run → consistent.
  • agent_bridge::tests::bridge_tests: the pass revives a failed row the Agent still owns; settles a failed row the Agent completed (asserting ended_at = started_at + durationMs); leaves an agreed failure and stops re-reading it; retries a row the Agent could not be asked about; and never asks about a row the user settled.

Each repair test was falsified first: with the pass disabled the revive/settle/retry tests fail (left: "failed" right: "completed"), and with the candidate predicate widened to all terminal shapes the scope test and the store listing test fail. Verified in the worktree: cargo fmt --check, cargo clippy --all-targets -D warnings (gui and --no-default-features --features headless), cargo test --lib (1838 passed) and the headless store/bridge suites.

…ed it

Nothing drives a terminal run. The send pipeline settles its own row and
never reads the Agent again, so a row it wrote while the Agent was still
working on that run stays wrong forever — and the durable writers cannot
correct it either: `update_run_status_if_active` and the observer's settle
are compare-and-sets **from a non-terminal state**, so once the row is
terminal the Agent's own `completed` marker never reaches it. The next
reload then binds the real assistant entry to that stale row.

That is the shape of the bug fixed on the frontend in #1093: an
accepted-but-queued prompt returns its invoke response at the ack, so the
row was marked `failed` ("[RESPONSE_UNCONFIRMED] response did not
complete") while the Agent queued that very run and answered it twenty
minutes later. #1093 stops new rows from being written that way; the rows
already written are only repair-able from the outside.

The runtime watchdog now does that. Alongside the non-terminal pass, it
re-reads the Agent ledger for the rows the GUI settled **without** the
Agent's evidence — `failed`, or the startup convergence's
`cancelled`+`interrupted` guess — within a recent window, and then:

- the ledger still owns the run (executing it, or holding it in
  `queuedRuns`): CAS the row back to `running` and ensure the session
  observer, so the existing re-attach path resumes and the active-run
  watchdog settles it later from the Agent's own terminal marker;
- the ledger completed it: settle `completed`, clearing the failure copy,
  with the Agent's own duration anchored on the row's start time;
- anything else: leave the row. A row is never asserted `completed`
  without the Agent's positive evidence, and this pass never converts one
  failure flavour into another — nor into a user stop the user never asked
  for. A user abort (`error_type='abort_requested'`) and a `completed` row
  are not even candidates.

A row confirmed consistent is remembered for the life of the process, so a
candidate costs at most one ledger read per launch; a non-answer (the
Agent could not resolve the session) is deliberately not remembered, so it
is retried. Nothing about the schema or the wire contract changes: the two
new writers are compare-and-set over the same predicate the candidate
query uses, and the caller owns the UI notifications.
@huichen
huichen enabled auto-merge (squash) October 15, 2026 10:31
@huichen
huichen merged commit 6761ac6 into main Oct 15, 2026
13 checks passed
@huichen
huichen deleted the fix/desktop-terminal-run-ledger-resync branch October 15, 2026 10:52
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.

1 participant