|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import fs from "node:fs" |
| 3 | +import os from "node:os" |
| 4 | +import path from "node:path" |
| 5 | +import { spawnSync } from "node:child_process" |
| 6 | + |
| 7 | +const root = fs.mkdtempSync(path.join(os.tmpdir(), "codex-compaction-continuity-")) |
| 8 | +const guard = path.resolve("scripts/operator/codex-compaction-continuity-guard.mjs") |
| 9 | +const base = { |
| 10 | + task_id: "task-20260729-001", |
| 11 | + completion_criteria_sha256: "criteria-sha256", |
| 12 | + checkpoint_id: "checkpoint-4", |
| 13 | + repository_sha: "repo-sha", |
| 14 | + diff_sha256: "diff-sha256", |
| 15 | + completed_steps_sha256: "completed-sha256", |
| 16 | + phase: "verification", |
| 17 | + next_action: "run final bounded test", |
| 18 | + completed_steps: ["inspect repository", "apply patch", "build application"], |
| 19 | + remaining_steps: ["run final bounded test", "publish completion report"], |
| 20 | + compaction_count: 1, |
| 21 | + repeated_command_count: 0, |
| 22 | + repeated_build_without_diff_count: 0, |
| 23 | + reopened_resolved_count: 0, |
| 24 | + duplicate_subagent_assignment_count: 0, |
| 25 | + completed_step_regression_count: 0, |
| 26 | + checkpoint_restored: true, |
| 27 | + repository_state_verified: true, |
| 28 | + uncertain_writes_reconciled: true, |
| 29 | + subagents_enabled: false, |
| 30 | + subagent_results_restored: false, |
| 31 | + restored_completion_criteria_sha256: "criteria-sha256", |
| 32 | + restored_completed_steps_sha256: "completed-sha256", |
| 33 | + restored_next_action: "run final bounded test", |
| 34 | + fresh_guarded_turn: false, |
| 35 | + continuation_state_persisted: true, |
| 36 | +} |
| 37 | + |
| 38 | +function run(name, payload, env = {}) { |
| 39 | + const file = path.join(root, `${name}.json`) |
| 40 | + fs.writeFileSync(file, `${JSON.stringify(payload, null, 2)}\n`, "utf8") |
| 41 | + const result = spawnSync(process.execPath, [guard, "--input", file, "--json"], { |
| 42 | + encoding: "utf8", |
| 43 | + env: { ...process.env, ...env }, |
| 44 | + }) |
| 45 | + const text = String(result.stdout || result.stderr || "").trim() |
| 46 | + return { status: result.status, output: text ? JSON.parse(text) : null } |
| 47 | +} |
| 48 | + |
| 49 | +try { |
| 50 | + const healthy = run("healthy", base) |
| 51 | + assert.equal(healthy.status, 0) |
| 52 | + assert.equal(healthy.output.reason, "compaction_continuity_verified") |
| 53 | + |
| 54 | + const regressed = run("regressed", { ...base, completed_step_regression_count: 1 }) |
| 55 | + assert.equal(regressed.status, 75) |
| 56 | + assert.equal(regressed.output.reason, "completed_work_regressed_after_compaction") |
| 57 | + |
| 58 | + const repeating = run("repeating", { ...base, repeated_build_without_diff_count: 3 }) |
| 59 | + assert.equal(repeating.status, 75) |
| 60 | + assert.equal(repeating.output.reason, "post_compaction_repetition_limit_reached") |
| 61 | + |
| 62 | + const incomplete = run("incomplete", { ...base, checkpoint_restored: false }) |
| 63 | + assert.equal(incomplete.status, 75) |
| 64 | + assert.equal(incomplete.output.reason, "compaction_checkpoint_restoration_incomplete") |
| 65 | + |
| 66 | + const freshTurn = run("fresh-turn", { |
| 67 | + ...base, |
| 68 | + checkpoint_restored: false, |
| 69 | + repository_state_verified: false, |
| 70 | + restored_completion_criteria_sha256: "missing", |
| 71 | + restored_completed_steps_sha256: "missing", |
| 72 | + restored_next_action: "missing", |
| 73 | + fresh_guarded_turn: true, |
| 74 | + continuation_state_persisted: true, |
| 75 | + }) |
| 76 | + assert.equal(freshTurn.status, 0) |
| 77 | + |
| 78 | + const subagentRoute = run("subagent-route", { |
| 79 | + ...base, |
| 80 | + subagents_enabled: true, |
| 81 | + subagent_results_restored: true, |
| 82 | + }) |
| 83 | + assert.equal(subagentRoute.status, 75) |
| 84 | + assert.equal(subagentRoute.output.reason, "subagent_compaction_route_unattested") |
| 85 | + |
| 86 | + const prohibited = run("prohibited", base, { OPERATOR_ROUTE: "model-gateway-auto-select" }) |
| 87 | + assert.equal(prohibited.status, 64) |
| 88 | + assert.equal(prohibited.output.reason, "prohibited_route_metadata") |
| 89 | + |
| 90 | + console.log("codex-compaction-continuity-guard self-test passed") |
| 91 | +} finally { |
| 92 | + fs.rmSync(root, { recursive: true, force: true }) |
| 93 | +} |
0 commit comments