#1 Persistent memory for AI coding agents based on real-world benchmarks
TypeScript agentmemoryagentsaiclaudeclaudecodecodexcopilotcursorgenaiharnesshermesmemoryompopenclawopencodepi
fix(cli): count a relative iii-exec worker entry only when it exists in the engine's working directory (#1578)
Dmitrii Zhukov3188d88259db
diff --git a/scripts/release-gate/startup.mjs b/scripts/release-gate/startup.mjsindex 16872600..dffdeed4 100644--- a/scripts/release-gate/startup.mjs+++ b/scripts/release-gate/startup.mjs@@ -160,19 +160,20 @@ class Instance {env(extra = {}) { return { ...baseEnv(this.home), AGENTMEMORY_DATA_DIR: this.data, AGENTMEMORY_RUNTIME_DIR: this.runtime, III_REST_PORT: String(this.port), III_STREAM_PORT: String(this.port + 1), III_VIEWER_PORT: String(this.port + 2), III_ENGINE_PORT: String(this.port + 46023), AGENTMEMORY_METRICS_PORT: String(this.port + 6353), ...this.envExtra, ...extra }; }get url() { return `http://127.0.0.1:${this.port}`; }pid(name) { try { return Number(readFileSync(join(this.runtime, `${name}.pid`), "utf8").trim()); } catch { return null; } }- config({ delayMs = 0, earlyPid = false, worker = true } = {}) {+ config({ delayMs = 0, earlyPid = false, worker = true, relative = false } = {}) {let raw = readFileSync(join(pkg, "iii-config.yaml"), "utf8").split(" - name: iii-exec")[0];raw = raw.replaceAll("3111", String(this.port)).replaceAll("3112", String(this.port + 1)).replaceAll("3113", String(this.port + 2)).replaceAll("./data/", `${this.data}/`);raw += ` - name: iii-worker-manager\n config:\n port: ${this.port + 46023}\n host: 127.0.0.1\n`;if (worker) {- const wrapper = join(this.dir, "delayed-worker.mjs");+ const wrapper = join(this.dir, relative ? "dist/index.mjs" : "delayed-worker.mjs");+ mkdirSync(dirname(wrapper), { recursive: true });writeFileSync(wrapper, `import { appendFileSync, writeFileSync } from 'node:fs';\nappendFileSync(${JSON.stringify(join(this.dir, "worker-starts.jsonl"))}, JSON.stringify({pid:process.pid,at:Date.now()})+'\\n');\n${earlyPid ? `writeFileSync(${JSON.stringify(join(this.runtime, "worker.pid"))}, String(process.pid));` : ""}\nawait new Promise(r=>setTimeout(r,${delayMs}));\nawait import(${JSON.stringify(pathToFileURL(join(pkg, "dist/index.mjs")).href)});\n`);- raw += ` - name: iii-exec\n config:\n exec:\n - ${process.execPath} ${wrapper}\n`;+ raw += ` - name: iii-exec\n config:\n exec:\n - ${relative ? "node dist/index.mjs" : `${process.execPath} ${wrapper}`}\n`;}- const file = join(this.dir, "iii-config.yaml"); writeFileSync(file, raw); this.envExtra.AGENTMEMORY_III_CONFIG = file; if (worker) this.envExtra.AGENTMEMORY_ENGINE_MANAGES_WORKER = "true"; return file;+ const file = join(this.dir, "iii-config.yaml"); writeFileSync(file, raw); this.envExtra.AGENTMEMORY_III_CONFIG = file; if (worker && !relative) this.envExtra.AGENTMEMORY_ENGINE_MANAGES_WORKER = "true"; return file;}- start(args = [], env = {}) {- this.cli = launch(process.execPath, [cli, ...args, "--port", String(this.port)], this.env(env), this.dir, this.logFile);+ start(args = [], env = {}, cwd = this.dir) {+ this.cli = launch(process.execPath, [cli, ...args, "--port", String(this.port)], this.env(env), cwd, this.logFile);this.ownedPids.add(this.cli.pid); return this.cli;}async startEngine(config) {@@ -209,8 +210,8 @@ async function scenario(name, fn) {catch (err) { results.push({ name, status: "fail", ms: Date.now() - at, error: err.stack, details }); log(`FAIL ${name}: ${err.message}`); }finally { if (instance) await instance.close(); }}-async function delayedConfigured(instance, details, { delayMs, earlyPid = false, adopt = false, args = [] } = {}) {- const config = instance.config({ delayMs, earlyPid });+async function delayedConfigured(instance, details, { delayMs, earlyPid = false, relative = false, adopt = false, args = [] } = {}) {+ const config = instance.config({ delayMs, earlyPid, relative });if (adopt) await instance.startEngine(config);const at = Date.now(); instance.start(args); await instance.ready(delayMs + 25_000);details.readyMs = Date.now() - at; details.delayMs = delayMs; details.transport = "real pinned native engine and installed worker via custom delayed-import config";@@ -284,6 +285,15 @@ async function main() {d.readyMs = Date.now() - at; assert.equal(i.pid("worker"), i.cli.pid, "Unrelated exec prevented the CLI-owned worker from starting");d.transport = "real engine with unrelated iii-exec command plus installed CLI-owned worker";});+ await scenario("relative-exec", (i, d) => delayedConfigured(i, d, { delayMs: 8_000, relative: true }));+ await scenario("unresolved-relative-exec", async (i, d) => {+ const config = i.config({ worker: false });+ appendFileSync(config, " - name: iii-exec\n config:\n exec:\n - node dist/index.mjs\n");+ const cliCwd = join(i.dir, "cli-cwd"); mkdirSync(join(cliCwd, "dist"), { recursive: true }); writeFileSync(join(cliCwd, "dist/index.mjs"), "");+ const at = Date.now(); i.start([], { AGENTMEMORY_READY_TIMEOUT_MS: "5000" }, cliCwd); await i.ready(10_000);+ d.readyMs = Date.now() - at; assert.equal(i.pid("worker"), i.cli.pid, "A relative exec entry missing from the engine working directory prevented the CLI-owned worker from starting");+ d.transport = "real engine with a relative iii-exec worker entry that does not exist in its working directory; installed CLI-owned worker";+ });await scenario("native-engine-timeout", async (i, d) => {i.config({ worker: false });const binary = join(i.runtime, "bin/iii"); const launchPid = join(i.dir, "launched-engine.pid");diff --git a/src/cli.ts b/src/cli.tsindex dd0f843b..c5ba40d4 100644--- a/src/cli.ts+++ b/src/cli.ts@@ -656,6 +656,7 @@ function engineStatePath(): string {type NativeEngineState = {kind: "native";configPath: string;+ cwd?: string;attached?: boolean;binPath?: string;restPort?: number;@@ -830,7 +831,7 @@ function configuredEngineMayStartWorker(): boolean {if (state?.kind !== "native" || state.attached) return false;try {const raw = readFileSync(state.configPath, "utf-8");- return configStartsAgentmemoryWorker(raw, join(__dirname, "index.mjs"));+ return configStartsAgentmemoryWorker(raw, join(__dirname, "index.mjs"), state.cwd);} catch {return false;}@@ -1717,6 +1718,7 @@ function startIiiBin(iiiBin: string, configPath: string): boolean {writeEngineState({kind: "native",configPath: launch.configPath,+ cwd: launch.cwd,binPath: iiiBin,});spawnEngineBackground(diff --git a/src/cli/readiness.ts b/src/cli/readiness.tsindex e7ca0fb9..e957af9c 100644--- a/src/cli/readiness.ts+++ b/src/cli/readiness.ts@@ -1,3 +1,6 @@+import { statSync } from "node:fs";+import { join } from "node:path";+export const DEFAULT_ENGINE_TIMEOUT_MS = 120_000;export const DEFAULT_WORKER_TIMEOUT_MS = 300_000;export const MAX_STARTUP_TIMEOUT_MS = 3_600_000;@@ -27,7 +30,7 @@ export function startupTimeouts(env: NodeJS.ProcessEnv = process.env): {};}-export function configStartsAgentmemoryWorker(raw: string, workerEntry: string): boolean {+export function configStartsAgentmemoryWorker(raw: string, workerEntry: string, engineCwd?: string): boolean {const lines = raw.split(/\r?\n/);let workerIndent: number | undefined;let execIndent: number | undefined;@@ -65,7 +68,13 @@ export function configStartsAgentmemoryWorker(raw: string, workerEntry: string):const entry = tokens.length === 2? tokens[1]: tokens.length === 4 && tokens[1] === "--import" && tokens[2] === "tsx" ? tokens[3] : undefined;- if (entry === workerEntry.replaceAll("\\", "/") || entry === "dist/index.mjs" || entry === "./dist/index.mjs" || entry === "src/index.ts" || entry === "./src/index.ts") return true;+ if (entry === workerEntry.replaceAll("\\", "/")) return true;+ if (entry === "dist/index.mjs" || entry === "./dist/index.mjs" || entry === "src/index.ts" || entry === "./src/index.ts") {+ if (engineCwd === undefined) return true;+ try {+ if (statSync(join(engineCwd, entry)).isFile()) return true;+ } catch {}+ }}return false;}diff --git a/test/cli-readiness.test.ts b/test/cli-readiness.test.tsindex 13f7c578..75218ac4 100644--- a/test/cli-readiness.test.ts+++ b/test/cli-readiness.test.ts@@ -1,4 +1,7 @@-import { afterEach, describe, expect, it, vi } from "vitest";+import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { dirname, join } from "node:path";+import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";import {configStartsAgentmemoryWorker,probeEngine,@@ -72,15 +75,83 @@ describe("startup configuration", () => {describe("configured worker ownership", () => {const config = (command: string) => `workers:\n - name: iii-exec\n config:\n exec:\n - ${command}\n - name: iii-http\n config:\n port: 3111\n`;+ let engineCwd: string;+ let emptyCwd: string;+ let distOnlyCwd: string;+ let dirEntryCwd: string;+ let linkCwd: string;+ beforeAll(() => {+ engineCwd = mkdtempSync(join(tmpdir(), "am-engine-cwd-"));+ emptyCwd = mkdtempSync(join(tmpdir(), "am-engine-cwd-empty-"));+ mkdirSync(join(emptyCwd, "dist"));+ mkdirSync(join(emptyCwd, "src"));+ distOnlyCwd = mkdtempSync(join(tmpdir(), "am-engine-cwd-dist-"));+ mkdirSync(join(distOnlyCwd, "dist"));+ writeFileSync(join(distOnlyCwd, "dist/index.mjs"), "");+ dirEntryCwd = mkdtempSync(join(tmpdir(), "am-engine-cwd-dir-"));+ mkdirSync(join(dirEntryCwd, "dist/index.mjs"), { recursive: true });+ mkdirSync(join(dirEntryCwd, "src/index.ts"), { recursive: true });+ linkCwd = mkdtempSync(join(tmpdir(), "am-engine-cwd-link-"));+ mkdirSync(join(linkCwd, "dist"));+ mkdirSync(join(linkCwd, "src"));+ writeFileSync(join(linkCwd, "target.mjs"), "");+ symlinkSync(join(linkCwd, "target.mjs"), join(linkCwd, "dist/index.mjs"));+ symlinkSync(join(linkCwd, "src"), join(linkCwd, "src/index.ts"));+ for (const entry of ["dist/index.mjs", "src/index.ts", "other.mjs", "delayed-wrapper.mjs"]) {+ mkdirSync(dirname(join(engineCwd, entry)), { recursive: true });+ writeFileSync(join(engineCwd, entry), "");+ }+ });+ afterAll(() => {+ rmSync(engineCwd, { recursive: true, force: true });+ rmSync(emptyCwd, { recursive: true, force: true });+ rmSync(distOnlyCwd, { recursive: true, force: true });+ rmSync(dirEntryCwd, { recursive: true, force: true });+ rmSync(linkCwd, { recursive: true, force: true });+ });it.each(["node dist/index.mjs", "node ./dist/index.mjs", "node --import tsx src/index.ts", "tsx ./src/index.ts", '"node dist/index.mjs"', "node dist/index.mjs # worker", '"node dist/index.mjs" # worker', "node --max-old-space-size=8192 dist/index.mjs", '\'/opt/node /package/dist/index.mjs\''])("recognizes a direct worker command: %s", (command) => {- expect(configStartsAgentmemoryWorker(config(command), "/package/dist/index.mjs")).toBe(true);+ expect(configStartsAgentmemoryWorker(config(command), "/package/dist/index.mjs", engineCwd)).toBe(true);});it.each(["node other.mjs", "node delayed-wrapper.mjs", "echo node dist/index.mjs", "sh -c 'node dist/index.mjs'", "node dist/index.mjs && something", "node dist/index.mjs.fake"])("does not claim unrelated or wrapped command: %s", (command) => {+ expect(configStartsAgentmemoryWorker(config(command), "/package/dist/index.mjs", engineCwd)).toBe(false);+ });+ it.each(["node dist/index.mjs", "node ./dist/index.mjs", "node --import tsx src/index.ts", "tsx ./src/index.ts"])("does not claim a relative worker entry missing from the engine working directory: %s", (command) => {+ expect(configStartsAgentmemoryWorker(config(command), "/package/dist/index.mjs", emptyCwd)).toBe(false);+ });+ it.each(["node dist/index.mjs", "node ./dist/index.mjs", "node --import tsx src/index.ts", "tsx ./src/index.ts"])("does not claim a relative worker entry that is a directory in the engine working directory: %s", (command) => {+ expect(configStartsAgentmemoryWorker(config(command), "/package/dist/index.mjs", dirEntryCwd)).toBe(false);+ });+ it("follows a symlinked relative worker entry to what it points at", () => {+ expect(configStartsAgentmemoryWorker(config("node dist/index.mjs"), "/package/dist/index.mjs", linkCwd)).toBe(true);+ expect(configStartsAgentmemoryWorker(config("tsx ./src/index.ts"), "/package/dist/index.mjs", linkCwd)).toBe(false);+ });+ it("treats an unreadable relative worker entry as missing and keeps reading the exec list", () => {+ const raw = `workers:\n - name: iii-exec\n config:\n exec:\n - node dist/index.mjs\n - /opt/node /package/dist/index.mjs\n`;+ const loopCwd = mkdtempSync(join(tmpdir(), "am-engine-cwd-loop-"));+ try {+ mkdirSync(join(loopCwd, "dist"));+ symlinkSync(join(loopCwd, "dist/index.mjs"), join(loopCwd, "dist/index.mjs"));+ expect(configStartsAgentmemoryWorker(raw, "/package/dist/index.mjs", loopCwd)).toBe(true);+ expect(configStartsAgentmemoryWorker(config("node dist/index.mjs"), "/package/dist/index.mjs", loopCwd)).toBe(false);+ } finally {+ rmSync(loopCwd, { recursive: true, force: true });+ }+ });+ it.each(["node --import tsx src/index.ts", "tsx ./src/index.ts"])("does not claim the source entry when only the built entry exists in the engine working directory: %s", (command) => {+ expect(configStartsAgentmemoryWorker(config(command), "/package/dist/index.mjs", distOnlyCwd)).toBe(false);+ });+ it("recognizes the absolute worker entry without an engine working directory", () => {+ expect(configStartsAgentmemoryWorker(config("/opt/node /package/dist/index.mjs"), "/package/dist/index.mjs")).toBe(true);+ });+ it.each(["node dist/index.mjs", "node ./dist/index.mjs", "node --import tsx src/index.ts", "tsx ./src/index.ts"])("keeps recognizing a relative worker entry when the engine working directory is unknown: %s", (command) => {+ expect(configStartsAgentmemoryWorker(config(command), "/package/dist/index.mjs")).toBe(true);+ });+ it.each(["node other.mjs", "sh -c 'node dist/index.mjs'"])("does not claim an unrelated entry when the engine working directory is unknown: %s", (command) => {expect(configStartsAgentmemoryWorker(config(command), "/package/dist/index.mjs")).toBe(false);});it("ignores comments, other workers and sibling configuration lists", () => {const raw = `# - name: iii-exec\n# exec:\n# - node dist/index.mjs\n - name: iii-state\n config:\n exec:\n - node dist/index.mjs\n - name: iii-exec\n config:\n exec:\n - node other.mjs\n watch:\n - node dist/index.mjs\n`;- expect(configStartsAgentmemoryWorker(raw, "/package/dist/index.mjs")).toBe(false);+ expect(configStartsAgentmemoryWorker(raw, "/package/dist/index.mjs", engineCwd)).toBe(false);});});