Repository navigation
Drop in-flight database reads after an active-database switch (ADR-053) - #1601
Conversation
Add a monotonic database epoch to SharedNodeStore, bumped by clearAll() (which the ADR-053 switch already calls). Every path that fetches from the daemon and then writes the result into the store now captures the epoch before awaiting and drops the write if it has advanced, so a read issued against database A can no longer populate the now-active database B's store with orphan rows that leak through global search / mention resolution. Guarded read-then-write paths: - SharedNodeStore.ensureNode, loadChildrenForParent, doLoadChildrenTree, resyncNodeFromServer - tauri-sync-listener: fetchAndUpdateNode (per-event hydration) and flushPendingNodeFetches (Pro reconnect-replay coalescer) — the busiest desktop store-population paths. isActiveDatabaseEvent gates on event arrival, before these async fetches dispatch, so it cannot close the in-flight window on its own. - browser-sync-service: fetchAndUpdateNode (HTTP-adapter parity) - node-card-inline (mention/node-card resolution) - navigation-service (link-click resolution) — now aborts the navigation entirely (returns null) when the epoch advances, rather than opening a tab for a node absent from the now-active store - query-node-viewer (schema query load) - ai-chat-node-viewer (browser-mode poll) - app-shell MCP node-updated listener loadChildrenForParent is restructured so both daemon reads complete before a single staleness check gates the parent, child, and structureTree-edge writes together. Update the switchTo comment to describe the epoch guard that now closes this window. Regression tests cover a store-level read that resolves after the epoch advances (dropped) versus within the same epoch (applied), plus the community per-event and Pro coalescer domain-event paths each dropping an in-flight read across a switch. Closes the in-flight-read window from ADR-053, #1599. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
APPROVE Reviewed independently (adversarial pass over every daemon read-then- Caught and fixed during review (was a blocker): the first pass guarded the Verification of the final state:
Non-blocking follow-up (out of scope here): the app-shell MCP test:e2e passed; test:all failed only on the environmental |
|
APPROVE — this correctly closes the in-flight-read race I flagged on #1598 (tracked as #1599). The mechanism is right and the coverage is thoughtfully broad. The guard pattern is textbook-correctAn epoch/generation counter is the right tool for invalidating stale async writes, and it's implemented properly:
Coverage is well-reasonedThe key insight is explicit in the PR: TestsGood: Caveat (non-blocking)Correctness of the mechanism is proven; exhaustive coverage rests on the enumeration being complete — i.e. that no other Merge gateCI |
Summary
Follow-on hardening from the ADR-053 desktop switcher (#1533 / #1598). When the active database is switched, a read dispatched against the previous database before the switch — whose response resolves after the switch's
clearAll()— couldsetNodethe old database's rows into the now-active store. Those nodes are unreferenced by the new tree (invisible in the outliner) but linger insharedNodeStoreand can surface via global search / mention resolution until the next reload.This closes that window using the issue's Option A: a monotonic database epoch on
SharedNodeStore, bumped byclearAll()(already called byswitchTo). Every path that reads from the daemon and then writes into the store captures the epoch before awaiting and drops the write if it has advanced.Changes
shared-node-store.svelte.ts—private databaseEpoch,currentEpoch()getter,clearAll()bumps it. Guards onensureNode,loadChildrenForParent(restructured so both daemon reads complete before a single staleness check gates the parent, children, and structureTree-edge writes together),doLoadChildrenTree,resyncNodeFromServer.tauri-sync-listener.ts— guards onfetchAndUpdateNode(per-event hydration) andflushPendingNodeFetches(Pro reconnect-replay coalescer, whole-burst drop).isActiveDatabaseEventgates on event arrival, before these async fetches dispatch, so it cannot close the in-flight window on its own.browser-sync-service.ts—fetchAndUpdateNodeguard (HTTP-adapter parity; browser mode can't switch databases today, so defensive/future-proof).navigation-service.ts— link-click resolution now aborts (returnsnull) when the epoch advances, rather than opening a tab for a node absent from the now-active store.node-card-inline,query-node-viewer,ai-chat-node-viewer,app-shellMCP node-updated listener.switchTocomment updated to describe the epoch guard that closes this window.Tests
shared-node-store-database-epoch.test.ts(new, 3) — store-level: a read resolving after the epoch advances is dropped; within the same epoch it is applied.tauri-sync-listener.test.ts(+2) — the community per-event path and the Pro coalescer path each drop an in-flight read across a switch.Acceptance
Closes #1599.