Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

pre-import asyncio in the kernel bootstrap (#315)

ktc630b71ea9c2
diff --git a/packages/coding-agent/src/core/kernel/state-snapshot.ts b/packages/coding-agent/src/core/kernel/state-snapshot.ts
index 17032ba9cb..6de1219277 100644
--- a/packages/coding-agent/src/core/kernel/state-snapshot.ts
+++ b/packages/coding-agent/src/core/kernel/state-snapshot.ts
@@ -73,9 +73,9 @@ def _prime_agent_snapshot_state():
ip = None
ns = ip.user_ns if ip is not None else _b.globals()
hidden = _b.set(_b.getattr(ip, "user_ns_hidden", {}) or {}) if ip is not None else _b.set()
- # rlm and the wrapped skill modules are live, connection-bound handles that
- # the bootstrap re-creates on restore; never snapshot them.
- always_skip = {"rlm", "In", "Out", "get_ipython", "exit", "quit", "open"}
+ # rlm and asyncio are re-created by the kernel bootstrap on every start;
+ # never snapshot them.
+ always_skip = {"rlm", "asyncio", "In", "Out", "get_ipython", "exit", "quit", "open"}
payload = {}
skipped = []
@@ -205,7 +205,7 @@ def _prime_agent_list_state_names():
ip = None
ns = ip.user_ns if ip is not None else _b.globals()
hidden = _b.set(_b.getattr(ip, "user_ns_hidden", {}) or {}) if ip is not None else _b.set()
- always_skip = {"rlm", "In", "Out", "get_ipython", "exit", "quit", "open"}
+ always_skip = {"rlm", "asyncio", "In", "Out", "get_ipython", "exit", "quit", "open"}
names = []
for name in _b.list(ns.keys()):
if name.startswith("_") or name in hidden or name in always_skip:
diff --git a/packages/coding-agent/src/core/prompts/rlm.ts b/packages/coding-agent/src/core/prompts/rlm.ts
index 0dcc86583d..6f614f1135 100644
--- a/packages/coding-agent/src/core/prompts/rlm.ts
+++ b/packages/coding-agent/src/core/prompts/rlm.ts
@@ -76,7 +76,7 @@ export function buildRlmPrompt(options: RlmPromptOptions): string {
parts.push(
"",
"A callable `rlm` is already in your global namespace — call it directly with `await rlm('sub-task')` to spawn a recursive sub-agent. Returns an `RLMResult` with `.answer` (string), `.usage`, `.turns`, and `.session_dir`.",
- "For parallel sub-agents, use normal Python async patterns such as `await asyncio.gather(rlm('task1'), rlm('task2'))`.",
+ "For parallel sub-agents, use normal Python async patterns such as `await asyncio.gather(rlm('task1'), rlm('task2'))`; `asyncio` is already imported.",
"For sub-agent work that can run in the background, keep the task handle from `asyncio.create_task(rlm('sub-task'))` so you do not block the main execution path; use normal task callbacks, `task.done()`, or `await task` later to observe completion and read the returned `RLMResult.answer`.",
);
}
diff --git a/packages/coding-agent/src/core/tools/ipython.ts b/packages/coding-agent/src/core/tools/ipython.ts
index 2f63cb96a7..9dbd62cbdc 100644
--- a/packages/coding-agent/src/core/tools/ipython.ts
+++ b/packages/coding-agent/src/core/tools/ipython.ts
@@ -18,6 +18,8 @@ import type { PythonSkillRuntimeInfo } from "../skills.js";
import { wrapToolDefinition } from "./tool-definition-wrapper.js";
const RLM_BOOTSTRAP_BASE_CODE = `
+import asyncio
+
try:
import nest_asyncio as _prime_agent_nest_asyncio
_prime_agent_nest_asyncio.apply()
diff --git a/packages/coding-agent/test/ipython-bootstrap.test.ts b/packages/coding-agent/test/ipython-bootstrap.test.ts
index 686558143a..d122d4564a 100644
--- a/packages/coding-agent/test/ipython-bootstrap.test.ts
+++ b/packages/coding-agent/test/ipython-bootstrap.test.ts
@@ -1,7 +1,16 @@
-import { describe, expect, it } from "vitest";
+import { spawnSync } from "node:child_process";
+import { existsSync, mkdtempSync, rmSync } from "node:fs";
+import { homedir, tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterAll, describe, expect, it } from "vitest";
+import { KernelManager } from "../src/core/kernel/index.js";
import { buildRlmBootstrapCode } from "../src/core/tools/ipython.js";
describe("IPython RLM bootstrap", () => {
+ it("pre-imports asyncio so the prompt's subagent patterns work without a manual import", () => {
+ expect(buildRlmBootstrapCode()).toMatch(/^import asyncio$/m);
+ });
+
it("guards Python skill imports so a broken skill does not abort bootstrap", () => {
const code = buildRlmBootstrapCode([
{
@@ -18,3 +27,43 @@ describe("IPython RLM bootstrap", () => {
expect(code).toContain("globals()[_prime_agent_skill_name] = _PrimeAgentUnavailableSkill");
});
});
+
+/** Find a python that can launch an ipykernel, or null to skip. */
+function resolveKernelPython(): string | null {
+ const candidates = [
+ process.env.PRIME_AGENT_KERNEL_PYTHON,
+ join(homedir(), ".prime", "agent", "kernel-venv", "bin", "python"),
+ ].filter((p): p is string => Boolean(p));
+ for (const python of candidates) {
+ if (!existsSync(python)) continue;
+ const check = spawnSync(python, ["-c", "import ipykernel"], { encoding: "utf8" });
+ if (check.status === 0) return python;
+ }
+ return null;
+}
+
+const python = resolveKernelPython();
+const describeIfKernel = python ? describe : describe.skip;
+
+describeIfKernel("IPython RLM bootstrap (real kernel)", () => {
+ const dir = mkdtempSync(join(tmpdir(), "prime-agent-bootstrap-"));
+
+ afterAll(() => {
+ rmSync(dir, { recursive: true, force: true });
+ });
+
+ it("binds asyncio in the user namespace", async () => {
+ const manager = new KernelManager({ python: python as string, cwd: dir });
+ try {
+ await manager.start();
+ const bootstrap = await manager.execute(buildRlmBootstrapCode());
+ expect(bootstrap.status).toBe("ok");
+
+ const result = await manager.execute("_t = asyncio.create_task(asyncio.sleep(0))\nprint(type(_t).__name__)");
+ expect(result.status).toBe("ok");
+ expect(result.stdout).toContain("Task");
+ } finally {
+ await manager.dispose();
+ }
+ }, 60_000);
+});