A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
fix(kernel): compare recorded Python skills by key and pin the marker swap (#2499)
Sebastian Müller5062b1b31eaf
diff --git a/packages/coding-agent/.changes/kernel-skill-marker-ready-gate.md b/packages/coding-agent/.changes/kernel-skill-marker-ready-gate.mdnew file mode 100644index 0000000000..7b1a72ad5d--- /dev/null+++ b/packages/coding-agent/.changes/kernel-skill-marker-ready-gate.md@@ -0,0 +1 @@+- Fixed kernel startup re-running the Python skill sync and rewriting the venv marker on every session when a skill depended on a sibling skill; such venvs now take the same zero-cost warm-start path as any other synced venv.diff --git a/packages/coding-agent/src/core/kernel/bootstrap.ts b/packages/coding-agent/src/core/kernel/bootstrap.tsindex 0f952f9b84..dd56934df1 100644--- a/packages/coding-agent/src/core/kernel/bootstrap.ts+++ b/packages/coding-agent/src/core/kernel/bootstrap.ts@@ -171,12 +171,16 @@ function fileContentHash(filePath: string): string {}}+function pythonSkillKey(skill: Pick<BootstrapPythonSkill, "importName" | "packagePath">): string {+ return `${skill.importName}\0${skill.packagePath}`;+}+function normalizePythonSkills(pythonSkills: readonly KernelPythonSkill[] | undefined): BootstrapPythonSkill[] {const byKey = new Map<string, BootstrapPythonSkill>();const addSkill = (skill: Pick<KernelPythonSkill, "importName" | "packagePath" | "pyprojectPath">): void => {const packagePath = path.resolve(skill.packagePath);const pyprojectPath = path.resolve(skill.pyprojectPath);- const key = `${skill.importName}\0${packagePath}`;+ const key = pythonSkillKey({ importName: skill.importName, packagePath });if (byKey.has(key)) {return;}@@ -647,18 +651,16 @@ function extraUvArgsMatch(a: string[] | undefined, b: string[] | undefined): booreturn a.every((v, i) => v === b[i]);}+// The marker lists skills in install order and the caller in path order, so compare by key, not index.function pythonSkillsMatch(a: BootstrapPythonSkill[] | undefined, b: readonly BootstrapPythonSkill[]): boolean {- const left = a ?? [];- if (left.length !== b.length) return false;- return left.every((skill, index) => {- const expected = b[index];- return (- skill.importName === expected.importName &&- skill.packagePath === expected.packagePath &&- skill.pyprojectPath === expected.pyprojectPath &&- skill.pyprojectHash === expected.pyprojectHash- );- });+ const recorded = new Map((a ?? []).map((skill) => [pythonSkillKey(skill), skill]));+ return (+ recorded.size === b.length &&+ b.every((skill) => {+ const match = recorded.get(pythonSkillKey(skill));+ return match?.pyprojectPath === skill.pyprojectPath && match.pyprojectHash === skill.pyprojectHash;+ })+ );}function bootstrapVersionCurrent(@@ -732,11 +734,9 @@ async function writeMergedBootstrapVersion(pythonSkills: readonly BootstrapPythonSkill[],): Promise<void> {const version = await readBootstrapVersion(venv);- const merged = new Map(- (version?.pythonSkills ?? []).map((skill) => [`${skill.importName}\0${skill.packagePath}`, skill]),- );+ const merged = new Map((version?.pythonSkills ?? []).map((skill) => [pythonSkillKey(skill), skill]));for (const skill of pythonSkills) {- merged.set(`${skill.importName}\0${skill.packagePath}`, skill);+ merged.set(pythonSkillKey(skill), skill);}await writeBootstrapVersion(venv, runtimeIdentity, [...merged.values()]);}@@ -847,9 +847,7 @@ async function syncPythonSkills(): Promise<void> {const version = await readBootstrapVersion(venv);const installedPythonSkills: BootstrapPythonSkill[] = [];- const currentPythonSkills = new Map(- (version?.pythonSkills ?? []).map((skill) => [`${skill.importName}\0${skill.packagePath}`, skill]),- );+ const currentPythonSkills = new Map((version?.pythonSkills ?? []).map((skill) => [pythonSkillKey(skill), skill]));const pythonSkillsByProjectName = new Map(pythonSkills.map((skill) => [readPythonSkillProjectName(skill).replaceAll("_", "-").toLowerCase(), skill]),);@@ -867,7 +865,7 @@ async function syncPythonSkills();for (const skill of sortPythonSkillsForInstall(pythonSkills)) {- const existingSkill = currentPythonSkills.get(`${skill.importName}\0${skill.packagePath}`);+ const existingSkill = currentPythonSkills.get(pythonSkillKey(skill));if (existingSkill?.pyprojectPath === skill.pyprojectPath && existingSkill.pyprojectHash === skill.pyprojectHash) {installedPythonSkills.push(skill);continue;@@ -876,7 +874,7 @@ async function syncPythonSkills(const localDependencies = dependenciesBySkill.get(skill) ?? [];const localDependencyArgs = localDependencies.filter((dependency) => {- const installedDependency = currentPythonSkills.get(`${dependency.importName}\0${dependency.packagePath}`);+ const installedDependency = currentPythonSkills.get(pythonSkillKey(dependency));const installedThisSync = installedPythonSkills.some((installed) =>installed.importName === dependency.importName &&diff --git a/packages/coding-agent/test/kernel-bootstrap.test.ts b/packages/coding-agent/test/kernel-bootstrap.test.tsindex fb568881b9..ae45032eed 100644--- a/packages/coding-agent/test/kernel-bootstrap.test.ts+++ b/packages/coding-agent/test/kernel-bootstrap.test.ts@@ -1,10 +1,11 @@import { spawn } from "node:child_process";import { createHash } from "node:crypto";import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";+import { stat, utimes } from "node:fs/promises";import { tmpdir } from "node:os";import { join, resolve } from "node:path";import { setTimeout as sleep } from "node:timers/promises";-import { afterEach, beforeEach, describe, expect, it } from "vitest";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import {DEFAULT_RLM_EXTRA_IMPORT_NAMES,DEFAULT_RLM_EXTRA_UV_ARGS,@@ -14,6 +15,17 @@ import {resolveRuntimeIdentity,} from "../src/core/kernel/bootstrap.js";+const renameFault = vi.hoisted(() => ({ remaining: 0 }));+vi.mock("node:fs/promises", async (importOriginal) => {+ const actual = await importOriginal<typeof import("node:fs/promises")>();+ const rename: typeof actual.rename = async (from, to) => {+ if (renameFault.remaining === 0) return actual.rename(from, to);+ renameFault.remaining -= 1;+ throw new Error("EBUSY: marker held open");+ };+ return { ...actual, rename };+});+const tsxPath = resolve(__dirname, "../../../node_modules/tsx/dist/cli.mjs");const kernelSyncChildPath = resolve(__dirname, "helpers/kernel-sync-child.ts");@@ -193,6 +205,7 @@ describe("kernel bootstrap", () => {});afterEach(() => {+ renameFault.remaining = 0;process.env = originalEnv;if (tempDir) {rmSync(tempDir, { recursive: true, force: true });@@ -269,51 +282,50 @@ describe("kernel bootstrap", () => {expect(log).toContain(`--editable ${dependentSkill.packagePath}`);});- it("preserves recorded Python skills when a no-skill bootstrap call reuses a warm venv", async () => {+ it("keeps the previous marker and surfaces the error when every marker swap fails", async () => {installFakeUv();const venv = join(tempDir, "kernel-venv");const python = join(venv, "bin", "python");- const pythonSkill = createPythonSkill();+ const markerPath = join(venv, ".bootstrap-version");mkdirSync(join(venv, "bin"), { recursive: true });writeFakePython(python, ["rlm", ...DEFAULT_RLM_EXTRA_IMPORT_NAMES]);- writeBootstrapVersion(venv, [pythonSkill]);+ writeBootstrapVersion(venv, [createPythonSkill()]);+ const marker = readFileSync(markerPath, "utf8");process.env.PRIME_AGENT_KERNEL_VENV = venv;+ renameFault.remaining = 3;- await expect(ensureKernelPython()).resolves.toBe(python);+ await expect(ensureKernelPython({ pythonSkills: [createPythonSkill("agent-b")] })).rejects.toThrow(/EBUSY/);- const version = JSON.parse(readFileSync(join(venv, ".bootstrap-version"), "utf8"));- expect(version.pythonSkills).toEqual([- {- importName: pythonSkill.importName,- packagePath: pythonSkill.packagePath,- pyprojectPath: pythonSkill.pyprojectPath,- pyprojectHash: pyprojectHash(pythonSkill.pyprojectPath),- },- ]);+ expect(readFileSync(markerPath, "utf8")).toBe(marker);+ expect(existsSync(`${markerPath}.tmp`)).toBe(false);});- it("keeps a skill-synced venv fast for real sessions after a no-skill bootstrap call", async () => {+ it("keeps a synced venv with a sibling dependency zero-cost across later and no-skill calls", async () => {const logPath = installFakeUv();const venv = join(tempDir, "kernel-venv");- const pythonSkill = createPythonSkill();+ createPythonSkill("agent-b");+ const pythonSkill = createPythonSkillWithDependency("agent-a", "agent-b");process.env.PRIME_AGENT_KERNEL_VENV = venv;await expect(ensureKernelPython({ pythonSkills: [pythonSkill] })).resolves.toBe(join(venv, "bin", "python"));const syncedLog = readFileSync(logPath, "utf8");+ await utimes(join(venv, ".bootstrap-version"), 0, 0);await expect(ensureKernelPython()).resolves.toBe(join(venv, "bin", "python"));await expect(ensureKernelPython({ pythonSkills: [pythonSkill] })).resolves.toBe(join(venv, "bin", "python"));expect(readFileSync(logPath, "utf8")).toBe(syncedLog);+ expect((await stat(join(venv, ".bootstrap-version"))).mtimeMs).toBe(0);});- it("writes the base marker before the first install and keeps a failed skill out of the record", async () => {+ it("retries a busy marker swap before the first install and keeps a failed skill out of the record", async () => {const logPath = installFakeUv();const venv = join(tempDir, "kernel-venv");const installedSkill = createPythonSkill("agent-a");const brokenSkill = createPythonSkill("agent-b");process.env.PRIME_AGENT_KERNEL_VENV = venv;process.env.UV_FAIL_ARG = brokenSkill.packagePath;+ renameFault.remaining = 2;await expect(ensureKernelPython({ pythonSkills: [installedSkill, brokenSkill] })).resolves.toBe(join(venv, "bin", "python"),