Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: invalidate Brain projections after live restores

Adam Eivy1f9a7a0ccf63
diff --git a/server/services/brainSearchIndex.js b/server/services/brainSearchIndex.js
index 407c348550..3c88411782 100644
--- a/server/services/brainSearchIndex.js
+++ b/server/services/brainSearchIndex.js
@@ -21,7 +21,7 @@
* payloads. The Daily Log projects a body-presence predicate and a bounded
* summary, so full `content`/`segments` never enter the cache.
*
- * FRESHNESS — three signal classes, all of them covered:
+ * FRESHNESS — local writes, peer writes, and whole-store replacements:
* 1. `${type}:upserted` / `${type}:deleted` — the per-record events every
* local write path emits (create/update/updateWith/updateMany/remove, and
* upsertWithId in its default mode). Patched incrementally, no re-scan.
@@ -30,7 +30,10 @@
* (`applyRemoteRecord`, silent to prevent the #1077 cross-peer echo) and
* `upsertWithId({ emitEvent: false })`. Without it an inbound sync or
* anti-entropy reconcile would leave this cache stale indefinitely.
- * 3. Nothing else writes brain entity records — `pruneTombstones` only hard-
+ * 3. `caches:invalidated` — live backup restores replace files directly, then
+ * call `brainStorage.invalidateAllCaches`. Drop every projection and bump
+ * generations so an older in-flight scan cannot repopulate the cache.
+ * Nothing else writes brain entity records — `pruneTombstones` only hard-
* prunes tombstones, which `getAll` strips and which therefore were never
* in the projection, and `backfillOriginInstanceId` runs once at boot and
* touches no projected field.
@@ -240,6 +243,9 @@ for (const type of BRAIN_PROJECTION_TYPES) {
brainEvents.on(`${type}:deleted`, ({ id } = {}) => patchDelete(type, id));
}
brainEvents.on('record:changed', ({ type } = {}) => invalidate(type));
+brainEvents.on('caches:invalidated', () => {
+ for (const type of BRAIN_PROJECTION_TYPES) invalidate(type);
+});
/** Test hook — drop every projection and reset the in-flight/generation state. */
export function __resetBrainSearchIndex() {
diff --git a/server/services/brainStorage.js b/server/services/brainStorage.js
index 2c6fdf4c14..be4ff6079f 100644
--- a/server/services/brainStorage.js
+++ b/server/services/brainStorage.js
@@ -1504,6 +1504,9 @@ export function invalidateAllCaches() {
index.generation[type] += 1;
}
}
+ // Live restores replace files without record events. Drop derived caches too,
+ // without turning cache invalidation into a write or federation announcement.
+ brainEvents.emit('caches:invalidated');
}
/**
diff --git a/server/services/brainStorage.test.js b/server/services/brainStorage.test.js
index cfcadb9787..0f1deff9ab 100644
--- a/server/services/brainStorage.test.js
+++ b/server/services/brainStorage.test.js
@@ -48,6 +48,7 @@ vi.mock('./brainSyncLog.js', async () => {
});
import * as brainStorage from './brainStorage.js';
+import { getBrainProjections } from './brainSearchIndex.js';
import * as brainSyncLog from './brainSyncLog.js';
import { applyRemoteChanges } from './brainSync.js';
import { applyBrainSnapshot, relayUnloggedRecords } from './brainReconcile.js';
@@ -63,6 +64,23 @@ beforeEach(() => {
const ISO = (s) => new Date(s).toISOString();
+it('refreshes search and graph projections after a live file restore', async () => {
+ const journalId = 'restore-journal';
+ const personId = 'restore-person';
+ await brainStorage.upsertWithId('journals', journalId, { content: 'Before restore' });
+ await brainStorage.upsertWithId('people', personId, { name: 'Before restore' });
+ expect((await getBrainProjections('journals')).find(row => row.id === journalId)?.summary).toBe('Before restore');
+ expect((await getBrainProjections('people')).find(row => row.id === personId)?.name).toBe('Before restore');
+
+ // Backup restores replace files directly, without per-record write events.
+ writeFileSync(join(getTempRoot(), 'brain', 'journals', journalId, 'index.json'), JSON.stringify({ id: journalId, content: 'Restored journal' }));
+ writeFileSync(join(getTempRoot(), 'brain', 'people', personId, 'index.json'), JSON.stringify({ id: personId, name: 'Restored person' }));
+ brainStorage.invalidateAllCaches();
+
+ expect((await getBrainProjections('journals')).find(row => row.id === journalId)?.summary).toBe('Restored journal');
+ expect((await getBrainProjections('people')).find(row => row.id === personId)?.name).toBe('Restored person');
+});
+
describe('brainStorage tombstones', () => {
it('remove() writes a tombstone instead of hard-deleting, and hides it from reads', async () => {
const created = await brainStorage.create('people', { name: 'Alice' });