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
TypeScript aiai-agentsai-memoryanthropicartificial-intelligencechromadbclaudeclaude-agent-sdkclaude-agentsclaude-codeclaude-code-pluginclaude-skillsembeddingslong-term-memorymem0memory-engineopenmemoryragsqlitesupermemory
import { afterEach, describe, expect, it } from 'bun:test';
import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
const skill = readFileSync(join(import.meta.dir, '../../plugin/skills/weekly-digests/SKILL.md'), 'utf8');
const oldTimeline = 'Previously saved timeline\n';
const newTimeline = '# Project timeline\n\n### Dec 2, 2026\n1 9:00am 💡 Recorded a decision\n';
const unauthorized = JSON.stringify({ error: 'Unauthorized', message: 'Missing or invalid local API token' });
const tempDirs: string[] = [];
afterEach(() => {
for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true });
});
async function fetchTimeline(response: Response | null, previous?: string) {
const step = skill.match(/^### Step 2:[^\r\n]*\r?\n[\s\S]*?```bash\r?\n([\s\S]*?)\r?\n```/m);
if (!step) throw new Error('Missing Step 2 Bash block in weekly-digests SKILL.md');
const cwd = mkdtempSync(join(tmpdir(), 'claude-mem-weekly-digests-'));
tempDirs.push(cwd);
const scratch = join(cwd, '.scratch');
const timeline = join(scratch, 'cm-timeline.md');
if (previous !== undefined) {
mkdirSync(scratch);
writeFileSync(timeline, previous);
}
const server = Bun.serve({
hostname: '127.0.0.1',
port: 0,
fetch(request) {
const url = new URL(request.url);
if (request.method !== 'GET' || url.pathname + url.search !== '/api/context/inject?project=sample-project&full=true') {
return new Response(newTimeline, { status: url.pathname === '/redirected' ? 200 : 404 });
}
return response!;
},
});
const port = String(server.port);
try {
if (!response) await server.stop(true);
const command = step[1].replaceAll('\r\n', '\n').replaceAll('PROJECT_NAME', 'sample-project');
const child = Bun.spawn(['bash', '-c', command], {
cwd,
env: { ...process.env, WORKER_PORT: port, NO_PROXY: 'localhost', no_proxy: 'localhost' },
stdout: 'pipe',
stderr: 'pipe',
});
const [exitCode, stdout, stderr] = await Promise.all([
child.exited,
new Response(child.stdout).text(),
new Response(child.stderr).text(),
]);
return {
exitCode,
stdout,
stderr,
timeline: existsSync(timeline) ? readFileSync(timeline, 'utf8') : null,
files: readdirSync(scratch),
};
} finally {
if (response) await server.stop(true);
}
}
describe.skipIf(!Bun.which('bash') || !Bun.which('curl'))('weekly-digests Step 2', () => {
it('saves a non-empty 200 response over the previous timeline', async () => {
const result = await fetchTimeline(new Response(newTimeline), oldTimeline);
expect(result.exitCode, result.stderr).toBe(0);
expect(result.timeline).toBe(newTimeline);
expect(result.stdout).toMatch(/4\s+\.scratch\/cm-timeline\.md/);
expect(result.files).toEqual(['cm-timeline.md']);
});
const failureCases = [
{ name: 'preserves the previous timeline on HTTP 401', status: 401, body: unauthorized, previous: oldTimeline, diagnostic: '401' },
{ name: 'leaves the timeline absent on HTTP 401', status: 401, body: unauthorized, diagnostic: '401' },
{ name: 'rejects an HTTP redirect', status: 302, body: 'Redirecting', diagnostic: '302' },
{ name: 'rejects an HTTP server error', status: 500, body: 'Server error', diagnostic: '500' },
{ name: 'rejects an empty HTTP 200 body', status: 200, body: '', diagnostic: 'empty' },
{ name: 'rejects a connection refusal', status: null, diagnostic: 'fetch failed' },
];
for (const { name, status, body, previous, diagnostic } of failureCases) {
it(name, async () => {
const headers = status === 302 ? { Location: '/redirected' } : undefined;
const response = status === null ? null : new Response(body, { status, headers });
const result = await fetchTimeline(response, previous);
expect(result.exitCode, `Timeline after fetch: ${JSON.stringify(result.timeline)}\n${result.stderr}`).not.toBe(0);
expect(result.stderr).toContain(diagnostic);
expect(result.stdout).toBe('');
expect(result.timeline).toBe(previous ?? null);
expect(result.files).toEqual(previous ? ['cm-timeline.md'] : []);
});
}
});