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

18.9 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
import { Database } from 'bun:sqlite';
import { SessionStore } from '../../src/services/sqlite/SessionStore';
import { ingestObservation, setIngestContext } from '../../src/services/worker/http/shared';
import { telemetryBuffer } from '../../src/services/telemetry/buffer';
import { __resetTelemetryForTests } from '../../src/services/telemetry/telemetry';
import { postHogCaptureCalls } from '../preload';
import { recordWorkStatePrivacy, recordWorkStateInstruction } from '../../src/services/telemetry/work-state';
import { SessionRoutes } from '../../src/services/worker/http/routes/SessionRoutes';
import express from 'express';
import { sessionInitHandler, setSessionInitDependenciesForTesting } from '../../src/cli/handlers/session-init';
import { observationHandler } from '../../src/cli/handlers/observation';
import { settleHookSpoolNudges } from '../../src/cli/spool-hook-event';
import * as workerUtils from '../../src/shared/worker-utils';
import { HookSpool } from '../../src/shared/hook-spool';
import { mkdtempSync, rmSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';

const ENV = ['CLAUDE_MEM_TELEMETRY', 'DO_NOT_TRACK', 'CLAUDE_MEM_SKIP_TOOLS', 'CLAUDE_MEM_SKIP_SUBAGENT_OBSERVATIONS', 'CLAUDE_MEM_EXCLUDED_PROJECTS'];
let previous: Array<string | undefined>;
let store: SessionStore;
let queue: ReturnType<typeof mock>;
const payload = {
  contentSessionId: 'telemetry-capture', platformSource: 'codex', cwd: '/tmp/telemetry-work',
  toolName: 'TodoWrite', toolUseId: 'native-todo-1', toolResponse: {},
};

beforeEach(() => {
  previous = ENV.map(key => process.env[key]);
  process.env.CLAUDE_MEM_TELEMETRY = '1';
  delete process.env.DO_NOT_TRACK;
  process.env.CLAUDE_MEM_SKIP_TOOLS = 'TodoWrite';
  process.env.CLAUDE_MEM_SKIP_SUBAGENT_OBSERVATIONS = 'false';
  process.env.CLAUDE_MEM_EXCLUDED_PROJECTS = '';
  __resetTelemetryForTests();
  telemetryBuffer.__resetForTests();
  postHogCaptureCalls.length = 0;
  store = new SessionStore(new Database(':memory:'));
  queue = mock(() => {});
  setIngestContext({
    sessionManager: { queueObservation: queue } as any,
    dbManager: { getSessionStore: () => store } as any,
    eventBroadcaster: { broadcastObservationQueued: () => {} } as any,
  });
});

afterEach(() => {
  store.close();
  ENV.forEach((key, i) => previous[i] === undefined ? delete process.env[key] : process.env[key] = previous[i]);
  telemetryBuffer.__resetForTests();
  __resetTelemetryForTests();
});

function prompt(text: string) {
  const id = store.createSDKSession(payload.contentSessionId, 'telemetry-work', '', undefined, 'codex');
  store.saveUserPrompt(payload.contentSessionId, 1, text, id);
  recordWorkStatePrivacy(store.db, { ...payload, enqueuedAtEpochMs: Date.now() }, !text.trim());
}

function rows() {
  telemetryBuffer.flush();
  return postHogCaptureCalls.filter(call => call.event === 'work_state_usage_rollup');
}

async function withRoutes(run: (port: number) => Promise<void>) {
  const routes = new SessionRoutes(
    { getSession: () => undefined, initializeSession: () => ({ project: 'telemetry-work' }) } as any,
    { getSessionStore: () => store, getCloudSync: () => null, getChromaSync: () => null } as any,
    {} as any, {} as any, {} as any,
    { broadcastNewPrompt: () => {}, broadcastSessionStarted: () => {} } as any,
    {} as any, {} as any,
  );
  (routes as any).ensureGeneratorRunning = async () => {};
  const app = express();
  app.use(express.json());
  routes.setupRoutes(app);
  const server = app.listen(0, '127.0.0.1');
  await new Promise<void>(resolve => server.once('listening', resolve));
  try { await run((server.address() as { port: number }).port); }
  finally { await new Promise<void>(resolve => server.close(() => resolve())); }
}

describe('work-state capture boundary', () => {
  it('counts native todo use once even when that tool is excluded from memory generation', async () => {
    prompt('Do the work');
    expect(await ingestObservation({ ...payload, enqueuedAtEpochMs: Date.now() })).toMatchObject({ status: 'skipped', reason: 'tool_excluded' });
    await ingestObservation({ ...payload, enqueuedAtEpochMs: Date.now() });
    expect(rows()[0].properties).toMatchObject({ native_todo_count: 1, captured_tool_count: 1, instruction_state: 'unknown', agent_role: 'unknown' });
    expect(queue).not.toHaveBeenCalled();
    expect(store.queryToolUses({})).toHaveLength(0);
  });

  it('does not count entirely private or missing prompts', async () => {
    await ingestObservation({ ...payload, enqueuedAtEpochMs: Date.now() });
    expect(rows()).toHaveLength(0);
    prompt('');
    await ingestObservation({ ...payload, enqueuedAtEpochMs: Date.now() });
    expect(rows()).toHaveLength(0);
  });

  it('counts each missing-ID work-state event only once', async () => {
    prompt('Do the work');
    await ingestObservation({ ...payload, enqueuedAtEpochMs: Date.now(), toolName: 'work_state_write', toolUseId: undefined });
    expect(rows()[0].properties.unidentified_tool_event_count).toBe(1);
  });

  it('records captured usage through the normal prompt hook and the live observation handoff', async () => {
    // A private data dir, without the helper's live-handoff stub: the real handoff runs.
    const savedDataDir = process.env.CLAUDE_MEM_DATA_DIR;
    const dataDir = mkdtempSync(join(tmpdir(), 'claude-mem-work-state-live-'));
    process.env.CLAUDE_MEM_DATA_DIR = dataDir;
    process.env.CLAUDE_MEM_SKIP_TOOLS = 'work_state_write';
    const liveHandoffs: string[] = [];
    try {
      await withRoutes(async port => {
        // The hook's one live handoff goes to these real routes, not an external worker.
        const request = spyOn(workerUtils, 'workerHttpRequest').mockImplementation(async (path: string, init: any = {}) => {
          liveHandoffs.push(path);
          return fetch(`http://127.0.0.1:${port}${path}`, { method: init.method, headers: init.headers, body: init.body });
        });
        setSessionInitDependenciesForTesting({
          shouldTrackProject: () => true,
          loadFromFileOnce: () => ({ CLAUDE_MEM_SEMANTIC_INJECT: 'false' }),
          resolveRuntimeContext: () => ({ runtime: 'worker' }),
          isWorkerFallback: () => false,
          executeWithWorkerFallback: async (path: string, _method: string, body: unknown) => {
            const response = await fetch(`http://127.0.0.1:${port}${path}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
            return response.json();
          },
        });
        try {
          const input = { sessionId: 'normal-hook-path', cwd: dataDir, platform: 'codex', agentId: 'main-agent' };
          recordWorkStateInstruction(store.db, { contentSessionId: input.sessionId, platformSource: 'codex', agentId: input.agentId, enqueuedAtEpochMs: Date.now() - 10 }, 'returned', 'normal-context');
          await sessionInitHandler.execute({ ...input, prompt: 'Read and update the saved progress' });
          await observationHandler.execute({ ...input, toolName: 'work_state_write', toolUseId: 'normal-write', toolResponse: 'Saved to "progress" in project.' });
          await settleHookSpoolNudges();
          expect(liveHandoffs).toEqual(['/api/sessions/observations']);
          const metrics = rows().map(row => row.properties);
          expect(metrics.some(row => row.captured_write_ok_count === 1 && row.platform === 'codex' && row.agent_role === 'main')).toBe(true);
          expect(metrics.some(row => row.captured_prompt_count === 1 && row.instruction_state === 'returned')).toBe(true);
          expect(queue).not.toHaveBeenCalled();
          expect((await new HookSpool().entries())).toEqual([]);
        } finally { request.mockRestore(); }
      });
    } finally {
      setSessionInitDependenciesForTesting();
      if (savedDataDir === undefined) delete process.env.CLAUDE_MEM_DATA_DIR; else process.env.CLAUDE_MEM_DATA_DIR = savedDataDir;
      rmSync(dataDir, { recursive: true, force: true });
    }
  });

  it('keeps public/private/public boundaries through real init and post-tool routes', async () => {
    await withRoutes(async port => {
      const post = async (path: string, body: Record<string, unknown>) => {
        const response = await fetch(`http://127.0.0.1:${port}/api/sessions/${path}`, {
          method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body),
        });
        expect(response.status).toBe(200);
        return response.json();
      };
      const base = { contentSessionId: payload.contentSessionId, platformSource: 'codex', project: 'telemetry-work', agentId: 'main-agent' };
      const tool = async (toolName: string, id: string, at = Date.now()) => post('observations', {
        ...base, tool_name: toolName, tool_response: 'Saved to "list" in project.', tool_use_id: id, enqueuedAtEpochMs: at,
      });
      await post('init', { ...base, prompt: 'First public request', telemetryPromptEpochMs: Date.now() });
      await tool('TodoWrite', 'public-1');
      await post('init', { ...base, prompt: '<private>never store this private turn</private>', telemetryPromptEpochMs: Date.now() });
      const privateAt = Date.now();
      await tool('TodoWrite', 'private-native');
      await tool('work_state_write', 'private-write');
      await tool('Read', 'private-read');
      await new Promise(resolve => setTimeout(resolve, 2));
      await post('init', { ...base, prompt: 'Second public request', telemetryPromptEpochMs: Date.now() });
      await tool('TodoWrite', 'delayed-private', privateAt);
      await tool('TodoWrite', 'public-2');
      const metrics = rows().map(row => row.properties);
      expect(metrics.reduce((n, r) => n + Number(r.native_todo_count ?? 0), 0)).toBe(2);
      expect(metrics.reduce((n, r) => n + Number(r.captured_write_count ?? 0), 0)).toBe(0);
      expect(metrics.reduce((n, r) => n + Number(r.captured_tool_count ?? 0), 0)).toBe(2);
      expect(metrics.find(r => r.captured_prompt_count)?.agent_role).toBe('main');
      expect(store.db.query('SELECT COUNT(*) AS n FROM user_prompts').get()).toEqual({ n: 2 });
      expect(JSON.stringify(store.db.query('SELECT * FROM work_state_telemetry_receipts').all())).not.toContain('never store');
    });
  });

  it('a repeated public ask is a new turn only when its timestamp is newer; retries stay deduped', async () => {
    await withRoutes(async port => {
      const base = { contentSessionId: 'repeated-public', platformSource: 'codex', project: 'telemetry-work' };
      const post = async (path: string, body: Record<string, unknown>) => {
        const response = await fetch(`http://127.0.0.1:${port}/api/sessions/${path}`, {
          method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ...base, ...body }),
        });
        expect(response.status).toBe(200);
        return response.json();
      };
      const at = Date.now();
      const clock = spyOn(Date, 'now').mockReturnValue(at);
      try {
        const publicPrompt = { prompt: 'Repeat this public request' };
        await post('init', { ...publicPrompt, telemetryPromptEpochMs: at });
        clock.mockReturnValue(at + 10);
        await post('init', { prompt: '<private>private turn</private>', telemetryPromptEpochMs: at + 10 });
        clock.mockReturnValue(at + 20);
        // The re-ask's own source time is strictly newer than the boundary
        // the private turn set, so it is a genuine new turn: its tool uses
        // must resolve to a public row, not to the private anchor.
        expect(await post('init', { ...publicPrompt, telemetryPromptEpochMs: at + 20 })).toMatchObject({ skipped: false, promptNumber: 3 });
        await post('observations', { tool_name: 'TodoWrite', tool_use_id: 'new-public', enqueuedAtEpochMs: at + 30 });
        clock.mockReturnValue(at + 40);
        await post('init', { prompt: '<private>newer private turn</private>', telemetryPromptEpochMs: at + 40 });
        clock.mockReturnValue(at + 50);
        await post('init', { ...publicPrompt, telemetryPromptEpochMs: at + 20 });
        await post('observations', { tool_name: 'TodoWrite', tool_use_id: 'old-public-retry', enqueuedAtEpochMs: at + 50 });
        await post('init', publicPrompt); // no original timestamp cannot reopen coverage either
        await post('observations', { tool_name: 'TodoWrite', tool_use_id: 'untimed-public-retry', enqueuedAtEpochMs: at + 60 });
        expect(rows().reduce((n, row) => n + Number(row.properties.native_todo_count ?? 0), 0)).toBe(1);
        // Turn 1 (the original public ask) and turn 3: the re-ask's own
        // timestamp proved it was typed after the private turn at +10, so it
        // is a genuine new turn instead of collapsing onto the first row —
        // its tool use would otherwise resolve to the private anchor and be
        // suppressed. The old-timestamp retry and the untimed ask prove
        // nothing and stay deduped.
        expect(store.db.query('SELECT COUNT(*) AS n FROM user_prompts').get()).toEqual({ n: 2 });
      } finally { clock.mockRestore(); }
    });
  });

  it('leaves untimestamped transcript prompts outside current activity and public coverage', async () => {
    await withRoutes(async port => {
      const base = { contentSessionId: 'transcript-unknown-time', platformSource: 'codex', project: 'telemetry-work' };
      const response = await fetch(`http://127.0.0.1:${port}/api/sessions/init`, {
        method: 'POST', headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ ...base, nativePromptId: 'old-transcript-prompt', prompt: 'Restored transcript request' }),
      });
      expect(response.status).toBe(200);
      await ingestObservation({ ...payload, ...base, enqueuedAtEpochMs: Date.now() });
      expect(rows()).toHaveLength(0);
    });
  });

  it('keeps queued direct-host tool timing separate from the prompt insertion time', async () => {
    await withRoutes(async port => {
      const base = { contentSessionId: 'delayed-live-init', platformSource: 'pi', cwd: '/tmp/telemetry-work', project: 'telemetry-work' };
      const post = async (path: string, body: Record<string, unknown>) => {
        const response = await fetch(`http://127.0.0.1:${port}/api/sessions/${path}`, {
          method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ...base, ...body }),
        });
        expect(response.status).toBe(200);
        return response.json();
      };
      const start = Date.now();
      const clock = spyOn(Date, 'now').mockReturnValue(start + 100);
      try {
        // The tool callback happened after its user prompt, but both queued
        // requests reached the worker after the tool callback's original time.
        await post('init', { prompt: 'Read the current file', telemetryPromptEpochMs: start });
        await post('observations', {
          tool_name: 'Read', tool_response: 'file contents', tool_use_id: 'queued-direct-tool',
          telemetryToolEpochMs: start + 10,
        });
        expect(queue).toHaveBeenCalledTimes(1);
        expect(queue.mock.calls[0][1].prompt_number).toBe(1);
        expect(store.db.query('SELECT prompt_number FROM tool_uses WHERE tool_use_id = ?').get('queued-direct-tool')).toEqual({ prompt_number: 1 });
        expect(rows().some(row => row.properties.captured_tool_count === 1 && row.properties.platform === 'pi')).toBe(true);
      } finally { clock.mockRestore(); }
    });
  });

  it('does not count missing-prompt writes or ordinary tools through the normal ingestion path', async () => {
    await ingestObservation({ ...payload, enqueuedAtEpochMs: Date.now(), toolName: 'work_state_write', toolUseId: 'missing-write', toolResponse: 'Saved to "a" in project.' });
    await ingestObservation({ ...payload, enqueuedAtEpochMs: Date.now(), toolName: 'Read', toolUseId: 'missing-read' });
    expect(queue).toHaveBeenCalledTimes(2); // existing observation behavior survives
    expect(rows()).toHaveLength(0); // telemetry needs affirmative public-prompt evidence
  });

  it('preserves unknown telemetry host despite the legacy Claude database default', async () => {
    const id = store.createSDKSession(payload.contentSessionId, 'telemetry-work', '', undefined, 'claude');
    store.saveUserPrompt(payload.contentSessionId, 1, 'Do the work', id);
    recordWorkStatePrivacy(store.db, { ...payload, platformSource: undefined, enqueuedAtEpochMs: Date.now() }, false);
    await ingestObservation({ ...payload, enqueuedAtEpochMs: Date.now(), platformSource: undefined });
    await ingestObservation({ ...payload, enqueuedAtEpochMs: Date.now(), toolUseId: 'legacy-normalized', platformSource: 'claude', telemetryPlatformSource: 'unknown' });
    expect(rows()[0].properties).toMatchObject({ platform: 'unknown', native_todo_count: 2 });
  });

  it('preserves unknown host through the real session-init HTTP route', async () => {
    await withRoutes(async port => {
      const response = await fetch(`http://127.0.0.1:${port}/api/sessions/init`, {
        method: 'POST', headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ contentSessionId: 'raw-http-no-host', project: 'telemetry-work', prompt: 'Read saved work state', telemetryPromptEpochMs: Date.now() }),
      });
      expect(await response.json()).toMatchObject({ status: 'initialized' });
      expect(response.status).toBe(200);
      expect(rows()[0].properties).toMatchObject({ platform: 'unknown', captured_prompt_count: 1 });
      expect(store.db.query('SELECT platform_source FROM sdk_sessions WHERE content_session_id = ?').get('raw-http-no-host')).toEqual({ platform_source: 'claude' });
    });
  });

  it('does not count excluded projects or excluded subagents', async () => {
    prompt('Do the work');
    process.env.CLAUDE_MEM_EXCLUDED_PROJECTS = '/tmp/telemetry-work';
    await ingestObservation({ ...payload, enqueuedAtEpochMs: Date.now() });
    expect(rows()).toHaveLength(0);
    process.env.CLAUDE_MEM_EXCLUDED_PROJECTS = '';
    process.env.CLAUDE_MEM_SKIP_SUBAGENT_OBSERVATIONS = 'true';
    await ingestObservation({ ...payload, enqueuedAtEpochMs: Date.now(), agentId: 'agent', agentType: 'Explore' });
    expect(rows()).toHaveLength(0);
  });

  it('counts real host failure receipts without forwarding their error text', async () => {
    prompt('Do the work');
    await ingestObservation({ ...payload, enqueuedAtEpochMs: Date.now(), toolName: 'mcp__mcp_search__work_state_write', toolResponse: { error: 'Error executing tool: secret failure', is_interrupt: false } });
    expect(rows()[0].properties).toMatchObject({ captured_write_count: 1, captured_write_error_count: 1 });
    expect(JSON.stringify(rows())).not.toContain('secret');
  });

  it('keeps an excluded tool harmless when the telemetry lookup cannot reach the database', async () => {
    setIngestContext({ sessionManager: {} as any, dbManager: { getSessionStore: () => { throw new Error('offline'); } } as any, eventBroadcaster: {} as any });
    expect(await ingestObservation({ ...payload, enqueuedAtEpochMs: Date.now() })).toMatchObject({ status: 'skipped', reason: 'tool_excluded' });
  });
});