diff --git a/.env.example b/.env.example index fd65b49424..b5106deb07 100644 --- a/.env.example +++ b/.env.example @@ -138,7 +138,7 @@ # AGENTMEMORY_AUTO_COMPRESS=true # Run LLM-written compression on every observation batch. Requires BOTH this flag and a configured provider; a provider alone leaves synthetic compression active. # AGENTMEMORY_INJECT_CONTEXT=true # Opt in to Claude-style hook context injection. Pi/OMP use 1 in their launch environment for file enrichment; native prompt recall is independent. -# AGENTMEMORY_ENRICH_TIMEOUT_MS=2000 # How long the PreToolUse hook waits for /agentmemory/enrich before it gives up and adds no context. Default 2000 ms. Raise it when enrich replies arrive after the hook has given up. +# AGENTMEMORY_ENRICH_TIMEOUT_MS=2000 # PreToolUse hook wait (default 2000 ms). Set this in the hook process environment; setting it only in this server env file does not change the hook timeout. # CONSOLIDATION_ENABLED=false # Disable the consolidation pipeline. Otherwise enabled when an LLM provider is configured, unless AGENTMEMORY_PROVIDER=noop. # CONSOLIDATION_DECAY_DAYS=30 # Age (days) after which non-reinforced memories decay during consolidation # GRAPH_EXTRACTION_ENABLED=true # Extract concept-graph edges on remember; powers the graph-traversal recall path diff --git a/.github/workflows/release-gate.yml b/.github/workflows/release-gate.yml index 7998c1ff56..a60e7e7824 100644 --- a/.github/workflows/release-gate.yml +++ b/.github/workflows/release-gate.yml @@ -51,7 +51,7 @@ jobs: engine_pin=$(node --import tsx --input-type=module -e 'import { III_PINNED_VERSION } from "./src/version.ts"; console.log(III_PINNED_VERSION)') engine_bin="$HOME/.cache/agentmemory-release-gate/iii-${engine_pin}-linux-x64" test -x "$engine_bin" - AGENTMEMORY_TEST_III="$engine_bin" AGENTMEMORY_REQUIRE_LIVE_TESTS=1 npx vitest run test/compose-supervision-integration.test.ts test/codex-live-daemon.test.ts + AGENTMEMORY_TEST_III="$engine_bin" AGENTMEMORY_REQUIRE_LIVE_TESTS=1 npx vitest run test/compose-supervision-integration.test.ts test/codex-live-daemon.test.ts test/compose-c1-integration.test.ts - run: npm run test:startup:packaged -- --skip-build --out "${{ runner.temp }}/startup-gate" - name: Test packaged vector recovery shell: bash diff --git a/README.md b/README.md index 31ad6a5546..935cc8057f 100644 --- a/README.md +++ b/README.md @@ -1200,8 +1200,8 @@ EMBEDDING_PROVIDER=local # Optional automatic context injection through the shared hooks # AGENTMEMORY_INJECT_CONTEXT=true -# How long the PreToolUse hook waits for that context, in ms (default 2000) -# AGENTMEMORY_ENRICH_TIMEOUT_MS=2000 +# Set the PreToolUse timeout in the hook process environment, not this server file: +# AGENTMEMORY_ENRICH_TIMEOUT_MS=2000 (default 2000 ms) # Optional outside LLM for summaries and consolidation. A key alone does nothing; # this line selects the provider whose key agentmemory may use. diff --git a/src/cli/compose-project.ts b/src/cli/compose-project.ts index e8e5ec6732..cd684ae8d2 100644 --- a/src/cli/compose-project.ts +++ b/src/cli/compose-project.ts @@ -171,6 +171,9 @@ function yamlString(value: string): string { if (/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/.test(value)) { throw new Error("A Compose path or configuration value contains an unsupported control character."); } + if (/[\r\n\u0080-\u009f\u2028\u2029]/.test(value)) { + return JSON.stringify(value).replace(/[\u0080-\u009f\u2028\u2029]/g, (char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, "0")}`); + } return `'${value.replace(/'/g, "''")}'`; } diff --git a/test/compose-c1-integration.test.ts b/test/compose-c1-integration.test.ts new file mode 100644 index 0000000000..0cfd24e619 --- /dev/null +++ b/test/compose-c1-integration.test.ts @@ -0,0 +1,95 @@ +import { spawn, spawnSync, type ChildProcess } from "node:child_process"; +import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { createServer } from "node:net"; +import { createRequire } from "node:module"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { pathToFileURL } from "node:url"; +import { setTimeout as sleep } from "node:timers/promises"; +import { describe, expect, it } from "vitest"; +import { composeNamespace, writeWorkerCompose } from "../src/cli/compose-project.js"; +import { III_PINNED_VERSION } from "../src/version.js"; + +const engine = process.env.AGENTMEMORY_TEST_III; +const version = engine ? spawnSync(engine, ["--version"], { encoding: "utf8", timeout: 3000 }) : null; +const available = version?.status === 0 && version.stdout.trim() === III_PINNED_VERSION; +const skipped = !available || process.platform === "win32" || process.env.AGENTMEMORY_SKIP_LIVE_TESTS === "1"; +if (process.env.AGENTMEMORY_REQUIRE_LIVE_TESTS === "1" && skipped) { + throw new Error(`Required Compose C1 test needs iii ${III_PINNED_VERSION} and live tests enabled on macOS or Linux`); +} + +async function freePort(): Promise { + const server = createServer(); + await new Promise((accept, reject) => { + server.once("error", reject); + server.listen(0, "127.0.0.1", accept); + }); + const port = (server.address() as { port: number }).port; + await new Promise((accept) => server.close(() => accept())); + return port; +} + +async function stop(child: ChildProcess): Promise { + if (child.exitCode !== null || child.signalCode !== null) return; + const exited = new Promise((accept) => child.once("exit", () => accept())); + child.kill("SIGINT"); + const result = await Promise.race([exited.then(() => true), sleep(5000).then(() => false)]); + if (!result && child.pid) { + try { process.kill(-child.pid, "SIGKILL"); } catch {} + await exited; + } +} + +describe.skipIf(skipped)("pinned Compose C1 environment round trip", () => { + it("delivers all C1 characters and line breaks unchanged to a real worker", async () => { + const root = mkdtempSync(join(tmpdir(), "agentmemory-compose-c1-")); + let child: ChildProcess | undefined; + try { + const pkg = join(root, "worker"); + const snapshot = join(root, "received.json"); + mkdirSync(join(pkg, "dist"), { recursive: true }); + const value = `first\n${String.fromCharCode(...Array.from({ length: 32 }, (_, i) => 0x80 + i))}\u2028\u2029\r\nquote'\"\\\tend\n`; + const sdk = pathToFileURL(createRequire(import.meta.url).resolve("iii-sdk")).href; + writeFileSync(join(pkg, "dist/index.mjs"), ` + import sdkModule from ${JSON.stringify(sdk)}; + import { renameSync, writeFileSync } from "node:fs"; + const { registerWorker } = sdkModule; + const worker = registerWorker(process.env.III_URL, { workerName: "agentmemory" }); + worker.registerFunction("test::env", async () => process.env.OPENAI_BASE_URL); + writeFileSync(${JSON.stringify(`${snapshot}.tmp`)}, JSON.stringify(process.env.OPENAI_BASE_URL)); + renameSync(${JSON.stringify(`${snapshot}.tmp`)}, ${JSON.stringify(snapshot)}); + `); + const ports = { restPort: await freePort(), viewerPort: await freePort(), enginePort: await freePort() }; + const project = writeWorkerCompose({ + ports, projectRoot: pkg, dataDir: join(root, "data"), homeDir: join(root, "home"), + nodeBin: process.execPath, workerEnvironment: { OPENAI_BASE_URL: value }, + }); + const header = project.text.slice(0, project.text.indexOf("containers:\n")); + const app = project.text.slice(project.text.indexOf(" agentmemory:\n")) + .replace(" start_after: [state, http]", " start_after: []"); + writeFileSync(project.file, `${header}containers:\n${app}`); + child = spawn(engine!, ["compose", "--namespace", composeNamespace(ports.restPort), "--up", "--file", project.file], { + env: { PATH: process.env.PATH, HOME: join(root, "home"), USERPROFILE: join(root, "home"), + III_COMPOSE_STATE_DIR: project.stateDir, III_TELEMETRY_ENABLED: "false", III_BUNDLE_WORKERS_DISABLED: "1", RUST_LOG: "error" }, + detached: true, stdio: ["ignore", "ignore", "pipe"], + }); + let logs = ""; + child.stderr?.on("data", (chunk) => { logs = `${logs}${chunk}`.slice(-16000); }); + const deadline = Date.now() + 20000; + while (Date.now() < deadline) { + try { + expect(JSON.parse(readFileSync(snapshot, "utf8"))).toBe(value); + return; + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; + } + if (child.exitCode !== null || child.signalCode !== null) throw new Error(`Compose exited before the worker received its value: ${logs}`); + await sleep(50); + } + throw new Error(`Worker environment snapshot timed out: ${logs}`); + } finally { + if (child) await stop(child); + rmSync(root, { recursive: true, force: true }); + } + }, 30000); +}); diff --git a/test/compose-project.test.ts b/test/compose-project.test.ts index 5809e2da4e..0a7047eddf 100644 --- a/test/compose-project.test.ts +++ b/test/compose-project.test.ts @@ -24,6 +24,30 @@ const options = (overrides: Record = {}) => ({ }); describe("Worker Compose project", () => { + it.each(Array.from({ length: 32 }, (_, offset) => 0x80 + offset))( + "escapes C1 character U+%s in single-line and multiline scalars", (code) => { + for (const value of [`first${String.fromCharCode(code)}second`, `first\n${String.fromCharCode(code)}second`]) { + const text = renderWorkerCompose(options({ workerEnvironment: { OPENAI_BASE_URL: value } })); + expect(text).not.toMatch(/[\u0080-\u009f]/); + expect(parse(text).containers.agentmemory.environment.OPENAI_BASE_URL).toBe(value); + } + }, + ); + it.each(["first\u2028second", "first\u2029second", "first\n\u2028\u2029second"])( + "escapes Unicode line and paragraph separators: %j", (value) => { + const text = renderWorkerCompose(options({ workerEnvironment: { OPENAI_BASE_URL: value } })); + expect(text).not.toMatch(/[\u2028\u2029]/); + expect(text).toContain(`OPENAI_BASE_URL: ${JSON.stringify(value).replace(/[\u2028\u2029]/g, (char) => `\\u${char.charCodeAt(0).toString(16)}`)}`); + expect(parse(text).containers.agentmemory.environment.OPENAI_BASE_URL).toBe(value); + }, + ); + it.each(["first\nsecond", "first\rsecond", "first\r\nsecond", "\nleading\n\ntrailing\n", "quote'\"\\\nnext\tline"])( + "preserves multiline configuration values through YAML parsing: %j", (value) => { + const text = renderWorkerCompose(options({ workerEnvironment: { OPENAI_BASE_URL: value } })); + expect(parse(text).containers.agentmemory.environment.OPENAI_BASE_URL).toBe(value); + }, + ); + it("pins registry workers and configures the isolated engine, state and HTTP", () => { const text = renderWorkerCompose(options()); expect(text).toContain("namespace: agentmemory-3511");