Skip to content

shared-node-store: distinguish dependency-wait cancellation propagation from direct cancellation - #2079

Merged
mstomar125 merged 2 commits into
mainfrom
issue-2057-dependency-wait-cancellation
Aug 14, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-2057-dependency-wait-cancellation

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

SimplePersistenceCoordinator's dependency-wait loop (runOperation, via persist()'s dependencies option — fed by persistenceDependencies and afterSiblingId) let a dependency's promise rejection reach a dependent write as a raw error, indistinguishable from the dependent being cancelled directly.

Every one of the 5 persist()-caller .catch sites in this file treats err instanceof OperationCancelledError as "I was personally cancelled — a newer write for the SAME node supersedes me, ignore this." That's true when cancelPending()/clearQueued()/the queued-operation overwrite rejects an operation's own promise directly. It is not true when the rejection belongs to a DIFFERENT node this operation depended on: the dependent's own op() (its actual backend RPC) never even ran, and nothing else is going to retry it — so a propagated OperationCancelledError from a cancelled dependency was silently swallowed as "expected," discarding the dependent write with no trace and nothing surfaced to the user.

This was latent before #2050/#2056 fixed cancelPending() to actually reject a superseded write's promise (previously left unsettled forever, so nothing propagated through the dependency-wait loop at all) — fixing that exposed this as a live bug.

Fix

  • Added DependencyFailedError, thrown by the dependency-wait loop (for both node-id and lambda dependencies) instead of letting the dependency's raw rejection propagate. Wraps the original cause; deliberately not a subclass of OperationCancelledError, so every existing .catch(err => err instanceof OperationCancelledError ...) site correctly falls through to its "not a cancellation, surface it" branch.
  • Audited all 5 catch sites: updateNode(), setNode(), and persistBatchedChanges() already surface non-cancellation failures via a write-failure notification and needed no changes — they automatically benefit from the new type distinction. deleteNode()'s catch site previously swallowed every non-cancellation error silently (the comment claiming "real errors are logged by PersistenceCoordinator" wasn't actually true) — fixed to match the established write-failure notification pattern, since deleteNode() also declares real dependencies and was the call site most directly exposed to this bug. updateTaskNode()'s catch site is unchanged — it never declares dependencies, so it isn't exposed to this specific propagation bug; its separate pre-existing gap (silently swallowing genuine non-cancellation failures) is a different bug class, left out of scope.
  • Audited resetInstance() for the same unsettled-promise-plus-live-timer defect class cancelPending()/the queue-overwrite had before PersistenceCoordinator leaves superseded write promises unsettled on two of three supersede paths #2050: no production call path exists (every call site across the codebase is test setup/teardown) — documented rather than fixed, per the issue's own conditional acceptance criterion.

Testing

New dependency-wait-cancellation-propagation.test.ts:

  • Coordinator-level (direct SimplePersistenceCoordinator tests, mirroring persistence-coordinator-supersede-settlement.test.ts's style): a cancelled dependency's rejection reaches the dependent as DependencyFailedError, not a raw OperationCancelledError; the dependent's own op() never runs; DependencyFailedError carries the dependency id and original cause; a genuine (non-cancellation) dependency failure — both already-settled and still-in-flight when the dependent starts waiting — is also wrapped.
  • End-to-end via SharedNodeStore.updateNode()'s persistenceDependencies: reproduces the concrete sequence from the issue (node A's debounced write cancelled by a superseding edit before it fires, node B depends on A) and asserts B's backend RPC never fires AND a write-failure notification surfaces for B. Verified this fails against the pre-fix code (temporarily stashed the source fix — B's write silently vanishes, no notification) and passes after the fix. A second test locks in that an ordinary same-node cancellation (no dependency involved) still raises no spurious notification.

Ran the broader regression suite most likely to be sensitive to this change (416 tests across shared-node-store-extended, shared-node-store-coverage, reactive-node-service, subtree-access-denied-restore, header-inheritance, shared-node-store, persistence-coordinator-supersede-settlement, persistence-serial-writer-regression, resync-direct-call-queued-write-regression, shared-node-store-skip-while-editing, ai-chat-occ-conflict-regression) — all green, including subtree-access-denied-restore.test.ts's delete-failure coverage (confirming the deleteNode catch-site fix doesn't disturb the subtree-access-denied handling, which happens inside the persist closure's own try/catch before re-throwing).

Full suite: bun run test — 205 files, 4619 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.

Scope note

Per the issue's own escape hatch ("if #2057 turns out to need a genuinely large redesign... it's fine to land a real, tested, narrower fix for the concrete failure sequence... and file a tighter follow-up for anything larger"): the acceptance criteria's three related findings (dependency-wait propagation, resetInstance(), and the "duplication, lower priority" refactor note) are addressed as scoped — the first fixed, the second audited with no production path found, and the third (a shared helper de-duplicating the "settle before drop" and cancellation-.catch patterns) is explicitly flagged in the issue itself as "disproportionate scope for a targeted bugfix, reasonable for a follow-up refactor" and left as such.

Closes #2057

🤖 Generated with Claude Code

SimplePersistenceCoordinator's dependency-wait loop (runOperation, via
persist()'s dependencies option -- fed by persistenceDependencies and
afterSiblingId) let a dependency's promise rejection reach a dependent
write as a raw error, indistinguishable from the dependent being
cancelled directly. Every one of the 5 persist()-caller .catch sites in
this file treats `err instanceof OperationCancelledError` as "I was
personally cancelled -- a newer write for the SAME node supersedes me,
ignore this." That is true when cancelPending()/clearQueued()/the
queued-operation overwrite rejects an operation's own promise directly.
It is NOT true when the rejection belongs to a DIFFERENT node this
operation depended on: the dependent's own op() (its actual backend
RPC) never even ran, and nothing else is going to retry it -- so a
propagated OperationCancelledError from a cancelled dependency was
silently swallowed as "expected," discarding the dependent write with
no trace and nothing surfaced to the user.

This was latent before #2050/PR#2056 fixed cancelPending() to actually
reject a superseded write's promise (previously left unsettled
forever, so nothing propagated through the dependency-wait loop at
all) -- fixing that exposed this as a live bug.

Add DependencyFailedError, thrown by the dependency-wait loop (for
both node-id and lambda dependencies) instead of letting the
dependency's raw rejection propagate. It wraps the original cause and
is deliberately NOT a subclass of OperationCancelledError, so every
existing `.catch(err => err instanceof OperationCancelledError ...)`
site correctly falls through to its "not a cancellation, surface it"
branch instead of silently swallowing it.

Of the 5 catch sites, updateNode(), setNode(), and
persistBatchedChanges() already surface non-cancellation failures via
a write-failure conflict notification and needed no changes -- they
automatically benefit from the new type distinction. deleteNode()'s
catch site previously swallowed EVERY non-cancellation error silently
(the comment claiming "real errors are logged by PersistenceCoordinator"
was not actually true -- nothing surfaced to the user either way);
fixed to match the established write-failure notification pattern,
since deleteNode() also declares real dependencies (nodes that must
persist first to avoid a FOREIGN KEY violation) and was the call site
most directly exposed to this bug. updateTaskNode()'s catch site is
unchanged: it never declares dependencies, so it isn't exposed to this
specific propagation bug -- its separate, pre-existing gap (silently
swallowing genuine non-cancellation task-update failures) is a
different bug class, left out of scope here.

Audited resetInstance() for the same unsettled-promise-plus-live-timer
defect class cancelPending()/the queue-overwrite had before #2050: no
production call path exists (every call site across the codebase is
test setup/teardown), so left as documented rather than fixed, per the
issue's own conditional acceptance criterion.

Regression tests (dependency-wait-cancellation-propagation.test.ts):
- Coordinator-level: a cancelled dependency's rejection reaches the
  dependent as DependencyFailedError, not a raw OperationCancelledError;
  the dependent's own op() never runs; DependencyFailedError carries
  the dependency id and original cause; a genuine (non-cancellation)
  dependency failure, both already-settled and still-in-flight when the
  dependent starts waiting, is also wrapped.
- End-to-end via SharedNodeStore.updateNode()'s persistenceDependencies:
  reproduces the concrete sequence (node A's debounced write cancelled by
  a superseding edit before it fires, node B depends on A) and asserts
  B's backend RPC never fires AND a write-failure notification surfaces
  for B -- verified this fails against the pre-fix code (no notification,
  B silently vanishes) and passes after the fix. A second test locks in
  that an ordinary same-node cancellation (no dependency involved) still
  raises no spurious notification.

Full existing OCC/resync/serial-writer/delete test suites re-verified
green.

Closes #2057

Co-authored-by: Claude <[email protected]>
…surfacing

Adversarial review of this PR found a real regression in the deleteNode()
catch-site fix: a subtree-access-denied refusal already raises its own
specific notification (showSubtreeAccessDenied) inside the persist
closure's own catch, before re-throwing to mark the coordinator operation
failed. The new outer handle.promise.catch()'s generic write-failure
surfacing had no guard against this, so a refusal now produced BOTH
notifications for the same event.

The straightforward fix (checking isSubtreeAccessDenied(err) on the outer
catch's error, mirroring the isVersionConflict guard at other call sites)
does not work: the closure re-throws `error` (`dbError instanceof Error ?
dbError : new Error(String(dbError))`), and the real command errors
crossing the Tauri/gRPC boundary are plain objects, not Error instances --
so `error` is always a freshly-wrapped generic Error by the time it
reaches the outer catch, having already lost the `.code`/`.conflictData`
shape isSubtreeAccessDenied checks for. Confirmed this empirically: the
naive isSubtreeAccessDenied(err) guard left the regression test failing
identically to no guard at all.

Fixed instead with a boolean captured in deleteNode()'s enclosing scope
(subtreeAccessDeniedAlreadyNotified), set from inside the closure at the
same point the specific notification is raised, and read directly by the
outer catch -- avoiding the error-shape-stripping problem entirely.

While investigating this, confirmed (via a throwaway reproduction, not
included) that the SAME error-shape-stripping bug already exists,
independently of this PR, at updateNode()'s and setNode()'s outer catch
sites: an OCC (version-conflict) failure's outer isVersionConflict(err)
guard is equally ineffective there, producing a spurious duplicate
write-failure notification alongside the correct version-mismatch one.
That is a distinct, pre-existing bug unrelated to dependency-wait
cancellation propagation -- filed separately as a follow-up rather than
fixed here.

Extends subtree-access-denied-restore.test.ts: the refusal test now
asserts zero write-failure notifications alongside the existing
subtree-access-denied assertions (verified failing without the fix,
passing with it); the non-refusal test now asserts exactly one
write-failure notification, locking in the deleteNode() fix's intended
behavior for a genuine deletion failure.

Co-authored-by: Claude <[email protected]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

Addressed a real finding from adversarial review, plus discovered and filed a related pre-existing bug.

Review finding: my `deleteNode()` fix (surfacing a generic write-failure notification for non-cancellation errors) had no guard against a subtree-access-denied refusal, which already raises its own specific notification inside the persist closure. Result: a refusal now produced BOTH notifications for the same event.

Root cause, found while fixing it: the straightforward fix (`isSubtreeAccessDenied(err)` on the outer catch, mirroring the existing `isVersionConflict` pattern) doesn't actually work — confirmed empirically. The closure re-throws `error = dbError instanceof Error ? dbError : new Error(String(dbError))`, and real command errors crossing the Tauri/gRPC boundary are plain objects, not `Error` instances — so `error` is always a freshly-wrapped generic `Error` by the time it reaches the outer catch, having already lost the `.code`/`.conflictData` shape `isSubtreeAccessDenied` checks for.

Fixed (commit `18ac7843`) with a boolean captured in `deleteNode()`'s enclosing scope (`subtreeAccessDeniedAlreadyNotified`), set inside the closure at the same point the specific notification fires, read directly by the outer catch — sidestepping the error-shape-stripping problem entirely. Verified empirically: reverted just the guard, confirmed the extended regression test fails identically to having no guard; restored it, confirmed it passes.

Also discovered: the same error-shape-stripping bug already exists, independently of this PR, at `updateNode()`'s and `setNode()`'s outer catch sites — an OCC (version-conflict) failure's `isVersionConflict(err)` guard is equally ineffective there, producing a spurious duplicate write-failure notification alongside the correct version-mismatch one. Reproduced with a throwaway test (not included). This is a distinct, pre-existing bug unrelated to dependency-wait cancellation propagation — filed separately as #2080 rather than fixed here, to keep this PR scoped to #2057.

Extended `subtree-access-denied-restore.test.ts`: the refusal test now asserts zero write-failure notifications; the non-refusal test now asserts exactly one, locking in the deleteNode() fix's intended behavior.

Full suite re-verified green (205 files, 4619 tests), quality:fix clean, pre-push gate passed in full on the follow-up push (one retry needed due to an unrelated timestamp-precision flake in text-node.test.ts, resource-contention under load, unrelated to this diff).

@mstomar125

Copy link
Copy Markdown
Contributor Author

APPROVE

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

  • Diff verified against actual merge-base (8824fc5), scoped correctly to exactly 3 files: shared-node-store.svelte.ts, the new dependency-wait-cancellation-propagation.test.ts, and the extended subtree-access-denied-restore.test.ts. No stray unrelated changes (the pre-existing cargo-fmt drift in nodes.rs was caught and reverted before each commit, consistent with shared-node-store: apply skip-while-editing guard to OCC direct-hydration path #2071/shared-node-store: fix resyncNodeFromServer's direct-call hasPending hardcode #2076).
  • Full suite green: 205 files, 4619 tests. quality:fix clean (eslint, tsc, cargo fmt, cargo clippy --all-targets). Pre-push gate passed in full on both pushes (one retry needed on the second push due to an unrelated timestamp-precision flake in text-node.test.ts under load, confirmed unrelated).
  • Two rounds of adversarial sub-agent review: round 1 found a real regression (deleteNode's new write-failure surfacing double-notified on subtree-access-denied refusals); fixed in 18ac784 with a captured-flag approach after empirically confirming the naive isSubtreeAccessDenied(err) check doesn't work (error-shape stripping via the dbError-to-Error conversion). Round 2 (focused re-review of the follow-up commit) independently reproduced both the bug and the fix, confirmed the naive fix fails identically, confirmed per-call state isolation (no leakage across deleteNode() calls), confirmed the OCC double-notification finding in updateNode()/setNode() is real but reasonably deferred to shared-node-store: OCC/subtree-access-denied errors lose their shape before reaching the outer persist().catch(), causing a spurious duplicate notification #2080, confirmed DependencyFailedError itself isn't subject to the same stripping bug (it's thrown as a real Error instance, never wrapped), and re-ran the full suite green. No gaps found in either round.

Merging.

@mstomar125
mstomar125 merged commit 06fc30f into main Aug 14, 2026
@mstomar125
mstomar125 deleted the issue-2057-dependency-wait-cancellation branch August 14, 2026 07:32
mstomar125 added a commit that referenced this pull request Aug 14, 2026
…notification in updateNode() (#2091)

* shared-node-store: fix OCC error-shape stripping causing a duplicate notification in updateNode()

updateNode()'s persist() closure re-throws `dbError instanceof Error ?
dbError : new Error(String(dbError))` on any failure. Real command
errors crossing the Tauri/gRPC boundary (VERSION_CONFLICT,
SUBTREE_ACCESS_DENIED) are plain objects, not instanceof Error, so an
OCC conflict got wrapped in a fresh generic Error before re-throwing —
losing its `.code`/`.conflictData` shape entirely.

The outer `handle.promise.catch((err) => { if (isVersionConflict(err))
return; ...generic write-failure notification... })` then checked
isVersionConflict on this already-stripped err, which could never
match. The guard silently failed, so every OCC conflict produced BOTH
the specific version-mismatch notification the inner closure already
raised AND a spurious duplicate generic write-failure notification
from the outer catch.

Fixed the same way #2079 fixed the identical bug for deleteNode(): a
boolean (occConflictAlreadyNotified) captured inside the closure, set
at the point the specific notification is raised, checked by the
outer catch instead of re-deriving the classification from the
already-stripped err.

Investigated setNode()'s presumed identical bug (per the issue, "not
independently reproduced with a test") and confirmed empirically it
does NOT reproduce: setNode()'s persist() closure has no OCC-specific
catch branch at all, so there is no earlier specific notification for
the outer catch's write-failure fallback to duplicate — an OCC failure
there already correctly surfaces exactly one (generic) notification.
The outer catch's isVersionConflict(err) check was dead code (never
matched, for the same error-stripping reason) rather than an active
duplicate-notification bug, so removed it as misleading dead code
instead of "fixing" it with an unnecessary captured flag.

Added occ-notification-dedup.test.ts covering both methods: updateNode()
raises exactly one version-mismatch notification on an OCC conflict
(not two); setNode() raises exactly one generic notification (not
zero, not two). Both files' non-OCC failure paths are covered too, as
regression checks that the fix didn't change that behavior.

Closes #2080

Co-Authored-By: Claude <[email protected]>

* Address adversarial review: fix a misleading test comment, file a follow-up for updateTaskNode's silent-failure gap

Review of #2091 found no BLOCKER/functional bugs — the updateNode() fix,
the setNode() investigation, and scope discipline all independently
verified correct (including the reviewer's own scratch test confirming
setNode()'s seeding/gating behavior).

One MINOR fix applied: occ-notification-dedup.test.ts's comment on
updateNode()'s persist-mode gating described setNode()'s formula
(`source.type !== 'viewer' || isNewNode`) as if it applied to
updateNode() too. Corrected to describe updateNode()'s actual gate
(structural/property/nodeType/type-specific change -> immediate mode
regardless of source) and clarified why setNode()'s tests specifically
need a non-viewer source.

The review also surfaced a real, more serious gap: updateTaskNode()'s
outer handle.promise.catch() has no fallback write-failure notification
at all for a non-OCC failure (unlike updateNode()/deleteNode()/
setNode(), which all correctly surface one) — a task status/priority/
due-date edit that fails outside an OCC conflict silently reverts with
zero user-visible signal. Out of scope for this PR (about deduplicating
a notification, not adding a missing one) — filed as #2092.

Co-Authored-By: Claude <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
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.

Dependency-wait cancellation propagation silently swallows genuine failures and can eat an unrelated write

1 participant