import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; import { EventEmitter } from "node:events"; import { createHash } from "node:crypto"; import { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, utimesSync, writeFileSync, } from "node:fs"; import { homedir, tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { connect } from "../../../core/src/db.js"; import { buildMemoryPackWithTrace } from "../../../core/src/pack.js"; import { MemoryStore } from "../../../core/src/store.js"; import { getRetrievalAttempt } from "../../../core/src/retrieval-ledger.js"; import { automaticRecallHealth } from "../../../core/src/automatic-recall.js"; import { initTestSchema } from "../../../core/src/test-utils.js"; import { refreshTestScopeRows } from "../../../core/src/scope-membership-cache-test-fixtures.js"; import { ensureDeviceIdentity, loadPublicKey } from "../../../core/src/sync-identity.js"; import { buildViewerIdentityTarget } from "../../../core/src/identity-target.js"; import { writeRawEventSpoolEntry } from "../../../opencode-plugin/.opencode/lib/raw-event-spool.js"; import { handleInstrumentedPackLedger, handlePromptPackLedger, parseInternalLedgerPayload, } from "../../src/commands/pack.js"; const spawnMock = vi.fn(); const execSyncMock = vi.fn(() => "test-version"); const pluginRegistrationsKey = Symbol.for("codemem.opencode-plugin.registrations"); vi.mock("node:child_process", () => ({ spawn: (...args) => spawnMock(...args), execSync: (...args) => execSyncMock(...args), })); const makeProcess = ({ stdout = "", stderr = "", exitCode = 0 }) => { const proc = new EventEmitter(); proc.stdout = new EventEmitter(); proc.stderr = new EventEmitter(); proc.stdin = { write: vi.fn(), end: vi.fn(), }; queueMicrotask(() => { if (stdout) proc.stdout.emit("data", stdout); if (stderr) proc.stderr.emit("data", stderr); proc.emit("exit", exitCode); }); return proc; }; const jsonResponse = (status, body) => ({ ok: status >= 200 && status < 300, status, json: vi.fn().mockResolvedValue(body), }); const holdSpoolPublication = async (home) => { const fs = await vi.importActual("node:fs/promises"); let release; let reached; const gate = new Promise((resolve) => { release = resolve; }); const publication = new Promise((resolve) => { reached = resolve; }); vi.doMock("node:fs/promises", () => ({ ...fs, link: async (temporary, destination) => { if (String(temporary).startsWith(join(home, ".codemem", "opencode-raw-event-spool"))) { reached(); await gate; } return fs.link(temporary, destination); }, })); return { publication, release, restore: () => { release(); vi.doUnmock("node:fs/promises"); }, }; }; const assertSpoolLatchRecovery = async ({ home, hooks, appLog, showToast, barrier }) => { // Act: fail once, then retry the retained event through the public idle hook. await hooks["tool.execute.after"]({ tool: "read", args: {}, sessionID: "sess-latch-first" }, {}); const deliveries = (kind) => appLog.mock.calls.filter(([entry]) => entry.body?.extra?.delivery === kind); await vi.waitFor(() => expect(deliveries("memory")).toHaveLength(1)); expect( appLog.mock.calls.filter(([entry]) => entry.body?.message === "codemem could not save a raw event for retry; it remains queued in memory", ), ).toHaveLength(1); rmSync(join(home, ".codemem"), { force: true }); const recovery = hooks.event({ event: { type: "session.idle", properties: { sessionID: "sess-latch-first" } }, }); await barrier.publication; await hooks["tool.execute.after"]({ tool: "read", args: {}, sessionID: "sess-latch-success" }, {}); const spoolDirectory = join(home, ".codemem", "opencode-raw-event-spool"); // Assert: the old readiness check passes while publication is still blocked. expect(readdirSync(spoolDirectory)).not.toHaveLength(0); expect(deliveries("spool")).toHaveLength(0); barrier.release(); // Act: BOTH writes must finish directory sync and reset the warning latch. await vi.waitFor(() => expect(deliveries("spool")).toHaveLength(2)); await recovery; expect( readdirSync(spoolDirectory) .map((name) => JSON.parse(readFileSync(join(spoolDirectory, name), "utf8")).session_id) .sort(), ).toEqual(["sess-latch-first", "sess-latch-success"]); rmSync(join(home, ".codemem"), { recursive: true, force: true }); writeFileSync(join(home, ".codemem"), "blocked-again"); await hooks["tool.execute.after"]({ tool: "read", args: {}, sessionID: "sess-latch-second" }, {}); await vi.waitFor(() => expect(deliveries("memory")).toHaveLength(2)); // Assert: genuine recovery allows exactly one new persistence warning/toast. expect( appLog.mock.calls.filter(([entry]) => entry.body?.message === "codemem could not save a raw event for retry; it remains queued in memory", ), ).toHaveLength(2); expect( showToast.mock.calls.filter(([entry]) => entry.body?.message.includes("could not save a raw event for retry"), ), ).toHaveLength(2); }; const packResponse = (text = "## Summary\n[1] (feature) Viewer-backed context") => ({ pack_text: text, metrics: { total_items: 1, pack_tokens: 24 }, }); test("OpenCode identity targeting matches the core contract for absolute paths", async () => { const root = resolve("/tmp/codemem-opencode-identity-parity"); const env = { HOME: join(root, "home"), CODEMEM_DEVICE_ID: "device-1", CODEMEM_ACTOR_ID: "actor-1", CODEMEM_CONFIG: join(root, "config.toml"), CODEMEM_RUNTIME_ROOT: join(root, "runtime"), CODEMEM_WORKSPACE_ID: "workspace-1", CODEMEM_PACK_COMPRESSION: "ids", CODEMEM_EMBEDDING_DISABLED: "yes", CODEMEM_EMBEDDING_OFFLINE: "YES", CODEMEM_EMBEDDING_MODEL: "model-1", CODEMEM_EMBEDDING_REVISION: "revision-1", }; const { __testUtils } = await import("../plugins/codemem.js"); expect(__testUtils.buildViewerIdentityTarget(env, root)).toEqual(buildViewerIdentityTarget(env)); expect(__testUtils.buildViewerIdentityTarget(env, root)).toMatchObject({ embedding_offline: true, }); }); test("OpenCode identity targeting preserves a missing custom-model revision as null", async () => { const root = resolve("/tmp/codemem-opencode-identity-custom-model"); const env = { HOME: join(root, "home"), CODEMEM_EMBEDDING_MODEL: "custom/model", CODEMEM_EMBEDDING_REVISION: " ", }; const { __testUtils } = await import("../plugins/codemem.js"); expect(__testUtils.buildViewerIdentityTarget(env, root)).toEqual(buildViewerIdentityTarget(env)); expect(__testUtils.buildViewerIdentityTarget(env, root)).toMatchObject({ embedding_model: "custom/model", embedding_revision: null, }); }); test("OpenCode identity retains a mutable revision for the default model", async () => { const root = resolve("/tmp/codemem-opencode-identity-default-mutable"); const env = { HOME: join(root, "home"), // Core retains the configured request while the runtime resolves the ref to // a canonical commit for persisted vector identity. CODEMEM_EMBEDDING_REVISION: "main", }; const { __testUtils } = await import("../plugins/codemem.js"); expect(__testUtils.buildViewerIdentityTarget(env, root)).toEqual(buildViewerIdentityTarget(env)); expect(__testUtils.buildViewerIdentityTarget(env, root)).toMatchObject({ embedding_model: "Xenova/bge-small-en-v1.5", embedding_revision: "main", }); }); const normalizeIdentityPath = (value, cwd = "/tmp/atlas") => { const trimmed = String(value || "").trim(); if (!trimmed) return null; const expanded = trimmed.startsWith("~/") ? join(process.env.HOME?.trim() || homedir(), trimmed.slice(2)) : trimmed; return resolve(cwd, expanded); }; const viewerProfileResponse = (overrides = {}) => { const embeddingModel = process.env.CODEMEM_EMBEDDING_MODEL || "Xenova/bge-small-en-v1.5"; const rawEmbeddingRevision = process.env.CODEMEM_EMBEDDING_REVISION?.trim(); // Mirror core's configured request identity; runtime resolution happens before persistence. let embeddingRevision; if (rawEmbeddingRevision) { embeddingRevision = rawEmbeddingRevision; } else { embeddingRevision = embeddingModel === "Xenova/bge-small-en-v1.5" ? "ea104dacec62c0de699686887e3f920caeb4f3e3" : null; } return jsonResponse(200, { service: "codemem-viewer", protocol_version: 1, min_supported_protocol_version: 1, db_path: resolve( normalizeIdentityPath(process.env.CODEMEM_DB) || join(homedir(), ".codemem", "mem.sqlite"), ), identity_target: { device_id: process.env.CODEMEM_DEVICE_ID?.trim() || null, actor_id_present: Object.hasOwn(process.env, "CODEMEM_ACTOR_ID"), actor_id: process.env.CODEMEM_ACTOR_ID?.trim() || null, config_path: normalizeIdentityPath(process.env.CODEMEM_CONFIG), runtime_root: normalizeIdentityPath(process.env.CODEMEM_RUNTIME_ROOT), workspace_id: process.env.CODEMEM_WORKSPACE_ID?.trim() || null, home_dir: normalizeIdentityPath(process.env.HOME || homedir()), pack_compression: process.env.CODEMEM_PACK_COMPRESSION?.trim() || null, embedding_disabled: ["1", "true", "yes"].includes( String(process.env.CODEMEM_EMBEDDING_DISABLED || "").toLowerCase(), ), embedding_offline: ["1", "true", "yes"].includes( String(process.env.CODEMEM_EMBEDDING_OFFLINE || "").toLowerCase(), ), embedding_model: embeddingModel, embedding_revision: embeddingRevision, }, ...overrides, }); }; const messageOutput = ({ messageId = "user-viewer", sessionID = "sess-viewer", text = "use viewer transport", } = {}) => ({ messages: [ { info: { id: messageId, sessionID, role: "user" }, parts: [ { id: `${messageId}-text`, sessionID, messageID: messageId, type: "text", text, }, ], }, ], }); const fetchPostCalls = (fetchMock) => fetchMock.mock.calls.filter(([, options]) => options?.method === "POST"); const fetchBody = (fetchMock, callIndex) => JSON.parse(fetchPostCalls(fetchMock)[callIndex][1].body); const isPackOrLedgerSpawn = ([, args]) => Array.isArray(args) && (args.includes("pack") || args.includes("prompt-pack-ledger")); const makeProcessFromPackCommand = (args, options = {}) => { const proc = new EventEmitter(); proc.stdout = new EventEmitter(); proc.stderr = new EventEmitter(); proc.stdin = { write: vi.fn(), end: vi.fn(), }; queueMicrotask(async () => { const stdout = []; const stderr = []; const originalCwd = process.cwd(); const originalExitCode = process.exitCode; const originalLog = console.log; const originalError = console.error; try { const cwd = options.cwd; if (cwd) process.chdir(cwd); process.exitCode = 0; console.log = (...values) => { stdout.push(values.join(" ")); }; console.error = (...values) => { stderr.push(values.join(" ")); }; const packIndex = args.indexOf("pack"); if (packIndex < 0) throw new Error(`pack command missing from ${args.join(" ")}`); const { packCommand } = await import("../../src/commands/pack.js"); await packCommand.parseAsync( args.slice(packIndex + 1).filter((arg) => arg !== "--internal-ledger"), { from: "user" }, ); const out = stdout.length > 0 ? `${stdout.join("\n")}\n` : ""; const err = stderr.length > 0 ? `${stderr.join("\n")}\n` : ""; if (out) proc.stdout.emit("data", out); if (err) proc.stderr.emit("data", err); proc.emit("exit", typeof process.exitCode === "number" ? process.exitCode : 0); } catch (error) { proc.stderr.emit("data", error instanceof Error ? error.message : String(error)); proc.emit("exit", 1); } finally { console.log = originalLog; console.error = originalError; process.exitCode = originalExitCode; if (process.cwd() !== originalCwd) process.chdir(originalCwd); } }); return proc; }; const insertSession = (db, { cwd, project }) => { const now = new Date().toISOString(); const info = db .prepare("INSERT INTO sessions(started_at, cwd, project, user, tool_version) VALUES (?, ?, ?, ?, ?)") .run(now, cwd, project, "plugin-test", "test"); return Number(info.lastInsertRowid); }; const insertCoordinatorScope = (db, scopeId) => { const now = new Date().toISOString(); db.prepare( `INSERT OR REPLACE INTO replication_scopes( scope_id, label, kind, authority_type, coordinator_id, group_id, membership_epoch, status, created_at, updated_at ) VALUES (?, ?, 'team', 'coordinator', 'coord-test', 'group-test', 0, 'active', ?, ?)`, ).run(scopeId, scopeId, now, now); }; const insertScopeMembership = (db, scopeId, deviceId) => { insertCoordinatorScope(db, scopeId); db.prepare( `INSERT OR REPLACE INTO scope_memberships( scope_id, device_id, role, status, membership_epoch, coordinator_id, group_id, updated_at ) VALUES (?, ?, 'member', 'active', 0, 'coord-test', 'group-test', ?)`, ).run(scopeId, deviceId, new Date().toISOString()); }; const grantScopeToDevice = async (db, scopeId, deviceId, keysDir) => { insertScopeMembership(db, scopeId, deviceId); // Use the same isolated signing identity as the CLI, not the helper's static key. ensureDeviceIdentity(db, { deviceId, keysDir }); const publicKey = loadPublicKey(keysDir); if (!publicKey) throw new Error("Missing fixture signing key"); // The default fixture date is fixed; retained proofs must match this test's clock. await refreshTestScopeRows(db, { [deviceId]: publicKey }, { now: new Date() }); }; const insertScopedMemory = ( db, { sessionId, scopeId, title, bodyText }, ) => { const now = new Date().toISOString(); db.prepare( `INSERT INTO memory_items(session_id, kind, title, body_text, confidence, tags_text, active, created_at, updated_at, metadata_json, rev, visibility, scope_id) VALUES (?, 'discovery', ?, ?, 0.9, '', 1, ?, ?, '{}', 1, 'shared', ?)`, ).run(sessionId, title, bodyText, now, now, scopeId); }; describe("OpenCode transform-time injection", () => { const originalEnv = { ...process.env }; const tmpDirs = []; beforeEach(() => { // The plugin schedules a delayed compatibility check that can emit its own // toast if a slow pack command crosses the timer boundary. These tests only // cover transform-time injection, so keep that background timer inert. vi.useFakeTimers(); vi.resetModules(); Reflect.deleteProperty(globalThis, pluginRegistrationsKey); spawnMock.mockReset(); execSyncMock.mockClear(); process.env = { ...originalEnv, HOME: mkdtempSync(join(tmpdir(), "codemem-opencode-transform-home-")), CODEMEM_VIEWER: "0", // This suite mocks the legacy command process, not default CLI discovery. CODEMEM_RUNNER: "npx", CODEMEM_PLUGIN_DEBUG: "1", CODEMEM_PLUGIN_LOG: "0", CODEMEM_INJECT_CONTEXT: "1", }; tmpDirs.push(process.env.HOME); for (const key of [ "CODEMEM_DB", "CODEMEM_DEVICE_ID", "CODEMEM_KEYS_DIR", "CODEMEM_ACTOR_ID", "CODEMEM_CONFIG", "CODEMEM_RUNTIME_ROOT", "CODEMEM_WORKSPACE_ID", "CODEMEM_PACK_COMPRESSION", "CODEMEM_EMBEDDING_DISABLED", "CODEMEM_EMBEDDING_OFFLINE", "CODEMEM_EMBEDDING_MODEL", "CODEMEM_EMBEDDING_REVISION", "CODEMEM_INJECT_TOKEN_BUDGET", "CODEMEM_INJECT_RETAINED_TOKEN_BUDGET", ]) { delete process.env[key]; } }); afterEach(() => { vi.clearAllTimers(); vi.useRealTimers(); vi.restoreAllMocks(); for (const tmpDir of tmpDirs.splice(0)) { // Unawaited plugin debug/spool writers can still add files under HOME // during teardown; retry instead of failing cleanup with ENOTEMPTY. rmSync(tmpDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 }); } process.env = originalEnv; Reflect.deleteProperty(globalThis, pluginRegistrationsKey); }); test("loads separate plugin copies once for the same project", async () => { process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "1"; process.env.CODEMEM_PLUGIN_LOG = join(process.env.HOME, "plugin.log"); const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => { if (String(url).includes("/api/raw-events/status")) { return jsonResponse(200, { ingest: { available: true } }); } return jsonResponse(200, { ok: true }); }); const firstAppLog = vi.fn().mockResolvedValue(undefined); const secondAppLog = vi.fn().mockResolvedValue(undefined); const { OpencodeMemPlugin: firstPlugin } = await import("../plugins/codemem.js"); vi.resetModules(); const { OpencodeMemPlugin: secondPlugin } = await import("../plugins/codemem.js"); expect(secondPlugin).not.toBe(firstPlugin); const pluginContext = { project: { name: "atlas" }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }; const firstHooks = await firstPlugin({ ...pluginContext, client: { app: { log: firstAppLog }, tui: {} }, }); const secondHooks = await secondPlugin({ ...pluginContext, client: { app: { log: secondAppLog }, tui: {} }, }); for (const hooks of [firstHooks, secondHooks]) { await hooks["tool.execute.after"]?.( { tool: "read", args: { filePath: "src/raw-event.ts" }, sessionID: "sess-duplicate" }, {}, ); } await vi.waitFor(() => expect(fetchPostCalls(fetchMock).filter(([url]) => String(url).endsWith("/api/raw-events"))) .toHaveLength(1), ); expect(secondHooks).toEqual({}); expect(secondAppLog).toHaveBeenCalledWith({ body: expect.objectContaining({ level: "warn", message: "codemem duplicate plugin registration skipped", }), }); const captureLines = readFileSync(process.env.CODEMEM_PLUGIN_LOG, "utf8") .split("\n") .filter((line) => line.includes("tool.execute.after read queued=1")); expect(captureLines).toHaveLength(1); }); test("keeps separate projects active in one process", async () => { process.env.CODEMEM_RAW_EVENTS = "0"; const { OpencodeMemPlugin: firstPlugin } = await import("../plugins/codemem.js"); vi.resetModules(); const { OpencodeMemPlugin: secondPlugin } = await import("../plugins/codemem.js"); const client = { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }; const firstHooks = await firstPlugin({ project: { name: "first" }, client, directory: "/tmp/first", worktree: "/tmp/first", }); const secondHooks = await secondPlugin({ project: { name: "second" }, client, directory: "/tmp/second", worktree: "/tmp/second", }); expect(firstHooks["tool.execute.after"]).toBeTypeOf("function"); expect(secondHooks["tool.execute.after"]).toBeTypeOf("function"); }); test("releases a project registration when the plugin is disposed", async () => { process.env.CODEMEM_RAW_EVENTS = "0"; const { OpencodeMemPlugin: firstPlugin } = await import("../plugins/codemem.js"); vi.resetModules(); const { OpencodeMemPlugin: secondPlugin } = await import("../plugins/codemem.js"); const pluginContext = { project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }; const firstHooks = await firstPlugin(pluginContext); firstHooks.dispose(); const replacementHooks = await secondPlugin(pluginContext); expect(replacementHooks["tool.execute.after"]).toBeTypeOf("function"); }); test("preserves delivery when an older CLI rejects optional recall measurements", async () => { const receipts = []; let ledgerCalls = 0; spawnMock.mockImplementation((_command, args) => { if (args.includes("pack")) return makeProcess({ stdout: JSON.stringify(packResponse()) }); if (!args.includes("prompt-pack-ledger")) return makeProcess({}); const proc = makeProcess({ exitCode: ++ledgerCalls === 1 ? 1 : 0 }); proc.stdin.write = vi.fn((value) => receipts.push(JSON.parse(String(value)))); return proc; }); const { CodememPlugin } = await import("../plugins/codemem.js"); const hooks = await CodememPlugin({ project: { name: "fixture" }, client: { app: { log: vi.fn() }, tui: {} }, directory: "/tmp/fixture", worktree: "/tmp/fixture" }); const output = messageOutput({ messageId: "old-cli" }); await hooks["experimental.chat.messages.transform"]({}, output); await vi.waitFor(() => expect(receipts).toHaveLength(2)); expect(receipts[0].automatic_recall).toMatchObject({ candidateItems: 1, packMetadata: "missing" }); expect(receipts[1]).toEqual({ action: "delivery", attempt_id: receipts[0].attempt_id, delivery_status: "handed_off" }); expect(output.messages[0].parts.at(-1).text).toContain("Viewer-backed context"); }); test("retries a stale compatible Viewer delivery once without optional recall fields", async () => { process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url, options) => { if (String(url).endsWith("/api/prompt-pack-profile")) return viewerProfileResponse(); if (String(url).endsWith("/api/pack")) return jsonResponse(200, packResponse()); const body = JSON.parse(options.body); return body.automatic_recall ? jsonResponse(409, { error: { code: "viewer_contract_unsupported" } }) : jsonResponse(200, { ok: true }); }); const { CodememPlugin } = await import("../plugins/codemem.js"); const hooks = await CodememPlugin({ project: { name: "fixture" }, client: { app: { log: vi.fn() }, tui: {} }, directory: "/tmp/fixture", worktree: "/tmp/fixture", }); await hooks["experimental.chat.messages.transform"]( {}, messageOutput({ messageId: "stale-viewer" }), ); await vi.waitFor(() => expect( fetchPostCalls(fetchMock).filter(([url]) => String(url).endsWith("/api/prompt-pack-ledger"), ), ).toHaveLength(2), ); const receipts = fetchPostCalls(fetchMock) .filter(([url]) => String(url).endsWith("/api/prompt-pack-ledger")) .map(([, options]) => JSON.parse(options.body)); expect(receipts[0].automatic_recall).toBeDefined(); expect(receipts[1]).toMatchObject({ action: "delivery", attempt_id: receipts[0].attempt_id, delivery_status: "handed_off", }); expect(receipts[1].automatic_recall).toBeUndefined(); expect(spawnMock.mock.calls.filter(isPackOrLedgerSpawn)).toEqual([]); }); test.each([ [401, "unauthorized"], [403, "policy_denied"], ])( "does not retry or fall back after Viewer ledger policy/auth failure %s", async (status, code) => { process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => { if (String(url).endsWith("/api/prompt-pack-profile")) return viewerProfileResponse(); if (String(url).endsWith("/api/pack")) return jsonResponse(200, packResponse()); return jsonResponse(status, { error: { code } }); }); const { CodememPlugin } = await import("../plugins/codemem.js"); const hooks = await CodememPlugin({ project: { name: "fixture" }, client: { app: { log: vi.fn() }, tui: {} }, directory: "/tmp/fixture", worktree: "/tmp/fixture", }); await hooks["experimental.chat.messages.transform"]( {}, messageOutput({ messageId: `terminal-${status}` }), ); await vi.waitFor(() => expect( fetchPostCalls(fetchMock).filter(([url]) => String(url).endsWith("/api/prompt-pack-ledger"), ), ).toHaveLength(1), ); expect(spawnMock.mock.calls.filter(isPackOrLedgerSpawn)).toEqual([]); }, ); test("appends built memory pack to the latest user message by default", async () => { const ledgerPayloads = []; spawnMock.mockImplementation((_command, args) => { if (Array.isArray(args) && args.includes("pack")) { const proc = makeProcess({ stdout: JSON.stringify({ pack_text: "## Summary\n[1] (feature) Titanic artifact client shipped", metrics: { total_items: 1, pack_tokens: 42 }, }), }); proc.stdin.write = vi.fn((value) => ledgerPayloads.push(JSON.parse(String(value)))); return proc; } const proc = makeProcess({ stdout: "" }); proc.stdin.write = vi.fn((value) => ledgerPayloads.push(JSON.parse(String(value)))); return proc; }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {}, }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); expect(typeof hooks["experimental.chat.messages.transform"]).toBe("function"); const output = { messages: [ { info: { id: "user-1", sessionID: "sess-1", role: "user" }, parts: [ { id: "user-1-text", sessionID: "sess-1", messageID: "user-1", type: "text", text: "ship the Titanic artifact client", }, ], }, ], }; await hooks["experimental.chat.messages.transform"]({}, output); expect(output.messages[0].parts.at(-1)).toEqual({ id: "codemem-context-user-1", sessionID: "sess-1", messageID: "user-1", type: "text", text: "[codemem context]\n## Summary\n[1] (feature) Titanic artifact client shipped", synthetic: true, }); expect(spawnMock).toHaveBeenCalledTimes(2); expect(ledgerPayloads).toHaveLength(2); expect(ledgerPayloads[0].attempt_id).toMatch(/^[0-9a-f-]{36}$/); expect(ledgerPayloads[0].request_id).toMatch(/^[0-9a-f-]{36}$/); expect(ledgerPayloads[0].request_id).not.toBe(ledgerPayloads[0].attempt_id); expect(ledgerPayloads[1]).toMatchObject({ action: "delivery", attempt_id: ledgerPayloads[0].attempt_id, delivery_status: "handed_off", }); }); test.each(["viewer", "fallback"])("applies remaining retained allowance through %s", async (transport) => { process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; process.env.CODEMEM_INJECT_RETAINED_TOKEN_BUDGET = "100"; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => { if (String(url).endsWith("/api/prompt-pack-profile")) return viewerProfileResponse(); if (String(url).endsWith("/api/pack")) return transport === "viewer" ? jsonResponse(200, packResponse("fresh")) : jsonResponse(503, { error: "unavailable" }); return jsonResponse(200, { ok: true }); }); spawnMock.mockImplementation(() => makeProcess({ stdout: JSON.stringify(packResponse("fresh")) })); const { CodememPlugin, __testUtils } = await import("../plugins/codemem.js"); const hooks = await CodememPlugin({ project: { name: "fixture" }, client: { app: { log: vi.fn() }, tui: {} }, directory: "/tmp/fixture", worktree: "/tmp/fixture" }); const old = messageOutput({ messageId: "old" }).messages[0]; const text = `[codemem context]\n${"x".repeat(320)}`; old.parts.push({ id: "codemem-context-old", type: "text", text, synthetic: true }); const output = messageOutput({ messageId: "new" }); output.messages.unshift(old); await hooks["experimental.chat.messages.transform"]({}, output); const remaining = 100 - __testUtils.estimateTokens(text); expect(fetchBody(fetchMock, 0).token_budget).toBe(remaining - 5); if (transport === "fallback") { const args = spawnMock.mock.calls.find(([, args]) => args.includes("pack"))[1]; expect(args[args.indexOf("--token-budget") + 1]).toBe(String(remaining - 5)); } expect(__testUtils.countRetainedInjectionTokens(output.messages)).toBeLessThanOrEqual(100); expect(old.parts.at(-1).text).toBe(text); }); test.each(["viewer", "fallback"])("dedup, changed facts, continuation, restart and measurements agree via %s", async (transport) => { process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; process.env.CODEMEM_PLUGIN_LOG = join(process.env.HOME, "recall.log"); let facts = "private fixture facts"; const recorded = []; const response = () => { const pack_text = `## Summary\n[1] ${facts}`; return { ...packResponse(pack_text), rendered_items: [{ id: 1, fingerprint: createHash("sha256").update(facts).digest("hex"), spans: [{ start: 11, end: pack_text.length }], }] }; }; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => { if (String(url).endsWith("/api/prompt-pack-profile")) return viewerProfileResponse(); if (String(url).endsWith("/api/pack")) return transport === "viewer" ? jsonResponse(200, response()) : jsonResponse(503, { error: "unavailable" }); return jsonResponse(200, { ok: true }); }); spawnMock.mockImplementation((_cmd, args) => { const proc = makeProcess({ stdout: args.includes("pack") ? JSON.stringify(response()) : "{}" }); if (args.includes("prompt-pack-ledger")) proc.stdin.write = vi.fn((value) => recorded.push(JSON.parse(String(value)))); return proc; }); const { CodememPlugin, __testUtils } = await import("../plugins/codemem.js"); const init = { project: { name: "fixture" }, client: { app: { log: vi.fn() }, tui: {} }, directory: "/tmp/fixture", worktree: "/tmp/fixture" }; let hooks = await CodememPlugin(init); const output = messageOutput({ messageId: "first", text: "retrieve private fixture" }); await hooks["experimental.chat.messages.transform"]({}, output); const original = JSON.stringify(output.messages[0]); output.messages.push(messageOutput({ messageId: "second", text: "continue" }).messages[0]); await hooks["experimental.chat.messages.transform"]({}, output); expect(output.messages[1].parts).toHaveLength(1); facts = "updated private fixture facts"; await hooks["experimental.chat.messages.transform"]({}, output); expect(output.messages[1].parts.at(-1).text).toContain(facts); expect(JSON.stringify(output.messages[0])).toBe(original); const retained = JSON.stringify(output); const requestsBeforeRestart = fetchPostCalls(fetchMock).filter(([url]) => String(url).endsWith("/api/pack")).length; hooks.dispose(); hooks = await CodememPlugin(init); await hooks["experimental.chat.messages.transform"]({}, output); expect(JSON.stringify(output)).toBe(retained); expect(fetchPostCalls(fetchMock).filter(([url]) => String(url).endsWith("/api/pack")).length).toBe(requestsBeforeRestart); await vi.waitFor(() => expect(readFileSync(process.env.CODEMEM_PLUGIN_LOG, "utf8").split("\n").filter((line) => line.includes("inject.recall "))).toHaveLength(4)); const lines = readFileSync(process.env.CODEMEM_PLUGIN_LOG, "utf8").split("\n").filter((line) => line.includes("inject.recall ")); const measurements = lines.map((line) => JSON.parse(line.split("inject.recall ")[1])); expect(measurements.map((value) => value.reason)).toEqual(["delivered", "continuation_only", "delivered", "replay"]); expect(measurements[1]).toMatchObject({ new_tokens: 0, duplicates_omitted: 1 }); expect(measurements.at(-1)).toMatchObject({ new_tokens: 0, retained_tokens: __testUtils.countRetainedInjectionTokens(output.messages) }); expect(JSON.stringify(measurements)).not.toContain("private"); const ledgerMeasurements = transport === "viewer" ? fetchPostCalls(fetchMock).filter(([url]) => String(url).endsWith("/api/prompt-pack-ledger")).map(([, options]) => JSON.parse(options.body)) : recorded; const fresh = ledgerMeasurements.filter((payload) => payload.automatic_recall); expect(fresh).toHaveLength(3); expect(fresh[1].automatic_recall).toMatchObject({ candidateItems: 1, duplicatesOmitted: 1, afterTokens: 0, packMetadata: "valid" }); expect(fresh[0].evaluation_key).not.toBe(fresh[1].evaluation_key); expect(fresh[1].evaluation_key).not.toBe(fresh[2].evaluation_key); expect(JSON.stringify(fresh.map((payload) => payload.automatic_recall))).not.toMatch(/private|session|fingerprint|path/); }); test.each(["neither", "session", "message", "both"])("keeps turn evaluations distinct with %s host IDs", async (hostIds) => { process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => { if (String(url).endsWith("/api/prompt-pack-profile")) return viewerProfileResponse(); if (String(url).endsWith("/api/pack")) return jsonResponse(200, { pack_text: "", metrics: { total_items: 0, pack_tokens: 0 } }); return jsonResponse(200, { ok: true }); }); const { CodememPlugin } = await import("../plugins/codemem.js"); const init = { project: { name: "fixture" }, client: { app: { log: vi.fn() }, tui: {} }, directory: "/tmp/fixture", worktree: "/tmp/fixture" }; let hooks = await CodememPlugin(init); const entry = (id) => ({ info: { role: "user", ...(hostIds === "message" || hostIds === "both" ? { id } : {}), ...(hostIds === "session" || hostIds === "both" ? { sessionID: "session" } : {}), }, parts: [{ type: "text", text: "same prompt" }], }); const output = { messages: [entry("first")] }; await hooks["experimental.chat.messages.transform"]({}, output); await hooks["experimental.chat.messages.transform"]({}, structuredClone(output)); output.messages.push(entry("second")); await hooks["experimental.chat.messages.transform"]({}, output); await hooks["experimental.chat.messages.transform"]({}, structuredClone(output)); await hooks.event({ event: { type: "session.created", properties: {} } }); await hooks["experimental.chat.messages.transform"]({}, { messages: [entry("first")] }); await hooks["experimental.chat.messages.transform"]({}, { messages: [entry("first")] }); hooks.dispose(); hooks = await CodememPlugin(init); await hooks["experimental.chat.messages.transform"]({}, { messages: [entry("first")] }); const measurements = fetchPostCalls(fetchMock) .filter(([url]) => String(url).endsWith("/api/prompt-pack-ledger")) .map(([, options]) => JSON.parse(options.body)) .filter((payload) => payload.automatic_recall); expect(measurements).toHaveLength(7); expect(measurements[0].evaluation_key).toBe(measurements[1].evaluation_key); expect(measurements[2].evaluation_key).toBe(measurements[3].evaluation_key); expect(measurements[0].evaluation_key).not.toBe(measurements[2].evaluation_key); expect(measurements[0].attempt_id).not.toBe(measurements[2].attempt_id); expect(measurements[4].evaluation_key).toBe(measurements[5].evaluation_key); if (hostIds === "both") { expect(measurements[4].evaluation_key).toBe(measurements[0].evaluation_key); expect(measurements[6].evaluation_key).toBe(measurements[0].evaluation_key); } else { expect(measurements[4].evaluation_key).not.toBe(measurements[0].evaluation_key); expect(measurements[6].evaluation_key).not.toBe(measurements[0].evaluation_key); } }); test("sends the default reserved token budget through the Viewer transport", async () => { // Arrange process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => { if (String(url).endsWith("/api/prompt-pack-profile")) return viewerProfileResponse(); if (String(url).endsWith("/api/pack")) return jsonResponse(200, packResponse()); return jsonResponse(200, { ok: true }); }); const { CodememPlugin } = await import("../plugins/codemem.js"); const hooks = await CodememPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = messageOutput({ messageId: "user-default-budget" }); // Act await hooks["experimental.chat.messages.transform"]({}, output); await vi.waitFor(() => expect(fetchPostCalls(fetchMock)).toHaveLength(2)); // Assert const packBody = fetchBody(fetchMock, 0); expect(packBody.token_budget).toBe(795); expect(output.messages[0].parts.at(-1).text).toContain("Viewer-backed context"); expect(spawnMock.mock.calls.filter(isPackOrLedgerSpawn)).toEqual([]); }); test("does not authorize the unknown ledger sentinel when host session identity is missing", async () => { process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => { if (String(url).endsWith("/api/prompt-pack-profile")) return viewerProfileResponse(); if (String(url).endsWith("/api/pack")) return jsonResponse(200, packResponse()); return jsonResponse(200, { ok: true }); }); const { CodememPlugin } = await import("../plugins/codemem.js"); const hooks = await CodememPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = messageOutput({ messageId: "user-missing-session", sessionID: null }); await hooks["experimental.chat.messages.transform"]({}, output); await vi.waitFor(() => expect(fetchPostCalls(fetchMock)).toHaveLength(2)); const packBody = fetchBody(fetchMock, 0); expect(packBody).toHaveProperty("automatic_context", null); expect(packBody.attempt).not.toHaveProperty("source_session_id"); }); test("does not infer a missing requester from the most recent active session event", async () => { process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => { if (String(url).endsWith("/api/prompt-pack-profile")) return viewerProfileResponse(); if (String(url).endsWith("/api/pack")) return jsonResponse(200, packResponse()); return jsonResponse(200, { ok: true }); }); const { CodememPlugin } = await import("../plugins/codemem.js"); const hooks = await CodememPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); // An overlapping session's event stream must not become another transform's requester. await hooks.event({ event: { type: "session.created", properties: { sessionID: "sess-other-active" } }, }); const output = messageOutput({ messageId: "user-missing-session-after-event", sessionID: null }); await hooks["experimental.chat.messages.transform"]({}, output); await vi.waitFor(() => expect(fetchPostCalls(fetchMock)).toHaveLength(2)); const packBody = fetchBody(fetchMock, 0); expect(packBody).toHaveProperty("automatic_context", null); expect(packBody.attempt).not.toHaveProperty("source_session_id"); expect(JSON.stringify(packBody)).not.toContain("sess-other-active"); }); test("keeps an explicit reserved token budget equal across Viewer and CLI fallback", async () => { // Arrange process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; process.env.CODEMEM_INJECT_TOKEN_BUDGET = "1200"; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => { if (String(url).endsWith("/api/prompt-pack-profile")) return viewerProfileResponse(); if (String(url).endsWith("/api/pack")) { return jsonResponse(503, { error: "unavailable" }); } return jsonResponse(200, { ok: true }); }); const packArgs = []; spawnMock.mockImplementation((_command, args) => { if (Array.isArray(args) && args.includes("pack")) { packArgs.push(args); return makeProcess({ stdout: JSON.stringify(packResponse("## Summary\n[2] CLI budget fallback")), }); } return makeProcess({ stdout: "" }); }); const { CodememPlugin } = await import("../plugins/codemem.js"); const hooks = await CodememPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = messageOutput({ messageId: "user-explicit-budget" }); // Act await hooks["experimental.chat.messages.transform"]({}, output); await vi.waitFor(() => expect(packArgs).toHaveLength(1)); // Assert const cliBudgetIndex = packArgs[0].indexOf("--token-budget"); expect(fetchBody(fetchMock, 0).token_budget).toBe(1195); expect(packArgs[0][cliBudgetIndex + 1]).toBe("1195"); expect(output.messages[0].parts.at(-1).text).toContain("CLI budget fallback"); }); test("skips pack transport and logs once when the positive budget cannot fit the prefix", async () => { // Arrange process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; process.env.CODEMEM_INJECT_TOKEN_BUDGET = "1"; const fetchMock = vi .spyOn(globalThis, "fetch") .mockResolvedValue(jsonResponse(200, { ok: true })); const appLog = vi.fn().mockResolvedValue(undefined); const { CodememPlugin } = await import("../plugins/codemem.js"); const hooks = await CodememPlugin({ project: { name: "atlas" }, client: { app: { log: appLog }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = messageOutput({ messageId: "user-tiny-budget", text: "sensitive prompt must not be logged", }); // Act await hooks["experimental.chat.messages.transform"]({}, output); await hooks["experimental.chat.messages.transform"]({}, output); // Assert const budgetWarnings = appLog.mock.calls .map(([entry]) => entry.body) .filter( (entry) => entry?.message === "codemem context injection skipped: token budget is too small", ); expect(budgetWarnings).toEqual([ expect.objectContaining({ level: "warn", extra: { inject_token_budget: 1, required_prefix_tokens: 5, next_action: "increase CODEMEM_INJECT_TOKEN_BUDGET or unset it to use the default", }, }), ]); expect(JSON.stringify(budgetWarnings)).not.toContain("sensitive prompt"); expect(fetchMock).not.toHaveBeenCalled(); expect(spawnMock.mock.calls.filter(isPackOrLedgerSpawn)).toEqual([]); expect(output.messages[0].parts).toHaveLength(1); }); test("does not use an older backend's unsafe generic pack fallback", async () => { const packArgs = []; spawnMock.mockImplementation((_command, args) => { if (Array.isArray(args) && args.includes("pack")) { packArgs.push(args); if (args.includes("--internal-ledger")) { return makeProcess({ stderr: "error: unknown option '--internal-ledger'", exitCode: 1, }); } return makeProcess({ stdout: JSON.stringify({ pack_text: "## Summary\n[1] (feature) Legacy backend context", metrics: { total_items: 1, pack_tokens: 20 }, }), }); } return makeProcess({ stdout: "" }); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = { messages: [ { info: { id: "user-legacy-backend", sessionID: "sess-legacy-backend", role: "user" }, parts: [{ type: "text", text: "legacy backend", messageID: "user-legacy-backend" }], }, ], }; await hooks["experimental.chat.messages.transform"]({}, output); expect(packArgs).toHaveLength(1); expect(packArgs[0]).toContain("--internal-ledger"); expect(output.messages[0].parts).toHaveLength(1); }); test.each([ { transport: "viewer", repair: false }, { transport: "fallback", repair: false }, { transport: "viewer", repair: true }, { transport: "fallback", repair: true }, ])("preserves changed empty-pack evaluation artifacts via $transport (repair=$repair)", async ({ transport, repair }) => { process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; let fingerprint = "a".repeat(64); let packCalls = 0; const receipts = []; const response = () => ({ pack_text: "## Summary\n\n## Observations", metrics: { total_items: 0, pack_tokens: 7 }, ledger_artifact_fingerprint: fingerprint, ledger_outcome: repair && ++packCalls % 2 === 1 ? { ok: false, errorCode: "retrieval_ledger_write_failed", reason: "idempotency_conflict" } : undefined, }); vi.spyOn(globalThis, "fetch").mockImplementation(async (url, options) => { if (String(url).endsWith("/api/prompt-pack-profile")) return viewerProfileResponse(); if (String(url).endsWith("/api/pack")) return transport === "viewer" ? jsonResponse(200, response()) : jsonResponse(503, { error: "unavailable" }); if (String(url).endsWith("/api/prompt-pack-ledger")) receipts.push(JSON.parse(options.body)); return jsonResponse(200, { ok: true }); }); spawnMock.mockImplementation((_command, args) => { const proc = makeProcess({ stdout: args.includes("pack") ? JSON.stringify(response()) : "{}" }); if (args.includes("prompt-pack-ledger")) proc.stdin.write = vi.fn((value) => receipts.push(JSON.parse(String(value)))); return proc; }); const { CodememPlugin } = await import("../plugins/codemem.js"); const hooks = await CodememPlugin({ project: { name: "fixture" }, client: { app: { log: vi.fn() }, tui: {} }, directory: "/tmp/fixture", worktree: "/tmp/fixture" }); const output = messageOutput({ messageId: "empty-artifact", text: "same query" }); await hooks["experimental.chat.messages.transform"]({}, output); await hooks["experimental.chat.messages.transform"]({}, output); fingerprint = "b".repeat(64); await hooks["experimental.chat.messages.transform"]({}, output); expect(receipts).toHaveLength(3); expect(receipts[0].evaluation_key).toBe(receipts[1].evaluation_key); expect(receipts[2].evaluation_key).not.toBe(receipts[1].evaluation_key); for (const receipt of receipts) { expect(receipt).toMatchObject({ action: "recall", automatic_recall: { candidateItems: 0, beforeTokens: 0, afterTokens: 0 } }); } expect(output.messages[0].parts).toHaveLength(1); }); test("keeps CLI repair retries fail-closed when the transform has no session identity", async () => { process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; let packCalls = 0; const packStdin = []; const response = () => ({ pack_text: "## Summary\n\n## Observations", metrics: { total_items: 0, pack_tokens: 7 }, ledger_artifact_fingerprint: "c".repeat(64), // First CLI pack reports a ledger conflict so the plugin rebuilds metadata and retries. ledger_outcome: ++packCalls === 1 ? { ok: false, errorCode: "retrieval_ledger_write_failed", reason: "idempotency_conflict" } : undefined, }); vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => { if (String(url).endsWith("/api/prompt-pack-profile")) return viewerProfileResponse(); if (String(url).endsWith("/api/pack")) return jsonResponse(503, { error: "unavailable" }); return jsonResponse(200, { ok: true }); }); spawnMock.mockImplementation((_command, args) => { const proc = makeProcess({ stdout: args.includes("pack") ? JSON.stringify(response()) : "{}" }); if (args.includes("pack")) proc.stdin.write = vi.fn((value) => packStdin.push(JSON.parse(String(value)))); return proc; }); const { CodememPlugin } = await import("../plugins/codemem.js"); const hooks = await CodememPlugin({ project: { name: "fixture" }, client: { app: { log: vi.fn() }, tui: {} }, directory: "/tmp/fixture", worktree: "/tmp/fixture" }); await hooks.event({ event: { type: "session.created", properties: { sessionID: "sess-other-active" } }, }); const output = messageOutput({ messageId: "repair-missing-session", sessionID: null, text: "repair query" }); await hooks["experimental.chat.messages.transform"]({}, output); expect(packCalls).toBeGreaterThanOrEqual(2); expect(packStdin.length).toBe(packCalls); for (const payload of packStdin) { expect(payload).not.toHaveProperty("source_session_id"); expect(payload).not.toHaveProperty("stream_id"); expect(JSON.stringify(payload)).not.toContain("sess-other-active"); } }); test("suppresses real zero-result packs without advancing delivery", async () => { const tmpDir = mkdtempSync(join(tmpdir(), "codemem-plugin-empty-pack-")); tmpDirs.push(tmpDir); const dbPath = join(tmpDir, "mem.sqlite"); const db = connect(dbPath); initTestSchema(db); db.close(); const store = new MemoryStore(dbPath); process.env.CODEMEM_DB = dbPath; const attempts = []; const deliveryPayloads = []; const packResponses = []; spawnMock.mockImplementation((_command, args) => { const proc = new EventEmitter(); proc.stdout = new EventEmitter(); proc.stderr = new EventEmitter(); let stdinText = ""; proc.stdin = { write: vi.fn((value) => { stdinText += String(value); }), end: vi.fn(), }; queueMicrotask(() => { try { const payload = parseInternalLedgerPayload(stdinText); if (Array.isArray(args) && args.includes("pack")) { attempts.push(payload); const context = args[args.indexOf("pack") + 1]; const artifacts = buildMemoryPackWithTrace(store, context, 10); packResponses.push(artifacts.response); handleInstrumentedPackLedger( store.db, payload, context, undefined, artifacts, ); proc.stdout.emit("data", JSON.stringify(artifacts.response)); } else { deliveryPayloads.push(payload); handlePromptPackLedger(store.db, payload); } proc.emit("exit", 0); } catch (error) { proc.stderr.emit("data", error instanceof Error ? error.message : String(error)); proc.emit("exit", 1); } }); return proc; }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = { messages: [ { info: { id: "user-empty", sessionID: "sess-empty", role: "user" }, parts: [{ type: "text", text: "nothing here", messageID: "user-empty" }], }, ], }; await hooks["experimental.chat.messages.transform"]({}, output); await hooks["experimental.chat.messages.transform"]({}, output); expect(attempts).toHaveLength(2); expect(attempts[1].attempt_id).not.toBe(attempts[0].attempt_id); expect(attempts[1].request_id).not.toBe(attempts[0].request_id); expect(packResponses).toHaveLength(2); for (const response of packResponses) { expect(response.metrics.total_items).toBe(0); expect(response.pack_text).toContain("## Summary"); } expect(output.messages[0].parts).toHaveLength(1); expect(deliveryPayloads.filter((payload) => payload.action === "delivery")).toEqual([]); expect(deliveryPayloads.filter((payload) => payload.action === "recall")).toHaveLength(2); expect(deliveryPayloads[0].evaluation_key).toBe(deliveryPayloads[1].evaluation_key); expect( store.db .prepare( "SELECT COUNT(*) AS n FROM retrieval_attempts WHERE automatic_recall_json IS NOT NULL", ) .get(), ).toEqual({ n: 2 }); expect( automaticRecallHealth(store.db, { actorId: store.actorId, deviceId: store.deviceId }), ).toMatchObject({ freshEvaluations: 1, unmeasuredAttempts: 0 }); for (const payload of attempts) { expect(getRetrievalAttempt(store.db, payload.attempt_id)).toMatchObject({ retrievalStatus: "no_results", deliveryStatus: "not_attempted", candidateCount: 0, selectedCount: 0, exposures: [], }); } store.close(); }); test("allocates fresh IDs when a failed pack transport succeeds on transform retry", async () => { const packPayloads = []; const ledgerPayloads = []; let packCalls = 0; spawnMock.mockImplementation((_command, args) => { if (Array.isArray(args) && args.includes("pack")) { packCalls += 1; const proc = makeProcess( packCalls === 1 ? { stderr: "pack transport failed", exitCode: 1 } : { stdout: JSON.stringify({ pack_text: "## Summary\n[1] (feature) Retry succeeded", metrics: { total_items: 1, pack_tokens: 12 }, }), }, ); proc.stdin.write = vi.fn((value) => packPayloads.push(JSON.parse(String(value)))); return proc; } const proc = makeProcess({ stdout: "" }); proc.stdin.write = vi.fn((value) => ledgerPayloads.push(JSON.parse(String(value)))); return proc; }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = { messages: [ { info: { id: "user-retry", sessionID: "sess-retry", role: "user" }, parts: [{ type: "text", text: "retry transport", messageID: "user-retry" }], }, ], }; await hooks["experimental.chat.messages.transform"]({}, output); expect(output.messages[0].parts).toHaveLength(1); expect(ledgerPayloads.filter((payload) => payload.action === "delivery")).toHaveLength(0); await hooks["experimental.chat.messages.transform"]({}, output); expect(packCalls).toBe(2); expect(packPayloads).toHaveLength(2); expect(packPayloads[1].attempt_id).not.toBe(packPayloads[0].attempt_id); expect(packPayloads[1].request_id).not.toBe(packPayloads[0].request_id); expect(ledgerPayloads.filter((payload) => payload.action === "record")).toHaveLength(1); expect(ledgerPayloads).toContainEqual( expect.objectContaining({ action: "record", attempt_id: packPayloads[0].attempt_id, request_id: packPayloads[0].request_id, retrieval_status: "failed", failure_stage: "transport", }), ); expect(ledgerPayloads).toContainEqual(expect.objectContaining({ action: "delivery", attempt_id: packPayloads[1].attempt_id, delivery_status: "handed_off", })); expect(output.messages[0].parts.at(-1).text).toContain("Retry succeeded"); }); test("records disabled injection once per session and surface until session deletion", async () => { process.env.CODEMEM_INJECT_CONTEXT = "0"; spawnMock.mockImplementation(() => makeProcess({ stdout: "" })); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = { messages: [ { info: { id: "user-disabled", sessionID: "sess-disabled", role: "user" }, parts: [{ type: "text", text: "disabled", messageID: "user-disabled" }], }, ], }; await hooks["experimental.chat.messages.transform"]({}, output); await hooks["experimental.chat.messages.transform"]({}, output); expect(spawnMock).toHaveBeenCalledTimes(1); await hooks.event({ event: { type: "session.deleted", properties: { info: { id: "sess-disabled" } } }, }); await hooks["experimental.chat.messages.transform"]({}, output); expect(spawnMock).toHaveBeenCalledTimes(2); }); test("skips message injection for the transform immediately following compaction", async () => { const packQueries = []; spawnMock.mockImplementation((_command, args) => { if (Array.isArray(args) && args.includes("pack")) { packQueries.push(args[args.indexOf("pack") + 1]); return makeProcess({ stdout: JSON.stringify({ pack_text: "## Summary\n[1] (feature) Normal turn context", metrics: { total_items: 1, pack_tokens: 42 }, }), }); } return makeProcess({ stdout: "" }); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {}, }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); expect(typeof hooks["experimental.session.compacting"]).toBe("function"); await hooks["experimental.session.compacting"]({ sessionID: "sess-compact" }, { context: [] }); const output = { messages: [ { info: { id: "user-compact", sessionID: "sess-compact", role: "user" }, parts: [ { id: "user-compact-text", sessionID: "sess-compact", messageID: "user-compact", type: "text", text: "summarize this session", }, ], }, ], }; await hooks["experimental.chat.messages.transform"]({ sessionID: "sess-compact" }, output); expect(output.messages[0].parts).toHaveLength(1); expect(spawnMock).toHaveBeenCalledTimes(1); await hooks["experimental.chat.messages.transform"]({ sessionID: "sess-compact" }, output); expect(output.messages[0].parts.at(-1).text).toBe( "[codemem context]\n## Summary\n[1] (feature) Normal turn context", ); expect(packQueries).toEqual(["summarize this session atlas"]); expect(spawnMock).toHaveBeenCalledTimes(3); }); test("keeps legacy system prompt injection when CODEMEM_INJECT_SURFACE=system", async () => { process.env.CODEMEM_INJECT_SURFACE = "system"; spawnMock.mockImplementation((_command, args) => { if (Array.isArray(args) && args.includes("pack")) { return makeProcess({ stdout: JSON.stringify({ pack_text: "## Summary\n[1] (feature) Legacy system injection", metrics: { total_items: 1, pack_tokens: 42 }, }), }); } return makeProcess({ stdout: "" }); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {}, }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = { system: ["base system prompt"] }; await hooks["experimental.chat.system.transform"]( { sessionID: "sess-legacy", model: {} }, output, ); expect(output.system).toEqual([ "base system prompt", "[codemem context]\n## Summary\n[1] (feature) Legacy system injection", ]); }); test("gives changed legacy system rebuilds fresh identity while exact retries stay idempotent", async () => { process.env.CODEMEM_INJECT_SURFACE = "system"; const tmpDir = mkdtempSync(join(tmpdir(), "codemem-plugin-system-rebuild-")); tmpDirs.push(tmpDir); const dbPath = join(tmpDir, "mem.sqlite"); const db = connect(dbPath); initTestSchema(db); db.close(); const store = new MemoryStore(dbPath); const sessionId = insertSession(store.db, { cwd: tmpDir, project: "atlas", }); const firstMemoryId = store.remember( sessionId, "feature", "Legacy rebuild first", "legacy rebuild evidence first", 0.9, ); process.env.CODEMEM_DB = dbPath; const packPayloads = []; spawnMock.mockImplementation((_command, args) => { const proc = new EventEmitter(); proc.stdout = new EventEmitter(); proc.stderr = new EventEmitter(); let stdinText = ""; proc.stdin = { write: vi.fn((value) => { stdinText += String(value); }), end: vi.fn(), }; queueMicrotask(() => { try { const payload = parseInternalLedgerPayload(stdinText); if (Array.isArray(args) && args.includes("pack")) { packPayloads.push(payload); const context = args[args.indexOf("pack") + 1]; const artifacts = buildMemoryPackWithTrace(store, context, 10); handleInstrumentedPackLedger( store.db, payload, context, undefined, artifacts, ); proc.stdout.emit("data", JSON.stringify(artifacts.response)); } else { handlePromptPackLedger(store.db, payload); } proc.emit("exit", 0); } catch (error) { proc.stderr.emit("data", error instanceof Error ? error.message : String(error)); proc.emit("exit", 1); } }); return proc; }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: tmpDir, worktree: tmpDir, }); const transform = hooks["experimental.chat.system.transform"]; const input = { sessionID: "sess-legacy-rebuild", model: {} }; const firstOutput = { system: [] }; await transform(input, firstOutput); const secondMemoryId = store.remember( sessionId, "decision", "Legacy rebuild second", "legacy rebuild evidence second", 0.95, ); const changedOutput = { system: [] }; await transform(input, changedOutput); const exactRetryOutput = { system: [] }; await transform(input, exactRetryOutput); expect(packPayloads).toHaveLength(4); expect(packPayloads[1].attempt_id).toBe(packPayloads[0].attempt_id); expect(packPayloads[1].request_id).toBe(packPayloads[0].request_id); expect(packPayloads[2].attempt_id).not.toBe(packPayloads[0].attempt_id); expect(packPayloads[2].request_id).not.toBe(packPayloads[0].request_id); expect(packPayloads[3].attempt_id).toBe(packPayloads[2].attempt_id); expect(packPayloads[3].request_id).toBe(packPayloads[2].request_id); const firstAttempt = getRetrievalAttempt(store.db, packPayloads[0].attempt_id); const changedAttempt = getRetrievalAttempt(store.db, packPayloads[2].attempt_id); expect( store.db.prepare("SELECT COUNT(*) AS count FROM retrieval_attempts").get(), ).toEqual({ count: 2 }); expect(firstAttempt).toMatchObject({ requestId: packPayloads[0].request_id, deliveryStatus: "handed_off", selectedCount: 1, }); expect(firstAttempt?.exposures.map((exposure) => exposure.memoryId)).toEqual([ firstMemoryId, ]); expect(changedAttempt).toMatchObject({ requestId: packPayloads[2].request_id, deliveryStatus: "handed_off", selectedCount: 2, }); expect(changedAttempt?.exposures.map((exposure) => exposure.memoryId)).toEqual( expect.arrayContaining([firstMemoryId, secondMemoryId]), ); expect( changedAttempt?.exposures.every( (exposure) => exposure.attemptId === packPayloads[2].attempt_id, ), ).toBe(true); expect(firstOutput.system.join("\n")).toContain("Legacy rebuild first"); expect(firstOutput.system.join("\n")).not.toContain("Legacy rebuild second"); expect(changedOutput.system.join("\n")).toContain("Legacy rebuild second"); expect(exactRetryOutput.system).toEqual(changedOutput.system); store.close(); }); test("uses the complete ledger artifact fingerprint for legacy system retry identity", async () => { process.env.CODEMEM_INJECT_SURFACE = "system"; const packPayloads = []; const ledgerPayloads = []; const fingerprints = ["1".repeat(64), "2".repeat(64), "2".repeat(64), "2".repeat(64)]; spawnMock.mockImplementation((_command, args) => { if (Array.isArray(args) && args.includes("pack")) { const fingerprint = fingerprints[packPayloads.length]; const proc = makeProcess({ stdout: JSON.stringify({ pack_text: "## Summary\n[1] (feature) Identical rendered text", metrics: { total_items: 1, pack_tokens: 12 }, ledger_artifact_fingerprint: fingerprint, }), }); proc.stdin.write = vi.fn((value) => packPayloads.push(JSON.parse(String(value)))); return proc; } const proc = makeProcess({ stdout: "" }); proc.stdin.write = vi.fn((value) => ledgerPayloads.push(JSON.parse(String(value)))); return proc; }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const transform = hooks["experimental.chat.system.transform"]; const input = { sessionID: "sess-ledger-fingerprint", model: {} }; const firstOutput = { system: [] }; const changedOutput = { system: [] }; const exactRetryOutput = { system: [] }; await transform(input, firstOutput); await transform(input, changedOutput); await transform(input, exactRetryOutput); expect(packPayloads).toHaveLength(4); expect(packPayloads[1].attempt_id).toBe(packPayloads[0].attempt_id); expect(packPayloads[2].attempt_id).not.toBe(packPayloads[0].attempt_id); expect(packPayloads[2].request_id).not.toBe(packPayloads[0].request_id); expect(packPayloads[3].attempt_id).toBe(packPayloads[2].attempt_id); expect(packPayloads[3].request_id).toBe(packPayloads[2].request_id); expect(changedOutput.system).toEqual(firstOutput.system); expect(exactRetryOutput.system).toEqual(changedOutput.system); expect( ledgerPayloads .filter((payload) => payload.action === "delivery") .map((payload) => payload.attempt_id), ).toEqual([packPayloads[0].attempt_id, packPayloads[2].attempt_id, packPayloads[2].attempt_id]); }); test("keeps unchanged restart retries idempotent before repairing changed artifacts", async () => { const tmpDir = mkdtempSync(join(tmpdir(), "codemem-plugin-restart-conflict-")); tmpDirs.push(tmpDir); const dbPath = join(tmpDir, "mem.sqlite"); const db = connect(dbPath); initTestSchema(db); const sessionId = insertSession(db, { cwd: tmpDir, project: "atlas" }); db.close(); const store = new MemoryStore(dbPath); const firstMemoryId = store.remember( sessionId, "feature", "Restart conflict first", "restart conflict evidence first", 0.9, ); process.env.CODEMEM_DB = dbPath; const packPayloads = []; const deliveryPayloads = []; spawnMock.mockImplementation((_command, args) => { const proc = new EventEmitter(); proc.stdout = new EventEmitter(); proc.stderr = new EventEmitter(); let stdinText = ""; proc.stdin = { write: vi.fn((value) => { stdinText += String(value); }), end: vi.fn(), }; queueMicrotask(() => { try { const payload = parseInternalLedgerPayload(stdinText); if (Array.isArray(args) && args.includes("pack")) { packPayloads.push(payload); const context = args[args.indexOf("pack") + 1]; const artifacts = buildMemoryPackWithTrace(store, context, 10); const ledgerOutcome = handleInstrumentedPackLedger( store.db, payload, context, undefined, artifacts, ); proc.stdout.emit( "data", JSON.stringify({ ...artifacts.response, ...(ledgerOutcome.ok ? {} : { ledger_outcome: ledgerOutcome }), }), ); } else { deliveryPayloads.push(payload); handlePromptPackLedger(store.db, payload); } proc.emit("exit", 0); } catch (error) { proc.stderr.emit("data", error instanceof Error ? error.message : String(error)); proc.emit("exit", 1); } }); return proc; }); const buildPlugin = async () => { Reflect.deleteProperty(globalThis, pluginRegistrationsKey); vi.resetModules(); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); return OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: tmpDir, worktree: tmpDir, }); }; const messageOutput = () => ({ messages: [ { info: { id: "user-restart", sessionID: "sess-restart", role: "user" }, parts: [{ type: "text", text: "restart conflict", messageID: "user-restart" }], }, ], }); const firstHooks = await buildPlugin(); const firstOutput = messageOutput(); await firstHooks["experimental.chat.messages.transform"]({}, firstOutput); vi.advanceTimersByTime(100); const unchangedRestartHooks = await buildPlugin(); const unchangedRestartOutput = messageOutput(); await unchangedRestartHooks["experimental.chat.messages.transform"]( {}, unchangedRestartOutput, ); expect(packPayloads).toHaveLength(2); expect(packPayloads[1].attempt_id).toBe(packPayloads[0].attempt_id); expect(packPayloads[1].request_id).toBe(packPayloads[0].request_id); expect(packPayloads[1].started_at).not.toBe(packPayloads[0].started_at); expect(store.db.prepare("SELECT COUNT(*) AS count FROM retrieval_attempts").get()).toEqual({ count: 1, }); expect(unchangedRestartOutput.messages[0].parts.at(-1).text).toContain( "Restart conflict first", ); const secondMemoryId = store.remember( sessionId, "decision", "Restart conflict second", "restart conflict evidence second", 0.95, ); const changedRestartHooks = await buildPlugin(); const changedRestartOutput = messageOutput(); await changedRestartHooks["experimental.chat.messages.transform"]({}, changedRestartOutput); expect(packPayloads).toHaveLength(4); expect(packPayloads[1].attempt_id).toBe(packPayloads[0].attempt_id); expect(packPayloads[2].attempt_id).toBe(packPayloads[0].attempt_id); expect(packPayloads[3].attempt_id).not.toBe(packPayloads[0].attempt_id); expect( deliveryPayloads .filter((payload) => payload.action === "delivery") .map((payload) => payload.attempt_id), ).toEqual([ packPayloads[0].attempt_id, packPayloads[0].attempt_id, packPayloads[3].attempt_id, ]); expect(changedRestartOutput.messages[0].parts.at(-1).text).toContain( "Restart conflict second", ); expect(getRetrievalAttempt(store.db, packPayloads[0].attempt_id)).toMatchObject({ deliveryStatus: "handed_off", selectedCount: 1, }); expect(getRetrievalAttempt(store.db, packPayloads[3].attempt_id)).toMatchObject({ deliveryStatus: "handed_off", selectedCount: 2, }); expect( getRetrievalAttempt(store.db, packPayloads[3].attempt_id)?.exposures.map((row) => row.memoryId), ).toEqual(expect.arrayContaining([firstMemoryId, secondMemoryId])); store.close(); }); test.each([ [ "timeout", { stderr: "repair timed out", exitCode: null }, "Restart conflict fallback", "pack_command_failed", false, ], [ "nonzero transport failure", { stderr: "repair transport failed", exitCode: 7 }, "Restart conflict fallback", "pack_command_failed", false, ], [ "malformed success", { stdout: "not-json", exitCode: 0 }, "Restart conflict fallback", "pack_identity_repair_failed", false, ], [ "repeated conflict", { stdout: JSON.stringify({ pack_text: "## Summary\n[2] (decision) Conflicting repair", metrics: { total_items: 1, pack_tokens: 12 }, ledger_outcome: { ok: false, errorCode: "retrieval_ledger_write_failed", reason: "idempotency_conflict", }, }), }, "Restart conflict fallback", null, false, ], [ "successful replacement", { stdout: JSON.stringify({ pack_text: "## Summary\n[2] (decision) Fresh replacement", metrics: { total_items: 1, pack_tokens: 12 }, }), }, "Fresh replacement", null, true, ], [ "successful zero results", { stdout: JSON.stringify({ pack_text: "## Summary", metrics: { total_items: 0, pack_tokens: 0 }, }), }, null, null, false, ], ])( "handles restarted-plugin conflict repair with %s", async (_label, repairResult, expectedText, expectedFailureCode, expectsFreshDelivery) => { const packPayloads = []; const ledgerPayloads = []; let packCalls = 0; spawnMock.mockImplementation((_command, args) => { if (Array.isArray(args) && args.includes("pack")) { packCalls += 1; const response = packCalls === 1 ? { stdout: JSON.stringify({ pack_text: "## Summary\n[1] (feature) Restart conflict fallback", metrics: { total_items: 1, pack_tokens: 10 }, ledger_outcome: { ok: false, errorCode: "retrieval_ledger_write_failed", reason: "idempotency_conflict", }, }), } : repairResult; const proc = makeProcess(response); proc.stdin.write = vi.fn((value) => packPayloads.push(JSON.parse(String(value)))); return proc; } const proc = makeProcess({ stdout: "" }); proc.stdin.write = vi.fn((value) => ledgerPayloads.push(JSON.parse(String(value)))); return proc; }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const transform = hooks["experimental.chat.messages.transform"]; const messageOutput = () => ({ messages: [ { info: { id: "restart-fallback", sessionID: "sess-restart-fallback", role: "user" }, parts: [{ type: "text", text: "restart fallback", messageID: "restart-fallback" }], }, ], }); const output = messageOutput(); await transform({}, output); expect(packPayloads).toHaveLength(2); expect(packPayloads[1].attempt_id).not.toBe(packPayloads[0].attempt_id); expect(packPayloads[1].request_id).not.toBe(packPayloads[0].request_id); const injectedText = output.messages[0].parts.find( (part) => String(part.id || "").startsWith("codemem-context-"), )?.text; if (expectedText) { expect(injectedText).toContain(expectedText); } else { expect(injectedText).toBeUndefined(); } const deliveries = ledgerPayloads.filter((payload) => payload.action === "delivery"); if (expectsFreshDelivery) { expect(deliveries).toMatchObject([ { action: "delivery", attempt_id: packPayloads[1].attempt_id, delivery_status: "handed_off", }, ]); } else { expect(deliveries).toEqual([]); } if (expectedFailureCode) { expect(ledgerPayloads).toContainEqual( expect.objectContaining({ action: "record", attempt_id: packPayloads[1].attempt_id, retrieval_status: "failed", failure_code: expectedFailureCode, }), ); } if (expectedText === "Restart conflict fallback") { const replayOutput = messageOutput(); await transform({}, replayOutput); expect(packPayloads).toHaveLength(2); expect( replayOutput.messages[0].parts.find( (part) => String(part.id || "").startsWith("codemem-context-"), )?.text, ).toContain(expectedText); expect(ledgerPayloads.filter((payload) => payload.action === "delivery")).toEqual([]); } }, ); test.each([ [ "timeout", { stderr: "timeout", exitCode: null }, "command_failed", "pack_command_failed", ], [ "nonzero exit", { stderr: "repair transport failed", exitCode: 7 }, "command_failed", "pack_command_failed", ], [ "malformed success", { stdout: "not-json", exitCode: 0 }, "malformed_success", "pack_identity_repair_failed", ], ])( "injects the first usable changed artifact when fresh-identity repair returns %s", async (_label, repairResult, expectedReason, expectedFailureCode) => { process.env.CODEMEM_INJECT_SURFACE = "system"; const packPayloads = []; const ledgerPayloads = []; const appLog = vi.fn().mockResolvedValue(undefined); let packCalls = 0; spawnMock.mockImplementation((_command, args) => { if (Array.isArray(args) && args.includes("pack")) { packCalls += 1; const response = packCalls === 1 ? { stdout: JSON.stringify({ pack_text: "## Summary\n[1] (feature) Original artifact", metrics: { total_items: 1, pack_tokens: 10 }, ledger_artifact_fingerprint: "1".repeat(64), }), } : packCalls === 2 ? { stdout: JSON.stringify({ pack_text: "## Summary\n[2] (decision) Changed artifact fallback", metrics: { total_items: 1, pack_tokens: 11 }, ledger_artifact_fingerprint: "2".repeat(64), }), } : repairResult; const proc = makeProcess(response); proc.stdin.write = vi.fn((value) => packPayloads.push(JSON.parse(String(value)))); return proc; } const proc = makeProcess({ stdout: "" }); proc.stdin.write = vi.fn((value) => ledgerPayloads.push(JSON.parse(String(value)))); return proc; }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: appLog }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const transform = hooks["experimental.chat.system.transform"]; const input = { sessionID: `sess-repair-${expectedReason}`, model: {} }; const firstOutput = { system: [] }; const changedOutput = { system: [] }; await transform(input, firstOutput); await transform(input, changedOutput); expect(packPayloads).toHaveLength(3); expect(packPayloads[1].attempt_id).toBe(packPayloads[0].attempt_id); expect(packPayloads[2].attempt_id).not.toBe(packPayloads[0].attempt_id); expect(packPayloads[2].request_id).not.toBe(packPayloads[0].request_id); expect(firstOutput.system.join("\n")).toContain("Original artifact"); expect(changedOutput.system).toEqual([ "[codemem context]\n## Summary\n[2] (decision) Changed artifact fallback", ]); expect( ledgerPayloads.filter((payload) => payload.action === "delivery"), ).toEqual([ { action: "delivery", attempt_id: packPayloads[0].attempt_id, delivery_status: "handed_off", }, ]); expect(ledgerPayloads).toContainEqual( expect.objectContaining({ action: "record", attempt_id: packPayloads[2].attempt_id, request_id: packPayloads[2].request_id, retrieval_status: "failed", failure_code: expectedFailureCode, }), ); expect(appLog).toHaveBeenCalledWith({ body: expect.objectContaining({ level: "warn", message: "codemem prompt-pack identity repair failed", extra: expect.objectContaining({ reason: expectedReason }), }), }); }, ); test("skips legacy system injection for the transform immediately following compaction", async () => { process.env.CODEMEM_INJECT_SURFACE = "system"; spawnMock.mockImplementation((_command, args) => { if (Array.isArray(args) && args.includes("pack")) { return makeProcess({ stdout: JSON.stringify({ pack_text: "## Summary\n[1] (feature) Legacy context after compaction", metrics: { total_items: 1, pack_tokens: 42 }, }), }); } return makeProcess({ stdout: "" }); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {}, }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); await hooks["experimental.session.compacting"]({ sessionID: "sess-legacy-compact" }, { context: [] }); const output = { system: ["base system prompt"] }; await hooks["experimental.chat.system.transform"]( { sessionID: "sess-legacy-compact", model: {} }, output, ); expect(output.system).toEqual(["base system prompt"]); expect(spawnMock).toHaveBeenCalledTimes(1); await hooks["experimental.chat.system.transform"]( { sessionID: "sess-legacy-compact", model: {} }, output, ); expect(output.system).toEqual([ "base system prompt", "[codemem context]\n## Summary\n[1] (feature) Legacy context after compaction", ]); expect(spawnMock).toHaveBeenCalledTimes(3); }); test("does not inject into system prompt in default message mode", async () => { spawnMock.mockImplementation((_command, args) => { if (Array.isArray(args) && args.includes("pack")) { return makeProcess({ stdout: JSON.stringify({ pack_text: "## Summary\n[1] (feature) Should not be used", metrics: { total_items: 1, pack_tokens: 42 }, }), }); } return makeProcess({ stdout: "" }); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {}, }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = { system: ["base system prompt"] }; await hooks["experimental.chat.system.transform"]( { sessionID: "sess-default-system", model: {} }, output, ); expect(output.system).toEqual(["base system prompt"]); expect(spawnMock).not.toHaveBeenCalled(); }); test("does not inject into messages in legacy system mode", async () => { process.env.CODEMEM_INJECT_SURFACE = "system"; spawnMock.mockImplementation((_command, args) => { if (Array.isArray(args) && args.includes("pack")) { return makeProcess({ stdout: JSON.stringify({ pack_text: "## Summary\n[1] (feature) Should not be used", metrics: { total_items: 1, pack_tokens: 42 }, }), }); } return makeProcess({ stdout: "" }); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {}, }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = { messages: [ { info: { id: "user-legacy", sessionID: "sess-legacy", role: "user" }, parts: [ { id: "user-legacy-text", sessionID: "sess-legacy", messageID: "user-legacy", type: "text", text: "legacy mode prompt", }, ], }, ], }; await hooks["experimental.chat.messages.transform"]({}, output); expect(output.messages[0].parts).toHaveLength(1); expect(output.messages[0].parts[0].text).toBe("legacy mode prompt"); expect(spawnMock).not.toHaveBeenCalled(); }); test("honors empty prompt overrides instead of falling back to stale captured prompts", async () => { process.env.CODEMEM_RAW_EVENTS = "0"; const packQueries = []; spawnMock.mockImplementation((_command, args) => { if (Array.isArray(args) && args.includes("pack")) { packQueries.push(args[args.indexOf("pack") + 1]); return makeProcess({ stdout: JSON.stringify({ pack_text: "## Summary\n[1] (feature) Empty prompt override respected", metrics: { total_items: 1, pack_tokens: 42 }, }), }); } return makeProcess({ stdout: "" }); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {}, }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); await hooks.event({ event: { type: "message.updated", properties: { info: { id: "user-stale", sessionID: "sess-empty-override", role: "user" }, }, }, }); await hooks.event({ event: { type: "message.part.updated", properties: { part: { id: "part-stale", sessionID: "sess-empty-override", messageID: "user-stale", type: "text", text: "stale captured prompt", }, }, }, }); const output = { messages: [ { info: { id: "user-empty", sessionID: "sess-empty-override", role: "user" }, parts: [ { id: "user-empty-text", sessionID: "sess-empty-override", messageID: "user-empty", type: "text", text: " ", }, ], }, ], }; await hooks["experimental.chat.messages.transform"]({ sessionID: "sess-empty-override" }, output); expect(packQueries).toEqual(["atlas"]); expect(output.messages[0].parts.at(-1).text).toBe( "[codemem context]\n## Summary\n[1] (feature) Empty prompt override respected", ); }); test("captures OpenCode 1.18.29 message and failed-tool event shapes", async () => { process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "1"; const postedBodies = []; vi.spyOn(globalThis, "fetch").mockImplementation(async (url, init) => { if (String(url).includes("/api/raw-events/status")) { return jsonResponse(200, { ingest: { available: true } }); } if (String(url).endsWith("/api/raw-events") && init?.method === "POST") { postedBodies.push(JSON.parse(String(init.body))); return jsonResponse(200, { ok: true }); } return jsonResponse(200, {}); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); await hooks.event({ event: { type: "message.part.updated", properties: { part: { id: "assistant-text-current", sessionID: "sess-current-sdk", messageID: "assistant-current", type: "text", text: "Current SDK assistant response", }, }, }, }); await hooks.event({ event: { type: "message.updated", properties: { info: { id: "assistant-current", sessionID: "sess-current-sdk", role: "assistant", time: { created: 1, completed: 2 }, parentID: "user-current", modelID: "model-current", providerID: "provider-current", mode: "chat", path: { cwd: "/tmp/atlas", root: "/tmp/atlas" }, cost: 0, tokens: { input: 10, output: 5, reasoning: 2, cache: { read: 3, write: 4 }, }, }, }, }, }); await hooks.event({ event: { type: "message.updated", properties: { info: { id: "assistant-legacy", sessionID: "sess-current-sdk", role: "assistant", finish: "stop", usage: { input_tokens: 7, output_tokens: 6, cache_creation_input_tokens: 5, cache_read_input_tokens: 4, }, }, }, }, }); const failedToolEvent = { type: "message.part.updated", properties: { part: { id: "tool-current", sessionID: "sess-current-sdk", messageID: "assistant-current", type: "tool", callID: "call-current", tool: "read", state: { status: "error", input: { filePath: "src/current.ts" }, error: "permission denied", time: { start: 1, end: 2 }, }, }, }, }; await hooks.event({ event: failedToolEvent }); await hooks.event({ event: failedToolEvent }); await hooks["tool.execute.after"]( { tool: "write", sessionID: "sess-current-sdk", callID: "call-completed", args: { filePath: "src/completed.ts" }, }, { title: "Write src/completed.ts", output: "saved", metadata: {} }, ); await vi.waitFor(() => expect(postedBodies).toHaveLength(5)); const assistantEnvelope = postedBodies.find((entry) => entry.event_type === "assistant_message"); expect(assistantEnvelope).toMatchObject({ session_id: "sess-current-sdk", payload: { assistant_text: "Current SDK assistant response" }, }); const usageEnvelope = postedBodies.find( (entry) => entry.event_type === "assistant_usage" && entry.payload.message_id === "assistant-current", ); expect(usageEnvelope).toMatchObject({ session_id: "sess-current-sdk", payload: { usage: { input_tokens: 10, output_tokens: 5, cache_creation_input_tokens: 4, cache_read_input_tokens: 3, }, }, }); const legacyUsageEnvelope = postedBodies.find( (entry) => entry.event_type === "assistant_usage" && entry.payload.message_id === "assistant-legacy", ); expect(legacyUsageEnvelope.payload.usage).toEqual({ input_tokens: 7, output_tokens: 6, cache_creation_input_tokens: 5, cache_read_input_tokens: 4, }); const failedToolEnvelope = postedBodies.find( (entry) => entry.event_type === "tool.execute.after" && entry.payload.tool === "read", ); expect(failedToolEnvelope).toMatchObject({ session_id: "sess-current-sdk", payload: { tool: "read", args: { filePath: "src/current.ts" }, result: null, }, }); expect(failedToolEnvelope.payload.error).toContain("permission denied"); const completedToolEnvelope = postedBodies.find( (entry) => entry.event_type === "tool.execute.after" && entry.payload.tool === "write", ); expect(completedToolEnvelope).toMatchObject({ session_id: "sess-current-sdk", payload: { args: { filePath: "src/completed.ts" }, error: null, }, }); expect(completedToolEnvelope.payload.result).toContain("saved"); }); test("tolerates null arguments from a successful tool hook", async () => { process.env.CODEMEM_RAW_EVENTS = "0"; const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); await expect(hooks["tool.execute.after"]( { tool: "custom", sessionID: "sess-null-args", callID: "call-null", args: null }, { title: "Custom", output: "done", metadata: {} }, )).resolves.toBeUndefined(); }); test("passes only normalized repository paths to pack retrieval and ledger recording", async () => { const packArgs = []; spawnMock.mockImplementation((_command, args) => { if (Array.isArray(args) && args.includes("pack")) { packArgs.push(args); return makeProcess({ stdout: JSON.stringify({ pack_text: "## Summary\n[1] (feature) Normalized working set", metrics: { total_items: 1, pack_tokens: 12 }, }), }); } return makeProcess({ stdout: "" }); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); for (const filePath of [ "/tmp/atlas/src/inside.ts", "src/relative.ts", "/tmp/atlas-private/prefix-secret.ts", "/tmp/outside-secret.ts", "../traversal-secret.ts", "x".repeat(401), ]) { await hooks["tool.execute.after"]( { tool: "write", args: { filePath }, sessionID: "sess-paths" }, {}, ); } const output = { messages: [ { info: { id: "user-paths", sessionID: "sess-paths", role: "user" }, parts: [{ type: "text", text: "normalize paths", messageID: "user-paths" }], }, ], }; await hooks["experimental.chat.messages.transform"]({}, output); const command = packArgs[0]; const workingSetFiles = command.flatMap((arg, index) => arg === "--working-set-file" ? [command[index + 1]] : [], ); expect(workingSetFiles).toEqual(["src/inside.ts", "src/relative.ts"]); expect(command.join(" ")).not.toContain("secret"); expect(command.join(" ")).not.toContain("/tmp/atlas"); }); test.each([ { name: "injects the CLI-scoped pack without unauthorized scope memories", withProof: true }, { name: "denies CLI-scoped injection for raw membership without proof", withProof: false }, ])("$name", async ({ withProof }) => { // Arrange: only a refreshed proof can authorize scope-a; raw scope-b stays denied. const tmpDir = mkdtempSync(join(tmpdir(), "codemem-plugin-scope-")); tmpDirs.push(tmpDir); const worktree = join(tmpDir, "atlas"); mkdirSync(worktree); const dbPath = join(tmpDir, "mem.sqlite"); const deviceId = "plugin-scope-device"; const keysDir = join(tmpDir, "keys"); process.env.CODEMEM_KEYS_DIR = keysDir; const db = connect(dbPath); initTestSchema(db); const sessionId = insertSession(db, { cwd: worktree, project: "atlas" }); process.env.CODEMEM_SYNC_KEY_STORE = "file"; if (withProof) { await grantScopeToDevice(db, "scope-a", deviceId, keysDir); } else { insertScopeMembership(db, "scope-a", deviceId); ensureDeviceIdentity(db, { deviceId, keysDir }); } // Add scope-b after refresh so even its active membership has no retained proof. insertScopeMembership(db, "scope-b", deviceId); insertScopedMemory(db, { sessionId, scopeId: "scope-a", title: "Atlas authorized scope note", bodyText: "atlas scope safety can use the authorized deployment note", }); insertScopedMemory(db, { sessionId, scopeId: "scope-b", title: "Atlas forbidden payroll secret", bodyText: "atlas scope safety must not inject forbidden payroll details", }); db.close(); process.env.CODEMEM_DB = dbPath; process.env.CODEMEM_DEVICE_ID = deviceId; process.env.CODEMEM_RUNNER = "codemem-test-runner"; process.env.CODEMEM_EMBEDDING_DISABLED = "1"; process.env.CODEMEM_EMBEDDING_OFFLINE = "1"; const showToast = vi.fn().mockResolvedValue(undefined); spawnMock.mockImplementation((_command, args, options) => { if (Array.isArray(args) && args.includes("pack")) { return makeProcessFromPackCommand(args, options); } return makeProcess({ stdout: "" }); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: { showToast }, }, directory: worktree, worktree, }); const output = { messages: [ { info: { id: "user-scope", sessionID: "sess-scope-a", role: "user" }, parts: [ { id: "user-scope-text", sessionID: "sess-scope-a", messageID: "user-scope", type: "text", text: "atlas scope safety", }, ], }, ], }; // Act await hooks["experimental.chat.messages.transform"]({}, output); // Assert const userPrompt = output.messages[0].parts.map((part) => part.text || "").join("\n"); if (withProof) { expect(userPrompt).toContain("Atlas authorized scope note"); } else { expect(userPrompt).toBe("atlas scope safety"); } expect(userPrompt).not.toContain("Atlas forbidden payroll secret"); expect(userPrompt).not.toContain("forbidden payroll details"); expect(showToast).toHaveBeenCalledTimes(withProof ? 1 : 0); expect(JSON.stringify(showToast.mock.calls)).not.toContain("forbidden payroll"); }); test.each(["::1", "[::1]"])("uses bracketed IPv6 viewer URLs for %s", async (viewerHost) => { // Arrange process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; process.env.CODEMEM_VIEWER_HOST = viewerHost; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => String(url).endsWith("/api/prompt-pack-profile") ? viewerProfileResponse() : String(url).endsWith("/api/pack") ? jsonResponse(200, packResponse()) : jsonResponse(200, { ok: true }), ); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = messageOutput(); // Act await hooks["experimental.chat.messages.transform"]({}, output); await vi.waitFor(() => expect(fetchPostCalls(fetchMock)).toHaveLength(2)); // Assert expect(String(fetchMock.mock.calls[0][0])).toBe( "http://[::1]:38888/api/prompt-pack-profile", ); expect(fetchPostCalls(fetchMock).map(([url, options]) => [String(url), options.method])).toEqual([ ["http://[::1]:38888/api/pack", "POST"], ["http://[::1]:38888/api/prompt-pack-ledger", "POST"], ]); expect(fetchBody(fetchMock, 1)).toMatchObject({ action: "delivery", attempt_id: fetchBody(fetchMock, 0).attempt.attempt_id, delivery_status: "handed_off", }); const defaultDbPath = join(homedir(), ".codemem", "mem.sqlite"); expect(fetchBody(fetchMock, 0).db_path).toBe(defaultDbPath); expect(fetchBody(fetchMock, 1).db_path).toBe(defaultDbPath); expect(output.messages[0].parts.at(-1).text).toBe( "[codemem context]\n## Summary\n[1] (feature) Viewer-backed context", ); expect(spawnMock.mock.calls.filter(isPackOrLedgerSpawn)).toEqual([]); }); test.each([ ["old client/new Viewer", { protocol_version: 2, min_supported_protocol_version: 1 }], ["new client/old Viewer", { protocol_version: 1, min_supported_protocol_version: undefined }], ])("uses Viewer HTTP for the compatible %s profile matrix", async (_label, profileRange) => { process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => String(url).endsWith("/api/prompt-pack-profile") ? viewerProfileResponse(profileRange) : String(url).endsWith("/api/pack") ? jsonResponse(200, packResponse("## Summary\n[1] (decision) Range-compatible bytes")) : jsonResponse(200, { ok: true }), ); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = messageOutput({ messageId: "user-range", sessionID: "sess-range" }); await hooks["experimental.chat.messages.transform"]({}, output); await vi.waitFor(() => expect(fetchPostCalls(fetchMock)).toHaveLength(2)); expect(output.messages[0].parts.at(-1).text).toBe( "[codemem context]\n## Summary\n[1] (decision) Range-compatible bytes", ); expect(spawnMock.mock.calls.filter(isPackOrLedgerSpawn)).toEqual([]); }); test("records a skipped injection through the healthy viewer without spawning ledger CLI", async () => { // Arrange process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; process.env.CODEMEM_INJECT_CONTEXT = "0"; process.env.CODEMEM_DB = "~/atlas.sqlite"; process.env.CODEMEM_PACK_COMPRESSION = "off"; process.env.CODEMEM_EMBEDDING_DISABLED = "true"; process.env.CODEMEM_EMBEDDING_MODEL = "test-embedding-model"; process.env.CODEMEM_EMBEDDING_REVISION = "test-embedding-revision"; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => String(url).endsWith("/api/prompt-pack-profile") ? viewerProfileResponse() : jsonResponse(200, { ok: true }), ); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = messageOutput({ messageId: "user-skipped", sessionID: "sess-skipped" }); // Act await hooks["experimental.chat.messages.transform"]({}, output); await vi.waitFor(() => expect(fetchPostCalls(fetchMock)).toHaveLength(1)); // Assert expect(fetchPostCalls(fetchMock)[0][0]).toBe( "http://127.0.0.1:38888/api/prompt-pack-ledger", ); expect(fetchBody(fetchMock, 0)).toMatchObject({ action: "record", db_path: join(process.env.HOME?.trim() || homedir(), "atlas.sqlite"), identity_target: { pack_compression: "off", embedding_disabled: true, embedding_model: "test-embedding-model", embedding_revision: "test-embedding-revision", }, retrieval_status: "skipped", failure_code: "injection_disabled", }); expect(output.messages[0].parts).toHaveLength(1); expect(spawnMock).not.toHaveBeenCalled(); }); test("records cache reuse and delivery through the healthy viewer without spawning ledger CLI", async () => { // Arrange process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; process.env.CODEMEM_DB = "relative.sqlite"; process.env.CODEMEM_CONFIG = "config/codemem.json"; process.env.CODEMEM_RUNTIME_ROOT = "runtime"; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => String(url).endsWith("/api/prompt-pack-profile") ? viewerProfileResponse() : String(url).endsWith("/api/pack") ? jsonResponse(200, packResponse("## Summary\n[1] (decision) Cache-stable bytes")) : jsonResponse(200, { ok: true }), ); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const firstOutput = messageOutput({ messageId: "user-cache", sessionID: "sess-cache" }); await hooks["experimental.chat.messages.transform"]({}, firstOutput); await vi.waitFor(() => expect(fetchPostCalls(fetchMock).some(([url]) => String(url).endsWith("/api/pack"))).toBe(true), ); const packCall = fetchPostCalls(fetchMock).find(([url]) => String(url).endsWith("/api/pack")); const originalAttemptId = JSON.parse(packCall[1].body).attempt.attempt_id; const cachedOutput = messageOutput({ messageId: "user-cache", sessionID: "sess-cache" }); // Act await hooks["experimental.chat.messages.transform"]({}, cachedOutput); await vi.waitFor(() => { const bodies = fetchPostCalls(fetchMock) .filter(([url]) => String(url).endsWith("/api/prompt-pack-ledger")) .map(([, options]) => JSON.parse(options.body)); const reuse = bodies.find((body) => body.action === "cache_reuse"); expect(reuse?.original_attempt_id).toBe(originalAttemptId); expect(bodies.some((body) => body.action === "delivery" && body.attempt_id === reuse?.attempt_id)).toBe(true); }); // Assert const ledgerBodies = fetchPostCalls(fetchMock) .filter(([url]) => String(url).endsWith("/api/prompt-pack-ledger")) .map(([, options]) => JSON.parse(options.body)); const cacheReuse = ledgerBodies.find((body) => body.action === "cache_reuse"); const cacheDelivery = ledgerBodies.find((body) => body.action === "delivery" && body.attempt_id === cacheReuse.attempt_id); expect(cacheReuse).toMatchObject({ action: "cache_reuse", original_attempt_id: originalAttemptId, }); expect(cacheReuse.attempt_id).not.toBe(originalAttemptId); expect(cacheDelivery).toEqual({ action: "delivery", attempt_id: cacheReuse.attempt_id, db_path: "/tmp/atlas/relative.sqlite", delivery_status: "handed_off", identity_target: { device_id: null, actor_id_present: false, actor_id: null, config_path: "/tmp/atlas/config/codemem.json", runtime_root: "/tmp/atlas/runtime", workspace_id: null, home_dir: resolve(process.env.HOME?.trim() || homedir()), pack_compression: null, embedding_disabled: false, embedding_offline: false, embedding_model: "Xenova/bge-small-en-v1.5", embedding_revision: "ea104dacec62c0de699686887e3f920caeb4f3e3", }, }); expect(cachedOutput.messages[0].parts.at(-1).text).toBe( "[codemem context]\n## Summary\n[1] (decision) Cache-stable bytes", ); expect(spawnMock.mock.calls.filter(isPackOrLedgerSpawn)).toEqual([]); }); test("repairs a viewer pack idempotency conflict over HTTP with fresh identity and no CLI", async () => { // Arrange process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; let packCalls = 0; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => { if (String(url).endsWith("/api/prompt-pack-profile")) return viewerProfileResponse(); if (String(url).endsWith("/api/pack")) { packCalls += 1; return jsonResponse(200, packCalls === 1 ? { ...packResponse("## Summary\n[1] (feature) Stale conflict bytes"), ledger_outcome: { ok: false, errorCode: "retrieval_ledger_write_failed", reason: "idempotency_conflict", }, } : packResponse("## Summary\n[2] (decision) Fresh viewer identity")); } return jsonResponse(200, { ok: true }); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = messageOutput({ messageId: "user-conflict", sessionID: "sess-conflict" }); // Act await hooks["experimental.chat.messages.transform"]({}, output); await vi.waitFor(() => { const packBodies = fetchPostCalls(fetchMock) .filter(([url]) => String(url).endsWith("/api/pack")) .map(([, options]) => JSON.parse(options.body)) .filter((body) => body.attempt?.stream_id === "sess-conflict"); expect(packBodies).toHaveLength(2); const deliveries = fetchPostCalls(fetchMock) .filter(([url]) => String(url).endsWith("/api/prompt-pack-ledger")) .map(([, options]) => JSON.parse(options.body)); expect(deliveries.some((body) => body.action === "delivery" && body.attempt_id === packBodies[1].attempt.attempt_id)).toBe(true); }); // Assert const conflictPackBodies = fetchPostCalls(fetchMock) .filter(([url]) => String(url).endsWith("/api/pack")) .map(([, options]) => JSON.parse(options.body)) .filter((body) => body.attempt?.stream_id === "sess-conflict"); const firstAttempt = conflictPackBodies[0].attempt; const repairedAttempt = conflictPackBodies[1].attempt; expect(firstAttempt.attempt_id).not.toBe(repairedAttempt.attempt_id); expect(firstAttempt.request_id).not.toBe(repairedAttempt.request_id); const repairedDelivery = fetchPostCalls(fetchMock) .filter(([url]) => String(url).endsWith("/api/prompt-pack-ledger")) .map(([, options]) => JSON.parse(options.body)) .find((body) => body.action === "delivery" && body.attempt_id === repairedAttempt.attempt_id); expect(repairedDelivery).toMatchObject({ action: "delivery", attempt_id: repairedAttempt.attempt_id, }); expect(output.messages[0].parts.at(-1).text).toContain("Fresh viewer identity"); expect(spawnMock.mock.calls.filter(isPackOrLedgerSpawn)).toEqual([]); }); test.each([ ["absent endpoint", () => jsonResponse(404, { error: "not_found" })], ["foreign service", () => jsonResponse(200, { service: "not-codemem" })], ["malformed protocol range", () => viewerProfileResponse({ protocol_version: 2, min_supported_protocol_version: 3, })], ["non-overlapping protocol range", () => viewerProfileResponse({ protocol_version: 3, min_supported_protocol_version: 2, })], ["database target mismatch", () => viewerProfileResponse({ db_path: "/tmp/other.sqlite" })], ["stale viewer identity", () => jsonResponse(409, { error: { code: "viewer_identity_mismatch", message: "viewer identity does not match request" }, })], ["pre-profile contract skew", () => jsonResponse(409, { error: { code: "viewer_contract_unsupported", message: "viewer request contract is incompatible", }, })], ["structured invalid request", () => jsonResponse(400, { error: { code: "invalid_request", message: "context is required" }, })], ["redirect", () => new Response(null, { status: 307, headers: { Location: "http://127.0.0.1:39999/capture" }, })], ])("falls back before sending prompt data when the viewer profile handshake hits a %s", async ( _label, profileResult, ) => { process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => { if (String(url).endsWith("/api/prompt-pack-profile")) return profileResult(); throw new Error("prompt payload must not be sent before profile validation"); }); spawnMock.mockImplementation((_command, args) => Array.isArray(args) && args.includes("pack") ? makeProcess({ stdout: JSON.stringify(packResponse("## Summary\n[9] Safe CLI fallback")) }) : makeProcess({ stdout: "" }), ); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = messageOutput({ messageId: "user-handshake", sessionID: "sess-handshake" }); await hooks["experimental.chat.messages.transform"]({}, output); await vi.waitFor(() => expect(spawnMock.mock.calls.filter(isPackOrLedgerSpawn)).toHaveLength(2), ); expect(fetchPostCalls(fetchMock)).toEqual([]); expect(fetchMock.mock.calls[0][1]).toMatchObject({ method: "GET", redirect: "manual" }); expect(spawnMock.mock.calls.filter(([, args]) => Array.isArray(args) && args.includes("pack"))) .toHaveLength(1); expect(output.messages[0].parts.at(-1).text).toContain("Safe CLI fallback"); }); test.each([ ["connection failure", () => Promise.reject(new Error("ECONNREFUSED"))], ["connection reset", () => Promise.reject(new Error("ECONNRESET"))], ["timeout", () => Promise.reject(Object.assign(new Error("timed out"), { name: "TimeoutError" }))], ["404", () => Promise.resolve(jsonResponse(404, { error: "not_found" }))], ["405", () => Promise.resolve(jsonResponse(405, { error: "method_not_allowed" }))], ["viewer restart", () => Promise.resolve(jsonResponse(503, { error: "unavailable" }))], ["database mismatch", () => Promise.resolve(jsonResponse(409, { error: { code: "viewer_db_mismatch", message: "viewer database does not match request" }, }))], ["identity mismatch", () => Promise.resolve(jsonResponse(409, { error: { code: "viewer_identity_mismatch", message: "viewer identity does not match request" }, }))], ["unrecognized 400", () => Promise.resolve(jsonResponse(400, { error: "bad_request" }))], ["malformed 2xx", () => Promise.resolve(jsonResponse(200, { pack_text: 42 }))], ])("falls back to pack and ledger CLI after viewer %s while preserving output and ledger handoff", async ( _label, viewerResult, ) => { // Arrange process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; process.env.CODEMEM_DB = "/tmp/atlas.sqlite"; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation((url) => String(url).endsWith("/api/prompt-pack-profile") ? Promise.resolve(viewerProfileResponse()) : viewerResult(), ); const packPayloads = []; const ledgerPayloads = []; spawnMock.mockImplementation((_command, args) => { if (Array.isArray(args) && args.includes("pack")) { const proc = makeProcess({ stdout: JSON.stringify(packResponse("## Summary\n[7] (feature) CLI fallback bytes")), }); proc.stdin.write = vi.fn((value) => packPayloads.push(JSON.parse(String(value)))); return proc; } const proc = makeProcess({ stdout: "" }); if (Array.isArray(args) && args.includes("prompt-pack-ledger")) { proc.stdin.write = vi.fn((value) => ledgerPayloads.push(JSON.parse(String(value)))); } return proc; }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = messageOutput({ messageId: "user-fallback", sessionID: "sess-fallback" }); // Act await hooks["experimental.chat.messages.transform"]({}, output); await vi.waitFor(() => expect(ledgerPayloads).toHaveLength(1)); // Assert expect(fetchPostCalls(fetchMock)).toHaveLength(1); expect(packPayloads).toHaveLength(1); expect(ledgerPayloads).toMatchObject([ { action: "delivery", attempt_id: packPayloads[0].attempt_id, delivery_status: "handed_off", }, ]); expect(output.messages[0].parts.at(-1).text).toBe( "[codemem context]\n## Summary\n[7] (feature) CLI fallback bytes", ); expect(spawnMock.mock.calls.filter(isPackOrLedgerSpawn)).toHaveLength(2); }); test.each([ ["connection failure", () => Promise.reject(new Error("ECONNREFUSED"))], ["timeout", () => Promise.reject(Object.assign(new Error("timed out"), { name: "TimeoutError" }))], ["404", () => Promise.resolve(jsonResponse(404, { error: "not_found" }))], ["405", () => Promise.resolve(jsonResponse(405, { error: "method_not_allowed" }))], ["identity mismatch", () => Promise.resolve(jsonResponse(409, { error: { code: "viewer_identity_mismatch", message: "viewer identity does not match request" }, }))], ["5xx", () => Promise.resolve(jsonResponse(503, { error: "unavailable" }))], ["malformed 2xx", () => Promise.resolve(jsonResponse(200, { ok: "yes" }))], ])("falls back only the ledger transition after viewer ledger %s", async ( _label, viewerLedgerResult, ) => { // Arrange process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation((url) => String(url).endsWith("/api/prompt-pack-profile") ? Promise.resolve(viewerProfileResponse()) : String(url).endsWith("/api/pack") ? Promise.resolve(jsonResponse(200, packResponse())) : viewerLedgerResult(), ); const ledgerPayloads = []; spawnMock.mockImplementation((_command, args) => { const proc = makeProcess({ stdout: "" }); if (Array.isArray(args) && args.includes("prompt-pack-ledger")) { proc.stdin.write = vi.fn((value) => ledgerPayloads.push(JSON.parse(String(value)))); } return proc; }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = messageOutput({ messageId: "user-ledger-fallback", sessionID: "sess-ledger-fallback" }); // Act await hooks["experimental.chat.messages.transform"]({}, output); await vi.waitFor(() => expect(ledgerPayloads).toHaveLength(1)); // Assert const viewerLedgerPayloads = fetchPostCalls(fetchMock) .filter(([url]) => String(url).endsWith("/api/prompt-pack-ledger")) .map(([, options]) => JSON.parse(options.body)); const deliveryPayload = viewerLedgerPayloads.find((body) => body.action === "delivery"); expect(deliveryPayload).toMatchObject({ action: "delivery", delivery_status: "handed_off", }); const { db_path: _viewerDbPath, identity_target: _viewerIdentityTarget, ...cliLedgerPayload } = deliveryPayload; expect(ledgerPayloads).toEqual([cliLedgerPayload]); expect(output.messages[0].parts.at(-1).text).toContain("Viewer-backed context"); expect(spawnMock.mock.calls.filter(isPackOrLedgerSpawn)).toHaveLength(1); expect(spawnMock.mock.calls.filter(isPackOrLedgerSpawn)[0][1]).toContain( "prompt-pack-ledger", ); }); test.each([ [400, { error: { code: "invalid_request", message: "context is required" } }], [401, { error: { code: "unauthorized" } }], [403, { error: { code: "policy_denied" } }], [409, { error: { code: "viewer_contract_unsupported", message: "viewer request contract is incompatible", }, }], ])("treats compatible-profile Viewer %s responses as terminal without CLI children", async ( status, packFailure, ) => { // Arrange process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => { if (String(url).endsWith("/api/prompt-pack-profile")) return viewerProfileResponse(); return String(url).endsWith("/api/pack") ? jsonResponse(status, packFailure) : jsonResponse(400, { error: { code: "invalid_request", message: "attempt_id is required" }, }); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); const output = messageOutput({ messageId: "user-invalid", sessionID: "sess-invalid" }); // Act await hooks["experimental.chat.messages.transform"]({}, output); await vi.waitFor(() => expect(fetchPostCalls(fetchMock)).toHaveLength(2)); // Assert expect(fetchPostCalls(fetchMock).map(([url]) => String(url))).toEqual([ "http://127.0.0.1:38888/api/pack", "http://127.0.0.1:38888/api/prompt-pack-ledger", ]); expect(fetchBody(fetchMock, 1)).toMatchObject({ action: "record", retrieval_status: "failed", failure_code: "pack_command_failed", }); expect(output.messages[0].parts).toHaveLength(1); expect(spawnMock.mock.calls.filter(isPackOrLedgerSpawn)).toEqual([]); }); test.each([ ["write 404", 404, "retrieval_ledger_write_failed", "attempt_not_found"], ["delivery 503", 503, "retrieval_ledger_delivery_write_failed", "storage_unavailable"], ["recall 422", 422, "automatic_recall_write_failed", "attempt_not_found"], ["recall 409", 409, "automatic_recall_write_failed", "idempotency_conflict"], ])("treats a structured ledger %s as terminal without arming pack backoff", async ( _label, status, errorCode, reason, ) => { // Arrange process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_RAW_EVENTS = "0"; let postCount = 0; const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (url) => { if (String(url).endsWith("/api/prompt-pack-profile")) return viewerProfileResponse(); postCount += 1; if (postCount === 1) return jsonResponse(200, errorCode === "automatic_recall_write_failed" ? { pack_text: "", metrics: { total_items: 0, pack_tokens: 0 } } : packResponse("## Summary\n[1] First pack")); if (postCount === 2) return jsonResponse(status, { ok: false, errorCode, reason }); if (postCount === 3) return jsonResponse(200, packResponse("## Summary\n[2] Next pack")); return jsonResponse(200, { ok: true }); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); // Act await hooks["experimental.chat.messages.transform"]({}, messageOutput({ messageId: "user-ledger-terminal", sessionID: "sess-ledger-terminal", })); await vi.waitFor(() => expect(fetchPostCalls(fetchMock)).toHaveLength(2)); await hooks["experimental.chat.messages.transform"]({}, messageOutput({ messageId: "user-after-ledger-terminal", sessionID: "sess-ledger-terminal", })); await vi.waitFor(() => expect(fetchPostCalls(fetchMock)).toHaveLength(4)); // Assert expect(fetchMock.mock.calls.filter(([url]) => String(url).endsWith("/api/pack"))).toHaveLength(2); expect(spawnMock.mock.calls.filter(isPackOrLedgerSpawn)).toEqual([]); }); test.each([ ["database", 409, "viewer_db_mismatch", "restart the viewer from the same workspace/config"], [ "identity", 409, "viewer_identity_mismatch", "restart the viewer and OpenCode", ], [ "contract", 409, "viewer_contract_unsupported", "update Codemem on the installed channel, then restart OpenCode", ], ["connection", 503, "other_failure", "check or restart the viewer"], ["queue", 503, "raw_event_queue_full", "repair or archive the viewer raw-event queue"], ])("spools failed HTTP delivery and reports bounded %s guidance", async ( category, status, errorCode, nextAction, ) => { const home = mkdtempSync(join(tmpdir(), "codemem-opencode-spool-")); tmpDirs.push(home); process.env.HOME = home; process.env.CODEMEM_VIEWER = "1"; process.env.CODEMEM_VIEWER_AUTO = "0"; process.env.CODEMEM_VIEWER_HOST = "sensitive-host.invalid"; process.env.CODEMEM_VIEWER_PORT = "49123"; process.env.CODEMEM_DB = "/sensitive/db.sqlite"; process.env.CODEMEM_DEVICE_ID = "sensitive-device"; process.env.CODEMEM_RAW_EVENTS = "1"; const spoolDirectory = join(home, ".codemem", "opencode-raw-event-spool"); const postedBodies = []; const appLog = vi.fn().mockResolvedValue(undefined); const showToast = vi.fn().mockResolvedValue(undefined); vi.spyOn(globalThis, "fetch").mockImplementation(async (url, init) => { if (String(url).includes("/api/raw-events/status")) { return jsonResponse(200, { ingest: { available: true } }); } if (String(url).endsWith("/api/raw-events") && init?.method === "POST") { postedBodies.push(JSON.parse(String(init.body))); return jsonResponse(status, { error: { code: errorCode, message: "sensitive-db sensitive-device sensitive payload", }, }); } return jsonResponse(200, {}); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: appLog }, tui: { showToast } }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); await hooks["tool.execute.after"]( { tool: "read", args: { filePath: "src/raw-event.ts" }, sessionID: `sess-${category}` }, {}, ); if (category === "identity") { await vi.waitFor(() => expect(appLog).toHaveBeenCalledWith({ body: expect.objectContaining({ level: "error", message: expect.stringContaining("capture delivery paused: viewer identity mismatch"), extra: { category: "identity", code: "viewer_identity_mismatch" }, }), }), ); expect(showToast).toHaveBeenCalledTimes(1); expect(showToast).toHaveBeenCalledWith({ body: expect.objectContaining({ variant: "error", message: expect.stringContaining(nextAction), }), }); } else { await vi.waitFor(() => expect(appLog).toHaveBeenCalledWith({ body: expect.objectContaining({ message: expect.stringContaining( `codemem raw event was saved for retry after viewer post returned ${status}`, ), extra: expect.objectContaining({ category, delivery: "spool", viewer_stage: "post", viewer_cause: "http_status", viewer_status: status, viewer_elapsed_ms: expect.any(Number), cli_attempts: 0, cli_elapsed_ms: expect.any(Number), }), }), }), ); expect(showToast).not.toHaveBeenCalled(); } expect(appLog.mock.calls.some(([call]) => call.body.message.includes(nextAction))).toBe(true); const httpEnvelope = { ...postedBodies[0] }; delete httpEnvelope.db_path; delete httpEnvelope.identity_target; let spoolFiles; await vi.waitFor(() => { spoolFiles = readdirSync(spoolDirectory).filter((name) => name.endsWith(".json")); expect(spoolFiles).toHaveLength(1); }); expect(spoolFiles).toHaveLength(1); const spooledEnvelope = JSON.parse(readFileSync(join(spoolDirectory, spoolFiles[0]), "utf8")); expect(spooledEnvelope).toEqual(httpEnvelope); expect(spooledEnvelope).not.toHaveProperty("db_path"); expect(spooledEnvelope).not.toHaveProperty("identity_target"); expect(spawnMock.mock.calls.some(([, args]) => args?.includes("enqueue-raw-event"))).toBe(false); const notices = JSON.stringify([...appLog.mock.calls, ...showToast.mock.calls]); for (const sensitive of [ "sensitive-host.invalid", "49123", "/sensitive/db.sqlite", "sensitive-device", "sensitive payload", ]) { expect(notices).not.toContain(sensitive); } }); test("retries a retained event after transport backoff without spawning the CLI", async () => { // Arrange: delay durable publication independently of the fake clock. const home = mkdtempSync(join(tmpdir(), "codemem-opencode-terminal-spool-")); tmpDirs.push(home); process.env.HOME = home; process.env.CODEMEM_RAW_EVENTS = "1"; process.env.CODEMEM_RAW_EVENTS_BACKOFF_MS = "1000"; const sessionID = "sess-http-retry"; let viewerAvailable = false; const postedEventIds = []; const appLog = vi.fn().mockResolvedValue(undefined); const barrier = await holdSpoolPublication(home); try { vi.spyOn(globalThis, "fetch").mockImplementation(async (url, init) => { if (!viewerAvailable) throw new Error("viewer unavailable"); if (String(url).includes("/api/raw-events/status")) { return jsonResponse(200, { ingest: { available: true } }); } if (String(url).endsWith("/api/raw-events") && init?.method === "POST") { postedEventIds.push(JSON.parse(String(init.body)).event_id); return jsonResponse(202, { accepted: 1, queued: 1 }); } return jsonResponse(200, {}); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: appLog }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); await hooks["tool.execute.after"]( { tool: "read", args: {}, sessionID }, {}, ); await barrier.publication; // Act: idle must join capture's cached delivery while real I/O is pending. let idleCompleted = false; const idle = hooks.event({ event: { type: "session.idle", properties: { sessionID } } }).then(() => { idleCompleted = true; }); await vi.advanceTimersByTimeAsync(1_001); // Assert: a whole polling deadline cannot complete an unpublished delivery. expect(idleCompleted).toBe(false); expect(appLog.mock.calls.map(([entry]) => entry.body.message)).toEqual([ "codemem plugin initialized", ]); barrier.release(); await idle; expect(appLog).toHaveBeenCalledWith({ body: expect.objectContaining({ message: expect.stringContaining( "codemem raw event was saved for retry after viewer status connection", ), extra: expect.objectContaining({ category: "connection", delivery: "spool", viewer_stage: "status", viewer_cause: "connection", viewer_elapsed_ms: expect.any(Number), cli_attempts: 0, cli_elapsed_ms: expect.any(Number), }), }), }); const spoolDirectory = join(home, ".codemem", "opencode-raw-event-spool"); expect(readdirSync(spoolDirectory).filter((name) => name.endsWith(".json"))).toHaveLength(1); viewerAvailable = true; const afterBackoff = Date.now() + 1_001; vi.spyOn(Date, "now").mockReturnValue(afterBackoff); await hooks.event({ event: { type: "session.idle", properties: { sessionID } } }); // Assert: the awaited idle hook also finishes retry delivery and spool removal. expect(postedEventIds).toHaveLength(1); expect(readdirSync(spoolDirectory).filter((name) => name.endsWith(".json"))).toEqual([]); expect(spawnMock.mock.calls.some(([, args]) => args?.includes("enqueue-raw-event"))).toBe(false); } finally { barrier.restore(); } }); test("recovers a retained event over HTTP after plugin module restart", async () => { const home = mkdtempSync(join(tmpdir(), "codemem-opencode-restart-spool-")); tmpDirs.push(home); process.env.HOME = home; process.env.CODEMEM_RAW_EVENTS = "1"; process.env.CODEMEM_RAW_EVENTS_BACKOFF_MS = "1000"; const sessionID = "sess-spool-restart"; let viewerAvailable = false; const postedEventIds = []; vi.spyOn(globalThis, "fetch").mockImplementation(async (url, init) => { if (!viewerAvailable) throw new Error("viewer unavailable"); if (String(url).includes("/api/raw-events/status")) { return jsonResponse(200, { ingest: { available: true } }); } if (String(url).endsWith("/api/raw-events") && init?.method === "POST") { postedEventIds.push(JSON.parse(String(init.body)).event_id); return jsonResponse(202, { accepted: 1, queued: 1 }); } return jsonResponse(200, {}); }); let { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); await hooks["tool.execute.after"]( { tool: "read", args: {}, sessionID }, {}, ); const spoolDirectory = join(home, ".codemem", "opencode-raw-event-spool"); await vi.waitFor(() => expect(readdirSync(spoolDirectory).filter((name) => name.endsWith(".json"))).toHaveLength(1), ); await hooks.event({ event: { type: "session.idle", properties: { sessionID } } }); expect(readdirSync(spoolDirectory).filter((name) => name.endsWith(".json"))).toHaveLength(1); viewerAvailable = true; Reflect.deleteProperty(globalThis, pluginRegistrationsKey); vi.resetModules(); ({ OpencodeMemPlugin } = await import("../plugins/codemem.js")); await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); await vi.waitFor(() => expect(postedEventIds).toHaveLength(1)); await vi.waitFor(() => expect(readdirSync(spoolDirectory).filter((name) => name.endsWith(".json"))).toEqual([]), ); expect(spawnMock.mock.calls.some(([, args]) => args?.includes("enqueue-raw-event"))).toBe(false); }); test("preserves the retry spool across every drain trigger when raw events are disabled", async () => { const home = mkdtempSync(join(tmpdir(), "codemem-opencode-disabled-spool-")); tmpDirs.push(home); process.env.HOME = home; process.env.CODEMEM_RAW_EVENTS = "0"; await writeRawEventSpoolEntry({ envelope: { event_id: "event-retained-disabled", event_type: "tool" }, homeDir: home, }); const enqueueBytes = []; spawnMock.mockImplementation((_command, args) => { const proc = makeProcess({ exitCode: 0 }); if (Array.isArray(args) && args.includes("enqueue-raw-event")) { proc.stdin.write = vi.fn((value) => enqueueBytes.push(String(value))); } return proc; }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); await hooks["tool.execute.after"]( { tool: "read", args: {}, sessionID: "sess-disabled-spool" }, {}, ); await hooks.event({ event: { type: "session.idle", properties: { sessionID: "sess-disabled-spool" } }, }); const spoolDirectory = join(home, ".codemem", "opencode-raw-event-spool"); expect(enqueueBytes).toEqual([]); expect(readdirSync(spoolDirectory).filter((name) => name.endsWith(".json"))).toHaveLength(1); expect(readFileSync(join(spoolDirectory, readdirSync(spoolDirectory)[0]), "utf8")).toContain( "event-retained-disabled", ); }); test("continues draining after a poisoned oldest spool entry", async () => { const home = mkdtempSync(join(tmpdir(), "codemem-opencode-poisoned-spool-")); tmpDirs.push(home); process.env.HOME = home; process.env.CODEMEM_RAW_EVENTS = "1"; const oldest = { event_id: "event-poisoned", event_type: "tool" }; const newer = { event_id: "event-valid", event_type: "tool" }; await writeRawEventSpoolEntry({ envelope: oldest, homeDir: home }); await writeRawEventSpoolEntry({ envelope: newer, homeDir: home }); const spoolDirectory = join(home, ".codemem", "opencode-raw-event-spool"); const spoolFiles = readdirSync(spoolDirectory).filter((name) => name.endsWith(".json")); for (const filename of spoolFiles) { const serialized = readFileSync(join(spoolDirectory, filename), "utf8"); const time = serialized.includes("event-poisoned") ? 1_000 : 2_000; utimesSync(join(spoolDirectory, filename), new Date(time), new Date(time)); } const attemptedIds = []; vi.spyOn(globalThis, "fetch").mockImplementation(async (url, init) => { if (String(url).endsWith("/api/raw-events") && init?.method === "POST") { const eventId = JSON.parse(String(init.body)).event_id; attemptedIds.push(eventId); return eventId === "event-poisoned" ? jsonResponse(400, { error: "invalid raw event request" }) : jsonResponse(202, { accepted: 1, queued: 1 }); } return jsonResponse(200, { ingest: { available: true } }); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); await vi.waitFor(() => expect(attemptedIds).toContain("event-valid")); expect(attemptedIds.slice(0, 2)).toEqual(["event-poisoned", "event-valid"]); const remaining = readdirSync(spoolDirectory) .filter((name) => name.endsWith(".json")) .map((name) => readFileSync(join(spoolDirectory, name), "utf8")); expect(remaining).toHaveLength(1); expect(remaining[0]).toContain("event-poisoned"); expect(spawnMock.mock.calls.some(([, args]) => args?.includes("enqueue-raw-event"))).toBe(false); }); test("shows a bounded error when the retry spool contains a corrupt entry", async () => { const home = mkdtempSync(join(tmpdir(), "codemem-opencode-corrupt-spool-")); tmpDirs.push(home); process.env.HOME = home; process.env.CODEMEM_RAW_EVENTS = "1"; const spoolDirectory = join(home, ".codemem", "opencode-raw-event-spool"); mkdirSync(spoolDirectory, { recursive: true }); writeFileSync(join(spoolDirectory, "corrupt.json"), "private malformed payload", "utf8"); const appLog = vi.fn().mockResolvedValue(undefined); const showToast = vi.fn().mockResolvedValue(undefined); vi.spyOn(globalThis, "fetch").mockResolvedValue( jsonResponse(200, { ingest: { available: true } }), ); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: appLog }, tui: { showToast } }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); await vi.waitFor(() => expect(showToast).toHaveBeenCalledWith({ body: expect.objectContaining({ message: expect.stringContaining("contains 1 corrupt retained entry"), variant: "error", }), }), ); const notices = JSON.stringify([...appLog.mock.calls, ...showToast.mock.calls]); expect(notices).not.toContain("private malformed payload"); expect(notices).not.toContain(spoolDirectory); }); test("stops an HTTP drain after a transient failure without an inline retry", async () => { const home = mkdtempSync(join(tmpdir(), "codemem-opencode-transient-spool-")); tmpDirs.push(home); process.env.HOME = home; process.env.CODEMEM_RAW_EVENTS = "1"; const oldest = { event_id: "event-transient", event_type: "tool" }; const newer = { event_id: "event-deferred", event_type: "tool" }; await writeRawEventSpoolEntry({ envelope: oldest, homeDir: home }); await writeRawEventSpoolEntry({ envelope: newer, homeDir: home }); const spoolDirectory = join(home, ".codemem", "opencode-raw-event-spool"); for (const filename of readdirSync(spoolDirectory).filter((name) => name.endsWith(".json"))) { const serialized = readFileSync(join(spoolDirectory, filename), "utf8"); const time = serialized.includes("event-transient") ? 1_000 : 2_000; utimesSync(join(spoolDirectory, filename), new Date(time), new Date(time)); } const attemptedIds = []; vi.spyOn(globalThis, "fetch").mockImplementation(async (url, init) => { if (String(url).endsWith("/api/raw-events") && init?.method === "POST") { attemptedIds.push(JSON.parse(String(init.body)).event_id); throw new Error("viewer unavailable"); } return jsonResponse(200, { ingest: { available: true } }); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: vi.fn().mockResolvedValue(undefined) }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); await vi.waitFor(() => expect(attemptedIds).toHaveLength(1)); await hooks.event({ event: { type: "session.idle", properties: { sessionID: "sess-transient-spool" } }, }); expect(attemptedIds).toEqual(["event-transient"]); expect(readdirSync(spoolDirectory).filter((name) => name.endsWith(".json"))).toHaveLength(2); expect(spawnMock.mock.calls.some(([, args]) => args?.includes("enqueue-raw-event"))).toBe(false); }); test("resets the persistence warning latch after a successful spool write", async () => { // Arrange: hold atomic publication so temporary-file visibility is deterministic. const home = mkdtempSync(join(tmpdir(), "codemem-opencode-spool-latch-")); tmpDirs.push(home); process.env.HOME = home; process.env.CODEMEM_RAW_EVENTS = "1"; process.env.CODEMEM_RAW_EVENTS_BACKOFF_MS = "60000"; writeFileSync(join(home, ".codemem"), "blocked"); const appLog = vi.fn().mockResolvedValue(undefined); const showToast = vi.fn().mockResolvedValue(undefined); vi.spyOn(globalThis, "fetch").mockRejectedValue(new Error("viewer unavailable")); spawnMock.mockImplementation((_command, args) => Array.isArray(args) && args.includes("enqueue-raw-event") ? makeProcess({ exitCode: 1, stderr: "fallback unavailable" }) : makeProcess({ stdout: "" }), ); const barrier = await holdSpoolPublication(home); try { const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: appLog }, tui: { showToast } }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); await assertSpoolLatchRecovery({ home, hooks, appLog, showToast, barrier }); } finally { barrier.restore(); } }); test("warns without claiming durability when the spool write fails", async () => { const root = mkdtempSync(join(tmpdir(), "codemem-opencode-spool-failure-")); tmpDirs.push(root); const blockedHome = join(root, "not-a-directory"); writeFileSync(blockedHome, "blocked"); process.env.HOME = blockedHome; process.env.CODEMEM_RAW_EVENTS = "1"; const appLog = vi.fn().mockResolvedValue(undefined); vi.spyOn(globalThis, "fetch").mockRejectedValue(new Error("viewer unavailable")); spawnMock.mockImplementation((_command, args) => { if (Array.isArray(args) && args.includes("enqueue-raw-event")) { return makeProcess({ exitCode: 1, stderr: "sensitive subprocess failure", }); } return makeProcess({ stdout: "" }); }); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: appLog }, tui: {} }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); await hooks["tool.execute.after"]( { tool: "read", args: {}, sessionID: "sess-spool-failure" }, {}, ); await vi.waitFor(() => expect(appLog).toHaveBeenCalledWith({ body: expect.objectContaining({ message: "codemem could not save a raw event for retry; it remains queued in memory", extra: { category: "persistence", reason: "write_failed" }, }), }), ); const notices = JSON.stringify(appLog.mock.calls); expect(notices).not.toContain("sensitive subprocess failure"); expect(notices).not.toContain("codemem raw event was saved for retry"); }); test("reports a full retry spool distinctly without exposing retained payloads", async () => { const home = mkdtempSync(join(tmpdir(), "codemem-opencode-spool-full-notice-")); tmpDirs.push(home); process.env.HOME = home; process.env.CODEMEM_RAW_EVENTS = "1"; process.env.CODEMEM_RAW_EVENT_SPOOL_MAX_ENTRIES = "1"; await writeRawEventSpoolEntry({ envelope: { event_id: "retained-private-event", event_type: "tool" }, homeDir: home, maxEntries: 1, }); const appLog = vi.fn().mockResolvedValue(undefined); const showToast = vi.fn().mockResolvedValue(undefined); vi.spyOn(globalThis, "fetch").mockRejectedValue(new Error("viewer unavailable")); spawnMock.mockImplementation(() => makeProcess({ exitCode: 1, stderr: "fallback unavailable" })); const { OpencodeMemPlugin } = await import("../plugins/codemem.js"); const hooks = await OpencodeMemPlugin({ project: { name: "atlas" }, client: { app: { log: appLog }, tui: { showToast } }, directory: "/tmp/atlas", worktree: "/tmp/atlas", }); await hooks["tool.execute.after"]( { tool: "read", args: {}, sessionID: "sess-spool-full" }, {}, ); await vi.waitFor(() => expect(appLog.mock.calls.some(([entry]) => entry.body?.extra?.reason === "spool_full")).toBe(true), ); const notices = [...appLog.mock.calls, ...showToast.mock.calls] .map(([entry]) => JSON.stringify(entry)) .join("\n"); expect(notices).toContain("retry spool is full"); expect(notices).not.toContain("retained-private-event"); }); });