Skip to content

shared-node-store: OCC direct-hydration branch needs the same skip-while-editing guard as resyncNodeFromServer #2068

Closed

Description

@mstomar125

Follow-up from PR #2066 (core#1985) review.

1. resyncNodeFromServer's idempotency guard silently drops a second in-flight resync request — fixed directly in PR #2066 (single-slot queued follow-up, mirroring PersistenceCoordinator's own pattern; regression-tested in shared-node-store.test.ts and shared-node-store-extended.test.ts). Leaving struck through rather than deleting so this issue's history stays legible against the original review comment it's responding to.

The OCC-conflict payload hydration branch bypasses the skip-while-editing guard too

In updateNode()'s OCC-conflict catch handling, there are two paths: hydrate directly from occError.conflictData.current_node if the daemon embedded it, or fall back to resyncNodeFromServer() if not. PR #2066 added a skip-while-editing guard (decideRemoteUpdate, isFocused-only) to the fallback path, inside resyncNodeFromServer. The direct hydration path — this.nodesSet(nodeId, currentNode), a few lines above the fallback — writes straight into the store via the same guard-free nodesSet(), with no equivalent check.

Concretely: an OCC conflict on a node the user is actively editing, where the daemon's conflict response happens to embed current_node, still clobbers the optimistic content — the same clobber class #2066 just closed for the other branch. Genuine OCC conflicts are rarer than the ordinary write failures #2066's fix targets, so this is lower-traffic, but it's the same bug in a sibling code path.

Fix sketch: apply the identical decideRemoteUpdate check (see resyncNodeFromServer's guard in shared-node-store.svelte.ts, and its comment on why hasPending is unsafe to reuse verbatim — a self-referential race with the very operation whose conflict triggered the check) before the nodesSet(nodeId, currentNode) call in the direct-hydration branch.

Scope note

packages/desktop-app/src/lib/services/shared-node-store.svelte.ts is core shared infrastructure used by every node type — not Kanban-specific. Flagged as a follow-up rather than folded into #2066 to keep that PR's diff to what it actually needed (hardening the path it newly exercises) rather than a broader hardening pass on every OCC-hydration branch.

Activity

  1. changed the title [-]shared-node-store: resync/OCC-hydration paths need queuing + a matching skip-while-editing guard on the sibling hydration branch[/-] [+]shared-node-store: OCC direct-hydration branch needs the same skip-while-editing guard as resyncNodeFromServer[/+] on Aug 13, 2026
  2. mstomar125 commented on Aug 13, 2026

    @mstomar125
    ContributorAuthor

    Additional gap found during PR #2066 review: the guard has no retry, so a focused node can loop on repeated OCC conflicts

    Both resyncNodeFromServer's guard (added in #2066, covers the OCC-fallback path and the non-OCC failure path that PR originally added) and the fix this issue proposes for the direct-hydration branch share a gap: when decideRemoteUpdate says "skip, the node is actively focused," nothing re-triggers the correction once the user stops editing.

    For the OCC-fallback path specifically, this is worse than a one-off stuck value: Node.version never gets corrected while the skip keeps firing, so the next debounced persist for that node reads the same stale version and hits another OCC conflict — a loop that only breaks if an unrelated broadcast happens to touch the node, or the user stops editing it entirely. A user actively typing when an OCC conflict happens (a very ordinary thing to be true) can get stuck in this loop for as long as they keep typing.

    Fix sketch

    Neither "skip and hope something else corrects it" nor "apply the resync mid-edit" is right. The missing piece is a retry once the skip condition clears — e.g. resyncNodeFromServer (or its caller) records "this node needs a resync" when decideRemoteUpdate skips, and something re-attempts it once focusManager reports the node is no longer focused (a blur/focus-change hook, or a periodic sweep of a small "needs-resync" set against current focus state). This needs its own design pass — it touches how focusManager and the persistence/resync machinery talk to each other, which is more than a inline patch.

    Scope

    Same as the rest of this issue: shared-node-store.svelte.ts is core infrastructure, not Kanban-specific. Applies to both the direct-hydration branch (this issue's original scope) and the OCC-fallback path resyncNodeFromServer already guards — worth designing the retry once, for both.

  3. mstomar125 commented on Aug 13, 2026

    @mstomar125
    ContributorAuthor

    Third gap found: resyncNodeFromServer's new identity guard assumes an invariant the codebase doesn't actually hold everywhere

    PR #2066 added an identity comparison to resyncNodeFromServer (this.nodes.get(nodeId) !== nodeBeforeFetch) to detect "a different write landed for this node while the fetch was in flight" and bail rather than clobber it. This relies on the invariant that every writer to this.nodes replaces the node object (never mutates one already stored in place) — which is true for the store's own nodesSet/updateNode optimistic-apply path, but not universally:

    localNode.version = createdNode.version;        // shared-node-store.svelte.ts:1435
    localNode.version = updatedTaskNode.version;     // :2300
    localNode.content = updatedTaskNode.content;     // :2312
    localNode.version = updatedNodeFromBackend.version; // :3672, :3736
    

    Each of these mutates a node object already sitting in this.nodes directly, then (in most cases) calls nodesSet(nodeId, localNode) with that same reference — so this.nodes.get(nodeId) is unchanged by reference even though its content changed. If a resync's nodeBeforeFetch snapshot happens to be that same object, the identity check sees "nothing changed" and proceeds to overwrite with stale server data, discarding the version bump one of these paths just applied.

    Pre-existing pattern (all four sites predate #2066 and are unrelated to Kanban), and the overlap window is narrow — needs a concurrent OCC-conflict resync in flight for a node that's also completing a create-fallback or task-update version-bump at the same moment. Flagging rather than auditing/fixing all four sites here, same reasoning as the rest of this issue.

    Fix sketch

    Either (a) make these four sites always replace (this.nodesSet(nodeId, { ...localNode, version: ... }) instead of mutating then re-setting the same reference), which is the more consistent fix and the one resyncNodeFromServer was already assuming, or (b) make the resync guard robust to in-place mutation too — e.g. compare a version/content snapshot instead of object identity. (a) seems preferable: it's the invariant most of the file already tries to hold (see setNode's own comment on why direct mutation of a node in the reactive Map is avoided — it also causes unwanted Svelte reactivity/remounts), so these four are already anomalies worth fixing on their own merits, not just for this guard's sake.

  4. mstomar125 commented on Aug 13, 2026

    @mstomar125
    ContributorAuthor

    Additional gaps found in a third review pass on PR #2066, deferred as follow-ups here

    All of these are narrow-reach and/or pre-existing infrastructure, not fixed inline to keep #2066's diff bounded to what it actually needed. Grouping them here since they're all in the same file/area as the rest of this issue.

    1. Batch-routing drops onPersistError/onPersistSuccess. updateNode's early-return for a node with an active batch (this.activeBatches.has(nodeId)) forwards only changes to addToBatch, discarding the rest of UpdateOptions. A caller's revert-on-failure hook (Kanban's moveCard, or any future consumer) silently doesn't fire if the same node also has an active content-edit batch running (e.g. a pattern-conversion type, or any node the user is simultaneously typing into via the autoBatch path) — only the generic write-failure toast surfaces. Fix needs the batching subsystem's own commit path (commitBatch) to thread these callbacks through, not just a one-line change.

    2. pendingUpdates entries for skipPersistence: true writes are never removed. Pre-existing — affects every caller that uses skipPersistence, not just Kanban — but Kanban's revert-on-failure path (onPersistError → updateNode(..., { skipPersistence: true })) now exercises it more frequently. Each such write leaves an unremovable NodeUpdate in pendingUpdates for that node, since markUpdatePersisted/rollbackUpdate (the two things that ever prune that map) are never reached for a skip-persistence write.

    3. The queued follow-up resync (resyncNodeFromServer's idempotency path) is fire-and-forget, not tied to the caller who triggered the queuing. A caller awaiting resyncNodeFromServer() when its call gets queued behind an in-flight one resolves as soon as the in-flight one settles, not its own specific follow-up — the follow-up's own correction lands later with nothing tying it back to that caller. No current caller depends on this precision (the only caller is fire-and-forget itself), but it's a latent trap for a future one that does.

    4. resyncNodeFromServer omits shouldSkipStaleAiChatUpdate, which setNode() runs before its own skip-guard. If an ai-chat node id were ever "focused" (focusManager.editingNodeId), a resync would incorrectly skip applying fetched server state, causing version drift. No code path currently focuses an ai-chat node, so this is dormant today, not reachable — flagging for whenever that changes.

    None of these are Kanban-specific; all are shared-node-store.svelte.ts infrastructure, same as the rest of this issue.

  5. mstomar125 commented on Aug 13, 2026

    @mstomar125
    ContributorAuthor

    Correction: the in-place-mutation gap from my last comment is fixed directly in PR #2066, not deferred

    Struck-through rather than deleted, same as the idempotency-guard item above, so this issue's history stays legible:

    Third gap found: resyncNodeFromServer's new identity guard assumes an invariant the codebase doesn't actually hold everywhere... — fixed directly in PR #2066. The identity guard now compares a JSON.stringify snapshot of the node before/after the fetch instead of object identity, which is correct regardless of whether a concurrent writer replaces the node object or mutates it in place (the four localNode.<field> = ...; nodesSet(nodeId, localNode) sites this comment originally flagged). No further action needed for the resync-guard side of this.

    The four in-place-mutation sites themselves (shared-node-store.svelte.ts:1435/2300/2312/3672/3736 roughly, may have shifted slightly) are still worth normalizing to always-replace on their own merits (matches setNode's own stated reasoning for avoiding in-place mutation of a reactive node), but nothing outside this issue's original scope depends on that anymore — leaving that as a genuinely optional cleanup rather than a correctness gap.

  6. mstomar125 commented on Aug 14, 2026

    @mstomar125
    ContributorAuthor

    Fixed in #2071: the OCC direct-hydration path in `updateNode()` now routes through the same `decideRemoteUpdate` skip-while-editing guard `resyncNodeFromServer()` already applies (added in #2066), with `hasPending` hardcoded to `false` for the same self-referential-race reason as that method's direct call. On a guard-skip the node is still marked persisted but content/version are left untouched, mirroring `resyncNodeFromServer`'s skip branch.

    Added 4 regression tests confirming: the clobber is prevented while focused, `persistedNodeIds` bookkeeping still updates on skip, the direct path (not the fallback) is exercised for the normal non-editing case, and exactly one conflict notification is raised on skip.

    Found the identical unguarded pattern in `updateTaskNode()`'s OCC handler while reviewing this — filing a separate follow-up issue for that since it's out of this issue's scope.

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