@@ -276,6 +276,7 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
276276 }
277277
278278 let planMode = false;
279+ let ralphMode = false;
279280
280281 const cmdCtx: CommandContext = {
281282 get currentModel() {
@@ -289,6 +290,12 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
289290 get planMode() {
290291 return planMode;
291292 },
293+ get ralphMode() {
294+ return ralphMode;
295+ },
296+ setRalphMode: (v) => {
297+ ralphMode = v;
298+ },
292299 setPlanMode: (v) => {
293300 planMode = v;
294301 },
@@ -380,7 +387,7 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
380387
381388 let input: InputResult;
382389 try {
383- input = await readline({ cwd, planMode });
390+ input = await readline({ cwd, planMode, ralphMode });
384391 } catch {
385392 break;
386393 }
@@ -421,14 +428,38 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
421428 continue;
422429 }
423430
424- case "submit":
425- await processUserInput(input.text);
431+ case "submit": {
432+ const RALPH_MAX_ITERATIONS = 20;
433+ let ralphIteration = 1;
434+ let lastText = await processUserInput(input.text);
435+ if (ralphMode) {
436+ const goal = input.text;
437+ while (ralphMode) {
438+ // If the LLM signalled done, stop before starting another iteration.
439+ if (hasRalphSignal(lastText)) {
440+ ralphMode = false;
441+ writeln(`${PREFIX.info} ${c.dim("ralph mode off")}`);
442+ break;
443+ }
444+ if (ralphIteration >= RALPH_MAX_ITERATIONS) {
445+ writeln(
446+ `${PREFIX.info} ${c.yellow("ralph")} ${c.dim("— max iterations reached, stopping")}`,
447+ );
448+ ralphMode = false;
449+ break;
450+ }
451+ ralphIteration++;
452+ cmdCtx.startNewSession();
453+ lastText = await processUserInput(goal);
454+ }
455+ }
426456 continue;
457+ }
427458 }
428459 }
429460
430461 // ── Process a user message ─────────────────────────────────────────────────
431- async function processUserInput(text: string): Promise<void> {
462+ async function processUserInput(text: string): Promise<string> {
432463 // Resolve @file references
433464 const resolvedText = await resolveFileRefs(text, cwd);
434465
@@ -442,7 +473,9 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
442473
443474 const coreContent = planMode
444475 ? `${resolvedText}\n\n<system-message>PLAN MODE ACTIVE: Help the user gather context for the plan -- READ ONLY</system-message>`
445- : resolvedText;
476+ : ralphMode
477+ ? `${resolvedText}\n\n<system-message>RALPH MODE: You are in an autonomous loop. When the task is fully complete (all tests pass, no outstanding issues), output exactly \`/ralph\` as your final message to exit the loop. Otherwise, keep working.</system-message>`
478+ : resolvedText;
446479 const userMsg: CoreMessage = { role: "user", content: coreContent };
447480 session.messages.push(userMsg);
448481 saveMessages(session.id, [userMsg], thisTurn);
@@ -452,12 +485,15 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
452485 const systemPrompt = buildSystemPrompt(cwd);
453486
454487 const abortController = new AbortController();
488+ let wasAborted = false;
455489 const onSigInt = () => {
490+ wasAborted = true;
456491 abortController.abort();
457492 process.removeListener("SIGINT", onSigInt);
458493 };
459494 process.on("SIGINT", onSigInt);
460495
496+ let lastAssistantText = "";
461497 try {
462498 // Always push so the stack stays aligned 1:1 with turns. A null entry means
463499 // the tree was clean at snapshot time — /undo will roll back conversation
@@ -487,13 +523,22 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
487523 saveMessages(session.id, newMessages, thisTurn);
488524 }
489525
526+ // Collect all assistant text from this turn so the ralph loop can detect
527+ // the /ralph signal even when the final action was a tool call.
528+ lastAssistantText = extractAssistantText(newMessages);
529+
490530 totalIn += inputTokens;
491531 totalOut += outputTokens;
492532 lastContextTokens = contextTokens;
493533 touchActiveSession(session);
494534 } finally {
495535 process.removeListener("SIGINT", onSigInt);
536+ // Stop the ralph loop on Ctrl+C or any unexpected throw, so the user
537+ // gets back to the prompt rather than resuming with a stale ralphMode=true.
538+ if (wasAborted) ralphMode = false;
496539 }
540+
541+ return lastAssistantText;
497542 }
498543
499544 // ── Render status bar (called before each prompt) ──────────────────────────
@@ -515,10 +560,45 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
515560 outputTokens: totalOut,
516561 contextTokens: lastContextTokens,
517562 contextWindow: getContextWindow(currentModel),
563+ ralphMode,
518564 });
519565 }
520566 }
521567
568+// ─── Ralph signal detection ───────────────────────────────────────────────────
569+
570+/**
571+ * Collect all text from assistant messages in a turn's newMessages array.
572+ * Scans every assistant message (not just the last) so that a trailing tool
573+ * call doesn't mask a `/ralph` signal that appeared in earlier text.
574+ */
575+export function extractAssistantText(
576+ newMessages: import("../llm-api/turn.ts").CoreMessage[],
577+): string {
578+ const parts: string[] = [];
579+ for (const msg of newMessages) {
580+ if (msg.role !== "assistant") continue;
581+ const content = msg.content;
582+ if (typeof content === "string") {
583+ parts.push(content);
584+ } else if (Array.isArray(content)) {
585+ for (const part of content as Array<{ type?: string; text?: string }>) {
586+ if (part?.type === "text" && part.text) parts.push(part.text);
587+ }
588+ }
589+ }
590+ return parts.join("\n");
591+}
592+
593+/**
594+ * Returns true if the assistant text contains the `/ralph` stop signal.
595+ * Matches `/ralph` as a word boundary so surrounding prose is allowed
596+ * (e.g. "All done. /ralph" or "/ralph\n").
597+ */
598+export function hasRalphSignal(text: string): boolean {
599+ return /\/ralph\b/.test(text);
600+}
601+
522602 // ─── Resolve @file references in user input ────────────────────────────────────
523603
524604 async function resolveFileRefs(text: string, cwd: string): Promise<string> {