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 { describe, expect, it } from 'bun:test';
import { reapOwnedLiveTree, reapSnapshottedDescendants, runForMatchingProcess, snapshotDescendants, survivingProcesses, type ProcessIdentity } from './process-tree.js';
describe('ghost fixture descendant cleanup', () => {
const snapshot: ProcessIdentity[] = [
{ pid: 21, startToken: 'root-1', name: 'chroma-mcp.exe' },
{ pid: 22, startToken: 'python-1', name: 'python.exe' },
{ pid: 23, startToken: 'python-2', name: 'python.exe' },
];
it('leaves reused and unverified PIDs alone', () => {
const killed: number[] = [];
const checked: number[] = [];
reapSnapshottedDescendants(
[
...snapshot,
{ pid: 24, startToken: null, name: 'python.exe' },
],
(pid, token) => {
checked.push(pid);
// pid 22 was reused; pid 23 could not be read this time.
return pid === 21 && token === 'root-1';
},
pid => { killed.push(pid); }
);
expect(checked).toEqual([23, 22, 21]);
expect(killed).toEqual([21]);
});
it('keeps the discovery-row token when a child PID is reused before cleanup', () => {
const rows = [
{ pid: 20, ppid: 1, name: 'fixture.exe', startToken: '100' },
{ pid: 21, ppid: 20, name: 'chroma-mcp.exe', startToken: '200' },
{ pid: 22, ppid: 21, name: 'python.exe', startToken: '300' },
];
// A later token probe of pid 22 would see `python-replacement`. The
// snapshot must carry the token from the row that proved its ancestry.
const takeSnapshot = snapshotDescendants as (...args: unknown[]) => ProcessIdentity[];
const discovered = takeSnapshot(20, '100', rows);
expect(discovered.find(entry => entry.pid === 22)?.startToken).toBe('300');
const killed: number[] = [];
reapSnapshottedDescendants(
discovered,
(pid, token) => pid === 21 && token === '200',
pid => { killed.push(pid); }
);
expect(killed).toEqual([21]);
});
it('refuses to walk a recycled fixture root', () => {
const rows = [
{ pid: 20, ppid: 1, name: 'unrelated.exe', startToken: '400' },
// This child predates the replacement root but retains its former PPID.
{ pid: 21, ppid: 20, name: 'python.exe', startToken: '200' },
];
const takeSnapshot = snapshotDescendants as (...args: unknown[]) => ProcessIdentity[];
expect(() => takeSnapshot(20, '100', rows)).toThrow('fixture root identity');
});
it('excludes an older child and its subtree under a matching but reused parent PID', () => {
const rows = [
{ pid: 20, ppid: 1, name: 'fixture.exe', startToken: '20261006210000.000000' },
{ pid: 21, ppid: 20, name: 'old-python.exe', startToken: '20261006200000.000000' },
{ pid: 22, ppid: 20, name: 'new-uv.exe', startToken: '20261006210100.000000' },
// Even a newer process below the impossible edge is not our descendant.
{ pid: 23, ppid: 21, name: 'old-python-child.exe', startToken: '20261006210200.000000' },
];
const killed: number[] = [];
const discovered = reapOwnedLiveTree(
20, rows[0]!.startToken, pid => { killed.push(pid); }, () => true, rows
);
expect(discovered).toEqual([
{ pid: 22, name: 'new-uv.exe', startToken: '20261006210100.000000' },
]);
expect(killed).toEqual([22, 20]);
expect(survivingProcesses(discovered!, pid => pid === 21 || pid === 23, () => null)).toEqual([]);
// The recycle gate omits expectedRootToken; it must exclude the same branch.
expect(snapshotDescendants(20, undefined, rows)).toEqual(discovered!);
});
it('accepts the exact CIM token format for a chronological Windows chain', () => {
const rows = [
{ pid: 20, ppid: 1, name: 'fixture.exe', startToken: '20261006210000.000001' },
{ pid: 21, ppid: 20, name: 'uv.exe', startToken: '20261006210000.000002' },
{ pid: 22, ppid: 21, name: 'python.exe', startToken: '20261006210001.000000' },
];
const killed: number[] = [];
const discovered = reapOwnedLiveTree(20, rows[0]!.startToken, pid => { killed.push(pid); }, () => true, rows);
expect(discovered?.map(entry => entry.startToken)).toEqual([
'20261006210000.000002', '20261006210001.000000',
]);
expect(killed).toEqual([22, 21, 20]);
});
it.each(['100', '300'])('retains possible orphans from an earlier intermediate PID owner (%s)', childToken => {
const rows = [
{ pid: 20, ppid: 1, name: 'fixture.exe', startToken: '100' },
{ pid: 21, ppid: 20, name: 'uv.exe', startToken: '500' },
// The fixture's earlier uv could have spawned this child, then exited
// before the new uv reused pid 21. It can still be a real orphan.
{ pid: 22, ppid: 21, name: 'old-python.exe', startToken: childToken },
{ pid: 23, ppid: 22, name: 'old-python-child.exe', startToken: '400' },
];
const killed: number[] = [];
const discovered = reapOwnedLiveTree(20, '100', pid => { killed.push(pid); }, () => true, rows);
expect(discovered).toEqual([
{ pid: 21, name: 'uv.exe', startToken: '500' },
{ pid: 22, name: 'old-python.exe', startToken: null },
{ pid: 23, name: 'old-python-child.exe', startToken: null },
]);
expect(killed).toEqual([21, 20]);
expect(survivingProcesses(discovered!, pid => pid === 22 || pid === 23, () => null)).toEqual([
{ pid: 22, name: 'old-python.exe', startToken: null },
{ pid: 23, name: 'old-python-child.exe', startToken: null },
]);
});
it('excludes a stale intermediate branch only when it predates the fixture root', () => {
const rows = [
{ pid: 20, ppid: 1, name: 'fixture.exe', startToken: '100' },
{ pid: 21, ppid: 20, name: 'uv.exe', startToken: '500' },
{ pid: 22, ppid: 21, name: 'old-python.exe', startToken: '50' },
{ pid: 23, ppid: 22, name: 'old-python-child.exe', startToken: '600' },
];
const discovered = snapshotDescendants(20, '100', rows);
expect(discovered).toEqual([{ pid: 21, name: 'uv.exe', startToken: '500' }]);
expect(survivingProcesses(discovered, pid => pid === 22 || pid === 23, () => null)).toEqual([]);
});
it.each(['150', null, 'malformed'])('retains possible fixture orphans below an unrelated branch (%s)', orphanToken => {
const rows = [
{ pid: 20, ppid: 1, name: 'fixture.exe', startToken: '100' },
{ pid: 21, ppid: 20, name: 'old-unrelated.exe', startToken: '50' },
// A later unrelated process reused the PID of a fixture-owned process
// that had spawned pid 23. Do not prune the whole branch at pid 21.
{ pid: 22, ppid: 21, name: 'new-unrelated.exe', startToken: '200' },
{ pid: 23, ppid: 22, name: 'python-orphan.exe', startToken: orphanToken },
{ pid: 24, ppid: 23, name: 'python-child.exe', startToken: '300' },
];
const discovered = snapshotDescendants(20, '100', rows);
expect(discovered).toEqual([
{ pid: 23, name: 'python-orphan.exe', startToken: null },
{ pid: 24, name: 'python-child.exe', startToken: null },
]);
const killed: number[] = [];
reapSnapshottedDescendants(discovered, () => true, pid => { killed.push(pid); });
expect(killed).toEqual([]);
expect(survivingProcesses(discovered, () => true, () => null)).toEqual(discovered);
});
it.each([
'20261006210000.000001',
'9007199254740993',
'utc:Tue Oct 6 21:00:00 2026',
])('accepts equal creation times (%s)', token => {
const rows = [
{ pid: 20, ppid: 1, name: 'fixture.exe', startToken: token },
{ pid: 21, ppid: 20, name: 'python.exe', startToken: token },
];
expect(snapshotDescendants(20, token, rows)).toEqual([
{ pid: 21, name: 'python.exe', startToken: token },
]);
});
it.each([
['20261006210000.000002', '20261006210000.000001'],
['9007199254740993', '9007199254740992'],
['utc:Tue Oct 6 21:00:01 2026', 'utc:Tue Oct 6 21:00:00 2026'],
])('excludes a child strictly older than its parent (%s > %s)', (parentToken, childToken) => {
const rows = [
{ pid: 20, ppid: 1, name: 'fixture.exe', startToken: parentToken },
{ pid: 21, ppid: 20, name: 'python.exe', startToken: childToken },
];
expect(snapshotDescendants(20, parentToken, rows)).toEqual([]);
});
it.each([
[null, '100'],
['100', null],
['', '100'],
['100', ''],
['malformed', '100'],
['100', 'malformed'],
['20261006210000.invalid', '20261006210000.000001'],
['20261006210000.000001', '20261006210000.invalid'],
['malformed', 'utc:Tue Oct 6 21:00:00 2026'],
['utc:Tue Oct 6 21:00:00 2026', 'malformed'],
['utc:Tue Oct 6 21:00:00 2026', '100'],
['100', 'utc:Tue Oct 6 21:00:00 2026'],
])('retains unknown chronology without kill authority (%s, %s)', (parentToken, childToken) => {
const rows = [
{ pid: 20, ppid: 1, name: 'fixture.exe', startToken: parentToken },
{ pid: 21, ppid: 20, name: 'python.exe', startToken: childToken },
{ pid: 22, ppid: 21, name: 'python-child.exe', startToken: '300' },
];
const discovered = snapshotDescendants(20, undefined, rows);
expect(discovered).toEqual([
{ pid: 21, name: 'python.exe', startToken: null },
{ pid: 22, name: 'python-child.exe', startToken: null },
]);
const killed: number[] = [];
reapSnapshottedDescendants(discovered, () => true, pid => { killed.push(pid); });
expect(killed).toEqual([]);
expect(survivingProcesses(discovered, () => true, () => null)).toEqual(discovered);
});
it('skips unverified roots in successful and failed-readiness cleanup', async () => {
const killed: number[] = [];
const action = (pid: number) => { killed.push(pid); };
expect(await runForMatchingProcess(20, null, action, () => true)).toBe(false);
expect(await runForMatchingProcess(20, 'fixture-original', action, () => false)).toBe(false);
expect(await runForMatchingProcess(20, 'fixture-original', action, (_, token) => token === 'fixture-original')).toBe(true);
expect(killed).toEqual([20]);
});
it('keeps reaping other descendants when one kill fails', () => {
const attempted: number[] = [];
expect(() => reapSnapshottedDescendants(
snapshot,
() => true,
pid => {
attempted.push(pid);
if (pid === 23) throw new Error('Access is denied');
}
)).toThrow('Access is denied');
expect(attempted).toEqual([23, 22, 21]);
});
it('never tree-kills a failed-readiness PID without its recorded identity', () => {
const rows = [
{ pid: 20, ppid: 1, name: 'unrelated.exe', startToken: '400' },
{ pid: 21, ppid: 20, name: 'python.exe', startToken: '500' },
];
const killed: number[] = [];
const kill = (pid: number) => { killed.push(pid); };
expect(reapOwnedLiveTree(20, null, kill, () => true, rows)).toBeNull();
expect(reapOwnedLiveTree(20, '100', kill, (_, token) => token === '400', rows)).toBeNull();
expect(killed).toEqual([]);
});
it('reaps identified children then an identified live root', () => {
const rows = [
{ pid: 20, ppid: 1, name: 'fixture.exe', startToken: '100' },
{ pid: 21, ppid: 20, name: 'chroma-mcp.exe', startToken: '200' },
{ pid: 22, ppid: 21, name: 'python.exe', startToken: '300' },
];
const killed: number[] = [];
expect(reapOwnedLiveTree(20, '100', pid => { killed.push(pid); }, () => true, rows)).toEqual([
{ pid: 21, name: 'chroma-mcp.exe', startToken: '200' },
{ pid: 22, name: 'python.exe', startToken: '300' },
]);
expect(killed).toEqual([22, 21, 20]);
let rootChecks = 0;
killed.length = 0;
expect(reapOwnedLiveTree(20, '100', pid => { killed.push(pid); }, pid =>
pid !== 20 || ++rootChecks === 1, rows)).toHaveLength(2);
expect(killed).toEqual([22, 21]); // Root PID was reissued during cleanup.
});
it('retains a live null-token child for final verification without killing it', () => {
const rows = [
{ pid: 20, ppid: 1, name: 'fixture.exe', startToken: '100' },
{ pid: 21, ppid: 20, name: 'python.exe', startToken: null },
{ pid: 22, ppid: 20, name: 'uv.exe', startToken: '200' },
];
const killed: number[] = [];
let retained: ProcessIdentity[] = [];
const discovered = reapOwnedLiveTree(
20, '100', pid => { killed.push(pid); }, () => true,
rows, children => { retained = children; }
);
expect(retained).toEqual([
{ pid: 20, name: 'fixture.exe', startToken: '100' },
...discovered!,
]);
expect(killed).toEqual([22, 20]);
expect(survivingProcesses(retained, pid => pid === 21, () => null)).toEqual([
{ pid: 21, name: 'python.exe', startToken: null },
]);
});
it('retains failed-start children before a kill error interrupts cleanup', () => {
const rows = [
{ pid: 20, ppid: 1, name: 'fixture.exe', startToken: '100' },
{ pid: 21, ppid: 20, name: 'python.exe', startToken: '200' },
];
let retained: ProcessIdentity[] = [];
expect(() => reapOwnedLiveTree(
20, '100', () => { throw new Error('Access is denied'); },
() => true, rows, children => { retained = children; }
)).toThrow('Access is denied');
expect(retained).toEqual([
{ pid: 20, name: 'fixture.exe', startToken: '100' },
{ pid: 21, name: 'python.exe', startToken: '200' },
]);
});
it('reports a live root when its final identity probe is unreadable', () => {
const rows = [{ pid: 20, ppid: 1, name: 'fixture.exe', startToken: '100' }];
let checks = 0;
let retained: ProcessIdentity[] = [];
const killed: number[] = [];
reapOwnedLiveTree(
20, '100', pid => { killed.push(pid); },
() => ++checks === 1,
rows, snapshot => { retained = snapshot; }
);
expect(killed).toEqual([]);
expect(survivingProcesses(retained, () => true, () => null)).toEqual([
{ pid: 20, name: 'fixture.exe', startToken: '100' },
]);
});
});