@@ -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;