2026-05-25 22:24:37 +09:00
// biome-ignore-all format: keep the single mandated checkpoint spec under the pure LOC budget.
import { mkdir , mkdtemp , readFile , writeFile } from "node:fs/promises" ;
import { tmpdir } from "node:os" ;
import { join } from "node:path" ;
import { describe , expect , it } from "vitest" ;
2026-05-29 12:38:22 +09:00
import { checkpointUlwLoop } from "../src/checkpoint.js" ;
import { ULW_LOOP_AGGREGATE_CODEX_OBJECTIVE } from "../src/goal-status.js" ;
import { ulwLoopBriefPath , ulwLoopDir , ulwLoopLedgerPath } from "../src/paths.js" ;
2026-05-25 22:24:37 +09:00
import { writePlan } from "../src/plan-io.js" ;
2026-05-29 12:38:22 +09:00
import type { UlwLoopItem , UlwLoopLedgerEntry , UlwLoopPlan , UlwLoopSuccessCriterion } from "../src/types.js" ;
import { UlwLoopError } from "../src/types.js" ;
2026-05-25 22:24:37 +09:00
const NOW = "2026-05-23T00:00:00.000Z" ;
const QUALITY_GATE_PATH = join ( process . cwd ( ) , "test" , "fixtures" , "sample-quality-gate.json" ) ;
2026-05-29 12:38:22 +09:00
function criterion ( id : string , status : UlwLoopSuccessCriterion [ "status" ] ) : UlwLoopSuccessCriterion {
2026-05-25 22:24:37 +09:00
return { id , scenario : ` ${ id } scenario ` , userModel : "happy" , expectedEvidence : ` ${ id } proof ` , capturedEvidence : status === "pass" ? ` ${ id } passed ` : null , status } ;
}
2026-05-29 12:38:22 +09:00
function goal ( overrides : Partial < UlwLoopItem > = { } ) : UlwLoopItem {
2026-05-25 22:24:37 +09:00
return { id : "G001" , title : "Build auth" , objective : "Implement JWT auth endpoint" , status : "in_progress" , successCriteria : [ criterion ( "C001" , "pass" ) , criterion ( "C002" , "pass" ) , criterion ( "C003" , "pass" ) ] , attempt : 1 , createdAt : NOW , updatedAt : NOW , . . . overrides } ;
}
2026-05-29 12:38:22 +09:00
function plan ( goals : UlwLoopItem [ ] , overrides : Partial < UlwLoopPlan > = { } ) : UlwLoopPlan {
const result : UlwLoopPlan = { version : 1 , createdAt : NOW , updatedAt : NOW , briefPath : ".omo/ulw-loop/brief.md" , goalsPath : ".omo/ulw-loop/goals.json" , ledgerPath : ".omo/ulw-loop/ledger.jsonl" , codexGoalMode : "aggregate" , codexObjective : ULW_LOOP_AGGREGATE_CODEX_OBJECTIVE , goals } ;
2026-05-25 22:24:37 +09:00
Object . assign ( result , overrides ) ;
const activeGoalId = goals . find ( ( candidate ) = > candidate . status === "in_progress" ) ? . id ;
if ( result . activeGoalId === undefined && activeGoalId !== undefined ) result . activeGoalId = activeGoalId ;
return result ;
}
2026-05-29 12:38:22 +09:00
async function samplePlan ( overrides : Partial < UlwLoopPlan > = { } ) : Promise < UlwLoopPlan > {
const fixture : UlwLoopPlan = JSON . parse ( await readFile ( new URL ( "./fixtures/sample-plan.json" , import . meta . url ) , "utf8" ) ) ;
2026-05-25 22:24:37 +09:00
return plan ( fixture . goals . map ( ( item , index ) = > goal ( { . . . item , attempt : index + 1 , createdAt : NOW , updatedAt : NOW } ) ) , overrides ) ;
}
2026-05-29 12:38:22 +09:00
async function repoWith ( seed : UlwLoopPlan ) : Promise < string > {
2026-05-25 22:24:37 +09:00
const repo = await mkdtemp ( join ( tmpdir ( ) , "ug-checkpoint-" ) ) ;
2026-05-29 12:38:22 +09:00
await mkdir ( ulwLoopDir ( repo ) , { recursive : true } ) ;
2026-05-25 22:24:37 +09:00
await writePlan ( repo , seed ) ;
return repo ;
}
2026-05-29 12:38:22 +09:00
function snapshot ( status : "active" | "complete" , objective = ULW_LOOP_AGGREGATE_CODEX_OBJECTIVE ) : string {
2026-05-25 22:24:37 +09:00
return JSON . stringify ( { goal : { objective , status } } ) ;
}
2026-05-29 12:38:22 +09:00
async function lastLedger ( repo : string ) : Promise < UlwLoopLedgerEntry > {
const last = ( await readFile ( ulwLoopLedgerPath ( repo ) , "utf8" ) ) . trim ( ) . split ( /\r?\n/ ) . at ( - 1 ) ;
2026-05-25 22:24:37 +09:00
if ( last === undefined ) throw new Error ( "expected ledger entry" ) ;
2026-05-29 12:38:22 +09:00
const entry : UlwLoopLedgerEntry = JSON . parse ( last ) ;
2026-05-25 22:24:37 +09:00
return entry ;
}
async function expectCode ( action : ( ) = > Promise < unknown > , code : string ) : Promise < void > {
try {
await action ( ) ;
} catch ( error ) {
2026-05-29 12:38:22 +09:00
expect ( error ) . toBeInstanceOf ( UlwLoopError ) ;
if ( ! ( error instanceof UlwLoopError ) ) throw error ;
2026-05-25 22:24:37 +09:00
expect ( error . code ) . toBe ( code ) ;
return ;
}
2026-05-29 12:38:22 +09:00
throw new Error ( "Expected UlwLoopError" ) ;
2026-05-25 22:24:37 +09:00
}
2026-05-29 12:38:22 +09:00
function passGoal ( id : string , overrides : Partial < UlwLoopItem > = { } ) : UlwLoopItem {
2026-05-25 22:24:37 +09:00
return goal ( { id , successCriteria : [ criterion ( "C001" , "pass" ) , criterion ( "C002" , "pass" ) , criterion ( "C003" , "pass" ) ] , . . . overrides } ) ;
}
2026-05-29 12:38:22 +09:00
describe ( "checkpointUlwLoop status=complete criteria gate" , ( ) = > {
it ( "THROWS ulw_loop_criteria_not_all_pass when any criterion is pending" , async ( ) = > {
2026-05-25 22:24:37 +09:00
const repo = await repoWith ( await samplePlan ( { goals : [ goal ( { successCriteria : [ criterion ( "C001" , "pass" ) , criterion ( "C002" , "pending" ) , criterion ( "C003" , "pass" ) ] } ) ] } ) ) ;
2026-05-29 12:38:22 +09:00
await expectCode ( ( ) = > checkpointUlwLoop ( repo , { goalId : "G001" , status : "complete" , evidence : "done" } ) , "ulw_loop_criteria_not_all_pass" ) ;
2026-05-25 22:24:37 +09:00
} ) ;
it ( "THROWS when any criterion is fail or blocked" , async ( ) = > {
2026-05-29 12:38:22 +09:00
for ( const status of [ "fail" , "blocked" ] satisfies UlwLoopSuccessCriterion [ "status" ] [ ] ) {
2026-05-25 22:24:37 +09:00
const repo = await repoWith ( plan ( [ goal ( { successCriteria : [ criterion ( "C001" , "pass" ) , criterion ( "C002" , status ) , criterion ( "C003" , "pass" ) ] } ) ] ) ) ;
2026-05-29 12:38:22 +09:00
await expectCode ( ( ) = > checkpointUlwLoop ( repo , { goalId : "G001" , status : "complete" , evidence : "done" } ) , "ulw_loop_criteria_not_all_pass" ) ;
2026-05-25 22:24:37 +09:00
}
} ) ;
it ( "THROWS when criteria list is empty" , async ( ) = > {
const repo = await repoWith ( plan ( [ goal ( { successCriteria : [ ] } ) , goal ( { id : "G002" , status : "pending" } ) ] ) ) ;
2026-05-29 12:38:22 +09:00
await expectCode ( ( ) = > checkpointUlwLoop ( repo , { goalId : "G001" , status : "complete" , evidence : "done" , codexGoalJson : snapshot ( "active" ) } ) , "ulw_loop_criteria_not_all_pass" ) ;
2026-05-25 22:24:37 +09:00
} ) ;
it ( "ACCEPTS complete when ALL criteria pass (with valid snapshot)" , async ( ) = > {
const repo = await repoWith ( plan ( [ passGoal ( "G001" ) , goal ( { id : "G002" , status : "pending" } ) ] ) ) ;
2026-05-29 12:38:22 +09:00
const result = await checkpointUlwLoop ( repo , { goalId : "G001" , status : "complete" , evidence : "implementation done and tests passed" , codexGoalJson : snapshot ( "active" ) } ) ;
2026-05-25 22:24:37 +09:00
expect ( result . goal . status ) . toBe ( "complete" ) ;
expect ( ( await lastLedger ( repo ) ) . kind ) . toBe ( "goal_completed" ) ;
} ) ;
} ) ;
2026-05-29 12:38:22 +09:00
describe ( "checkpointUlwLoop reconciliation (status=complete)" , ( ) = > {
2026-05-25 22:24:37 +09:00
it ( "succeeds when snapshot objective matches expected (aggregate active)" , async ( ) = > {
const repo = await repoWith ( plan ( [ passGoal ( "G001" ) , goal ( { id : "G002" , status : "pending" } ) ] ) ) ;
2026-05-29 12:38:22 +09:00
await expect ( checkpointUlwLoop ( repo , { goalId : "G001" , status : "complete" , evidence : "work complete and validation passed" , codexGoalJson : snapshot ( "active" ) } ) ) . resolves . toMatchObject ( { goal : { status : "complete" } } ) ;
2026-05-25 22:24:37 +09:00
} ) ;
it ( "throws on mismatched objective" , async ( ) = > {
const repo = await repoWith ( plan ( [ passGoal ( "G001" ) , goal ( { id : "G002" , status : "pending" } ) ] ) ) ;
2026-05-29 12:38:22 +09:00
await expectCode ( ( ) = > checkpointUlwLoop ( repo , { goalId : "G001" , status : "complete" , evidence : "work complete and validation passed" , codexGoalJson : snapshot ( "active" , "wrong objective" ) } ) , "ulw_loop_codex_snapshot_mismatch" ) ;
2026-05-25 22:24:37 +09:00
} ) ;
it ( "throws on mismatched status (snapshot complete when expected active)" , async ( ) = > {
const repo = await repoWith ( plan ( [ passGoal ( "G001" ) , goal ( { id : "G002" , status : "pending" } ) ] ) ) ;
2026-05-29 12:38:22 +09:00
await expectCode ( ( ) = > checkpointUlwLoop ( repo , { goalId : "G001" , status : "complete" , evidence : "work complete and validation passed" , codexGoalJson : snapshot ( "complete" ) } ) , "ulw_loop_codex_snapshot_mismatch" ) ;
2026-05-25 22:24:37 +09:00
} ) ;
} ) ;
2026-05-29 12:38:22 +09:00
describe ( "checkpointUlwLoop final story" , ( ) = > {
2026-05-25 22:24:37 +09:00
it ( "requires quality-gate-json for the final goal complete" , async ( ) = > {
const repo = await repoWith ( plan ( [ passGoal ( "G001" , { status : "complete" } ) , passGoal ( "G002" ) ] , { activeGoalId : "G002" } ) ) ;
2026-05-29 12:38:22 +09:00
await expectCode ( ( ) = > checkpointUlwLoop ( repo , { goalId : "G002" , status : "complete" , evidence : "final work complete and validation passed" , codexGoalJson : snapshot ( "complete" ) } ) , "ULW_LOOP_QUALITY_GATE_INVALID" ) ;
2026-05-25 22:24:37 +09:00
} ) ;
it ( "accepts final story when quality gate JSON includes valid criteriaCoverage" , async ( ) = > {
const repo = await repoWith ( plan ( [ passGoal ( "G001" , { status : "complete" } ) , passGoal ( "G002" ) ] , { activeGoalId : "G002" } ) ) ;
2026-05-29 12:38:22 +09:00
const result = await checkpointUlwLoop ( repo , { goalId : "G002" , status : "complete" , evidence : "final work complete and validation passed" , codexGoalJson : snapshot ( "complete" ) , qualityGateJson : QUALITY_GATE_PATH } ) ;
2026-05-25 22:24:37 +09:00
expect ( result . aggregateCompletion ? . status ) . toBe ( "complete" ) ;
expect ( result . plan . aggregateCompletion ? . status ) . toBe ( "complete" ) ;
} ) ;
2026-05-29 12:38:22 +09:00
it ( "ACCEPTS complete when task-scoped completed Codex objective maps to the ulw-loop brief" , async ( ) = > {
const taskObjective = "Fix ulw-loop objective mismatch and install local ulw" ;
2026-05-25 22:24:37 +09:00
const repo = await repoWith ( plan ( [ passGoal ( "G001" ) ] , { activeGoalId : "G001" } ) ) ;
2026-05-29 12:38:22 +09:00
await writeFile ( ulwLoopBriefPath ( repo ) , ` ${ taskObjective } \ n ` , "utf8" ) ;
2026-05-25 22:24:37 +09:00
2026-05-29 12:38:22 +09:00
const result = await checkpointUlwLoop ( repo , {
2026-05-25 22:24:37 +09:00
goalId : "G001" ,
status : "complete" ,
evidence : "final implementation complete and quality gate passed" ,
codexGoalJson : snapshot ( "complete" , taskObjective ) ,
qualityGateJson : QUALITY_GATE_PATH ,
} ) ;
expect ( result . aggregateCompletion ? . status ) . toBe ( "complete" ) ;
expect ( result . ledgerEntry . kind ) . toBe ( "aggregate_completed" ) ;
} ) ;
it ( "explains final task-scoped objective mapping when completed Codex objective is unrelated" , async ( ) = > {
const repo = await repoWith ( plan ( [ passGoal ( "G001" ) ] , { activeGoalId : "G001" } ) ) ;
2026-05-29 12:38:22 +09:00
await writeFile ( ulwLoopBriefPath ( repo ) , "Fix ulw-loop objective mismatch and install local ulw\n" , "utf8" ) ;
2026-05-25 22:24:37 +09:00
await expect (
2026-05-29 12:38:22 +09:00
checkpointUlwLoop ( repo , {
2026-05-25 22:24:37 +09:00
goalId : "G001" ,
status : "complete" ,
evidence : "final implementation complete and quality gate passed" ,
codexGoalJson : snapshot ( "complete" , "unrelated completed task" ) ,
qualityGateJson : QUALITY_GATE_PATH ,
} ) ,
) . rejects . toThrow ( "Final task-scoped aggregate reconciliation" ) ;
} ) ;
} ) ;
2026-05-29 12:38:22 +09:00
describe ( "checkpointUlwLoop status=failed" , ( ) = > {
2026-05-25 22:24:37 +09:00
it ( "sets goal.status=failed, goal.failedAt, appends ledger" , async ( ) = > {
const repo = await repoWith ( plan ( [ goal ( { successCriteria : [ criterion ( "C001" , "pending" ) ] } ) ] ) ) ;
2026-05-29 12:38:22 +09:00
const result = await checkpointUlwLoop ( repo , { goalId : "G001" , status : "failed" , evidence : "tests failed" } ) ;
2026-05-25 22:24:37 +09:00
expect ( result . goal . status ) . toBe ( "failed" ) ;
expect ( result . goal . failedAt ) . toMatch ( / ^ \ d { 4 } - \ d { 2 } - \ d { 2 } T / u ) ;
expect ( ( await lastLedger ( repo ) ) . kind ) . toBe ( "goal_failed" ) ;
} ) ;
it ( "classifies external authorization blocker signatures" , async ( ) = > {
const repo = await repoWith ( plan ( [ goal ( ) ] ) ) ;
2026-05-29 12:38:22 +09:00
const result = await checkpointUlwLoop ( repo , { goalId : "G001" , status : "failed" , evidence : "ghcr.io returned 401 authentication required because token missing" } ) ;
2026-05-25 22:24:37 +09:00
expect ( result . goal . blockerSignature ) . toBe ( "GHCR_PULL_ACCESS:HTTP_401_ANONYMOUS:GHCR_VISIBILITY_OR_CREDENTIAL_REQUIRED" ) ;
} ) ;
it ( "after 3 same-signature blockers, marks needs_user_decision + nonRetriable" , async ( ) = > {
const repo = await repoWith ( plan ( [ goal ( { id : "G001" , status : "failed" , blockerSignature : "EXTERNAL_AUTHORIZATION_REQUIRED" } ) , goal ( { id : "G002" , status : "blocked" , blockerSignature : "EXTERNAL_AUTHORIZATION_REQUIRED" } ) , goal ( { id : "G003" } ) ] , { activeGoalId : "G003" } ) ) ;
2026-05-29 12:38:22 +09:00
const result = await checkpointUlwLoop ( repo , { goalId : "G003" , status : "failed" , evidence : "Registry returned 401 because credentials are missing" } ) ;
2026-05-25 22:24:37 +09:00
expect ( result . goal . status ) . toBe ( "needs_user_decision" ) ;
expect ( result . goal . nonRetriable ) . toBe ( true ) ;
} ) ;
it ( "skips the criteria gate for failed status" , async ( ) = > {
const repo = await repoWith ( plan ( [ goal ( { successCriteria : [ criterion ( "C001" , "pending" ) ] } ) ] ) ) ;
2026-05-29 12:38:22 +09:00
await expect ( checkpointUlwLoop ( repo , { goalId : "G001" , status : "failed" , evidence : "not done" } ) ) . resolves . toMatchObject ( { goal : { status : "failed" } } ) ;
2026-05-25 22:24:37 +09:00
} ) ;
} ) ;
2026-05-29 12:38:22 +09:00
describe ( "checkpointUlwLoop status=blocked" , ( ) = > {
2026-05-25 22:24:37 +09:00
it ( "preserves blocker fields + appends ledger" , async ( ) = > {
const repo = await repoWith ( plan ( [ goal ( ) ] ) ) ;
2026-05-29 12:38:22 +09:00
const result = await checkpointUlwLoop ( repo , { goalId : "G001" , status : "blocked" , evidence : "ghcr.io requires token and credentials are missing" } ) ;
2026-05-25 22:24:37 +09:00
expect ( result . goal . status ) . toBe ( "blocked" ) ;
expect ( result . goal . blockedReason ) . toContain ( "ghcr.io" ) ;
expect ( result . goal . blockerSignature ) . toContain ( "GHCR_PULL_ACCESS" ) ;
expect ( ( await lastLedger ( repo ) ) . kind ) . toBe ( "goal_blocked" ) ;
} ) ;
it ( "skips the criteria gate for blocked status" , async ( ) = > {
const repo = await repoWith ( plan ( [ goal ( { successCriteria : [ criterion ( "C001" , "pending" ) ] } ) ] ) ) ;
2026-05-29 12:38:22 +09:00
await expect ( checkpointUlwLoop ( repo , { goalId : "G001" , status : "blocked" , evidence : "waiting for approval" } ) ) . resolves . toMatchObject ( { goal : { status : "blocked" } } ) ;
2026-05-25 22:24:37 +09:00
} ) ;
} ) ;
2026-05-29 12:38:22 +09:00
describe ( "checkpointUlwLoop rebrand" , ( ) = > {
2026-05-25 22:24:37 +09:00
it ( "does not emit legacy brand token in any returned text or ledger payload" , async ( ) = > {
const repo = await repoWith ( plan ( [ passGoal ( "G001" ) , goal ( { id : "G002" , status : "pending" } ) ] ) ) ;
2026-05-29 12:38:22 +09:00
const result = await checkpointUlwLoop ( repo , { goalId : "G001" , status : "complete" , evidence : "implementation done in .omo/ulw-loop/goals.json for G001 and validation passed" , codexGoalJson : snapshot ( "active" ) } ) ;
2026-05-25 22:24:37 +09:00
const forbidden = [ "o" , "m" , "x" ] . join ( "" ) ;
2026-05-29 12:38:22 +09:00
const payload = ` ${ JSON . stringify ( result ) } \ n ${ await readFile ( ulwLoopLedgerPath ( repo ) , "utf8" ) } ` . toLowerCase ( ) ;
2026-05-25 22:24:37 +09:00
expect ( payload ) . not . toContain ( forbidden ) ;
} ) ;
} ) ;