Skip to content

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

Rust

add initial attempt (#306)

az2b2da9a49c74
diff --git a/packages/coding-agent/src/core/prompts/index.ts b/packages/coding-agent/src/core/prompts/index.ts
index 58a23ab1f0..157a75346d 100644
--- a/packages/coding-agent/src/core/prompts/index.ts
+++ b/packages/coding-agent/src/core/prompts/index.ts
@@ -1 +1 @@
-export { buildRlmPrompt, type RlmPromptOptions } from "./rlm.js";
+export { buildRlmPrompt, buildSubagentGuidance, type RlmPromptOptions } from "./rlm.js";
diff --git a/packages/coding-agent/src/core/prompts/rlm.ts b/packages/coding-agent/src/core/prompts/rlm.ts
index 6f614f1135..ee04b12a93 100644
--- a/packages/coding-agent/src/core/prompts/rlm.ts
+++ b/packages/coding-agent/src/core/prompts/rlm.ts
@@ -87,3 +87,48 @@ export function buildRlmPrompt(options: RlmPromptOptions): string {
return parts.join("\n");
}
+
+/**
+ * Supplemental sub-agent delegation guidance, appended AFTER the trained
+ * `buildRlmPrompt` prefix (see system-prompt.ts). This is intentionally NOT part of
+ * `buildRlmPrompt`'s `parts` array: the text that function emits is frozen as the v1
+ * training prefix and must stay byte-identical. The trained recursion block already
+ * covers the mechanics (`await rlm(...)`, `asyncio.gather`, `asyncio.create_task`);
+ * this block adds the *when* and *why* — the decision guidance the trained prefix
+ * lacks — in the same When -> Why -> (menu) order Claude Code's Agent tool uses. The
+ * subagent-spec "menu" itself renders just after this, inside the harness-state block.
+ */
+export function buildSubagentGuidance(): string {
+ return [
+ "# Delegating to sub-agents",
+ "",
+ "You already have `rlm` in scope. This is about *when* to spawn one — which matters as much as how.",
+ "",
+ "Reach for `await rlm(...)` when:",
+ "- you have independent sub-tasks that can run in parallel — fan them out with `await asyncio.gather(rlm('task1'), rlm('task2'))` rather than working them one after another;",
+ "- a sub-task would mean reading across many files, outputs, or sources you don't need to keep — delegate it and you keep the answer, not the raw material;",
+ "- the sub-task matches one of your saved subagent specs (listed in the harness state below) — turn the spec into a concise task prompt and call `rlm` with it.",
+ "",
+ "Do it inline instead when the step is a single known lookup, edit, or command — there, a sub-agent just adds latency. Once you've delegated a sub-task, await its handle and use the result instead of redoing the work yourself.",
+ "",
+ "For example:",
+ "```python",
+ "# Independent sub-tasks in parallel — each returns just its conclusion, not the files it read",
+ "auth, api = await asyncio.gather(",
+ " rlm('Summarize how authentication works in this repo: entrypoints, token flow, and key files.'),",
+ " rlm('Summarize the HTTP API layer: routes, middleware, and error handling.'),",
+ ")",
+ "",
+ "# Context isolation — a sub-agent digests a large file and hands back only the answer",
+ "res = await rlm('Read build.log, find the failing step and its root cause, and report it in 3 lines.')",
+ "print(res.answer)",
+ "",
+ "# Background — kick off a slow sub-task, keep working, collect it later",
+ "task = asyncio.create_task(rlm('Run the full test suite and report any failures with root causes.'))",
+ "# ... continue with other work while it runs ...",
+ "failures = (await task).answer",
+ "```",
+ "",
+ "These are illustrations, not a fixed menu: delegate any self-contained sub-task that fits the cases above.",
+ ].join("\n");
+}
diff --git a/packages/coding-agent/src/core/refinement/refinement.ts b/packages/coding-agent/src/core/refinement/refinement.ts
index 3791eb5131..f4b1ad481d 100644
--- a/packages/coding-agent/src/core/refinement/refinement.ts
+++ b/packages/coding-agent/src/core/refinement/refinement.ts
@@ -415,7 +415,16 @@ export function formatHarnessStateForPrompt(
[a.path, a.title, a.id].join("\0").localeCompare([b.path, b.title, b.id].join("\0")),
);
totalEntries += entries.length;
- lines.push(`${kind}: ${entries.length}`);
+ // Render subagent specs as a task-shaped roster the model can match against — the
+ // analogue of Claude Code's agent-type menu — rather than a bare count. The
+ // invocation hint makes clear each spec is reached through the single `rlm` entrypoint.
+ if (kind === "subagent" && entries.length > 0) {
+ lines.push(
+ `${kind}: ${entries.length} (invoke a spec by turning it into a concise task prompt and calling \`await rlm('<task>')\`)`,
+ );
+ } else {
+ lines.push(`${kind}: ${entries.length}`);
+ }
for (const entry of entries.slice(0, maxEntriesPerKind)) {
const argumentsText =
entry.kind === "skill" && Object.keys(entry.arguments).length > 0
diff --git a/packages/coding-agent/src/core/system-prompt.ts b/packages/coding-agent/src/core/system-prompt.ts
index ad3e0a0033..f3d573015c 100644
--- a/packages/coding-agent/src/core/system-prompt.ts
+++ b/packages/coding-agent/src/core/system-prompt.ts
@@ -2,7 +2,7 @@
* System prompt construction and project context loading
*/
-import { buildRlmPrompt } from "./prompts/index.js";
+import { buildRlmPrompt, buildSubagentGuidance } from "./prompts/index.js";
import { formatHarnessStateForPrompt, type HarnessState } from "./refinement/index.js";
import { formatSkillsForPrompt, getPythonSkillRuntimeInfo, type Skill } from "./skills.js";
@@ -103,6 +103,13 @@ export function buildSystemPrompt(options: BuildSystemPromptOptions): string {
allowRecursion,
});
+ // Appended AFTER the trained buildRlmPrompt prefix, and before the harness-state
+ // menu, so the model reads when/why to delegate and then sees the concrete subagent
+ // specs it can match against — the same ordering as Claude Code's Agent tool.
+ if (allowRecursion ?? true) {
+ prompt += `\n\n${buildSubagentGuidance()}`;
+ }
+
if (harnessState) {
prompt += `\n\n${formatHarnessStateForPrompt(harnessState)}`;
}