Skip to content

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

Closed

Description

@mstomar125

Overview

resyncNodeFromServer's direct (non-queued) call path hardcodes hasPending: false instead of consulting PersistenceCoordinator, which lets it silently overwrite a second write that is genuinely still queued for the same node — discarding that write's optimistic value even though it may go on to persist successfully moments later.

This is a follow-up from the #2066 (#1985) review — a distinct finding from #2068 (a sibling gap in the other branch of the same OCC-conflict handling).

Problem Statement

resyncNodeFromServer(nodeId, _isQueuedFollowUp) in shared-node-store.svelte.ts is invoked two ways: directly, from inside 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 reads PersistenceCoordinator.getInstance().hasPending(nodeId) for real; the direct call hardcodes hasPending: false.

Concrete failure sequence:

  1. Write A is executing for node X (any PersistenceCoordinator.persist() caller — a Kanban drag, a debounced content edit, etc.).
  2. Write B, for the same node, is submitted while A is executing. PersistenceCoordinator.persist()'s isExecuting branch collapses B into its single-slot queuedOperations map — B's optimistic value is already applied to the local store at this point.
  3. Write A fails with an OCC conflict whose response does not embed current_node. updateNode()'s OCC-conflict handling falls back to resyncNodeFromServer(nodeId) — the direct call, not the queued-follow-up one.
  4. If node X is not focused, decideRemoteUpdate is evaluated with isFocused: false, hasPending: false (hardcoded) — decision.apply is true, and the resync overwrites the store with the fetched server row.
  5. That fetched row reflects state from before B was ever attempted (the server has no knowledge of B — it hasn't been sent yet). B's still-pending optimistic value is silently discarded.

This is the same class of "two rapid writes to the same card must not stomp each other" bug that chainOrigin was added to #2066's Kanban moveCard to prevent, but the OCC-conflict path routes through resyncNodeFromServer directly, bypassing chainOrigin/onPersistError entirely, and has no equivalent protection or test coverage.

This was flagged during #2066's review, with the reviewer's own recommendation to fix it before merge, but #2066 merged before it was addressed — noted there as real but broader application-wide debt worth its own follow-up rather than blocking that PR further.

Why a direct swap to real hasPending is not safe

The direct call was hardcoded to false in the first place for a specific, empirically-confirmed reason (from #2066's review history): hasPending() ORs pendingOperations/executingOperations/queuedOperations together, and at the moment the failing write's own catch handler runs, that same write's executingOperations entry for the node was believed to still be present (its own finally block "hasn't cleared it yet") — so a naive hasPending check there was found, empirically, to read the failing write's own bookkeeping as "something is pending" almost every time, defeating the resync for its single most common case (an isolated failure, nothing else in flight). This was hit directly during #2066's review: wiring in a real hasPending check broke 3 existing tests, including the pre-existing OCC-hydration regression suite.

Whoever picks this up should verify the actual call/microtask ordering with a real regression test rather than reasoning about it in the abstract — do not assume either this issue's failure scenario or the round-1 empirical finding without re-confirming both against current code. In particular, confirm empirically:

  • Whether runOperation's finally block (which clears executingOperations and promotes a queued write into pendingOperations) has actually completed by the time the failing write's own catch-triggered call into resyncNodeFromServer runs, given how updateNode() awaits the write's promise. If it has, hasPending() at that point would already reflect a genuinely-different queued write B, not the failing write's own residue — which would change the fix needed here.
  • The exact scenario in this issue (write B genuinely queued behind failing write A) against a written-first, verified-to-fail regression test.

Proposed Solution

One of, pending the verification above:

  • Add a PersistenceCoordinator predicate that answers "is there a queued/pending write for this node other than the one currently failing" — e.g. by passing the failing operation's own identity/token so it can be excluded from the check.
  • Or, if verification shows finally genuinely completes before the direct call's catch handler runs, simply route the direct call through the same real hasPending() check the queued-follow-up path already uses, and re-verify the previously-broken tests to confirm whether the round-1 finding still applies to the current code shape.
  • Or a narrower check against queuedOperations specifically (where a genuinely-different write B lives while A is still executing), rather than hasPending()'s coarser OR of three sets.

Acceptance Criteria

  • A regression test reproduces the failure sequence above (write A executing, write B queued behind it for the same node, A fails with an OCC conflict with no embedded current_node, node not focused) and is verified to fail against the current code (B's optimistic value gets clobbered) before any fix is applied.
  • The same test passes after the fix, with B's optimistic value preserved.
  • The existing OCC-hydration regression suite (and the 3 tests referenced as having broken in Kanban: cap column rendering and add browser drag-and-drop tests #2066's round 1 when hasPending was first tried) still pass — confirming the fix does not reintroduce the self-referential false-positive.
  • bun run test:all has no new failures.
  • bun run quality:fix is clean.

Technical Specifications

Reference Files

  • packages/desktop-app/src/lib/services/shared-node-store.svelte.ts: resyncNodeFromServer (direct-call hasPending hardcode) and PersistenceCoordinator (hasPending(), queuedOperations, executingOperations, persist()'s isExecuting branch) — all in this one file.
  • packages/desktop-app/src/lib/services/remote-update-policy.ts: decideRemoteUpdate — consumes the hasPending/isFocused inputs this issue is about.

Architecture Context

Reviewed against decisions/026-write-flow-conflict-resolution.md (ADR-026), which documents the OCC version-conflict pipeline and the resyncNodeFromServer fallback this issue concerns — this is an implementation gap within that documented design, not a reversal of it. No companion nodespace-docs change is expected; the fix operates entirely within ADR-026's existing decision.

Related Issues

Activity

  1. mstomar125 commented on Aug 14, 2026

    @mstomar125
    ContributorAuthor

    Fixed in #2076. Empirically confirmed (before designing the fix) that the naive "just swap in a live PersistenceCoordinator.hasPending() read" approach flagged as unsafe in this issue really is unsafe: at resyncNodeFromServer's decision point, executingOperations still has the failing write's own entry (its runOperation hasn't reached finally yet), so a live hasPending() read is true almost every time regardless of whether anything else is genuinely queued — reproduced the exact false-positive from #2066's round-1 finding (an isolated failure with nothing else queued would wrongly skip the resync).

    Also found, empirically, that the issue's own literal queuedOperations-based framing needed one more layer: both OCC handlers call PersistenceCoordinator.clearQueued(nodeId) unconditionally (to stop the failing write's own stale-version retry) immediately before either hydration branch runs — which removes a genuinely-queued second write from queuedOperations before resyncNodeFromServer's decision point is ever reached. So the fix captures PersistenceCoordinator.isQueued(nodeId) (a new, self-reference-free predicate) BEFORE clearQueued() runs, and threads that captured value into resyncNodeFromServer as directCallHadQueuedWrite.

    Regression test reproduces the concrete race (write A executing, write B collapsed into queuedOperations, A fails OCC with no current_node, node unfocused) and is verified to fail against the pre-fix code before the fix, and pass after. A second test locks in the #2066 round-1 finding (isolated failure still resyncs normally). Both direct call sites of resyncNodeFromServer (updateNode and updateTaskNode) are fixed, since they share the identical bug via the same shared function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions