Repository navigation
shared-node-store: fix resyncNodeFromServer's direct-call hasPending hardcode - #2076
Conversation
…isQueued signal resyncNodeFromServer(nodeId, _isQueuedFollowUp)'s DIRECT call (invoked from inside a failing write's own OCC-conflict catch handler) hardcoded hasPending: false, so it had no way to detect a second, genuinely different write collapsed into PersistenceCoordinator's queuedOperations while the first write was still executing. When that first write failed with an OCC conflict whose response didn't embed current_node (falling back to resyncNodeFromServer) and the node wasn't focused, the resync applied a stale, pre-second-write server snapshot, silently discarding the second write's still-pending optimistic value. Empirically confirmed (via instrumented test runs, reverted before this commit) that a naive swap to a live PersistenceCoordinator.hasPending() read at the decision point does not fix this correctly: both OCC handlers call clearQueued(nodeId) unconditionally, immediately before either hydration branch runs, to stop the failing write's own queued retry from firing against its now-stale version. That unconditionally removes any second write from queuedOperations (and its pendingOperations placeholder) before resyncNodeFromServer's decision point is ever reached. At that point hasPending() reads true anyway, but only because the failing write's own executingOperations entry hasn't cleared yet (runOperation's finally hasn't run) -- self-referential, and confirmed true even in an isolated failure with nothing else queued at all, which is exactly the false-positive #2066's review caught when this was first attempted. Add SimplePersistenceCoordinator.isQueued(nodeId), true only when a genuinely different write is collapsed behind this node's currently-executing one -- never true from an operation's own bookkeeping, unlike isPending/isExecuting/hasPending. Both OCC handlers (updateNode and updateTaskNode) now capture this BEFORE calling clearQueued(), and pass it into resyncNodeFromServer as a new directCallHadQueuedWrite parameter, used instead of the hardcoded false for the direct call. The queued-follow-up call is unchanged -- it continues to use a live hasPending() read, safe there because it fires strictly after the triggering write's own bookkeeping has settled. Adds a regression test proving the actual race: write A executing, write B submitted and collapsed into queuedOperations while A executes, A fails OCC with no embedded current_node, node not focused -- asserts B's optimistic content survives. Verified this test fails against the pre-fix code (B's content gets overwritten by the stale server snapshot) and passes after the fix. A second test locks in the #2066 round-1 finding: an isolated OCC failure with nothing else queued still resyncs normally (no false-positive skip). Full existing OCC/resync/ serial-writer test suites re-verified green. Closes #2069 Co-authored-by: Claude <[email protected]>
… too Adversarial review of this PR found the fix was incomplete: updateNode()'s OCC-conflict direct-hydration branch (the one that applies occError.conflictData.current_node straight into the store when the daemon embeds it, guarded since #2068) still hardcoded hasPending: false in its decideRemoteUpdate call, even though hadQueuedWrite is computed earlier in the very same catch handler for the sibling fallback branch this PR already fixed. Whenever the daemon's OCC response embeds current_node (rather than omitting it, which routes into the already-fixed resyncNodeFromServer fallback) and the node isn't focused, a genuinely queued second write was still silently clobbered -- the same #2069 bug, reached through the sibling branch instead. Pass hadQueuedWrite through to this branch's hasPending instead of the hardcoded false. Verified empirically: reverted just this one line back to hasPending: false and confirmed the new regression test below fails against it (clobbers a queued write's content), then restored it and confirmed the test passes. updateTaskNode()'s equivalent direct-hydration branch has no decideRemoteUpdate guard at all (a larger, pre-existing gap tracked separately in #2072) -- out of scope here, since wiring hasPending into a branch that has no guard mechanism to receive it isn't a one-line fix. Adds a regression test to shared-node-store-skip-while-editing.test.ts's existing OCC direct-hydration guard block, mirroring the fallback-path test: write A executing, write B collapsed into queuedOperations, A's OCC conflict embeds current_node (forcing the direct-hydration branch, not the fallback), node unfocused -- asserts B's optimistic content survives. Co-authored-by: Claude <[email protected]>
|
Addressed a real finding from adversarial review: the fix was initially incomplete. The review found that `updateNode()`'s OCC-conflict DIRECT-HYDRATION branch (the one guarded since #2068, which applies `occError.conflictData.current_node` straight into the store when the daemon embeds it) still hardcoded `hasPending: false`, even though `hadQueuedWrite` is computed earlier in the exact same catch handler for the sibling fallback branch this PR fixes. Whenever the daemon's OCC response embeds `current_node` (rather than omitting it) and the node isn't focused, a genuinely queued second write was still silently clobbered — same bug, sibling branch. Fixed in the follow-up commit `9ee05257`: wired `hadQueuedWrite` into that branch's `hasPending` too. Verified empirically the same way as the original fix — reverted just that one line back to `hasPending: false`, confirmed the new regression test fails (clobbers B's content), restored it, confirmed it passes. Added a matching regression test to the existing OCC direct-hydration guard block in `shared-node-store-skip-while-editing.test.ts`. `updateTaskNode()`'s equivalent direct-hydration branch has no guard at all (larger, pre-existing gap, tracked separately in #2072) — correctly left out of scope, since wiring a value into a branch with no guard mechanism isn't a one-line fix. Full suite re-verified green (204 files, 4613 tests), quality:fix clean, pre-push gate passed in full on the follow-up push. |
|
APPROVE Independent final verification (GitHub blocks formal self-review approval of one's own PR, so recording this as a comment):
Merging. |
Summary
resyncNodeFromServer(nodeId, _isQueuedFollowUp)is invoked two ways: directly from a failing write's own OCC-conflict catch handler, and as a queued follow-up when a second resync request arrives while one is already in flight. Only the queued-follow-up call readPersistenceCoordinator.hasPending(nodeId)for real; the direct call hardcodedhasPending: false.Concrete failure sequence:
PersistenceCoordinator's single-slotqueuedOperationsmap. B's optimistic value is already applied to the local store.current_node—updateNode()'s OCC handling falls back toresyncNodeFromServer(nodeId), the direct call.hasPendinghardcodedfalse, the resync applies the fetched (stale, pre-B) server row, silently discarding B's still-pending optimistic value.Why a naive swap to a live
hasPending()read doesn't workEmpirically confirmed (instrumented the actual code and ran it against both scenarios before writing the fix — details in the commit message): at the resync's decision point,
executingOperationsstill has an entry for the failing write itself (its ownrunOperationhasn't reachedfinallyyet), so a livehasPending()read istruethere almost every time — including in an isolated failure with nothing else queued at all. That's the exact false-positive #2066's review caught when this was first tried (broke 3 existing tests).Worse: both OCC handlers (
updateNodeandupdateTaskNode) callPersistenceCoordinator.clearQueued(nodeId)unconditionally, immediately before either hydration branch runs — to stop the failing write's own queued retry from firing against its now-stale version. That call removes any second write fromqueuedOperations(and itspendingOperationsplaceholder) beforeresyncNodeFromServer's decision point is ever reached, so even a fix that correctly checksqueuedOperationsspecifically (rather than the coarserhasPending()) would find nothing there unless it captures that state beforeclearQueued()erases it.Fix
SimplePersistenceCoordinator.isQueued(nodeId): true only when a genuinely different write is collapsed behind this node's currently-executing one — never true from an operation's own bookkeeping (a write is never routed through theisExecuting-queueing branch for itself).PersistenceCoordinator.isQueued(nodeId)before callingclearQueued(), and pass it intoresyncNodeFromServeras a newdirectCallHadQueuedWriteparameter — used instead of the hardcodedfalsefor the direct call. The queued-follow-up call is unchanged (still a livehasPending()read, safe there since it fires strictly after the triggering write's own bookkeeping has settled).Testing
New file
resync-direct-call-queued-write-regression.test.ts:queuedOperations, A fails OCC with no embeddedcurrent_node, node unfocused) and asserts B's optimistic content survives. Verified this test fails against the pre-fix code (temporarily stashed the source fix and re-ran it — B's content gets overwritten by the stale server snapshot) and passes after the fix.Re-ran the full existing OCC/resync/serial-writer suites most likely to be sensitive to this change (
shared-node-store-skip-while-editing,ai-chat-occ-conflict-regression,persistence-serial-writer-regression,shared-node-store-extended,shared-node-store,persistence-coordinator-supersede-settlement) — all green (244 tests). Full suite:bun run test— 204 files, 4612 tests passed.bun run quality:fixclean. Pre-push gate (test:all+cargo build --bin nodespaced+test:e2e+cargo test -p nodespace-app) passed in full (one transient timeout on an unrelated plugin-resolution test under coverage instrumentation during a loaded run — reproduced as passing standalone in 2.1s, confirmed as resource-contention flakiness on retry, unrelated to this change).Scope note
Both direct call sites of
resyncNodeFromServer(updateNode()andupdateTaskNode()) share the identicalclearQueued-before-resync bug, so both are fixed here — the root-cause fix lives insideresyncNodeFromServeritself and is incomplete without updating both callers consistently.Closes #2069
🤖 Generated with Claude Code