voyage/lib/util/autonomy-gate.mjs
Kjell Tore Guttormsen 29b4ac91b3
fix: round 7 of the release review of 6.3.0, for 6.3.1
Round 7 (516dffa..45aa9e1, WARN: 12 MAJOR, 0 BLOCKER) and the fence defect
found while writing its review.md. Each fix began with a test that failed on
0f400d3 (v6.3.0); suite 2208 (2206/0/2), 14 tests more.

- Tests-only (operator 2026-10-11): the plan's Files: alone decides, and the
  text says so (red-proof, trekexecute Sub-step B, step-implementer,
  HANDOVER-CONTRACTS). red-proof no longer reads the step's diff, so a change
  already in the tree when the step began no longer counts against it, and a
  plan placed outside .claude/ reads the same. The round-6 test that had the
  diff overrule the plan is rewritten to the new rule.
- The Files: reader keeps a plain file beside a backticked path
  (", Dockerfile", "the CLI entry src/cli.mjs", "and Makefile"). Measured on
  the 520 Files: fields in 182 plans: no field reads differently.
- Red proof reports a command its timeout killed as killed after the timeout,
  not "could not start"; the release check tells a timeout from output past
  its buffer and from another signal. One reading of a stop
  (criteria-runner stoppedBy) for red proof, the criteria runner and the
  release check.
- A trailing `# comment` is dropped in a backticked check too, and in a
  command passed as written, so the words screened are the words run.
- The release check reads a relative --review in --repo.
- The 15-minute timeout is defined once (criteria-runner.mjs).
- The findings check reads each cited file once per validation.
- One isMain helper (lib/util/is-main.mjs) for every CLI: the 31 copies of
  the realpath guard and the 20 modules still on the file:// guard, which
  exited 0 having done nothing under a symlink or a path with a space.
- The Bash guard writes its no-one-to-ask refusal once, on a direct condition.
- The findings check ends a ```json block only at a fence on its own line, so
  three backticks inside a JSON string no longer end it (also in 6.2.0).
- Brief-conformance notes: a test for test-lock green on exit 127 (it
  passed at once; it fails when 127 is read as could-not-run); the prose now
  names the git revision `@~1` as the one leading-@ exception.

Not changed: the example plan's steps 1-2 keep their tests-only warning
(operator decision 2026-10-09; the r5 test records it). Brief note 4 (a
Regression: whose runner is not installed goes to the fix rounds) is a
routing question for the operator.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 09:11:57 +02:00

131 lines
4.3 KiB
JavaScript

// lib/util/autonomy-gate.mjs
// Autonomy-gate state machine for /trekexecute + /trekplan
// (plan-v2 Step 4 — drives the --gates flag).
//
// States:
// idle — not yet started
// gates_on — gates enabled, between phases
// auto_running — running phases continuously without pausing
// paused_for_gate — stopped at a phase boundary; awaiting `resume`
// completed — terminal
//
// Events:
// start — begin a run (gates flag chooses route)
// phase_boundary — a phase finished
// resume — operator confirmed; leave the gate
// finish — pipeline reached its end
//
// CLI shim:
// node lib/util/autonomy-gate.mjs --state X --event Y [--gates true|false]
// → JSON: { ok: true, next_state: "..." } (success)
// → JSON: { ok: false, error: "..." } (invalid transition; exit 1)
//
// Pure data; no I/O. Re-entry to `completed` is idempotent.
import { isMain } from './is-main.mjs';
export const STATES = Object.freeze({
IDLE: 'idle',
GATES_ON: 'gates_on',
AUTO_RUNNING: 'auto_running',
PAUSED_FOR_GATE: 'paused_for_gate',
COMPLETED: 'completed',
});
export const EVENTS = Object.freeze({
START: 'start',
PHASE_BOUNDARY: 'phase_boundary',
RESUME: 'resume',
FINISH: 'finish',
});
const STATE_SET = new Set(Object.values(STATES));
const EVENT_SET = new Set(Object.values(EVENTS));
/**
* Compute the next state given the current state, event, and (optional)
* gates-flag intent (only consulted on `start` from `idle`).
*
* @param {string} state
* @param {string} event
* @param {{ gates?: boolean }} [opts]
* @returns {{ ok: true, next_state: string } | { ok: false, error: string }}
*/
export function transition(state, event, opts = {}) {
if (!STATE_SET.has(state)) {
return { ok: false, error: `unknown state: ${state}` };
}
if (!EVENT_SET.has(event)) {
return { ok: false, error: `unknown event: ${event}` };
}
// completed is terminal & idempotent
if (state === STATES.COMPLETED) {
return { ok: true, next_state: STATES.COMPLETED };
}
if (state === STATES.IDLE) {
if (event === EVENTS.START) {
const gates = opts.gates === true;
return { ok: true, next_state: gates ? STATES.GATES_ON : STATES.AUTO_RUNNING };
}
return { ok: false, error: `invalid transition: idle + ${event} (only \`start\` allowed from idle)` };
}
if (state === STATES.GATES_ON) {
if (event === EVENTS.PHASE_BOUNDARY) return { ok: true, next_state: STATES.PAUSED_FOR_GATE };
if (event === EVENTS.FINISH) return { ok: true, next_state: STATES.COMPLETED };
return { ok: false, error: `invalid transition: gates_on + ${event}` };
}
if (state === STATES.AUTO_RUNNING) {
if (event === EVENTS.PHASE_BOUNDARY) return { ok: true, next_state: STATES.AUTO_RUNNING };
if (event === EVENTS.FINISH) return { ok: true, next_state: STATES.COMPLETED };
return { ok: false, error: `invalid transition: auto_running + ${event}` };
}
if (state === STATES.PAUSED_FOR_GATE) {
if (event === EVENTS.RESUME) return { ok: true, next_state: STATES.GATES_ON };
if (event === EVENTS.FINISH) return { ok: true, next_state: STATES.COMPLETED };
return { ok: false, error: `invalid transition: paused_for_gate + ${event}` };
}
return { ok: false, error: `unhandled state: ${state}` };
}
/**
* Convenience: is this state terminal?
*/
export function isTerminal(state) {
return state === STATES.COMPLETED;
}
// ---- CLI shim ----------------------------------------------------------------
function parseArgs(argv) {
const out = {};
for (let i = 0; i < argv.length; i++) {
const a = argv[i];
if (a === '--state') out.state = argv[++i];
else if (a === '--event') out.event = argv[++i];
else if (a === '--gates') {
const v = argv[++i];
out.gates = v === 'true';
}
}
return out;
}
if (isMain(import.meta.url)) {
const args = parseArgs(process.argv.slice(2));
if (!args.state || !args.event) {
process.stdout.write(JSON.stringify({
ok: false,
error: 'usage: autonomy-gate.mjs --state <state> --event <event> [--gates true|false]',
}) + '\n');
process.exit(1);
}
const result = transition(args.state, args.event, { gates: args.gates });
process.stdout.write(JSON.stringify(result) + '\n');
process.exit(result.ok ? 0 : 1);
}