Skip to content

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

4.3 KB
Raw Blame
// biome-ignore-all format: compact port must stay within the requested pure LOC budget.
import { CodexGoalSnapshotError, formatCodexGoalReconciliation, readCodexGoalSnapshotInput, reconcileCodexGoalSnapshot, } from "./codex-goal-snapshot.js";
import { acknowledgeDriverObjective, acknowledgedDriverObjectives } from "./driver-objective-ack.js";
import { compatibleCodexObjectives, expectedCodexObjective, isFinalRunCompletionCandidate } from "./goal-status.js";
import { commit } from "./plan-commit.js";
import { seedDefaultSuccessCriteria } from "./plan-crud.js";
import { readUlwLoopPlan, withUlwLoopMutationLock } from "./plan-io.js";
import { iso, UlwLoopError } from "./types.js";
const BLOCKER_FIELDS = "blockedReason blockerSignature blockerOccurrenceCount requiredExternalDecision nonRetriable failedAt failureReason completedAt blocker blockerEvidence blockerOccurrences blockedAt".split(" ");
function ulwLoopError(message, code) {
    throw new UlwLoopError(message, code);
}
function nextGoalId(plan) {
    const max = plan.goals.reduce((current, goal) => {
        const digits = /^G(\d+)/u.exec(goal.id)?.[1];
        return digits === undefined ? current : Math.max(current, Number(digits));
    }, 0);
    return `G${String(max + 1).padStart(3, "0")}`;
}
function appendBlockerGoal(plan, args, now) {
    const index = plan.goals.length;
    const id = nextGoalId(plan);
    const goal = {
        id,
        title: args.title,
        objective: args.objective,
        status: "pending",
        successCriteria: seedDefaultSuccessCriteria(index, args.objective, { goalId: id }),
        attempt: 0,
        createdAt: now,
        updatedAt: now,
    };
    plan.goals.push(goal);
    return goal;
}
export async function recordFinalReviewBlockers(repoRoot, args, scope) {
    return withUlwLoopMutationLock(repoRoot, scope, async () => {
        const plan = await readUlwLoopPlan(repoRoot, scope);
        const goal = plan.goals.find((candidate) => candidate.id === args.goalId);
        if (goal === undefined)
            ulwLoopError(`Unknown ulw-loop id: ${args.goalId}`, "ulw_loop_goal_not_found");
        if (goal.status !== "in_progress")
            ulwLoopError(`${goal.id} is ${goal.status}.`, "ulw_loop_goal_not_in_progress");
        if (!isFinalRunCompletionCandidate(plan, goal))
            ulwLoopError(`${goal.id} is not final.`, "ulw_loop_not_final_story");
        const snapshot = await readCodexGoalSnapshotInput(args.codexGoalJson, repoRoot);
        const reconciliation = reconcileCodexGoalSnapshot(snapshot, {
            expectedObjective: expectedCodexObjective(plan, goal),
            acceptedObjectives: compatibleCodexObjectives(plan),
            acknowledgedObjectives: acknowledgedDriverObjectives(plan),
        });
        if (!reconciliation.ok)
            throw new CodexGoalSnapshotError(formatCodexGoalReconciliation(reconciliation));
        acknowledgeDriverObjective(plan, reconciliation.unacknowledgedObjective);
        const now = iso();
        for (const field of BLOCKER_FIELDS)
            Reflect.deleteProperty(goal, field);
        goal.status = "review_blocked";
        goal.reviewBlockedAt = now;
        goal.evidence = args.evidence;
        goal.updatedAt = now;
        if (plan.activeGoalId === goal.id)
            delete plan.activeGoalId;
        const newGoal = appendBlockerGoal(plan, args, now);
        plan.updatedAt = now;
        const codexGoal = snapshot?.raw;
        const blockedEntry = { at: now, kind: "goal_review_blocked", goalId: goal.id, status: goal.status, evidence: args.evidence, codexGoal };
        const addedEntry = { at: now, kind: "goal_added", goalId: newGoal.id, status: newGoal.status, evidence: args.evidence, message: newGoal.title };
        const summaryEntry = { at: now, kind: "goal_review_blocked", goalId: goal.id, status: goal.status, evidence: args.evidence, codexGoal, message: `Review blockers recorded; appended ${newGoal.id}.` };
        Reflect.set(summaryEntry, "kind", "blocker_recorded");
        const ledgerEntries = [blockedEntry, addedEntry, summaryEntry];
        await commit(repoRoot, scope, { plan, entries: ledgerEntries });
        return {
            plan,
            blockedGoal: goal,
            newGoal,
            ledgerEntries,
            nextActions: reconciliation.nextActions,
            warnings: reconciliation.warnings,
        };
    });
}