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

2.8 KB
Raw Blame
import { expect, it } from 'bun:test';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';

// #6503: the KnowledgeAgent's prime and query sessions set a fixed title, so the
// Claude Code CLI skips its title-generation request. Runs the production agent
// in isolation; only the SDK and executable/OAuth discovery are replaced.
const fixture = String.raw`
  import { mock } from 'bun:test';
  const titles = [];
  mock.module('@anthropic-ai/claude-agent-sdk', () => ({ query: ({ options }) => {
    titles.push(options.title);
    return (async function* () {
      const session_id = options.resume ? 'answer-session' : 'primed-session';
      yield { type: 'system', subtype: 'init', session_id };
      if (options.resume) yield { type: 'assistant', message: { content: [{ type: 'text', text: 'Answer' }] } };
      yield { type: 'result', subtype: 'success', is_error: false, session_id, result: 'done' };
    })();
  } }));
  mock.module('./src/shared/EnvManager.ts', () => ({ buildIsolatedEnvWithFreshOAuth: async () => ({ PATH: process.env.PATH }) }));
  mock.module('./src/shared/find-claude-executable.ts', () => ({ findClaudeExecutable: () => '/fixture/claude', isClaudeExecutableUnspawnable: () => false }));
  const { KnowledgeAgent } = await import('./src/services/worker/knowledge/KnowledgeAgent.ts');
  const { CorpusStore } = await import('./src/services/worker/knowledge/CorpusStore.ts');
  const store = new CorpusStore();
  const agent = new KnowledgeAgent(store);
  const corpus = { version: 1, name: 'title-corpus', description: '', created_at: new Date().toISOString(),
    updated_at: new Date().toISOString(), filter: {},
    stats: { observation_count: 0, token_estimate: 0, date_range: { earliest: '', latest: '' }, type_breakdown: {} },
    system_prompt: 'Use only this corpus', session_id: null, observations: [] };
  store.write(corpus);
  await agent.prime(corpus);
  await agent.query(corpus, 'question');
  console.log(JSON.stringify(titles));
`;

it('sets a fixed title on the KnowledgeAgent prime and query sessions (#6503)', () => {
  const dir = mkdtempSync(join(tmpdir(), 'knowledge-session-title-'));
  try {
    const run = Bun.spawnSync([process.execPath, '-e', fixture], {
      cwd: join(import.meta.dir, '../../..'),
      env: { ...process.env, CLAUDE_MEM_DATA_DIR: join(dir, 'data'), CLAUDE_CONFIG_DIR: join(dir, 'config') },
      stdout: 'pipe', stderr: 'pipe',
    });
    if (run.exitCode !== 0) throw new Error(new TextDecoder().decode(run.stderr));
    const titles = JSON.parse(new TextDecoder().decode(run.stdout).trim().split('\n').at(-1)!);
    expect(titles).toEqual(['claude-mem knowledge agent', 'claude-mem knowledge agent']);
  } finally {
    rmSync(dir, { recursive: true, force: true });
  }
});