Skip to content

shared-node-store: fix resyncNodeFromServer's direct-call hasPending hardcode - #2076

Merged
mstomar125 merged 2 commits into
mainfrom
issue-2069-hasPending-direct-call
Aug 14, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-2069-hasPending-direct-call

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

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 read PersistenceCoordinator.hasPending(nodeId) for real; the direct call hardcoded hasPending: false.

Concrete failure sequence:

  1. Write A is executing for node X.
  2. Write B, for the same node, is submitted while A is executing — it collapses into PersistenceCoordinator's single-slot queuedOperations map. B's optimistic value is already applied to the local store.
  3. Write A fails with an OCC conflict whose response does not embed current_node — updateNode()'s OCC handling falls back to resyncNodeFromServer(nodeId), the direct call.
  4. With the node not focused and hasPending hardcoded false, 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 work

Empirically 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, executingOperations still has an entry for the failing write itself (its own runOperation hasn't reached finally yet), so a live hasPending() read is true there 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 (updateNode and updateTaskNode) call PersistenceCoordinator.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 from queuedOperations (and its pendingOperations placeholder) before resyncNodeFromServer's decision point is ever reached, so even a fix that correctly checks queuedOperations specifically (rather than the coarser hasPending()) would find nothing there unless it captures that state before clearQueued() erases it.

Fix

  • Added 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 the isExecuting-queueing branch for itself).
  • Both OCC handlers now capture PersistenceCoordinator.isQueued(nodeId) 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 (still a live hasPending() 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:

  • Reproduces the exact failure sequence (write A executing, write B collapsed into queuedOperations, A fails OCC with no embedded current_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.
  • A second test locks in the Kanban: cap column rendering and add browser drag-and-drop tests #2066 round-1 finding: an isolated OCC failure with nothing else queued still resyncs normally (no false-positive skip).

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:fix clean. 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() and updateTaskNode()) share the identical clearQueued-before-resync bug, so both are fixed here — the root-cause fix lives inside resyncNodeFromServer itself and is incomplete without updating both callers consistently.

Closes #2069

🤖 Generated with Claude Code

…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]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

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.

@mstomar125

Copy link
Copy Markdown
Contributor Author

APPROVE

Independent final verification (GitHub blocks formal self-review approval of one's own PR, so recording this as a comment):

  • Diff scoped correctly to 3 files (verified against actual merge-base 99030cd, not a stale comparison): shared-node-store.svelte.ts, the new resync-direct-call-queued-write-regression.test.ts, and the extended shared-node-store-skip-while-editing.test.ts. No stray unrelated changes (the pre-existing cargo-fmt drift in nodes.rs was caught and reverted before each commit, as with shared-node-store: apply skip-while-editing guard to OCC direct-hydration path #2071).
  • grep confirms zero remaining hasPending: false hardcodes in the file.
  • Full suite green: 204 files, 4613 tests. quality:fix clean (eslint, tsc, cargo fmt, cargo clippy --all-targets). Pre-push gate passed in full on both pushes (test:all, cargo build --bin nodespaced, test:e2e, cargo test -p nodespace-app).
  • Two rounds of adversarial sub-agent review: round 1 found a real, legitimate gap (direct-hydration branch not wired to hadQueuedWrite); fixed in 9ee0525 and empirically verified (reverted the line, confirmed the new test fails; restored it, confirmed it passes). Round 2 (focused re-review of the follow-up commit) confirmed the fix is correct and complete, confirmed the regression test is non-vacuous by independently reproducing the revert-and-fail/restore-and-pass check, confirmed no other unaddressed instances of the bug class remain, and confirmed the one full-suite test failure seen along the way (a plugin-resolution test timeout under coverage instrumentation) is pre-existing environmental flakiness, reproduced as passing against clean main.

Merging.

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.

resyncNodeFromServer's direct call hardcodes hasPending: false, dropping a genuinely-queued second write

1 participant