Repository navigation
fix(desktop): re-sync a run the GUI settled while the Agent still owned it - #1097
Merged
Merged
Conversation
…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.
…run-ledger-resync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_activeand the observer's settle are compare-and-sets from a non-terminal state, so once the row is terminal the Agent's owncompletedmarker 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 acompletedrow 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:activeRunis that run, orqueuedRunsholds itrunning(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 markercompletedcompleted, dropping the failure copy, with the Agent's owndurationMsanchored on the row's start (a zero duration keeps the end time the row already had)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-updatedpush (the sidebar) plusremote-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 viaactiveRun(even parked,isStreamingfalse), alive viaqueuedRuns, terminalcompletedwith its duration, terminalerror, and no-marker/different-run → consistent.agent_bridge::tests::bridge_tests: the pass revives afailedrow the Agent still owns; settles afailedrow the Agent completed (assertingended_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.