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, expect, test } from 'bun:test';
import { Database } from 'bun:sqlite';
import { existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { ensureLocalDeviceIdentity } from '../../../src/services/sync/local-device-identity.js';
import { SessionStore } from '../../../src/services/sqlite/SessionStore.js';
import { publishSharedRecords } from '../../../src/services/work-view/shared-record-store.mjs';
import { scratchDir } from '../../helpers/scratch-dir.js';
const cleanup: Array<() => void> = [];
afterEach(() => { for (const close of cleanup.splice(0).reverse()) close(); });
function fixture() {
const dir = scratchDir('local-device-identity');
cleanup.push(() => rmSync(dir, { recursive: true, force: true }));
const db = new Database(join(dir, 'state.db'));
db.exec('PRAGMA busy_timeout=5000');
cleanup.push(() => db.close());
const path = join(dir, 'settings.json');
writeFileSync(path, '{}');
return { dir, db, path };
}
test('pins one durable identity and restores the same settings key before enabling cloud', () => {
const { db, path } = fixture();
const id = ensureLocalDeviceIdentity(db, path);
expect(id).toMatch(/^[0-9a-f-]{36}$/);
expect(JSON.parse(readFileSync(path, 'utf8')).CLAUDE_MEM_CLOUD_SYNC_DEVICE_ID).toBe(id);
writeFileSync(path, '{}');
expect(ensureLocalDeviceIdentity(db, path)).toBe(id);
expect(JSON.parse(readFileSync(path, 'utf8')).CLAUDE_MEM_CLOUD_SYNC_DEVICE_ID).toBe(id);
});
test('adopts configured or legacy native identity, preserving wrapped settings', () => {
const configured = fixture();
writeFileSync(configured.path, JSON.stringify({ theme: 'dark', env: { CLAUDE_MEM_MODEL: 'model' } }));
expect(ensureLocalDeviceIdentity(configured.db, configured.path, 'configured-device')).toBe('configured-device');
expect(JSON.parse(readFileSync(configured.path, 'utf8'))).toMatchObject({ theme: 'dark', env: { CLAUDE_MEM_MODEL: 'model', CLAUDE_MEM_CLOUD_SYNC_DEVICE_ID: 'configured-device' } });
const native = fixture();
new SessionStore(native.db);
publishSharedRecords(native.db, { deviceId: 'native-device', project: 'repo', updates: [{ kind: 'group', key: 'g', value: 'retained' }] });
expect(ensureLocalDeviceIdentity(native.db, native.path)).toBe('native-device');
});
test('does not adopt a device writer from an authored version-like list name', () => {
const { db, path } = fixture();
const store = new SessionStore(db);
store.appendWorkStateEntry({ project: 'repo', listName: '__my_work_v1draft:notes', fields: { writer: 'document-author' } });
expect(ensureLocalDeviceIdentity(db, path, 'configured-device')).toBe('configured-device');
expect(JSON.parse(readFileSync(path, 'utf8')).CLAUDE_MEM_CLOUD_SYNC_DEVICE_ID).toBe('configured-device');
});
test('refuses a real identity conflict without rotating either stored identity', () => {
const { db, path } = fixture();
ensureLocalDeviceIdentity(db, path, 'original-device');
const before = readFileSync(path, 'utf8');
expect(() => ensureLocalDeviceIdentity(db, path, 'other-device')).toThrow('conflicts');
expect(readFileSync(path, 'utf8')).toBe(before);
expect(db.query('SELECT device_id FROM local_work_view_identity').get()).toEqual({ device_id: 'original-device' });
});
test('does not establish an identity when settings cannot be persisted', () => {
const { db, dir } = fixture();
expect(() => ensureLocalDeviceIdentity(db, dir, 'device-a')).toThrow();
expect(db.query("SELECT name FROM sqlite_master WHERE name='local_work_view_identity'").get()).toBeNull();
});
test('keeps the pinned identity when settings.json is not valid JSON, without overwriting it', () => {
const { db, path } = fixture();
const id = ensureLocalDeviceIdentity(db, path);
writeFileSync(path, '{ "CLAUDE_MEM_MODEL": ');
expect(ensureLocalDeviceIdentity(db, path)).toBe(id);
expect(readFileSync(path, 'utf8')).toBe('{ "CLAUDE_MEM_MODEL": ');
});
test('names settings.json when an unreadable file blocks the first identity', () => {
const { db, path } = fixture();
writeFileSync(path, '{ "CLAUDE_MEM_MODEL": ');
expect(() => ensureLocalDeviceIdentity(db, path)).toThrow(`Could not save the local device identity to ${path}`);
expect(readFileSync(path, 'utf8')).toBe('{ "CLAUDE_MEM_MODEL": ');
expect(db.query("SELECT name FROM sqlite_master WHERE name='local_work_view_identity'").get()).toBeNull();
});
test('concurrent pre-sync processes acquire one identity through the database lock', async () => {
const { db, dir, path } = fixture();
const moduleUrl = new URL('../../../src/services/sync/local-device-identity.ts', import.meta.url).href;
const start = join(dir, 'start');
const ready = [join(dir, 'ready-0'), join(dir, 'ready-1')];
const children = ready.map(pathname => Bun.spawn([process.execPath, '--eval', `
import { Database } from 'bun:sqlite';
import { existsSync, writeFileSync } from 'node:fs';
import { ensureLocalDeviceIdentity } from ${JSON.stringify(moduleUrl)};
const db = new Database(${JSON.stringify(join(dir, 'state.db'))});
db.exec('PRAGMA busy_timeout=5000');
writeFileSync(${JSON.stringify(pathname)}, '');
while (!existsSync(${JSON.stringify(start)})) await Bun.sleep(1);
try { console.log('IDENTITY=' + ensureLocalDeviceIdentity(db, ${JSON.stringify(path)})); } finally { db.close(); }
`], { stdout: 'pipe', stderr: 'pipe', env: { PATH: process.env.PATH, CLAUDE_MEM_DATA_DIR: dir } }));
try {
const deadline = Date.now() + 4_000;
while (!ready.every(pathname => existsSync(pathname))) {
if (Date.now() > deadline) throw new Error('identity processes did not reach the start barrier');
await Bun.sleep(5);
}
writeFileSync(start, '');
const ids = await Promise.all(children.map(async child => {
const [stdout, stderr, exit] = await Promise.all([new Response(child.stdout).text(), new Response(child.stderr).text(), child.exited]);
expect(exit, stderr).toBe(0);
const id = /IDENTITY=([^\n]+)/.exec(stdout)?.[1];
expect(id).toBeTruthy();
return id;
}));
expect(ids[0]).toBe(ids[1]);
expect(ensureLocalDeviceIdentity(db, path)).toBe(ids[0]);
} finally {
for (const child of children) {
if (child.exitCode === null) child.kill();
}
await Promise.all(children.map(child => child.exited));
}
});
test('confirms an identity already pinned and saved without taking the write lock', () => {
const { db, dir, path } = fixture();
const id = ensureLocalDeviceIdentity(db, path);
// Another connection holds the write lock, and this one would not wait for it: only reads can succeed.
const holder = new Database(join(dir, 'state.db'));
cleanup.push(() => holder.close());
holder.exec('BEGIN IMMEDIATE');
db.exec('PRAGMA busy_timeout=0');
expect(ensureLocalDeviceIdentity(db, path)).toBe(id);
expect(ensureLocalDeviceIdentity(db, path, id)).toBe(id);
// A conflict is still refused before anything is written.
expect(() => ensureLocalDeviceIdentity(db, path, 'other-device')).toThrow('conflicts');
// Settings that lost the identity need a write, which waits for the lock like any other.
writeFileSync(path, '{}');
expect(() => ensureLocalDeviceIdentity(db, path)).toThrow('database is locked');
holder.exec('COMMIT');
expect(ensureLocalDeviceIdentity(db, path)).toBe(id);
expect(JSON.parse(readFileSync(path, 'utf8')).CLAUDE_MEM_CLOUD_SYNC_DEVICE_ID).toBe(id);
});