Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.
JavaScript agentic-codingai-agentsai-governanceautonomous-agentsclaude-codeclicode-reviewcodexcoding-agentsdeveloper-toolsgit-hooksllmlocal-firstmulti-agentsoftware-factoryspec-driven-developmentverificationvibe-coding
feat(machine): the item lifecycle, declared once and proved [M1,M2,M4]
Corey Olsonc88df1790ec0
diff --git a/src/machine.js b/src/machine.jsnew file mode 100644index 00000000..6a539faa--- /dev/null+++ b/src/machine.js@@ -0,0 +1,349 @@+/**+ * The item lifecycle, declared once (M1).+ *+ * Every state an item can be in, every move between states, who may make it, and the guards it+ * must pass in the order they are checked, each guard with the refusal it raises and the step that+ * gets past it. Final states carry exit guards that every move into them must name (M2), so no+ * command can become a second way into verified or withdrawn. `machineProblems` proves the+ * declaration's properties (M4); test/machine.test.js runs it, shows each property failing on a+ * broken copy, and checks the declaration against the refusals board.js and cli.js raise today.+ */++/** @typedef {'agent' | 'coordinator' | 'clock'} Role */++/**+ * @typedef {object} Guard+ * @property {string} id - How moves name it.+ * @property {string} refuse - The refusal code it raises when it does not hold.+ * @property {string} rule - What must hold, in words.+ * @property {string} next - The step that gets past it.+ * @property {'board' | 'cli'} source - Where its fact comes from: the board file, or the CLI's look at git and the worktree.+ */++/**+ * @typedef {object} State+ * @property {string} id+ * @property {string} means+ * @property {boolean} [final] - Nothing leaves it.+ * @property {string[]} requires - Item fields that are set whenever an item is in this state.+ */++/**+ * @typedef {object} Move+ * @property {string} verb+ * @property {string[]} from - The states it starts from.+ * @property {string} to+ * @property {Role[]} by - Who may make it.+ * @property {string[]} guards - Checked in this order. IN_STATE marks where the item's state is checked.+ * @property {string} [refuse] - The code raised when the item is in none of `from`.+ * @property {string[]} [sets] - The fields it sets.+ * @property {string} [command] - The command that makes it.+ * @property {string} [when] - For the clock's move: when it happens.+ */++/**+ * @typedef {object} Machine+ * @property {Role[]} roles+ * @property {State[]} states+ * @property {Guard[]} guards+ * @property {Move[]} moves+ * @property {Record<string, string[]>} exitGuards - Per final state, the guards every move into it names.+ * @property {{ refuse: string, rule: string, next: string }} unknownMove - The refusal for a move that does not exist.+ */++/** The guard id that marks where a move checks the item's state; its code is the move's own. */+export const IN_STATE = 'inState';++/** @type {Role[]} */+export const ROLES = ['agent', 'coordinator', 'clock'];++/** @type {State[]} */+export const STATES = [+ { id: 'open', means: 'waiting for a builder; claimable once what it waits on is verified', requires: [] },+ { id: 'claimed', means: 'an agent holds it under a lease, its criterion frozen', requires: ['item_owner', 'item_lease_until', 'item_frozen_digest'] },+ { id: 'submitted', means: 'built and gated green at a pinned commit, waiting for another agent', requires: ['item_built_by', 'item_commit', 'item_frozen_digest'] },+ { id: 'verified', final: true, means: 'another agent accepted it at the submitted commit', requires: ['item_verified_by', 'item_commit'] },+ { id: 'withdrawn', final: true, means: 'nobody should build it; the reason stays on it', requires: ['item_withdrawn_reason'] },+];++/** @type {Guard[]} */+export const GUARDS = [+ { id: IN_STATE, refuse: '', rule: "the move can start from the item's current state", next: 'pullboard show <id>', source: 'board' },+ { id: 'joined', refuse: 'NOT_JOINED', rule: 'the caller is the main checkout, or a worktree that joined a lane', next: 'pullboard join <lane> (see: pullboard lanes)', source: 'cli' },+ { id: 'itemExists', refuse: 'NO_ITEM', rule: 'the item exists', next: 'pullboard list --all', source: 'board' },+ { id: 'coordinatorOnly', refuse: 'COORDINATOR_ONLY', rule: 'the caller is the coordinator', next: 'run it from the main checkout', source: 'board' },+ { id: 'coordinatorSaysAs', refuse: 'MAIN_IS_COORDINATOR', rule: 'in the main checkout, the caller says it is the coordinator', next: 'verify from your own worktree; the coordinator adds --as coordinator', source: 'cli' },+ { id: 'holderOrCoordinator', refuse: 'NOT_YOURS', rule: 'the caller holds the item, or is the coordinator', next: 'shout its holder, or the coordinator', source: 'board' },+ { id: 'isHolder', refuse: 'NOT_YOURS', rule: 'the caller holds the claim', next: 'pullboard claim <id>', source: 'board' },+ { id: 'inLane', refuse: 'WRONG_LANE', rule: "the item is in the caller's lane; the main checkout builds coordinator-lane items only", next: "cd <the lane's worktree> && pullboard claim <id>", source: 'board' },+ { id: 'routeAllows', refuse: 'ROUTE', rule: "the caller's route covers the item's: light, then mid, then strong", next: 'pullboard next, which offers only items your route covers', source: 'board' },+ { id: 'dependenciesVerified', refuse: 'BLOCKED', rule: 'every item it waits on is verified', next: 'claim another item, or shout the lane it waits on', source: 'board' },+ { id: 'notHeldByAnother', refuse: 'HELD', rule: 'no other agent holds it under a live lease', next: 'pullboard next', source: 'board' },+ { id: 'laneOpen', refuse: 'LANE_HELD', rule: 'nobody holds its lane', next: 'pullboard next --wait 9 (minutes)', source: 'board' },+ { id: 'oneLiveClaim', refuse: 'ONE_CLAIM', rule: 'the caller holds no other live top-level claim, reworks of its own rejected items aside', next: 'submit or release the other item first; child items are free', source: 'board' },+ { id: 'rowsInForce', refuse: 'UNKNOWN_SPEC', rule: 'every row the item cites exists and is in force', next: 'fix the spec, or the coordinator withdraws the item', source: 'board' },+ { id: 'criterionUnchanged', refuse: 'CRITERIA_CHANGED', rule: 'the criterion and the rows it cites read as they did at claim', next: 'the coordinator runs pullboard refreeze <id>', source: 'cli' },+ { id: 'treeClean', refuse: 'DIRTY', rule: 'the worktree has no uncommitted changes', next: 'commit your changes, then submit', source: 'cli' },+ { id: 'nothingUntracked', refuse: 'UNTRACKED', rule: 'the worktree has no untracked files', next: 'commit or ignore them, then submit', source: 'cli' },+ { id: 'hasCommit', refuse: 'NO_COMMIT', rule: 'there is a commit to submit', next: 'commit your work, then submit', source: 'cli' },+ { id: 'gateConfigured', refuse: 'NO_GATE', rule: 'the repo names a gate command', next: 'set "gate" in pullboard.json, e.g. "npm test"', source: 'cli' },+ { id: 'gateGreen', refuse: 'GATE_RED', rule: 'the gate is green at HEAD', next: 'fix what the digest names, commit, submit again', source: 'cli' },+ { id: 'childrenDone', refuse: 'CHILDREN_OPEN', rule: 'every child item is verified or withdrawn', next: 'finish the child items, or the coordinator withdraws them', source: 'board' },+ { id: 'headIsNew', refuse: 'HEAD_NOT_NEW', rule: 'a verifier has not already rejected this commit', next: 'commit the rework, then submit', source: 'board' },+ { id: 'atSubmittedCommit', refuse: 'NOT_AT_COMMIT', rule: "the caller's checkout contains the submitted commit", next: 'git switch --detach <commit>', source: 'cli' },+ { id: 'notBuilder', refuse: 'SELF_VERIFY', rule: 'the caller did not build it', next: 'another agent verifies it: pullboard next --verify', source: 'board' },+ { id: 'policyAllows', refuse: 'COORDINATOR_VERIFIES', rule: "the repo's verify policy lets the caller verify this lane's work", next: 'the coordinator verifies it', source: 'board' },+ { id: 'reasonIsMet', refuse: 'BAD_REASON', rule: 'an accept gives CRITERION_MET as its reason', next: 'a failed criterion is a reject: pullboard verify <id> reject --reason CODE', source: 'board' },+ { id: 'proofNoted', refuse: 'PROOF_REQUIRED', rule: 'an accept notes how it was proved', next: '--note "what you broke or which edge you tried, and what happened"', source: 'board' },+ { id: 'reasonCoded', refuse: 'BAD_REASON', rule: 'a reject names one of the reject reasons', next: '--reason TEST_FAILURE, BEHAVIOR_MISMATCH, INSUFFICIENT_EVIDENCE, STALE_HEAD or OTHER', source: 'board' },+ { id: 'noteGiven', refuse: 'NOTE_REQUIRED', rule: 'the move carries a note: what failed, what was tried, or why', next: '--note "..." or --note-file <file>', source: 'board' },+];++/** @type {Move[]} */+export const MOVES = [+ {+ verb: 'claim', from: ['open', 'claimed'], to: 'claimed', by: ['agent', 'coordinator'], refuse: 'NOT_CLAIMABLE',+ guards: ['joined', 'itemExists', IN_STATE, 'inLane', 'routeAllows', 'dependenciesVerified', 'notHeldByAnother', 'laneOpen', 'oneLiveClaim', 'rowsInForce'],+ sets: ['item_owner', 'item_lease_until', 'item_frozen_digest'], command: 'pullboard claim <id>',+ },+ {+ verb: 'release', from: ['claimed'], to: 'open', by: ['agent', 'coordinator'], refuse: 'NOT_YOURS',+ guards: ['joined', 'itemExists', IN_STATE, 'isHolder'], command: 'pullboard release <id>',+ },+ { verb: 'lapse', from: ['claimed'], to: 'open', by: ['clock'], guards: [], when: 'its lease runs out' },+ {+ verb: 'submit', from: ['claimed'], to: 'submitted', by: ['agent', 'coordinator'], refuse: 'NOT_YOURS',+ guards: ['joined', 'itemExists', IN_STATE, 'isHolder', 'criterionUnchanged', 'treeClean', 'nothingUntracked', 'hasCommit', 'gateConfigured', 'gateGreen', 'childrenDone', 'headIsNew'],+ sets: ['item_built_by', 'item_commit'], command: 'pullboard submit <id>',+ },+ {+ verb: 'accept', from: ['submitted'], to: 'verified', by: ['agent', 'coordinator'], refuse: 'NOT_SUBMITTED',+ guards: ['coordinatorSaysAs', 'joined', 'itemExists', IN_STATE, 'atSubmittedCommit', 'notBuilder', 'routeAllows', 'policyAllows', 'criterionUnchanged', 'reasonIsMet', 'proofNoted'],+ sets: ['item_verified_by'], command: 'pullboard verify <id> accept --note "..."',+ },+ {+ verb: 'reject', from: ['submitted'], to: 'open', by: ['agent', 'coordinator'], refuse: 'NOT_SUBMITTED',+ guards: ['coordinatorSaysAs', 'joined', 'itemExists', IN_STATE, 'atSubmittedCommit', 'notBuilder', 'routeAllows', 'policyAllows', 'criterionUnchanged', 'reasonCoded', 'noteGiven'],+ command: 'pullboard verify <id> reject --reason CODE --note "..."',+ },+ {+ verb: 'escalate', from: ['open', 'claimed'], to: 'open', by: ['agent', 'coordinator'], refuse: 'CLOSED',+ guards: ['joined', 'noteGiven', 'itemExists', 'holderOrCoordinator', IN_STATE], command: 'pullboard escalate <id> --note "..."',+ },+ {+ verb: 'refreeze', from: ['open', 'claimed', 'submitted'], to: 'open', by: ['coordinator'], refuse: 'CLOSED',+ guards: ['joined', 'coordinatorOnly', 'itemExists', IN_STATE, 'rowsInForce'],+ sets: ['item_frozen_digest'], command: 'pullboard refreeze <id>',+ },+ {+ verb: 'withdraw', from: ['open', 'claimed', 'submitted'], to: 'withdrawn', by: ['coordinator'], refuse: 'CLOSED',+ guards: ['joined', 'coordinatorOnly', 'noteGiven', 'itemExists', IN_STATE],+ sets: ['item_withdrawn_reason'], command: 'pullboard withdraw <id> <reason>',+ },+];++/**+ * What every move into a final state must pass, whatever the command (M2). The interpreter adds+ * any a move leaves out, and `machineProblems` reports the move anyway: a guard nobody can see in+ * a move's list is a guard nobody reviews.+ *+ * @type {Record<string, string[]>}+ */+export const EXIT_GUARDS = {+ verified: ['atSubmittedCommit', 'notBuilder', 'criterionUnchanged', 'proofNoted'],+ withdrawn: ['coordinatorOnly', 'noteGiven'],+};++/** The refusal for a move nobody declared, such as a verdict that is neither accept nor reject. */+export const UNKNOWN_MOVE = { refuse: 'BAD_DECISION', rule: 'a verdict is accept or reject', next: 'pullboard verify <id> accept, or reject --reason CODE --note "..."' };++/** @type {Machine} */+export const MACHINE = { roles: ROLES, states: STATES, guards: GUARDS, moves: MOVES, exitGuards: EXIT_GUARDS, unknownMove: UNKNOWN_MOVE };++/**+ * The guards a move passes, in order: its own, then any exit guard of its target it left out.+ *+ * @param {Move} move+ * @param {Machine} [machine]+ * @returns {string[]}+ */+export function effectiveGuards(move, machine = MACHINE) {+ const exit = machine.exitGuards[move.to] ?? [];+ return [...move.guards, ...exit.filter((id) => !move.guards.includes(id))];+}++/**+ * The states reachable from `start` along `edges`.+ *+ * @param {string} start+ * @param {Map<string, string[]>} edges+ * @returns {Set<string>}+ */+function reach(start, edges) {+ const seen = new Set([start]);+ const queue = [start];+ while (queue.length) {+ for (const next of edges.get(/** @type {string} */ (queue.shift())) ?? []) {+ if (!seen.has(next)) {+ seen.add(next);+ queue.push(next);+ }+ }+ }+ return seen;+}++/**+ * Property 1: every state, guard and role a move names is declared.+ *+ * @param {Machine} machine+ * @returns {string[]}+ */+function unresolved(machine) {+ const states = new Set(machine.states.map((state) => state.id));+ const guards = new Set(machine.guards.map((guard) => guard.id));+ const problems = [];+ for (const move of machine.moves) {+ for (const state of [...move.from, move.to]) if (!states.has(state)) problems.push(`${move.verb}: no state "${state}"`);+ for (const id of move.guards) if (!guards.has(id)) problems.push(`${move.verb}: no guard "${id}"`);+ for (const role of move.by) if (!machine.roles.includes(role)) problems.push(`${move.verb}: no role "${role}"`);+ }+ for (const [state, ids] of Object.entries(machine.exitGuards)) {+ if (!states.has(state)) problems.push(`exit guards for an undeclared state "${state}"`);+ for (const id of ids) if (!guards.has(id)) problems.push(`exit guards of ${state}: no guard "${id}"`);+ }+ return problems;+}++/**+ * Property 2: every state is reachable from open, every non-final state can still reach a final+ * one (no traps), and nothing leaves a final state.+ *+ * @param {Machine} machine+ * @returns {string[]}+ */+function unreachable(machine) {+ const forward = new Map();+ const back = new Map();+ for (const move of machine.moves) {+ for (const from of move.from) {+ forward.set(from, [...(forward.get(from) ?? []), move.to]);+ back.set(move.to, [...(back.get(move.to) ?? []), from]);+ }+ }+ const fromOpen = reach('open', forward);+ const canEnd = new Set();+ for (const state of machine.states.filter((entry) => entry.final)) for (const id of reach(state.id, back)) canEnd.add(id);+ const problems = [];+ for (const state of machine.states) {+ if (!fromOpen.has(state.id)) problems.push(`state ${state.id} cannot be reached from open`);+ if (!canEnd.has(state.id)) problems.push(`state ${state.id} is a trap: no way from it to a final state`);+ if (state.final && forward.has(state.id)) problems.push(`final state ${state.id} has a move out`);+ }+ return problems;+}++/**+ * Properties 3 and 4: every final state has exit guards, and every move into it names them all.+ * A move that leaves one out is a second door (M2).+ *+ * @param {Machine} machine+ * @returns {string[]}+ */+function secondDoors(machine) {+ const problems = [];+ for (const state of machine.states.filter((entry) => entry.final)) {+ if (!(machine.exitGuards[state.id] ?? []).length) problems.push(`final state ${state.id} has no exit guards`);+ }+ for (const move of machine.moves) {+ for (const id of machine.exitGuards[move.to] ?? []) {+ if (!move.guards.includes(id)) problems.push(`${move.verb} reaches ${move.to} without naming its exit guard ${id}: a second door`);+ }+ }+ return problems;+}++/**+ * Property 5: every guard has a code, a rule and a next step, and some move uses it; every move+ * a caller makes checks the item's state exactly once and has a code for it (P4).+ *+ * @param {Machine} machine+ * @returns {string[]}+ */+function unnamedRefusals(machine) {+ const used = new Set([...machine.moves.flatMap((move) => move.guards), ...Object.values(machine.exitGuards).flat()]);+ const problems = [];+ for (const guard of machine.guards) {+ if (guard.id !== IN_STATE && !/^[A-Z][A-Z_]*$/.test(guard.refuse)) problems.push(`guard ${guard.id} names no refusal code`);+ if (!guard.rule.trim()) problems.push(`guard ${guard.id} states no rule`);+ if (!guard.next.trim()) problems.push(`guard ${guard.id} refuses without a next step`);+ if (!used.has(guard.id)) problems.push(`guard ${guard.id} is declared but no move uses it`);+ }+ for (const move of machine.moves.filter((entry) => !entry.by.includes('clock'))) {+ const checks = move.guards.filter((id) => id === IN_STATE).length;+ if (checks !== 1) problems.push(`${move.verb} checks the item's state ${checks} times, not once`);+ if (!/^[A-Z][A-Z_]*$/.test(move.refuse ?? '')) problems.push(`${move.verb} names no code for an item in the wrong state`);+ }+ if (!/^[A-Z][A-Z_]*$/.test(machine.unknownMove.refuse) || !machine.unknownMove.next.trim()) problems.push('the unknown-move refusal has no code or no next step');+ return problems;+}++/**+ * Property 6: who may make a move agrees with its guards. The coordinator's moves name+ * coordinatorOnly, and only they do; the clock's move is the clock's alone and checks nothing a+ * caller could supply.+ *+ * @param {Machine} machine+ * @returns {string[]}+ */+function roleMismatches(machine) {+ const problems = [];+ for (const move of machine.moves) {+ const coordinatorOnly = move.by.length === 1 && move.by[0] === 'coordinator';+ if (coordinatorOnly !== move.guards.includes('coordinatorOnly')) {+ problems.push(`${move.verb} is for ${move.by.join(' or ')}, but its guards ${coordinatorOnly ? 'do not name' : 'name'} coordinatorOnly`);+ }+ if (move.by.includes('clock') && (move.by.length !== 1 || move.guards.length)) {+ problems.push(`${move.verb} mixes the clock with callers or guards a caller could supply`);+ }+ }+ return problems;+}++/**+ * Property 7: every move sets each field its target state requires, or comes from states that+ * already require it.+ *+ * @param {Machine} machine+ * @returns {string[]}+ */+function missingFields(machine) {+ const requires = new Map(machine.states.map((state) => [state.id, state.requires]));+ const problems = [];+ for (const move of machine.moves) {+ for (const from of move.from) {+ const carried = new Set([...(requires.get(from) ?? []), ...(move.sets ?? [])]);+ for (const field of requires.get(move.to) ?? []) {+ if (!carried.has(field)) problems.push(`${move.verb} from ${from} can reach ${move.to} without ${field}`);+ }+ }+ }+ return problems;+}++/**+ * Every property the declaration breaks (M4); empty when it is sound.+ *+ * @param {Machine} [machine]+ * @returns {string[]}+ */+export function machineProblems(machine = MACHINE) {+ return [+ ...unresolved(machine),+ ...unreachable(machine),+ ...secondDoors(machine),+ ...unnamedRefusals(machine),+ ...roleMismatches(machine),+ ...missingFields(machine),+ ];+}diff --git a/test/machine.test.js b/test/machine.test.jsnew file mode 100644index 00000000..dc9e75a1--- /dev/null+++ b/test/machine.test.js@@ -0,0 +1,295 @@+/**+ * The item lifecycle's declaration (M1, M2, M4): sound on its own terms, each property shown+ * failing on a broken copy, and in step with the refusals board.js and cli.js raise today.+ */+import assert from 'node:assert/strict';+import { readFileSync } from 'node:fs';+import { test } from 'node:test';+import { IN_STATE, MACHINE, effectiveGuards, machineProblems } from '../src/machine.js';++/**+ * Refusals that are not about an item's lifecycle, so no move declares them: how a command was+ * typed, and registering who is asking, which happens before any move.+ */+const NOT_MOVES = {+ USAGE: 'how a command was typed',+ NO_FILE: 'how a command was typed: a --note-file that does not exist',+ ALREADY_JOINED: 'registering who is asking, before any move',+ BAD_ROUTE: 'registering who is asking, before any move',+ ONE_COORDINATOR: 'registering who is asking, before any move',+};++/** Where each move starts in the code: the board's functions, and the CLI's submit and verify. */+const ENTRY_POINTS = [+ ['board.js', 'claim'],+ ['board.js', 'release'],+ ['board.js', 'submit'],+ ['board.js', 'verify'],+ ['board.js', 'escalate'],+ ['board.js', 'withdraw'],+ ['board.js', 'refreeze'],+ ['cli.js', 'submitHere'],+ ['cli.js', 'verifyHere'],+];++const parsed = new Map();++/**+ * A source file's top-level functions by name, and what it imports from this package.+ *+ * @param {string} file - A file in src/.+ * @returns {{ bodies: Map<string, string>, named: Map<string, [string, string]>, spaces: Map<string, string> }}+ */+function parse(file) {+ if (parsed.has(file)) return parsed.get(file);+ const source = readFileSync(new URL(`../src/${file}`, import.meta.url), 'utf8');+ const bodies = new Map();+ let name = null;+ let lines = [];+ for (const line of source.split('\n')) {+ const start = /^(?:export )?(?:async )?function (\w+)\(/.exec(line);+ if (start) {+ if (name !== null) bodies.set(name, lines.join('\n'));+ [name, lines] = [start[1], []];+ }+ if (name === null) continue;+ lines.push(line);+ if (line === '}') {+ bodies.set(name, lines.join('\n'));+ name = null;+ }+ }+ const named = new Map();+ for (const [, names, from] of source.matchAll(/import\s*\{([^}]*)\}\s*from\s*'\.\/([\w-]+\.js)'/g)) {+ for (const part of names.split(',').map((text) => text.trim()).filter(Boolean)) {+ const [original, local = original] = part.split(/\s+as\s+/);+ named.set(local, [from, original]);+ }+ }+ const spaces = new Map([...source.matchAll(/import \* as (\w+) from '\.\/([\w-]+\.js)'/g)].map(([, space, from]) => [space, from]));+ const result = { bodies, named, spaces };+ parsed.set(file, result);+ return result;+}++/**+ * Every refusal code a function raises, itself or through the functions of this package it calls.+ *+ * @param {string} file+ * @param {string} name+ * @param {Set<string>} [seen]+ * @returns {Set<string>}+ */+function refusalsOf(file, name, seen = new Set()) {+ const key = `${file}#${name}`;+ if (seen.has(key)) return new Set();+ seen.add(key);+ const { bodies, named, spaces } = parse(file);+ const body = bodies.get(name) ?? '';+ const codes = new Set([...body.matchAll(/new Refused\(\s*'([A-Z_]+)'/g)].map((match) => match[1]));+ for (const [, callee] of body.matchAll(/(?<![.\w])(\w+)\(/g)) {+ const target = bodies.has(callee) ? [file, callee] : named.get(callee);+ if (target) for (const code of refusalsOf(target[0], target[1], seen)) codes.add(code);+ }+ for (const [, space, callee] of body.matchAll(/\b(\w+)\.(\w+)\(/g)) {+ if (spaces.has(space)) for (const code of refusalsOf(spaces.get(space), callee, seen)) codes.add(code);+ }+ return codes;+}++/**+ * Every refusal code a declaration names: its guards', its moves' wrong-state codes, and the+ * unknown move's.+ *+ * @param {any} machine+ * @returns {Set<string>}+ */+function declaredCodes(machine) {+ return new Set([+ ...machine.guards.map((guard) => guard.refuse),+ ...machine.moves.map((move) => move.refuse),+ machine.unknownMove.refuse,+ ].filter(Boolean));+}++/** Every code board.js and cli.js raise, directly or through the functions they call. */+const raisedAnywhere = new Set(['board.js', 'cli.js'].flatMap((file) => [...parse(file).bodies.keys()].flatMap((name) => [...refusalsOf(file, name)])));++/** Every code the lifecycle's moves raise, starting where each move starts in the code. */+const raisedByMoves = new Set(ENTRY_POINTS.flatMap(([file, name]) => [...refusalsOf(file, name)]));++/**+ * Where a declaration and the code disagree: a code the moves raise that it does not declare,+ * or a code it declares that the code never raises.+ *+ * @param {any} machine+ * @returns {string[]}+ */+function codeProblems(machine) {+ const declared = declaredCodes(machine);+ return [+ ...[...raisedByMoves].filter((code) => !declared.has(code) && !(code in NOT_MOVES)).map((code) => `${code} is raised by a move but not declared`),+ ...[...declared].filter((code) => !raisedAnywhere.has(code)).map((code) => `${code} is declared but board.js and cli.js never raise it`),+ ];+}++/**+ * A deep copy of the declaration to break, so the real one stays whole.+ *+ * @returns {any}+ */+const copy = () => structuredClone(MACHINE);++/**+ * The move with this verb.+ *+ * @param {any} machine+ * @param {string} verb+ * @returns {any}+ */+const moveOf = (machine, verb) => machine.moves.find((move) => move.verb === verb);++/** Each property, broken the way a real board could break, and what the check must then say. */+const BROKEN = [+ {+ property: 'every state, guard and role a move names is declared',+ breaks: (machine) => moveOf(machine, 'claim').from.push('limbo'),+ says: /claim: no state "limbo"/,+ },+ {+ property: 'every state is reachable from open',+ breaks: (machine) => {+ machine.states.push({ id: 'orphan', final: true, means: 'nothing leads here', requires: [] });+ machine.exitGuards.orphan = ['notBuilder'];+ },+ says: /state orphan cannot be reached from open/,+ },+ {+ property: 'every non-final state has a way out',+ breaks: (machine) => {+ machine.states.push({ id: 'parked', means: 'set aside', requires: [] });+ machine.moves.push({ verb: 'park', from: ['open'], to: 'parked', by: ['agent', 'coordinator'], refuse: 'CLOSED', guards: ['joined', 'itemExists', IN_STATE] });+ },+ says: /state parked is a trap/,+ },+ {+ property: 'nothing leaves a final state',+ breaks: (machine) => machine.moves.push({ verb: 'reopen', from: ['verified'], to: 'open', by: ['agent', 'coordinator'], refuse: 'NOT_VERIFIED', guards: ['joined', 'itemExists', IN_STATE] }),+ says: /final state verified has a move out/,+ },+ {+ property: 'every final state has exit guards',+ breaks: (machine) => {+ machine.exitGuards.verified = [];+ },+ says: /final state verified has no exit guards/,+ },+ {+ property: 'accept names every exit guard of verified',+ breaks: (machine) => {+ const accept = moveOf(machine, 'accept');+ accept.guards = accept.guards.filter((id) => id !== 'notBuilder');+ },+ says: /accept reaches verified without naming its exit guard notBuilder: a second door/,+ },+ {+ property: 'no second door into verified',+ breaks: (machine) => machine.moves.push({ verb: 'land', from: ['submitted'], to: 'verified', by: ['agent', 'coordinator'], refuse: 'NOT_SUBMITTED', guards: ['joined', 'itemExists', IN_STATE], sets: ['item_verified_by'] }),+ says: /land reaches verified without naming its exit guard atSubmittedCommit: a second door/,+ },+ {+ property: 'every guard names a refusal code',+ breaks: (machine) => {+ machine.guards.find((guard) => guard.id === 'laneOpen').refuse = '';+ },+ says: /guard laneOpen names no refusal code/,+ },+ {+ property: 'every guard says the next step',+ breaks: (machine) => {+ machine.guards.find((guard) => guard.id === 'laneOpen').next = ' ';+ },+ says: /guard laneOpen refuses without a next step/,+ },+ {+ property: 'every guard is used by some move',+ breaks: (machine) => machine.guards.push({ id: 'unused', refuse: 'UNUSED', rule: 'nothing', next: 'nothing', source: 'board' }),+ says: /guard unused is declared but no move uses it/,+ },+ {+ property: "every caller's move checks the item's state once",+ breaks: (machine) => {+ const release = moveOf(machine, 'release');+ release.guards = release.guards.filter((id) => id !== IN_STATE);+ },+ says: /release checks the item's state 0 times, not once/,+ },+ {+ property: "the coordinator's moves name coordinatorOnly",+ breaks: (machine) => {+ moveOf(machine, 'withdraw').by = ['agent', 'coordinator'];+ },+ says: /withdraw is for agent or coordinator, but its guards name coordinatorOnly/,+ },+ {+ property: "the clock's move is the clock's alone",+ breaks: (machine) => {+ moveOf(machine, 'lapse').by = ['clock', 'agent'];+ },+ says: /lapse mixes the clock with callers/,+ },+ {+ property: "every move sets its target state's fields",+ breaks: (machine) => {+ moveOf(machine, 'submit').sets = ['item_built_by'];+ },+ says: /submit from claimed can reach submitted without item_commit/,+ },+];++test('the declared lifecycle is sound: reachable, no traps, no second door, every refusal with a next step [M1, M2, M4]', () => {+ assert.deepEqual(machineProblems(), []);+ assert.deepEqual(MACHINE.states.map((state) => state.id), ['open', 'claimed', 'submitted', 'verified', 'withdrawn']);+ assert.deepEqual(MACHINE.states.filter((state) => state.final).map((state) => state.id), ['verified', 'withdrawn']);+ assert.deepEqual(MACHINE.moves.map((move) => move.verb), ['claim', 'release', 'lapse', 'submit', 'accept', 'reject', 'escalate', 'refreeze', 'withdraw']);+});++for (const broken of BROKEN) {+ test(`a broken copy fails the check: ${broken.property} [M4]`, () => {+ const machine = copy();+ broken.breaks(machine);+ const problems = machineProblems(machine);+ assert.ok(problems.some((problem) => broken.says.test(problem)), `expected ${broken.says}, got:\n${problems.join('\n')}`);+ assert.deepEqual(machineProblems(), [], 'the real declaration stays sound');+ });+}++test('a door declared without its exit guards still passes them [M2]', () => {+ const machine = copy();+ const land = { verb: 'land', from: ['submitted'], to: 'verified', by: ['agent', 'coordinator'], refuse: 'NOT_SUBMITTED', guards: ['joined', 'itemExists', IN_STATE] };+ machine.moves.push(land);+ const passes = effectiveGuards(land, machine);+ for (const id of machine.exitGuards.verified) assert.ok(passes.includes(id), `land passes ${id}`);+ assert.deepEqual(passes.slice(0, land.guards.length), land.guards, 'its own guards come first, in order');+ assert.deepEqual(effectiveGuards(moveOf(MACHINE, 'accept')), moveOf(MACHINE, 'accept').guards, 'accept names them all already, so nothing is added');+});++test('the declaration names every refusal the moves raise today, and only codes the code raises [M1]', () => {+ assert.deepEqual(codeProblems(MACHINE), []);+ for (const code of ['NOT_CLAIMABLE', 'LANE_HELD', 'GATE_RED', 'NO_GATE', 'NOT_AT_COMMIT', 'SELF_VERIFY', 'BAD_DECISION']) {+ assert.ok(raisedByMoves.has(code), `the code walk finds ${code}`);+ }+ for (const code of Object.keys(NOT_MOVES)) assert.ok(raisedByMoves.has(code), `${code} is listed as not a move because a move's code path raises it`);+});++test('a broken copy fails the check against the code, both ways [M1, M4]', () => {+ const missing = copy();+ missing.guards = missing.guards.filter((guard) => guard.id !== 'laneOpen');+ for (const move of missing.moves) move.guards = move.guards.filter((id) => id !== 'laneOpen');+ assert.deepEqual(codeProblems(missing), ['LANE_HELD is raised by a move but not declared']);++ const invented = copy();+ invented.guards.push({ id: 'moonPhase', refuse: 'WRONG_MOON', rule: 'the moon is full', next: 'wait', source: 'board' });+ moveOf(invented, 'claim').guards.push('moonPhase');+ assert.deepEqual(codeProblems(invented), ['WRONG_MOON is declared but board.js and cli.js never raise it']);+});