import { execSync, spawn as nodeSpawn } from "node:child_process"; import { createHash, randomUUID } from "node:crypto"; import { existsSync, readFileSync } from "node:fs"; import { appendFile, mkdir } from "node:fs/promises"; import { homedir } from "node:os"; import { basename, dirname, join, posix, resolve, win32 } from "node:path"; import { createCachedCliRunner } from "./cached-cli-runner.js"; import { runChildCommand } from "./child-command.js"; import { isVersionAtLeast, parseBackendUpdatePolicy, parseSemver, resolveAutoUpdatePlan, resolveUpgradeGuidance, } from "./compat.js"; import { V2_ADAPTER_DIAGNOSTICS } from "./host-contract.js"; import { createMemoryToolExecutor, createNativeMemoryTools, nativeMemoryToolsEnabled, } from "./memory-tools.js"; import { createPromptTransportDiagnostics } from "./prompt-transport-diagnostics.js"; import { classifyRawEventTransportCause, createRawEventDelivery, describeRawEventViewerFailure, } from "./raw-event-delivery.js"; import { DEFAULT_DRAIN_LIMIT, DEFAULT_MAX_ENTRIES, } from "./raw-event-spool.js"; import { createRecallLifecycle, getSessionMessageInjectionCache, MAX_MESSAGE_INJECTION_CACHE_ENTRIES, setSessionMessageInjectionCacheEntry, } from "./recall-lifecycle.js"; const TRUTHY_VALUES = ["1", "true", "yes"]; const DISABLED_VALUES = ["0", "false", "off"]; const PINNED_BACKEND_VERSION = "0.49.1"; const COMPAT_CHECK_DELAY_MS = 1500; const COMPAT_CHECK_CACHE_TTL_MS = 5 * 60 * 1000; const MAX_UPDATE_STATUS_BYTES = 16 * 1024; const MAX_UPDATE_ACTION_CHARS = 1000; const MAX_UPDATE_VERSION_CHARS = 128; const RELEASE_VERSION = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/; const CODEMEM_CONTEXT_PART_ID_PREFIX = "codemem-context-"; const COMPACTION_INJECTION_SKIP_TTL_MS = 30 * 1000; const MAX_WORKING_SET_PATH_CHARS = 400; const VIEWER_HEALTH_CHECK_INTERVAL_MS = 60_000; const VIEWER_HEALTH_TIMEOUT_MS = 5_000; const VIEWER_HEALTH_RECOVERY_THRESHOLD = 2; // Every plugin instance probes the shared viewer, so the cooldown keeps one // instance from retrying recovery on every interval while the viewer boots. const VIEWER_HEALTH_RECOVERY_COOLDOWN_MS = 5 * 60_000; const RAW_EVENTS_STATUS_TIMEOUT_MS = 5_000; const DEFAULT_INJECT_TOKEN_BUDGET = 800; const DEFAULT_RETAINED_TOKEN_BUDGET = Infinity; const CODEMEM_CONTEXT_PREFIX = "[codemem context]\n"; const DEFAULT_EMBEDDING_MODEL = "Xenova/bge-small-en-v1.5"; const DEFAULT_EMBEDDING_REVISION = "ea104dacec62c0de699686887e3f920caeb4f3e3"; const PLUGIN_REGISTRATIONS_KEY = Symbol.for("codemem.opencode-plugin.registrations"); const ADAPTER_DIAGNOSTIC_CODES = new Set(Object.values(V2_ADAPTER_DIAGNOSTICS)); let compatCheckCache = null; const notifiedReleaseVersions = new Set(); const claimPluginRegistration = (cwd) => { let registrations = globalThis[PLUGIN_REGISTRATIONS_KEY]; if (!(registrations instanceof Map)) { registrations = new Map(); globalThis[PLUGIN_REGISTRATIONS_KEY] = registrations; } const projectPath = resolve(cwd); if (registrations.has(projectPath)) return null; const registration = Symbol(projectPath); registrations.set(projectPath, registration); return () => { if (registrations.get(projectPath) === registration) { registrations.delete(projectPath); } }; }; const registrationDirectoryFor = ({ directory, cwd, hostGeneration }) => { // V2 loads plugins per location, including nested locations in one worktree. if (hostGeneration === "v2" && directory) return directory; return cwd; }; // Release an unread response body without surfacing cancellation failures. const discardResponseBody = (response) => { try { const cancelled = response?.body?.cancel?.(); if (cancelled && typeof cancelled.catch === "function") { cancelled.catch(() => {}); } } catch { // Best effort — a locked or already-errored stream is fine to abandon. } }; // Bounded ingest-availability preflight: a hung viewer socket must not stall // raw-event delivery indefinitely. Failures fall into the existing stream // backoff + CLI enqueue fallback path. const fetchRawEventsStatus = (url, fetchFn = fetch) => fetchFn(url, { method: "GET", signal: AbortSignal.timeout(RAW_EVENTS_STATUS_TIMEOUT_MS), }); const normalizeEnvValue = (value) => (value || "").toLowerCase(); const envHasValue = (value, truthyValues) => truthyValues.includes(normalizeEnvValue(value)); const envNotDisabled = (value) => !DISABLED_VALUES.includes(normalizeEnvValue(value)); /** `codemem serve start` prints this when a viewer already holds the port. */ const viewerAlreadyRunning = (result) => /viewer already running/i.test(`${result?.stdout ?? ""}\n${result?.stderr ?? ""}`); /** * Health recovery runs `serve start`, which exits without touching a viewer that * already holds the port, so concurrent recoveries from several instances cannot * stop each other's freshly started process the way `serve restart` would. The * instance owns the viewer only if its start launched one: when another * instance's viewer holds the port, `serve start` exits 0 and says so. */ const recoverViewerStart = async (started, onLaunched) => { const result = await started; if (result?.exitCode === 0 && !viewerAlreadyRunning(result)) onLaunched(); return result; }; const createViewerHealthMonitor = ({ viewerHealthUrl, legacyStatusUrl, isActive, recoverViewer, logLine, fetchFn = fetch, now = Date.now, setIntervalFn = setInterval, clearIntervalFn = clearInterval, timeoutSignal = (timeoutMs) => AbortSignal.timeout(timeoutMs), }) => { let timer = null; let consecutiveFailures = 0; let lastRestartAttempt = 0; const boundedFetch = (url) => fetchFn(url, { method: "GET", signal: timeoutSignal(VIEWER_HEALTH_TIMEOUT_MS), }); const probe = async () => { let response; try { response = await boundedFetch(viewerHealthUrl); } catch (error) { return { live: false, detail: `error: ${String(error).slice(0, 200)}` }; } if (response.status === 404) { discardResponseBody(response); try { // Old-viewer compatibility: released viewers serving this route // always include the `ingest` availability object, so require that // identifying evidence rather than trusting any 2xx from an // arbitrary local service. const fallbackResponse = await boundedFetch(legacyStatusUrl); if (!fallbackResponse.ok) { discardResponseBody(fallbackResponse); return { live: false, detail: `fallback status=${fallbackResponse.status}` }; } const fallbackPayload = await fallbackResponse.json(); const looksLikeViewer = fallbackPayload && typeof fallbackPayload === "object" && fallbackPayload.ingest && typeof fallbackPayload.ingest === "object"; return looksLikeViewer ? { live: true } : { live: false, detail: "fallback unexpected payload" }; } catch (error) { return { live: false, detail: `fallback error: ${String(error).slice(0, 200)}` }; } } if (!response.ok) { // Release the unread body so a persistently failing viewer does not // pin connections across the 60s monitor interval. discardResponseBody(response); return { live: false, detail: `status=${response.status}` }; } let payload; try { payload = await response.json(); } catch (error) { return { live: false, detail: `invalid JSON: ${String(error).slice(0, 200)}` }; } if (!payload || typeof payload !== "object" || payload.service !== "codemem-viewer") { return { live: false, detail: "unexpected service" }; } return { live: true }; }; const check = async () => { if (!isActive()) return; const result = await probe(); if (result.live) { if (consecutiveFailures > 0) { await logLine(`viewer.health recovered after ${consecutiveFailures} failure(s)`); } consecutiveFailures = 0; return; } consecutiveFailures += 1; await logLine( `viewer.health check failed (${result.detail}, consecutive=${consecutiveFailures})` ); if ( consecutiveFailures < VIEWER_HEALTH_RECOVERY_THRESHOLD || now() - lastRestartAttempt < VIEWER_HEALTH_RECOVERY_COOLDOWN_MS ) { return; } // Re-check after the awaited probe: a stop requested while the probe was // in flight must not be undone by a recovery start. if (!isActive()) return; lastRestartAttempt = now(); await logLine(`viewer.health starting viewer after ${consecutiveFailures} consecutive failures`); try { const startResult = await recoverViewer(); const started = startResult?.exitCode === 0; await logLine( `viewer.health start ${started ? "succeeded" : "failed"} (exit=${startResult?.exitCode ?? "unknown"})` ); if (started) consecutiveFailures = 0; } catch (error) { await logLine(`viewer.health start error: ${String(error).slice(0, 200)}`); } }; const start = () => { if (timer) return; consecutiveFailures = 0; timer = setIntervalFn(() => { check().catch(() => {}); }, VIEWER_HEALTH_CHECK_INTERVAL_MS); if (timer?.unref) timer.unref(); }; const stop = () => { if (timer) { clearIntervalFn(timer); timer = null; } consecutiveFailures = 0; }; return { check, start, stop, state: () => ({ consecutiveFailures, lastRestartAttempt, running: timer !== null, }), }; }; const resolveInjectSurface = (value) => { const normalized = String(value || "message").trim().toLowerCase(); if (normalized === "system") { return "system"; } return "message"; }; const hasOwn = (value, key) => Object.hasOwn(value || {}, key); const deterministicUuid = (namespace, components) => { const hex = createHash("sha256") .update(JSON.stringify([namespace, ...components.map((value) => String(value ?? ""))])) .digest("hex"); const bytes = Buffer.from(hex.slice(0, 32), "hex"); bytes[6] = (bytes[6] & 0x0f) | 0x50; bytes[8] = (bytes[8] & 0x3f) | 0x80; const value = bytes.toString("hex"); return `${value.slice(0, 8)}-${value.slice(8, 12)}-${value.slice(12, 16)}-${value.slice(16, 20)}-${value.slice(20)}`; }; const promptPackIdentity = ({ source = "opencode", sessionID = "unknown", requestKey, surface, promptNumber = 0, queryHash, }) => { const components = [source, sessionID, requestKey, surface, promptNumber, queryHash]; return { attemptId: deterministicUuid("codemem.prompt-pack.attempt.v1", components), requestId: deterministicUuid("codemem.prompt-pack.request.v1", components), }; }; const hashPromptPackQuery = (query) => createHash("sha256").update(String(query || "")).digest("hex"); const redactPackCommand = (runner, runnerArgs, packArgs) => { const safePackArgs = [...packArgs]; const queryIndex = safePackArgs[0] === "memory" ? 2 : 1; if (safePackArgs[queryIndex - 1] === "pack" && safePackArgs.length > queryIndex) { safePackArgs[queryIndex] = "[query-redacted]"; } for (let index = 0; index < safePackArgs.length - 1; index += 1) { if (safePackArgs[index] === "--working-set-file") { safePackArgs[index + 1] = "[path-redacted]"; } } return [runner, ...runnerArgs, ...safePackArgs].join(" "); }; const rejectsInternalLedgerFlag = (result) => { if (!result || result.exitCode === 0) return false; const diagnostics = `${result.stderr || ""}\n${result.stdout || ""}`; return diagnostics.includes("--internal-ledger") && /(?:unknown|unsupported|unrecognized|invalid)\s+(?:option|argument)/i.test(diagnostics); }; const parseFallbackStructuredError = (stdout) => { const lines = String(stdout || "").trim().split(/\r?\n/).reverse(); for (const line of lines) { try { const parsed = JSON.parse(line); if (parsed && typeof parsed === "object") { return { code: typeof parsed.error === "string" ? parsed.error : "", message: typeof parsed.message === "string" ? parsed.message : "", }; } } catch { // Continue past non-JSON CLI output. } } return { code: "", message: "" }; }; const classifyFallbackCommandResult = (result) => { const structured = parseFallbackStructuredError(result?.stdout); const diagnostic = [structured.code, structured.message, result?.stderr, result?.stdout] .filter(Boolean) .join("\n"); if (/SQLITE_(?:BUSY|LOCKED)|database(?: table)? (?:is )?(?:busy|locked)/i.test(diagnostic)) { return { retryable: true, cause: "SQLite database is locked" }; } if (result?.exitCode == null && /(?:timeout|timed out|ETIMEDOUT)/i.test(diagnostic)) { return { retryable: true, cause: "enqueue-raw-event command timeout" }; } if (structured.code === "validation_error" || /invalid raw event|session id required/i.test(diagnostic)) { return { retryable: false, cause: "enqueue-raw-event validation failed" }; } if (/unknown command ['"]?enqueue-raw-event|command not found|\bENOENT\b/i.test(diagnostic)) { return { retryable: false, cause: "enqueue-raw-event command unavailable" }; } const exitCode = result?.exitCode ?? "unknown"; return { retryable: false, cause: `enqueue-raw-event failed (${exitCode})` }; }; const DEFAULT_LOG_PATH = (homeDir, cwd) => `${homeDir || cwd}/.codemem/plugin.log`; const resolveLogPath = (logPathEnvRaw, cwd, homeDir) => { const logPathEnv = normalizeEnvValue(logPathEnvRaw); const logEnabled = !!logPathEnvRaw && !DISABLED_VALUES.includes(logPathEnv); if (!logEnabled) { return null; } if (["true", "yes", "1"].includes(logPathEnv)) { return DEFAULT_LOG_PATH(homeDir, cwd); } return logPathEnvRaw; }; /** Path for error/warning logging — always available regardless of debug flag. */ const resolveErrorLogPath = (cwd, homeDir) => DEFAULT_LOG_PATH(homeDir, cwd); const resolveCompatCheckCacheKey = ({ backendUpdatePolicy, minVersion, runner, runnerFrom }) => [backendUpdatePolicy, minVersion, runner, runnerFrom || ""].join("|"); const readCompatCheckCache = (cacheKey) => { if (!compatCheckCache) { return null; } if (compatCheckCache.cacheKey !== cacheKey) { return null; } if (Date.now() >= compatCheckCache.expiresAtMs) { compatCheckCache = null; return null; } return compatCheckCache.value; }; const writeCompatCheckCache = (cacheKey, value) => { compatCheckCache = { cacheKey, expiresAtMs: Date.now() + COMPAT_CHECK_CACHE_TTL_MS, value, }; }; const clearCompatCheckCache = () => { compatCheckCache = null; }; const parseReleaseVersion = (value) => { if (typeof value !== "string" || value.length > MAX_UPDATE_VERSION_CHARS) return null; const match = RELEASE_VERSION.exec(value); if (!match?.slice(1, 4).map(Number).every(Number.isSafeInteger)) return null; const prerelease = match[4]?.split(".") || []; if (prerelease.some((identifier) => /^0\d+$/.test(identifier))) return null; return { core: match.slice(1, 4).map(Number), prerelease }; }; const releaseChannelForVersion = (value) => { const parsed = parseReleaseVersion(value); if (!parsed) return null; if (parsed.prerelease.length === 0) return "latest"; const channel = parsed.prerelease[0]; return channel === "alpha" || channel === "beta" || channel === "rc" ? channel : null; }; const comparePrereleaseIdentifier = (left, right) => { if (left === undefined) return right === undefined ? 0 : -1; if (right === undefined) return 1; if (left === right) return 0; const leftNumeric = /^\d+$/.test(left); const rightNumeric = /^\d+$/.test(right); if (leftNumeric && rightNumeric && left.length !== right.length) { return Math.sign(left.length - right.length); } if (leftNumeric !== rightNumeric) return leftNumeric ? -1 : 1; return left < right ? -1 : 1; }; const compareReleaseVersions = (left, right) => { const leftVersion = parseReleaseVersion(left); const rightVersion = parseReleaseVersion(right); if (!leftVersion || !rightVersion) return null; for (let index = 0; index < leftVersion.core.length; index += 1) { const difference = leftVersion.core[index] - rightVersion.core[index]; if (difference !== 0) return Math.sign(difference); } if (leftVersion.prerelease.length === 0) { return rightVersion.prerelease.length === 0 ? 0 : 1; } if (rightVersion.prerelease.length === 0) return -1; const length = Math.max(leftVersion.prerelease.length, rightVersion.prerelease.length); for (let index = 0; index < length; index += 1) { const comparison = comparePrereleaseIdentifier( leftVersion.prerelease[index], rightVersion.prerelease[index], ); if (comparison !== 0) return comparison; } return 0; }; const isSameChannelVersionAtLeast = (currentVersion, minimumVersion) => { const currentChannel = releaseChannelForVersion(currentVersion); if (!currentChannel || currentChannel !== releaseChannelForVersion(minimumVersion)) return false; const comparison = compareReleaseVersions(currentVersion, minimumVersion); return comparison !== null && comparison >= 0; }; const PINNED_RELEASE_CHANNEL = releaseChannelForVersion(PINNED_BACKEND_VERSION); const guidanceMatchesRelease = (guidance, latestVersion) => { const versions = String(guidance || "").match( /\b\d+\.\d+\.\d+(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?/g, ) || []; return versions.length === 0 || versions.every((version) => version === latestVersion); }; const parseReleaseNotification = (result) => { if (result?.exitCode !== 0) return null; const stdout = String(result?.stdout || "").trim(); if (!stdout || Buffer.byteLength(stdout, "utf8") > MAX_UPDATE_STATUS_BYTES) return null; try { const status = JSON.parse(stdout); if (!status || typeof status !== "object" || Array.isArray(status)) return null; if (status.update_available !== true) return null; if ( !PINNED_RELEASE_CHANNEL || status.channel !== PINNED_RELEASE_CHANNEL || releaseChannelForVersion(status.latest_version) !== PINNED_RELEASE_CHANNEL ) { return null; } if ( typeof status.recommended_action !== "string" || !status.recommended_action.trim() || status.recommended_action.length > MAX_UPDATE_ACTION_CHARS || !guidanceMatchesRelease(status.recommended_action, status.latest_version) ) { return null; } return { latestVersion: status.latest_version, channel: PINNED_RELEASE_CHANNEL, recommendedAction: status.recommended_action.trim(), autoUpdateEligible: status.auto_update_eligible === true, }; } catch { return null; } }; const resolveAutoUpdateEnvironment = ({ platform = process.platform, env = process.env, } = {}) => { if (platform !== "linux") return env; return { ...env, ONNXRUNTIME_NODE_INSTALL: "skip" }; }; const createLogLine = (logPath) => async (line) => { if (!logPath) { return; } try { await mkdir(dirname(logPath), { recursive: true }); await appendFile(logPath, `${new Date().toISOString()} ${line}\n`); } catch { // ignore logging failures } }; const createDebugLogger = ({ debug, host, logTimeoutMs, getLogLine, getErrorLogLine }) => async (level, message, extra = {}) => { // Always log errors and warnings to the error log path const alwaysLog = level === "error" || level === "warn"; if (alwaysLog) { const extraStr = Object.keys(extra).length > 0 ? ` ${JSON.stringify(extra)}` : ""; await getErrorLogLine()(`[${level}] ${message}${extraStr}`); } if (!debug && !alwaysLog) { return; } try { const logPromise = host.log({ service: "codemem", level, message, extra, }); if (!Number.isFinite(logTimeoutMs) || logTimeoutMs <= 0) { await logPromise; return; } let timedOut = false; await Promise.race([ logPromise, new Promise((resolve) => setTimeout(() => { timedOut = true; resolve(); }, logTimeoutMs) ), ]); if (timedOut) { await getLogLine()("debug log timed out"); } } catch { // ignore debug logging failures } }; const extractApplyPatchPaths = (patchText) => { if (!patchText || typeof patchText !== "string") { return []; } const paths = []; const seen = new Set(); const lines = patchText.split(/\r?\n/); for (const line of lines) { const match = line.match(/^\*\*\* (?:Update|Add|Delete) File: (.+)$/); if (!match) { continue; } const path = String(match[1] || "").trim(); if (!path || seen.has(path)) { continue; } seen.add(path); paths.push(path); } return paths; }; const windowsPathFlavor = (value, doubleSlashIsUnc = true) => /^[A-Za-z]:[\\/]/.test(value) || /^\\\\/.test(value) || (doubleSlashIsUnc && /^\/\/[^/]+[\\/][^/]+/.test(value)); const hasTraversalSegment = (value) => value.replaceAll("\\", "/").split("/").includes(".."); const normalizeWorkingSetPath = (value, repositoryRoot) => { if (typeof value !== "string" || typeof repositoryRoot !== "string") return null; const candidate = value.trim(); if ( !candidate || candidate.length > MAX_WORKING_SET_PATH_CHARS || hasTraversalSegment(candidate) ) { return null; } const root = repositoryRoot.trim(); const candidateIsAbsolute = posix.isAbsolute(candidate) || win32.isAbsolute(candidate); if (candidateIsAbsolute) { if (!root || hasTraversalSegment(root)) return null; const rootUsesWindows = windowsPathFlavor(root); const candidateUsesWindows = windowsPathFlavor(candidate, rootUsesWindows); if (candidateUsesWindows !== rootUsesWindows) return null; const pathApi = candidateUsesWindows ? win32 : posix; if (!pathApi.isAbsolute(root) || !pathApi.isAbsolute(candidate)) return null; const relative = pathApi.relative(pathApi.normalize(root), pathApi.normalize(candidate)); if ( !relative || relative === ".." || relative.startsWith(`..${pathApi.sep}`) || pathApi.isAbsolute(relative) ) { return null; } const normalized = relative.replaceAll("\\", "/"); return normalized.length <= MAX_WORKING_SET_PATH_CHARS ? normalized : null; } if (/^[A-Za-z]:/.test(candidate)) return null; const normalized = posix.normalize(candidate.replaceAll("\\", "/")).replace(/^\.\//, ""); if ( !normalized || normalized === "." || normalized === ".." || normalized.startsWith("../") || posix.isAbsolute(normalized) || normalized.length > MAX_WORKING_SET_PATH_CHARS ) { return null; } return normalized; }; const addWorkingSetPath = (paths, value, repositoryRoot) => { const normalized = normalizeWorkingSetPath(value, repositoryRoot); if (!normalized) return null; const duplicate = windowsPathFlavor(repositoryRoot) ? Array.from(paths).some((existing) => existing.toLowerCase() === normalized.toLowerCase()) : paths.has(normalized); if (!duplicate) paths.add(normalized); return normalized; }; const appendWorkingSetFileArgs = (args, workingSetFiles) => { if (!Array.isArray(workingSetFiles) || workingSetFiles.length === 0) { return args; } for (const file of workingSetFiles) { const normalized = String(file || "").trim(); if (!normalized || normalized.length > MAX_WORKING_SET_PATH_CHARS) { continue; } args.push("--working-set-file", normalized); } return args; }; const buildInjectQuery = ({ firstPrompt, lastPromptText, projectName, filesModified }) => { const parts = []; if (firstPrompt && String(firstPrompt).trim()) { parts.push(String(firstPrompt).trim()); } if ( lastPromptText && String(lastPromptText).trim() && String(lastPromptText).trim() !== String(firstPrompt || "").trim() && String(lastPromptText).trim().length > 5 ) { parts.push(String(lastPromptText).trim()); } if (projectName) { parts.push(String(projectName)); } const recentFiles = Array.from(filesModified || []) .slice(-5) .map((filePath) => String(filePath || "").split("/").pop()) .filter(Boolean) .join(" "); if (recentFiles) { parts.push(recentFiles); } if (parts.length === 0) { return "recent work"; } const query = parts.join(" "); return query.length > 500 ? query.slice(0, 500) : query; }; const buildPackArgs = ({ query, filesModified, injectLimit, injectTokenBudget, internalLedger = false }) => { const workingSetFiles = Array.from(filesModified || []) .slice(-8) .map((value) => String(value || "").trim()) .filter(Boolean); const args = ["memory", "pack", query, "--json"]; if (internalLedger) { args.push("--internal-ledger"); } if (injectLimit !== null && Number.isFinite(injectLimit) && injectLimit > 0) { args.push("--limit", String(injectLimit)); } if ( injectTokenBudget !== null && Number.isFinite(injectTokenBudget) && injectTokenBudget > 0 ) { args.push("--token-budget", String(injectTokenBudget)); } return appendWorkingSetFileArgs(args, workingSetFiles); }; const parsePackText = (stdout) => { if (!stdout?.trim()) { return ""; } try { const payload = JSON.parse(stdout); return (payload?.pack_text || "").trim(); } catch { return ""; } }; const parsePackMetrics = (stdout) => { if (!stdout?.trim()) { return null; } try { const payload = JSON.parse(stdout); return payload?.metrics || null; } catch { return null; } }; const buildViewerIdentityTarget = (env = process.env, cwd = process.cwd()) => { const normalizeIdentityPath = (value) => { const trimmed = String(value || "").trim(); if (!trimmed) return null; const expanded = trimmed.startsWith("~/") ? join(env.HOME?.trim() || homedir(), trimmed.slice(2)) : trimmed; return resolve(cwd, expanded); }; const embeddingModel = env.CODEMEM_EMBEDDING_MODEL || DEFAULT_EMBEDDING_MODEL; const configuredEmbeddingRevision = env.CODEMEM_EMBEDDING_REVISION?.trim(); // Match core's configured request identity. The runtime resolves mutable refs // to canonical commits before vectors are persisted. let embeddingRevision; if (configuredEmbeddingRevision) { embeddingRevision = configuredEmbeddingRevision; } else { embeddingRevision = embeddingModel === DEFAULT_EMBEDDING_MODEL ? DEFAULT_EMBEDDING_REVISION : null; } return { device_id: env.CODEMEM_DEVICE_ID?.trim() || null, actor_id_present: Object.hasOwn(env, "CODEMEM_ACTOR_ID"), actor_id: env.CODEMEM_ACTOR_ID?.trim() || null, config_path: normalizeIdentityPath(env.CODEMEM_CONFIG), runtime_root: normalizeIdentityPath(env.CODEMEM_RUNTIME_ROOT), workspace_id: env.CODEMEM_WORKSPACE_ID?.trim() || null, home_dir: normalizeIdentityPath(env.HOME || homedir()), pack_compression: env.CODEMEM_PACK_COMPRESSION?.trim() || null, embedding_disabled: ["1", "true", "yes"].includes( String(env.CODEMEM_EMBEDDING_DISABLED || "").toLowerCase(), ), embedding_offline: ["1", "true", "yes"].includes( String(env.CODEMEM_EMBEDDING_OFFLINE || "").toLowerCase(), ), embedding_model: embeddingModel, embedding_revision: embeddingRevision, }; }; const parsePackOutput = (result) => { const succeeded = result?.exitCode === 0; let payload = null; if (succeeded) { try { payload = JSON.parse(result.stdout); } catch { payload = null; } } const ledgerConflict = payload?.ledger_outcome?.ok === false && payload.ledger_outcome.errorCode === "retrieval_ledger_write_failed" && payload.ledger_outcome.reason === "idempotency_conflict"; const metrics = succeeded ? payload?.metrics || null : null; const itemCount = Number.isFinite(Number(metrics?.total_items)) ? Number(metrics.total_items) : null; const candidatePackText = succeeded && itemCount !== 0 ? String(payload?.pack_text || "") : ""; return { conflictPackText: ledgerConflict ? candidatePackText : "", ledgerConflict, metrics, itemCount, // The real builder renders section headings even when it selected no // memories. Treat the structured count as authoritative so headings alone // are never handed to the model as retrieved context. packText: !ledgerConflict ? candidatePackText : "", }; }; const isRecord = (value) => value != null && typeof value === "object" && !Array.isArray(value); const isValidPackHttpPayload = (payload) => { if (!isRecord(payload) || typeof payload.pack_text !== "string" || !isRecord(payload.metrics)) { return false; } const itemCount = payload.metrics.total_items; if (!Number.isInteger(itemCount) || itemCount < 0) { return false; } if ( payload.ledger_artifact_fingerprint != null && !/^[0-9a-f]{64}$/i.test(payload.ledger_artifact_fingerprint) ) { return false; } if (payload.ledger_outcome != null) { return isRecord(payload.ledger_outcome) && payload.ledger_outcome.ok === false && payload.ledger_outcome.errorCode === "retrieval_ledger_write_failed" && payload.ledger_outcome.reason === "idempotency_conflict"; } return true; }; const isValidLedgerHttpPayload = (payload) => isRecord(payload) && payload.ok === true; const isValidLedgerFailureHttpPayload = (payload) => isRecord(payload) && payload.ok === false && ( payload.errorCode === "retrieval_ledger_write_failed" || payload.errorCode === "retrieval_ledger_delivery_write_failed" || payload.errorCode === "automatic_recall_write_failed" ) && typeof payload.reason === "string"; const isViewerDbMismatchPayload = (payload) => isRecord(payload) && isRecord(payload.error) && payload.error.code === "viewer_db_mismatch"; const isViewerIdentityMismatchPayload = (payload) => isRecord(payload) && isRecord(payload.error) && payload.error.code === "viewer_identity_mismatch"; const isViewerContractUnsupportedPayload = (payload) => isRecord(payload) && isRecord(payload.error) && payload.error.code === "viewer_contract_unsupported"; const isViewerInvalidRequestPayload = (payload) => isRecord(payload) && isRecord(payload.error) && payload.error.code === "invalid_request"; const classifyRawEventViewerFailure = (payload) => { if (isViewerDbMismatchPayload(payload)) return "database"; if (isViewerIdentityMismatchPayload(payload)) return "identity"; if (isViewerContractUnsupportedPayload(payload)) return "contract"; if ( isRecord(payload) && isRecord(payload.error) && ["raw_event_queue_full", "raw_event_queue_write_failed"].includes(payload.error.code) ) return "queue"; return "connection"; }; const RAW_EVENT_FAILURE_ACTIONS = Object.freeze({ database: "restart the viewer from the same workspace/config", identity: "restart Codemem and OpenCode with the same environment", contract: "update Codemem on the installed channel, then restart OpenCode", queue: "repair or archive the viewer raw-event queue", connection: "check or restart the viewer", }); // Dependency-free port of @codemem/core prompt-transport semantics. Keep the // range and classifier parity pinned by the plugin injection tests. const PROMPT_TRANSPORT_PROTOCOL_RANGE = Object.freeze({ minSupportedProtocolVersion: 1, protocolVersion: 1, }); const normalizePromptTransportProtocolRange = ( protocolVersion, minSupportedProtocolVersion = undefined, ) => { if (!Number.isSafeInteger(protocolVersion) || protocolVersion < 1) return null; const minimum = minSupportedProtocolVersion === undefined ? protocolVersion : minSupportedProtocolVersion; if (!Number.isSafeInteger(minimum) || minimum < 1 || minimum > protocolVersion) return null; return { minSupportedProtocolVersion: minimum, protocolVersion }; }; const arePromptTransportProtocolRangesCompatible = (left, right) => left.minSupportedProtocolVersion <= right.protocolVersion && right.minSupportedProtocolVersion <= left.protocolVersion; const classifyPromptTransportFailure = ({ kind, compatibleProfile = false }) => { if (kind === "database_mismatch" || kind === "runtime_identity_mismatch") { return "local_fallback"; } if (kind === "invalid_request") { return compatibleProfile ? "terminal" : "fallback"; } if (kind === "policy_failure" || kind === "authorization_failure") { return "terminal"; } if (kind === "viewer_contract_unsupported") { return compatibleProfile ? "terminal" : "fallback"; } return "fallback"; }; const isViewerPolicyOrAuthFailurePayload = (payload) => { let code = null; if (isRecord(payload)) { if (isRecord(payload.error)) code = payload.error.code; else code = payload.error; } return typeof code === "string" && [ "authorization_failed", "forbidden", "policy_denied", "policy_disabled", "unauthorized", ].includes(code); }; const diagnosticTimeoutReason = (signal, fallback) => signal?.aborted && signal.reason?.name === "TimeoutError" ? "timeout" : fallback; const diagnosticStatusClass = (status) => Number.isInteger(status) ? `${Math.floor(status / 100)}xx` : "none"; const viewerHttpDiagnosticReason = ({ operation, status, body, signal }) => { // A timeout is reported only when our own deadline fired, never from raw // exception text or merely observing that a CLI fallback was selected. if (diagnosticTimeoutReason(signal) === "timeout") return "timeout"; if (isViewerDbMismatchPayload(body)) return "database_mismatch"; if (isViewerIdentityMismatchPayload(body)) return "identity_mismatch"; if (isViewerContractUnsupportedPayload(body)) return "contract_unsupported"; if (operation === "prompt-pack-ledger" && isValidLedgerFailureHttpPayload(body)) { return "request_rejected"; } if (status === 401 || status === 403) return "authorization_failure"; if (isViewerPolicyOrAuthFailurePayload(body)) return "policy_failure"; if (isViewerInvalidRequestPayload(body)) { return "request_rejected"; } if (status === 404 || status === 405) return "endpoint_unavailable"; if (Number.isInteger(status) && status >= 500) return "server_failure"; if (Number.isInteger(status)) return "unexpected_status"; return "connection_failure"; }; const viewerProfileFailureKind = (profileBody, dbPath, identityTarget) => { if (!isRecord(profileBody) || profileBody.service !== "codemem-viewer") return "profile_malformed"; const range = normalizePromptTransportProtocolRange( profileBody.protocol_version, profileBody.min_supported_protocol_version, ); if (!range) return "protocol_malformed"; if (!arePromptTransportProtocolRangesCompatible(PROMPT_TRANSPORT_PROTOCOL_RANGE, range)) { return "protocol_range_mismatch"; } if (profileBody.db_path !== dbPath) return "database_mismatch"; if (canonicalJson(profileBody.identity_target) !== canonicalJson(identityTarget)) { return "runtime_identity_mismatch"; } return null; }; const classifyViewerProfileFailure = (profileBody, { operation, dbPath, identityTarget }) => { const kind = viewerProfileFailureKind(profileBody, dbPath, identityTarget); if (!kind) return null; const details = { profile_malformed: ["profile malformed", "profile_malformed"], protocol_malformed: ["protocol range malformed", "profile_malformed"], protocol_range_mismatch: ["protocol range unsupported", "protocol_mismatch"], database_mismatch: ["database mismatch", "database_mismatch"], runtime_identity_mismatch: ["runtime identity mismatch", "identity_mismatch"], }; const [description, reason] = details[kind]; return { classification: viewerFailureClassification( `${operation} viewer ${description}`, classifyPromptTransportFailure({ kind }), ), reason, }; }; const createPromptTransportResult = ({ operation, runner, diagnostics, viewerOnly, timing }) => (result, reason, status = null) => { let selectedRunner = "viewer"; if (!result.ok) { selectedRunner = result.classification.retryable && !viewerOnly ? runner : "none"; } diagnostics.record({ operation, reason, stage: timing.stage, elapsed_ms: performance.now() - timing.startedAt, status_class: diagnosticStatusClass(status), runner: selectedRunner, disposition: result.classification?.disposition, }); return result; }; const viewerFailureClassification = (cause, disposition, kind = null) => ({ cause, disposition, kind, retryable: disposition !== "terminal", }); const classifyViewerHttpFailure = ({ operation, status = null, error = null, malformed = false, body = null, compatibleProfile = false, }) => { if (malformed) { return viewerFailureClassification( `${operation} returned malformed success`, classifyPromptTransportFailure({ kind: "malformed_response" }), ); } if (isViewerDbMismatchPayload(body)) { return viewerFailureClassification( `${operation} viewer database mismatch`, classifyPromptTransportFailure({ kind: "database_mismatch" }), ); } if (isViewerIdentityMismatchPayload(body)) { return viewerFailureClassification( `${operation} viewer runtime identity mismatch`, classifyPromptTransportFailure({ kind: "runtime_identity_mismatch" }), ); } if (isViewerContractUnsupportedPayload(body)) { return viewerFailureClassification( `${operation} viewer contract unsupported`, classifyPromptTransportFailure({ kind: "viewer_contract_unsupported", compatibleProfile, }), "viewer_contract_unsupported", ); } if (operation === "prompt-pack-ledger" && isValidLedgerFailureHttpPayload(body)) { return viewerFailureClassification(`${operation} request rejected (${status})`, "terminal"); } if (status === 401 || status === 403 || isViewerPolicyOrAuthFailurePayload(body)) { return viewerFailureClassification( `${operation} policy or authorization failure (${status})`, classifyPromptTransportFailure({ kind: status === 401 || status === 403 ? "authorization_failure" : "policy_failure", }), ); } if (isViewerInvalidRequestPayload(body)) { return viewerFailureClassification( `${operation} request rejected (${status})`, classifyPromptTransportFailure({ kind: "invalid_request", compatibleProfile }), ); } if (status === 404 || status === 405) { return viewerFailureClassification(`${operation} endpoint unavailable (${status})`, "fallback"); } if (Number.isInteger(status) && status >= 500) { return viewerFailureClassification(`${operation} server failure (${status})`, "fallback"); } if (Number.isInteger(status)) { return viewerFailureClassification(`${operation} unexpected response (${status})`, "fallback"); } const diagnostic = [ error?.name, error?.code, error?.cause?.code, error?.message, error, ].filter(Boolean).join(" "); const timedOut = /AbortError|TimeoutError|timeout|timed out|ETIMEDOUT/i.test(diagnostic); return viewerFailureClassification( timedOut ? `${operation} request timeout` : `${operation} connection failed`, "fallback", ); }; const buildPackHttpBody = ({ query, filesModified, injectLimit, injectTokenBudget, projectName, cwd, dbPath, identityTarget, attempt, automaticContext, }) => ({ context: query, limit: injectLimit !== null && Number.isFinite(injectLimit) && injectLimit > 0 ? Math.trunc(injectLimit) : 10, token_budget: injectTokenBudget !== null && Number.isFinite(injectTokenBudget) && injectTokenBudget > 0 ? Math.trunc(injectTokenBudget) : null, ...(projectName ? { project: projectName } : {}), ...(cwd ? { cwd } : {}), db_path: dbPath, identity_target: identityTarget, working_set_files: Array.from(filesModified || []) .slice(-8) .map((value) => String(value || "").trim()) .filter((value) => value && value.length <= MAX_WORKING_SET_PATH_CHARS), attempt, ...(automaticContext !== undefined ? { automatic_context: automaticContext } : {}), }); const canonicalJson = (value) => { if (Array.isArray(value)) { return `[${value.map((item) => canonicalJson(item)).join(",")}]`; } if (value && typeof value === "object") { return `{${Object.keys(value) .sort() .map((key) => `${JSON.stringify(key)}:${canonicalJson(value[key])}`) .join(",")}}`; } return JSON.stringify(value); }; const promptPackArtifactFingerprint = (stdout, packText) => { try { const payload = JSON.parse(stdout); if (/^[0-9a-f]{64}$/i.test(payload?.ledger_artifact_fingerprint || "")) { return payload.ledger_artifact_fingerprint.toLowerCase(); } return createHash("sha256").update(canonicalJson(payload)).digest("hex"); } catch { return createHash("sha256").update(packText).digest("hex"); } }; const applyInjectedContextToOutput = async ({ injectEnabled, input, output, injectionToastShown, showToast, resolveInjectQuery, buildInjectedContext, confirmDelivery, recordSkipped, }) => { if (!injectEnabled) { recordSkipped?.("injection_disabled", input?.sessionID || null); return false; } // The previous incarnation cached by sessionID-only, so a follow-up // turn whose query happened to match the cache key (e.g. because // lastPromptText hadn't yet been captured by the time // experimental.chat.system.transform fired) would silently re-serve // the first turn's pack. Recompute on every call instead — the chat // path tolerates the transport round trip, and correctness beats the // O(1) hit when the cache key isn't tied to the prompt that produced // the pack. const query = resolveInjectQuery(); const sessionID = input?.sessionID || null; const injected = await buildInjectedContext(query, { sessionID, requestKey: `system:${sessionID || "unknown"}`, surface: "system", }); if (!injected?.text) { return false; } if (!injectionToastShown.has(input.sessionID) && showToast) { injectionToastShown.add(input.sessionID); try { await showToast(buildInjectionToastMessage(injected.metrics)); } catch { // best-effort only } } try { if (!Array.isArray(output.system)) { output.system = []; } output.system.push(injected.text); } catch (error) { if (injected.attemptId) { confirmDelivery?.(injected.attemptId, "failed"); } throw error; } if (injected.attemptId) { confirmDelivery?.(injected.attemptId); } return true; }; const isUserMessageEntry = (entry) => entry?.info?.role === "user"; const resolveEntryMessageId = (entry) => { const id = entry?.info?.id || entry?.parts?.find((part) => part?.messageID)?.messageID; return id ? String(id) : null; }; const fallbackEntryMessageId = (entry, index = 0) => { const id = resolveEntryMessageId(entry); if (id) return id; const text = extractMessageText(entry); if (text) { return `message-${createHash("sha256").update(text).digest("hex").slice(0, 16)}`; } return `message-${index}`; }; const resolveEntrySessionID = (entry) => { const id = entry?.info?.sessionID || entry?.parts?.find((part) => part?.sessionID)?.sessionID; return id ? String(id) : null; }; const isCodememContextPart = (part) => part?.type === "text" && String(part?.id || "").startsWith(CODEMEM_CONTEXT_PART_ID_PREFIX); const extractMessageText = (entry) => { if (!Array.isArray(entry?.parts)) { return ""; } return entry.parts .filter((part) => part?.type === "text" && !isCodememContextPart(part)) .map((part) => String(part?.text || "").trim()) .filter(Boolean) .join("\n") .trim(); }; const findFirstUserMessage = (messages) => { for (let index = 0; index < messages.length; index += 1) { const entry = messages[index]; if (isUserMessageEntry(entry)) { return { entry, index }; } } return null; }; const findLatestUserMessage = (messages) => { for (let index = messages.length - 1; index >= 0; index -= 1) { const entry = messages[index]; if (isUserMessageEntry(entry)) { return { entry, index }; } } return null; }; const markCompactionInjectionSkip = (compactionInjectionSkips, sessionID, now = Date.now()) => { if (!compactionInjectionSkips || !sessionID) return; compactionInjectionSkips.set(String(sessionID), now + COMPACTION_INJECTION_SKIP_TTL_MS); }; const consumeCompactionInjectionSkip = (compactionInjectionSkips, sessionID, now = Date.now()) => { if (!compactionInjectionSkips || !sessionID) return false; const key = String(sessionID); const expiresAt = compactionInjectionSkips.get(key); if (!expiresAt) return false; compactionInjectionSkips.delete(key); return now <= expiresAt; }; // OpenCode does not guarantee that synthetic message parts survive into the // next transform call. Cache each message's injected block by message ID and // re-append the exact same bytes on later turns: older prompts stay stable for // provider prefix caching while only the newest user message triggers a fresh // pack build. The replay cache only activates when OpenCode provides both a // session ID and a real message ID; unidentified messages still receive current // turn context, but are not cached because positional fallbacks can drift across // turns or sessions. Do not "simplify" this into recomputing previous turns. const commitImmediateDelivery = (commit) => commit(); const normalizeInjectedMessageParts = ( messages, sessionCache, { pruneAbsent = true, evictionSnapshot = null, commitDelivery = commitImmediateDelivery } = {}, ) => { const presentIds = new Set(messages.map(resolveEntryMessageId).filter(Boolean)); for (const id of sessionCache?.keys() || []) { if (pruneAbsent && !presentIds.has(id)) sessionCache.delete(id); } for (let index = 0; index < messages.length; index += 1) { const entry = messages[index]; if (!isUserMessageEntry(entry) || !Array.isArray(entry.parts)) { continue; } const messageId = resolveEntryMessageId(entry); const parts = entry.parts.filter(isCodememContextPart); if (parts.length && sessionCache && messageId) { const cached = sessionCache.get(messageId); commitDelivery(() => setSessionMessageInjectionCacheEntry( sessionCache, messageId, { ...cached, text: parts[0].text, parts: parts.map((part) => { const recall = retainedPartRecall(part, cached?.parts); return recall ? { ...part, metadata: { ...part.metadata, codemem: recall } } : { ...part }; }), }, { preserveMessageIDs: presentIds, evictionSnapshot }, )); } } }; const buildInjectedContextPart = (entry, index, text, options = {}) => { const messageId = options.messageId || fallbackEntryMessageId(entry, index); const sessionID = options.sessionID || resolveEntrySessionID(entry) || "unknown"; return { id: `${CODEMEM_CONTEXT_PART_ID_PREFIX}${messageId}`, sessionID, messageID: messageId, type: "text", text, synthetic: true, ...(options.recall ? { metadata: { codemem: options.recall } } : {}), }; }; const appendCachedInjectedContextParts = async ( messages, sessionCache, sessionID = null, options = {}, ) => { let appended = 0; const presentMessageIDs = new Set(messages.map(resolveEntryMessageId).filter(Boolean)); for (let index = 0; index < messages.length; index += 1) { const entry = messages[index]; if (!isUserMessageEntry(entry) || !Array.isArray(entry.parts)) { continue; } const messageId = resolveEntryMessageId(entry); if (!messageId) { continue; } const cached = sessionCache.get(messageId); const text = typeof cached === "string" ? cached : cached?.text; if (!text) { continue; } const commitDelivery = options.commitDelivery || commitImmediateDelivery; commitDelivery(() => { const current = sessionCache.get(messageId); if (!current) return; setSessionMessageInjectionCacheEntry( sessionCache, messageId, current, { preserveMessageIDs: presentMessageIDs, evictionSnapshot: options.evictionSnapshot }, ); }); let attemptId = cached?.attemptId || null; let cacheReuseReady = null; if ( messageId === options.latestMessageId && !options.latestWasBuilt && attemptId && options.recordCacheReuse ) { const reuse = options.recordCacheReuse(cached, { messageId, sessionID, }); attemptId = typeof reuse === "string" ? reuse : reuse?.attemptId || null; cacheReuseReady = typeof reuse === "object" ? reuse?.ready : null; } try { if (!entry.parts.some(isCodememContextPart)) { const parts = cached?.parts || [buildInjectedContextPart(entry, index, text, { messageId, sessionID })]; entry.parts.push(...parts.map((part) => ({ ...part }))); } } catch (error) { if (messageId === options.latestMessageId && attemptId && options.confirmDelivery) { if (cacheReuseReady) { void Promise.resolve(cacheReuseReady) .then(() => options.confirmDelivery(attemptId, "failed")) .catch(() => {}); } else { options.confirmDelivery(attemptId, "failed"); } } throw error; } if (messageId === options.latestMessageId && attemptId && options.confirmDelivery) { if (cacheReuseReady) { void Promise.resolve(cacheReuseReady) .then(() => options.confirmDelivery(attemptId)) .catch(() => {}); } else { options.confirmDelivery(attemptId); } } appended += 1; } return appended; }; const scopeMessagesToSession = (messages, sessionID) => messages.filter((entry) => { const entrySession = resolveEntrySessionID(entry); return !entrySession || !sessionID || entrySession === sessionID; }); const applyInjectedContextToMessages = async ({ injectEnabled, input, output, injectionToastShown, showToast, resolveInjectQuery, buildInjectedContext, messageInjectionCache, emptyRecallCache, compactionInjectionSkips, confirmDelivery, recordCacheReuse, recordSkipped, injectTokenBudget = DEFAULT_INJECT_TOKEN_BUDGET, retainedTokenBudget = DEFAULT_RETAINED_TOKEN_BUDGET, workingSet = [], recordMeasurement, commitDelivery = commitImmediateDelivery, beginFinalization = async () => {}, pruneAbsentCacheEntries = true, requireLatestUserMessageID = false, cacheSuccessfulEmpty = false, surface = "message", }) => { const hasMessages = Array.isArray(output?.messages); const hookSessionID = input?.sessionID || null; const hookMessages = hasMessages ? scopeMessagesToSession(output.messages, hookSessionID) : []; const latestUser = findLatestUserMessage(hookMessages); const sessionID = hookSessionID || (latestUser ? resolveEntrySessionID(latestUser.entry) : null); // Both transforms use the hook session when an entry omits session identity. const messages = scopeMessagesToSession(hookMessages, sessionID); const report = (reason, newTokens = 0, duplicates = 0) => { const systemPart = latestUser?.entry.parts?.find(isCodememContextPart); const measurement = { new_tokens: newTokens, retained_tokens: surface === "system" ? estimateTokens(systemPart?.text || "") : countRetainedInjectionTokens(messages), duplicates_omitted: duplicates, reason, }; try { void Promise.resolve(recordMeasurement?.(measurement)).catch(() => {}); } catch { /* best effort */ } }; if (!injectEnabled) { recordSkipped?.("injection_disabled", sessionID); report("injection_disabled"); return false; } if (!hasMessages) { report("missing_history"); return false; } if (consumeCompactionInjectionSkip(compactionInjectionSkips, sessionID)) { const cache = getSessionMessageInjectionCache(messageInjectionCache, sessionID); normalizeInjectedMessageParts(messages, cache, { pruneAbsent: false, evictionSnapshot: cache ? new Map(cache) : null, commitDelivery, }); recordSkipped?.("compaction_skipped", sessionID); report("compaction_skipped"); return false; } if (!latestUser) { report("missing_user"); return false; } const latestMessageId = resolveEntryMessageId(latestUser.entry); const sessionCache = getSessionMessageInjectionCache(messageInjectionCache, sessionID); const emptySessionCache = cacheSuccessfulEmpty ? getSessionMessageInjectionCache(emptyRecallCache, sessionID) : null; const evictionSnapshot = sessionCache ? new Map(sessionCache) : null; const emptyEvictionSnapshot = emptySessionCache ? new Map(emptySessionCache) : null; normalizeInjectedMessageParts(messages, sessionCache, { pruneAbsent: pruneAbsentCacheEntries, evictionSnapshot, commitDelivery, }); const canReplay = Boolean(sessionCache && latestMessageId); const latestCached = canReplay ? sessionCache.get(latestMessageId) : null; const latestWasEmpty = Boolean(latestMessageId && emptySessionCache?.has(latestMessageId)); const latestWasCached = Boolean(latestCached) || latestUser.entry.parts?.some(isCodememContextPart); if (sessionCache) { try { await appendCachedInjectedContextParts(messages, sessionCache, sessionID, { latestMessageId, confirmDelivery, recordCacheReuse, evictionSnapshot, commitDelivery, }); } catch (error) { report("delivery_failed"); throw error; } } if (requireLatestUserMessageID && !latestMessageId) { recordSkipped?.("missing_message_identity", sessionID); report("missing_message_identity"); return false; } const retainedTokens = countRetainedInjectionTokens(messages); const fullBudget = Math.min(injectTokenBudget, Math.max(0, retainedTokenBudget - retainedTokens)); let latestWasBuilt = false; let newlyDelivered = 0; let duplicates = 0; let reason = latestWasCached ? "replay" : "no_context"; if (!latestWasCached && !latestWasEmpty) { if (reserveContextPrefixBudget(fullBudget) === null) { if (reserveContextPrefixBudget(injectTokenBudget) !== null) { recordSkipped?.("allowance_exhausted", sessionID); } report("allowance_exhausted"); return retainedTokens > 0; } const firstUser = findFirstUserMessage(messages); const workingContext = workingContextDigest(messages, workingSet); const query = resolveInjectQuery({ firstPrompt: firstUser ? extractMessageText(firstUser.entry) : null, lastPromptText: extractMessageText(latestUser.entry), }); let injected = await buildInjectedContext(query, { sessionID, requestKey: latestMessageId || fallbackEntryMessageId(latestUser.entry, latestUser.index), fallbackTurn: sessionID && latestMessageId ? null : latestUser.index, surface, tokenBudget: fullBudget, retainedItems: retainedMemoryFingerprints(messages, sessionCache), retainedMetadata: retainedMetadataGaps(messages, sessionCache), workingContext, continuationOnly: isContinuationOnly(extractMessageText(latestUser.entry), messages, workingContext, sessionCache), }); await beginFinalization(sessionID); // Another transform may have delivered while retrieval was awaiting I/O. if (sessionCache) { await appendCachedInjectedContextParts(messages, sessionCache, sessionID, { latestMessageId, confirmDelivery, recordCacheReuse, evictionSnapshot, commitDelivery, }); } if (injected?.refilter) { const { refilter, ...retrieval } = injected; injected = { ...retrieval, ...refilter(retainedMemoryFingerprints(messages, sessionCache, evictionSnapshot)), }; } if (latestUser.entry.parts?.some(isCodememContextPart)) { duplicates = injected?.duplicates || 0; if (injected?.attemptId) { confirmDelivery?.(injected.attemptId, "unknown", injected.evaluation); } report("replay", 0, duplicates); return true; } const deliveryBudget = Math.min(fullBudget, Math.max( 0, retainedTokenBudget - countRetainedInjectionTokens(messages), )); duplicates = injected?.duplicates || 0; reason = injected?.skipReason || "no_context"; if (injected?.text && estimateTokens(injected.text) <= deliveryBudget) { latestWasBuilt = true; if (Array.isArray(latestUser.entry.parts)) { try { latestUser.entry.parts.push( buildInjectedContextPart(latestUser.entry, latestUser.index, injected.text, { messageId: latestMessageId || undefined, sessionID: sessionID || undefined, recall: injected.recall, }) ); } catch (error) { if (injected.attemptId) { confirmDelivery?.(injected.attemptId, "failed", injected.evaluation); } report("delivery_failed", 0, duplicates); throw error; } newlyDelivered = estimateTokens(injected.text); reason = "delivered"; if (injected.attemptId) { confirmDelivery?.(injected.attemptId, "handed_off", injected.evaluation); } if (canReplay) { commitDelivery(() => setSessionMessageInjectionCacheEntry( sessionCache, latestMessageId, { ...injected, parts: latestUser.entry.parts.filter(isCodememContextPart).map((part) => ({ ...part })), reuseCount: 0, }, { preserveMessageIDs: new Set(messages.map(resolveEntryMessageId).filter(Boolean)), evictionSnapshot, }, )); } } const toastKey = sessionID || latestMessageId || "unknown"; await commitDelivery(async () => { if (injectionToastShown.has(toastKey) || !showToast) return; injectionToastShown.add(toastKey); try { await showToast(buildInjectionToastMessage(injected.metrics)); } catch { return; } }); } else if (injected?.skipReason) { // The retrieval already has an attempt. Withheld output is not a second retrieval. if (injected.attemptId) confirmDelivery?.(injected.attemptId, "unknown", injected.evaluation); } else if (injected?.text) { reason = "budget_rejected"; if (injected.attemptId) confirmDelivery?.(injected.attemptId, "failed", injected.evaluation); } else if (emptySessionCache && latestMessageId && injected?.cacheableEmpty) { commitDelivery(() => setSessionMessageInjectionCacheEntry( emptySessionCache, latestMessageId, true, { evictionSnapshot: emptyEvictionSnapshot }, )); } } report(reason, newlyDelivered, duplicates); if (!canReplay) { return Boolean( Array.isArray(latestUser.entry.parts) && latestUser.entry.parts.some(isCodememContextPart) ); } return latestWasBuilt || retainedTokens > 0; }; const countRetainedInjectionTokens = (messages) => messages.reduce( (sum, entry) => sum + (entry.parts || []).filter(isCodememContextPart) .reduce((tokens, part) => tokens + estimateTokens(part.text), 0), 0, ); const injectionDigest = (text) => createHash("sha256").update(text).digest("hex"); const retainedPartRecall = (part, cachedParts = []) => { const valid = (candidate) => candidate?.metadata?.codemem?.v === 1 && candidate.metadata.codemem.digest === injectionDigest(String(part.text || "")); if (valid(part)) return part.metadata.codemem; const cached = cachedParts.find((candidate) => candidate.id === part.id && candidate.text === part.text && valid(candidate)); return cached?.metadata?.codemem; }; const workingContextDigest = (messages, workingSet) => { const scalar = (value) => typeof value === "string" ? value.slice(0, 256) : null; const hash = createHash("sha256"); for (const path of [...workingSet].filter((value) => typeof value === "string").sort()) { hash.update(JSON.stringify(["file", path.slice(0, MAX_WORKING_SET_PATH_CHARS)])); } for (const entry of messages) { for (const part of entry.parts || []) { if (part.type === "text") continue; // Never traverse args, output, attachments, or arbitrary tool-state payloads. hash.update(JSON.stringify([ scalar(entry.info?.id), scalar(part.type), scalar(part.id), scalar(part.callID), scalar(part.state?.status), ])); } } return hash.digest("hex"); }; const isContinuationOnly = (prompt, messages, workingContext, sessionCache) => { if (!/^(continue|proceed|go on|keep going)[.!]?$/i.test(prompt.trim())) return false; const latest = findLatestUserMessage(messages); for (let index = (latest?.index ?? 0) - 1; index >= 0; index -= 1) { const entry = messages[index]; if (!isUserMessageEntry(entry)) continue; return (entry.parts || []).some((part) => { const recall = retainedPartRecall(part, sessionCache?.get(resolveEntryMessageId(entry))?.parts); return isCodememContextPart(part) && recall?.v === 1 && recall.digest === injectionDigest(String(part.text || "")) && recall.workingContext === workingContext; }); } return false; }; const retainedMemoryFingerprints = (messages, sessionCache, evictionSnapshot = null) => { const retained = new Set(); const collect = (parts, cachedParts = []) => { for (const part of (parts || []).filter(isCodememContextPart)) { const recall = retainedPartRecall(part, cachedParts); if (recall?.v !== 1 || recall.digest !== injectionDigest(String(part.text || ""))) continue; for (const item of Array.isArray(recall.items) ? recall.items : []) { if (isRecord(item) && Number.isSafeInteger(item.id) && item.id > 0 && /^[a-f0-9]{64}$/.test(item.fingerprint)) { retained.add(`${item.id}:${item.fingerprint}`); } } } }; for (const entry of messages) { collect(entry.parts, sessionCache?.get(resolveEntryMessageId(entry))?.parts); } if (evictionSnapshot) { for (const [messageID, cached] of sessionCache?.entries() || []) { if (evictionSnapshot.get(messageID) === cached) continue; collect(cached?.parts, cached?.parts); } } return retained; }; const filterRetainedPack = (stdout, packText, retained = new Set()) => { const fallback = { text: packText, items: [], duplicates: 0, packMetadata: "missing" }; let payload; try { payload = JSON.parse(stdout); } catch { return fallback; } const items = payload?.rendered_items; if (items != null) fallback.packMetadata = "invalid"; // Old transports and malformed metadata remain eligible rather than guessing from prose. if (payload?.pack_text !== packText || !Array.isArray(items) || !items.length) return fallback; if (payload.metrics?.total_items !== items.length) return fallback; const spans = []; const seen = new Set(); for (const item of items) { if (!isRecord(item) || !Number.isSafeInteger(item.id) || item.id <= 0 || seen.has(item.id) || !/^[a-f0-9]{64}$/.test(item.fingerprint) || !Array.isArray(item.spans) || !item.spans.length) return fallback; seen.add(item.id); for (const span of item.spans) { if (!isRecord(span) || !Number.isSafeInteger(span.start) || !Number.isSafeInteger(span.end) || span.start < 0 || span.end <= span.start || span.end > packText.length) return fallback; spans.push({ ...span, omit: retained.has(`${item.id}:${item.fingerprint}`) }); } } if (payload.items != null) { if (!Array.isArray(payload.items) || payload.items.length !== seen.size) return fallback; const selectedIds = new Set(payload.items.map((item) => item?.id)); if (selectedIds.size !== seen.size || [...selectedIds].some((id) => !seen.has(id))) return fallback; } for (const ids of [payload.item_ids, payload.metrics?.pack_item_ids]) { if (ids == null) continue; if (!Array.isArray(ids) || new Set(ids).size !== ids.length || ids.some((id) => !Number.isSafeInteger(id) || id <= 0) || [...seen].some((id) => !ids.includes(id))) return fallback; } spans.sort((a, b) => a.start - b.start); if (spans.some((span, index) => index > 0 && span.start < spans[index - 1].end)) return fallback; const kept = items.filter((item) => !retained.has(`${item.id}:${item.fingerprint}`)); let text = packText; for (const span of spans.reverse()) { if (span.omit) text = text.slice(0, span.start) + text.slice(span.end); } return { text: kept.length ? text : "", items: kept.map(({ id, fingerprint }) => ({ id, fingerprint })), duplicates: items.length - kept.length, packMetadata: "valid", }; }; const retainedMetadataGaps = (messages, sessionCache) => { let missingRetainedMetadata = false; let invalidRetainedMetadata = false; for (const entry of messages) { for (const part of (entry.parts || []).filter(isCodememContextPart)) { const recall = retainedPartRecall(part, sessionCache?.get(resolveEntryMessageId(entry))?.parts); if (recall?.v === 1 && Array.isArray(recall.items) && recall.items.length > 0 && recall.items.every((item) => isRecord(item) && Number.isSafeInteger(item.id) && item.id > 0 && /^[a-f0-9]{64}$/.test(item.fingerprint))) continue; if (part.metadata?.codemem != null || recall != null) invalidRetainedMetadata = true; else missingRetainedMetadata = true; } } return { missingRetainedMetadata, invalidRetainedMetadata }; }; const mapOpencodeEventTypeToAdapterType = (eventType) => { if (eventType === "user_prompt") { return "prompt"; } if (eventType === "assistant_message") { return "assistant"; } if (eventType === "tool.execute.after") { return "tool_result"; } return null; }; const buildOpencodeAdapterPayload = (event) => { const eventType = event?.type; if (eventType === "user_prompt") { const text = String(event?.prompt_text || "").trim(); if (!text) { return null; } return { text, prompt_number: typeof event?.prompt_number === "number" ? event.prompt_number : null, }; } if (eventType === "assistant_message") { const text = String(event?.assistant_text || "").trim(); if (!text) { return null; } return { text, message_id: event?.message_id ? String(event.message_id) : null, }; } if (eventType === "tool.execute.after") { const toolName = String(event?.tool || "unknown"); const toolCallID = event?.tool_call_id ? String(event.tool_call_id) : null; return { tool_name: toolName, status: event?.error ? "error" : "ok", tool_input: event?.args || {}, tool_output: event?.result ?? null, error: event?.error ?? null, ...(toolCallID ? { tool_call_id: toolCallID } : {}), }; } return null; }; const stableStringify = (value) => { if (value === null || typeof value !== "object") { return JSON.stringify(value); } if (Array.isArray(value)) { return `[${value.map(stableStringify).join(",")}]`; } const keys = Object.keys(value).sort(); return `{${keys .map((key) => `${JSON.stringify(key)}:${stableStringify(value[key])}`) .join(",")}}`; }; const stableDigest = (value) => createHash("sha256").update(stableStringify(value)).digest("hex").slice(0, 20); const sanitizeIdPart = (value, fallback, maxChars) => { const normalized = String(value || "") .replace(/[^A-Za-z0-9._:-]/g, "_") .slice(0, maxChars); return normalized || fallback; }; const buildAdapterEventId = ({ sessionID, eventType, event, payload, ts }) => { const safeSessionID = sanitizeIdPart(sessionID, "unknown", 48); const safeType = sanitizeIdPart(eventType, "event", 24); const rawTimestamp = typeof event?.timestamp === "string" && event.timestamp.trim() ? event.timestamp.trim() : ts; const digest = stableDigest({ session_id: String(sessionID || ""), event_type: String(eventType || ""), raw_event_type: String(event?.type || ""), timestamp: rawTimestamp, payload, }); return `oc:${safeSessionID}:${safeType}:${digest}`.slice(0, 128); }; const buildOpencodeAdapterEvent = ({ sessionID, event }) => { if (!sessionID || !event || typeof event !== "object") { return null; } const adapterType = mapOpencodeEventTypeToAdapterType(event.type); if (!adapterType) { return null; } const payload = buildOpencodeAdapterPayload(event); if (!payload) { return null; } const ts = typeof event.timestamp === "string" ? event.timestamp : new Date().toISOString(); return { schema_version: "1.0", source: "opencode", session_id: String(sessionID), event_id: buildAdapterEventId({ sessionID, eventType: adapterType, event, payload, ts, }), event_type: adapterType, ts, ordering_confidence: "low", payload, meta: { original_event_type: String(event.type || "unknown"), }, }; }; const normalizeProjectLabel = (value) => { if (typeof value !== "string") { return null; } const cleaned = value.trim(); if (!cleaned) { return null; } if (cleaned.includes("/") || cleaned.includes("\\")) { const normalized = cleaned.replaceAll("\\", "/").replace(/\/+$/, ""); return basename(normalized) || null; } return cleaned; }; const inferredProjectByCwd = new Map(); const inferProjectFromCwd = (cwd) => { if (typeof cwd !== "string") { return null; } const cleaned = cwd.trim(); if (!cleaned) { return null; } if (inferredProjectByCwd.has(cleaned)) { return inferredProjectByCwd.get(cleaned); } let current = cleaned; while (true) { const gitPath = `${current}/.git`; if (existsSync(gitPath)) { try { const text = readFileSync(gitPath, "utf8").trim(); if (text.startsWith("gitdir:")) { const normalized = resolve(current, text.slice("gitdir:".length).trim()).replaceAll( "\\", "/", ); const worktreeMarker = "/.git/worktrees/"; const worktreeIndex = normalized.indexOf(worktreeMarker); if (worktreeIndex >= 0) { const inferred = normalizeProjectLabel(normalized.slice(0, worktreeIndex)); inferredProjectByCwd.set(cleaned, inferred); return inferred; } } } catch { // .git is a directory in normal repos; fall through to cwd basename. } const inferred = normalizeProjectLabel(current); inferredProjectByCwd.set(cleaned, inferred); return inferred; } const parent = dirname(current); if (parent === current) { const inferred = normalizeProjectLabel(cleaned); inferredProjectByCwd.set(cleaned, inferred); return inferred; } current = parent; } }; const resolveProjectName = (project, cwd) => normalizeProjectLabel(process.env.CODEMEM_PROJECT) || normalizeProjectLabel(project?.name) || normalizeProjectLabel(project?.root) || inferProjectFromCwd(cwd) || null; const selectRawEventId = ({ payload, nextEventId }) => { const fromPayload = payload && typeof payload === "object" && payload._raw_event_id; return String(fromPayload || nextEventId()); }; const buildRawEventEnvelope = ({ sessionID, type, payload, cwd, project, startedAt, nowMs, nowMono, nextEventId, captureContext, hostGeneration, }) => ({ session_stream_id: sessionID, session_id: sessionID, opencode_session_id: sessionID, event_id: selectRawEventId({ payload, nextEventId }), event_type: type, ts_wall_ms: nowMs, ts_mono_ms: nowMono, payload, cwd, project, started_at: startedAt, ...(hostGeneration === "v2" ? { host_generation: "v2" } : {}), ...(captureContext ? { capture_context: captureContext } : {}), }); const trimEventQueue = ({ events, maxEvents, hardMaxEvents, onUnsentPressure, onForcedDrop }) => { if (!Number.isFinite(maxEvents) || maxEvents <= 0) { return; } while (events.length > maxEvents) { const droppableIndex = events.findIndex( (queued) => queued && typeof queued === "object" && queued._raw_enqueued ); if (droppableIndex >= 0) { events.splice(droppableIndex, 1); continue; } if (typeof onUnsentPressure === "function") { onUnsentPressure(events.length, maxEvents); } if ( Number.isFinite(hardMaxEvents) && hardMaxEvents > 0 && events.length > hardMaxEvents ) { const durableIndex = events.findIndex( (queued) => queued && typeof queued === "object" && queued._raw_spooled ); const dropIndex = durableIndex >= 0 ? durableIndex : 0; const [dropped] = events.splice(dropIndex, 1); if (typeof onForcedDrop === "function") { onForcedDrop(dropped, events.length, hardMaxEvents); } continue; } break; } }; const attachAdapterEvent = ({ sessionID, event }) => { if (!event || typeof event !== "object") { return event; } let adapterEvent = null; try { adapterEvent = buildOpencodeAdapterEvent({ sessionID, event }); } catch { return event; } if (!adapterEvent) { return event; } return { ...event, _adapter: adapterEvent, }; }; const asNonNegativeCount = (value) => { if (Array.isArray(value)) { return value.length; } if (typeof value === "number" && Number.isFinite(value)) { return Math.max(0, Math.trunc(value)); } return null; }; const asFiniteNonNegativeInt = (value) => { if (typeof value !== "number" || !Number.isFinite(value)) { return null; } if (value < 0) { return null; } return Math.trunc(value); }; const parsePositiveInt = (value, fallback) => { const parsed = Number.parseInt(String(value ?? ""), 10); if (!Number.isFinite(parsed) || parsed <= 0) { return fallback; } return parsed; }; const estimateTokens = (text) => Math.ceil(String(text || "").length / 4); const resolveInjectTokenBudget = (value) => parsePositiveInt(value, DEFAULT_INJECT_TOKEN_BUDGET); const resolveRetainedTokenBudget = (value) => { const text = String(value ?? "").trim(); const parsed = Number(text); return /^\d+$/.test(text) && Number.isSafeInteger(parsed) && parsed > 0 ? parsed : DEFAULT_RETAINED_TOKEN_BUDGET; }; const reserveContextPrefixBudget = (tokenBudget) => { const packBudget = tokenBudget - estimateTokens(CODEMEM_CONTEXT_PREFIX); return packBudget > 0 ? packBudget : null; }; const wrapInjectedContext = (packText) => `${CODEMEM_CONTEXT_PREFIX}${packText}`; export const buildInjectionToastMessage = (metrics) => { const items = asFiniteNonNegativeInt(metrics?.items) ?? asFiniteNonNegativeInt(metrics?.total_items); const packTokens = asFiniteNonNegativeInt(metrics?.pack_tokens); const avoided = asFiniteNonNegativeInt(metrics?.avoided_work_tokens); const avoidedUnknown = asNonNegativeCount(metrics?.avoided_work_unknown_items); const avoidedKnown = asNonNegativeCount(metrics?.avoided_work_known_items); const addedCount = asNonNegativeCount(metrics?.added_ids); const removedCount = asNonNegativeCount(metrics?.removed_ids); const deltaAvailable = metrics?.pack_delta_available === true; const messageParts = ["codemem injected"]; if (items !== null) messageParts.push(`${items} items`); if (packTokens !== null) messageParts.push(`~${packTokens} tokens`); if ( avoided !== null && avoided > 0 && avoidedKnown !== null && avoidedUnknown !== null && avoidedKnown >= avoidedUnknown ) { messageParts.push(`avoided work ~${avoided} tokens`); } if (deltaAvailable && (addedCount !== null || removedCount !== null)) { messageParts.push(`delta +${addedCount || 0}/-${removedCount || 0}`); } return messageParts.join(" · "); }; const detectRunner = ({ cwd, envRunner }) => { if (envRunner) { return envRunner; } // Prefer the TS codemem if installed globally, fall back to npx try { const versionOutput = execSync("codemem --version", { encoding: "utf-8", timeout: 3000, // Suppress shell "not found" noise when codemem is not on PATH; the // catch below falls back to npx. Without this, stderr leaks to the // terminal on every OpenCode startup for npx-only installs. stdio: ["ignore", "pipe", "ignore"], windowsHide: true, }).trim(); // Use a newer global CLI only when it remains on the plugin's release // channel. This prevents both channel crossing and fallback to an older pin // immediately after a successful prerelease auto-update. if (isSameChannelVersionAtLeast(versionOutput, PINNED_BACKEND_VERSION)) { return "codemem"; } } catch { // not on PATH or timed out } return "npx"; }; // When an explicit npx override targets the codemem package, pair it with the // matching @codemem/embeddings spec so the detached Viewer can resolve the // optional runtime instead of silently falling back to lexical search. Returns // null for any spec that is not a bare `codemem`/`codemem@`, so custom // package overrides pass through unchanged. const pairedEmbeddingsForCodememSpec = (spec) => { if (typeof spec !== "string") return null; const trimmed = spec.trim(); if (trimmed === "codemem") return "@codemem/embeddings"; const match = /^codemem@([^\s]+)$/.exec(trimmed); if (!match || !/^[A-Za-z0-9~^<>=*][A-Za-z0-9._~^<>=*|-]*$/.test(match[1])) return null; return `@codemem/embeddings@${match[1]}`; }; const buildRunnerArgs = ({ runner, runnerFrom, runnerFromExplicit }) => { if (runner === "codemem") { return []; } if (runner === "npx") { if (runnerFromExplicit) { const pairedEmbeddings = pairedEmbeddingsForCodememSpec(runnerFrom); if (pairedEmbeddings) { return [ "-y", "--package", runnerFrom, "--package", pairedEmbeddings, "codemem", ]; } return ["-y", runnerFrom]; } return [ "-y", "--package", `codemem@${PINNED_BACKEND_VERSION}`, "--package", `@codemem/embeddings@${PINNED_BACKEND_VERSION}`, "codemem", ]; } if (runner === "node") { const cliPath = runnerFromExplicit ? runnerFrom : join(runnerFrom, "packages/cli/dist/index.js"); return [cliPath]; } // Custom runner via CODEMEM_RUNNER env — pass through as-is return runnerFromExplicit ? [runnerFrom] : []; }; const resolveRuntimeRunner = ({ cwd, log }) => { const runner = detectRunner({ cwd, envRunner: process.env.CODEMEM_RUNNER }); const runnerFromExplicit = Boolean(String(process.env.CODEMEM_RUNNER_FROM || "").trim()); const runnerFrom = process.env.CODEMEM_RUNNER_FROM || cwd; const runnerArgs = buildRunnerArgs({ runner, runnerFrom, runnerFromExplicit }); const cliRunner = createCachedCliRunner({ runner, runnerArgs, pin: PINNED_BACKEND_VERSION, cwd, explicit: Object.hasOwn(process.env, "CODEMEM_RUNNER") || Object.hasOwn(process.env, "CODEMEM_RUNNER_FROM"), isCompatibleVersion: isSameChannelVersionAtLeast, warn: (reason) => log("warn", "codemem default CLI runner could not be reused", { reason }), }); return { runner, runnerArgs, runnerFrom, runnerFromExplicit, cliRunner }; }; const createViewerCliArgs = ({ viewerHost, viewerPort, viewerDbPath, viewerConfigPath }) => (action) => { const args = ["serve", action, "--host", viewerHost, "--port", viewerPort]; if (String(viewerDbPath || "").trim()) args.push("--db-path", viewerDbPath); if (String(viewerConfigPath || "").trim()) args.push("--config", viewerConfigPath); return args; }; /** The CLI-backed `mem-*` status tools every plugin instance registers. */ const createCliStatusTools = ({ runCli, viewerUrlHost, viewerPort, logPath }) => ({ "mem-status": { description: "Show codemem stats and recent entries", args: {}, async execute() { const stats = await runCli(["memory", "stats"]); const recent = await runCli(["memory", "list", "--limit", "5"]); const lines = [ `viewer: http://${viewerUrlHost}:${viewerPort}`, `log: ${logPath || "disabled"}`, ]; if (stats.exitCode === 0 && stats.stdout.trim()) { lines.push("", "stats:", stats.stdout.trim()); } if (recent.exitCode === 0 && recent.stdout.trim()) { lines.push("", "recent:", recent.stdout.trim()); } return lines.join("\n"); }, }, "mem-recent": { description: "Show recent codemem entries", args: { limit: { type: "number", optional: true }, }, async execute({ limit }) { // Number.isFinite accepts floats and negatives; coerce to a positive // integer (default 5) before forwarding to the CLI. const safeLimit = String(parsePositiveInt(limit, 5)); const recent = await runCli(["memory", "list", "--limit", safeLimit]); if (recent.exitCode === 0) { return recent.stdout.trim() || "No recent memories."; } return `Failed to fetch recent: ${recent.stderr || recent.exitCode}`; }, }, "mem-stats": { description: "Show codemem stats", args: {}, async execute() { const stats = await runCli(["memory", "stats"]); if (stats.exitCode === 0) { return stats.stdout.trim() || "No stats yet."; } return `Failed to fetch stats: ${stats.stderr || stats.exitCode}`; }, }, }); /** Native memory tools, or null unless CODEMEM_OPENCODE_NATIVE_TOOLS enables them. */ const createRuntimeMemoryTools = ({ viewerEnabled, viewerBaseUrl, dbPath, identityTarget, runCli, location, log, notify, }) => { if (!nativeMemoryToolsEnabled()) return null; return createNativeMemoryTools({ executor: createMemoryToolExecutor({ viewerEnabled, viewerBaseUrl, dbPath, identityTarget, // The same target proof prompt recall uses. isProfileMatch: (profile) => viewerProfileFailureKind(profile, dbPath, identityTarget) === null, runCli, defaultDirectory: resolve(location.directory || location.cwd), }), log, notify, }); }; export const createCodememRuntime = async ({ location, host, hostGeneration = null }) => { const { project, directory, worktree } = location; const hostLog = typeof host?.log === "function" ? host.log : async () => {}; const hostNotify = typeof host?.notify === "function" ? host.notify : null; const events = []; const flushingBatches = new Set(); // Session boundaries retain distinct owners but share one serialized drain. const pendingBoundaryBatches = []; let boundaryDrain = null; let deferredFlush = null; const queuedEventOrder = new WeakMap(); let nextQueuedEventOrder = 0; const maxEvents = parsePositiveInt(process.env.CODEMEM_PLUGIN_MAX_EVENTS, 200); const maxChars = Number.parseInt( process.env.CODEMEM_PLUGIN_MAX_EVENT_CHARS || "8000", 10 ); const cwd = worktree || directory || process.cwd(); const debug = envHasValue(process.env.CODEMEM_PLUGIN_DEBUG, TRUTHY_VALUES); const debugExtraction = envHasValue( process.env.CODEMEM_DEBUG_EXTRACTION, TRUTHY_VALUES ); const logTimeoutMs = Number.parseInt( process.env.CODEMEM_PLUGIN_LOG_TIMEOUT_MS || "1500", 10 ); const logPathEnvRaw = process.env.CODEMEM_PLUGIN_LOG || ""; const logPath = resolveLogPath(logPathEnvRaw, cwd, process.env.HOME); const errorLogPath = resolveErrorLogPath(cwd, process.env.HOME); const logLine = createLogLine(logPath); const errorLogLine = createLogLine(errorLogPath); const promptTransportDiagnostics = createPromptTransportDiagnostics({ logLine: errorLogLine }); const log = createDebugLogger({ debug, host: { log: hostLog }, logTimeoutMs, getLogLine: () => logLine, getErrorLogLine: () => errorLogLine, }); const pluginIgnored = envHasValue( process.env.CODEMEM_PLUGIN_IGNORE, TRUTHY_VALUES ); if (pluginIgnored) { return null; } const releasePluginRegistration = claimPluginRegistration(registrationDirectoryFor({ directory, cwd, hostGeneration })); if (!releasePluginRegistration) { await log("warn", "codemem duplicate plugin registration skipped", { next_action: "remove either the configured npm plugin or the project-local Codemem plugin", }); return null; } const { runner, runnerArgs, runnerFrom, runnerFromExplicit, cliRunner } = resolveRuntimeRunner({ cwd, log }); const viewerEnabled = envNotDisabled(process.env.CODEMEM_VIEWER || "1"); const viewerAutoStart = envNotDisabled( process.env.CODEMEM_VIEWER_AUTO || "1" ); // Opt-in: the viewer is shared by every OpenCode project on this machine, so // one session ending must not stop it for the others. const viewerAutoStop = envHasValue(process.env.CODEMEM_VIEWER_AUTO_STOP, TRUTHY_VALUES); const viewerHost = process.env.CODEMEM_VIEWER_HOST || "127.0.0.1"; const viewerPort = process.env.CODEMEM_VIEWER_PORT || "38888"; const viewerDbPath = process.env.CODEMEM_DB || ""; const expandedViewerDbPath = viewerDbPath.startsWith("~/") ? join(process.env.HOME?.trim() || homedir(), viewerDbPath.slice(2)) : viewerDbPath; const promptPackDbPath = resolve( cwd, expandedViewerDbPath || join(homedir(), ".codemem", "mem.sqlite"), ); const promptPackIdentityTarget = buildViewerIdentityTarget(process.env, cwd); const viewerConfigPath = process.env.CODEMEM_CONFIG || ""; // A malformed value (e.g. "abc", "", "-1") falls back to the 20s default // instead of NaN, which would silently disable the timeout. An explicit "0" // is preserved as the intentional opt-out (disable the timeout, e.g. for slow // first-run npx installs/backend updates) — the `commandTimeout > 0` guard at // the call site skips installing the timer when it is 0. const rawCommandTimeout = String(process.env.CODEMEM_PLUGIN_CMD_TIMEOUT ?? "").trim(); const commandTimeout = rawCommandTimeout === "0" ? 0 : parsePositiveInt(rawCommandTimeout, 20000); const promptPackHttpTimeout = parsePositiveInt(process.env.CODEMEM_INJECT_HTTP_MAX_TIME_S || "2", 2) * 1000; const backendUpdatePolicy = parseBackendUpdatePolicy( process.env.CODEMEM_BACKEND_UPDATE_POLICY || "notify" ); const parseNumber = (value, fallback) => { const parsed = Number.parseInt(value, 10); return Number.isFinite(parsed) ? parsed : fallback; }; const injectEnabled = envNotDisabled( process.env.CODEMEM_INJECT_CONTEXT || "1" ); const injectSurface = resolveInjectSurface(process.env.CODEMEM_INJECT_SURFACE); // The item limit remains optional so the CLI can use its configured default. const injectLimitEnv = process.env.CODEMEM_INJECT_LIMIT; const injectLimit = injectLimitEnv ? parseNumber(injectLimitEnv, null) : null; const injectTokenBudget = resolveInjectTokenBudget( process.env.CODEMEM_INJECT_TOKEN_BUDGET ); const injectPackTokenBudget = reserveContextPrefixBudget(injectTokenBudget); const retainedTokenBudget = resolveRetainedTokenBudget(process.env.CODEMEM_INJECT_RETAINED_TOKEN_BUDGET); if (injectEnabled && injectPackTokenBudget === null) { await log( "warn", "codemem context injection skipped: token budget is too small", { inject_token_budget: injectTokenBudget, required_prefix_tokens: estimateTokens(CODEMEM_CONTEXT_PREFIX), next_action: "increase CODEMEM_INJECT_TOKEN_BUDGET or unset it to use the default", } ); } const compactionInjectionSkips = new Map(); let sessionStartedAt = null; let sessionStartSessionID = null; let activeSessionID = null; let viewerStarted = false; let viewerStartInFlight = false; let compatibilityAutoUpdateAttempted = false; let promptCounter = 0; let lastPromptText = null; const capturedPrompts = new Set(); const pendingPrompts = new Map(); const capturedAssistantMessages = new Set(); const assistantUsageCaptured = new Set(); const failedToolCaptured = new Set(); // Track message roles and accumulated text by messageID const messageRoles = new Map(); const messageTexts = new Map(); const promptPartsByMessage = new Map(); const rawEventsEnabled = envNotDisabled( process.env.CODEMEM_RAW_EVENTS || "1" ); const viewerUrlHost = viewerHost.includes(":") && !viewerHost.startsWith("[") ? `[${viewerHost}]` : viewerHost; const rawEventsUrl = `http://${viewerUrlHost}:${viewerPort}/api/raw-events`; const rawEventsStatusUrl = `http://${viewerUrlHost}:${viewerPort}/api/raw-events/status?limit=0`; const packUrl = `http://${viewerUrlHost}:${viewerPort}/api/pack`; const promptPackProfileUrl = `http://${viewerUrlHost}:${viewerPort}/api/prompt-pack-profile`; const promptPackLedgerUrl = `http://${viewerUrlHost}:${viewerPort}/api/prompt-pack-ledger`; const viewerHealthUrl = `http://${viewerUrlHost}:${viewerPort}/api/health`; const rawEventsBackoffMs = parseNumber( process.env.CODEMEM_RAW_EVENTS_BACKOFF_MS || "10000", 10000 ); const rawEventsStatusCheckMs = parseNumber( process.env.CODEMEM_RAW_EVENTS_STATUS_CHECK_MS || "30000", 30000 ); const rawEventsHardMax = parseNumber( process.env.CODEMEM_RAW_EVENTS_HARD_MAX || "2000", 2000 ); const rawEventQueueCapacity = Math.max(maxEvents, rawEventsHardMax); const rawEventSpoolDrainLimit = parsePositiveInt( process.env.CODEMEM_RAW_EVENT_SPOOL_DRAIN_LIMIT, DEFAULT_DRAIN_LIMIT, ); const rawEventSpoolMaxEntries = parsePositiveInt( process.env.CODEMEM_RAW_EVENT_SPOOL_MAX_ENTRIES, DEFAULT_MAX_ENTRIES, ); const rawEventSpoolHome = process.env.HOME?.trim() || homedir(); let runtimeActive = true; let rawEventDelivery = null; const rawEventPreparations = new WeakMap(); const rawEventDeliveries = new WeakMap(); const capacityDeferredEvents = new Set(); let rawEventDeliveryTail = Promise.resolve(); let rawEventDeliveryCount = 0; let flushDeliveryTail = Promise.resolve(); let promptPackTransportUnavailableUntil = 0; const nextEventId = () => randomUUID(); const buildViewerCliArgs = createViewerCliArgs({ viewerHost, viewerPort, viewerDbPath, viewerConfigPath }); // Session context tracking for comprehensive memories const createSessionContext = () => ({ firstPrompt: null, promptCount: 0, toolCount: 0, startTime: null, filesModified: new Set(), filesRead: new Set(), }); let sessionContext = createSessionContext(); const resetSessionContext = () => { sessionContext = createSessionContext(); }; const mergeSessionContext = (detachedContext) => { const detachedStartedFirst = detachedContext.startTime != null && (sessionContext.startTime == null || detachedContext.startTime <= sessionContext.startTime); if (detachedContext.firstPrompt && (!sessionContext.firstPrompt || detachedStartedFirst)) { sessionContext.firstPrompt = detachedContext.firstPrompt; } if (detachedStartedFirst) { sessionContext.startTime = detachedContext.startTime; } sessionContext.promptCount += detachedContext.promptCount; sessionContext.toolCount += detachedContext.toolCount; for (const path of detachedContext.filesModified) { sessionContext.filesModified.add(path); } for (const path of detachedContext.filesRead) { sessionContext.filesRead.add(path); } }; // Check if we should force flush immediately (threshold-based) const shouldForceFlush = () => { const { toolCount, promptCount } = sessionContext; // Force flush if we've accumulated a lot of work if (toolCount >= 50 || promptCount >= 15) { return true; } // Force flush if session has been running for 10+ minutes if (sessionContext.startTime) { const sessionDurationMs = Date.now() - sessionContext.startTime; if (sessionDurationMs >= 600000) { // 10 minutes return true; } } return false; }; const updateActivity = () => {}; const promptMessageKey = (sessionID, messageID) => `${sessionID || activeSessionID || "unknown"}:${String(messageID)}`; const accumulatePromptPart = (part) => { const key = promptMessageKey(part.sessionID, part.messageID); const parts = promptPartsByMessage.get(key) || new Map(); parts.set(String(part.id || "text"), part.text); promptPartsByMessage.set(key, parts); return [...parts.values()].join("").trim(); }; const promptIdentityForEvent = (event) => { const info = event?.messageInfo; if (event?.type === "message.updated" && info?.role === "user" && info.id) { return promptMessageKey(info.sessionID || event.sessionID, info.id); } const part = event?.part; if ( event?.type === "message.part.updated" && part?.messageID && messageRoles.get(part.messageID) === "user" ) { return promptMessageKey(part.sessionID || event.sessionID, part.messageID); } return null; }; const clearPromptPartsForSession = (sessionID) => { const resolvedSessionID = sessionID || activeSessionID; if (!resolvedSessionID) { return; } const prefix = `${resolvedSessionID}:`; for (const key of promptPartsByMessage.keys()) { if (key.startsWith(prefix)) { promptPartsByMessage.delete(key); } } }; const clearPromptSession = (sessionID, { preserveCapturedPrompts = false } = {}) => { if (!sessionID) { return; } const prefix = `${sessionID}:`; if (!preserveCapturedPrompts) { for (const key of capturedPrompts) { if (key.startsWith(prefix)) { capturedPrompts.delete(key); } } } for (const key of pendingPrompts.keys()) { if (key.startsWith(prefix)) { pendingPrompts.delete(key); } } for (const key of capturedAssistantMessages) { if (key.startsWith(prefix)) { capturedAssistantMessages.delete(key); } } for (const key of assistantUsageCaptured) { if (key.startsWith(prefix)) { assistantUsageCaptured.delete(key); } } clearPromptPartsForSession(sessionID); }; const extractPrompt = (event) => { if (!event) { return null; } // For message.updated events, track the role and check if we have buffered text if (event.type === "message.updated" && event.messageInfo) { const info = event.messageInfo; if (info.id && info.role) { messageRoles.set(info.id, info.role); const promptKey = promptMessageKey(info.sessionID, info.id); const promptParts = promptPartsByMessage.get(promptKey); if (info.role === "user" && (promptParts?.size || messageTexts.has(info.id))) { const text = promptParts?.size ? [...promptParts.values()].join("").trim() : messageTexts.get(info.id).trim(); messageTexts.delete(info.id); // Clean up if (debugExtraction) { logLine( `user prompt captured from buffered text id=${info.id.slice( -8 )} len=${text.length}` ); } return { messageID: String(info.id), text }; } if (info.role !== "user") { promptPartsByMessage.delete(promptKey); } } return null; } // For message.part.updated events, accumulate or return text based on known role if (event.type === "message.part.updated" && event.part) { const part = event.part; if (part.type !== "text" || !part.text) { return null; } const role = messageRoles.get(part.messageID); if (role === "user") { const text = accumulatePromptPart(part); if (debugExtraction) { logLine( `user prompt captured immediately id=${part.messageID.slice( -8 )} len=${text.length}` ); } if (!text) { return null; } return { messageID: String(part.messageID), text }; } else if (!role) { // Buffer this text until we know the role accumulatePromptPart(part); const existing = messageTexts.get(part.messageID) || ""; messageTexts.set(part.messageID, existing + part.text); if (debugExtraction) { logLine( `buffering text for unknown role id=${part.messageID.slice( -8 )} len=${(existing + part.text).length}` ); } } } return null; }; const extractAssistantText = (event) => { if (!event) { return null; } // Only capture assistant messages when complete (message.updated with finish) if (event.type === "message.updated" && event.messageInfo) { const info = event.messageInfo; if (info.id && info.role) { messageRoles.set(info.id, info.role); // Log when we see an assistant message.updated (debug only) if (debugExtraction && info.role === "assistant") { logLine( `assistant message.updated id=${info.id.slice( -8 )} finish=${!!info.finish} hasText=${messageTexts.has( info.id )} textLen=${messageTexts.get(info.id)?.length || 0}` ); } // Only return assistant text when message is finished if ( info.role === "assistant" && (info.finish || info.time?.completed) && messageTexts.has(info.id) ) { const text = messageTexts.get(info.id); messageTexts.delete(info.id); // Clean up const assistantText = text.trim(); return assistantText ? { messageID: String(info.id), text: assistantText } : null; } } return null; } // For message.part.updated, store the latest text (don't capture yet) // Store for ALL messages regardless of role - role might not be known yet if (event.type === "message.part.updated" && event.part) { const part = event.part; if (part.type === "text" && part.text) { // Store latest text, will be captured on finish (for assistant) or on role discovery (for user) if (debugExtraction) { const prevLen = messageTexts.get(part.messageID)?.length || 0; logLine( `text part stored id=${part.messageID.slice( -8 )} prevLen=${prevLen} newLen=${part.text.length} role=${ messageRoles.get(part.messageID) || "unknown" }` ); } messageTexts.set(part.messageID, part.text); } } return null; }; const normalizeUsage = (usage) => { if (!usage || typeof usage !== "object") { return null; } const currentTokens = usage.tokens && typeof usage.tokens === "object" ? usage.tokens : null; const currentCache = currentTokens?.cache && typeof currentTokens.cache === "object" ? currentTokens.cache : null; const inputTokens = Number(currentTokens?.input ?? usage.input_tokens ?? 0); const outputTokens = Number(currentTokens?.output ?? usage.output_tokens ?? 0); const cacheCreationTokens = Number( currentCache?.write ?? usage.cache_creation_input_tokens ?? 0 ); const cacheReadTokens = Number(currentCache?.read ?? usage.cache_read_input_tokens ?? 0); const total = inputTokens + outputTokens + cacheCreationTokens; if (!Number.isFinite(total) || total <= 0) { return null; } return { input_tokens: inputTokens, output_tokens: outputTokens, cache_creation_input_tokens: cacheCreationTokens, cache_read_input_tokens: cacheReadTokens, }; }; const extractAssistantUsage = (event) => { if (event?.type !== "message.updated" || !event.messageInfo) { return null; } const info = event.messageInfo; if (!info.id || info.role !== "assistant" || (!info.finish && !info.time?.completed)) { return null; } const usageIdentity = promptMessageKey(info.sessionID || event.sessionID, info.id); if (assistantUsageCaptured.has(usageIdentity)) { return null; } const usage = normalizeUsage( info.tokens ? info : info.usage || event.usage ); if (!usage) { return null; } assistantUsageCaptured.add(usageIdentity); return { usage, id: info.id }; }; const startViewer = async () => { if (!viewerEnabled || !viewerAutoStart || viewerStarted || viewerStartInFlight) { if (viewerStarted) logLine("viewer already started, skipping auto-start").catch(() => {}); return; } // Monitor the shared viewer whether or not this instance ends up spawning it. watchViewerHealth(); viewerStartInFlight = true; let existingViewer = false; try { const existing = await fetch(viewerHealthUrl, { method: "GET", redirect: "manual", signal: AbortSignal.timeout(1_000), }); existingViewer = existing.ok; } catch { // No live viewer responded; proceed with the plugin-owned start. } if (existingViewer) { viewerStartInFlight = false; logLine("viewer already running, skipping plugin-owned auto-start").catch(() => {}); return; } const viewerArgs = buildViewerCliArgs("start"); try { const cmd = await cliRunner.command(viewerArgs); if (!cmd || !runtimeActive) { viewerStartInFlight = false; return; } logLine("auto-starting viewer").catch(() => {}); const child = nodeSpawn(cmd[0], cmd.slice(1), { cwd, env: process.env, detached: true, stdio: "ignore", windowsHide: true, }); child.once("spawn", () => { viewerStarted = true; viewerStartInFlight = false; }); child.on("error", (err) => { viewerStartInFlight = false; logLine(`viewer spawn error: ${err.message}`).catch(() => {}); }); child.unref(); } catch (err) { viewerStartInFlight = false; logLine(`viewer spawn failed: ${err}`).catch(() => {}); } }; // Options may set cwd, env, signal, stdinText, and timeoutMs; see runChildCommand. const runCommand = async (cmd, options = {}) => runChildCommand(cmd, options, { cwd, timeoutMs: commandTimeout }); const runCli = (args, options = {}) => { const command = cliRunner.command(args); if (Array.isArray(command)) return runCommand(command, options); return command.then((cmd) => { if (!cmd) return { exitCode: 1, stdout: "", stderr: "CLI runner unavailable" }; return runCommand(cmd, options); }); }; const memoryTools = createRuntimeMemoryTools({ viewerEnabled, viewerBaseUrl: `http://${viewerUrlHost}:${viewerPort}`, dbPath: promptPackDbPath, identityTarget: promptPackIdentityTarget, runCli, location: { directory, cwd }, log, notify: hostNotify, }); const installCliUpdate = async (command) => { const result = await runCommand(command, { env: resolveAutoUpdateEnvironment(), timeoutMs: 480_000 }); if (result?.exitCode === 0) cliRunner.invalidate(); return result; }; rawEventDelivery = createRawEventDelivery({ backoffMs: rawEventsBackoffMs, buildEnvelope: (input) => buildRawEventEnvelope({ ...input, hostGeneration }), classifyViewerFailure: classifyRawEventViewerFailure, cwd, discardResponseBody, enabled: rawEventsEnabled, failureActions: RAW_EVENT_FAILURE_ACTIONS, fetchRawEventsStatus, hostLog, hostNotify, identityTarget: promptPackIdentityTarget, isActive: () => runtimeActive, logLine, nextEventId, projectName: resolveProjectName(project, cwd), promptPackDbPath, rawEventsStatusTimeoutMs: RAW_EVENTS_STATUS_TIMEOUT_MS, rawEventsStatusUrl, rawEventsUrl, sessionStartedAt: () => sessionStartedAt, statusCheckMs: rawEventsStatusCheckMs, spoolDrainLimit: rawEventSpoolDrainLimit, spoolHome: rawEventSpoolHome, spoolMaxEntries: rawEventSpoolMaxEntries, }); const postViewerJson = async ({ url, operation, payload, validate, viewerOnly = false }) => { const timing = { startedAt: performance.now(), stage: "skipped" }; let signal; const finish = createPromptTransportResult({ operation, runner, diagnostics: promptTransportDiagnostics, viewerOnly, timing, }); if (!viewerEnabled) { return finish({ ok: false, classification: viewerFailureClassification( `${operation} viewer transport disabled`, "fallback", ), }, "disabled"); } if (Date.now() < promptPackTransportUnavailableUntil) { return finish({ ok: false, classification: viewerFailureClassification( `${operation} viewer transport in backoff`, "fallback", ), }, "backoff"); } let response; try { timing.stage = "profile"; signal = AbortSignal.timeout(promptPackHttpTimeout); const profileResponse = await fetch(promptPackProfileUrl, { method: "GET", redirect: "manual", signal, }); let profileBody; try { profileBody = await profileResponse.json(); } catch { profileBody = null; } if (!profileResponse.ok) { const classification = classifyViewerHttpFailure({ operation: `${operation} profile`, status: profileResponse.status, body: profileBody, }); if (classification.retryable) { promptPackTransportUnavailableUntil = Date.now() + Math.max(1000, rawEventsBackoffMs); } return finish({ ok: false, classification }, viewerHttpDiagnosticReason({ operation: `${operation} profile`, status: profileResponse.status, body: profileBody, signal, }), profileResponse.status); } const profileFailure = classifyViewerProfileFailure(profileBody, { operation, dbPath: promptPackDbPath, identityTarget: promptPackIdentityTarget, }); if (profileFailure) { promptPackTransportUnavailableUntil = Date.now() + Math.max(1000, rawEventsBackoffMs); return finish({ ok: false, classification: profileFailure.classification, }, diagnosticTimeoutReason(signal, profileFailure.reason), profileResponse.status); } timing.stage = "request"; signal = AbortSignal.timeout(promptPackHttpTimeout); response = await fetch(url, { method: "POST", redirect: "manual", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), signal, }); } catch (error) { const classification = classifyViewerHttpFailure({ operation, error }); promptPackTransportUnavailableUntil = Date.now() + Math.max(1000, rawEventsBackoffMs); return finish({ ok: false, classification }, viewerHttpDiagnosticReason({ operation, signal })); } let body; try { body = await response.json(); } catch { body = null; } if (!response.ok) { const classification = classifyViewerHttpFailure({ operation, status: response.status, body, compatibleProfile: true, }); if (classification.retryable) { promptPackTransportUnavailableUntil = Date.now() + Math.max(1000, rawEventsBackoffMs); } return finish({ ok: false, classification }, viewerHttpDiagnosticReason({ operation, status: response.status, body, signal, }), response.status); } if (!validate(body)) { const classification = classifyViewerHttpFailure({ operation, malformed: true }); promptPackTransportUnavailableUntil = Date.now() + Math.max(1000, rawEventsBackoffMs); return finish({ ok: false, classification }, diagnosticTimeoutReason(signal, "malformed_response"), response.status); } promptPackTransportUnavailableUntil = 0; return finish({ ok: true, body }, "success", response.status); }; const runPromptPackLedger = async (payload, { viewerOnly = false } = {}) => { const viewerPayload = { ...payload, db_path: promptPackDbPath, identity_target: promptPackIdentityTarget, }; const httpResult = await postViewerJson({ url: promptPackLedgerUrl, operation: "prompt-pack-ledger", viewerOnly, payload: viewerPayload, validate: isValidLedgerHttpPayload, }); if (httpResult.ok) { return { exitCode: 0, stdout: JSON.stringify(httpResult.body), stderr: "", transport: "viewer", }; } const { cause, kind, retryable } = httpResult.classification; await logLine( `inject.ledger.http_error cause=${JSON.stringify(redactLog(cause, 200))} retryable=${retryable}` ); if (!retryable || viewerOnly) { return { exitCode: 1, stdout: "", stderr: cause, transport: "viewer", failureKind: kind }; } try { const result = await runCli(["prompt-pack-ledger"], { stdinText: JSON.stringify(payload), }); return { ...result, transport: "cli" }; } catch { return null; } }; const recallLifecycle = createRecallLifecycle({ createIdentity: promptPackIdentity, emptyQueryHash: hashPromptPackQuery(""), runPromptPackLedger, logLine, getPromptCounter: () => promptCounter, defaultSurface: injectSurface, }); const { injectionToastShown, messageInjectionCache, emptyRecallCache, attemptMetadata, recordSkippedPromptPack, recordCachedPromptPack, confirmPromptPackDelivery, createTransformDelivery, advancePromptPackRetryIdentity, getPromptPackRetryCount, getSuccessfulPromptPackArtifact, rememberSuccessfulPromptPackArtifact, } = recallLifecycle; const showToast = async (message, variant = "warning") => { if (backendUpdatePolicy === "off") { return; } if (!hostNotify) { return; } try { await hostNotify({ message, variant }); } catch { // best-effort only } }; const restartViewerAfterAutoUpdate = async () => { if (!viewerEnabled || !viewerAutoStart || !viewerStarted) { return { attempted: false, ok: false }; } const restartResult = await runCli(buildViewerCliArgs("restart"), { timeoutMs: 60_000 }); if (restartResult?.exitCode === 0) { await logLine("compat.auto_update_viewer_restart ok"); return { attempted: true, ok: true }; } await logLine( `compat.auto_update_viewer_restart_failed exit=${restartResult?.exitCode ?? "unknown"} stderr=${redactLog( (restartResult?.stderr || "").trim() )}` ); return { attempted: true, ok: false }; }; const verifyCliCompatibility = async () => { const minVersion = process.env.CODEMEM_MIN_VERSION || "0.9.20"; const cacheKey = resolveCompatCheckCacheKey({ backendUpdatePolicy, minVersion, runner, runnerFrom, }); const cachedVersion = readCompatCheckCache(cacheKey); if (cachedVersion && isVersionAtLeast(cachedVersion, minVersion)) { await logLine(`compat.version_check_cached current=${cachedVersion} required=${minVersion}`); return; } const versionResult = await runCli(["--version"]); if (versionResult?.exitCode !== 0) { await logLine( `compat.version_check_failed exit=${versionResult?.exitCode ?? "unknown"} stderr=${ versionResult?.stderr ? redactLog(versionResult.stderr.trim()) : "" }` ); return; } const currentVersion = (versionResult.stdout || "").trim(); const parsedCurrent = parseSemver(currentVersion); const parsedMinimum = parseSemver(minVersion); if (!parsedCurrent || !parsedMinimum) { const guidance = resolveUpgradeGuidance({ runner, runnerFrom }); await logLine( `compat.version_unparsed current=${redactLog(currentVersion || "")} required=${redactLog(minVersion)}` ); await log("warn", "codemem compatibility check could not parse versions", { currentVersion, minVersion, runner, runnerFromSet: Boolean(String(runnerFrom || "").trim()), upgradeMode: guidance.mode, }); await showToast( `codemem compatibility check could not parse versions (cli='${currentVersion || "unknown"}', required='${minVersion}'). Suggested action: ${guidance.action}`, "warning" ); return; } if (isVersionAtLeast(currentVersion, minVersion)) { writeCompatCheckCache(cacheKey, currentVersion); return; } clearCompatCheckCache(); const guidance = resolveUpgradeGuidance({ runner, runnerFrom }); const message = `codemem CLI ${currentVersion || "unknown"} is older than required ${minVersion}`; await log("warn", message, { currentVersion, minVersion, runner, runnerFromSet: Boolean(String(runnerFrom || "").trim()), upgradeMode: guidance.mode, upgradeAction: guidance.action, }); await logLine( `compat.version_mismatch current=${currentVersion} required=${minVersion} mode=${guidance.mode} note=${redactLog(guidance.note)}` ); if (backendUpdatePolicy === "auto") { compatibilityAutoUpdateAttempted = true; const notification = parseReleaseNotification( await runCli(["update", "check", "--json"]) ); if (!notification?.autoUpdateEligible) { await logLine("compat.auto_update_skipped reason=release_not_eligible"); await showToast( `${message}. Auto-update skipped (not eligible). Suggested action: ${guidance.action}`, "warning" ); return; } const autoPlan = resolveAutoUpdatePlan({ runner, runnerFrom, runnerFromExplicit, targetVersion: notification.latestVersion, }); if (!autoPlan.allowed) { await logLine(`compat.auto_update_skipped reason=${autoPlan.reason || "not_eligible"}`); await showToast( `${message}. Auto-update skipped (not eligible). Suggested action: ${guidance.action}`, "warning" ); return; } await logLine(`compat.auto_update_start cmd=${autoPlan.commandText}`); const updateResult = await installCliUpdate(autoPlan.command); if (updateResult?.exitCode === 0) { await logLine( `compat.auto_update_result exit=${updateResult?.exitCode ?? "unknown"} stderr=${redactLog( (updateResult?.stderr || "").trim() )}` ); const refreshedResult = await runCli(["--version"]); const refreshedVersion = (refreshedResult?.stdout || "").trim(); if ( refreshedResult?.exitCode === 0 && refreshedVersion === notification.latestVersion && isVersionAtLeast(refreshedVersion, minVersion) ) { writeCompatCheckCache(cacheKey, refreshedVersion); const viewerRestart = await restartViewerAfterAutoUpdate(); await logLine( `compat.auto_update_success before=${currentVersion} after=${refreshedVersion}` ); await showToast( `Updated codemem backend from ${currentVersion || "unknown"} to ${refreshedVersion}.`, "success" ); if (viewerRestart.attempted && !viewerRestart.ok) { await showToast( "Backend updated, but viewer restart failed. Run `codemem serve restart`.", "warning" ); } return; } await logLine( `compat.auto_update_verification_failed current=${redactLog(refreshedVersion || "unknown")} required=${redactLog(minVersion)}` ); await showToast( `${message}. Auto-update completed, but the active CLI failed verification. Suggested action: ${guidance.action}`, "warning" ); return; } await logLine( `compat.auto_update_skipped reason=update_install_failed exit=${updateResult?.exitCode ?? "unknown"} stderr=${redactLog((updateResult?.stderr || "").trim())}` ); await showToast( `${message}. Auto-update skipped (installation failed). Suggested action: ${guidance.action}`, "warning" ); return; } await showToast(`${message}. Suggested action: ${guidance.action}`, "warning"); }; const checkForReleaseUpdate = async () => { if (backendUpdatePolicy === "off" || compatibilityAutoUpdateAttempted) return; const notification = parseReleaseNotification( await runCli(["update", "check", "--json"]) ); if ( !notification || notifiedReleaseVersions.has(notification.latestVersion) ) { return; } notifiedReleaseVersions.add(notification.latestVersion); if ( backendUpdatePolicy === "auto" && notification.autoUpdateEligible && !compatibilityAutoUpdateAttempted ) { const autoPlan = resolveAutoUpdatePlan({ runner, runnerFrom, runnerFromExplicit, targetVersion: notification.latestVersion, }); if (autoPlan.allowed) { await logLine(`release.auto_update_start cmd=${autoPlan.commandText}`); const installation = await installCliUpdate(autoPlan.command); if (installation?.exitCode === 0) { const verification = await runCli(["--version"]); const installedVersion = (verification?.stdout || "").trim(); if (verification?.exitCode !== 0 || installedVersion !== notification.latestVersion) { await logLine( `release.auto_update_verification_failed current=${redactLog(installedVersion || "unknown")} expected=${notification.latestVersion}` ); await showToast( `codemem ${notification.latestVersion} was installed, but the active CLI failed verification.`, "warning" ); return; } const viewerRestart = await restartViewerAfterAutoUpdate(); await showToast(`Updated codemem to ${notification.latestVersion}.`, "success"); if (viewerRestart.attempted && !viewerRestart.ok) { await showToast( "Backend updated, but viewer restart failed. Run `codemem serve restart`.", "warning" ); } return; } await logLine( `release.auto_update_failed exit=${installation?.exitCode ?? "unknown"} stderr=${redactLog( (installation?.stderr || "").trim() )}` ); } } await showToast( `codemem ${notification.latestVersion} is available. ${notification.recommendedAction}`, "warning" ); }; const resolveInjectQuery = (overrides = {}) => { const firstPrompt = hasOwn(overrides, "firstPrompt") ? overrides.firstPrompt : sessionContext.firstPrompt; const resolvedLastPromptText = hasOwn(overrides, "lastPromptText") ? overrides.lastPromptText : lastPromptText; return buildInjectQuery({ firstPrompt, lastPromptText: resolvedLastPromptText, projectName: resolveProjectName(project, cwd), filesModified: sessionContext.filesModified, }); }; const describeInjectQuery = (query, overrides = {}) => { const safeQuery = redactLog((query || "").trim(), 240); const projectName = resolveProjectName(project, cwd) || ""; const firstPrompt = hasOwn(overrides, "firstPrompt") ? overrides.firstPrompt : sessionContext.firstPrompt; const resolvedLastPromptText = hasOwn(overrides, "lastPromptText") ? overrides.lastPromptText : lastPromptText; return { safeQuery, firstPromptLen: firstPrompt?.trim()?.length || 0, lastPromptLen: resolvedLastPromptText?.trim()?.length || 0, projectName, filesModifiedCount: sessionContext.filesModified.size, }; }; const redactLog = (value, limit = 400) => { if (!value) return ""; const masked = String(value).replace(/(Bearer\s+)[^\s]+/gi, "$1[redacted]"); return masked.length > limit ? `${masked.slice(0, limit)}…` : masked; }; let fallbackEvaluationSessionId = nextEventId(); const buildInjectedContext = async (query, context = {}) => { const requestPackBudget = reserveContextPrefixBudget(context.tokenBudget ?? injectTokenBudget); const queryHash = hashPromptPackQuery(query); // Requester eligibility comes only from the transform's own session identity. // activeSessionID tracks the latest event stream, which can belong to another // overlapping session; inferring from it would authorize the wrong summaries. const requesterHostSessionID = context.sessionID || null; const sessionID = requesterHostSessionID || "unknown"; const surface = context.surface || injectSurface; let requestKey = context.requestKey || "unknown"; if (context.fallbackTurn != null) { requestKey = JSON.stringify([requestKey, fallbackEvaluationSessionId, promptCounter, context.fallbackTurn]); } const evaluationFields = ( beforeText, afterText, candidateItems, duplicatesOmitted, packMetadata, artifactFingerprint, ) => ({ evaluation_key: injectionDigest(JSON.stringify([ sessionID, requestKey, surface, queryHash, artifactFingerprint, ])), automatic_recall: { v: 1, candidateItems, duplicatesOmitted, beforeTokens: beforeText ? estimateTokens(wrapInjectedContext(beforeText)) : 0, afterTokens: afterText ? estimateTokens(wrapInjectedContext(afterText)) : 0, missingRetainedMetadata: context.retainedMetadata?.missingRetainedMetadata === true, invalidRetainedMetadata: context.retainedMetadata?.invalidRetainedMetadata === true, packMetadata, }, }); const attemptKey = JSON.stringify([ sessionID, requestKey, surface, promptCounter, queryHash, ]); let retryCount = getPromptPackRetryCount(attemptKey); const resolveIdentity = () => promptPackIdentity({ sessionID, requestKey: retryCount > 0 ? `${requestKey}:empty-retry:${retryCount}` : requestKey, surface, promptNumber: promptCounter, queryHash, }); let identity = resolveIdentity(); let metadata = attemptMetadata( identity, requesterHostSessionID, ); if (requestPackBudget === null) { return { text: "", attemptId: identity.attemptId, requestId: identity.requestId, queryHash, promptNumber: promptCounter, }; } const runPack = async () => { const packArgs = buildPackArgs({ query, filesModified: sessionContext.filesModified, injectLimit, injectTokenBudget: requestPackBudget, internalLedger: true, }); const httpResult = await postViewerJson({ url: packUrl, operation: "pack", payload: buildPackHttpBody({ query, filesModified: sessionContext.filesModified, injectLimit, injectTokenBudget: requestPackBudget, projectName: normalizeProjectLabel(process.env.CODEMEM_PROJECT), cwd, dbPath: promptPackDbPath, identityTarget: promptPackIdentityTarget, attempt: metadata, automaticContext: requesterHostSessionID ? { source: "opencode", host_session_id: requesterHostSessionID } : null, }), validate: isValidPackHttpPayload, }); if (httpResult.ok) { return { packArgs, result: { exitCode: 0, stdout: JSON.stringify(httpResult.body), stderr: "", transport: "viewer", }, }; } const { cause, retryable } = httpResult.classification; await logLine( `inject.pack.http_error cause=${JSON.stringify(redactLog(cause, 200))} retryable=${retryable}` ); if (!retryable) { return { packArgs, result: { exitCode: 1, stdout: "", stderr: cause, transport: "viewer" }, }; } const result = await runCli(packArgs, { stdinText: JSON.stringify(metadata) }); if (rejectsInternalLedgerFlag(result)) { // An older CLI cannot enforce requester-session continuity. Do not retry // through its generic pack path, which may inject an unrelated summary. await logLine("inject.pack.cli_legacy_unsafe_fallback_blocked"); } return { packArgs, result: { ...result, transport: "cli" } }; }; let { packArgs, result } = await runPack(); let { packText, conflictPackText, metrics, itemCount, ledgerConflict } = parsePackOutput(result); let artifactFingerprint = packText || itemCount === 0 ? promptPackArtifactFingerprint(result.stdout, packText) : ""; let injectedIdentity = identity; let repairFallbackUsed = false; if (ledgerConflict) { // A restarted plugin has no artifact cache to predict this conflict. The // ledger marker is authoritative: never attribute delivery to the stale // identity, and retry once with a fresh deterministic identity. await log("warn", "codemem prompt-pack ledger conflict; retrying with fresh identity", { sessionID, surface, }); const fallback = conflictPackText ? { packArgs, result, packText: conflictPackText, metrics, itemCount, artifactFingerprint: promptPackArtifactFingerprint(result.stdout, conflictPackText), ledgerConflict, } : null; advancePromptPackRetryIdentity(attemptKey); retryCount = getPromptPackRetryCount(attemptKey); identity = resolveIdentity(); metadata = attemptMetadata(identity, requesterHostSessionID); ({ packArgs, result } = await runPack()); // Repair-conflict bytes are never preferred over the original preserved pack. ({ packText, metrics, itemCount, ledgerConflict } = parsePackOutput(result)); artifactFingerprint = packText || itemCount === 0 ? promptPackArtifactFingerprint(result.stdout, packText) : ""; const repairFailed = ledgerConflict || !result || result.exitCode !== 0 || (!packText && itemCount !== 0); if (fallback && repairFailed) { if (ledgerConflict) { await log("warn", "codemem prompt-pack fresh identity also conflicted", { sessionID, surface, }); } else { const malformedSuccess = result?.exitCode === 0; const exitCode = result?.exitCode ?? "unknown"; const stderr = redactLog(result?.stderr ? result.stderr.trim() : ""); const cmd = redactPackCommand(runner, runnerArgs, packArgs); await logLine( `inject.pack.identity_repair_failed reason=${malformedSuccess ? "malformed_success" : "command_failed"} exit=${exitCode} cmd=${cmd}` + `${stderr ? ` stderr=${stderr}` : ""}` ); await log("warn", "codemem prompt-pack identity repair failed", { reason: malformedSuccess ? "malformed_success" : "command_failed", exitCode, }); void runPromptPackLedger({ action: "record", ...metadata, retrieval_status: "failed", failure_code: malformedSuccess ? "pack_identity_repair_failed" : "pack_command_failed", failure_stage: malformedSuccess ? "decode" : "transport", }); } advancePromptPackRetryIdentity(attemptKey); retryCount = getPromptPackRetryCount(attemptKey); ({ packArgs, result, packText, metrics, itemCount, artifactFingerprint, ledgerConflict, } = fallback); // Neither persisted identity represents fallback delivery: the first // conflicted and the fresh repair failed. Keep the bytes fail-open but // leave delivery and replay attribution empty. injectedIdentity = null; repairFallbackUsed = true; } else { injectedIdentity = identity; } } if (packText) { const previous = getSuccessfulPromptPackArtifact(attemptKey); if ( previous?.retryCount === retryCount && previous.fingerprint !== artifactFingerprint ) { // The in-memory fingerprint detected a changed artifact before handoff. // Defensively rebuild once with a fresh identity; older CLIs do not // expose the persisted conflict marker used by the restart path above. // Keep the usable changed bytes as a fail-open fallback: diagnostics // identity repair must never suppress context injection. const fallback = { packArgs, result, packText, metrics, itemCount, artifactFingerprint, ledgerConflict, }; advancePromptPackRetryIdentity(attemptKey); retryCount = getPromptPackRetryCount(attemptKey); identity = resolveIdentity(); metadata = attemptMetadata(identity, requesterHostSessionID); ({ packArgs, result } = await runPack()); ({ packText, metrics, itemCount, ledgerConflict } = parsePackOutput(result)); artifactFingerprint = packText || itemCount === 0 ? promptPackArtifactFingerprint(result.stdout, packText) : ""; if (ledgerConflict) { // The repair identity is also persisted with different artifacts. // Preserve the marker for the fail-closed return below; never use the // stale changed bytes as the transport-failure fallback. injectedIdentity = identity; } else if (result?.exitCode === 0 && itemCount === 0) { // A fresh identity can legitimately resolve to no results if the // underlying memory set changed between rebuilds. The CLI already // recorded that terminal outcome; do not misclassify it as a decode // failure or fall back to stale non-empty context. injectedIdentity = identity; } else if (result?.exitCode !== 0 || !packText) { const malformedSuccess = result?.exitCode === 0; const exitCode = result?.exitCode ?? "unknown"; const stderr = redactLog(result?.stderr ? result.stderr.trim() : ""); const cmd = redactPackCommand(runner, runnerArgs, packArgs); await logLine( `inject.pack.identity_repair_failed reason=${malformedSuccess ? "malformed_success" : "command_failed"} exit=${exitCode} cmd=${cmd}` + `${stderr ? ` stderr=${stderr}` : ""}` ); await log("warn", "codemem prompt-pack identity repair failed", { reason: malformedSuccess ? "malformed_success" : "command_failed", exitCode, }); void runPromptPackLedger({ action: "record", ...metadata, retrieval_status: "failed", failure_code: malformedSuccess ? "pack_identity_repair_failed" : "pack_command_failed", failure_stage: malformedSuccess ? "decode" : "transport", }); advancePromptPackRetryIdentity(attemptKey); ({ packArgs, result, packText, metrics, itemCount, artifactFingerprint, ledgerConflict, } = fallback); // The stale attempt does not represent these changed bytes, and the // fresh repair attempt failed. Inject without delivery attribution // rather than falsely marking either ledger attempt handed off. injectedIdentity = null; repairFallbackUsed = true; } else { injectedIdentity = identity; } } } if (result?.exitCode !== 0) { const exitCode = result?.exitCode ?? "unknown"; const stderr = redactLog(result?.stderr ? result.stderr.trim() : ""); const stdout = redactLog(result?.stdout ? result.stdout.trim() : ""); const cmd = redactPackCommand(runner, runnerArgs, packArgs); await logLine( `inject.pack.error ${exitCode} cmd=${cmd}` + `${stderr ? ` stderr=${stderr}` : ""}` + `${stdout ? ` stdout=${stdout}` : ""}` ); void runPromptPackLedger({ action: "record", ...metadata, retrieval_status: "failed", failure_code: "pack_command_failed", failure_stage: "transport", }); advancePromptPackRetryIdentity(attemptKey); return { text: "", attemptId: identity.attemptId, requestId: identity.requestId, queryHash, promptNumber: promptCounter, }; } if (ledgerConflict && !repairFallbackUsed) { await log("warn", "codemem prompt-pack fresh identity also conflicted", { sessionID, surface, }); advancePromptPackRetryIdentity(attemptKey); return { text: "", attemptId: identity.attemptId, requestId: identity.requestId, queryHash, promptNumber: promptCounter, }; } if (!packText) { if (itemCount === 0) { if (surface === "message") { void runPromptPackLedger({ action: "recall", attempt_id: identity.attemptId, ...evaluationFields("", "", 0, 0, "valid", artifactFingerprint) }).catch(() => {}); } advancePromptPackRetryIdentity(attemptKey); } if (debug) { const { safeQuery, firstPromptLen, lastPromptLen, projectName, filesModifiedCount } = describeInjectQuery(query); await logLine( `inject.pack.empty query_len=${query ? query.length : 0} query=${JSON.stringify(safeQuery)} first_prompt_len=${firstPromptLen} last_prompt_len=${lastPromptLen} project=${JSON.stringify(projectName)} files_modified=${filesModifiedCount} stdout=${JSON.stringify(redactLog((result.stdout || "").trim(), 240))}` ); } return { text: "", cacheableEmpty: itemCount === 0, attemptId: identity.attemptId, requestId: identity.requestId, queryHash, promptNumber: promptCounter, }; } // The pack JSON exposes the item count as `total_items`; `metrics.items` // does not exist on that payload, so reading it would always log 0. const packTokens = Number.isFinite(Number(metrics?.pack_tokens)) ? Number(metrics.pack_tokens) : 0; await logLine( `inject.pack.ok source=opencode items=${itemCount ?? 0} pack_tokens=${packTokens} query_len=${query ? query.length : 0}` ); if (!repairFallbackUsed) { rememberSuccessfulPromptPackArtifact( attemptKey, retryCount, artifactFingerprint || promptPackArtifactFingerprint(result.stdout, packText) ); } const refilter = (retainedItems) => { const filtered = context.surface === "message" ? filterRetainedPack(result.stdout, packText, retainedItems) : { text: packText, items: [], duplicates: 0 }; const text = filtered.text ? wrapInjectedContext(filtered.text) : ""; const evaluation = surface === "message" ? evaluationFields( packText, filtered.text, itemCount, filtered.duplicates, filtered.packMetadata, artifactFingerprint, ) : undefined; const recall = filtered.items.length ? { v: 1, digest: injectionDigest(text), items: filtered.items, workingContext: context.workingContext } : undefined; let skipReason = null; if (!text && filtered.duplicates > 0) { skipReason = context.continuationOnly ? "continuation_only" : "unchanged_memories"; } return { text, recall, duplicates: filtered.duplicates, evaluation, skipReason, }; }; return { ...refilter(context.retainedItems), refilter, ...(metrics ? { metrics } : {}), attemptId: injectedIdentity?.attemptId || null, requestId: injectedIdentity?.requestId || null, queryHash, promptNumber: promptCounter, }; }; const stopViewer = async () => { if (!viewerEnabled || !viewerAutoStop || !viewerStarted) return; viewerStarted = false; stopHealthCheck(); await logLine("viewer stop requested"); await runCli(buildViewerCliArgs("stop")); }; const viewerHealthMonitor = createViewerHealthMonitor({ viewerHealthUrl, legacyStatusUrl: rawEventsStatusUrl, isActive: () => viewerEnabled && viewerAutoStart && viewerHealthMonitor.state().running, recoverViewer: () => recoverViewerStart(runCli(buildViewerCliArgs("start")), () => { viewerStarted = true; }), logLine, }); const startHealthCheck = viewerHealthMonitor.start; const stopHealthCheck = viewerHealthMonitor.stop; const watchViewerHealth = () => { if (runtimeActive) startHealthCheck(); }; // Get version info (commit hash) for debugging let version = "unknown"; try { version = execSync("git rev-parse --short HEAD", { cwd: runnerFrom, timeout: 500, encoding: "utf-8", // Suppress "fatal: not a git repository" when the working directory is // not a git repo. The catch below leaves version as "unknown". stdio: ["ignore", "pipe", "ignore"], windowsHide: true, }).trim(); } catch { // Ignore - version will remain 'unknown' } await log("info", "codemem plugin initialized", { cwd, version }); await logLine(`plugin initialized cwd=${cwd} version=${version}`); void startViewer(); const updateCheckTimer = setTimeout(() => { void (async () => { try { await verifyCliCompatibility(); } catch (err) { await logLine( `compat.version_check_error message=${String(err?.message || err || "unknown")}` ); } try { await checkForReleaseUpdate(); } catch (err) { await logLine( `release.update_check_error message=${String(err?.message || err || "unknown")}` ); } })(); }, COMPAT_CHECK_DELAY_MS); if (updateCheckTimer.unref) updateCheckTimer.unref(); const truncate = (value) => { if (value === undefined || value === null) { return null; } const text = String(value); if (Number.isNaN(maxChars) || maxChars <= 0) { return ""; } if (text.length <= maxChars) { return text; } return `${text.slice(0, maxChars)}\n[codemem] event truncated\n`; }; const safeStringify = (value) => { if (value === undefined || value === null) { return null; } if (typeof value === "string") { return value; } try { return JSON.stringify(value); } catch { return String(value); } }; const trimRetainedEvents = (retainedEvents) => { trimEventQueue({ events: retainedEvents, maxEvents, hardMaxEvents: rawEventQueueCapacity, onUnsentPressure: (queuedCount, cap) => { void logLine(`queue.pressure unsent_preserved queued=${queuedCount} max_events=${cap}`); }, onForcedDrop: (dropped, queuedCount, hardCap) => { const durableSpoolCoverage = dropped?._raw_spooled === true; void logLine( `queue.drop hard_cap durable_spool_coverage=${durableSpoolCoverage} queued=${queuedCount} hard_max=${hardCap}` ); void log("error", "codemem raw event dropped at in-memory hard cap", { category: "queue_capacity", delivery: durableSpoolCoverage ? "spool" : "memory", durable_spool_coverage: durableSpoolCoverage, queued_count: queuedCount, hard_max_events: hardCap, }); }, }); }; const trimRetainedEventQueues = () => { // The currently delivering batch is unavoidable; cap all mutable retained storage together. const retainedEvents = [ ...pendingBoundaryBatches.flatMap((batch) => batch.events), ...events, ].sort( (left, right) => (queuedEventOrder.get(left) ?? Number.MAX_SAFE_INTEGER) - (queuedEventOrder.get(right) ?? Number.MAX_SAFE_INTEGER) ); trimRetainedEvents(retainedEvents); const retained = new Set(retainedEvents); const liveEvents = events.filter((event) => retained.has(event)); events.splice(0, events.length, ...liveEvents); for (let index = pendingBoundaryBatches.length - 1; index >= 0; index--) { const batch = pendingBoundaryBatches[index]; batch.events = batch.events.filter((event) => retained.has(event)); if (batch.events.length) continue; pendingBoundaryBatches.splice(index, 1); flushingBatches.delete(batch); batch.completion.resolve(); } const trackedEvents = new Set(retainedEvents); for (const batch of flushingBatches) { for (const event of batch.events) trackedEvents.add(event); } for (const deferredEvent of capacityDeferredEvents) { if (!trackedEvents.has(deferredEvent)) capacityDeferredEvents.delete(deferredEvent); } }; const recordEvent = (event) => { queuedEventOrder.set(event, nextQueuedEventOrder++); events.push(event); trimRetainedEventQueues(); }; // Reserve delivery in capture order. Flush/disposal reuse the same task, and // preparation never waits on delivery, so neither can serialize early or cycle. const emitRawEvent = (input) => { const existing = rawEventDeliveries.get(input.payload); if (existing) return existing; const inputOrder = queuedEventOrder.get(input.payload) ?? Number.MAX_SAFE_INTEGER; const olderDeferredEventExists = [...capacityDeferredEvents].some((event) => (queuedEventOrder.get(event) ?? Number.MAX_SAFE_INTEGER) < inputOrder); if (runtimeActive && (rawEventDeliveryCount >= rawEventQueueCapacity || olderDeferredEventExists)) { capacityDeferredEvents.add(input.payload); void logLine( `delivery.pressure queued=${rawEventDeliveryCount} hard_max=${rawEventQueueCapacity}` ); return Promise.resolve(false); } capacityDeferredEvents.delete(input.payload); rawEventDeliveryCount++; const delivery = rawEventDeliveryTail.then(async () => { await rawEventPreparations.get(input.payload); return rawEventDelivery.deliver(input); }); rawEventDeliveries.set(input.payload, delivery); rawEventDeliveryTail = delivery.catch(() => {}).finally(() => { rawEventDeliveries.delete(input.payload); rawEventDeliveryCount--; }); return delivery; }; const captureCodememEvent = (sessionID, event, prompt = null) => { const normalizedSessionID = typeof sessionID === "string" && sessionID.trim() ? sessionID.trim() : null; if (normalizedSessionID) { activeSessionID = normalizedSessionID; } const effectiveSessionID = normalizedSessionID || activeSessionID; const resolvedSessionID = effectiveSessionID || `missing:${Date.now()}:${String(nextEventId()).slice(0, 8)}`; if (!effectiveSessionID) { activeSessionID = resolvedSessionID; void logLine(`capture.fallback_session_id ${resolvedSessionID}`); } const adapterAnnotatedEvent = attachAdapterEvent({ sessionID: resolvedSessionID, event, }); const rawEventId = adapterAnnotatedEvent?._adapter?.event_id || adapterAnnotatedEvent?._raw_event_id || nextEventId(); const queuedEvent = { ...adapterAnnotatedEvent, _raw_event_id: rawEventId, _raw_session_id: resolvedSessionID, }; rawEventDelivery.setStartedAt(queuedEvent, sessionStartedAt); recordEvent(queuedEvent); if (prompt && rawEventsEnabled && host.resolveCaptureContext) { const preparation = Promise.resolve().then(() => host.resolveCaptureContext(prompt)) .then((context) => { if (context) rawEventDelivery.setCaptureContext(queuedEvent, context); }).catch(() => { // Optional provenance must never discard an otherwise valid prompt. }); rawEventPreparations.set(queuedEvent, preparation); } const deliveryTask = emitRawEvent({ sessionID: resolvedSessionID, type: queuedEvent?.type || "unknown", payload: queuedEvent, }); void deliveryTask.catch(() => {}); return deliveryTask; }; const drainPendingPrompts = async ({ exceptIdentity = null, sessionID = null, skipFlush = false, awaitDurability = false, } = {}) => { const deliveries = []; for (const [promptIdentity, prompt] of pendingPrompts) { if (!runtimeActive && !skipFlush) return true; if (sessionID && prompt.sessionID !== sessionID) continue; if (promptIdentity === exceptIdentity) { continue; } if (capturedPrompts.has(promptIdentity)) { pendingPrompts.delete(promptIdentity); continue; } pendingPrompts.delete(promptIdentity); capturedPrompts.add(promptIdentity); if (!skipFlush && (prompt.text.trim() === "/new" || prompt.text.trim().startsWith("/new "))) { void logLine("detected /new command, flushing events"); void flushEvents(); } promptCounter += 1; updateActivity(); if (!sessionContext.firstPrompt) { sessionContext.firstPrompt = prompt.text; sessionContext.startTime = Date.now(); } sessionContext.promptCount++; const promptEvent = { type: "user_prompt", prompt_number: promptCounter, prompt_text: prompt.text, timestamp: new Date().toISOString(), }; deliveries.push(captureCodememEvent(prompt.sessionID, promptEvent, prompt)); void logLine( `user_prompt captured #${promptCounter}: ${prompt.text.substring(0, 50)}` ); if (!skipFlush && shouldForceFlush()) { void logLine(`force flush triggered: tools=${sessionContext.toolCount}, prompts=${sessionContext.promptCount}, duration=${Math.round((Date.now() - (sessionContext.startTime || Date.now())) / 1000)}s`); void flushEvents(); } } if (!awaitDurability) return true; const results = await Promise.allSettled(deliveries); return results.every((result) => result.status === "fulfilled" && result.value); }; const capturePendingPrompts = (options = {}) => drainPendingPrompts(options); const requeueDetachedBatch = (batch, failedEvents) => { if (batch.restored || !failedEvents.length) return; batch.restored = true; events.push(...failedEvents); events.sort( (left, right) => (queuedEventOrder.get(left) ?? Number.MAX_SAFE_INTEGER) - (queuedEventOrder.get(right) ?? Number.MAX_SAFE_INTEGER) ); trimRetainedEventQueues(); if (batch.owner && batch.owner === activeSessionID) { mergeSessionContext(batch.context); } }; const deliverDetachedBatch = async (batch) => { const failedEvents = []; for (const queuedEvent of batch.events) { if (queuedEvent && typeof queuedEvent === "object" && queuedEvent._raw_enqueued) { continue; } const queuedSessionID = queuedEvent?._raw_session_id || queuedEvent?.properties?.sessionID || null; try { const delivered = await emitRawEvent({ sessionID: queuedSessionID, type: queuedEvent?.type || "unknown", payload: queuedEvent, }); if (!delivered) failedEvents.push(queuedEvent); } catch { failedEvents.push(queuedEvent); } } requeueDetachedBatch(batch, failedEvents); const durationMs = batch.context.startTime ? Date.now() - batch.context.startTime : 0; try { if (failedEvents.length) { await logLine(`flush.retry_deferred count=${failedEvents.length}`); } else { await logLine( `flush.stream_only finalize count=${batch.events.length} tools=${batch.context.toolCount} prompts=${batch.context.promptCount} duration=${Math.round(durationMs / 1000)}s` ); await logLine(`flush.ok count=${batch.events.length} dropped=0`); } if (runtimeActive) await rawEventDelivery.drainSpool(); } catch { // Flush delivery and diagnostics must not reject into OpenCode hooks. } }; const resumeDeferredFlush = () => { if (flushingBatches.size > 0 || !deferredFlush) return; const pendingFlush = deferredFlush; deferredFlush = null; if (!runtimeActive || !events.length) { pendingFlush.resolve(); return; } void flushEvents().then(pendingFlush.resolve, pendingFlush.resolve); }; const scheduleDetachedBatch = (batch) => { flushingBatches.add(batch); const delivery = flushDeliveryTail.then(() => deliverDetachedBatch(batch)); const contained = delivery.catch(() => { const undelivered = batch.events.filter((event) => !event?._raw_enqueued); requeueDetachedBatch(batch, undelivered); }).finally(() => { flushingBatches.delete(batch); resumeDeferredFlush(); }); flushDeliveryTail = contained; return contained; }; const flushEmptyQueue = async () => { try { await rawEventDelivery.drainSpool(); await logLine("flush.skip empty"); } catch { // Empty flushes follow the same non-rejecting hook contract. } }; const detachQueuedEvents = ({ owner }) => { const batch = { events: events.splice(0, events.length), context: sessionContext, owner, restored: false, }; resetSessionContext(); return batch; }; const scheduleQueuedEvents = ({ owner }) => { return scheduleDetachedBatch(detachQueuedEvents({ owner })); }; const startBoundaryDrain = () => { if (boundaryDrain) return; // One drain consumes every queued owner batch without extending the promise tail per session. const drain = flushDeliveryTail.then(async () => { while (pendingBoundaryBatches.length) { const batch = pendingBoundaryBatches.shift(); try { await deliverDetachedBatch(batch); } catch { const undelivered = batch.events.filter((event) => !event?._raw_enqueued); requeueDetachedBatch(batch, undelivered); } finally { flushingBatches.delete(batch); batch.completion.resolve(); } } }); const contained = drain.catch(() => {}).finally(() => { boundaryDrain = null; if (pendingBoundaryBatches.length) { startBoundaryDrain(); return; } resumeDeferredFlush(); }); boundaryDrain = contained; flushDeliveryTail = contained; }; const scheduleBoundaryBatch = (batch) => { batch.completion = Promise.withResolvers(); pendingBoundaryBatches.push(batch); flushingBatches.add(batch); trimRetainedEventQueues(); startBoundaryDrain(); return batch.completion.promise; }; const flushBoundaryEvents = ({ owner }) => { if (!runtimeActive || !events.length) return Promise.resolve(); return scheduleBoundaryBatch(detachQueuedEvents({ owner })); }; const flushEvents = () => { if (!runtimeActive) return Promise.resolve(); if (flushingBatches.size > 0) { if (!events.length) return flushDeliveryTail; if (!deferredFlush) deferredFlush = Promise.withResolvers(); return deferredFlush.promise; } if (!events.length) { return flushEmptyQueue(); } return scheduleQueuedEvents({ owner: activeSessionID }); }; void rawEventDelivery.drainSpool(); const deactivateRuntime = () => { if (!runtimeActive) return; runtimeActive = false; clearTimeout(updateCheckTimer); stopHealthCheck(); rawEventDelivery.abort(); releasePluginRegistration(); }; const ensureQueuedEventsDurable = async () => { let durable = true; const queuedEvents = new Set(events); for (const batch of flushingBatches) { for (const queuedEvent of batch.events) { queuedEvents.add(queuedEvent); } } for (const queuedEvent of queuedEvents) { if (queuedEvent?._raw_enqueued || queuedEvent?._raw_spooled) { continue; } const queuedSessionID = queuedEvent?._raw_session_id || null; let persisted = false; try { persisted = await emitRawEvent({ sessionID: queuedSessionID, type: queuedEvent?.type || "unknown", payload: queuedEvent, }); } catch { // Disposal must contain serialization and persistence failures. } durable &&= persisted; } return durable; }; return { capturePendingPrompts, deactivate: deactivateRuntime, dispose: async () => { deactivateRuntime(); const promptsDurable = await capturePendingPrompts({ skipFlush: true, awaitDurability: true, }); const deliveriesDurable = await ensureQueuedEventsDurable(); await rawEventDeliveryTail.catch(() => {}); await flushDeliveryTail.catch(() => {}); if (!promptsDurable || !deliveriesDurable) { await errorLogLine("raw_events.dispose_durability_failed category=persistence"); } }, reportDiagnostic: async (code) => { const safeCode = ADAPTER_DIAGNOSTIC_CODES.has(code) ? code : "unknown_adapter_diagnostic"; await errorLogLine(`adapter.diagnostic code=${safeCode}`); }, // Test-only state inspection; adapters must not depend on this method. inspectBufferedPromptPartCount: () => [...promptPartsByMessage.values()] .reduce((count, parts) => count + parts.size, 0), inspectQueuedPrompts: () => events .filter((event) => event.type === "user_prompt") .map((event) => ({ number: event.prompt_number, text: event.prompt_text })), inspectQueuedEventTypes: () => events.map((event) => event.type), inspectQueuedEvents: () => events.map((event) => ({ sessionID: event._raw_session_id, toolCallID: event.tool_call_id ?? null, type: event.type, })), inspectPendingBoundaryEvents: () => pendingBoundaryBatches.flatMap((batch) => batch.events.map((event) => ({ owner: batch.owner, sessionID: event._raw_session_id, }))), inspectRawEventDeliveryCount: () => rawEventDeliveryCount, inspectFlushingBatchCount: () => flushingBatches.size, inspectSessionContext: () => ({ firstPrompt: sessionContext.firstPrompt, promptCount: sessionContext.promptCount, toolCount: sessionContext.toolCount, filesModified: [...sessionContext.filesModified], filesRead: [...sessionContext.filesRead], }), inspectCapturedPromptCount: () => capturedPrompts.size, handleCompacting: async ({ sessionID: requestedSessionID } = {}) => { const sessionID = requestedSessionID || activeSessionID; markCompactionInjectionSkip(compactionInjectionSkips, sessionID); if (debug) { await logLine( `inject.compaction_skip_marked sessionID=${sessionID || "unknown"}` ); } }, transformMessages: async (input, output, options = {}) => { const delivery = createTransformDelivery(options.deferDeliveryConfirmation === true); if (injectSurface === "system" && options.enableSystemSurface !== true) { return { applied: false, surface: injectSurface, completeDelivery: delivery.complete }; } const hookMessages = Array.isArray(output?.messages) ? scopeMessagesToSession(output.messages, input?.sessionID || null) : []; const latestUser = findLatestUserMessage(hookMessages); const sessionID = input?.sessionID || (latestUser ? resolveEntrySessionID(latestUser.entry) : null); const latestPromptText = latestUser ? extractMessageText(latestUser.entry) : ""; if (debug) { const query = resolveInjectQuery({ lastPromptText: latestPromptText }); const { safeQuery, firstPromptLen, lastPromptLen, projectName, filesModifiedCount } = describeInjectQuery(query, { lastPromptText: latestPromptText }); await logLine( `inject.messages_transform sessionID=${sessionID || "unknown"} query_len=${ query ? query.length : 0 } inject_enabled=${injectEnabled} tui_toast=${Boolean(hostNotify)} query=${JSON.stringify(safeQuery)} first_prompt_len=${firstPromptLen} last_prompt_len=${lastPromptLen} project=${JSON.stringify(projectName)} files_modified=${filesModifiedCount}` ); } let applied = false; try { applied = await applyInjectedContextToMessages({ injectEnabled, input, output, injectionToastShown, showToast: hostNotify ? async (message) => hostNotify({ message, variant: "info" }) : null, resolveInjectQuery, buildInjectedContext, messageInjectionCache, emptyRecallCache, injectTokenBudget, retainedTokenBudget: injectSurface === "system" ? Number.POSITIVE_INFINITY : retainedTokenBudget, workingSet: [...sessionContext.filesModified, ...sessionContext.filesRead], recordMeasurement: delivery.recordMeasurement, commitDelivery: delivery.commit, beginFinalization: delivery.beginFinalization, compactionInjectionSkips, confirmDelivery: delivery.confirm, recordCacheReuse: (cached, context) => recordCachedPromptPack(cached, { ...context, surface: injectSurface, }), recordSkipped: (reason, sessionID) => { const latestMessageID = latestUser ? resolveEntryMessageId(latestUser.entry) : null; const requestKey = latestMessageID || (reason === "missing_message_identity" ? "missing-identity" : null); return recordSkippedPromptPack(reason, sessionID, injectSurface, requestKey); }, pruneAbsentCacheEntries: options.pruneAbsentCacheEntries !== false, requireLatestUserMessageID: options.requireLatestUserMessageID === true, cacheSuccessfulEmpty: options.cacheSuccessfulEmpty === true, surface: injectSurface, }); } catch (err) { await delivery.finish("failed"); await logLine( `inject.messages_transform.error sessionID=${sessionID || "unknown"} message=${JSON.stringify(err instanceof Error ? err.message : String(err))}` ); } if (debug) { const partsCount = Array.isArray(output?.messages) ? output.messages.reduce( (count, entry) => count + (Array.isArray(entry?.parts) ? entry.parts.length : 0), 0 ) : 0; await logLine( `inject.messages_transform.result sessionID=${sessionID || "unknown"} applied=${Boolean(applied)} messages=${Array.isArray(output?.messages) ? output.messages.length : 0} parts=${partsCount}` ); } return { applied, surface: injectSurface, completeDelivery: delivery.complete }; }, transformSystem: async (input, output) => { if (injectSurface !== "system") { return; } const hookSessionID = input?.sessionID || activeSessionID; if (consumeCompactionInjectionSkip(compactionInjectionSkips, hookSessionID)) { recordSkippedPromptPack("compaction_skipped", hookSessionID, "system"); if (debug) { await logLine( `inject.transform.skip_compaction sessionID=${hookSessionID || "unknown"}` ); } return; } const query = resolveInjectQuery(); if (debug) { const { safeQuery, firstPromptLen, lastPromptLen, projectName, filesModifiedCount } = describeInjectQuery(query); await logLine( `inject.transform sessionID=${input.sessionID} query_len=${ query ? query.length : 0 } inject_enabled=${injectEnabled} tui_toast=${Boolean(hostNotify)} query=${JSON.stringify(safeQuery)} first_prompt_len=${firstPromptLen} last_prompt_len=${lastPromptLen} project=${JSON.stringify(projectName)} files_modified=${filesModifiedCount}` ); } // Without the old per-session cache, every transform call rebuilds the // pack. Swallow rejections here so a single failed build (viewer failure, // CLI fallback crash, sqlite lock, or network blip) // can't take down the chat path. let applied = false; try { applied = await applyInjectedContextToOutput({ injectEnabled, input, output, injectionToastShown, showToast: hostNotify ? async (message) => hostNotify({ message, variant: "info" }) : null, resolveInjectQuery, buildInjectedContext, confirmDelivery: confirmPromptPackDelivery, recordSkipped: recordSkippedPromptPack, }); } catch (err) { await logLine( `inject.transform.error sessionID=${input.sessionID} message=${JSON.stringify(err instanceof Error ? err.message : String(err))}` ); } if (debug) { await logLine( `inject.transform.result sessionID=${input.sessionID} applied=${Boolean(applied)} system_entries=${Array.isArray(output.system) ? output.system.length : 0}` ); } }, handleEvent: async (event) => { if (!runtimeActive) return; const eventType = event?.type || "unknown"; const sessionID = event?.sessionID || null; const priorActiveSessionID = activeSessionID; const rawEvent = event?.raw; let sessionBoundaryDelivery = null; if (eventType === "session.created") { if (priorActiveSessionID) { await capturePendingPrompts({ sessionID: priorActiveSessionID, skipFlush: true }); } if (!runtimeActive) { resetSessionContext(); return; } const hasBoundaryEvents = events.length > 0; sessionBoundaryDelivery = hasBoundaryEvents ? flushBoundaryEvents({ owner: priorActiveSessionID }) : Promise.resolve(); if (!hasBoundaryEvents) resetSessionContext(); activeSessionID = sessionID || null; sessionStartSessionID = sessionID || null; sessionStartedAt = new Date().toISOString(); promptCounter = 0; fallbackEvaluationSessionId = nextEventId(); recallLifecycle.startSession(); lastPromptText = null; if (sessionID) { await capturePendingPrompts({ sessionID, exceptIdentity: promptIdentityForEvent(event), skipFlush: true, }); clearPromptSession(sessionID, { preserveCapturedPrompts: true }); } } else if (sessionID) { await capturePendingPrompts({ sessionID, exceptIdentity: promptIdentityForEvent(event) }); } if (!runtimeActive) return; // Always log session-related events for debugging /new if (eventType.startsWith("session.")) { await logLine(`SESSION EVENT: ${eventType}`); } if (debugExtraction) { await logLine(`event ${eventType}`); } // Debug: log event structure for message events (only when debug enabled) if ( debugExtraction && [ "message.updated", "message.created", "message.appended", "message.part.updated", ].includes(eventType) ) { // Log full event structure for debugging (only first few times per event type) if (!global.eventLogCount) global.eventLogCount = {}; if (!global.eventLogCount[eventType]) global.eventLogCount[eventType] = 0; if (global.eventLogCount[eventType] < 2) { global.eventLogCount[eventType]++; await logLine( `FULL EVENT (${eventType}): ${JSON.stringify( rawEvent, null, 2 ).substring(0, 3000)}` ); } await logLine( `event payload keys: ${Object.keys(rawEvent || {}).join(", ")}` ); if (rawEvent?.properties) { await logLine( `event properties keys: ${Object.keys(rawEvent.properties).join(", ")}` ); if (rawEvent.properties.role) { await logLine(`event role: ${rawEvent.properties.role}`); } if (rawEvent.properties.message) { await logLine(`event has properties.message`); } if (rawEvent.properties.info) { const infoKeys = Object.keys(rawEvent.properties.info); await logLine(`event properties.info keys: ${infoKeys.join(", ")}`); if (rawEvent.properties.info.role) { await logLine(`event info.role: ${rawEvent.properties.info.role}`); } } } } if ( [ "message.updated", "message.created", "message.appended", "message.part.updated", ].includes(eventType) ) { const prompt = extractPrompt(event); if (prompt) { const promptText = prompt.text; const promptIdentity = promptIdentityForEvent(event) || promptMessageKey(sessionID, prompt.messageID); const promptSessionID = event?.messageInfo?.sessionID || event?.part?.sessionID || sessionID; lastPromptText = promptText; if (!capturedPrompts.has(promptIdentity)) { pendingPrompts.set(promptIdentity, { sessionID: promptSessionID, messageID: prompt.messageID, text: promptText }); } } const assistant = extractAssistantText(event); if (assistant) { const assistantIdentity = promptMessageKey(sessionID, assistant.messageID); if (!capturedAssistantMessages.has(assistantIdentity)) { capturedAssistantMessages.add(assistantIdentity); updateActivity(); captureCodememEvent(sessionID, { type: "assistant_message", message_id: assistant.messageID, assistant_text: assistant.text, timestamp: new Date().toISOString(), }); await logLine( `assistant_message captured: ${assistant.text.substring(0, 50)}` ); } } const assistantUsage = extractAssistantUsage(event); if (assistantUsage) { updateActivity(); captureCodememEvent(sessionID, { type: "assistant_usage", message_id: assistantUsage.id, usage: assistantUsage.usage, timestamp: new Date().toISOString(), }); await logLine( `assistant_usage captured id=${assistantUsage.id.slice(-8)}` ); } const toolPart = eventType === "message.part.updated" ? event.part : null; if (toolPart?.type === "tool" && toolPart.state?.status === "error") { const failedToolKey = `${sessionID || "unknown"}:${toolPart.callID || toolPart.id}`; if (!failedToolCaptured.has(failedToolKey)) { failedToolCaptured.add(failedToolKey); updateActivity(); sessionContext.toolCount++; captureCodememEvent(sessionID, { type: "tool.execute.after", tool: toolPart.tool, args: toolPart.state.input, result: null, error: truncate(safeStringify(toolPart.state.error)), timestamp: new Date().toISOString(), }); await logLine( `tool.execute.after ${toolPart.tool} failed queued=${events.length} tools=${sessionContext.toolCount}` ); } } } // NEW ACCUMULATION STRATEGY // Only flush on: // - session.error (immediate error boundary) // - session.idle AFTER delay (scheduled via timeout) // - /new command (handled above) // - session.created (session boundary) // // REMOVED: session.compacted, session.compacting (too frequent) if (eventType === "session.error") { clearPromptPartsForSession(sessionID); await logLine("session.error detected, flushing immediately"); await flushEvents(); } if (eventType === "session.idle") { clearPromptPartsForSession(sessionID); await logLine( `session.idle detected, flushing immediately (tools=${sessionContext.toolCount}, prompts=${sessionContext.promptCount})` ); await flushEvents(); } if (eventType === "session.created") { await sessionBoundaryDelivery; startViewer(); } if (eventType === "session.deleted") { if (!sessionID || sessionID === activeSessionID) { activeSessionID = null; } if (!sessionID || sessionID === sessionStartSessionID) { sessionStartedAt = null; sessionStartSessionID = null; } if (sessionID) { rawEventDelivery.clearSession(sessionID); recallLifecycle.clearSession(sessionID); compactionInjectionSkips.delete(sessionID); for (const key of failedToolCaptured) { if (key.startsWith(`${sessionID}:`)) { failedToolCaptured.delete(key); } } clearPromptSession(sessionID); } await stopViewer(); } }, // V1 sends successes here and failures as ToolPart events; V2 sends both variants here. handleToolResult: async (input, output) => { if (!runtimeActive) return; const sessionID = input.sessionID || activeSessionID; if (sessionID) await capturePendingPrompts({ sessionID }); if (!runtimeActive) return; const args = input.args ?? {}; const result = output.output; const error = output.error ?? null; const toolName = input.tool; const toolCallID = input.id ?? null; // Update activity and session context updateActivity(); sessionContext.toolCount++; // Track files from tool events const filePath = args.filePath || args.path; if (filePath) { const lowerTool = toolName.toLowerCase(); if (lowerTool === "edit" || lowerTool === "write") { addWorkingSetPath(sessionContext.filesModified, filePath, cwd); } else if (lowerTool === "read") { addWorkingSetPath(sessionContext.filesRead, filePath, cwd); } } if (toolName.toLowerCase() === "apply_patch") { const patchPaths = extractApplyPatchPaths(args.patchText); for (const path of patchPaths) { addWorkingSetPath(sessionContext.filesModified, path, cwd); } } captureCodememEvent(input.sessionID, { type: "tool.execute.after", tool: toolName, args, result: error ? null : truncate(safeStringify(result)), error: error ? truncate(safeStringify(error)) : null, ...(toolCallID ? { tool_call_id: String(toolCallID) } : {}), timestamp: new Date().toISOString(), }); await logLine(`tool.execute.after ${toolName} queued=${events.length} tools=${sessionContext.toolCount}`); // Check if we should force flush due to threshold if (shouldForceFlush()) { await logLine(`force flush triggered: tools=${sessionContext.toolCount}, prompts=${sessionContext.promptCount}, duration=${Math.round((Date.now() - (sessionContext.startTime || Date.now())) / 1000)}s`); await flushEvents(); } }, memoryTools, tools: createCliStatusTools({ runCli, viewerUrlHost, viewerPort, logPath }), }; }; export const __testUtils = { adapterDiagnosticCodes: [...ADAPTER_DIAGNOSTIC_CODES], MAX_MESSAGE_INJECTION_CACHE_ENTRIES, PINNED_BACKEND_VERSION, fetchRawEventsStatus, inferProjectFromCwd, normalizeProjectLabel, resolveProjectName, buildInjectQuery, buildPackArgs, deterministicUuid, promptPackIdentity, hashPromptPackQuery, redactPackCommand, rejectsInternalLedgerFlag, classifyFallbackCommandResult, classifyRawEventViewerFailure, classifyRawEventTransportCause, describeRawEventViewerFailure, detectRunner, PROMPT_TRANSPORT_PROTOCOL_RANGE, normalizePromptTransportProtocolRange, arePromptTransportProtocolRangesCompatible, classifyPromptTransportFailure, classifyViewerHttpFailure, viewerHttpDiagnosticReason, classifyViewerProfileFailure, createPromptTransportResult, isValidPackHttpPayload, isValidLedgerHttpPayload, buildPackHttpBody, parsePackText, parsePackMetrics, buildViewerIdentityTarget, resolveInjectSurface, applyInjectedContextToOutput, applyInjectedContextToMessages, extractMessageText, isCodememContextPart, buildRunnerArgs, appendWorkingSetFileArgs, extractApplyPatchPaths, normalizeWorkingSetPath, addWorkingSetPath, mapOpencodeEventTypeToAdapterType, buildOpencodeAdapterPayload, buildOpencodeAdapterEvent, attachAdapterEvent, selectRawEventId, buildRawEventEnvelope, trimEventQueue, parsePositiveInt, resolveInjectTokenBudget, reserveContextPrefixBudget, wrapInjectedContext, estimateTokens, countRetainedInjectionTokens, retainedMemoryFingerprints, retainedMetadataGaps, filterRetainedPack, isContinuationOnly, resolveRetainedTokenBudget, setSessionMessageInjectionCacheEntry, workingContextDigest, DEFAULT_INJECT_TOKEN_BUDGET, CODEMEM_CONTEXT_PREFIX, createViewerHealthMonitor, };