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

7.7 KB
Raw Blame
import { afterAll, afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import type { NormalizedHookInput } from '../../src/cli/types.js';
import type { TranscriptSchema, WatchTarget } from '../../src/services/transcripts/types.js';

const sessionInitCalls: NormalizedHookInput[] = [];

// Snapshot the real module BEFORE mock.module mutates the live namespace, then
// re-register it in afterAll. bun's mock.module is process-global and
// mock.restore() does NOT undo it, so this partial session-init stub would
// otherwise leak into other test files in the same `bun test` run.
import * as realSessionInit from '../../src/cli/handlers/session-init.js';
const realSessionInitSnapshot = { ...realSessionInit };

const fakeSessionInit = async (input: NormalizedHookInput) => {
  sessionInitCalls.push(input);
  return { continue: true, suppressOutput: true };
};
mock.module('../../src/cli/handlers/session-init.js', () => ({
  sessionInitHandler: { execute: fakeSessionInit },
  recordSessionPrompt: fakeSessionInit,
}));

afterAll(() => {
  mock.module('../../src/cli/handlers/session-init.js', () => realSessionInitSnapshot);
});

import { logger } from '../../src/utils/logger.js';
import { TranscriptWatcher } from '../../src/services/transcripts/watcher.js';
import * as ingestion from '../../src/services/worker/http/shared.js';

interface ExampleConfig {
  schemas: Record<string, TranscriptSchema>;
}

const exampleConfig = JSON.parse(
  readFileSync(join(import.meta.dir, '..', '..', 'docs', 'transcript-watch.example.json'), 'utf8'),
) as ExampleConfig;
const codexSchema = exampleConfig.schemas.codex;

const responseItem = (role: string, text: string) =>
  JSON.stringify({ type: 'response_item', payload: { type: 'message', role, content: [{ text }] } });

// Rule matching and field extraction are covered in match-rule-multi-field.test.ts.
// This asserts the stronger claim #4211 actually makes: that the prompt which
// reaches session-init (and therefore sdk_sessions.user_prompt) is the real user
// prompt. Codex 0.155 injects a role:user preamble ahead of it, and the shipped
// schema has to drop the preamble without dropping the user's own message.
describe('shipped codex schema: prompt delivery to session-init', () => {
  let tmpRoot: string;
  let loggerSpies: Array<ReturnType<typeof spyOn>> = [];

  beforeEach(() => {
    sessionInitCalls.length = 0;
    tmpRoot = mkdtempSync(join(tmpdir(), 'claude-mem-codex-delivery-'));
    loggerSpies = [
      spyOn(logger, 'info').mockImplementation(() => {}),
      spyOn(logger, 'debug').mockImplementation(() => {}),
      spyOn(logger, 'warn').mockImplementation(() => {}),
      spyOn(logger, 'error').mockImplementation(() => {}),
    ];
  });

  afterEach(() => {
    loggerSpies.forEach(spy => spy.mockRestore());
    rmSync(tmpRoot, { recursive: true, force: true });
  });

  function rollout(sessionId: string, ...conversation: Array<[string, string]>): string[] {
    return [
      JSON.stringify({ type: 'session_meta', payload: { id: sessionId, cwd: tmpRoot } }),
      ...conversation.map(([role, text]) => responseItem(role, text)),
    ];
  }

  async function deliver(lines: string[], sessionId: string): Promise<void> {
    const filePath = join(tmpRoot, `${sessionId}.jsonl`);
    writeFileSync(filePath, `${lines.join('\n')}\n`, 'utf8');

    const watch: WatchTarget = {
      name: 'codex',
      path: join(tmpRoot, '*.jsonl'),
      schema: 'codex',
    };
    const watcher = new TranscriptWatcher(
      { version: 1, schemas: exampleConfig.schemas, watches: [watch] },
      join(tmpRoot, 'state.json'),
    );

    await (watcher as any).addTailer(filePath, watch, codexSchema);
    await (watcher as any).tailers.get(filePath).readTask;
    watcher.stop();
  }

  it('delivers the real user prompt and never the injected preamble', async () => {
    const sessionId = '019e050e-7ae0-71b2-b19f-6cc428e57601';
    await deliver(rollout(
      sessionId,
      ['developer', '<skills_instructions> ...'],
      ['user', '<recommended_plugins>\nHere is a list of plugins that are available but not installed'],
      ['user', 'Repeat this token back exactly once: ZORBAX-QQ-7731'],
      ['assistant', 'ZORBAX-QQ-7731'],
    ), sessionId);

    expect(sessionInitCalls).toHaveLength(1);
    expect(sessionInitCalls[0].prompt).toBe('Repeat this token back exactly once: ZORBAX-QQ-7731');
    expect(sessionInitCalls[0].sessionId).toBe(sessionId);
  });

  it('still delivers a user prompt that merely mentions the marker', async () => {
    // Regression guard: rejecting the preamble by substring would silently drop
    // this prompt, because it contains the marker without being one.
    const sessionId = '019e050e-7ae0-71b2-b19f-6cc428e57602';
    const ask = 'why does <recommended_plugins> show up before my message?';

    await deliver(rollout(
      sessionId,
      ['user', '<recommended_plugins>\nHere is a list of plugins'],
      ['user', ask],
    ), sessionId);

    expect(sessionInitCalls.map(call => call.prompt)).toEqual([ask]);
  });

  it('ignores developer and assistant turns in the same rollout', async () => {
    const sessionId = '019e050e-7ae0-71b2-b19f-6cc428e57603';
    await deliver(rollout(
      sessionId,
      ['developer', '<skills_instructions> ...'],
      ['developer', '<multi_agent_mode> ...'],
      ['assistant', 'working on it'],
    ), sessionId);

    expect(sessionInitCalls).toHaveLength(0);
  });

  it('retains every text part of the user request', async () => {
    const sessionId = '019e050e-7ae0-71b2-b19f-6cc428e57604';
    await deliver([
      ...rollout(sessionId),
      JSON.stringify({ type: 'response_item', payload: { type: 'message', role: 'user', content: [
        { type: 'input_text', text: 'Inspect the failed build.' },
        { type: 'input_text', text: 'Keep the public API unchanged.' },
      ] } }),
    ], sessionId);

    expect(sessionInitCalls.map(call => call.prompt)).toEqual(['Inspect the failed build.\nKeep the public API unchanged.']);
  });

  it('retains the request text when an image comes before it', async () => {
    const sessionId = '019e050e-7ae0-71b2-b19f-6cc428e57605';
    await deliver([
      ...rollout(sessionId),
      JSON.stringify({ type: 'response_item', payload: { type: 'message', role: 'user', content: [
        { type: 'input_image', image_url: 'data:image/png;base64,fixture' },
        { type: 'input_text', text: 'Fix the layout shown in this screenshot.' },
      ] } }),
    ], sessionId);

    expect(sessionInitCalls.map(call => call.prompt)).toEqual(['Fix the layout shown in this screenshot.']);
  });

  it('hands the full assistant answer to summary ingestion', async () => {
    const sessionId = '019e050e-7ae0-71b2-b19f-6cc428e57606';
    const summary = spyOn(ingestion, 'ingestSummarize').mockImplementation(async () => ({ status: 'queued' }));
    try {
      await deliver([
        ...rollout(sessionId, ['user', 'Repair the build and document the remaining issue.']),
        JSON.stringify({ type: 'response_item', payload: { type: 'message', role: 'assistant', content: [
          { type: 'output_text', text: 'The build is repaired.' },
          { type: 'output_text', text: 'The deployment still requires a credential.' },
        ] } }),
        JSON.stringify({ type: 'event_msg', payload: { type: 'task_complete' } }),
      ], sessionId);

      expect(summary).toHaveBeenCalledTimes(1);
      expect(summary.mock.calls[0][0]).toMatchObject({
        contentSessionId: sessionId,
        lastAssistantMessage: 'The build is repaired.\nThe deployment still requires a credential.',
      });
    } finally {
      summary.mockRestore();
    }
  });
});