Skip to content

Re-confirm cached nodes on daemon reconnect instead of trusting presence forever - #2084

Merged
mstomar125 merged 2 commits into
mainfrom
issue-1979-watchnodes-reconnect-stale-cache
Aug 14, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-1979-watchnodes-reconnect-stale-cache

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

AI chat conversations (and, more generally, any node) could render as empty/stale in the UI after a daemon restart triggered a WatchNodes reconnect, even though the underlying data in the database was fully intact. This was a frontend cache-staleness bug, not a persistence bug.

Root cause

The desktop app's WatchNodes bridge (packages/desktop-app/src-tauri/src/watcher.rs) reconnects with exponential backoff on any daemon disruption (crash, restart, transient h2 error), but opens a fresh live-forward stream with no catch-up replay — any node:created/node:updated/node:deleted events that would have landed during the outage are gone, not delayed.

pane-content.svelte's hydrateNode() (which ensures a tab's node is loaded into sharedNodeStore before its viewer mounts) and its onDaemonReconnect retry handler both only checked whether a node was present in the store — never whether it might be missing an update from the outage window. So a node cached before an outage (e.g. an AI chat conversation opened, then several turns appended by the daemon while WatchNodes was down across a restart) kept rendering its pre-outage snapshot indefinitely: navigating away and back never re-fetched it, because "already cached" was trusted forever.

This is the general sharedNodeStore reconnect-handling gap called out in the issue (not ai-chat-specific) — any node type reachable via ensureNode was affected identically.

Fix

Add reconnect-generation tracking to SharedNodeStore:

  • reconnectGeneration is bumped once per daemon reconnect (wired via the existing onDaemonReconnect hook — the same idiom schemasStore/collectionsData already use to reload on reconnect, but applied lazily per-node instead of eagerly reloading everything).
  • Each node's cache entry records the generation it was last written under (nodeGeneration).
  • isPossiblyStale(nodeId) reports true when a cached node's recorded generation is behind the current one.
  • ensureNode() re-fetches a possibly-stale node instead of returning the cache outright — routed through the existing setNode() → decideRemoteUpdate()/shouldSkipStaleAiChatUpdate() OCC and skip-while-editing guards, exactly like any other database-sourced update, so an in-progress edit or a genuinely newer local write can't be clobbered by the re-confirm.
  • pane-content.svelte's hydrateNode() now gates on presence and freshness; its onDaemonReconnect handler no longer duplicates a presence-only check — it unconditionally delegates to hydrateNode, which owns the decision.

No eviction happens on reconnect — an already-open viewer keeps rendering its last-known content with no flicker while the re-confirm runs in the background, then updates reactively once it lands.

ai-chat-node-viewer.svelte needed no changes: it already reads sharedNodeStore.getNode() reactively, so it updates automatically once the re-fetch resolves.

Testing

  • New shared-node-store-reconnect-staleness.test.ts: store-level coverage of isPossiblyStale/markPossiblyStaleAfterReconnect/ensureNode, including the reconnect-then-navigate-back repro end-to-end through the real setNode write path, a failed re-confirm leaving the node stale for retry, and concurrent ensureNode calls de-duping to one fetch.
  • Updated pane-content-reconnect-hydration.test.ts (mounts the real component): the existing "does not re-fetch on reconnect once hydrated" test is retained for the non-stale case; a new test locks in that a possibly-stale-but-hydrated node does get re-fetched on reconnect.
  • bun run test:all — no new failures. (Two pre-existing flaky failures — model-store.test.ts fake-timer leak from browser-sync-service.ts, and a core-plugins.test.ts timeout — reproduce identically on unmodified main, unrelated to this change.)
  • bun run quality:fix — clean (0 eslint errors/warnings, tsc --noEmit clean, cargo clippy --all-targets -D warnings clean — no Rust files touched).

Closes #1979

…nce forever (#1979)

The WatchNodes bridge (src-tauri/src/watcher.rs) reconnects with backoff
on any daemon disruption, but opens a fresh live-forward stream with no
catch-up replay: node:created/updated/deleted events that would have
landed during the outage are gone, not delayed. pane-content.svelte's
hydrateNode() and its onDaemonReconnect handler only checked whether a
node was *present* in sharedNodeStore, never whether it might be
missing an update from the gap — so a node cached before an outage
(e.g. an AI chat conversation opened, then several turns appended while
WatchNodes was down across a daemon restart) kept rendering its
pre-outage snapshot indefinitely, even though the database held the
full, intact content the whole time.

Add reconnect-generation tracking to SharedNodeStore: a node's cache
entry records the generation it was last written under; a daemon
reconnect (wired via the existing onDaemonReconnect hook, same idiom
schemasStore/collectionsData already use) bumps the generation without
evicting anything, so isPossiblyStale(id) starts reporting true for
every node cached before that point. ensureNode() re-fetches a
possibly-stale node instead of returning the cache outright, routing
through the existing setNode()/decideRemoteUpdate() OCC and
skip-while-editing guards exactly like any other database-sourced
update. pane-content's hydrateNode() gates on presence-AND-freshness,
and its reconnect handler now unconditionally delegates to it instead
of duplicating a presence-only check.

The gap was general, not ai-chat-specific (any node type reachable via
ensureNode benefits), so the fix lives entirely in
shared-node-store.svelte.ts and pane-content.svelte — no changes to
ai-chat-node-viewer.svelte were needed, since it already reads the
store reactively and updates once the re-fetch lands.

Closes #1979

Co-Authored-By: Claude <[email protected]>
…, fix restore()'s bypass of the generation tracking

Two findings from re-review of #2084:

1. node-ref-inline.svelte and node-ref-preview.svelte.ts both pre-empted
   ensureNode with their own `if (!getNode(id))` presence-only guard, so
   the new staleness re-fetch inside ensureNode never triggered for them
   — a wikilink's inline title or hover-preview card for a node cached
   before a daemon outage would keep showing pre-outage content
   indefinitely, the same symptom class as #1979 in a different surface.
   Both guards now also check isPossiblyStale(id), matching the pattern
   already applied to pane-content.svelte's hydrateNode().

2. SharedNodeStore.restore() (optimistic-rollback) wrote into `this.nodes`
   directly instead of through nodesSet()/nodesClear(), so a restored
   node's nodeGeneration entry was left stale (or, for a node dropped by
   the restore, orphaned) instead of being kept in sync with what's
   actually in the map. Routed restore() through the same nodesSet()/
   nodesClear() helpers every other write path uses.

Added a regression test for the node-ref-preview.svelte.ts re-confirm
path (isPossiblyStale mocked true forces a re-fetch even though the
node is cached). node-ref-inline.svelte has no pre-existing test
harness to extend; its fix is a one-line mirror of the already-tested
store contract and the already-tested pane-content.svelte guard.

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

Copy link
Copy Markdown
Contributor Author

Adversarial review complete. Two IMPORTANT findings, both addressed:

  1. Fix wasn't fully general — node-ref-inline.svelte (wikilink inline title) and node-ref-preview.svelte.ts (hover-preview card) both pre-empted ensureNode with their own presence-only guard, so the new staleness re-fetch never triggered for them — same symptom class as AI chat nodes can render empty in the UI after a WatchNodes reconnect, despite intact database content #1979, different surface. Fixed both to also check isPossiblyStale, matching the pane-content.svelte pattern. Added a regression test for the preview-controller path; node-ref-inline.svelte has no pre-existing test harness to extend (its fix is a one-line mirror of the already-tested store contract).

  2. restore() (optimistic-rollback) bypassed the new generation tracking — it wrote into this.nodes directly instead of through nodesSet()/nodesClear(), so a restored node's generation stamp could go stale/orphaned. Routed it through the same helpers every other write path uses.

Everything else the reviewer checked out clean: the reconnect-during-in-flight-fetch race is not a bug (the fetch always reflects genuinely current backend state at resolution, regardless of generation bookkeeping); the skip-while-editing/OCC guards correctly leave a rejected update's node ungenerationed so it stays flagged for retry; no pile-up risk from rapid reconnects (existing inFlightEnsures dedup covers it); scope is clean (no Rust files, no agent/nlp-engine/skills/eval/playbook).

Full suite + quality:fix re-run clean after the fixes; pushed in 579a1a8.

@mstomar125

Copy link
Copy Markdown
Contributor Author

APPROVE (self-review — GitHub blocks a native self-approval on your own PR, posting as a comment instead)

Independent re-verification complete:

  • Diff reviewed end-to-end: 7 files, all in scope (shared-node-store.svelte.ts, pane-content.svelte, node-ref-inline.svelte, node-ref-preview.svelte.ts, and their tests). No Rust files, no agent/nlp-engine/skills/eval/playbook.
  • Root cause confirmed general (not ai-chat-specific): all 3 production ensureNode call sites now check isPossiblyStale, not just presence.
  • Adversarial review (separate sub-agent) found 2 IMPORTANT issues, both fixed and re-verified: the 2 other ensureNode call sites, and restore()'s bypass of the generation-tracking invariant.
  • Full test suite green (bun run test:all via the pre-push gate, both pushes) — only pre-existing, unrelated flakiness reproduces identically on unmodified main (model-store.test.ts timer leak, core-plugins.test.ts timeout).
  • bun run quality:fix clean (eslint, tsc --noEmit, cargo clippy — 0 errors/warnings).
  • New regression tests cover the reconnect-then-navigate-back repro at the store level (through the real setNode/OCC write path) and the component level (real pane-content.svelte mount).

Merging.

@mstomar125
mstomar125 merged commit a140206 into main Aug 14, 2026
@mstomar125
mstomar125 deleted the issue-1979-watchnodes-reconnect-stale-cache branch August 14, 2026 08:32
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.

AI chat nodes can render empty in the UI after a WatchNodes reconnect, despite intact database content

1 participant