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

45.0 KB
Raw Blame
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';

// The bridge memoizes its parsed bridge-map in a module-level variable
// (loadBridgeMap caches), so each test resets the module registry and
// re-imports a fresh copy to stay isolated. Mocks are declared with
// vi.mock (hoisted) so they apply to every fresh import; shared mock fns
// live in module scope so assertions can reach them after re-import.

let bridgeFileContents = null; // null = file absent
let pendingFileContents = null; // memory-resync-pending.json; null = absent
const isPendingPath = (path) => String(path).endsWith('memory-resync-pending.json');

const createMemory = vi.fn(async (data) => ({ id: `mem-${data.content?.slice(0, 6) || 'x'}` }));
const updateMemory = vi.fn(async () => ({ id: 'updated' }));
const updateMemoryEmbedding = vi.fn(async () => {});
const purgeMemory = vi.fn(async () => ({ success: true }));
const generateMemoryEmbedding = vi.fn(async () => [0.1, 0.2, 0.3]);
const getMemoryIdsMissingEmbedding = vi.fn(async () => new Set());
const getById = vi.fn();
const getAll = vi.fn(async () => []);
const listLiveIds = vi.fn(async () => []);
const getDigests = vi.fn(async () => []);
const getReviews = vi.fn(async () => []);
const listJournals = vi.fn(async () => ({ records: [] }));
const getJournal = vi.fn();
const writeThroughMock = async (filePath, data) => {
  const payload = (typeof data === 'string' || Buffer.isBuffer(data)) ? data : JSON.stringify(data, null, 2);
  const { writeFile } = await import('fs/promises');
  return writeFile(filePath, payload);
};
const atomicWrite = vi.fn(writeThroughMock);

vi.mock('fs', () => ({ existsSync: vi.fn(() => bridgeFileContents !== null) }));
vi.mock('fs/promises', () => ({
  readFile: vi.fn(async (path) => {
    if (!isPendingPath(path)) return bridgeFileContents ?? '{}';
    if (pendingFileContents === null) throw Object.assign(new Error('ENOENT'), { code: 'ENOENT' });
    return pendingFileContents;
  }),
  writeFile: vi.fn(async (path, data) => {
    if (isPendingPath(path)) pendingFileContents = data;
    else bridgeFileContents = data;
  }),
}));
vi.mock('../lib/fileUtils.js', () => ({
  PATHS: { brain: '/tmp/test-brain' },
  ensureDir: vi.fn(async () => {}),
  // Route atomic writes through the mocked fs/promises.writeFile so a reloaded
  // bridge sees the persisted map.
  atomicWrite,
}));
vi.mock('./memoryBackend.js', () => ({
  createMemory, updateMemory, updateMemoryEmbedding, purgeMemory, getMemoryIdsMissingEmbedding,
  getBrainMemoryLinks: vi.fn(async legacyMap => legacyMap),
  upsertBrainMemory: vi.fn(async (_key, data, embedding, legacyId) => {
    const updated = legacyId && await updateMemory(legacyId, { ...data, status: 'active' });
    if (!updated) return createMemory(data, embedding);
    if (embedding) await updateMemoryEmbedding(legacyId, embedding);
    return { ...updated, id: legacyId };
  }),
}));
vi.mock('./memoryEmbeddings.js', () => ({ generateMemoryEmbedding }));
vi.mock('./brainStorage.js', () => {
  const { EventEmitter } = require('events');
  return { brainEvents: new EventEmitter(), getById, getAll, listLiveIds, getDigests, getReviews };
});
vi.mock('./brainJournal.js', () => ({ listJournals, getJournal, iterateJournals: async function* () { const { records } = await listJournals(); yield* records; } }));

// Re-import a fresh bridge module (clears the cached bridge map).
async function loadBridge() {
  vi.resetModules();
  return import('./brainMemoryBridge.js');
}

beforeEach(async () => {
  vi.clearAllMocks();
  bridgeFileContents = null;
  pendingFileContents = null;
  // The mocked brainEvents emitter is created by the vi.mock factory, which
  // outlives vi.resetModules() — so each test's initBridge() stacks another
  // set of listeners on the SAME emitter, and one emit fans out to every
  // previous test's handlers. Existing tests only assert "not called" so they
  // never noticed; any call-count assertion does. Reset to one listener per test.
  const { brainEvents } = await import('./brainStorage.js');
  brainEvents.removeAllListeners();
});

describe('brainMemoryBridge — resyncBrainRecord (issue #1080)', () => {
  it('re-embeds a present, non-archived synced-in record', async () => {
    const bridge = await loadBridge();
    getById.mockResolvedValue({ id: 'p1', name: 'Alice', context: 'met at conf' });

    await bridge.resyncBrainRecord('people', 'p1');

    expect(getById).toHaveBeenCalledWith('people', 'p1');
    expect(createMemory).toHaveBeenCalledTimes(1); // no prior map → new vector
  });

  it('archives the mapped memory when the record is gone (tombstoned)', async () => {
    const bridge = await loadBridge();
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('people', 'p2')]: 'mem-existing' });
    getById.mockResolvedValue(null); // getById returns null for tombstones

    await bridge.resyncBrainRecord('people', 'p2');

    expect(updateMemory).toHaveBeenCalledWith('mem-existing', { status: 'archived' });
    expect(createMemory).not.toHaveBeenCalled();
  });

  it('archives the mapped memory when the record is archived', async () => {
    const bridge = await loadBridge();
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('projects', 'pr1')]: 'mem-proj' });
    getById.mockResolvedValue({ id: 'pr1', name: 'X', status: 'done', archived: true });

    await bridge.resyncBrainRecord('projects', 'pr1');

    expect(updateMemory).toHaveBeenCalledWith('mem-proj', { status: 'archived' });
  });

  it('reads journals via getJournal, not getById', async () => {
    const bridge = await loadBridge();
    getJournal.mockResolvedValue({ id: '2026-06-09', date: '2026-06-09', content: 'a good day' });

    await bridge.resyncBrainRecord('journals', '2026-06-09');

    expect(getJournal).toHaveBeenCalledWith('2026-06-09');
    expect(getById).not.toHaveBeenCalled();
    expect(createMemory).toHaveBeenCalledTimes(1);
  });

  it('no-ops for a type the bridge does not mirror (links/buckets/inbox)', async () => {
    const bridge = await loadBridge();

    await bridge.resyncBrainRecord('links', 'l1');
    await bridge.resyncBrainRecord('inbox', 'i1');

    expect(getById).not.toHaveBeenCalled();
    expect(createMemory).not.toHaveBeenCalled();
    expect(updateMemory).not.toHaveBeenCalled();
  });

  it('no archive call when a deleted record was never mapped', async () => {
    const bridge = await loadBridge();
    getById.mockResolvedValue(null);

    await bridge.resyncBrainRecord('people', 'never-seen');

    expect(updateMemory).not.toHaveBeenCalled();
    expect(createMemory).not.toHaveBeenCalled();
  });

  it('hard-deletes the mapped memory for a tombstoned record when hardDelete:true (local delete, #1318)', async () => {
    const bridge = await loadBridge();
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('people', 'p2')]: 'mem-existing' });
    getById.mockResolvedValue(null); // tombstoned

    await bridge.resyncBrainRecord('people', 'p2', { hardDelete: true });

    expect(purgeMemory).toHaveBeenCalledWith('mem-existing');
    expect(updateMemory).not.toHaveBeenCalled();
    expect(createMemory).not.toHaveBeenCalled();
  });

  it('soft-archives (never hard-deletes) a tombstoned record when hardDelete is absent (synced delete stays recoverable)', async () => {
    const bridge = await loadBridge();
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('people', 'p2')]: 'mem-existing' });
    getById.mockResolvedValue(null);

    await bridge.resyncBrainRecord('people', 'p2'); // sync:applied path — no hardDelete

    expect(updateMemory).toHaveBeenCalledWith('mem-existing', { status: 'archived' });
    expect(purgeMemory).not.toHaveBeenCalled();
  });

  it('soft-archives (never hard-deletes) a present-but-archived record even when hardDelete:true', async () => {
    const bridge = await loadBridge();
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('projects', 'pr1')]: 'mem-proj' });
    getById.mockResolvedValue({ id: 'pr1', name: 'X', status: 'done', archived: true });

    await bridge.resyncBrainRecord('projects', 'pr1', { hardDelete: true });

    // Archived ≠ deleted — keep it recoverable, do not drop the row.
    expect(updateMemory).toHaveBeenCalledWith('mem-proj', { status: 'archived' });
    expect(purgeMemory).not.toHaveBeenCalled();
  });

  it('no hard-delete call when a deleted record was never mapped', async () => {
    const bridge = await loadBridge();
    getById.mockResolvedValue(null);

    await bridge.resyncBrainRecord('people', 'never-seen', { hardDelete: true });

    expect(purgeMemory).not.toHaveBeenCalled();
    expect(updateMemory).not.toHaveBeenCalled();
  });

  it('reactivates an archived memory when its record comes back live (status:active)', async () => {
    const bridge = await loadBridge();
    // p1 is already mapped (memory was archived for a prior synced-in delete);
    // now the record resolves live again. The re-embed must reset status:active
    // or memory search (which filters status='active') keeps hiding it.
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('people', 'p1')]: 'mem-resurrect' });
    getById.mockResolvedValue({ id: 'p1', name: 'Alice', context: 'back again' });

    await bridge.resyncBrainRecord('people', 'p1');

    expect(updateMemory).toHaveBeenCalledWith('mem-resurrect', expect.objectContaining({ status: 'active' }));
    expect(createMemory).not.toHaveBeenCalled();
  });
});

describe('brainMemoryBridge — queueResync debounce + dedup', () => {
  it('dedups repeated touches of the same record and drops unmirrored types', async () => {
    const bridge = await loadBridge();
    getById.mockResolvedValue({ id: 'p1', name: 'Alice' });

    bridge.queueResync([
      { type: 'people', id: 'p1' },
      { type: 'people', id: 'p1' }, // duplicate — collapses
      { type: 'links', id: 'l1' },  // unmirrored — dropped
    ]);
    await bridge.flushPendingResync(); // flush deterministically

    expect(getById).toHaveBeenCalledTimes(1);
    expect(getById).toHaveBeenCalledWith('people', 'p1');
  });

  it('keeps a hard-delete sticky when a later soft touch coalesces onto the same key (#1318)', async () => {
    const bridge = await loadBridge();
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('people', 'p1')]: 'mem-del' });
    getById.mockResolvedValue(null); // still tombstoned at flush time

    // Local delete marks p1 for a hard prune; a concurrent sync:applied (no flag)
    // coalesces onto the same key before the flush. The hard intent must survive.
    bridge.queueResync([{ type: 'people', id: 'p1', hardDelete: true }]);
    bridge.queueResync([{ type: 'people', id: 'p1' }]);
    await bridge.flushPendingResync();

    expect(purgeMemory).toHaveBeenCalledWith('mem-del');
    expect(updateMemory).not.toHaveBeenCalledWith('mem-del', { status: 'archived' });
  });

  it('processes the queue sequentially across multiple records', async () => {
    const bridge = await loadBridge();
    getById.mockImplementation(async (_type, id) => ({ id, name: id }));

    bridge.queueResync([
      { type: 'people', id: 'a' },
      { type: 'projects', id: 'b' },
      { type: 'ideas', id: 'c' },
    ]);
    await bridge.flushPendingResync();

    expect(getById).toHaveBeenCalledTimes(3);
  });

  it('ignores a non-array payload without throwing', async () => {
    const bridge = await loadBridge();

    expect(() => bridge.queueResync(undefined)).not.toThrow();
    expect(() => bridge.queueResync(null)).not.toThrow();
    await bridge.flushPendingResync();

    expect(getById).not.toHaveBeenCalled();
  });

  it('single-flights overlapping flushes so a record is never embedded twice', async () => {
    const bridge = await loadBridge();
    // Slow embed for p1 — while the first flush is awaiting it, a second
    // sync:applied for the SAME not-yet-mapped record arrives. Without the
    // single-flight guard, a second flush would start and createMemory twice.
    let resolveFirst;
    const gate = new Promise((r) => { resolveFirst = r; });
    let calls = 0;
    getById.mockImplementation(async (_type, id) => {
      calls += 1;
      if (id === 'p1' && calls === 1) await gate; // hold the first resync open
      return { id, name: id };
    });

    bridge.queueResync([{ type: 'people', id: 'p1' }]);
    const inFlight = bridge.flushPendingResync(); // begins, awaits the gate on p1
    // Same record re-queued mid-flush + a direct flush attempt (the re-arm path).
    bridge.queueResync([{ type: 'people', id: 'p1' }]);
    const second = bridge.flushPendingResync(); // must early-return (guard), not run concurrently
    resolveFirst();
    await Promise.all([inFlight, second]);

    // p1 was mapped by the first create; the re-drain sees it mapped → update,
    // not a second create. Exactly one memory created for p1.
    expect(createMemory).toHaveBeenCalledTimes(1);
  });

  it('re-vectorizes when brainEvents emits sync:applied (end-to-end wiring)', async () => {
    const bridge = await loadBridge();
    const { brainEvents } = await import('./brainStorage.js');
    bridge.initBridge();
    getById.mockResolvedValue({ id: 'p1', name: 'Alice' });

    brainEvents.emit('sync:applied', { records: [{ type: 'people', id: 'p1' }] });
    await bridge.flushPendingResync();

    expect(getById).toHaveBeenCalledWith('people', 'p1');
    expect(createMemory).toHaveBeenCalledTimes(1);
  });
});

describe('brainMemoryBridge — entity :upserted/:deleted route through queueResync (bulk-create throttle)', () => {
  it('does NOT embed synchronously on a single :upserted — it enqueues for the debounced flush', async () => {
    const bridge = await loadBridge();
    const { brainEvents } = await import('./brainStorage.js');
    bridge.initBridge();
    getById.mockResolvedValue({ id: 'm1', title: 'note', content: 'hi' });

    brainEvents.emit('memories:upserted', { id: 'm1', record: { id: 'm1', title: 'note' } });
    // Before the flush nothing has been embedded — the burst is only queued.
    expect(getById).not.toHaveBeenCalled();
    expect(createMemory).not.toHaveBeenCalled();

    await bridge.flushPendingResync();
    // After the flush it re-reads canonical state and embeds exactly once.
    expect(getById).toHaveBeenCalledWith('memories', 'm1');
    expect(createMemory).toHaveBeenCalledTimes(1);
  });

  it('coalesces a tight burst of N :upserted events into N sequential resyncs (one per distinct id)', async () => {
    const bridge = await loadBridge();
    const { brainEvents } = await import('./brainStorage.js');
    bridge.initBridge();
    getById.mockImplementation(async (_type, id) => ({ id, title: id, content: id }));

    // Simulate the ChatGPT import creating many conversations back-to-back.
    for (let i = 0; i < 50; i++) {
      brainEvents.emit('memories:upserted', { id: `c${i}`, record: { id: `c${i}` } });
    }
    // A repeat touch of an already-queued id collapses (dedup).
    brainEvents.emit('memories:upserted', { id: 'c0', record: { id: 'c0' } });

    await bridge.flushPendingResync();

    // 50 distinct ids → 50 reads, NOT 51 — and they ran through the sequential
    // queue, not 51 concurrent fire-and-forget embeds.
    expect(getById).toHaveBeenCalledTimes(50);
    expect(createMemory).toHaveBeenCalledTimes(50);
  });

  it('routes a local :deleted through the resync path, HARD-deleting the mapped memory (issue #1318)', async () => {
    const bridge = await loadBridge();
    const { brainEvents } = await import('./brainStorage.js');
    bridge.initBridge();
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('people', 'p9')]: 'mem-del' });
    getById.mockResolvedValue(null); // deleted/tombstoned → getById returns null

    brainEvents.emit('people:deleted', { id: 'p9' });
    await bridge.flushPendingResync();

    // Real local delete → hard prune (drop row + embedding), NOT soft archive.
    expect(purgeMemory).toHaveBeenCalledWith('mem-del');
    expect(updateMemory).not.toHaveBeenCalledWith('mem-del', { status: 'archived' });
    expect(createMemory).not.toHaveBeenCalled();
  });

  it('drops the bridge-map entry after a local hard-delete so a re-create starts fresh', async () => {
    const bridge = await loadBridge();
    const { brainEvents } = await import('./brainStorage.js');
    bridge.initBridge();
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('people', 'p9')]: 'mem-del' });
    getById.mockResolvedValue(null);

    brainEvents.emit('people:deleted', { id: 'p9' });
    await bridge.flushPendingResync();

    const map = await bridge.loadBridgeMap();
    expect(map[bridge.bridgeKey('people', 'p9')]).toBeUndefined();
  });

  it('hard-deletes the mapped memory on a local journals:deleted (issue #1318)', async () => {
    const bridge = await loadBridge();
    const { brainEvents } = await import('./brainStorage.js');
    bridge.initBridge();
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('journals', '2026-06-09')]: 'mem-day' });

    brainEvents.emit('journals:deleted', { date: '2026-06-09', entry: { id: '2026-06-09' } });
    // handleJournalDeleted is async; flush the microtask queue.
    await new Promise((r) => setImmediate(r));

    expect(purgeMemory).toHaveBeenCalledWith('mem-day');
    expect(updateMemory).not.toHaveBeenCalled();
  });
});

// The daily-log editor autosaves, so 'journals:upserted' now fires every couple
// of seconds for the same day. Embedding per event would mean an embedding call
// per typing pause — and a full LLM summarization per event once the entry
// outgrows the embedding char budget.
describe('brainMemoryBridge — journal re-embed debounce (daily-log autosave)', () => {
  beforeEach(() => { vi.useFakeTimers(); });
  afterEach(() => { vi.useRealTimers(); });

  const emitSave = async (brainEvents, date, content) => {
    getJournal.mockResolvedValue({ id: date, date, content });
    brainEvents.emit('journals:upserted', { entry: { id: date, date, content } });
  };

  it('coalesces a writing session into one re-embed of the latest content', async () => {
    const bridge = await loadBridge();
    const { brainEvents } = await import('./brainStorage.js');
    bridge.initBridge();

    // 20 autosaves of the same day, 1.5s apart — a normal typing run.
    for (let i = 0; i < 20; i += 1) {
      await emitSave(brainEvents, '2026-06-09', `draft ${i}`);
      await vi.advanceTimersByTimeAsync(1500);
    }
    expect(generateMemoryEmbedding).not.toHaveBeenCalled();

    // Settle past the debounce, then drain.
    await vi.advanceTimersByTimeAsync(31_000);
    await bridge.flushPendingResync();

    expect(generateMemoryEmbedding).toHaveBeenCalledTimes(1);
    // Re-read canonical at flush, so it embeds the newest text, not the first.
    // (createMemory takes the payload plus the embedding vector.)
    expect(createMemory).toHaveBeenCalledWith(
      expect.objectContaining({ content: expect.stringContaining('draft 19') }),
      expect.anything(),
    );
  });

  it('re-embeds each day separately rather than collapsing across days', async () => {
    const bridge = await loadBridge();
    const { brainEvents } = await import('./brainStorage.js');
    bridge.initBridge();

    await emitSave(brainEvents, '2026-06-09', 'day one');
    await emitSave(brainEvents, '2026-06-10', 'day two');
    await vi.advanceTimersByTimeAsync(31_000);
    await bridge.flushPendingResync();

    expect(generateMemoryEmbedding).toHaveBeenCalledTimes(2);
  });

  it('drops a pending re-embed when the day is deleted', async () => {
    const bridge = await loadBridge();
    const { brainEvents } = await import('./brainStorage.js');
    bridge.initBridge();
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('journals', '2026-06-09')]: 'mem-day' });

    await emitSave(brainEvents, '2026-06-09', 'about to be deleted');
    brainEvents.emit('journals:deleted', { date: '2026-06-09', entry: { id: '2026-06-09' } });
    // handleJournalDeleted is async; flush microtasks (setImmediate is faked here).
    await vi.advanceTimersByTimeAsync(0);

    // The pending flush must not resurrect work against a tombstoned day.
    getJournal.mockResolvedValue(null);
    await vi.advanceTimersByTimeAsync(31_000);
    await bridge.flushPendingResync();

    expect(purgeMemory).toHaveBeenCalledWith('mem-day');
    expect(generateMemoryEmbedding).not.toHaveBeenCalled();
  });
});

describe('brainMemoryBridge — migration preview safety', () => {
  afterEach(() => {
    getAll.mockResolvedValue([]);
    listJournals.mockResolvedValue({ records: [] });
    getDigests.mockResolvedValue([]);
    getReviews.mockResolvedValue([]);
    getById.mockReset();
    getJournal.mockReset();
  });

  it('previews every source without writes or embeddings, even when refresh would archive an orphan', async () => {
    const bridge = await loadBridge();
    const person = { id: 'preview-person', name: 'Example Person' };
    const journal = { id: '2026-01-01', date: '2026-01-01', content: 'Example daily log' };
    getAll.mockImplementation(async type => type === 'people' ? [person] : []);
    listJournals.mockResolvedValue({ records: [journal] });
    getDigests.mockResolvedValue([{ id: 'preview-digest', digestText: 'Example digest' }]);
    getReviews.mockResolvedValue([]);
    getById.mockImplementation(async (_type, id) => id === person.id ? person : null);
    getJournal.mockResolvedValue(journal);
    const originalMap = JSON.stringify({
      [bridge.bridgeKey('people', 'deleted-person')]: 'mem-orphan',
    });
    bridgeFileContents = originalMap;

    expect(await bridge.syncAllBrainData({ dryRun: true, refresh: true })).toEqual({
      synced: 3, skipped: 0, errors: 0, archived: 0,
    });
    for (const write of [createMemory, updateMemory, purgeMemory, updateMemoryEmbedding,
      generateMemoryEmbedding, atomicWrite]) {
      expect(write).not.toHaveBeenCalled();
    }
    const { writeFile } = await import('fs/promises');
    expect(writeFile).not.toHaveBeenCalled();
    expect(bridgeFileContents).toBe(originalMap);

    // Execute the same fixture to prove the adapters and orphan are reachable.
    expect(await bridge.syncAllBrainData({ dryRun: false, refresh: true })).toEqual({
      synced: 3, skipped: 0, errors: 0, archived: 1,
    });
    expect(createMemory).toHaveBeenCalledTimes(3);
    expect(generateMemoryEmbedding).toHaveBeenCalledTimes(3);
    expect(updateMemory).toHaveBeenCalledWith('mem-orphan', { status: 'archived' });
    expect(atomicWrite).toHaveBeenCalled();
    expect(JSON.parse(bridgeFileContents)).toMatchObject({
      [bridge.bridgeKey('people', person.id)]: expect.any(String),
      [bridge.bridgeKey('journals', journal.id)]: expect.any(String),
      [bridge.bridgeKey('digests', 'preview-digest')]: expect.any(String),
    });
  });
});

describe('brainMemoryBridge — syncAllBrainData refresh mode (issue #1080 recovery)', () => {
  it('persists all newly created mappings once after a bulk sync', async () => {
    const bridge = await loadBridge();
    getAll.mockImplementation(async (type) => (type === 'people'
      ? [{ id: 'p1', name: 'Alice' }, { id: 'p2', name: 'Bob' }]
      : []));

    const stats = await bridge.syncAllBrainData();

    expect(stats.synced).toBe(2);
    expect(atomicWrite).toHaveBeenCalledTimes(1);
    expect(JSON.parse(bridgeFileContents)).toMatchObject({
      [bridge.bridgeKey('people', 'p1')]: expect.any(String),
      [bridge.bridgeKey('people', 'p2')]: expect.any(String),
    });
  });

  it('persists a replacement for a stale falsy mapping', async () => {
    const bridge = await loadBridge();
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('people', 'p1')]: null });
    getAll.mockImplementation(async (type) => (type === 'people'
      ? [{ id: 'p1', name: 'Alice' }]
      : []));

    await bridge.syncAllBrainData();

    expect(atomicWrite).toHaveBeenCalledTimes(1);
    expect(JSON.parse(bridgeFileContents)).toMatchObject({
      [bridge.bridgeKey('people', 'p1')]: expect.any(String),
    });
  });

  it('persists created mappings when a later bulk read fails', async () => {
    const bridge = await loadBridge();
    getAll.mockImplementation(async (type) => {
      if (type === 'people') return [{ id: 'p1', name: 'Alice' }];
      if (type === 'projects') throw new Error('store unavailable');
      return [];
    });

    await expect(bridge.syncAllBrainData()).rejects.toThrow('store unavailable');

    expect(atomicWrite).toHaveBeenCalledTimes(1);
    expect(JSON.parse(bridgeFileContents)).toMatchObject({
      [bridge.bridgeKey('people', 'p1')]: expect.any(String),
    });
  });

  it('skips already-mapped records by default but re-embeds them with refresh:true', async () => {
    const bridge = await loadBridge();
    getAll.mockImplementation(async (type) => (type === 'people' ? [{ id: 'p1', name: 'Alice' }] : []));
    // p1 already mapped — the pre-#1080 staleness scenario.
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('people', 'p1')]: 'mem-old' });

    const normal = await bridge.syncAllBrainData();
    expect(normal.skipped).toBeGreaterThanOrEqual(1);
    expect(updateMemory).not.toHaveBeenCalled();

    const refreshed = await bridge.syncAllBrainData({ refresh: true });
    expect(refreshed.synced).toBeGreaterThanOrEqual(1);
    expect(updateMemory).toHaveBeenCalledWith('mem-old', expect.any(Object));
  });

  it('refresh reconcile archives a mapped entry whose record was deleted on a peer pre-fix', async () => {
    const bridge = await loadBridge();
    // p-gone is mapped but its canonical record no longer resolves (deleted/
    // tombstoned on a peer before the fix) — getAll won't surface it, so only
    // the reconcile pass can retire its stale, still-searchable memory entry.
    getAll.mockResolvedValue([]);
    getById.mockResolvedValue(null);
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('people', 'p-gone')]: 'mem-orphan' });

    // Default mode does NOT reconcile (no archival).
    await bridge.syncAllBrainData();
    expect(updateMemory).not.toHaveBeenCalled();

    const refreshed = await bridge.syncAllBrainData({ refresh: true });
    expect(updateMemory).toHaveBeenCalledWith('mem-orphan', { status: 'archived' });
    expect(refreshed.archived).toBe(1);
  });

  it('refresh reconcile leaves live mapped records alone (no spurious archive)', async () => {
    const bridge = await loadBridge();
    getAll.mockImplementation(async (type) => (type === 'people' ? [{ id: 'p1', name: 'Alice' }] : []));
    getById.mockResolvedValue({ id: 'p1', name: 'Alice' }); // still live
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('people', 'p1')]: 'mem-live' });

    const refreshed = await bridge.syncAllBrainData({ refresh: true });

    // Re-embedded (update via syncBrainRecord), never archived.
    expect(refreshed.archived).toBe(0);
    expect(updateMemory).not.toHaveBeenCalledWith('mem-live', { status: 'archived' });
  });
});

describe('brainMemoryBridge — getEmbeddingCoverage (issue #3508)', () => {
  const ENTITY_TYPES = ['people', 'projects', 'ideas', 'admin', 'memories', 'songs'];

  beforeEach(() => {
    listLiveIds.mockImplementation(async () => []);
    getDigests.mockImplementation(async () => []);
    getReviews.mockImplementation(async () => []);
    getMemoryIdsMissingEmbedding.mockImplementation(async () => new Set());
  });

  it('tallies live ids without loading any record bodies', async () => {
    const bridge = await loadBridge();
    listLiveIds.mockImplementation(async (type) => {
      if (type === 'people') return ['p1', 'p2'];
      if (type === 'journals') return ['2026-01-01'];
      return [];
    });
    // p1 is embedded; p2 is mapped to a vector-less memory; the day is unmapped.
    bridgeFileContents = JSON.stringify({
      [bridge.bridgeKey('people', 'p1')]: 'mem-p1',
      [bridge.bridgeKey('people', 'p2')]: 'mem-p2',
    });
    getMemoryIdsMissingEmbedding.mockResolvedValue(new Set(['mem-p2']));

    expect(await bridge.getEmbeddingCoverage()).toEqual({ total: 3, missing: 2 });

    // The whole point of the fix: identity comes from the id index, never from
    // a full-body walk of every collection.
    expect(getAll).not.toHaveBeenCalled();
    expect(listJournals).not.toHaveBeenCalled();
    for (const type of [...ENTITY_TYPES, 'journals']) {
      expect(listLiveIds).toHaveBeenCalledWith(type);
    }
  });

  it('counts the append-only JSONL logs alongside the id-indexed stores', async () => {
    const bridge = await loadBridge();
    listLiveIds.mockImplementation(async (type) => (type === 'ideas' ? ['i1'] : []));
    getDigests.mockResolvedValue([{ id: 'd1' }]);
    getReviews.mockResolvedValue([{ id: 'r1' }]);
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('digests', 'd1')]: 'mem-d1' });

    expect(await bridge.getEmbeddingCoverage()).toEqual({ total: 3, missing: 2 });
  });

  it('treats an unreachable missing-embedding query as "nothing known missing"', async () => {
    const bridge = await loadBridge();
    listLiveIds.mockImplementation(async (type) => (type === 'admin' ? ['a1'] : []));
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('admin', 'a1')]: 'mem-a1' });
    getMemoryIdsMissingEmbedding.mockRejectedValue(new Error('backend down'));

    expect(await bridge.getEmbeddingCoverage()).toEqual({ total: 1, missing: 0 });
  });
});

describe('brainMemoryBridge — SongBook enrollment (issue #4105)', () => {
  const SONG = Object.freeze({
    id: 's1',
    title: 'Example Song',
    artist: 'Placeholder Band',
    instrument: 'guitar',
    stage: 'learning',
    key: 'G',
    tuning: 'Drop D',
    capo: 2,
    notes: 'Bridge needs work',
    tags: ['acoustic'],
    // The sheet body — deliberately NOT embedded (see composeSongContent).
    content: { format: 'chordpro', text: '[C]la '.repeat(20000) }
  });

  it('maps a song to a memory payload without its sheet body', async () => {
    const bridge = await loadBridge();

    const payload = bridge.brainRecordToMemory('songs', SONG);

    expect(payload.category).toBe('songbook');
    expect(payload.tags).toEqual(expect.arrayContaining(['acoustic', 'brain', 'songs']));
    expect(payload.content).toContain('Example Song');
    expect(payload.content).toContain('Placeholder Band');
    expect(payload.content).toContain('Stage: learning');
    expect(payload.content).toContain('Key: G');
    expect(payload.content).toContain('Capo: 2');
    expect(payload.content).toContain('Bridge needs work');
    // The 200k-char tab/ChordPro body is notation, not prose: embedding it adds
    // no semantic signal and would push the record into the embedder's
    // over-budget LLM-summarization branch.
    expect(payload.content).not.toContain('[C]la');
    expect(payload.content.length).toBeLessThan(1000);
  });

  it('omits the setup line when a song has no key/tuning and capo is the 0 default', async () => {
    const bridge = await loadBridge();

    // `capo: 0` is the schema default meaning "no capo" — not a capo at fret 0
    // — so it contributes nothing, and with no key/tuning the line disappears.
    const payload = bridge.brainRecordToMemory('songs', {
      id: 's2', title: 'Bare Song', capo: 0, content: { format: 'tab', text: '' }
    });

    expect(payload.content).toBe('Song: Bare Song');

    // A real capo still renders, so the omission above is the 0 default and not
    // the whole field being dropped.
    const capoed = bridge.brainRecordToMemory('songs', { id: 's3', title: 'Capoed', capo: 3 });
    expect(capoed.content).toBe('Song: Capoed\nCapo: 3');
  });

  it('re-embeds a song on :upserted through the debounced queue', async () => {
    const bridge = await loadBridge();
    const { brainEvents } = await import('./brainStorage.js');
    bridge.initBridge();
    getById.mockResolvedValue(SONG);

    brainEvents.emit('songs:upserted', { id: 's1', record: { id: 's1' } });
    expect(createMemory).not.toHaveBeenCalled(); // queued, not embedded inline

    await bridge.flushPendingResync();

    expect(getById).toHaveBeenCalledWith('songs', 's1');
    expect(createMemory).toHaveBeenCalledTimes(1);
  });

  it('hard-prunes the mapped memory when a song is deleted locally', async () => {
    const bridge = await loadBridge();
    const { brainEvents } = await import('./brainStorage.js');
    bridge.initBridge();
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('songs', 's1')]: 'mem-song' });
    getById.mockResolvedValue(null); // tombstoned

    brainEvents.emit('songs:deleted', { id: 's1' });
    await bridge.flushPendingResync();

    expect(purgeMemory).toHaveBeenCalledWith('mem-song');
  });

  it('includes songs in the bulk sync and the embedding-coverage tally', async () => {
    const bridge = await loadBridge();
    getAll.mockImplementation(async (type) => (type === 'songs' ? [SONG] : []));
    listLiveIds.mockImplementation(async (type) => (type === 'songs' ? ['s1'] : []));

    // Unmapped in a fresh bridge map → the tally counts it as a backfill
    // target rather than skipping the store entirely.
    expect(await bridge.getEmbeddingCoverage()).toEqual({ total: 1, missing: 1 });

    const stats = await bridge.syncAllBrainData();
    expect(stats.synced).toBe(1);
    expect(createMemory).toHaveBeenCalledTimes(1);

    // …and the bulk sync clears it.
    expect(await bridge.getEmbeddingCoverage()).toEqual({ total: 1, missing: 0 });
  });

  it('archives an orphaned song mapping in the refresh reconcile', async () => {
    const bridge = await loadBridge();
    getAll.mockResolvedValue([]);
    getById.mockResolvedValue(null);
    bridgeFileContents = JSON.stringify({ [bridge.bridgeKey('songs', 's-gone')]: 'mem-orphan' });

    const refreshed = await bridge.syncAllBrainData({ refresh: true });

    expect(updateMemory).toHaveBeenCalledWith('mem-orphan', { status: 'archived' });
    expect(refreshed.archived).toBe(1);
  });
});

describe('brain bridge durable identity (#11007)', () => {
  it('recovers after the memory commit succeeds but the cache save fails', async () => {
    const backend = await import('./memoryBackend.js');
    const durable = {};
    let inserted = 0;
    await backend.upsertBrainMemory.withImplementation(async key => {
      durable[key] ??= `durable-${++inserted}`;
      return { id: durable[key] };
    }, async () => {
      await backend.getBrainMemoryLinks.withImplementation(async () => ({ ...durable }), async () => {
        const first = await loadBridge();
        atomicWrite.mockRejectedValueOnce(new Error('simulated lost cache save'));
        await expect(first.syncBrainRecord('people', { id: 'example', name: 'Example' }))
          .rejects.toThrow('simulated lost cache save');
        // Reset only process state; the durable backend survived the crash.
        const restarted = await loadBridge();
        expect(await restarted.syncBrainRecord('people', { id: 'example', name: 'Updated example' }))
          .toBe('durable-1');
        expect(inserted).toBe(1);
        expect(await restarted.loadBridgeMap()).toEqual({ 'people:example': 'durable-1' });
      });
    });
  });

  it('rebuilds a corrupt cache from durable links before updating the record', async () => {
    const backend = await import('./memoryBackend.js');
    bridgeFileContents = '{broken';
    await backend.getBrainMemoryLinks.withImplementation(async () => ({ 'people:example': 'durable-1' }), async () => {
      const bridge = await loadBridge();
      await bridge.syncBrainRecord('people', { id: 'example', name: 'Example' });
      expect(createMemory).not.toHaveBeenCalled();
      expect(updateMemory).toHaveBeenCalledWith('durable-1', expect.objectContaining({ status: 'active' }));
      expect(JSON.parse(bridgeFileContents)).toEqual({ 'people:example': 'durable-1' });
    });
  });

  it('keeps a cold dry run write-free and still imports links on the subsequent write', async () => {
    const backend = await import('./memoryBackend.js');
    bridgeFileContents = JSON.stringify({ 'people:example': 'legacy-1' });
    const bridge = await loadBridge();
    await bridge.syncAllBrainData({ dryRun: true });
    expect(backend.getBrainMemoryLinks).toHaveBeenLastCalledWith(
      { 'people:example': 'legacy-1' }, { readOnly: true });
    expect(atomicWrite).not.toHaveBeenCalled();
    await bridge.syncBrainRecord('people', { id: 'example', name: 'Example' });
    expect(backend.getBrainMemoryLinks).toHaveBeenLastCalledWith(
      { 'people:example': 'legacy-1' }, { readOnly: false });
  });

  it('Embed missing heals a purged row even when the process already cached its old id', async () => {
    const backend = await import('./memoryBackend.js');
    bridgeFileContents = JSON.stringify({ 'people:example': 'purged-1' });
    const bridge = await loadBridge();
    await bridge.loadBridgeMap();
    getAll.mockImplementation(async type => type === 'people'
      ? [{ id: 'example', name: 'Example' }] : []);
    backend.getBrainMemoryLinks.mockResolvedValueOnce({});
    updateMemory.mockResolvedValueOnce(null);
    const stats = await bridge.syncAllBrainData({ onlyMissing: true });
    expect(stats.synced).toBe(1);
    expect(createMemory).toHaveBeenCalledTimes(1);
    expect(JSON.parse(bridgeFileContents)['people:example']).not.toBe('purged-1');
    expect(atomicWrite).toHaveBeenCalledTimes(1);
  });

  it('drains overlapping JSONL adds through the in-flight resync and preserves every cache key', async () => {
    const backend = await import('./memoryBackend.js');
    const { brainEvents } = await import('./brainStorage.js');
    const bridge = await loadBridge();
    bridge.initBridge();
    getById.mockResolvedValue({ id: 'example', name: 'Example' });
    getDigests.mockResolvedValue([
      { id: 'digest-one', digestText: 'First example digest' },
      { id: 'digest-two', digestText: 'Second example digest' },
    ]);
    let releaseWrite;
    let enteredWrite;
    const entered = new Promise(resolve => { enteredWrite = resolve; });
    const held = new Promise(resolve => { releaseWrite = resolve; });
    await createMemory.withImplementation(async data => ({ id: `memory-${data.content}` }), async () => {
      bridge.queueResync([{ type: 'people', id: 'example' }]);
      // Hold only the bridge map's first write; pending-set persists pass through.
      let holdNext = true;
      atomicWrite.mockImplementation(async (path, data) => {
        if (!holdNext || isPendingPath(path)) return writeThroughMock(path, data);
        holdNext = false;
        const snapshot = JSON.stringify(data);
        enteredWrite();
        await held;
        bridgeFileContents = snapshot;
      });
      const flush = bridge.flushPendingResync();
      await entered;
      try {
        brainEvents.emit('digests:added', { id: 'digest-one' });
        brainEvents.emit('digests:added', { id: 'digest-two' });
        brainEvents.emit('digests:added', { id: 'digest-one' });
        await bridge.flushPendingResync();
        expect(backend.upsertBrainMemory).toHaveBeenCalledTimes(1);
      } finally {
        releaseWrite();
        await flush;
      }
      expect(backend.upsertBrainMemory).toHaveBeenCalledTimes(3);
      expect(Object.keys(JSON.parse(bridgeFileContents)).sort()).toEqual([
        'digests:digest-one', 'digests:digest-two', 'people:example',
      ]);
    });
  });
});

describe('brain catch-up beyond old caps', () => {
  it('embeds the oldest journal beyond 10000 days', async () => {
    const bridge = await loadBridge();
    getAll.mockResolvedValue([]);
    getDigests.mockResolvedValue([]);
    getReviews.mockResolvedValue([]);
    const records = Array.from({ length: 10001 }, (_, i) => ({
      id: new Date(Date.UTC(2000, 0, i + 1)).toISOString().slice(0, 10),
      date: new Date(Date.UTC(2000, 0, i + 1)).toISOString().slice(0, 10),
      content: 'Journal day ' + i
    })).reverse();
    listJournals.mockResolvedValue({ records });
    expect((await bridge.syncAllBrainData({ onlyMissing: true })).synced).toBe(10001);
    // A second pass skips every mapped day, including the oldest.
    expect((await bridge.syncAllBrainData({ onlyMissing: true })).skipped).toBe(10001);
  });

  it('counts and embeds the oldest of 1001 digests and reviews', async () => {
    const bridge = await loadBridge();
    getAll.mockResolvedValue([]);
    listJournals.mockResolvedValue({ records: [] });
    listLiveIds.mockResolvedValue([]);
    const records = Array.from({ length: 1001 }, (_, i) => ({ id: 'record-' + i, digestText: 'Digest', reviewText: 'Review' }));
    getDigests.mockImplementation(async (limit = 10) => records.slice(0, limit));
    getReviews.mockImplementation(async (limit = 10) => records.slice(0, limit));
    expect(await bridge.getEmbeddingCoverage()).toEqual({ total: 2002, missing: 2002 });
    const stats = await bridge.syncAllBrainData({ onlyMissing: true });
    expect(stats.synced).toBe(2002);
    expect(getDigests).toHaveBeenCalledWith(Infinity);
    expect(getReviews).toHaveBeenCalledWith(Infinity);
    expect(await bridge.getEmbeddingCoverage()).toEqual({ total: 2002, missing: 0 });
  });
});

describe('brainMemoryBridge — pending resyncs survive a restart (#11756)', () => {
  const persisted = () => JSON.parse(pendingFileContents).entries.map(({ type, id, hardDelete }) => `${type}:${id}:${hardDelete}`);

  // The first process "dies" mid-batch; fake timers keep its debounce timer from
  // firing into a later test.
  beforeEach(() => vi.useFakeTimers());
  afterEach(() => vi.useRealTimers());

  it('finishes records left behind after the first one settled', async () => {
    const first = await loadBridge();
    let hang;
    getById.mockImplementation(async (_type, id) => {
      if (id === 'b') await new Promise((resolve) => { hang = resolve; }); // process dies here
      return { id, name: id };
    });

    first.queueResync([{ type: 'people', id: 'a' }, { type: 'people', id: 'b' }, { type: 'people', id: 'c' }]);
    first.flushPendingResync(); // never settles: b is in flight, c still queued
    await vi.waitFor(() => expect(hang).toBeTypeOf('function'));
    await first.flushPendingResyncWrites();
    expect(persisted()).toEqual(['people:b:false', 'people:c:false']); // a was removed once it settled

    getById.mockClear();
    getById.mockImplementation(async (_type, id) => ({ id, name: id }));
    const second = await loadBridge();
    expect(await second.replayPersistedResync()).toBe(2);
    await second.flushPendingResync();

    expect(getById.mock.calls.map(([, id]) => id)).toEqual(['b', 'c']);
    await second.flushPendingResyncWrites();
    expect(persisted()).toEqual([]);
  });

  it('archives a synced-in delete and hard-deletes a local one after the restart', async () => {
    const first = await loadBridge();
    first.queueResync([{ type: 'people', id: 'synced' }]);
    first.queueResync([{ type: 'people', id: 'local', hardDelete: true }]);
    first.queueResync([{ type: 'people', id: 'local' }]); // coalesced soft touch must not downgrade
    await first.flushPendingResyncWrites();

    const second = await loadBridge();
    bridgeFileContents = JSON.stringify({
      [second.bridgeKey('people', 'synced')]: 'mem-synced',
      [second.bridgeKey('people', 'local')]: 'mem-local',
    });
    getById.mockResolvedValue(null); // both tombstoned
    await second.replayPersistedResync();
    await second.flushPendingResync();

    expect(updateMemory).toHaveBeenCalledWith('mem-synced', { status: 'archived' });
    expect(purgeMemory).toHaveBeenCalledWith('mem-local');
    expect(purgeMemory).not.toHaveBeenCalledWith('mem-synced');
  });

  it('keeps an entry re-queued while its resync was in flight', async () => {
    const bridge = await loadBridge();
    let release;
    getById.mockImplementationOnce(async () => { await new Promise((resolve) => { release = resolve; }); return { id: 'a', name: 'a' }; });
    getById.mockResolvedValue({ id: 'a', name: 'a' });

    bridge.queueResync([{ type: 'people', id: 'a' }]);
    const flush = bridge.flushPendingResync();
    await vi.waitFor(() => expect(release).toBeTypeOf('function'));
    bridge.queueResync([{ type: 'people', id: 'a' }]); // edited again mid-resync
    release();
    await flush;
    await bridge.flushPendingResyncWrites();

    expect(getById).toHaveBeenCalledTimes(2);
    expect(persisted()).toEqual([]); // removed only after the re-queued pass settled
  });

  it('persists a journal day inside its debounce window and replays it', async () => {
    const first = await loadBridge();
    const { brainEvents } = await import('./brainStorage.js');
    first.initBridge();
    brainEvents.emit('journals:upserted', { entry: { id: '2026-01-02' } });
    await first.flushPendingResyncWrites();
    expect(persisted()).toEqual(['journals:2026-01-02:false']);

    getJournal.mockResolvedValue({ id: '2026-01-02', date: '2026-01-02', content: 'hello' });
    const second = await loadBridge();
    await second.replayPersistedResync();
    await second.flushPendingResync();

    expect(getJournal).toHaveBeenCalledWith('2026-01-02');
    expect(createMemory).toHaveBeenCalledTimes(1);
  });

  it('makes no embedding or store call at boot when nothing is pending', async () => {
    const absent = await loadBridge();
    expect(await absent.replayPersistedResync()).toBe(0);

    pendingFileContents = JSON.stringify({ entries: [] });
    const empty = await loadBridge();
    expect(await empty.replayPersistedResync()).toBe(0);

    pendingFileContents = '{not json';
    const corrupt = await loadBridge();
    expect(await corrupt.replayPersistedResync()).toBe(0);
    await corrupt.flushPendingResync();

    expect(generateMemoryEmbedding).not.toHaveBeenCalled();
    expect(getById).not.toHaveBeenCalled();
    expect(atomicWrite).not.toHaveBeenCalled();
  });
});