Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
test(sdk): retain the server-assigned integration port
Samir Hanna Verza6ffbed76eab3
diff --git a/companions/ai-memory-ts/test/integration/server.test.ts b/companions/ai-memory-ts/test/integration/server.test.tsnew file mode 100644index 00000000..7dd76c32--- /dev/null+++ b/companions/ai-memory-ts/test/integration/server.test.ts@@ -0,0 +1,83 @@+import assert from "node:assert/strict";+import childProcess, { type SpawnOptions } from "node:child_process";+import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";+import { syncBuiltinESMExports } from "node:module";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { it } from "node:test";++import { startServer, type ServerHandle } from "../support/server.js";++it("starts when another listener takes a port before the server binds", {+ // Windows kill('SIGTERM') terminates the wrapper without forwarding to its child.+ skip: process.platform === "win32" && "the competing-listener wrapper forwards POSIX termination signals",+}, async (t) => {+ const root = await mkdtemp(join(tmpdir(), "ai-memory-ts-bind-race-"));+ const wrapper = join(root, "competing-listener.mjs");+ const occupied = join(root, "occupied.json");+ // Interpose after the harness chooses its bind address, but before the real+ // binary starts. A preselected nonzero port is now deterministically busy;+ // binding port zero lets the real server choose its own available listener.+ await writeFile(wrapper, `+import { spawn } from "node:child_process";+import { writeFileSync } from "node:fs";+import { createServer } from "node:net";+const [binary, ...args] = process.argv.slice(2);+const address = args[args.indexOf("--bind") + 1];+const requested = Number(address.split(":").at(-1));+const competitor = createServer((socket) => socket.destroy());+competitor.listen(requested, "127.0.0.1", () => {+ writeFileSync(${JSON.stringify(occupied)}, JSON.stringify({ port: competitor.address().port }));+ const child = spawn(binary, args, { stdio: "inherit" });+ process.on("SIGTERM", () => {+ child.kill("SIGTERM");+ setTimeout(() => child.kill("SIGKILL"), 5_000).unref();+ });+ child.once("error", (error) => {+ console.error(error);+ competitor.close(() => process.exit(1));+ });+ child.once("exit", (code) => competitor.close(() => process.exit(code ?? 1)));+});+`);+ const spawn = childProcess.spawn;+ t.mock.method(childProcess, "spawn", (binary: string, args: readonly string[], options: SpawnOptions) =>+ spawn(process.execPath, [wrapper, binary, ...args], options),+ );+ syncBuiltinESMExports();+ let server: ServerHandle | undefined;+ try {+ server = await startServer({ basePath: "/port-probe" });+ const { port } = JSON.parse(await readFile(occupied, "utf8")) as { port: number };+ assert.notEqual(Number(new URL(server.url).port), port);+ assert.equal(new URL(server.url).pathname, "/port-probe");+ const response = await fetch(`${server.url}/identity`, {+ headers: { Authorization: `Bearer ${server.rootToken}` },+ });+ assert.equal(response.status, 200);+ assert.equal(((await response.json()) as { level: string }).level, "root");+ } finally {+ await server?.stop();+ t.mock.restoreAll();+ syncBuiltinESMExports();+ await rm(root, { recursive: true, force: true });+ }+});++it("reports an early startup failure without restarting the server", async (t) => {+ const spawn = childProcess.spawn;+ const mocked = t.mock.method(+ childProcess,+ "spawn",+ (_binary: string, _args: readonly string[], options: SpawnOptions) =>+ spawn(process.execPath, ["-e", "console.error('synthetic startup failure'); process.exit(7)"], options),+ );+ syncBuiltinESMExports();+ try {+ await assert.rejects(startServer(), /ai-memory exited before it was ready:\nsynthetic startup failure/);+ assert.equal(mocked.mock.callCount(), 1);+ } finally {+ t.mock.restoreAll();+ syncBuiltinESMExports();+ }+});diff --git a/companions/ai-memory-ts/test/support/server.ts b/companions/ai-memory-ts/test/support/server.tsindex 609a0dd1..51f960a9 100644--- a/companions/ai-memory-ts/test/support/server.ts+++ b/companions/ai-memory-ts/test/support/server.ts@@ -10,7 +10,6 @@ import { spawn, type ChildProcess } from "node:child_process";import { randomBytes } from "node:crypto";import { closeSync, openSync } from "node:fs";import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";-import { createServer } from "node:net";import { tmpdir } from "node:os";import { join, resolve } from "node:path";@@ -54,16 +53,14 @@ export function serverBinary(): string {return resolve(bin);}-async function freePort(): Promise<number> {- return new Promise((resolve, reject) => {- const server = createServer();- server.once("error", reject);- server.listen(0, "127.0.0.1", () => {- const address = server.address();- const port = typeof address === "object" && address !== null ? address.port : 0;- server.close(() => resolve(port));- });- });+/** The listener address from a complete ready line, not the configured bind. */+export function serverAddress(log: string): string | undefined {+ for (const line of log.split("\n").slice(0, -1)) {+ if (!line.includes("MCP HTTP server ready")) continue;+ const match = /\blocal_addr=(127\.0\.0\.1:(\d+))\b/.exec(line);+ if (match !== null && Number(match[2]) > 0 && Number(match[2]) <= 65535) return match[1];+ }+ return undefined;}function sleep(ms: number): Promise<void> {@@ -98,10 +95,9 @@ export async function startServer(options: ServerOptions = {}): Promise<ServerHaawait Promise.all([home, data, tmp, project].map((dir) => mkdir(dir, { recursive: true })));const gitconfig = join(root, "gitconfig");await writeFile(gitconfig, "");- const port = await freePort();const rootToken = `FAKEsdkroot${randomBytes(12).toString("hex")}`;const basePath = options.basePath ?? "";- const url = `http://127.0.0.1:${port}${basePath}`;+ let url = "";const env: Record<string, string> = {HOME: home,@@ -122,7 +118,8 @@ export async function startServer(options: ServerOptions = {}): Promise<ServerHaAI_MEMORY_AUTO_IMPROVE__SCHEDULER__ENABLED: "false",AI_MEMORY_AUTH_TOKEN: rootToken,AI_MEMORY_AUTH__TOKEN_PEPPER: `FAKEsdkpepper${randomBytes(16).toString("hex")}`,- RUST_LOG: "off",+ RUST_LOG: "off,ai_memory_cli::commands::serve=info",+ NO_COLOR: "1",};const args = ["--data-dir",@@ -131,7 +128,9 @@ export async function startServer(options: ServerOptions = {}): Promise<ServerHa"--transport","http","--bind",- `127.0.0.1:${port}`,+ // Let this listener keep its kernel-assigned port: reserving and then+ // releasing a port before spawn races other integration-test processes.+ "127.0.0.1:0","--no-watcher",];if (options.api !== false) args.push("--enable-api");@@ -167,13 +166,19 @@ export async function startServer(options: ServerOptions = {}): Promise<ServerHaawait rm(root, { recursive: true, force: true });throw new Error(`ai-memory exited before it was ready:\n${log.slice(-2000)}`);}+ if (url === "") {+ const address = serverAddress(await readFile(logPath, "utf8"));+ if (address !== undefined) url = `http://${address}${basePath}`;+ }try {- const response = await fetch(`${url}/identity`, {- headers: { Authorization: `Bearer ${rootToken}` },- signal: AbortSignal.timeout(1_000),- });- await response.arrayBuffer();- if (response.ok) break;+ if (url !== "") {+ const response = await fetch(`${url}/identity`, {+ headers: { Authorization: `Bearer ${rootToken}` },+ signal: AbortSignal.timeout(1_000),+ });+ await response.arrayBuffer();+ if (response.ok) break;+ }} catch {// Not listening yet.}diff --git a/companions/ai-memory-ts/test/unit/server.test.ts b/companions/ai-memory-ts/test/unit/server.test.tsnew file mode 100644index 00000000..8c1a7c69--- /dev/null+++ b/companions/ai-memory-ts/test/unit/server.test.ts@@ -0,0 +1,17 @@+import assert from "node:assert/strict";+import { it } from "node:test";++import { serverAddress } from "../support/server.js";++it("discovers only a complete ready line with a bound loopback port", () => {+ const startup = "INFO starting server bind=127.0.0.1:49374\n";+ const ready = "INFO MCP HTTP server ready (POST /mcp) local_addr=127.0.0.1:54321 auth=true";+ assert.equal(serverAddress(startup), undefined);+ assert.equal(serverAddress(`${startup}${ready}`), undefined, "a log write may be incomplete");+ assert.equal(serverAddress(`${startup}${ready}\n`), "127.0.0.1:54321");+ assert.equal(serverAddress(`${ready}\r\n`), "127.0.0.1:54321");+ assert.equal(serverAddress("INFO other log local_addr=127.0.0.1:54321\n"), undefined);+ for (const address of ["127.0.0.1:0", "127.0.0.1:65536", "0.0.0.0:54321"]) {+ assert.equal(serverAddress(`INFO MCP HTTP server ready local_addr=${address}\n`), undefined);+ }+});