xonecas.io
64875
Mc

mini-coder / commit 38c152e

refactor(ralph): true subprocess loop — fresh process each iteration

Replace the in-process ralph loop (startNewSession + processUserInput)
with fresh mc subprocess calls via runSubagent.

Each iteration is now a completely isolated process. State persists via
git commits and the filesystem — not the context window. The worktree
infrastructure merges each iteration's branch back before the next one
starts, so subsequent iterations pick up all committed changes.

- Add buildRalphIterationPrompt() in agent-helpers.ts — wraps the goal
  with instructions to commit progress and only emit /ralph when done
- input-loop.ts: replace while loop with for loop over runSubagent calls;
  warn when image attachments are present (not forwarded to subprocess);
  move RALPH_MAX_ITERATIONS to module scope
- session-runner.ts: remove ralph system prompt injection from
  processUserInput — the main runner is no longer involved in iterations

Closes the 'in-harness scaffolding' anti-pattern described by Huntley:
'If you're implementing Ralph as part of the agent harness you are
missing the point of Ralph which is to always use a fresh context.'

xonecas2026-03-10 38c152e320cc2bc7a0f2e228ee4e3fd3cc6f5a14 parent e682a8b

3 files changed

src/agent/agent-helpers.ts+4−0
@@ -56,6 +56,10 @@ export function hasRalphSignal(text: string): boolean {
5656 return /\/ralph\b/.test(text);
5757 }
5858
59+export function buildRalphIterationPrompt(goal: string): string {
60+ return `${goal}\n\n<system-message>RALPH MODE: Work autonomously toward the goal. Commit your changes to git to persist progress. When the task is fully complete, output \`/ralph\` as your final message. If the task is not yet complete, do NOT output \`/ralph\`.</system-message>`;
61+}
62+
5963 export async function resolveFileRefs(
6064 text: string,
6165 cwd: string,
src/agent/input-loop.ts+54−23
@@ -4,7 +4,11 @@ import { type InputResult, readline } from "../cli/input.ts";
44 import { PREFIX } from "../cli/output.ts";
55 import type { CoreMessage } from "../llm-api/turn.ts";
66 import { saveMessages } from "../session/db/index.ts";
7-import { hasRalphSignal, runShellPassthrough } from "./agent-helpers.ts";
7+import {
8+ buildRalphIterationPrompt,
9+ hasRalphSignal,
10+ runShellPassthrough,
11+} from "./agent-helpers.ts";
812 import type { AgentReporter } from "./reporter.ts";
913 import type { SessionRunner } from "./session-runner.ts";
1014
@@ -16,6 +20,8 @@ interface InputLoopOptions {
1620 renderStatusBar: () => Promise<void>;
1721 }
1822
23+const RALPH_MAX_ITERATIONS = 20;
24+
1925 export async function runInputLoop(opts: InputLoopOptions): Promise<void> {
2026 const { cwd, reporter, cmdCtx, runner, renderStatusBar } = opts;
2127
@@ -70,29 +76,54 @@ export async function runInputLoop(opts: InputLoopOptions): Promise<void> {
7076 }
7177
7278 case "submit": {
73- const RALPH_MAX_ITERATIONS = 20;
74- let ralphIteration = 1;
75- let lastText = await runner.processUserInput(input.text, input.images);
79+ if (!runner.ralphMode) {
80+ await runner.processUserInput(input.text, input.images);
81+ continue;
82+ }
83+
84+ // True ralph loop: each iteration is a fresh subprocess.
85+ // State persists via the filesystem and git history — NOT the context window.
86+ if (input.images && input.images.length > 0) {
87+ reporter.writeText(
88+ `${PREFIX.info} ${c.yellow("ralph")} ${c.dim("— image attachments are not supported and will be ignored")}`,
89+ );
90+ }
91+ const ralphGoal = buildRalphIterationPrompt(input.text);
92+
93+ for (
94+ let iteration = 1;
95+ iteration <= RALPH_MAX_ITERATIONS;
96+ iteration++
97+ ) {
98+ reporter.writeText(
99+ `${PREFIX.info} ${c.magenta("ralph")} ${c.dim(`— iteration ${String(iteration)}`)}`,
100+ );
101+
102+ let result: string;
103+ try {
104+ const output = await cmdCtx.runSubagent(ralphGoal);
105+ result = output.result;
106+ } catch (err) {
107+ reporter.writeText(
108+ `${PREFIX.info} ${c.yellow("ralph")} ${c.dim(`— iteration ${String(iteration)} failed: ${String(err)}`)}`,
109+ );
110+ runner.ralphMode = false;
111+ break;
112+ }
113+
114+ if (hasRalphSignal(result)) {
115+ reporter.writeText(
116+ `${PREFIX.info} ${c.dim("ralph — task complete")}`,
117+ );
118+ runner.ralphMode = false;
119+ break;
120+ }
76121
77- if (runner.ralphMode) {
78- const goal = input.text;
79- const goalImages = input.images;
80- while (runner.ralphMode) {
81- if (hasRalphSignal(lastText)) {
82- runner.ralphMode = false;
83- reporter.writeText(`${PREFIX.info} ${c.dim("ralph mode off")}`);
84- break;
85- }
86- if (ralphIteration >= RALPH_MAX_ITERATIONS) {
87- reporter.writeText(
88- `${PREFIX.info} ${c.yellow("ralph")} ${c.dim("— max iterations reached, stopping")}`,
89- );
90- runner.ralphMode = false;
91- break;
92- }
93- ralphIteration++;
94- cmdCtx.startNewSession();
95- lastText = await runner.processUserInput(goal, goalImages);
122+ if (iteration === RALPH_MAX_ITERATIONS) {
123+ reporter.writeText(
124+ `${PREFIX.info} ${c.yellow("ralph")} ${c.dim("— max iterations reached, stopping")}`,
125+ );
126+ runner.ralphMode = false;
96127 }
97128 }
98129 continue;
src/agent/session-runner.ts+1−3
@@ -131,9 +131,7 @@ export class SessionRunner {
131131
132132 const coreContent = this.planMode
133133 ? `${resolvedText}\n\n<system-message>PLAN MODE ACTIVE: Help the user gather context for the plan -- READ ONLY</system-message>`
134- : this.ralphMode
135- ? `${resolvedText}\n\n<system-message>RALPH MODE: You are in an autonomous loop. Work iteratively to complete the task. Once the task is fully complete, output \`/ralph\` as your final message to end the loop.</system-message>`
136- : resolvedText;
134+ : resolvedText;
137135
138136 const userMsg: CoreMessage =
139137 allImages.length > 0