Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

49.4 KB
Raw Blame
/**
 * Precomputed SessionStart context (liveness plan, Phase 6).
 *
 * - fillContextPlaceholders is the one fill both the hook and the live route use,
 *   so a cached block filled at T is byte-identical to the live answer at T.
 * - Every write point invalidates and the block re-renders within the debounce.
 * - The hook serves a cached block without calling the worker, falls back to
 *   the live path on a miss or a stale (>24h) file, and never leaks placeholders.
 */
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, mock } from 'bun:test';
import { Database } from 'bun:sqlite';
import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';

import * as realHookSettings from '../../src/shared/hook-settings.js';
import * as realProjectName from '../../src/utils/project-name.js';
import * as realWorkerUtils from '../../src/shared/worker-utils.js';

import {
  CONTEXT_CACHE_MAX_AGE_MS,
  CONTEXT_PLACEHOLDER_NONCE,
  contextCacheDir,
  contextCacheFilePath,
  contextCacheKeys,
  fillContextPlaceholders,
  readContextCache,
  readContextProjectCatalog,
  refreshContextProjectCatalog,
  relativeTimePlaceholder,
  saveContextProjectCatalog,
  writeContextCache,
} from '../../src/shared/context-cache.js';
import { emitContextInvalidation } from '../../src/shared/context-invalidation.js';
import { formatHeaderDateTime } from '../../src/shared/timeline-formatting.js';
import { describeDuration, recordObserverFailure, recordObserverSuccess } from '../../src/shared/observer-health.js';
import { writeSyncHealth } from '../../src/shared/sync-health.js';
import { memoryInstructionsForRuntime } from '../../src/shared/memory-instructions.js';
const MEMORY_PLUGIN_INSTRUCTIONS = memoryInstructionsForRuntime('worker');
import { ContextCacheService, type ContextVariantRender } from '../../src/services/worker/ContextCacheService.js';
import { SessionStore } from '../../src/services/sqlite/SessionStore.js';
import { SyncApply, type SyncOp } from '../../src/services/sync/SyncApply.js';
import { DataRoutes } from '../../src/services/worker/http/routes/DataRoutes.js';

const realHookSettingsSnapshot = { ...realHookSettings };
const realProjectNameSnapshot = { ...realProjectName };
const realWorkerUtilsSnapshot = { ...realWorkerUtils };

const PLACEHOLDER_MARKER = '⟦CMEM_';
// Blocks cached before the header became the one Rules + Tools line carry this.
const LEGACY_HEADER_TIME_PLACEHOLDER = `⟦CMEM_NOW_HEADER:${CONTEXT_PLACEHOLDER_NONCE}⟧`;

describe('fillContextPlaceholders', () => {
  const nowEpochMs = 1_790_000_000_000;

  it('fills a legacy cached header time and every relative duration', () => {
    const body = [
      `# [proj] recent context, ${LEGACY_HEADER_TIME_PLACEHOLDER}`,
      `- release: status=active, updated ${relativeTimePlaceholder(nowEpochMs - 3 * 3_600_000)} ago`,
      `  - [todo] publish, updated ${relativeTimePlaceholder(nowEpochMs - 5 * 60_000)} ago`,
    ].join('\n');

    expect(fillContextPlaceholders(body, nowEpochMs)).toBe([
      `# [proj] recent context, ${formatHeaderDateTime(new Date(nowEpochMs))}`,
      '- release: status=active, updated about 3 hours ago',
      '  - [todo] publish, updated 5 minutes ago',
    ].join('\n'));
  });

  it('never leaves a placeholder behind', () => {
    const body = `${LEGACY_HEADER_TIME_PLACEHOLDER} ${relativeTimePlaceholder(1)} ${relativeTimePlaceholder(nowEpochMs)}`;
    expect(fillContextPlaceholders(body, nowEpochMs)).not.toContain(PLACEHOLDER_MARKER);
  });

  it('leaves placeholder look-alikes from memory text untouched', () => {
    // An observation can quote anything, including a placeholder's shape; only
    // placeholders carrying the rendering nonce are filled.
    const injected = `⟦CMEM_NOW_HEADER:000000000000⟧ ⟦CMEM_AGO:000000000000:0000000000001⟧ ⟦CMEM_AGO:99999999999999999999⟧`;
    expect(fillContextPlaceholders(injected, nowEpochMs)).toBe(injected);
    const userCopiedRealPlaceholder = `${LEGACY_HEADER_TIME_PLACEHOLDER} ${relativeTimePlaceholder(1)}`;
    expect(fillContextPlaceholders(userCopiedRealPlaceholder, nowEpochMs, 'abcdefabcdef')).toBe(userCopiedRealPlaceholder);
    expect(() => fillContextPlaceholders('x', nowEpochMs, '../not-a-nonce')).toThrow();
  });

  it('keeps fitted text within budget once filled', () => {
    // A relative-time placeholder is never shorter than the duration it becomes.
    for (const ageMs of [0, 59_000, 3_600_000, 47 * 3_600_000, 400 * 86_400_000, nowEpochMs]) {
      expect(relativeTimePlaceholder(nowEpochMs - ageMs).length)
        .toBeGreaterThanOrEqual(describeDuration(ageMs).length);
    }
  });
});

describe('readContextCache', () => {
  const keys = contextCacheKeys(['cache-read-parent', 'cache-read-repo'], 'claude', false);
  afterEach(() => rmSync(contextCacheFilePath(keys), { force: true }));

  it('returns a fresh file', () => {
    writeContextCache(keys, 'BODY', 1_000);
    expect(readContextCache(keys, 1_000 + CONTEXT_CACHE_MAX_AGE_MS)?.body).toBe('BODY');
  });

  it('treats a file older than 24h as a miss', () => {
    writeContextCache(keys, 'BODY', 1_000);
    expect(readContextCache(keys, 1_000 + CONTEXT_CACHE_MAX_AGE_MS + 1)).toBeNull();
  });

  it('treats a missing or unreadable file as a miss', () => {
    expect(readContextCache(keys, 1_000)).toBeNull();
    writeFileSync(contextCacheFilePath(keys), '{not json');
    expect(readContextCache(keys, 1_000)).toBeNull();
  });

  it('keys each variant separately', () => {
    writeContextCache(keys, 'AGENT', 1_000);
    expect(readContextCache(contextCacheKeys(['cache-read-parent', 'cache-read-repo'], 'claude', true), 1_000)).toBeNull();
    expect(readContextCache(contextCacheKeys(['cache-read-parent', 'cache-read-repo'], undefined, false), 1_000)).toBeNull();
    expect(readContextCache(contextCacheKeys([' cache-read-parent', 'cache-read-repo '], 'claude', false), 1_000)).toBeNull();
  });
});

describe('ContextCacheService invalidation', () => {
  const DEBOUNCE_MS = 20;
  const project = 'invalidate-proj';
  let service: ContextCacheService;
  let renders: string[];
  let renderCount: number;
  let tempDir: string;
  /** When set, each re-render waits on it (an in-flight render a removal can overtake). */
  let renderGate: Promise<void> | null;

  const keys = contextCacheKeys([project], 'claude', false);
  const otherKeys = contextCacheKeys(['unrelated-proj'], 'claude', false);
  /**
   * Let the debounce timer fire, then wait for the renders it queued. A fixed
   * sleep alone raced the sequential per-variant renders under a loaded full-suite
   * run (CI saw only the first of two variants rendered).
   */
  const waitPastDebounce = async () => {
    await new Promise(resolve => setTimeout(resolve, DEBOUNCE_MS * 4));
    await service.flushPendingRenders();
  };

  beforeEach(async () => {
    tempDir = mkdtempSync(join(tmpdir(), 'claude-mem-context-cache-'));
    renders = [];
    renderCount = 0;
    renderGate = null;
    service = new ContextCacheService({
      debounceMs: DEBOUNCE_MS,
      renderVariant: async (variantKeys): Promise<ContextVariantRender> => {
        if (renderGate) await renderGate;
        renderCount++;
        renders.push(variantKeys.projects.join(','));
        return { body: `render #${renderCount} ${relativeTimePlaceholder(Date.now())} ago`, cacheable: true };
      },
      // As projectReadKeys would: a project merged into this one, stored in other casing.
      expandProjectReadKeys: projects => projects.includes(project) ? [...projects, 'Merged-Into-Invalidate-Proj'] : projects,
    });
    service.start();
    service.recordLiveRender(keys, { body: 'live', cacheable: true }, Date.now());
    service.recordLiveRender(otherKeys, { body: 'other', cacheable: true }, Date.now());
    // A started service re-renders every variant the index remembers; let that settle first.
    await service.flushPendingRenders();
    renders = [];
  });

  afterAll(() => rmSync(contextCacheDir(), { recursive: true, force: true }));

  afterEach(async () => {
    await service.flushPendingRenders();
    service.stop();
    rmSync(contextCacheFilePath(keys), { force: true });
    rmSync(contextCacheFilePath(otherKeys), { force: true });
    rmSync(tempDir, { recursive: true, force: true });
  });

  async function expectRerenderedOnlyFor(write: () => void | Promise<void>, expectedProjects: string[] = [project]): Promise<void> {
    await write();
    expect(renders).toEqual([]);
    await waitPastDebounce();
    expect(renders.sort()).toEqual([...expectedProjects].sort());
    expect(readContextCache(keys, Date.now())?.body).toStartWith('render #');
  }

  function memoryStore(): SessionStore {
    const store = new SessionStore(':memory:');
    const sessionDbId = store.createSDKSession('content-1', project, 'prompt');
    store.ensureMemorySessionIdRegistered(sessionDbId, 'memory-1');
    return store;
  }

  const observation = {
    type: 'discovery', title: 'A title', subtitle: null, facts: [], narrative: 'n',
    concepts: [], files_read: [], files_modified: [],
  };

  it('SessionStore.storeObservations', async () => {
    const store = memoryStore();
    await expectRerenderedOnlyFor(() => {
      store.storeObservations('memory-1', project, [observation], null);
    });
    store.close();
  });

  it('SessionStore.storeSummary', async () => {
    const store = memoryStore();
    await expectRerenderedOnlyFor(() => {
      store.storeSummary('memory-1', project, {
        request: 'r', investigated: 'i', learned: 'l', completed: 'c', next_steps: 'n', notes: null,
      });
    });
    store.close();
  });

  it('a local summary replacement stops serving the old next steps immediately', async () => {
    const store = memoryStore();
    const summary = {
      request: 'Publish release', investigated: 'release status', learned: 'ready',
      completed: 'prepared', next_steps: 'publish the release', notes: null,
    };
    try {
      const initialBody = readContextCache(keys, Date.now())?.body;
      const first = store.storeObservations('memory-1', project, [], { ...summary }, 1);
      expect(first.summaryUpdated).toBe(false);
      // A new summary is still additive; the previous block remains usable.
      expect(readContextCache(keys, Date.now())?.body).toBe(initialBody);
      await waitPastDebounce();
      service.recordLiveRender(keys, { body: summary.next_steps, cacheable: true }, Date.now());
      const unrelatedBody = readContextCache(otherKeys, Date.now())?.body;

      const revised = store.storeObservations('memory-1', project, [], {
        ...summary, completed: 'published', next_steps: '',
      }, 1);
      expect(revised.summaryUpdated).toBe(true);
      expect(revised.summaryId).toBe(first.summaryId);
      expect(store.db.prepare('SELECT completed, next_steps FROM session_summaries WHERE id = ?')
        .get(first.summaryId)).toEqual({ completed: 'published', next_steps: '' });
      expect(readContextCache(keys, Date.now())).toBeNull();
      expect(readContextCache(otherKeys, Date.now())?.body).toBe(unrelatedBody);
      await waitPastDebounce();
      expect(readContextCache(keys, Date.now())?.body).toStartWith('render #');
    } finally { store.close(); }
  });

  it('SessionStore.appendWorkStateEntry', async () => {
    const store = memoryStore();
    await expectRerenderedOnlyFor(() => {
      store.appendWorkStateEntry({ project, listName: 'release', fields: { status: 'active' } });
    });
    store.close();
  });

  it('SessionStore.importObservation (matched through a read key, case-insensitively)', async () => {
    const store = memoryStore();
    await expectRerenderedOnlyFor(() => {
      store.importObservation({
        memory_session_id: 'memory-1', project: 'merged-into-invalidate-proj', text: null, type: 'discovery',
        title: 'Imported', subtitle: null, facts: null, narrative: null, concepts: null, files_read: null,
        files_modified: null, prompt_number: 1, discovery_tokens: 0, created_at: new Date().toISOString(),
        created_at_epoch: Date.now(),
      } as any);
    });
    store.close();
  });

  it('SessionStore.importSessionSummary', async () => {
    const store = memoryStore();
    await expectRerenderedOnlyFor(() => {
      store.importSessionSummary({
        memory_session_id: 'memory-1', project, request: 'r', investigated: null, learned: null, completed: null,
        next_steps: null, files_read: null, files_edited: null, notes: null, prompt_number: 1, discovery_tokens: 0,
        created_at: new Date().toISOString(), created_at_epoch: Date.now(),
      } as any);
    });
    store.close();
  });

  it('SyncApply.applyOps', async () => {
    const db = new Database(':memory:');
    new SessionStore(db);
    const apply = new SyncApply(db, { deviceId: 'device-self', now: () => Date.now() });
    const body = {
      memory_session_id: 'mem-remote', project: 'remote-proj', text: null, type: 'discovery', title: 'Remote',
      subtitle: null, facts: '[]', narrative: 'n', concepts: '[]', files_read: '[]', files_modified: '[]',
      prompt_number: 1, discovery_tokens: 0, content_hash: 'h', generated_by_model: null, agent_type: null,
      agent_id: null, metadata: null, merged_into_project: null, created_at: new Date().toISOString(),
      created_at_epoch: Date.now(),
    };
    const op: SyncOp = {
      seq: '1', kind: 'observation', origin_device: 'device-remote', origin_id: '11', rev: '1',
      body: JSON.stringify(body), server_ts: Date.now(),
    };
    // A pulled op can re-key projects (remap mutations), so every variant re-renders.
    await expectRerenderedOnlyFor(() => {
      expect(apply.applyOps([op]).applied).toBe(1);
    }, [project, 'unrelated-proj']);
    db.close();
  });

  it('DataRoutes observation delete', async () => {
    const store = memoryStore();
    const { observationIds } = store.storeObservations('memory-1', project, [observation], null);
    await waitPastDebounce();
    renders = [];
    let deleteHandler: ((req: any, res: any) => void) | undefined;
    const routes = new DataRoutes(
      {} as any,
      { getSessionStore: () => store, getCloudSync: () => null, getChromaSync: () => null } as any,
      {} as any,
      { broadcast: () => {} } as any,
      {} as any,
      Date.now(),
    );
    routes.setupRoutes({
      get: () => {}, post: () => {}, use: () => {},
      delete: (path: string, handler: (req: any, res: any) => void) => {
        if (path === '/api/observation/:id') deleteHandler = handler;
      },
    } as any);
    const res = { json: mock(() => {}), status: mock(() => res), setHeader: () => {}, headersSent: false };
    await expectRerenderedOnlyFor(() => {
      deleteHandler!({ params: { id: String(observationIds[0]) }, query: {} }, res);
      expect(res.json).toHaveBeenCalledWith(expect.objectContaining({ success: true }));
    }, [project, 'unrelated-proj']);
    store.close();
  });

  it('DataRoutes observation delete removes the cached file before the route responds', async () => {
    const store = memoryStore();
    const { observationIds } = store.storeObservations('memory-1', project, [observation], null);
    await waitPastDebounce();
    expect(existsSync(contextCacheFilePath(keys))).toBe(true);
    let deleteHandler: ((req: any, res: any) => void) | undefined;
    const routes = new DataRoutes(
      {} as any,
      { getSessionStore: () => store, getCloudSync: () => null, getChromaSync: () => null } as any,
      {} as any,
      { broadcast: () => {} } as any,
      {} as any,
      Date.now(),
    );
    routes.setupRoutes({
      get: () => {}, post: () => {}, use: () => {},
      delete: (path: string, handler: (req: any, res: any) => void) => {
        if (path === '/api/observation/:id') deleteHandler = handler;
      },
    } as any);
    let fileExistedWhenResponded: boolean | null = null;
    const res = {
      json: mock(() => { fileExistedWhenResponded = existsSync(contextCacheFilePath(keys)); }),
      status: mock(() => res), setHeader: () => {}, headersSent: false,
    };
    deleteHandler!({ params: { id: String(observationIds[0]) }, query: {} }, res);
    expect(res.json).toHaveBeenCalledWith(expect.objectContaining({ success: true }));
    // Inside the debounce window: no re-render yet, and nothing stale to serve.
    expect(fileExistedWhenResponded).toBe(false);
    expect(existsSync(contextCacheFilePath(otherKeys))).toBe(false);
    await waitPastDebounce();
    expect(readContextCache(keys, Date.now())?.body).toStartWith('render #');
    store.close();
  });

  it('an additive write keeps serving the cached file until the re-render', async () => {
    emitContextInvalidation({ projects: [project] }, 'storeObservations');
    expect(existsSync(contextCacheFilePath(keys))).toBe(true);
    await waitPastDebounce();
    expect(renders).toEqual([project]);
  });

  it('a pulled tombstone (SyncApply) removes the cached files synchronously', async () => {
    const db = new Database(':memory:');
    new SessionStore(db);
    const apply = new SyncApply(db, { deviceId: 'device-self', now: () => Date.now() });
    const tombstone: SyncOp = {
      seq: '1', kind: 'observation', origin_device: 'device-remote', origin_id: '42', rev: '2',
      body: '{}', server_ts: Date.now(), entity_id: 'entity-42', entity_rev: '2',
      operation_sha256: 'a'.repeat(64), deleted: true, deleted_at: new Date().toISOString(),
    };
    expect(apply.applyOps([tombstone]).applied).toBe(1);
    expect(existsSync(contextCacheFilePath(keys))).toBe(false);
    expect(existsSync(contextCacheFilePath(otherKeys))).toBe(false);
    await waitPastDebounce();
    expect(readContextCache(keys, Date.now())?.body).toStartWith('render #');
    db.close();
  });

  for (const kind of ['observation', 'summary', 'prompt'] as const) {
    it(`a pulled ${kind} revision stops serving the replaced context before re-rendering`, async () => {
      const db = new Database(':memory:');
      new SessionStore(db);
      const apply = new SyncApply(db, { deviceId: 'device-self' });
      const createdAtEpoch = Date.now();
      const base = { memory_session_id: 'mem-remote', project, created_at_epoch: createdAtEpoch };
      const body = kind === 'observation' ? { ...base, type: 'discovery', title: 'old text' }
        : kind === 'summary' ? { ...base, request: 'old text' }
          : { ...base, content_session_id: 'content-remote', platform_source: 'claude', prompt_number: 1, prompt_text: 'old text' };
      const field = kind === 'observation' ? 'title' : kind === 'summary' ? 'request' : 'prompt_text';
      const rowOp: SyncOp = {
        seq: '1', kind, origin_device: 'device-remote', origin_id: '11', rev: '1',
        body: JSON.stringify(body), server_ts: createdAtEpoch,
        entity_id: `entity-${kind}`, entity_rev: '1', operation_sha256: `hash-${kind}-1`,
      };
      try {
        expect(apply.applyOps([rowOp]).applied).toBe(1);
        await waitPastDebounce();
        service.recordLiveRender(keys, { body: 'old text', cacheable: true }, Date.now());
        expect(apply.applyOps([{ ...rowOp, seq: '2', rev: '2', entity_rev: '2', operation_sha256: `hash-${kind}-2`,
          body: JSON.stringify({ ...body, [field]: 'corrected text' }) }]).applied).toBe(1);
        const table = kind === 'observation' ? 'observations' : kind === 'summary' ? 'session_summaries' : 'user_prompts';
        expect(db.prepare(`SELECT ${field} AS text FROM ${table}`).get()).toEqual({ text: 'corrected text' });
        expect(readContextCache(keys, Date.now())).toBeNull();
        expect(existsSync(contextCacheFilePath(otherKeys))).toBe(false);
        await waitPastDebounce();
        expect(readContextCache(keys, Date.now())?.body).toStartWith('render #');
      } finally {
        db.close();
      }
    });
  }

  it('a constraint-blocked revision stops serving old context when a later pull applies it', async () => {
    const db = new Database(':memory:');
    new SessionStore(db);
    const apply = new SyncApply(db, { deviceId: 'device-self' });
    const createdAtEpoch = Date.now();
    const rowOp = (seq: string, id: string, rev: string, title: string, hash: string): SyncOp => ({
      seq, kind: 'observation', origin_device: 'device-remote', origin_id: id, rev,
      entity_id: `entity-${id}`, entity_rev: rev, operation_sha256: `hash-${id}-${rev}`,
      body: JSON.stringify({ memory_session_id: 'mem-remote', project, type: 'discovery', title,
        content_hash: hash, created_at_epoch: createdAtEpoch }), server_ts: createdAtEpoch,
    });
    try {
      apply.applyOps([rowOp('1', 'a', '1', 'old text', 'old-hash')]);
      // A local row holds the unique hash needed by the incoming revision.
      const local = db.prepare(`INSERT INTO observations
        (memory_session_id, project, type, title, content_hash, created_at, created_at_epoch)
        VALUES ('mem-remote', ?, 'discovery', 'local duplicate', 'new-hash', ?, ?) RETURNING id`)
        .all(project, new Date(createdAtEpoch).toISOString(), createdAtEpoch)[0] as { id: number };
      expect(apply.applyOps([rowOp('2', 'a', '2', 'corrected text', 'new-hash')]).quarantined).toBe(1);
      // The user deletes that local duplicate and a fresh context render can
      // still contain the old remote revision until the next sync pull.
      db.prepare('DELETE FROM observations WHERE id = ?').run(local.id);
      emitContextInvalidation('all', 'delete-observation', 'removal');
      await waitPastDebounce();
      service.recordLiveRender(keys, { body: 'old text', cacheable: true }, Date.now());
      // This page only adds a first revision; the older blocked update is
      // applied by SyncApply's existing end-of-batch constraint handling.
      apply.applyOps([rowOp('3', 'c', '1', 'unrelated new row', 'other-hash')]);
      expect(db.prepare("SELECT title FROM observations WHERE origin_local_id = 'a'").get())
        .toEqual({ title: 'corrected text' });
      expect(db.prepare('SELECT COUNT(*) AS n FROM sync_pull_quarantine').get()).toEqual({ n: 0 });
      expect(readContextCache(keys, Date.now())).toBeNull();
      await waitPastDebounce();
    } finally {
      db.close();
    }
  });

  it('a render in flight when a removal lands is discarded and re-run', async () => {
    let releaseGate!: () => void;
    renderGate = new Promise<void>(resolve => { releaseGate = resolve; });
    emitContextInvalidation({ projects: [project] }, 'storeObservations');
    await new Promise(resolve => setTimeout(resolve, DEBOUNCE_MS * 4));
    // The re-render is now waiting on the gate (it started before the delete).
    emitContextInvalidation('all', 'delete-observation', 'removal');
    expect(existsSync(contextCacheFilePath(keys))).toBe(false);
    renderGate = null;
    releaseGate();
    // Let the in-flight render finish: it must not write its pre-delete body back.
    await new Promise(resolve => setImmediate(resolve));
    await new Promise(resolve => setImmediate(resolve));
    expect(existsSync(contextCacheFilePath(keys))).toBe(false);
    await waitPastDebounce();
    await waitPastDebounce();
    expect(readContextCache(keys, Date.now())?.body).toStartWith('render #');
  });

  it('a live render a removal overtook is not written to the cache', async () => {
    const generationAtStart = service.removalGenerationNow();
    emitContextInvalidation('all', 'delete-session', 'removal');
    service.recordLiveRender(keys, { body: 'pre-delete live render', cacheable: true }, Date.now(), generationAtStart);
    expect(existsSync(contextCacheFilePath(keys))).toBe(false);
    await waitPastDebounce();
    expect(readContextCache(keys, Date.now())?.body).toStartWith('render #');
  });

  it('observer-health writer (banner)', async () => {
    const healthPath = join(tempDir, 'observer-health.json');
    // Below the threshold the banner stays hidden, so nothing re-renders.
    recordObserverFailure('claude', 'boom', healthPath);
    recordObserverFailure('claude', 'boom', healthPath);
    await waitPastDebounce();
    expect(renders).toEqual([]);
    await expectRerenderedOnlyFor(() => recordObserverFailure('claude', 'boom', healthPath), [project, 'unrelated-proj']);
    renders = [];
    await expectRerenderedOnlyFor(() => recordObserverSuccess(healthPath), [project, 'unrelated-proj']);
  });

  it('a success while healthy (every stored observation) re-renders nothing', async () => {
    const healthPath = join(tempDir, 'observer-health.json');
    recordObserverSuccess(healthPath);
    recordObserverSuccess(healthPath);
    await waitPastDebounce();
    expect(renders).toEqual([]);
  });

  it('stopping with a re-render pending removes the stale file', async () => {
    expect(existsSync(contextCacheFilePath(keys))).toBe(true);
    emitContextInvalidation('all', 'delete-observation');
    service.stop();
    expect(existsSync(contextCacheFilePath(keys))).toBe(false);
    expect(existsSync(contextCacheFilePath(otherKeys))).toBe(false);
  });

  it('sync-health writer (banner)', async () => {
    await expectRerenderedOnlyFor(() => writeSyncHealth({
      state: 'ok', code: null, message: null, consecutiveFailures: 0, failingSinceAt: null,
      lastErrorAt: null, lastSuccessAt: Date.now(), updatedAt: Date.now(),
    }, join(tempDir, 'sync-health.json')), [project, 'unrelated-proj']);
  });

  it('settings change', async () => {
    // SettingsRoutes.handleUpdateSettings emits exactly this after persisting.
    await expectRerenderedOnlyFor(() => emitContextInvalidation('all', 'settings'), [project, 'unrelated-proj']);
  });

  it('coalesces a burst of writes into one render per variant', async () => {
    for (let index = 0; index < 5; index++) {
      emitContextInvalidation({ projects: [project] }, 'burst');
    }
    await waitPastDebounce();
    expect(renders).toEqual([project]);
  });

  it('removes the file instead of caching a block that carries the health banner', async () => {
    expect(existsSync(contextCacheFilePath(keys))).toBe(true);
    service.recordLiveRender(keys, { body: 'with banner', cacheable: false }, Date.now());
    expect(existsSync(contextCacheFilePath(keys))).toBe(false);
  });

  it('re-renders every remembered variant when a new worker starts', async () => {
    service.stop();
    renders = [];
    const booted = new ContextCacheService({
      debounceMs: DEBOUNCE_MS,
      renderVariant: async (variantKeys) => {
        renders.push(variantKeys.projects.join(','));
        return { body: 'boot render', cacheable: true };
      },
      expandProjectReadKeys: projects => projects,
    });
    booted.start();
    expect(booted.knownVariantCount()).toBeGreaterThanOrEqual(2);
    await booted.flushPendingRenders();
    expect(renders).toContain(project);
    expect(renders).toContain('unrelated-proj');
    expect(readContextCache(keys, Date.now())?.body).toBe('boot render');
    booted.stop();
  });
});

describe('ContextCacheService with cloud sync (servable only while Realtime is live)', () => {
  const keys = contextCacheKeys(['servable-proj'], 'claude', false);
  let renders: number;

  function syncedService(): ContextCacheService {
    renders = 0;
    return new ContextCacheService({
      debounceMs: 10,
      initiallyServable: false,
      renderVariant: async () => {
        renders++;
        return { body: `synced render #${renders}`, cacheable: true };
      },
      expandProjectReadKeys: projects => projects,
    });
  }

  afterEach(() => rmSync(contextCacheFilePath(keys), { force: true }));

  it('writes no file while Realtime has not joined; the hook takes the live (pulling) path', async () => {
    writeContextCache(keys, 'left by a previous worker', Date.now());
    const service = syncedService();
    service.recordLiveRender(keys, { body: 'live', cacheable: true }, Date.now());
    service.stop();
    const booted = syncedService();
    booted.start();
    // A previous worker's file may predate remote ops: gone before any render.
    expect(existsSync(contextCacheFilePath(keys))).toBe(false);
    booted.recordLiveRender(keys, { body: 'live', cacheable: true }, Date.now());
    await booted.flushPendingRenders();
    expect(existsSync(contextCacheFilePath(keys))).toBe(false);
    booted.stop();
  });

  it('re-renders every variant when Realtime joins, and removes the files when it drops', async () => {
    const service = syncedService();
    service.start();
    service.recordLiveRender(keys, { body: 'live', cacheable: true }, Date.now());
    expect(existsSync(contextCacheFilePath(keys))).toBe(false);

    service.setServable(true);
    await new Promise(resolve => setTimeout(resolve, 40));
    await service.flushPendingRenders();
    expect(readContextCache(keys, Date.now())?.body).toStartWith('synced render #');

    service.setServable(false);
    expect(existsSync(contextCacheFilePath(keys))).toBe(false);
    service.recordLiveRender(keys, { body: 'live again', cacheable: true }, Date.now());
    expect(existsSync(contextCacheFilePath(keys))).toBe(false);
    service.stop();
  });
});

// ---------------------------------------------------------------------------
// Hook: cache hit, miss, stale, colors (worker-utils mocked; nothing is spawned)
// ---------------------------------------------------------------------------

const HOOK_PROJECTS = ['cache-hook-parent', 'cache-hook-repo'];
const workerCalls: string[] = [];
let showTerminalOutput = false;
let showLastMessage = false;

mock.module('../../src/shared/hook-settings.js', () => ({
  ...realHookSettingsSnapshot,
  loadFromFileOnce: () => ({
    ...realHookSettingsSnapshot.loadFromFileOnce(),
    CLAUDE_MEM_CONTEXT_SHOW_TERMINAL_OUTPUT: String(showTerminalOutput),
    CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE: String(showLastMessage),
    CLAUDE_MEM_SESSION_START_INCLUDE_ALL_SOURCES: 'false',
    CLAUDE_MEM_PROVIDER: 'codex',
    CLAUDE_MEM_PRO_FALLBACK_AT: '',
    CLAUDE_MEM_PRO_PLAN: '',
    CLAUDE_MEM_EXCLUDED_PROJECTS: '',
    CLAUDE_MEM_MEMORY_INSTRUCTIONS_ENABLED: 'true',
  }),
}));

mock.module('../../src/utils/project-name.js', () => ({
  ...realProjectNameSnapshot,
  getProjectContext: () => ({
    primary: HOOK_PROJECTS[1],
    parent: HOOK_PROJECTS[0],
    isWorktree: true,
    allProjects: [...HOOK_PROJECTS],
  }),
}));

mock.module('../../src/shared/worker-utils.js', () => ({
  ...realWorkerUtilsSnapshot,
  executeWithWorkerFallback: async (path: string) => {
    workerCalls.push(path);
    return path.includes('colors=true') ? 'LIVE COLORED' : 'LIVE CONTEXT';
  },
  getWorkerPort: () => 37777,
  isWorkerFallback: () => false,
  consumeWorkerOutageNotice: async () => null,
}));

afterAll(() => {
  mock.module('../../src/shared/hook-settings.js', () => realHookSettingsSnapshot);
  mock.module('../../src/utils/project-name.js', () => realProjectNameSnapshot);
  mock.module('../../src/shared/worker-utils.js', () => realWorkerUtilsSnapshot);
});

describe('context hook reads the precomputed block', () => {
  const agentKeys = contextCacheKeys(HOOK_PROJECTS, 'claude', false, '/tmp/cache-hook-repo');
  const colorKeys = contextCacheKeys(HOOK_PROJECTS, 'claude', true, '/tmp/cache-hook-repo');
  const cachedBody = 'Access Claude-Mem Rules + Tools: ~/.claude-mem/RULES-AND-TOOLS.md\n\n'
    + `- release: updated ${relativeTimePlaceholder(Date.now() - 2 * 3_600_000)} ago\n`;

  beforeEach(() => {
    workerCalls.length = 0;
    showTerminalOutput = false;
    showLastMessage = false;
  });
  afterEach(() => {
    rmSync(contextCacheFilePath(agentKeys), { force: true });
    rmSync(contextCacheFilePath(colorKeys), { force: true });
  });

  async function runHook(cwd = '/tmp/cache-hook-repo') {
    const { contextHandler } = await import('../../src/cli/handlers/context.js');
    return contextHandler.execute({ sessionId: 'cache-session', cwd, platform: 'claude-code' });
  }

  it('re-renders an old cached body that no longer fits the delivery limit, instead of cutting it', async () => {
    writeContextCache(agentKeys, 'old record\n'.repeat(1_000), Date.now());
    const result = await runHook();
    expect(workerCalls).toHaveLength(1);
    expect(result.hookSpecificOutput?.additionalContext).toContain('LIVE CONTEXT');
    expect(result.hookSpecificOutput?.additionalContext).not.toContain('old record');
    expect(result.hookSpecificOutput!.additionalContext!.length).toBeLessThanOrEqual(10_000);
  });

  it('serves a cached block without calling the worker', async () => {
    writeContextCache(agentKeys, cachedBody, Date.now());
    const result = await runHook();
    expect(workerCalls).toEqual([]);
    const additionalContext = result.hookSpecificOutput?.additionalContext ?? '';
    expect(additionalContext).toStartWith('Access Claude-Mem Rules + Tools: ');
    expect(additionalContext).toContain('- release: updated about 2 hours ago');
    expect(additionalContext).not.toContain(PLACEHOLDER_MARKER);
  });

  it('passes the host session and answers live when prior messages are enabled', async () => {
    showLastMessage = true;
    writeContextCache(agentKeys, 'A block without the prior reply', Date.now());
    const result = await runHook();
    expect(workerCalls).toEqual(['/api/context/inject?projects=cache-hook-parent%2Ccache-hook-repo&platformSource=claude&cwd=%2Ftmp%2Fcache-hook-repo&sessionId=cache-session']);
    expect(result.hookSpecificOutput?.additionalContext).toBe('LIVE CONTEXT');
  });

  it('takes the live path on a miss, including the observed checkout cwd', async () => {
    const result = await runHook();
    expect(workerCalls).toEqual(['/api/context/inject?projects=cache-hook-parent%2Ccache-hook-repo&platformSource=claude&cwd=%2Ftmp%2Fcache-hook-repo']);
    // Instructions on, and still nothing after the timeline: the MCP server sends them.
    expect(result.hookSpecificOutput?.additionalContext).toBe('LIVE CONTEXT');
  });

  it('takes the live path when the cached block is older than 24h', async () => {
    writeContextCache(agentKeys, cachedBody, Date.now() - CONTEXT_CACHE_MAX_AGE_MS - 60_000);
    const result = await runHook();
    expect(workerCalls).toHaveLength(1);
    expect(result.hookSpecificOutput?.additionalContext).toBe('LIVE CONTEXT');
  });

  it('skips the colored fetch too when that variant is cached', async () => {
    showTerminalOutput = true;
    writeContextCache(agentKeys, cachedBody, Date.now());
    writeContextCache(colorKeys, `COLORED ${relativeTimePlaceholder(Date.now() - 60_000)} ago`, Date.now());
    const result = await runHook();
    expect(workerCalls).toEqual([]);
    // The timeline, then a blank line and the closing viewer line.
    expect(result.systemMessage).toMatch(/COLORED [^\n]+\n\nView Observations Live @ [^\n]+$/);
    expect(result.systemMessage).not.toContain(PLACEHOLDER_MARKER);
  });

  it('does not inject or display cached timelines on Claude resume', async () => {
    showTerminalOutput = true;
    writeContextCache(agentKeys, cachedBody, Date.now());
    writeContextCache(colorKeys, 'COLORED TIMELINE', Date.now());
    const { contextHandler } = await import('../../src/cli/handlers/context.js');
    const result = await contextHandler.execute({
      sessionId: 'cache-session',
      cwd: '/tmp/cache-hook-repo',
      platform: 'claude-code',
      sessionSource: 'resume',
    });

    expect(result.hookSpecificOutput?.additionalContext).toBe('');
    expect(result.systemMessage).toBeUndefined();
    expect(workerCalls).toEqual([]);
    expect(readContextCache(agentKeys, Date.now())?.body).toBe(cachedBody);
    expect(readContextCache(colorKeys, Date.now())?.body).toBe('COLORED TIMELINE');
  });

  it('serves the cached model block wherever in the checkout the session starts', async () => {
    showTerminalOutput = true;
    writeContextCache(agentKeys, cachedBody, Date.now());
    writeContextCache(colorKeys, `COLORED ${relativeTimePlaceholder(Date.now() - 60_000)} ago`, Date.now());
    const result = await runHook('/tmp/cache-hook-repo/packages/app');
    // The model's block does not depend on the directory. The colored one shows
    // file headings relative to it, so that one is fetched for the new directory.
    expect(workerCalls).toEqual(['/api/context/inject?projects=cache-hook-parent%2Ccache-hook-repo&platformSource=claude&cwd=%2Ftmp%2Fcache-hook-repo%2Fpackages%2Fapp&colors=true']);
    expect(result.hookSpecificOutput?.additionalContext).toContain('- release: updated about 2 hours ago');
    expect(result.systemMessage).toMatch(/LIVE COLORED\n\nView Observations Live @ [^\n]+$/);
  });

  it('fetches only the colored render when only the model block is cached', async () => {
    showTerminalOutput = true;
    writeContextCache(agentKeys, cachedBody, Date.now());
    const result = await runHook();
    expect(workerCalls).toEqual(['/api/context/inject?projects=cache-hook-parent%2Ccache-hook-repo&platformSource=claude&cwd=%2Ftmp%2Fcache-hook-repo&colors=true']);
    expect(result.systemMessage).toMatch(/LIVE COLORED\n\nView Observations Live @ [^\n]+$/);
  });
});

// ---------------------------------------------------------------------------
// End to end in a child process (own data dir and database, real renderer):
// live route == cached body + fill, byte for byte; hook from cache, no fetch.
// ---------------------------------------------------------------------------

const childScript = `
  const FIXED_NOW = 1_790_000_000_000;
  Date.now = () => FIXED_NOW;
  const { SessionStore } = await import('./src/services/sqlite/SessionStore.ts');
  const { ModeManager } = await import('./src/services/domain/ModeManager.ts');
  const { SearchRoutes } = await import('./src/services/worker/http/routes/SearchRoutes.ts');
  const { ContextCacheService } = await import('./src/services/worker/ContextCacheService.ts');
  const { projectReadKeys } = await import('./src/services/sqlite/project-read-keys.ts');
  const { getProjectContext } = await import('./src/utils/project-name.ts');
  const cache = await import('./src/shared/context-cache.ts');
  ModeManager.getInstance().loadMode('code');

  const cwd = process.env.HOOK_CWD;
  const projects = getProjectContext(cwd).allProjects;
  const primary = projects[projects.length - 1];
  const store = new SessionStore(process.env.CLAUDE_MEM_DATA_DIR + '/claude-mem.db');
  const sessionDbId = store.createSDKSession('content-e2e', primary, 'prompt');
  store.ensureMemorySessionIdRegistered(sessionDbId, 'memory-e2e');
  store.storeObservations('memory-e2e', primary, [{
    type: 'discovery', title: 'FIRST_OBSERVATION', subtitle: 'sub', facts: ['fact'], narrative: 'narrative',
    concepts: ['how-it-works'], files_read: [], files_modified: [],
  }], null, 1, 0, FIXED_NOW - 3_600_000);
  store.appendWorkStateEntry({ project: primary, listName: 'release', fields: { status: 'active' }, createdAtEpoch: FIXED_NOW - 2 * 3_600_000 });

  let routes;
  const service = new ContextCacheService({
    debounceMs: 30,
    renderVariant: (keys) => routes.renderContextVariant(keys),
    expandProjectReadKeys: (p) => projectReadKeys(store.db, p),
  });
  routes = new SearchRoutes({ getSessionStore: () => store }, service);
  service.start();
  let handler;
  routes.setupRoutes({ get: (path, h) => { if (path === '/api/context/inject') handler = h; }, post: () => {}, delete: () => {}, use: () => {} });

  async function live(query) {
    return await new Promise((resolve) => {
      const res = { setHeader: () => {}, send: (body) => resolve(body), status: () => res, json: (b) => resolve(JSON.stringify(b)), headersSent: false };
      handler({ query, get: () => undefined, body: undefined }, res);
    });
  }
  const projectsParam = projects.join(',');
  const liveAgent = await live({ projects: projectsParam, platformSource: 'claude', cwd });
  const liveColors = await live({ projects: projectsParam, platformSource: 'claude', colors: 'true', cwd });
  const agentFile = cache.readContextCache(cache.contextCacheKeys(projects, 'claude', false, cwd), FIXED_NOW);
  const colorFile = cache.readContextCache(cache.contextCacheKeys(projects, 'claude', true, cwd), FIXED_NOW);

  let fetchCalls = 0;
  globalThis.fetch = async () => { fetchCalls++; throw new Error('no worker in this test'); };
  const { contextHandler } = await import('./src/cli/handlers/context.ts');
  const hook = await contextHandler.execute({ sessionId: 'e2e', cwd, platform: 'claude-code' });

  let budgetResult = null;
  if (process.env.BUDGET_CASE === '1') {
    for (let i = 0; i < 80; i++) {
      store.storeObservations('memory-e2e', primary, [{
        type: 'discovery', title: 'RECORD_' + i + '_' + 'x'.repeat(350),
        subtitle: '', facts: [], narrative: '', concepts: ['how-it-works'], files_read: [], files_modified: [],
      }], null, i + 3, 0, FIXED_NOW - (80 - i) * 60_000);
    }
    await service.flushPendingRenders();
    const normal = await live({ projects: projectsParam, platformSource: 'claude', cwd });
    const keys = cache.contextCacheKeys(projects, 'claude', false, cwd);
    const cachedBefore = cache.readContextCache(keys, FIXED_NOW)?.body;
    const reserved = await live({ projects: projectsParam, platformSource: 'claude', cwd, reserveChars: '3000' });
    const cachedAfter = cache.readContextCache(keys, FIXED_NOW)?.body;
    // A different, never-requested source scope starts with no cache file.
    const coldKeys = cache.contextCacheKeys(projects, undefined, false, cwd);
    const coldBefore = cache.readContextCache(coldKeys, FIXED_NOW);
    const coldReserved = await live({ projects: projectsParam, cwd, reserveChars: '3000' });
    await service.flushPendingRenders();
    const coldFile = cache.readContextCache(coldKeys, FIXED_NOW);
    const coldWarmed = coldFile ? cache.fillContextPlaceholders(coldFile.body, FIXED_NOW, coldFile.placeholderNonce) : null;
    budgetResult = { normal, reserved, cachedBefore, cachedAfter, coldBefore, coldReserved, coldWarmed };
  }

  // A write re-renders the block within the debounce.
  store.storeObservations('memory-e2e', primary, [{
    type: 'discovery', title: 'SECOND_OBSERVATION', subtitle: 'sub', facts: [], narrative: 'n',
    concepts: ['how-it-works'], files_read: [], files_modified: [],
  }], null, 2, 0, FIXED_NOW - 60_000);
  await new Promise((resolve) => setTimeout(resolve, 200));
  await service.flushPendingRenders();
  const agentAfterWrite = cache.readContextCache(cache.contextCacheKeys(projects, 'claude', false, cwd), FIXED_NOW);
  if (budgetResult) {
    const coldAfterWrite = cache.readContextCache(cache.contextCacheKeys(projects, undefined, false, cwd), FIXED_NOW);
    budgetResult.coldAfterWrite = coldAfterWrite?.body ?? null;
  }
  service.stop();
  store.close();

  console.log(JSON.stringify({
    liveAgent, liveColors, budgetResult,
    agentBody: agentFile?.body ?? null, agentRenderedAt: agentFile?.renderedAtEpochMs ?? null,
    colorBody: colorFile?.body ?? null,
    filledAgent: agentFile ? cache.fillContextPlaceholders(agentFile.body, FIXED_NOW, agentFile.placeholderNonce) : null,
    filledColors: colorFile ? cache.fillContextPlaceholders(colorFile.body, FIXED_NOW, colorFile.placeholderNonce) : null,
    hookContext: hook.hookSpecificOutput?.additionalContext ?? null,
    hookSystemMessage: hook.systemMessage ?? null,
    fetchCalls,
    agentAfterWrite: agentAfterWrite ? cache.fillContextPlaceholders(agentAfterWrite.body, FIXED_NOW, agentAfterWrite.placeholderNonce) : null,
  }));
  process.exit(0);
`;

describe('precomputed context end to end', () => {
  let result: any;
  beforeAll(async () => {
    const dataDir = mkdtempSync(join(tmpdir(), 'cmem-notice-budget-'));
    const hookCwd = mkdtempSync(join(tmpdir(), 'cmem-notice-project-'));
    try {
      writeFileSync(join(dataDir, 'settings.json'), JSON.stringify({
        CLAUDE_MEM_CONTEXT_SHOW_TERMINAL_OUTPUT: 'true',
        CLAUDE_MEM_WORKER_PORT: '1',
        CLAUDE_MEM_PROVIDER: 'codex',
        CLAUDE_MEM_CHROMA_ENABLED: 'false',
        CLAUDE_MEM_MEMORY_INSTRUCTIONS_ENABLED: 'true',
        CLAUDE_MEM_CONTEXT_OBSERVATIONS: '80',
      }));
      const child = Bun.spawn([process.execPath, '-e', childScript], {
        cwd: process.cwd(),
        env: { ...process.env, CLAUDE_MEM_DATA_DIR: dataDir, HOOK_CWD: hookCwd, BUDGET_CASE: '1', DO_NOT_TRACK: '1' },
        stdout: 'pipe', stderr: 'pipe',
      });
      const [stdout, stderr] = await Promise.all([new Response(child.stdout).text(), new Response(child.stderr).text()]);
      expect(await child.exited).toBe(0);
      const line = stdout.trim().split('\n').pop() ?? '';
      if (!line.startsWith('{')) throw new Error(stdout + stderr);
      result = JSON.parse(line);
    } finally {
      rmSync(dataDir, { recursive: true, force: true });
      rmSync(hookCwd, { recursive: true, force: true });
    }
  }, 60_000);

  it('fits a dynamic notice reserve with whole newest records and leaves the regular cache untouched', () => {
    const { budgetResult } = result;
    expect(budgetResult.normal).toContain('RECORD_79_' + 'x'.repeat(350));
    expect(budgetResult.reserved).toContain('RECORD_79_' + 'x'.repeat(350));
    expect(budgetResult.reserved.length + 3000).toBeLessThanOrEqual(10_000);
    expect(budgetResult.reserved.length).toBeLessThan(budgetResult.normal.length);
    expect(budgetResult.cachedAfter).toBe(budgetResult.cachedBefore);
    expect(budgetResult.coldBefore).toBeNull();
    expect(budgetResult.coldWarmed).toContain('RECORD_79_' + 'x'.repeat(350));
    expect(budgetResult.coldWarmed.length).toBeGreaterThan(budgetResult.coldReserved.length);
    expect(budgetResult.coldAfterWrite).toContain('SECOND_OBSERVATION');
    expect(budgetResult.reserved).not.toContain('Recent context omitted');
  });

  it('serves exactly what the live route sends, and keeps it fresh', () => {
    // The live answer contains real content, and no placeholder ever leaves the worker.
    expect(result.liveAgent).toContain('FIRST_OBSERVATION');
    expect(result.liveAgent).toContain('updated about 2 hours ago');
    for (const text of [result.liveAgent, result.liveColors, result.hookContext, result.hookSystemMessage]) {
      expect(text).not.toContain(PLACEHOLDER_MARKER);
    }
    // The cached bodies carry placeholders where (and only where) time is formatted.
    // (The child has its own placeholder nonce, so match the shape.)
    // The header has no time any more, so none carries its placeholder.
    expect(result.agentBody).not.toContain('⟦CMEM_NOW_HEADER:');
    expect(result.agentBody).toMatch(/⟦CMEM_AGO:[0-9a-f]{12}:\d{13}⟧/);
    expect(result.colorBody).not.toContain('⟦CMEM_NOW_HEADER:');
    expect(result.agentRenderedAt).toBe(1_790_000_000_000);

    // Byte-identical: cached body + fill == live render, agent and colors variants.
    expect(result.filledAgent).toBe(result.liveAgent);
    expect(result.filledColors).toBe(result.liveColors);

    // The hook answered from the cache without any request to the worker.
    expect(result.fetchCalls).toBe(0);
    expect(result.hookContext).toBe(result.liveAgent.trim());
    expect(result.hookContext).not.toContain(MEMORY_PLUGIN_INSTRUCTIONS);
    expect(result.hookContext).not.toContain('# Work state: your to-do lists');
    // The newest timeline row, a blank line, then the closing viewer line with the row count.
    const [bannerTimeline, bannerFooter] = result.hookSystemMessage.split(/\n\n(?=View Observations Live @)/);
    expect(bannerTimeline).toEndWith(result.liveColors.trim());
    expect(bannerTimeline.split('\n').at(-1)).toContain('FIRST_OBSERVATION');
    expect(bannerFooter).toMatch(/^View Observations Live @ \S+ · 1 line$/);
    expect(result.hookSystemMessage).not.toContain('tokens of past research');

    // A store write re-rendered the cached block.
    expect(result.agentAfterWrite).toContain('SECOND_OBSERVATION');
  });
});

describe('stored SessionStart project catalog (#6403)', () => {
  const catalogPath = () => join(contextCacheDir(), 'project-catalog.json');
  afterEach(() => rmSync(catalogPath(), { force: true }));

  it('is refreshed only once stored, and only for a write naming a project it lacks', () => {
    let listed = 0;
    const listProjects = () => { listed++; return ['zeta', 'alpha', 'new-project']; };

    refreshContextProjectCatalog(['new-project'], listProjects);
    expect(listed).toBe(0);
    expect(readContextProjectCatalog()).toBeNull();

    saveContextProjectCatalog(['zeta', 'alpha']);
    expect(readContextProjectCatalog()).toEqual(['alpha', 'zeta']);
    refreshContextProjectCatalog(['alpha'], listProjects);
    expect(listed).toBe(0);

    refreshContextProjectCatalog(['new-project'], listProjects);
    expect(listed).toBe(1);
    expect(readContextProjectCatalog()).toEqual(['alpha', 'new-project', 'zeta']);

    refreshContextProjectCatalog('all', listProjects);
    expect(listed).toBe(2);
  });

  it('treats a malformed stored list as none, so the hook asks the worker', () => {
    writeFileSync(catalogPath(), '{"projects":"not a list"}');
    expect(readContextProjectCatalog()).toBeNull();
  });

  it('reaches the stored list on the worker write that adds a new project, and never breaks that write', async () => {
    const store = new SessionStore(':memory:');
    let listerThrows = false;
    const service = new ContextCacheService({
      debounceMs: 20,
      renderVariant: async () => ({ body: 'render', cacheable: true }),
      expandProjectReadKeys: projects => projects,
      listCatalogProjects: () => {
        if (listerThrows) throw new Error('catalog query failed');
        return store.getProjectCatalog().projects;
      },
    });
    service.start();
    try {
      saveContextProjectCatalog(['known-project']);
      const sessionDbId = store.createSDKSession('content-new', 'brand-new-project', 'prompt');
      store.ensureMemorySessionIdRegistered(sessionDbId, 'memory-new');
      store.storeObservations('memory-new', 'brand-new-project', [{
        type: 'discovery', title: 'A title', subtitle: null, facts: [], narrative: 'n',
        concepts: [], files_read: [], files_modified: [],
      }], null);
      expect(readContextProjectCatalog()).toContain('brand-new-project');

      listerThrows = true;
      expect(() => emitContextInvalidation({ projects: ['another-new-project'] }, 'test')).not.toThrow();
    } finally {
      await service.flushPendingRenders();
      service.stop();
      store.close();
    }
  });
});