Skip to content

fix(transcript): reconcile tools stranded running at the next round's start - #1414

Merged
pascalandr merged 3 commits into
NeuralNomadsAI:devfrom
heunghingwan:fix/stranded-tool-round-reconcile
Nov 9, 2026
Merged

pascalandr merged 3 commits into
NeuralNomadsAI:devfrom
heunghingwan:fix/stranded-tool-round-reconcile

Conversation

@heunghingwan

Copy link
Copy Markdown
Contributor

Why

A tool call whose terminal event (session.tool.success/failed) never reached the UI mid-execution — lost across a disconnect window, or never published by the daemon — stayed on its "running" spinner through every later agent round. Two layers conspired:

  • The SDK reducer applies session.tool.success only to a part whose status is exactly running (and silently no-ops when it does not hold the message), so a lost event leaves the part stuck in running/streaming forever.
  • CodeNomad reconciled such rows only at execution settlement (finishOpenCodeDataEvent → completeSettlementRead). Nothing repaired a finished round's tool while the execution kept looping, and a quickly following execution suppressed that settlement publication too — so the spinner could persist through a whole second execution.

User-visible change

The stuck spinner now clears when the agent loop advances: once the next round begins (session.step.started reduces), a tool still shown as running/pending in an earlier, finished round is reconciled from the authoritative page within one read, instead of waiting for the execution to end.

Approach

  • After a reduced session.step.started — both the direct emit in applyOpenCodeDataEvent and the queued replay in drainTranscriptQueue — scan assistant rounds whose time.completed is stamped for tool parts still streaming/running. The native loop feeds every tool result back before it continues, so such a part has definitively lost its terminal event; reconcile it with the same read settlement uses (message-key invalidate + completeSettlementRead).
  • The replay-site scan runs at the task boundary between replay chunks (queueMicrotask), so a chunk's synchronous replay budget is untouched.
  • New settlementReadPending flag on the transcript entry: all reconciliation callers (settlement, liveness, inbox, step boundary) share one in-flight read. Skipping also avoids the invalidation that would otherwise chain a duplicate fetch under the SDK's single-flight machinery.

Edge cases

  • A tool running inside the active round (normal parallel execution) never triggers a reconcile — only finished rounds are scanned.
  • A resyncing transcript leaves the repair to its authoritative read; a rotating transcript may reconcile on the shared entry (single-flight coalesces).
  • A settlement publication suppressed by a newer execution still heals through later per-event projections of the reconciled reducer, so the repair converges in every ordering.

Validation

  • New tests in packages/ui/src/stores/opencode-data-settlement.test.ts: reconciliation fires at the next round's start without any execution terminal event; a tool running inside its own round triggers no read. Written test-first (failed 0 !== 1 before the fix).
  • Full CI store test group: 316 pass / 0 fail; tsc --noEmit clean. One caught-and-fixed regression along the way: an in-flight settlement read must be shared, not invalidated beneath (2 !== 1), and the replay scan must stay out of the chunk budget.

@heunghingwan
heunghingwan force-pushed the fix/stranded-tool-round-reconcile branch from 5eb7852 to 6c3c162 Compare November 9, 2026 07:12

@pascalandr pascalandr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@heunghingwan I reviewed 5eb78528 (your rebase to 6c3c1624 leaves the three files identical). The inference holds: against the installed @opencode/* 2.2.3 contracts and the native runner, a step only completes after its local tool fibers settle, so a finished round's tool still running has lost its terminal event. Two regressions in the shared pending read block, though; each reproduction passes on the base and fails on this head.

P2: sharing the in-flight read drops a newer settlement's publication

packages/ui/src/stores/opencode-data.ts:987-998

  1. An execution settles with a stranded tool; reconciliation read A starts.
  2. A newer execution starts and settles before A resolves.
  3. Its terminal event triggers the SDK's trailing read B, but settlementReadPending makes completeSettlementRead() return early.
  4. Resolve both reads with the completed tool.

The tool stays running: A's continuation refuses to publish because its captured executionEpoch is older, and B has no publication continuation. Nothing repairs it without further events (running !== completed).

Please share the fetch without discarding the newest settlement's publication demand, e.g. keep a continuation for the current execution (or re-check the epoch and publish for the latest one when the shared read resolves).

P2: the pending flag outlives the reducer that owns it

packages/ui/src/stores/opencode-data.ts:989-993, with replaceTranscriptEntry (:325-329) and invalidateTranscript (:658-669)

  1. Leave a reconciliation read unresolved.
  2. Call invalidateOpenCodeSessionContent(), then complete the replacement reducer's authoritative resync.
  3. Admit another stranded tool and run the step-boundary or liveness reconciliation.

The discarded reducer's flag suppresses the healthy replacement's read (2 reads instead of 3) until the abandoned request settles; disposal does not cancel it.

Please scope the pending read to the reducer (or generation) that started it, release it when the reducer is replaced, and fence the old completion so it cannot clear a replacement's flag.

P3: the replay hook has no regression test

packages/ui/src/stores/opencode-data-settlement.test.ts:119-147

Removing the drainTranscriptQueue hook leaves all 21 settlement/replay tests green. A case that queues the next session.step.started behind a full 200-message window passes on head and fails without the hook. The active-round negative test also survives a "scan active rounds too" mutation (only the mixed replay test catches it): assert the read count right after the in-round tool's step boundary.

Note (not blocking)

There is no no-progress guard: if the authoritative page keeps the tool running, every later step boundary issues another read (four boundaries gave four reads in a probe). It is event-driven and bounded by the window, but worth a comment or a per-part guard.

Validation

UI typecheck passes; 22 transcript/settlement/replay/reconnect/idle/compaction/instance/projection files (307 tests) pass twice; tool-images, streaming-projection, compaction-responsiveness and part-durations pass (16/16).

The red runtime-contracts and WebKit jobs on 5eb78528 were workflow/head skew: the run used dev's workflow against the old head, which lacked scripts/test-session-archive-native.mjs and .github/scripts/tauri-webkit-workflow.test.cjs. Your rebase should clear them.

@heunghingwan

Copy link
Copy Markdown
Contributor Author

Response to the review (both P2 regressions and the P3 coverage gap are addressed in 15a2461; thank you for the reproductions — each one failed on the unpatched head exactly as described).

P2 (shared read drops a newer settlement's publication): the pending read now records settlementReadEpoch, and a demand arriving mid-read upgrades it to the current execution. When the shared fetch resolves, it publishes for the newest recorded demand; only an execution newer than every recorded demand still suppresses publication. Your scenario — read A in flight, newer execution starts and settles, both reads resolve with the completed tool — now heals, covered by a settlement arriving while a shared read is in flight still publishes.

P2 (pending flag outlives the reducer): the pending read is now scoped to the DataEntry that started it. replaceTranscriptEntry releases the flag with the old entry, a stale flag no longer suppresses the replacement's demand, and the abandoned completion is fenced by owner identity so it can neither clear a successor's flag nor publish against it. Covered by a replaced reducer abandons its pending reconciliation read (3 reads, second tool heals).

P3 (replay hook coverage): added both cases to opencode-transcript-replay.test.ts. a stranded tool behind a full window reconciles through the queued replay fails when the drainTranscriptQueue hook is removed (the rotation's own snapshot read never reaches message.list, so the assertion isolates the hook's read), and an active round's running tool does not reconcile at the replay boundary fails when the scan drops the finished-round check — verified by applying both mutations locally and watching each test go red.

Note: recorded as a comment on reconcileStrandedRoundTools — re-reading is event-driven and bounded per step boundary, with the daemon's own state as the progress guard.

Validation: the two new settlement cases and two replay cases are red on 6c3c162 and green on 15a2461; the full CI store group passes 324/324 and tsc --noEmit is clean.

… start

A tool whose terminal event never reached the UI mid-execution (lost
across a disconnect window, or never published by the daemon) kept its
"running" spinner through every later agent round: the SDK reducer
drops session.tool.success unless the part is exactly running, and
CodeNomad only reconciled at execution settlement — which a quickly
following execution could also suppress, leaving the spinner stuck
through that whole execution as well.

When a session.step.started reduces (both the direct emit and the
queued replay; the replay scan runs at the task boundary between
chunks so a chunk's synchronous budget stays untouched), scan finished
rounds — assistant rows whose time.completed is stamped — for tool
parts still streaming or running. The loop feeds every tool result
back before it continues, so such a part definitively lost its
terminal event: reconcile it immediately with the same read the
settlement uses (message key invalidate + completeSettlementRead).
The active round's parallel tools are excluded, and a resyncing
transcript leaves the repair to its authoritative read.

A new settlementReadPending flag makes every reconciliation caller
share one in-flight read instead of chaining a duplicate fetch: the
SDK's invalidate marks an in-flight read to re-run, so skipping also
avoids the invalidation that would chain it.

A settlement publication suppressed by a newer execution still heals
through later per-event projections of the reconciled reducer, so the
repair converges in every ordering.

Validated with two new cases in opencode-data-settlement.test.ts
(reconcile at the next round's start; no reconcile for a tool running
inside its own round), the full store test group (316 passing), and
tsc.
…nd epoch

Two regressions in the shared pending-read block, both from the PR review:

Sharing an in-flight settlement read dropped a newer settlement's
publication: a read A started by execution N's settlement, then a newer
execution started and settled before A resolved — its terminal event's
demand hit settlementReadPending and returned, and A's continuation
refused to publish because its captured epoch was older, leaving the
stranded tool running with nothing to repair it. The shared read now
upgrades settlementReadEpoch to the demand's execution and publishes for
the newest one when it resolves; only an execution newer than every
recorded demand still suppresses publication.

The pending flag also outlived the reducer that owned it: a read left
unresolved against a replaced entry (pruning invalidation, authoritative
resync) kept suppressing the healthy replacement's reconciliation until
the abandoned request settled. The pending read is now scoped to the
DataEntry that started it — replaceTranscriptEntry releases it, a stale
flag no longer blocks a new read, and an abandoned completion is fenced
by owner identity so it can neither clear a successor's flag nor publish
against it.

The replay hook also gets its regression coverage: a stranded tool
queued behind a full 200-message window reconciles through the drained
step.started (fails when the drain hook is removed), and an active
round's running tool does not reconcile at the replay's task boundary
(fails when the scan drops the finished-round check). A comment records
the bounded no-progress behavior of re-reading at later boundaries.

Validated with the new settlement cases (shared-read publication,
replaced-reducer abandonment; red on the unpatched head) and the full CI
store group (324 passing) plus tsc.
@heunghingwan
heunghingwan force-pushed the fix/stranded-tool-round-reconcile branch from 15a2461 to d4decac Compare November 9, 2026 09:05
@heunghingwan

Copy link
Copy Markdown
Contributor Author

Rebased onto dev (9a7114c3) in d4decac — both commits unchanged. The failing automation-forms shards need #1407's fixture synchronization, which sat one commit past the old base; dev's own run on 9a7114c is green on that suite.

@pascalandr pascalandr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@heunghingwan I re-reviewed d4decacf. The reducer-lifetime P2 is fixed, and both new replay tests kill their intended mutations. The epoch upgrade fixes the case where read A already returns completed data, but one version of the shared-read P2 remains.

P2: publication follows read A rather than the later fresh read B

packages/ui/src/stores/opencode-data.ts:1000-1002

  1. Reconciliation read A is in flight.
  2. A genuinely later execution settlement invalidates the SDK message key and queues read B.
  3. Resolve A with the tool still running.
  4. Wait until B actually dispatches, then resolve B with the tool completed, without emitting another event.

The reducer contains the completed tool, but the displayed tool stays running. Upgrading settlementReadEpoch preserves the publication demand, but its continuation still awaits only A: it publishes before B, and B has no publication continuation.

This reproduces for the newer-execution scenario and for a same-execution case where A snapshots a currently running second tool that subsequently settles. The latter distinguishes genuinely dirty demand from duplicate synchronous invalidations.

The installed SDK's contract-fh77hmr5.js:91-99,113-119 marks a started read dirty on invalidation; the next sync() creates a trailing promise. Callers sharing that trailing promise do not require another fetch. Please follow the current reconciliation promise when fresh demand arrives, binding publication and retirement to that promise's ownership rather than only upgrading A's epoch.

Strengthen the shared-read regression

packages/ui/src/stores/opencode-data-settlement.test.ts:165-173

The current for (const read of f.reads) runs before a queued B can dispatch. Instrumentation observed only one dispatched read before cleanup. Since A already returns completed data, its publication can retire the reducer without exercising B.

Please resolve A with old running data, await B's dispatch, then resolve B separately and assert the visible tool becomes completed without another event.

Confirmed fixed

  • Replacing the reducer releases the old read's ownership. Both obsolete success and rejection leave a successor's pending read intact; independently reversing ownership admission/reset reproduces the original failure.
  • Removing the replay hook now fails the queued positive test. Including active rounds fails the new active-round negative test. The positive genuinely exercises queued replay; the rotation snapshot itself does not issue the counted HTTP read.
  • Read failure/abort release ownership for retry. Existing rotation/resync/reconnect units remain green.

The step-boundary early return does not upgrade the publication epoch, so the comment claiming that it does should also be aligned with the implementation. I found no separate correctness blocker there.

Validation

UI typecheck passes; the same 22-file unit selection passes 311/311 twice; tool-images, streaming-projection, compaction-responsiveness and part-durations pass 16/16. The two stale-A/later-B probes fail on this head; six additional lifetime/failure probes pass.

The rebase resolves the prior missing-script mismatch: the completed runtime-minimum job now explicitly passes archive/restore. No failures were observed in the new run when inspected, though several jobs were still running. Merge with current dev is clean.

…ublication

A demand arriving while a reconciliation read A was in flight upgraded the
recorded epoch but left publication bound to A's own continuation. When the
newer settlement had invalidated the SDK message key, the SDK chained a
trailing read B after A; A then resolved with stale data, published it and
released the pending flag, and B — carrying the completed truth — merged
into the reducer with no publication continuation of its own. The display
kept the running spinner with no further event to repair it.

completeSettlementRead now follows the SDK's current message.sync promise
each time demand arrives: a shared in-flight fetch returns the same promise
(no extra fetch, no re-follow), while a settlement that invalidated the key
returns the trailing promise, and only the followed promise's owner
publishes, releases the pending flag and runs retirement. The step-boundary
reconcile still declines to re-enter while the current reducer's read is in
flight (re-invalidating beneath it would chain a duplicate fetch), and the
epoch comment no longer claims that path upgrades the demand.

The shared-read regression now reproduces the reviewer's sequence: A
resolves with the tool still running, the trailing B dispatches only after
A settles, and B's resolution must heal the visible tool without another
event — red on the previous head.

Validated with the full CI store group (324 passing) and tsc.
@heunghingwan

Copy link
Copy Markdown
Contributor Author

Response to the second review round (0869922).

P2 (publication follows read A rather than the later fresh read B): completeSettlementRead now follows the SDK's current message.sync promise every time demand arrives. A shared in-flight fetch returns the same promise object, so re-demands neither re-follow nor fetch again; a settlement that invalidated the message key returns the trailing promise, and only the followed promise's owner clears the pending state, publishes, and runs retirement — A's stale continuation is skipped by promise identity the same way replaced-entry completions are skipped by owner identity. Both your probes' scenarios (newer-execution settlement, and the same-execution second tool settling) reduce to the same mechanism.

Strengthened regression: the shared-read test now resolves A with the tool still running, polls until the trailing B actually dispatches (its fetch only starts after A settles), resolves B with the completed page, and asserts the visible tool heals with no further event — it is red on d4decac exactly as your instrumentation described.

Comment aligned: settlementReadEpoch's comment no longer claims the step-boundary early return upgrades the demand; that path still declines to re-enter while the current reducer's read is in flight, since re-invalidating beneath it would chain the duplicate fetch.

Validation: full CI store group 324/324, tsc --noEmit clean. The rebase onto 9a7114c3 is retained; both prior commits are otherwise unchanged.

@pascalandr pascalandr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@heunghingwan I re-reviewed 08699226: the remaining shared-read P2 is resolved. I found no remaining P0-P3 findings in this review.

  • Both stale-A/completed-B reproductions now converge without another event, for a newer execution and a same-execution second tool. Independently reversing promise-following makes both fail on the visible tool status.
  • The authored regression now actually waits for B to dispatch and resolves it separately. It also fails on visible status when the correction is reversed.
  • The installed SDK returns the same promise for shared demand and a distinct promise for a trailing read. Following that identity preserves A/B/C supersession; removing the promise ownership fence causes obsolete A to clear B's ownership.
  • Reducer replacement, obsolete success/rejection, retry after failure/abort, and the newer-execution publication fence remain correct. Queued replay and active-round tests still kill their intended mutations. A burst of 128 terminal events and 48 step boundaries produced three message reads.

Validation: UI typecheck passes; the focused 22-file unit selection passes 311/311 twice; tool-images, streaming-projection, compaction-responsiveness and part-durations pass 16/16. Nine independent reconciliation/ownership probes pass. Merge with current dev is clean.

This approval covers the reviewed code and local validation. The current CI run was still queued at authorization when inspected; its results remain pending. I have not merged the PR.

@pascalandr
pascalandr merged commit 9f00cff into NeuralNomadsAI:dev Nov 9, 2026
21 checks passed
@pascalandr

Copy link
Copy Markdown
Contributor

Thanks @heunghingwan !

@heunghingwan
heunghingwan deleted the fix/stranded-tool-round-reconcile branch November 9, 2026 15:35
heunghingwan added a commit to heunghingwan/CodeNomad that referenced this pull request Nov 9, 2026
Rebase onto NeuralNomadsAI#1414 (9f00cff) surfaced an ordering hazard in the merged
completeSettlementRead: the SDK read promise settles one microtask hop
before the publishing chain (which passes through .catch), so a caller
holding obligations on the returned promise — the delivery repair's
counter decrement and retirement retry — ran its retirement before the
chain published, destroying the reducer and fencing out the repair's
publication.

completeSettlementRead now records and returns the publishing chain
itself; the shared in-flight path returns the recorded chain, and
replaceTranscriptEntry releases it with the other read state.

Validation: 259 focused store tests on the rebased tree, UI typecheck,
the delivery browser file and the fixture-wiring gate.
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.

2 participants