Repository navigation
Settle superseded persist() promises on cancelPending and queue-overwrite paths - #2056
Conversation
…rite paths SimplePersistenceCoordinator handed every persist() caller a promise but left it unsettled on two of the three supersede paths: cancelPending() cleared the debounce timer and deleted the pending entry without ever resolving/rejecting pending.promise, and the queued-operation overwrite in persist() replaced an already-queued entry via Map.set, discarding its resolve/reject along with it. Both now reject with OperationCancelledError before the entry is dropped/replaced, mirroring clearQueued's existing settlement rule. Rejecting on cancelPending() exposed a fifth persist() call site (persistBatchedChanges, used by startBatch/commitBatch) that never captured its handle at all, so a superseded batch write's rejection surfaced as an unhandled promise rejection. Added the same handle.promise.catch() pattern used by the other four call sites there. Adds direct coverage of SimplePersistenceCoordinator (exported for this purpose) for a debounced write superseded before its timer fires, and two writes queued behind one in-flight write. Co-Authored-By: Claude <[email protected]>
Pragmatic Code ReviewReview Type: Initial Review Requirements Check:
Code Review Findings: 🔴 Critical:
|
The reviewer found that cancelPending() rejected pending.promise unconditionally, even when the entry belongs to a write that is still actually executing (reachable via startBatch(), which calls cancelPending() with no isExecuting guard, unlike persist()'s own internal call site). That let a genuinely in-flight write's promise settle early as "cancelled" — silently masking a real write failure once the actual RPC's resolve/reject later became a no-op on the already-settled promise, and handing waitForPersistence/flushAndWaitForNodes/flushAllPendingSaves a false signal even on success. Gate the reject on !isExecuting: while a write is in flight, the found entry's resolve/reject either belong to that real RPC (whose outcome must not be pre-empted) or are the dead no-op placeholder for a write queued behind it (whose real promise lives in queuedOperations and still settles normally once the in-flight write's finally block dispatches it) — in both cases the correct behavior is to leave settlement to the real completion path, exactly as the function's own isExecuting logging already implied. Adds a regression test exercising this exact race directly against SimplePersistenceCoordinator: cancelPending() called while a write is executing must not settle its promise early, and the real outcome (success or failure) must still reach the caller once the write actually completes. Verified the test fails without the fix (reverted the guard locally, confirmed the new assertion catches it, restored the fix). Co-Authored-By: Claude <[email protected]>
Address ReviewAddressed (1/1 finding): ✅ 🔴 Critical — Added a regression test ( Skipped: none — the single finding was addressed as described. Verification after the fix: Re-Review DecisionDecision: RE-REVIEW NEEDED Rationale: The addressed finding was a 🔴 Critical correctness bug fixed with a real logic change (an added conditional gating the reject), not a style/doc-only edit, so per the review workflow this warrants a re-review pass to verify the fix is correct and complete and that it introduced nothing new. |
…omment Three findings addressed from the re-review: 1. persistBatchedChanges()'s new handle.promise.catch() copied the isVersionConflict-swallow pattern from updateNode()/updateTaskNodeStatus(), but unlike those call sites, this operation closure has no OCC-specific handling of its own (no rollback, no resync, no notification) — it only logs and re-throws. Silently swallowing VERSION_CONFLICT here dropped a genuine batch-commit failure with zero visible recovery: no rollback of the optimistic state, no notification, and the local version never corrected from the backend, so the next edit to that node would hit the same conflict again through the same silent path. Removed the swallow so every non-cancellation failure, including OCC, surfaces the standard write-failure notification for this call site — matching what it actually handles today. Added a regression test through SharedNodeStore's real startBatch/commitBatch wiring (not the isolated coordinator) asserting a write-failure notification is raised; verified it fails without the fix. 2. Exporting SimplePersistenceCoordinator for direct testing incidentally made its constructor public, so external code could `new` a second, uncoordinated instance with maps disjoint from the real singleton — defeating the per-node serial-writer guarantee. Added `private constructor()` to close that off; getInstance()/resetInstance() are unaffected since they already only call the constructor internally. 3. Added a one-line comment on the `PersistenceCoordinator` alias explaining why it stays (renaming ~20 internal call sites is disproportionate to this PR) now that the class is separately exported under its real name. Not addressed, documented as follow-ups (see PR comment): a dependency-wait path can silently abort a node's own write if a node it depends on gets superseded mid-wait (broadens a pre-existing dependency-rejection pattern, not introduced by this PR — real fix needs new error-type threading through 5 call sites); cancelPending()'s unconditional pendingOperations.delete() and its never touching queuedOperations are both pre-existing behavior unchanged by this PR (verified against origin/main); resetInstance() dropping unsettled promises is a different method with no confirmed production call site; the settle-before-drop pattern's 3x duplication and the cancellation-catch pattern's 5x duplication match pre-existing conventions in this file, not something to refactor unprompted here. Co-Authored-By: Claude <[email protected]>
Address Review (round 2)The re-review found 11 items. Addressed 3, skipped 8 with reasoning below. Addressed: ✅ Silent OCC-swallow in ✅ Constructor made private — exporting ✅ Alias comment — added a one-line comment on Skipped, with reasoning:
Verification: Re-Review DecisionDecision: RE-REVIEW NEEDED Rationale: One of the three addressed items (the OCC-swallow fix) is a real logic change to error-handling behavior, not a style/doc-only edit. |
Review wrap-up (rounds 3-4 consolidated)Two further deep review passes (beyond the round 1/round 2 fixes already applied and commented above) surfaced a cluster of additional findings in Distinct findings, deduplicated:
Scope decision: None of these are new regressions introduced by this PR's actual diff ( Recommend filing a follow-up issue for the dependency-wait cancellation-propagation design (findings #1-3 share that root cause) rather than expanding this PR further. The fix as committed (7c08114, 5e018d7, 5f40a99) remains correct and complete for issue #2050's stated scope, with |
Address Review (round 3/4 — final triage)The consolidated wrap-up above surfaces no new correctness issue introduced by this PR's actual diff (the No code changes made this round. Filing a follow-up issue for the dependency-wait cancellation-propagation design (the shared root cause across findings #1-3) is the right next step, not expanding this PR further. Re-Review DecisionDecision: NO RE-REVIEW NEEDED Rationale: No code was changed in this round — there is nothing new to re-review. The PR's diff has been reviewed across three passes; the two rounds that found real issues (the |
Closes #2050.
What changed
SimplePersistenceCoordinator(packages/desktop-app/src/lib/services/shared-node-store.svelte.ts) hands everypersist()caller a promise but left it unsettled on two of the three supersede paths:cancelPending()cleared the debounce timer and deleted the pending entry without ever resolving/rejectingpending.promise.persist()replaced an already-queued entry viaMap.set, discarding itsresolve/rejectalong with it — one orphaned promise per superseded keystroke during an in-flight write.Both paths now reject with
OperationCancelledErrorbefore the entry is dropped/replaced, mirroringclearQueued()'s existing (already-correct) settlement rule.Fixing
cancelPending()exposed a fifthpersist()call site (persistBatchedChanges, used bystartBatch/commitBatch) that never captured itshandleat all — so once cancellation started actually rejecting, a superseded batch write surfaced as an unhandled promise rejection (caught locally via a fullbun run test:allrun:startBatch()'s call tocancelPending()during an in-flight batch CREATE). Added the samehandle.promise.catch()pattern already used by the other four call sites (swallowOperationCancelledErroras expected, surface real write failures viaconflictNotifications).Tests
New
persistence-coordinator-supersede-settlement.test.ts, exercisingSimplePersistenceCoordinatordirectly (exported for this purpose, so the tests are precise about which write's promise settles and how, without going through the fullSharedNodeStoreupdate pipeline):OperationCancelledError(cancelPendingpath).OperationCancelledErrorwhen a second supersedes it in the single-slot queue; the surviving write still runs to completion.Verification
bun run test, before this change): 201 files / 4574 tests passed.bun run test(after): 202 files / 4576 tests passed — the +1 file/+2 tests is exactly the new test file. (One incidental run hit a known pre-existing cross-file test-isolation flake inagent-store.test.ts/browser-sync-service.ts, unrelated to this diff — it passes in isolation and on repeat full runs; not touched by this PR.)bun run test:all(coverage + scripts + skill + full Rust workspace): all green, 0 failures.bun run quality:fix: ESLint,tsc --noEmit,cargo fmt,cargo clippy --all-targets -- -D warnings -D clippy::unused-asyncall clean.test:all+cargo build --bin nodespaced+cargo test -p nodespace-appe2e/Tauri-seam suite): all green.🤖 Generated with Claude Code