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

21.9 KB
Raw Blame
import { describe, it, expect } from 'bun:test';
import { Database } from 'bun:sqlite';
import { SessionStore } from '../../src/services/sqlite/SessionStore.js';
import {
  buildTimeline,
  countObservationsByProjects,
  queryObservationsMulti,
  queryObservationsNewest,
  querySummariesMulti,
} from '../../src/services/context/ObservationCompiler.js';
import type { ContextConfig, Observation, SummaryTimelineItem } from '../../src/services/context/types.js';

function createTestObservation(overrides: Partial<Observation> = {}): Observation {
  return {
    id: 1,
    memory_session_id: 'session-123',
    type: 'discovery',
    title: 'Test Observation',
    subtitle: null,
    narrative: 'A test narrative',
    facts: '["fact1"]',
    concepts: '["concept1"]',
    files_read: null,
    files_modified: null,
    discovery_tokens: 100,
    created_at: '2025-01-01T12:00:00.000Z',
    created_at_epoch: 1735732800000,
    ...overrides,
  };
}

function createTestSummaryTimelineItem(overrides: Partial<SummaryTimelineItem> = {}): SummaryTimelineItem {
  return {
    id: 1,
    memory_session_id: 'session-123',
    request: 'Test Request',
    investigated: 'Investigated things',
    learned: 'Learned things',
    completed: 'Completed things',
    next_steps: 'Next steps',
    created_at: '2025-01-01T12:00:00.000Z',
    created_at_epoch: 1735732800000,
    displayEpoch: 1735732800000,
    displayTime: '2025-01-01T12:00:00.000Z',
    shouldShowLink: false,
    ...overrides,
  };
}

function config(concept: string): ContextConfig {
  return {
    totalObservationCount: 20,
    fullObservationCount: 3,
    sessionCount: 20,
    showReadTokens: true,
    showWorkTokens: true,
    observationTypes: new Set(['discovery']),
    observationConcepts: new Set([concept]),
    fullObservationField: 'narrative',
    showLastSummary: true,
    showLastMessage: false,
    mainAgentOnly: true,
  };
}

describe('buildTimeline', () => {
  it.each([
    ['combines both kinds', [{ id: 1, created_at_epoch: 1000 }], [{ id: 1, displayEpoch: 2000 }], ['observation:1', 'summary:1']],
    ['sorts chronologically', [{ id: 1, created_at_epoch: 3000 }, { id: 2, created_at_epoch: 1000 }], [{ id: 1, displayEpoch: 2000 }], ['observation:2', 'summary:1', 'observation:1']],
    ['keeps one turn in stored order', [{ id: 3, created_at_epoch: 1000 }, { id: 2, created_at_epoch: 1000 }, { id: 1, created_at_epoch: 1000 }], [{ id: 1, displayEpoch: 1000 }], ['observation:1', 'observation:2', 'observation:3', 'summary:1']],
    ['orders one turn by id when it arrives ranked', [{ id: 2, created_at_epoch: 1000 }, { id: 3, created_at_epoch: 1000 }, { id: 1, created_at_epoch: 1000 }], [{ id: 1, displayEpoch: 1000 }], ['observation:1', 'observation:2', 'observation:3', 'summary:1']],
    ['reverses the read order of server ids', [{ id: 'b', created_at_epoch: 1000 }, { id: 'c', created_at_epoch: 1000 }, { id: 'a', created_at_epoch: 1000 }], [], ['observation:a', 'observation:c', 'observation:b']],
    ['no observations', [], [{ id: 1, displayEpoch: 1000 }], ['summary:1']],
    ['no summaries', [{ id: 1, created_at_epoch: 1000 }], [], ['observation:1']],
    ['both empty', [], [], []],
    ['tags both kinds', [{}], [{}], ['observation:1', 'summary:1']],
    ['uses summary display epoch', [{ id: 1, created_at_epoch: 2000 }], [{ id: 1, created_at_epoch: 3000, displayEpoch: 1000 }], ['summary:1', 'observation:1']],
  ] as [string, Partial<Observation>[], Partial<SummaryTimelineItem>[], string[]][])('%s', (_, observations, summaries, expected) => {
    const timeline = buildTimeline(observations.map(createTestObservation), summaries.map(createTestSummaryTimelineItem));
    expect(timeline.map(item => `${item.type}:${item.data.id}`)).toEqual(expected);
    for (const item of timeline) {
      expect(item.data).toHaveProperty(item.type === 'observation' ? 'narrative' : 'request');
    }
  });
});

describe('timeline of turns that stored several observations', () => {
  it('lists each turn oldest first, as the turns themselves are', () => {
    const store = new SessionStore(':memory:');
    try {
      const sessionDbId = store.createSDKSession('content-turns', 'turn-order', 'prompt');
      store.ensureMemorySessionIdRegistered(sessionDbId, 'memory-turns');
      const observation = (title: string) => ({
        type: 'discovery', title, subtitle: null, facts: [], narrative: title,
        concepts: ['turn-order'], files_read: [], files_modified: [],
      });
      // One observer response per turn; storeObservations stamps each turn once.
      const first = store.storeObservations('memory-turns', 'turn-order',
        [observation('read the config'), observation('found the bug'), observation('fixed the bug')], null, 1, 0, 1_700_000_000_000);
      const second = store.storeObservations('memory-turns', 'turn-order',
        [observation('wrote the test'), observation('ran the test')], null, 2, 0, 1_700_000_060_000);

      const rows = queryObservationsMulti(store, ['turn-order'], config('turn-order'));
      expect(buildTimeline(rows, []).map(item => item.data.id))
        .toEqual([...first.observationIds, ...second.observationIds]);
    } finally {
      store.close();
    }
  });
});

describe('timeline of a turn that reinforcement ranking selected', () => {
  it('lists the turn in stored order when one of its rows was confirmed again', () => {
    const store = new SessionStore(':memory:');
    try {
      const day = 86_400_000;
      const now = Date.now();
      const sessionDbId = store.createSDKSession('content-ranked', 'ranked-turn', 'prompt');
      store.ensureMemorySessionIdRegistered(sessionDbId, 'memory-ranked');
      const observation = (title: string) => ({
        type: 'discovery', title, subtitle: null, facts: [], narrative: title,
        concepts: ['ranked-turn'], files_read: [], files_modified: [],
      });
      store.storeObservation('memory-ranked', 'ranked-turn', observation('old 40d'), 1, 0, now - 40 * day);
      store.storeObservation('memory-ranked', 'ranked-turn', observation('old 30d'), 2, 0, now - 30 * day);
      const turn = store.storeObservations('memory-ranked', 'ranked-turn',
        [observation('read the config'), observation('found the bug'), observation('fixed the bug')], null, 3, 0, now - 2 * day);
      // The same session reports the middle row again a day later: the store reinforces it.
      store.storeObservation('memory-ranked', 'ranked-turn', observation('found the bug'), 4, 0, now - day);
      const today = store.storeObservation('memory-ranked', 'ranked-turn', observation('today'), 5, 0, now).id;

      // More candidates (6) than the window (4), so the ranked path runs and
      // puts the reinforced row ahead of the rest of its turn.
      const ranked = { ...config('ranked-turn'), totalObservationCount: 4, reinforcementAlpha: 1 };
      const rows = queryObservationsMulti(store, ['ranked-turn'], ranked);
      expect(rows.map(row => row.id)).toEqual([today, turn.observationIds[1], turn.observationIds[2], turn.observationIds[0]]);
      expect(buildTimeline(rows, []).map(item => item.data.id)).toEqual([...turn.observationIds, today]);
    } finally {
      store.close();
    }
  });
});

describe('context compiler platform scoping', () => {
  const contextConfig = config('platform-scope');

  function seed(
    store: SessionStore,
    input: {
      project: string;
      contentSessionId: string;
      memorySessionId: string;
      platformSource: string;
      title: string;
      summaryRequest: string;
      createdAtEpoch: number;
    },
  ): void {
    const sessionDbId = store.createSDKSession(
      input.contentSessionId,
      input.project,
      `${input.platformSource} prompt`,
      undefined,
      input.platformSource,
    );
    store.ensureMemorySessionIdRegistered(sessionDbId, input.memorySessionId);
    store.storeObservation(
      input.memorySessionId,
      input.project,
      {
        type: 'discovery',
        title: input.title,
        subtitle: null,
        facts: [],
        narrative: `${input.platformSource} context narrative`,
        concepts: ['platform-scope'],
        files_read: [],
        files_modified: [],
      },
      1,
      0,
      input.createdAtEpoch,
    );
    store.storeSummary(
      input.memorySessionId,
      input.project,
      {
        request: input.summaryRequest,
        investigated: 'investigated',
        learned: 'learned',
        completed: 'completed',
        next_steps: 'next',
        notes: null,
      },
      1,
      0,
      input.createdAtEpoch,
    );
  }

  it('filters observations, summaries, and project counts by platformSource when supplied', () => {
    const store = new SessionStore(':memory:');
    try {
      seed(store, {
        project: 'context-platform-project',
        contentSessionId: 'shared-context-id',
        memorySessionId: 'codex-context-memory',
        platformSource: 'codex',
        title: 'CODEX_CONTEXT_OBS',
        summaryRequest: 'CODEX_CONTEXT_SUMMARY',
        createdAtEpoch: 1_700_000_000_000,
      });
      seed(store, {
        project: 'context-platform-project',
        contentSessionId: 'shared-context-id',
        memorySessionId: 'claude-context-memory',
        platformSource: 'claude',
        title: 'CLAUDE_CONTEXT_OBS',
        summaryRequest: 'CLAUDE_CONTEXT_SUMMARY',
        createdAtEpoch: 1_700_000_001_000,
      });

      const codexObservations = queryObservationsMulti(store, ['context-platform-project'], contextConfig, 'codex');
      expect(codexObservations.map(obs => obs.title)).toEqual(['CODEX_CONTEXT_OBS']);
      expect(codexObservations[0].platform_source).toBe('codex');

      const codexSummaries = querySummariesMulti(store, ['context-platform-project'], contextConfig, 'codex');
      expect(codexSummaries.map(summary => summary.request)).toEqual(['CODEX_CONTEXT_SUMMARY']);
      expect(codexSummaries[0].platform_source).toBe('codex');

      expect(countObservationsByProjects(store, ['context-platform-project'], 'codex')).toBe(1);
      expect(countObservationsByProjects(store, ['context-platform-project'], 'claude')).toBe(1);
      expect(countObservationsByProjects(store, ['context-platform-project'])).toBe(2);
    } finally {
      store.close();
    }
  });

  it('applies platformSource across multi-project context queries', () => {
    const store = new SessionStore(':memory:');
    try {
      seed(store, {
        project: 'context-parent',
        contentSessionId: 'parent-codex',
        memorySessionId: 'parent-codex-memory',
        platformSource: 'codex',
        title: 'PARENT_CODEX_OBS',
        summaryRequest: 'PARENT_CODEX_SUMMARY',
        createdAtEpoch: 1_700_000_000_000,
      });
      seed(store, {
        project: 'context-worktree',
        contentSessionId: 'worktree-codex',
        memorySessionId: 'worktree-codex-memory',
        platformSource: 'codex',
        title: 'WORKTREE_CODEX_OBS',
        summaryRequest: 'WORKTREE_CODEX_SUMMARY',
        createdAtEpoch: 1_700_000_001_000,
      });
      seed(store, {
        project: 'context-worktree',
        contentSessionId: 'worktree-claude',
        memorySessionId: 'worktree-claude-memory',
        platformSource: 'claude',
        title: 'WORKTREE_CLAUDE_OBS',
        summaryRequest: 'WORKTREE_CLAUDE_SUMMARY',
        createdAtEpoch: 1_700_000_002_000,
      });

      const projects = ['context-parent', 'context-worktree'];
      expect(queryObservationsMulti(store, projects, contextConfig, 'codex').map(obs => obs.title)).toEqual([
        'WORKTREE_CODEX_OBS',
        'PARENT_CODEX_OBS',
      ]);
      expect(querySummariesMulti(store, projects, contextConfig, 'codex').map(summary => summary.request)).toEqual([
        'WORKTREE_CODEX_SUMMARY',
        'PARENT_CODEX_SUMMARY',
      ]);
    } finally {
      store.close();
    }
  });
});

describe('case-insensitive project retrieval (#3531)', () => {
  const contextConfig = config('case-scope');

  it('resolves rows stored under a mixed-case key when queried with a different case', () => {
    const store = new SessionStore(':memory:');
    try {
      // Machine A wrote the bucket as `PasteyPal` (mixed case). Machine B, whose
      // directory name differs only in case, now derives `pasteypal` and must
      // still reach those rows.
      const sessionDbId = store.createSDKSession('content-3531', 'PasteyPal', 'prompt');
      store.ensureMemorySessionIdRegistered(sessionDbId, 'mem-3531');
      store.storeObservation(
        'mem-3531',
        'PasteyPal',
        {
          type: 'discovery',
          title: 'CASE_OBS',
          subtitle: null,
          facts: [],
          narrative: 'case narrative',
          concepts: ['case-scope'],
          files_read: [],
          files_modified: [],
        },
        1,
        0,
        1_700_000_000_000,
      );
      store.storeSummary(
        'mem-3531',
        'PasteyPal',
        {
          request: 'CASE_SUMMARY',
          investigated: 'investigated',
          learned: 'learned',
          completed: 'completed',
          next_steps: 'next',
          notes: null,
        },
        1,
        0,
        1_700_000_000_000,
      );

      expect(queryObservationsMulti(store, ['pasteypal'], contextConfig).map(o => o.title)).toEqual(['CASE_OBS']);
      expect(querySummariesMulti(store, ['pasteypal'], contextConfig).map(s => s.request)).toEqual(['CASE_SUMMARY']);
      expect(countObservationsByProjects(store, ['pasteypal'])).toBe(1);
    } finally {
      store.close();
    }
  });
});

describe('concept exact-match injection (#3379)', () => {
  const contextConfig = config('gotcha');

  it('excludes a row whose stored concept carries a "keyword: description" prefix', () => {
    // The injection query matches concepts exactly (`WHERE value IN (...)`).
    // A row stored as "gotcha: x" must NOT match — this is the #3379 defect
    // that the parser normalization and the v49 backfill remove at the write
    // side; the query itself intentionally stays exact-match.
    const db = new Database(':memory:');
    try {
      const store = new SessionStore(db);
      const sessionDbId = store.createSDKSession('content-3379', 'concept-project', 'prompt');
      store.ensureMemorySessionIdRegistered(sessionDbId, 'mem-3379');
      // Insert directly: the fresh store is already past v49, so this mimics
      // a malformed row written before the migration existed.
      db.prepare(`
        INSERT INTO observations (memory_session_id, project, type, title, concepts, created_at, created_at_epoch)
        VALUES ('mem-3379', 'concept-project', 'discovery', 'MALFORMED_CONCEPT_OBS', '["gotcha: x"]', ?, ?)
      `).run(new Date().toISOString(), 1_700_000_000_000);

      expect(queryObservationsMulti(store, ['concept-project'], contextConfig)).toEqual([]);
    } finally {
      db.close();
    }
  });

  it('skips a row whose concepts value is not valid JSON and still returns the other rows', () => {
    const db = new Database(':memory:');
    try {
      const store = new SessionStore(db);
      const sessionDbId = store.createSDKSession('content-bad-json', 'concept-project', 'prompt');
      store.ensureMemorySessionIdRegistered(sessionDbId, 'mem-bad-json');
      const insert = db.prepare(`
        INSERT INTO observations (memory_session_id, project, type, title, concepts, created_at, created_at_epoch)
        VALUES ('mem-bad-json', 'concept-project', 'discovery', ?, ?, ?, ?)
      `);
      insert.run('GOOD_OBS', '["gotcha"]', new Date().toISOString(), 1_700_000_000_000);
      insert.run('BAD_JSON_OBS', 'gotcha, [unterminated', new Date().toISOString(), 1_700_000_100_000);

      expect(queryObservationsMulti(store, ['concept-project'], contextConfig).map(obs => obs.title)).toEqual(['GOOD_OBS']);
      expect(queryObservationsNewest(store, contextConfig, { limit: 10 }).map(obs => obs.title)).toEqual(['GOOD_OBS']);
    } finally {
      db.close();
    }
  });
});

describe('queryObservationsNewest house feed', () => {
  const contextConfig = config('platform-scope');

  it('returns newest rows across projects when no project filter is passed', () => {
    const store = new SessionStore(':memory:');
    try {
      const seat = store.createSDKSession('seat-content', 'cmem_work_thin', 'seat', undefined, 'grok-bot');
      store.ensureMemorySessionIdRegistered(seat, 'seat-mem');
      store.storeObservation('seat-mem', 'cmem_work_thin', {
        type: 'discovery',
        title: 'SEAT_ONLY',
        subtitle: null,
        facts: [],
        narrative: 'thin diary',
        concepts: ['platform-scope'],
        files_read: [],
        files_modified: [],
      }, 1, 0, 1_700_000_000_000);

      const house = store.createSDKSession('house-content', 'claude-mem', 'house', undefined, 'claude');
      store.ensureMemorySessionIdRegistered(house, 'house-mem');
      store.storeObservation('house-mem', 'claude-mem', {
        type: 'discovery',
        title: 'HOUSE_NEWEST',
        subtitle: null,
        facts: [],
        narrative: 'house feed',
        concepts: ['platform-scope'],
        files_read: [],
        files_modified: [],
      }, 1, 0, 1_700_000_100_000);

      const scoped = queryObservationsNewest(store, contextConfig, {
        limit: 10,
        projects: ['cmem_work_thin'],
      });
      expect(scoped.map(obs => obs.title)).toEqual(['SEAT_ONLY']);

      const houseFeed = queryObservationsNewest(store, contextConfig, { limit: 10 });
      expect(houseFeed.map(obs => obs.title)).toEqual(['HOUSE_NEWEST', 'SEAT_ONLY']);
    } finally {
      store.close();
    }
  });
});

describe('context compiler main-agent-only injection filtering', () => {
  const baseConfig = config('agent-scope');

  function seedObs(
    store: SessionStore,
    input: {
      project: string;
      contentSessionId: string;
      memorySessionId: string;
      title: string;
      agentId: string | null;
      agentType: string | null;
      createdAtEpoch: number;
    },
  ): void {
    const sessionDbId = store.createSDKSession(
      input.contentSessionId,
      input.project,
      'prompt',
      undefined,
      'claude',
    );
    store.ensureMemorySessionIdRegistered(sessionDbId, input.memorySessionId);
    store.storeObservation(
      input.memorySessionId,
      input.project,
      {
        type: 'discovery',
        title: input.title,
        subtitle: null,
        facts: [],
        narrative: 'agent scope narrative',
        concepts: ['agent-scope'],
        files_read: [],
        files_modified: [],
        agent_id: input.agentId,
        agent_type: input.agentType,
      },
      1,
      0,
      input.createdAtEpoch,
    );
  }

  function seedMix(store: SessionStore, project: string): void {
    seedObs(store, {
      project,
      contentSessionId: 'main-session',
      memorySessionId: 'main-memory',
      title: 'MAIN_OBS',
      agentId: null,
      agentType: null,
      createdAtEpoch: 1_700_000_000_000,
    });
    // A Claude Code subagent: the hook sends both agent_id and agent_type.
    seedObs(store, {
      project,
      contentSessionId: 'sub-session',
      memorySessionId: 'sub-memory',
      title: 'SUB_OBS',
      agentId: 'agent-42',
      agentType: 'Explore',
      createdAtEpoch: 1_700_000_001_000,
    });
  }

  it('keeps main-agent rows that carry only one agent field', () => {
    const store = new SessionStore(':memory:');
    try {
      seedMix(store, 'agent-scope-project');
      // Transcript-watch ingestion stamps agent_id alone on main-agent rows
      // (a Grok Bot seat id). Filtering on agent_id alone would make
      // session_start_context return nothing for those seats.
      seedObs(store, {
        project: 'agent-scope-project',
        contentSessionId: 'grok-seat-session',
        memorySessionId: 'grok-seat-memory',
        title: 'GROK_SEAT_OBS',
        agentId: 'grok-seat-7',
        agentType: null,
        createdAtEpoch: 1_700_000_002_000,
      });
      // `claude --agent reviewer` runs a main thread that carries agent_type alone.
      seedObs(store, {
        project: 'agent-scope-project',
        contentSessionId: 'agent-main-session',
        memorySessionId: 'agent-main-memory',
        title: 'AGENT_MAIN_OBS',
        agentId: null,
        agentType: 'reviewer',
        createdAtEpoch: 1_700_000_003_000,
      });

      const observations = queryObservationsMulti(store, ['agent-scope-project'], baseConfig);
      expect(observations.map(obs => obs.title)).toEqual(['AGENT_MAIN_OBS', 'GROK_SEAT_OBS', 'MAIN_OBS']);
    } finally {
      store.close();
    }
  });

  it('excludes subagent observations from the injection window when mainAgentOnly is true (default)', () => {
    const store = new SessionStore(':memory:');
    try {
      seedMix(store, 'agent-scope-project');

      const observations = queryObservationsMulti(store, ['agent-scope-project'], baseConfig);
      expect(observations.map(obs => obs.title)).toEqual(['MAIN_OBS']);
    } finally {
      store.close();
    }
  });

  it('keeps subagent observations on a project-scoped newest query', () => {
    const store = new SessionStore(':memory:');
    try {
      seedMix(store, 'agent-scope-project');

      const observations = queryObservationsNewest(store, baseConfig, {
        limit: 20,
        projects: ['agent-scope-project'],
        includeManualSaves: true,
      });
      expect(observations.map(obs => obs.title).sort()).toEqual(['MAIN_OBS', 'SUB_OBS']);
    } finally {
      store.close();
    }
  });

  it('includes subagent observations when mainAgentOnly is false (backward compat) and leaves the count query unfiltered', () => {
    const store = new SessionStore(':memory:');
    try {
      seedMix(store, 'agent-scope-project');

      const observations = queryObservationsMulti(
        store,
        ['agent-scope-project'],
        { ...baseConfig, mainAgentOnly: false },
      );
      expect(observations.map(obs => obs.title).sort()).toEqual(['MAIN_OBS', 'SUB_OBS']);

      // Guard the "don't over-filter" constraint: the count query has no
      // agent_id filter, so it still sees both main and subagent rows.
      expect(countObservationsByProjects(store, ['agent-scope-project'])).toBe(2);
    } finally {
      store.close();
    }
  });
});