Skip to content

The one and only agent harness for complex codebases. Project memory, planning, execution, and verified completion inside Codex.

3.2 KB
Raw Blame
import { checkpointUlwLoop } from "./checkpoint.js";
import { checkpointTemplate } from "./checkpoint-template.js";
import { hasFlag, parseCodexGoalJson, readValue } from "./cli-arg-parser.js";
import { blockedDecisionHandoff, printJson } from "./cli-output.js";
import { buildCodexGoalInstruction } from "./codex-goal-instruction.js";
import { startNextUlwLoop, summarizeUlwLoopPlan } from "./plan-crud.js";
import { UlwLoopError } from "./types.js";
export async function checkpointAndContinue(repoRoot, args, scope) {
    const result = await checkpointUlwLoop(repoRoot, args, scope);
    if (args.status !== "complete" || result.aggregateCompletion !== undefined || !args.advance)
        return result;
    const next = await startNextUlwLoop(repoRoot, {}, scope);
    if ("done" in next)
        return { ...result, plan: next.plan, next: doneNext(next.plan) };
    const instruction = buildCodexGoalInstruction({ plan: next.plan, goal: next.goal });
    return { ...result, plan: next.plan, next: { resumed: next.resumed, goal: next.goal, instruction } };
}
export async function checkpoint(repoRoot, argv, json, scope) {
    if (hasFlag(argv, "--print-template")) {
        const template = await checkpointTemplate(repoRoot, scope, readValue(argv, "--goal-id"));
        if (json)
            printJson({ ok: true, ...template });
        else
            printJson(template);
        return 0;
    }
    const goalId = required(argv, "--goal-id");
    const statusValue = checkpointStatus(required(argv, "--status"));
    const evidence = required(argv, "--evidence");
    const codexGoalJson = await parseCodexGoalJson(readValue(argv, "--codex-goal-json"));
    const qualityGateJson = readValue(argv, "--quality-gate-json");
    const args = {
        goalId,
        status: statusValue,
        evidence,
        advance: !hasFlag(argv, "--no-advance"),
        ...(codexGoalJson === undefined ? {} : { codexGoalJson }),
        ...(qualityGateJson === undefined ? {} : { qualityGateJson }),
    };
    const result = await checkpointAndContinue(repoRoot, args, scope);
    if (json)
        printJson({ ok: true, ...result, summary: summarizeUlwLoopPlan(result.plan) });
    else
        printCheckpointText(result);
    return 0;
}
function printCheckpointText(result) {
    process.stdout.write(`ulw-loop checkpoint: ${result.goal.id} -> ${result.goal.status}\n`);
    if (result.next === undefined)
        return;
    if ("instruction" in result.next)
        process.stdout.write(`${result.next.instruction.text}\n`);
    else
        process.stdout.write(`${result.next.handoff || "ulw-loop: all goals complete"}\n`);
}
function doneNext(plan) {
    const handoff = blockedDecisionHandoff(plan);
    return { done: true, blocked: handoff.length > 0, handoff };
}
function required(argv, flag) {
    const value = readValue(argv, flag)?.trim();
    if (value)
        return value;
    throw new UlwLoopError(`Missing ${flag}.`, "ULW_LOOP_ARGUMENT_MISSING", { details: { flag } });
}
function checkpointStatus(value) {
    if (value === "complete" || value === "failed" || value === "blocked")
        return value;
    throw new UlwLoopError("Missing or invalid --status; expected complete, failed, or blocked.", "ULW_LOOP_STATUS_INVALID", { details: { status: value } });
}