Skip to content

#1 Persistent memory for AI coding agents based on real-world benchmarks

fix(cli): faster state saves, bounded glibc arenas and a higher fd limit for the engine (#1457)

Rohit Ghumareb3d6cf500266
diff --git a/.env.example b/.env.example
index 4e4e3445..0c38394c 100644
--- a/.env.example
+++ b/.env.example
@@ -159,6 +159,7 @@
# ~/Library/Application Support/agentmemory; Linux $XDG_DATA_HOME/agentmemory
# or ~/.local/share/agentmemory; Windows %APPDATA%\agentmemory.
# AGENTMEMORY_DATA_DIR=/absolute/path
+# AGENTMEMORY_STATE_SAVE_INTERVAL_MS=2000 # How often the engine writes state to disk; a crash loses at most this much
# AGENTMEMORY_USE_DOCKER=1 # Prefer bundled Docker even when a compatible native iii binary exists
# AGENTMEMORY_EXPORT_ROOT=~/agentmemory-backup # Default destination for `agentmemory export`
diff --git a/deploy/coolify/entrypoint.sh b/deploy/coolify/entrypoint.sh
index 808f7fe1..8df88f49 100755
--- a/deploy/coolify/entrypoint.sh
+++ b/deploy/coolify/entrypoint.sh
@@ -21,6 +21,14 @@ HMAC_FILE="${AGENTMEMORY_HMAC_FILE:-/data/.hmac}"
RUN_AS="node:node"
III_CONFIG="/opt/agentmemory/node_modules/@agentmemory/agentmemory/dist/iii-config.yaml"
+if [ -z "${MALLOC_ARENA_MAX:-}" ]; then
+ export MALLOC_ARENA_MAX=2
+fi
+cur_nofile="$(ulimit -n)"
+if [ "$cur_nofile" != unlimited ] && [ "$cur_nofile" -lt 10240 ]; then
+ ulimit -n 10240 2>/dev/null || ulimit -n "$(ulimit -H -n)" 2>/dev/null || true
+fi
+
mkdir -p "$DATA_DIR"
chown -R "$RUN_AS" "$DATA_DIR"
@@ -44,6 +52,7 @@ workers:
name: kv
config:
store_method: file_based
+ save_interval_ms: 2000
file_path: /data/state_store.db
- name: iii-queue
config:
@@ -65,6 +74,7 @@ workers:
name: kv
config:
store_method: file_based
+ save_interval_ms: 2000
file_path: /data/stream_store
- name: iii-observability
config:
diff --git a/deploy/fly/entrypoint.sh b/deploy/fly/entrypoint.sh
index befe7106..f81f4bcb 100755
--- a/deploy/fly/entrypoint.sh
+++ b/deploy/fly/entrypoint.sh
@@ -21,6 +21,14 @@ HMAC_FILE="${AGENTMEMORY_HMAC_FILE:-/data/.hmac}"
RUN_AS="node:node"
III_CONFIG="/opt/agentmemory/node_modules/@agentmemory/agentmemory/dist/iii-config.yaml"
+if [ -z "${MALLOC_ARENA_MAX:-}" ]; then
+ export MALLOC_ARENA_MAX=2
+fi
+cur_nofile="$(ulimit -n)"
+if [ "$cur_nofile" != unlimited ] && [ "$cur_nofile" -lt 10240 ]; then
+ ulimit -n 10240 2>/dev/null || ulimit -n "$(ulimit -H -n)" 2>/dev/null || true
+fi
+
mkdir -p "$DATA_DIR"
chown -R "$RUN_AS" "$DATA_DIR"
@@ -44,6 +52,7 @@ workers:
name: kv
config:
store_method: file_based
+ save_interval_ms: 2000
file_path: /data/state_store.db
- name: iii-queue
config:
@@ -65,6 +74,7 @@ workers:
name: kv
config:
store_method: file_based
+ save_interval_ms: 2000
file_path: /data/stream_store
- name: iii-observability
config:
diff --git a/deploy/railway/entrypoint.sh b/deploy/railway/entrypoint.sh
index 808f7fe1..8df88f49 100755
--- a/deploy/railway/entrypoint.sh
+++ b/deploy/railway/entrypoint.sh
@@ -21,6 +21,14 @@ HMAC_FILE="${AGENTMEMORY_HMAC_FILE:-/data/.hmac}"
RUN_AS="node:node"
III_CONFIG="/opt/agentmemory/node_modules/@agentmemory/agentmemory/dist/iii-config.yaml"
+if [ -z "${MALLOC_ARENA_MAX:-}" ]; then
+ export MALLOC_ARENA_MAX=2
+fi
+cur_nofile="$(ulimit -n)"
+if [ "$cur_nofile" != unlimited ] && [ "$cur_nofile" -lt 10240 ]; then
+ ulimit -n 10240 2>/dev/null || ulimit -n "$(ulimit -H -n)" 2>/dev/null || true
+fi
+
mkdir -p "$DATA_DIR"
chown -R "$RUN_AS" "$DATA_DIR"
@@ -44,6 +52,7 @@ workers:
name: kv
config:
store_method: file_based
+ save_interval_ms: 2000
file_path: /data/state_store.db
- name: iii-queue
config:
@@ -65,6 +74,7 @@ workers:
name: kv
config:
store_method: file_based
+ save_interval_ms: 2000
file_path: /data/stream_store
- name: iii-observability
config:
diff --git a/deploy/render/entrypoint.sh b/deploy/render/entrypoint.sh
index 808f7fe1..8df88f49 100755
--- a/deploy/render/entrypoint.sh
+++ b/deploy/render/entrypoint.sh
@@ -21,6 +21,14 @@ HMAC_FILE="${AGENTMEMORY_HMAC_FILE:-/data/.hmac}"
RUN_AS="node:node"
III_CONFIG="/opt/agentmemory/node_modules/@agentmemory/agentmemory/dist/iii-config.yaml"
+if [ -z "${MALLOC_ARENA_MAX:-}" ]; then
+ export MALLOC_ARENA_MAX=2
+fi
+cur_nofile="$(ulimit -n)"
+if [ "$cur_nofile" != unlimited ] && [ "$cur_nofile" -lt 10240 ]; then
+ ulimit -n 10240 2>/dev/null || ulimit -n "$(ulimit -H -n)" 2>/dev/null || true
+fi
+
mkdir -p "$DATA_DIR"
chown -R "$RUN_AS" "$DATA_DIR"
@@ -44,6 +52,7 @@ workers:
name: kv
config:
store_method: file_based
+ save_interval_ms: 2000
file_path: /data/state_store.db
- name: iii-queue
config:
@@ -65,6 +74,7 @@ workers:
name: kv
config:
store_method: file_based
+ save_interval_ms: 2000
file_path: /data/stream_store
- name: iii-observability
config:
diff --git a/docker-compose.yml b/docker-compose.yml
index 0c06226a..f0be2cc5 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -39,6 +39,12 @@ services:
III_VIEWER_PORT: "${III_VIEWER_PORT:-3113}"
III_ENGINE_PORT: "${III_ENGINE_PORT:-49134}"
AGENTMEMORY_REDIS_URL: "${AGENTMEMORY_REDIS_URL:-}"
+ AGENTMEMORY_STATE_SAVE_INTERVAL_MS: "${AGENTMEMORY_STATE_SAVE_INTERVAL_MS:-2000}"
+ MALLOC_ARENA_MAX: "${MALLOC_ARENA_MAX:-2}"
+ ulimits:
+ nofile:
+ soft: 10240
+ hard: 10240
ports:
- "127.0.0.1:${III_ENGINE_PORT:-49134}:${III_ENGINE_PORT:-49134}"
- "127.0.0.1:${III_REST_PORT:-3111}:${III_REST_PORT:-3111}"
diff --git a/iii-config.docker.yaml b/iii-config.docker.yaml
index 302b9145..10863607 100644
--- a/iii-config.docker.yaml
+++ b/iii-config.docker.yaml
@@ -17,6 +17,7 @@ workers:
name: kv
config:
store_method: file_based
+ save_interval_ms: ${AGENTMEMORY_STATE_SAVE_INTERVAL_MS:2000}
file_path: /data/state_store.db
- name: iii-queue
config:
@@ -38,6 +39,7 @@ workers:
name: kv
config:
store_method: file_based
+ save_interval_ms: ${AGENTMEMORY_STATE_SAVE_INTERVAL_MS:2000}
file_path: /data/stream_store
- name: iii-observability
config:
diff --git a/iii-config.yaml b/iii-config.yaml
index 62892f51..91cd1d70 100644
--- a/iii-config.yaml
+++ b/iii-config.yaml
@@ -13,6 +13,7 @@ workers:
name: kv
config:
store_method: file_based
+ save_interval_ms: 2000
file_path: ./data/state_store.db
- name: iii-queue
config:
@@ -34,6 +35,7 @@ workers:
name: kv
config:
store_method: file_based
+ save_interval_ms: 2000
file_path: ./data/stream_store
- name: iii-observability
config:
diff --git a/plugin/skills/agentmemory-config/REFERENCE.md b/plugin/skills/agentmemory-config/REFERENCE.md
index 5cc5cbfd..b26a9655 100644
--- a/plugin/skills/agentmemory-config/REFERENCE.md
+++ b/plugin/skills/agentmemory-config/REFERENCE.md
@@ -3,7 +3,7 @@
Generated by scanning `src/` for `AGENTMEMORY_*` usage. Do not edit the block below by hand; run `npm run skills:gen` after adding or removing a variable. Internal markers ending in two underscores are excluded.
<!-- AUTOGEN:env START - generated by scripts/skills/generate.ts, do not edit by hand -->
-Configuration is read from the environment and from `~/.agentmemory/.env` (no `export` prefix). 59 recognized variables:
+Configuration is read from the environment and from `~/.agentmemory/.env` (no `export` prefix). 60 recognized variables:
- `AGENTMEMORY_AGENT_SCOPE`
- `AGENTMEMORY_ALLOW_AGENT_SDK`
@@ -53,6 +53,7 @@ Configuration is read from the environment and from `~/.agentmemory/.env` (no `e
- `AGENTMEMORY_SESSION_SWEEP_STALE_HOURS`
- `AGENTMEMORY_SLOTS`
- `AGENTMEMORY_STATE_BACKEND`
+- `AGENTMEMORY_STATE_SAVE_INTERVAL_MS`
- `AGENTMEMORY_SUPPRESS_COST_WARNING`
- `AGENTMEMORY_TOOLS`
- `AGENTMEMORY_URL`
diff --git a/src/cli.ts b/src/cli.ts
index 7d4b72e9..9ff18051 100644
--- a/src/cli.ts
+++ b/src/cli.ts
@@ -74,7 +74,7 @@ import { createStartupStderrCapture } from "./cli/startup-stderr.js";
import {
clearPersistedBuiltinConfig,
engineFlushWaitMs,
- persistedBuiltinConfigDirs,
+ engineStateConfigPaths,
renderEngineConfig,
} from "./cli/engine-config.js";
import { SHUTDOWN_HARD_EXIT_MS } from "./shutdown.js";
@@ -84,7 +84,7 @@ import { renderSplash } from "./cli/splash.js";
import { isFirstRun, readPrefs, resetPrefs, writePrefs } from "./cli/preferences.js";
import { runOnboarding } from "./cli/onboarding.js";
import { setBootVerbose } from "./logger.js";
-import { getRedisUrl, getStateBackend, hydrateProcessEnvFromFile } from "./config.js";
+import { getRedisUrl, getStateBackend, getStateSaveIntervalMs, hydrateProcessEnvFromFile } from "./config.js";
import { III_PINNED_VERSION, VERSION } from "./version.js";
import { getAllTools, ESSENTIAL_TOOLS } from "./mcp/tools-registry.js";
import { knownAgents } from "./cli/connect/index.js";
@@ -1482,6 +1482,7 @@ function prepareEngineLaunch(configPath: string): {
enginePort: getEnginePort(),
},
stateBackend: { kind: stateBackendKind, redisUrl: getRedisUrl() },
+ saveIntervalMs: getStateSaveIntervalMs(),
};
const rewritten = bundledConfig
? rewriteBundledConfig(
@@ -3285,10 +3286,7 @@ async function waitForEngineFlush(): Promise<void> {
} catch {}
const runtimePath = runtimeConfigPath(dataDirResolution.dataDir);
const configTexts: string[] = [];
- for (const path of [
- ...persistedBuiltinConfigDirs(join(homedir(), ".agentmemory"), runtimePath).map((dir) => join(dir, "iii-state.yaml")),
- runtimePath,
- ]) {
+ for (const path of engineStateConfigPaths(join(homedir(), ".agentmemory"), runtimePath)) {
try {
configTexts.push(readFileSync(path, "utf-8"));
} catch {}
diff --git a/src/cli/engine-config.ts b/src/cli/engine-config.ts
index e873494c..6f0a4136 100644
--- a/src/cli/engine-config.ts
+++ b/src/cli/engine-config.ts
@@ -34,18 +34,33 @@ export function configuredSaveIntervalMs(renderedConfig: string): number | null
}
export const ENGINE_DEFAULT_SAVE_INTERVAL_MS = 5000;
+export const AGENTMEMORY_STATE_SAVE_INTERVAL_MS = 2000;
export const ENGINE_FLUSH_MARGIN_MS = 1500;
-export const ENGINE_FLUSH_WAIT_CAP_MS = 15_000;
export function engineFlushWaitMs(
stateBackend: "file" | "redis",
configTexts: readonly string[],
): number {
if (stateBackend === "redis") return 0;
- const interval =
- configTexts.map(configuredSaveIntervalMs).find((ms) => ms !== null) ??
- ENGINE_DEFAULT_SAVE_INTERVAL_MS;
- return Math.min(interval + ENGINE_FLUSH_MARGIN_MS, ENGINE_FLUSH_WAIT_CAP_MS);
+ return engineSaveIntervalMs(configTexts) + ENGINE_FLUSH_MARGIN_MS;
+}
+
+export function engineSaveIntervalMs(configTexts: readonly string[]): number {
+ const intervals: number[] = [];
+ for (const text of configTexts) {
+ const explicit = [...text.matchAll(/save_interval_ms:\s*(\d+)/g)].map((m) => parseInt(m[1]!, 10));
+ intervals.push(...explicit);
+ const fileStores = text.match(/store_method:\s*file_based/g)?.length ?? 0;
+ if (fileStores > explicit.length) intervals.push(ENGINE_DEFAULT_SAVE_INTERVAL_MS);
+ }
+ return intervals.length > 0 ? Math.max(...intervals) : ENGINE_DEFAULT_SAVE_INTERVAL_MS;
+}
+
+export function engineStateConfigPaths(engineCwd: string, runtimePath: string): string[] {
+ return [
+ ...persistedBuiltinConfigDirs(engineCwd, runtimePath).map((dir) => join(dir, "iii-state.yaml")),
+ runtimePath,
+ ];
}
export function persistedBuiltinConfigDirs(
@@ -104,6 +119,7 @@ export interface EngineConfigOptions {
dataDir: string;
ports?: EngineRuntimePorts;
stateBackend?: StateBackendOptions;
+ saveIntervalMs?: number;
}
export interface EngineRuntimePorts {
@@ -180,6 +196,40 @@ function setWorkerPort(lines: string[], name: string, port: number): void {
}
}
+function setFileStoreSaveInterval(
+ lines: string[],
+ workerName: string,
+ override: number | undefined,
+): void {
+ const block = workerBlock(lines, workerName);
+ if (!block) return;
+ const methodIndex = lines.findIndex(
+ (line, index) =>
+ index > block.start &&
+ index < block.end &&
+ line.trim() === "store_method: file_based",
+ );
+ if (methodIndex === -1) return;
+ const indent = lines[methodIndex]!.match(/^\s*/)?.[0] ?? "";
+ const intervalIndex = lines.findIndex(
+ (line, index) =>
+ index > block.start &&
+ index < block.end &&
+ line.trim().startsWith("save_interval_ms:"),
+ );
+ if (intervalIndex !== -1) {
+ if (override === undefined) return;
+ const existingIndent = lines[intervalIndex]!.match(/^\s*/)?.[0] ?? indent;
+ lines[intervalIndex] = `${existingIndent}save_interval_ms: ${override}`;
+ return;
+ }
+ lines.splice(
+ methodIndex + 1,
+ 0,
+ `${indent}save_interval_ms: ${override ?? AGENTMEMORY_STATE_SAVE_INTERVAL_MS}`,
+ );
+}
+
const REDIS_URL_ENV_REF = "${AGENTMEMORY_REDIS_URL}";
const UNSAFE_REDIS_URL_CHARS = /['\u0000-\u001f\u007f]/;
@@ -276,9 +326,9 @@ export function renderEngineConfig(
);
const usesRedis = options.stateBackend?.kind === "redis";
- if (!options.ports && !usesRedis) return rendered;
-
const lines = rendered.split("\n");
+ setFileStoreSaveInterval(lines, "iii-state", options.saveIntervalMs);
+ setFileStoreSaveInterval(lines, "iii-stream", options.saveIntervalMs);
if (options.ports) {
setWorkerPort(lines, "iii-http", options.ports.restPort);
setWorkerPort(lines, "iii-stream", options.ports.streamPort);
diff --git a/src/cli/engine-launch.ts b/src/cli/engine-launch.ts
index d96c4371..a5b980d8 100644
--- a/src/cli/engine-launch.ts
+++ b/src/cli/engine-launch.ts
@@ -163,6 +163,7 @@ export function rewriteBundledConfig(
dataDir: options?.dataDir ?? join(agentmemoryHome(home), "data"),
...(options?.ports ? { ports: options.ports } : {}),
...(options?.stateBackend ? { stateBackend: options.stateBackend } : {}),
+ ...(options?.saveIntervalMs !== undefined ? { saveIntervalMs: options.saveIntervalMs } : {}),
});
return removeAgentmemoryExecCommand(rendered, nodeBin, workerEntry);
}
@@ -189,8 +190,17 @@ export function legacyDataMigrations(
];
}
-export function engineChildEnv(base: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
- return { ...base, III_TELEMETRY_ENABLED: base["III_TELEMETRY_ENABLED"] ?? "false" };
+export const ENGINE_MALLOC_ARENA_MAX = "2";
+
+export function engineChildEnv(
+ base: NodeJS.ProcessEnv,
+ platform: NodeJS.Platform = process.platform,
+): NodeJS.ProcessEnv {
+ const env: NodeJS.ProcessEnv = { ...base, III_TELEMETRY_ENABLED: base["III_TELEMETRY_ENABLED"] ?? "false" };
+ if (platform === "linux" && !base.MALLOC_ARENA_MAX) {
+ env.MALLOC_ARENA_MAX = ENGINE_MALLOC_ARENA_MAX;
+ }
+ return env;
}
export type LaunchRenderFailure =
diff --git a/src/config.ts b/src/config.ts
index 359e7a76..693871f2 100644
--- a/src/config.ts
+++ b/src/config.ts
@@ -542,6 +542,11 @@ export function getIndexSaveIntervalMs(): number {
return raw > 0 ? raw : INDEX_SAVE_INTERVAL_DEFAULT_MS;
}
+export function getStateSaveIntervalMs(): number | undefined {
+ const raw = safeParseInt(getMergedEnv()["AGENTMEMORY_STATE_SAVE_INTERVAL_MS"], 0);
+ return raw > 0 ? raw : undefined;
+}
+
export const VECTOR_BUCKET_SIZE_DEFAULT = 500;
export function getVectorBucketSize(): number {
diff --git a/src/functions/audit.ts b/src/functions/audit.ts
index f0f9b524..aeed9c28 100644
--- a/src/functions/audit.ts
+++ b/src/functions/audit.ts
@@ -1,4 +1,5 @@
import { stat, readFile } from "node:fs/promises";
+import { homedir } from "node:os";
import { join } from "node:path";
import type { AuditEntry, AuditMigrationState, AuditQueryResult } from "../types.js";
import { KV, generateId } from "../state/schema.js";
@@ -7,7 +8,7 @@ import { logger } from "../logger.js";
import { withKeyedLock } from "../state/keyed-mutex.js";
import { getEnvVar, getAuditMigrateMaxBytes } from "../config.js";
import { runtimeConfigPath } from "../cli/engine-launch.js";
-import { configuredSaveIntervalMs } from "../cli/engine-config.js";
+import { ENGINE_DEFAULT_SAVE_INTERVAL_MS, engineSaveIntervalMs, engineStateConfigPaths } from "../cli/engine-config.js";
// Audit coverage policy (issue #125).
//
@@ -43,7 +44,6 @@ const AUDIT_MIGRATION_DELETE_CONCURRENCY = 8;
const AUDIT_MONTHS_LOCK = KV.auditMonths;
const AUDIT_MIGRATION_LOCK = "mem:audit:migration-lock";
export const AUDIT_MIGRATION_STATE_KEY = "migration";
-const DEFAULT_SAVE_INTERVAL_MS = 5000;
const AUDIT_MIGRATION_DELETE_WAIT_INTERVALS = 2;
const STATE_STORE_DIR_NAME = "state_store.db";
@@ -116,14 +116,14 @@ export async function probeLegacyAuditScope(kv: StateKV): Promise<LegacyAuditPro
async function auditMigrationDeleteDelayMs(): Promise<number> {
const dataDir = getEnvVar("AGENTMEMORY_DATA_DIR");
- if (!dataDir) return DEFAULT_SAVE_INTERVAL_MS * AUDIT_MIGRATION_DELETE_WAIT_INTERVALS;
- try {
- const rendered = await readFile(runtimeConfigPath(dataDir), "utf-8");
- const interval = configuredSaveIntervalMs(rendered) ?? DEFAULT_SAVE_INTERVAL_MS;
- return interval * AUDIT_MIGRATION_DELETE_WAIT_INTERVALS;
- } catch {
- return DEFAULT_SAVE_INTERVAL_MS * AUDIT_MIGRATION_DELETE_WAIT_INTERVALS;
+ if (!dataDir) return ENGINE_DEFAULT_SAVE_INTERVAL_MS * AUDIT_MIGRATION_DELETE_WAIT_INTERVALS;
+ const configTexts: string[] = [];
+ for (const path of engineStateConfigPaths(join(homedir(), ".agentmemory"), runtimeConfigPath(dataDir))) {
+ try {
+ configTexts.push(await readFile(path, "utf-8"));
+ } catch {}
}
+ return engineSaveIntervalMs(configTexts) * AUDIT_MIGRATION_DELETE_WAIT_INTERVALS;
}
async function readAuditMigrationState(kv: StateKV): Promise<AuditMigrationState | null> {
diff --git a/test/engine-flush-wait.test.ts b/test/engine-flush-wait.test.ts
index c5b3cb75..23edcf8b 100644
--- a/test/engine-flush-wait.test.ts
+++ b/test/engine-flush-wait.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { ENGINE_FLUSH_WAIT_CAP_MS, engineFlushWaitMs } from "../src/cli/engine-config.js";
+import { engineFlushWaitMs, engineSaveIntervalMs } from "../src/cli/engine-config.js";
const persistedState = [
"id: iii-state",
@@ -22,11 +22,27 @@ describe("engine flush wait on stop", () => {
it("follows a configured save interval", () => {
const custom = `${persistedState}\n save_interval_ms: 2000`;
- expect(engineFlushWaitMs("file", [custom, "save_interval_ms: 9000"])).toBe(3500);
+ expect(engineFlushWaitMs("file", [custom])).toBe(3500);
});
- it("caps a very long save interval", () => {
- expect(engineFlushWaitMs("file", ["save_interval_ms: 600000"])).toBe(ENGINE_FLUSH_WAIT_CAP_MS);
+ it("waits for the longest save interval across all configs", () => {
+ const custom = `${persistedState}\n save_interval_ms: 2000`;
+ expect(engineFlushWaitMs("file", [custom, "save_interval_ms: 9000"])).toBe(10500);
+ });
+
+ it("counts a file store without an explicit interval at the engine default", () => {
+ const stateAndStream = [
+ " - name: iii-state",
+ " store_method: file_based",
+ " save_interval_ms: 2000",
+ " - name: iii-stream",
+ " store_method: file_based",
+ ].join("\n");
+ expect(engineSaveIntervalMs([stateAndStream])).toBe(5000);
+ });
+
+ it("does not cut a long configured interval short", () => {
+ expect(engineFlushWaitMs("file", ["save_interval_ms: 600000"])).toBe(601500);
});
it("does not wait for the redis state backend", () => {
diff --git a/test/engine-launch-hardening.test.ts b/test/engine-launch-hardening.test.ts
new file mode 100644
index 00000000..ce3c4a75
--- /dev/null
+++ b/test/engine-launch-hardening.test.ts
@@ -0,0 +1,165 @@
+import { spawnSync } from "node:child_process";
+import { readFileSync } from "node:fs";
+import { join } from "node:path";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import {
+ AGENTMEMORY_STATE_SAVE_INTERVAL_MS,
+ configuredSaveIntervalMs,
+ engineFlushWaitMs,
+ engineSaveIntervalMs,
+ engineStateConfigPaths,
+ renderEngineConfig,
+} from "../src/cli/engine-config.js";
+import {
+ engineChildEnv,
+ rewriteBundledConfig,
+} from "../src/cli/engine-launch.js";
+import { getStateSaveIntervalMs } from "../src/config.js";
+
+const root = join(import.meta.dirname, "..");
+const read = (path: string) => readFileSync(join(root, path), "utf-8");
+
+function intervals(text: string): string[] {
+ return [...text.matchAll(/save_interval_ms:\s*(\S+)/g)].map((m) => m[1]!);
+}
+
+const customConfig = [
+ "workers:",
+ " - name: iii-state",
+ " config:",
+ " adapter:",
+ " name: kv",
+ " config:",
+ " store_method: file_based",
+ " file_path: /srv/state",
+ " - name: iii-stream",
+ " config:",
+ " adapter:",
+ " name: kv",
+ " config:",
+ " store_method: in_memory",
+].join("\n");
+
+describe("engine file-store save interval", () => {
+ afterEach(() => {
+ vi.unstubAllEnvs();
+ });
+
+ it("ships 2000 ms for both file-backed stores in every bundled config", () => {
+ expect(AGENTMEMORY_STATE_SAVE_INTERVAL_MS).toBe(2000);
+ expect(intervals(read("iii-config.yaml"))).toEqual(["2000", "2000"]);
+ expect(intervals(read("iii-config.docker.yaml"))).toEqual([
+ "${AGENTMEMORY_STATE_SAVE_INTERVAL_MS:2000}",
+ "${AGENTMEMORY_STATE_SAVE_INTERVAL_MS:2000}",
+ ]);
+ for (const platform of ["coolify", "fly", "railway", "render"]) {
+ expect(intervals(read(`deploy/${platform}/entrypoint.sh`))).toEqual(["2000", "2000"]);
+ }
+ });
+
+ it("keeps 2000 ms through the CLI render of the bundled config", () => {
+ const rendered = rewriteBundledConfig(read("iii-config.yaml"), "/home/u", "node", "/x/index.mjs", {
+ dataDir: "/home/u/.agentmemory/data",
+ ports: { restPort: 4711, streamPort: 4712, viewerPort: 4713, enginePort: 49951 },
+ });
+ expect(intervals(rendered)).toEqual(["2000", "2000"]);
+ expect(engineFlushWaitMs("file", [rendered])).toBe(3500);
+ });
+
+ it("applies an explicit override to both stores", () => {
+ const rendered = renderEngineConfig(read("iii-config.yaml"), {
+ dataDir: "/d",
+ saveIntervalMs: 750,
+ });
+ expect(intervals(rendered)).toEqual(["750", "750"]);
+ expect(configuredSaveIntervalMs(rendered)).toBe(750);
+ });
+
+ it("adds the interval to a custom file-backed store that has none and leaves in-memory stores alone", () => {
+ const rendered = renderEngineConfig(customConfig, { dataDir: "/d" });
+ expect(rendered).toContain(" store_method: file_based\n save_interval_ms: 2000\n");
+ expect(intervals(rendered)).toEqual(["2000"]);
+ });
+
+ it("keeps a value the user wrote in their own config unless overridden", () => {
+ const own = customConfig.replace("store_method: file_based", "store_method: file_based\n save_interval_ms: 9000");
+ expect(intervals(renderEngineConfig(own, { dataDir: "/d" }))).toEqual(["9000"]);
+ expect(intervals(renderEngineConfig(own, { dataDir: "/d", saveIntervalMs: 1000 }))).toEqual(["1000"]);
+ });
+
+ it("does not carry the interval into the redis adapter", () => {
+ const rendered = renderEngineConfig(read("iii-config.yaml"), {
+ dataDir: "/d",
+ stateBackend: { kind: "redis", redisUrl: "redis://localhost:6379" },
+ });
+ expect(intervals(rendered)).toEqual([]);
+ expect(engineFlushWaitMs("redis", [rendered])).toBe(0);
+ });
+
+ it("derives the interval from the persisted iii-state entry before the runtime config", () => {
+ const paths = engineStateConfigPaths("/home/u/.agentmemory", "/data/iii-config.runtime.yaml");
+ expect(paths[0]).toBe("/home/u/.agentmemory/config/iii-state.yaml");
+ expect(paths.at(-1)).toBe("/data/iii-config.runtime.yaml");
+ const persisted = "id: iii-state\nvalue:\n adapter:\n config:\n save_interval_ms: 2000\n store_method: file_based\n";
+ const strippedRuntime = "workers:\n - name: iii-state\n # value now lives in the configuration worker\n";
+ expect(engineSaveIntervalMs([persisted, strippedRuntime])).toBe(2000);
+ expect(engineSaveIntervalMs([strippedRuntime])).toBe(5000);
+ expect(engineFlushWaitMs("file", [persisted, strippedRuntime])).toBe(3500);
+ });
+
+ it("reads AGENTMEMORY_STATE_SAVE_INTERVAL_MS as a positive override", () => {
+ vi.stubEnv("AGENTMEMORY_STATE_SAVE_INTERVAL_MS", "1500");
+ expect(getStateSaveIntervalMs()).toBe(1500);
+ vi.stubEnv("AGENTMEMORY_STATE_SAVE_INTERVAL_MS", "0");
+ expect(getStateSaveIntervalMs()).toBeUndefined();
+ vi.stubEnv("AGENTMEMORY_STATE_SAVE_INTERVAL_MS", "abc");
+ expect(getStateSaveIntervalMs()).toBeUndefined();
+ });
+});
+
+describe("engine child environment", () => {
+ it("bounds glibc malloc arenas on linux", () => {
+ expect(engineChildEnv({ PATH: "/usr/bin" }, "linux")["MALLOC_ARENA_MAX"]).toBe("2");
+ });
+
+ it("keeps a MALLOC_ARENA_MAX the user already set", () => {
+ expect(engineChildEnv({ MALLOC_ARENA_MAX: "8" }, "linux")["MALLOC_ARENA_MAX"]).toBe("8");
+ });
+
+ it("does not set it on macOS or Windows", () => {
+ expect(engineChildEnv({}, "darwin")["MALLOC_ARENA_MAX"]).toBeUndefined();
+ expect(engineChildEnv({}, "win32")["MALLOC_ARENA_MAX"]).toBeUndefined();
+ });
+
+ it("sets it for the docker engine and the deploy entrypoints without overriding the user", () => {
+ expect(read("docker-compose.yml")).toContain('MALLOC_ARENA_MAX: "${MALLOC_ARENA_MAX:-2}"');
+ for (const platform of ["coolify", "fly", "railway", "render"]) {
+ expect(read(`deploy/${platform}/entrypoint.sh`)).toContain('if [ -z "${MALLOC_ARENA_MAX:-}" ]; then');
+ }
+ });
+});
+
+describe("engine open-file limit", () => {
+ it.skipIf(process.platform === "win32")("a child spawned by the CLI runtime inherits a soft limit raised to the hard limit", () => {
+ const script = [
+ 'const { execSync } = require("node:child_process");',
+ 'process.stdout.write(execSync("ulimit -n").toString().trim());',
+ ].join(" ");
+ const result = spawnSync("/bin/sh", ["-c", 'ulimit -S -n 256 && exec "$@"', "outer", process.execPath, "-e", script], {
+ encoding: "utf-8",
+ timeout: 15_000,
+ });
+ expect(result.status).toBe(0);
+ const hard = spawnSync("/bin/sh", ["-c", "ulimit -H -n"], { encoding: "utf-8" }).stdout.trim();
+ const seen = result.stdout.trim();
+ if (hard === "unlimited") expect(seen === "unlimited" || Number(seen) >= 10240).toBe(true);
+ else expect(Number(seen)).toBe(Number(hard));
+ });
+
+ it("sets the limit for the docker engine and the deploy entrypoints", () => {
+ expect(read("docker-compose.yml")).toMatch(/ulimits:\n\s+nofile:\n\s+soft: 10240\n\s+hard: 10240/);
+ for (const platform of ["coolify", "fly", "railway", "render"]) {
+ expect(read(`deploy/${platform}/entrypoint.sh`)).toContain("ulimit -n 10240 2>/dev/null");
+ }
+ });
+});