A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
[RSI, performance] Land the kernel skill-sync marker so killed sessions do not re-pay it (#2405)
Seth Kartend96a990b408d
diff --git a/packages/coding-agent/.changes/perf-skill-sync-marker.md b/packages/coding-agent/.changes/perf-skill-sync-marker.mdnew file mode 100644index 0000000000..01f7ec0bb1--- /dev/null+++ b/packages/coding-agent/.changes/perf-skill-sync-marker.md@@ -0,0 +1 @@+- Fixed short-lived sessions re-paying the whole Python kernel skill sync or wiping and rebuilding the venv after being killed mid-sync: the bootstrap version marker is now written atomically and persisted incrementally (base first, then after every installed skill), so the next session resumes only the remaining skills instead of starting over.diff --git a/packages/coding-agent/src/core/kernel/bootstrap.ts b/packages/coding-agent/src/core/kernel/bootstrap.tsindex 2c8a597b2c..0f952f9b84 100644--- a/packages/coding-agent/src/core/kernel/bootstrap.ts+++ b/packages/coding-agent/src/core/kernel/bootstrap.ts@@ -1,6 +1,6 @@import { createHash, randomUUID } from "node:crypto";import { constants, existsSync, readdirSync, readFileSync } from "node:fs";-import { access, mkdir, readdir, readFile, rm, stat, writeFile } from "node:fs/promises";+import { access, mkdir, readdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";import os from "node:os";import path from "node:path";import { stderr, stdin } from "node:process";@@ -97,8 +97,13 @@ const REQUIRED_HARNESS_METHODS = [];export const RUNTIME_READY_CHECK = `import inspect; import rlm; from rlm import McpIntegration; import rlm.mcp as mcp; from rlm.harness import HarnessEntry; _harness_methods = ${JSON.stringify(REQUIRED_HARNESS_METHODS)}; assert callable(mcp.list_tools); assert callable(mcp.call_tool); assert all(callable(getattr(mcp, _m, None)) for _m in ${JSON.stringify(REQUIRED_MCP_DISCOVERY_METHODS)}), "rlm.mcp is missing MCP discovery methods (list_plugins, search_plugins, list_connections, search_tools, describe_tool); the kernel venv needs a current prime-agent-runtime"; assert callable(rlm.spawn); assert hasattr(rlm, 'rlm'); assert callable(rlm.rlm.spawn); assert inspect.signature(rlm.spawn).parameters['name'].default is inspect.Parameter.empty; assert not hasattr(rlm, 'run'); assert not hasattr(rlm.rlm, 'run'); assert callable(rlm.host_request); assert callable(rlm.find_models); assert callable(rlm.rlm.find_models); assert callable(rlm.create_session); assert callable(rlm.rlm.create_session); assert callable(rlm.progress_note); assert callable(rlm.rlm.progress_note); assert hasattr(rlm, 'harness'); assert hasattr(rlm, 'get_harness_state'); assert hasattr(rlm.rlm, 'harness'); assert hasattr(rlm.rlm, 'get_harness_state'); assert all(callable(getattr(_harness, _method, None)) for _harness in (rlm.harness, rlm.rlm.harness) for _method in _harness_methods); assert 'reference' in HarnessEntry.__dataclass_fields__; assert 'scope' in HarnessEntry.__dataclass_fields__; assert 'reference' in inspect.signature(rlm.harness.create_skill).parameters; assert 'reference' in inspect.signature(rlm.harness.update_skill).parameters; assert 'global_' in inspect.signature(rlm.harness.create_memory).parameters; assert 'global_' in inspect.signature(rlm.get_harness_state).parameters; assert not hasattr(rlm, 'background'); assert not hasattr(rlm.rlm, 'background'); from rlm.bash import BashHandle, BashResult; assert callable(rlm.bash); assert all(callable(getattr(BashHandle, _m, None)) for _m in ('tail', 'output', 'poll', 'kill')); assert {'exit_code', 'output', 'duration'} <= set(BashResult.__dataclass_fields__); import rlm.repl as _repl; assert callable(_repl.main); assert callable(_repl.emit); assert callable(_repl.host_request); assert callable(_repl.is_active); assert _repl.PROTOCOL_VERSION == 3; assert callable(rlm.emit); assert not hasattr(rlm, 'HOST_COMM_TARGET'); assert not hasattr(mcp, 'install_shutdown_hook')`;const BOOTSTRAP_VERSION_FILE = ".bootstrap-version";+const BOOTSTRAP_VERSION_TMP_FILE = `${BOOTSTRAP_VERSION_FILE}.tmp`;const BOOTSTRAP_LOCK_NAME = ".bootstrap.lock";const BOOTSTRAP_LOCK_RETRY_MS = 100;+// Bounded retry for the atomic marker swap: replacing an existing marker can+// fail while a scanner or editor holds the file open.+const BOOTSTRAP_MARKER_SWAP_ATTEMPTS = 3;+const BOOTSTRAP_MARKER_SWAP_RETRY_MS = 50;const BOOTSTRAP_LOCK_STALE_WITHOUT_PID_MS = 30_000;let inFlightEnsureKernelPython: { key: string; promise: Promise<string> } | null = null;@@ -689,7 +694,51 @@ async function writeBootstrapVersion(extraUvArgs: DEFAULT_RLM_EXTRA_UV_ARGS,pythonSkills: [...pythonSkills],};- await writeFile(path.join(venv, BOOTSTRAP_VERSION_FILE), `${JSON.stringify(version)}\n`, "utf8");+ const filePath = path.join(venv, BOOTSTRAP_VERSION_FILE);+ const tmpPath = path.join(venv, BOOTSTRAP_VERSION_TMP_FILE);+ const serialized = `${JSON.stringify(version)}\n`;+ // Write-then-rename is atomic: a kill mid-write can never leave a partial+ // marker, which would read as absent and force a rebuild. Replacing an+ // existing marker can fail transiently while another process holds it, so+ // retry the swap. A marker that stays unwritten is only stale: the next+ // startup re-syncs skills, whereas an in-place overwrite truncated by a+ // failure or a kill would read as absent and rebuild the whole venv.+ let lastError: unknown;+ for (let attempt = 1; attempt <= BOOTSTRAP_MARKER_SWAP_ATTEMPTS; attempt += 1) {+ try {+ await writeFile(tmpPath, serialized, "utf8");+ await rename(tmpPath, filePath);+ return;+ } catch (error) {+ lastError = error;+ // Retry below; the previous marker is still intact.+ }+ if (attempt < BOOTSTRAP_MARKER_SWAP_ATTEMPTS) await sleep(BOOTSTRAP_MARKER_SWAP_RETRY_MS);+ }+ // Give up without overwriting the marker in place: a write truncated there+ // by a failure or a kill reads as absent and forces a full venv rebuild,+ // while the untouched previous marker stays valid. The failure still+ // surfaces: a marker this process cannot write is one the next startup+ // cannot trust.+ await rm(tmpPath, { force: true }).catch(() => undefined);+ throw lastError;+}++// Incremental marker write that merges fresh entries into the skills already+// recorded on disk. A missing or corrupt marker contributes no base entries.+async function writeMergedBootstrapVersion(+ venv: string,+ runtimeIdentity: string,+ pythonSkills: readonly BootstrapPythonSkill[],+): Promise<void> {+ const version = await readBootstrapVersion(venv);+ const merged = new Map(+ (version?.pythonSkills ?? []).map((skill) => [`${skill.importName}\0${skill.packagePath}`, skill]),+ );+ for (const skill of pythonSkills) {+ merged.set(`${skill.importName}\0${skill.packagePath}`, skill);+ }+ await writeBootstrapVersion(venv, runtimeIdentity, [...merged.values()]);}function runtimeCandidateDirs(): string[] {@@ -781,6 +830,10 @@ async function bootstrapVenv(STATE_SNAPSHOT_REQUIREMENT,...DEFAULT_RLM_EXTRA_UV_ARGS,]);+ // Land the base marker before the skill sync: a session killed mid-sync+ // must leave the next one on the skills-only path instead of wiping the+ // venv and re-paying the runtime install.+ await writeBootstrapVersion(venv, runtimeIdentity, []);await syncPythonSkills(uv, venv, python, runtimeIdentity, pythonSkills, options);}@@ -848,16 +901,23 @@ async function syncPythonSkills(...formatPythonSkillInstallArgs(skill),...localDependencyArgs,]);- installedPythonSkills.push(- skill,- ...localDependencies.filter((dependency) => !installedPythonSkills.includes(dependency)),- );} catch (error) {reportProgress(options,`Warning: Python skill ${skill.importName} failed to install and will be unavailable: ${errorMessage(error)}`,);+ continue;}+ installedPythonSkills.push(+ skill,+ ...localDependencies.filter((dependency) => !installedPythonSkills.includes(dependency)),+ );+ // Persist progress after every completed install so a killed session+ // resumes at the first missing skill instead of re-syncing from scratch.+ // Merge with the on-disk marker so skills already recorded but not yet+ // visited this sync (they sit later in install order) survive this+ // incremental write; the final write below stays an authoritative replace.+ await writeMergedBootstrapVersion(venv, runtimeIdentity, installedPythonSkills);}await writeBootstrapVersion(venv, runtimeIdentity, installedPythonSkills);}diff --git a/packages/coding-agent/test/helpers/kernel-sync-child.ts b/packages/coding-agent/test/helpers/kernel-sync-child.tsnew file mode 100644index 0000000000..9a2b821f53--- /dev/null+++ b/packages/coding-agent/test/helpers/kernel-sync-child.ts@@ -0,0 +1,11 @@+import { ensureKernelPython, type KernelPythonSkill } from "../../src/core/kernel/bootstrap.js";++// Runs one ensureKernelPython pass as its own process so a test can SIGKILL it+// mid-sync and prove that the next pass resumes instead of rebuilding.+const skillsArg = process.env.KERNEL_SYNC_CHILD_SKILLS;+if (!skillsArg) {+ throw new Error("KERNEL_SYNC_CHILD_SKILLS is required");+}+const pythonSkills: readonly KernelPythonSkill[] = JSON.parse(skillsArg);+const python = await ensureKernelPython({ pythonSkills });+console.log(`KERNEL-SYNC-CHILD-DONE ${python}`);diff --git a/packages/coding-agent/test/kernel-bootstrap.test.ts b/packages/coding-agent/test/kernel-bootstrap.test.tsindex 0e00dd8dde..fb568881b9 100644--- a/packages/coding-agent/test/kernel-bootstrap.test.ts+++ b/packages/coding-agent/test/kernel-bootstrap.test.ts@@ -1,7 +1,9 @@+import { spawn } from "node:child_process";import { createHash } from "node:crypto";import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";-import { join } from "node:path";+import { join, resolve } from "node:path";+import { setTimeout as sleep } from "node:timers/promises";import { afterEach, beforeEach, describe, expect, it } from "vitest";import {DEFAULT_RLM_EXTRA_IMPORT_NAMES,@@ -12,6 +14,9 @@ import {resolveRuntimeIdentity,} from "../src/core/kernel/bootstrap.js";+const tsxPath = resolve(__dirname, "../../../node_modules/tsx/dist/cli.mjs");+const kernelSyncChildPath = resolve(__dirname, "helpers/kernel-sync-child.ts");+let tempDir = "";let originalEnv: NodeJS.ProcessEnv;let runtimeIdentity = "";@@ -77,9 +82,17 @@ dependencies = ["${dependencyName}"]return skill;}-function writeFakePython(filePath: string, importableModules: readonly string[]): void {+function writeFakePython(+ filePath: string,+ importableModules: readonly string[],+ deniedProbes: readonly string[] = [],+): void {const cases = importableModules.map((moduleName) => ` "import ${moduleName}") exit 0 ;;`).join("\n");- const runtimeCase = importableModules.includes("rlm") ? ' *"_harness_methods"*) exit 0 ;;' : "";+ const denyCases = deniedProbes.map((probe) => ` *"${probe}"*) exit 1 ;;`).join("\n");+ const runtimeCase =+ importableModules.includes("rlm") && !deniedProbes.includes("_harness_methods")+ ? ' *"_harness_methods"*) exit 0 ;;'+ : "";writeExecutable(filePath,[@@ -87,6 +100,7 @@ function writeFakePython(filePath: string, importableModules: readonly string[])'if [ "$1" = "-c" ]; then',' case "$2" in',cases,+ denyCases,runtimeCase," *) exit 1 ;;"," esac",@@ -132,11 +146,31 @@ function installFakeUv(): string {" exit 0","fi",'if [ "$1" = "pip" ]; then',+ ' marker_file=""',+ ' seen_editable=""',+ ' prev=""',' for arg in "$@"; do',+ ' if [ "$prev" = "--python" ]; then',+ ' marker_file="$(dirname "$arg")/../.bootstrap-version"',+ " fi",+ ' if [ "$arg" = "--editable" ]; then',+ " seen_editable=1",+ " fi",+ ' if [ "$UV_HANG_ARG" != "" ] && [ "$arg" = "$UV_HANG_ARG" ]; then',+ " sleep 30",+ " fi",' if [ "$UV_FAIL_ARG" != "" ] && [ "$arg" = "$UV_FAIL_ARG" ]; then'," exit 1"," fi",+ ' prev="$arg"'," done",+ ' if [ "$seen_editable" != "" ] && [ "$marker_file" != "" ]; then',+ ' if [ -f "$marker_file" ]; then',+ ' printf "MARKER %s\n" "$(cat "$marker_file")" >> "$UV_LOG"',+ " else",+ ' printf "MARKER missing\n" >> "$UV_LOG"',+ " fi",+ " fi"," exit 0","fi","exit 2",@@ -235,33 +269,6 @@ describe("kernel bootstrap", () => {expect(log).toContain(`--editable ${dependentSkill.packagePath}`);});- it("syncs a warm venv when a Python skill pyproject changes", async () => {- const logPath = installFakeUv();- const venv = join(tempDir, "kernel-venv");- const python = join(venv, "bin", "python");- const pythonSkill = createPythonSkill();- mkdirSync(join(venv, "bin"), { recursive: true });- writeFakePython(python, ["rlm", ...DEFAULT_RLM_EXTRA_IMPORT_NAMES]);- writeBootstrapVersion(venv, [pythonSkill]);- writeFileSync(- pythonSkill.pyprojectPath,- `[project]-name = "${pythonSkill.name}"-version = "0.1.0"-dependencies = ["httpx"]-`,- );- process.env.PRIME_AGENT_KERNEL_VENV = venv;-- await expect(ensureKernelPython({ pythonSkills: [pythonSkill] })).resolves.toBe(python);-- const log = readFileSync(logPath, "utf8");- expect(log).not.toContain(`venv ${venv} --python 3.11`);- expect(log).toContain(`--editable ${pythonSkill.packagePath}`);- const version = JSON.parse(readFileSync(join(venv, ".bootstrap-version"), "utf8"));- expect(version.pythonSkills[0].pyprojectHash).toBe(pyprojectHash(pythonSkill.pyprojectPath));- });-it("preserves recorded Python skills when a no-skill bootstrap call reuses a warm venv", async () => {installFakeUv();const venv = join(tempDir, "kernel-venv");@@ -300,40 +307,99 @@ dependencies = ["httpx"]expect(readFileSync(logPath, "utf8")).toBe(syncedLog);});- it("continues when a Python skill editable install fails and retries it next startup", async () => {+ it("writes the base marker before the first install and keeps a failed skill out of the record", async () => {const logPath = installFakeUv();const venv = join(tempDir, "kernel-venv");- const goodSkill = createPythonSkill("good-skill");- const brokenSkill = createPythonSkill("broken-skill");+ const installedSkill = createPythonSkill("agent-a");+ const brokenSkill = createPythonSkill("agent-b");process.env.PRIME_AGENT_KERNEL_VENV = venv;process.env.UV_FAIL_ARG = brokenSkill.packagePath;- await expect(ensureKernelPython({ pythonSkills: [goodSkill, brokenSkill] })).resolves.toBe(+ await expect(ensureKernelPython({ pythonSkills: [installedSkill, brokenSkill] })).resolves.toBe(join(venv, "bin", "python"),);-- const log = readFileSync(logPath, "utf8");- expect(log).toContain(`--editable ${goodSkill.packagePath}`);- expect(log).toContain(`--editable ${brokenSkill.packagePath}`);+ const markers = readFileSync(logPath, "utf8")+ .split("\n")+ .filter((line) => line.startsWith("MARKER "))+ .map((probe) => probe.slice("MARKER ".length));+ expect(JSON.parse(markers[0]).pythonSkills).toEqual([]);const version = JSON.parse(readFileSync(join(venv, ".bootstrap-version"), "utf8"));- expect(version.pythonSkills).toEqual([- {- importName: goodSkill.importName,- packagePath: goodSkill.packagePath,- pyprojectPath: goodSkill.pyprojectPath,- pyprojectHash: pyprojectHash(goodSkill.pyprojectPath),+ expect(version.pythonSkills.map((skill: { importName: string }) => skill.importName)).toEqual([+ installedSkill.importName,+ ]);+ });++ // test-policy: allow explicit-test-timeout -- bounds real killed tsx respawn and resume variance, not the assertion+ it("resumes a real killed mid-sync process without rebuilding the venv", { timeout: 60_000 }, async () => {+ const logPath = installFakeUv();+ const venv = join(tempDir, "kernel-venv");+ const python = join(venv, "bin", "python");+ const staleSkill = createPythonSkill("agent-a");+ const recordedSkill = createPythonSkill("agent-b");+ const hangingSkill = createPythonSkill("agent-c");+ mkdirSync(join(venv, "bin"), { recursive: true });+ writeFakePython(python, ["rlm", ...DEFAULT_RLM_EXTRA_IMPORT_NAMES]);+ writeBootstrapVersion(venv, [staleSkill, recordedSkill]);+ writeFileSync(staleSkill.pyprojectPath, `[project]\nname = "${staleSkill.name}"\nversion = "0.2.0"\n`);+ process.env.PRIME_AGENT_KERNEL_VENV = venv;++ const child = spawn(process.execPath, [tsxPath, kernelSyncChildPath], {+ env: {+ ...process.env,+ KERNEL_SYNC_CHILD_SKILLS: JSON.stringify([staleSkill, recordedSkill, hangingSkill]),+ TSX_TSCONFIG_PATH: resolve(__dirname, "../../../tsconfig.json"),+ UV_HANG_ARG: hangingSkill.packagePath,},+ stdio: "ignore",+ detached: true,+ });+ try {+ const deadline = Date.now() + 30_000;+ while (Date.now() < deadline) {+ if (child.exitCode !== null || child.signalCode !== null) break;+ const log = existsSync(logPath) ? readFileSync(logPath, "utf8") : "";+ if (+ log.includes(`--editable ${staleSkill.packagePath}`) &&+ log.includes(`--editable ${hangingSkill.packagePath}`)+ ) {+ break;+ }+ // test-policy: allow wall-clock-sleep -- polls the external fake-uv log appends; no in-process signal exists+ await sleep(50);+ }+ } finally {+ const pid = child.pid;+ if (pid !== undefined) {+ try {+ process.kill(-pid, "SIGKILL");+ } catch {+ child.kill("SIGKILL");+ }+ }+ await new Promise((resolve) => child.once("exit", resolve));+ }+ const versionAtKill = JSON.parse(readFileSync(join(venv, ".bootstrap-version"), "utf8"));+ expect(versionAtKill.pythonSkills.map((skill: { importName: string }) => skill.importName)).toEqual([+ staleSkill.importName,+ recordedSkill.importName,]);+ expect(versionAtKill.pythonSkills[0].pyprojectHash).toBe(pyprojectHash(staleSkill.pyprojectPath));- await expect(ensureKernelPython({ pythonSkills: [goodSkill, brokenSkill] })).resolves.toBe(- join(venv, "bin", "python"),+ await expect(ensureKernelPython({ pythonSkills: [staleSkill, recordedSkill, hangingSkill] })).resolves.toBe(+ python,);- const retryLog = readFileSync(logPath, "utf8");- expect(retryLog.split("\n").filter((line) => line.startsWith(`venv ${venv} `))).toHaveLength(1);- expect(- retryLog.split("\n").filter((line) => line.includes(`--editable ${brokenSkill.packagePath}`)),- ).toHaveLength(2);+ const lines = readFileSync(logPath, "utf8").split("\n");+ expect(lines.some((line) => line.startsWith(`venv ${venv} `))).toBe(false);+ expect(lines.filter((line) => line.includes(`--editable ${staleSkill.packagePath}`))).toHaveLength(1);+ expect(lines.filter((line) => line.includes(`--editable ${recordedSkill.packagePath}`))).toHaveLength(0);+ expect(lines.filter((line) => line.includes(`--editable ${hangingSkill.packagePath}`))).toHaveLength(2);+ const version = JSON.parse(readFileSync(join(venv, ".bootstrap-version"), "utf8"));+ expect(version.pythonSkills.map((skill: { importName: string }) => skill.importName)).toEqual([+ staleSkill.importName,+ recordedSkill.importName,+ hangingSkill.importName,+ ]);});it("shares concurrent bootstrap work in one process", async () => {@@ -402,20 +468,7 @@ dependencies = ["httpx"]rebuilds: true,prepare: (venv: string, python: string) => {// Imports rlm but fails the runtime-capability probe.- writeExecutable(- python,- [- "#!/bin/sh",- 'if [ "$1" = "-c" ]; then',- ' case "$2" in',- ' "import rlm") exit 0 ;;',- " *) exit 1 ;;",- " esac",- "fi",- "exit 0",- "",- ].join("\n"),- );+ writeFakePython(python, ["rlm"], ["_harness_methods"]);writeBootstrapVersion(venv);},},@@ -472,45 +525,13 @@ dependencies = ["httpx"]},{name: "rejects an interpreter exposing only the legacy harness API",- write: (path: string) =>- writeExecutable(- path,- [- "#!/bin/sh",- 'if [ "$1" = "-c" ]; then',- ' case "$2" in',- ' "import rlm") exit 0 ;;',- ' *"_harness_methods"*) exit 1 ;;',- " *\"assert not hasattr(rlm.rlm, 'background')\"*) exit 0 ;;",- " *) exit 1 ;;",- " esac",- "fi",- "exit 0",- "",- ].join("\n"),- ),+ write: (path: string) => writeFakePython(path, ["rlm"], ["_harness_methods"]),withSkill: false,error: /current prime-agent-runtime with callable rlm\.spawn/,},{name: "rejects an interpreter with a pre-progress-note rlm runtime",- write: (path: string) =>- writeExecutable(- path,- [- "#!/bin/sh",- 'if [ "$1" = "-c" ]; then',- ' case "$2" in',- ' "import rlm") exit 0 ;;',- ' *"progress_note"*) exit 1 ;;',- ' *"_harness_methods"*) exit 0 ;;',- " *) exit 1 ;;",- " esac",- "fi",- "exit 0",- "",- ].join("\n"),- ),+ write: (path: string) => writeFakePython(path, ["rlm"], ["progress_note"]),withSkill: false,error: /current prime-agent-runtime with callable rlm\.spawn, rlm\.create_session, rlm\.host_request, rlm\.progress_note/,},