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, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
import { mkdirSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { SettingsDefaultsManager } from '../../src/shared/SettingsDefaultsManager.js';
import { InvalidWorkerPortError } from '../../src/shared/install-port.js';
import { clearPortCache, getWorkerBaseUrl, getWorkerPort } from '../../src/shared/worker-utils.js';
import { logger } from '../../src/utils/logger.js';
// Evaluate paths.ts before any per-test env override, exactly as
// worker-utils-timeout.test.ts does: paths.ts freezes DATA_DIR at first
// evaluation and must not see the per-test temp dir.
import '../../src/shared/paths.js';
describe('worker-utils invalid worker port resolution', () => {
let tempDir: string;
const originalDataDir = process.env.CLAUDE_MEM_DATA_DIR;
const originalPort = process.env.CLAUDE_MEM_WORKER_PORT;
const originalUrl = process.env.CLAUDE_MEM_WORKER_URL;
beforeEach(() => {
tempDir = join(tmpdir(), `worker-port-${Date.now()}-${Math.random().toString(36).slice(2)}`);
mkdirSync(tempDir, { recursive: true });
process.env.CLAUDE_MEM_DATA_DIR = tempDir;
delete process.env.CLAUDE_MEM_WORKER_PORT;
delete process.env.CLAUDE_MEM_WORKER_URL;
mock.restore();
});
afterEach(() => {
mock.restore();
clearPortCache();
if (originalDataDir === undefined) delete process.env.CLAUDE_MEM_DATA_DIR;
else process.env.CLAUDE_MEM_DATA_DIR = originalDataDir;
if (originalPort === undefined) delete process.env.CLAUDE_MEM_WORKER_PORT;
else process.env.CLAUDE_MEM_WORKER_PORT = originalPort;
if (originalUrl === undefined) delete process.env.CLAUDE_MEM_WORKER_URL;
else process.env.CLAUDE_MEM_WORKER_URL = originalUrl;
rmSync(tempDir, { recursive: true, force: true });
});
function writeSettings(workerPort: unknown): void {
const settings = SettingsDefaultsManager.getAllDefaults();
settings.CLAUDE_MEM_DATA_DIR = tempDir;
(settings as unknown as Record<string, unknown>).CLAUDE_MEM_WORKER_PORT = workerPort;
writeFileSync(join(tempDir, 'settings.json'), JSON.stringify(settings, null, 2), 'utf-8');
}
for (const invalid of ['abc', '37700abc', '', 0, 70000, true] as const) {
for (const readOnly of [false, true]) {
it(`refuses ${JSON.stringify(invalid)} instead of using another port (readOnly ${readOnly})`, () => {
writeSettings(invalid);
const warnSpy = spyOn(logger, 'warn');
clearPortCache();
expect(() => getWorkerPort({ readOnly })).toThrow(InvalidWorkerPortError);
expect(() => getWorkerPort({ readOnly })).toThrow(
`CLAUDE_MEM_WORKER_PORT must be a whole number from 1 to 65535 (got "${String(invalid)}")`,
);
// Nothing was cached: a later call refuses again rather than reusing a port.
expect(() => getWorkerPort({ readOnly })).toThrow(InvalidWorkerPortError);
// A read-only caller must not write the data folder, and the logger writes there.
expect(warnSpy).not.toHaveBeenCalled();
});
}
}
it('refuses an invalid environment override the same way', () => {
writeSettings('39000');
process.env.CLAUDE_MEM_WORKER_PORT = 'not-a-port';
clearPortCache();
expect(() => getWorkerPort()).toThrow(InvalidWorkerPortError);
expect(() => getWorkerBaseUrl({ readOnly: true })).toThrow(InvalidWorkerPortError);
});
it('resolves a valid string port and the JSON number form', () => {
writeSettings('39000');
clearPortCache();
expect(getWorkerPort()).toBe(39000);
expect(getWorkerPort({ readOnly: true })).toBe(39000);
writeSettings(39001);
clearPortCache();
expect(getWorkerPort()).toBe(39001);
expect(getWorkerPort({ readOnly: true })).toBe(39001);
// A port below 1024 bound and connected before validation existed.
writeSettings('80');
clearPortCache();
expect(getWorkerPort()).toBe(80);
});
it('does not consult the port when CLAUDE_MEM_WORKER_URL names the worker', () => {
writeSettings('abc');
process.env.CLAUDE_MEM_WORKER_URL = 'http://127.0.0.1:41000';
clearPortCache();
expect(getWorkerBaseUrl({ readOnly: true })).toBe('http://127.0.0.1:41000');
});
});