Skip to content

Drop in-flight reads across a database switch (ADR-053 orphan-node hardening) #1599

Closed

Description

@mstomar125

Overview

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 cache clear — can setNode the old database's rows into the now-active store.

Problem

database.svelte.ts::switchTo does: flush pending saves → set_active_database → sharedNodeStore.clearAll() → reset workspace → reload. Writes are drained by flushAllPendingSaves(), and live events are dropped by the isActiveDatabaseEvent database_id guard. But an in-flight read (e.g. loadChildren) issued against database A before the switch, resolving after clearAll(), will insert A's rows into the B store. Those nodes are unreferenced by B's tree (invisible in the outliner) but remain in sharedNodeStore and can surface via global search / mention resolution until the next reload.

This is a narrow, self-limiting window (only reads in flight across the sub-100ms switch), which is why it did not block #1533 — but it should be closed properly.

Proposed approach

Tag data reads with the database generation they were issued under and drop (or route to the correct store) any response whose generation no longer matches the active one. Options:

  • A monotonic database epoch on sharedNodeStore (bumped on clearAll/switch); setNode ignores writes carrying a stale epoch. Reads capture the epoch at dispatch and pass it through.
  • Or await/cancel outstanding reads as part of switchTo before clearing.

The switcher store already has a switchSeq token for switch re-entrancy; this extends the same idea to in-flight reads.

Acceptance

  • A read issued against database A that resolves after a switch to B does not leave A's nodes in the B store (regression test).
  • No behavior change in the common (no in-flight read across a switch) case.

Activity

  1. self-assigned this
    on Jul 9, 2026
  2. mstomar125 commented on Jul 10, 2026

    @mstomar125
    ContributorAuthor

    Resolved in #1601.

    Implemented the issue's Option A: a monotonic database epoch on SharedNodeStore, bumped by clearAll() (which the ADR-053 switchTo already calls). 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, so a read issued against database A can no longer populate the now-active database B's store with orphan rows.

    Guarded paths: SharedNodeStore (ensureNode, loadChildrenForParent — restructured so both daemon reads complete before a single staleness check gates the parent + children + structureTree-edge writes together, doLoadChildrenTree, resyncNodeFromServer), the Tauri domain-event hydration paths (tauri-sync-listener fetchAndUpdateNode and the Pro reconnect-replay coalescer flushPendingNodeFetches), browser-sync-service (HTTP-adapter parity), navigation-service (link-click resolution now aborts rather than opening a tab for a node absent from the now-active store), plus node-card-inline, query-node-viewer, ai-chat-node-viewer, and the app-shell MCP node-updated listener. isActiveDatabaseEvent gates on event arrival (before the async fetch dispatches), so it does not cover the in-flight read window on its own — hence the epoch guard on those event paths.

    Acceptance:

    • A read issued against database A that resolves after a switch to B does not leave A's nodes in the B store — regression tests at the store level (shared-node-store-database-epoch.test.ts) and for the community per-event and Pro coalescer domain-event paths (tauri-sync-listener.test.ts).
    • No behavior change in the common (no in-flight read across a switch) case — the guard is a same-epoch no-op.

    Validation: targeted suites green (34 tests across the two files), eslint + svelte-check 0 errors / 0 warnings (1738 files), full cargo test -p nodespace-app pre-push gate green.

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions