import { SYSTEM_DIRECTIVE_PREFIX } from "../../shared/system-directive" import type { RalphLoopState } from "./types" function getMaxIterationsLabel(state: RalphLoopState): string { return typeof state.max_iterations === "number" ? String(state.max_iterations) : "unbounded" } const CONTINUATION_PROMPT = `${SYSTEM_DIRECTIVE_PREFIX} - RALPH LOOP {{ITERATION}}/{{MAX}}] Your previous attempt did not output the completion promise. Continue working on the task. IMPORTANT: - Review your progress so far - Continue from where you left off - When FULLY complete, output: {{PROMISE}} - Do not stop until the task is truly done Original task: {{PROMPT}}` const ULTRAWORK_VERIFICATION_PROMPT = `${SYSTEM_DIRECTIVE_PREFIX} - ULTRAWORK LOOP VERIFICATION {{ITERATION}}/{{MAX}}] You already emitted {{INITIAL_PROMISE}}. This does NOT finish the loop yet. REQUIRED NOW: - Call Oracle using task(subagent_type="oracle", load_skills=[], run_in_background=false, ...) - Ask Oracle to verify whether the original task is actually complete - The system will inspect the Oracle session directly for the verification result - If Oracle does not verify, continue fixing the task and do not consider it complete Original task: {{PROMPT}}` export function buildContinuationPrompt(state: RalphLoopState): string { const template = state.verification_pending ? ULTRAWORK_VERIFICATION_PROMPT : CONTINUATION_PROMPT const continuationPrompt = template.replace( "{{ITERATION}}", String(state.iteration), ) .replace("{{MAX}}", getMaxIterationsLabel(state)) .replace("{{INITIAL_PROMISE}}", state.initial_completion_promise ?? state.completion_promise) .replace("{{PROMISE}}", state.completion_promise) .replace("{{PROMPT}}", state.prompt) return state.ultrawork ? `ultrawork ${continuationPrompt}` : continuationPrompt }