Repository navigation
Re-confirm cached nodes on daemon reconnect instead of trusting presence forever - #2084
Conversation
…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]>
|
Adversarial review complete. Two IMPORTANT findings, both addressed:
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 Full suite + quality:fix re-run clean after the fixes; pushed in 579a1a8. |
|
APPROVE (self-review — GitHub blocks a native self-approval on your own PR, posting as a comment instead) Independent re-verification complete:
Merging. |
Summary
AI chat conversations (and, more generally, any node) could render as empty/stale in the UI after a daemon restart triggered a
WatchNodesreconnect, 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
WatchNodesbridge (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 — anynode:created/node:updated/node:deletedevents that would have landed during the outage are gone, not delayed.pane-content.svelte'shydrateNode()(which ensures a tab's node is loaded intosharedNodeStorebefore its viewer mounts) and itsonDaemonReconnectretry 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 whileWatchNodeswas 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
sharedNodeStorereconnect-handling gap called out in the issue (notai-chat-specific) — any node type reachable viaensureNodewas affected identically.Fix
Add reconnect-generation tracking to
SharedNodeStore:reconnectGenerationis bumped once per daemon reconnect (wired via the existingonDaemonReconnecthook — the same idiomschemasStore/collectionsDataalready use to reload on reconnect, but applied lazily per-node instead of eagerly reloading everything).nodeGeneration).isPossiblyStale(nodeId)reportstruewhen 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 existingsetNode()→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'shydrateNode()now gates on presence and freshness; itsonDaemonReconnecthandler no longer duplicates a presence-only check — it unconditionally delegates tohydrateNode, 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.svelteneeded no changes: it already readssharedNodeStore.getNode()reactively, so it updates automatically once the re-fetch resolves.Testing
shared-node-store-reconnect-staleness.test.ts: store-level coverage ofisPossiblyStale/markPossiblyStaleAfterReconnect/ensureNode, including the reconnect-then-navigate-back repro end-to-end through the realsetNodewrite path, a failed re-confirm leaving the node stale for retry, and concurrentensureNodecalls de-duping to one fetch.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.tsfake-timer leak frombrowser-sync-service.ts, and acore-plugins.test.tstimeout — reproduce identically on unmodifiedmain, unrelated to this change.)bun run quality:fix— clean (0 eslint errors/warnings,tsc --noEmitclean,cargo clippy --all-targets -D warningsclean — no Rust files touched).Closes #1979