pi-legion 4.0.0 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/CHANGELOG.md +248 -1
  2. package/README.md +101 -44
  3. package/agents/centurion.md +20 -16
  4. package/agents/scout.md +3 -1
  5. package/agents/worker.md +5 -7
  6. package/docs/contracts/declarative-phase-machine-v5.md +128 -0
  7. package/docs/contracts/extension-inheritance-wiring-v1.md +428 -0
  8. package/docs/contracts/subagent-renewal-v1.md +260 -0
  9. package/docs/headless-child-contract.md +6 -3
  10. package/docs/inprocess-supervisor.md +70 -24
  11. package/docs/legion-v2-contract.md +9 -0
  12. package/docs/spawn-mode-visibility-v1.md +4 -1
  13. package/index.ts +598 -44
  14. package/package.json +2 -2
  15. package/src/agents/agents.ts +293 -36
  16. package/src/agents/overrides.ts +3 -1
  17. package/src/agents/settings.ts +6 -18
  18. package/src/extension/child-guard.ts +25 -1
  19. package/src/extension/legion-tools.ts +66 -21
  20. package/src/extension/notify.ts +136 -4
  21. package/src/extension/playbook.ts +2 -2
  22. package/src/extension/runtime.ts +56 -1
  23. package/src/extension/summary.ts +13 -4
  24. package/src/extension/timing.ts +8 -4
  25. package/src/legion/phase-conditions.ts +164 -0
  26. package/src/legion/phase-machine.ts +403 -0
  27. package/src/legion/state-machine.ts +219 -115
  28. package/src/runs/args.ts +49 -18
  29. package/src/runs/extension-inheritance.ts +463 -0
  30. package/src/runs/orchestrator.ts +137 -44
  31. package/src/runs/progress-signal.ts +137 -0
  32. package/src/runs/store-lookup.ts +270 -0
  33. package/src/shared/types.ts +88 -3
  34. package/src/supervisor/child-context.ts +19 -0
  35. package/src/supervisor/inprocess-supervisor.ts +44 -5
  36. package/src/supervisor/routing-supervisor.ts +16 -1
  37. package/src/supervisor/rpc-supervisor.ts +16 -2
  38. package/src/tui/tree-view.ts +56 -0
  39. package/src/tui/viz/palette.ts +0 -3
  40. package/agents/advisor.md +0 -110
  41. package/agents/designer.md +0 -87
  42. package/agents/planner.md +0 -53
  43. package/agents/prototype.md +0 -72
  44. package/agents/reviewer.md +0 -113
@@ -0,0 +1,164 @@
1
+ /**
2
+ * Condition vocabulary for a declarative phase machine (#298).
3
+ *
4
+ * The old state machine keyed its rules on ROLE NAMES: leaving `implementing`
5
+ * required every `role === "worker"` to be settled, leaving `reviewing` required
6
+ * a settled `role === "reviewer"` with `ok: true`. That made a role's mere
7
+ * existence load-bearing — "worker" stopped meaning "a worker" and became "the
8
+ * specific slot in a fixed pipeline".
9
+ *
10
+ * A condition asks the same question without naming anyone: "are the children
11
+ * settled", "did any settled child return ok", "did verification pass". That is
12
+ * the property that lets `worker` generalise into a plain worker, and it is what
13
+ * makes a custom phase list possible at all.
14
+ *
15
+ * The vocabulary is CLOSED on purpose. A machine that can express arbitrary code
16
+ * cannot be validated at load time, and an invalid machine would only surface
17
+ * mid-run — after a team has already spent tokens.
18
+ */
19
+
20
+ /** A child of the node being evaluated, as the machine sees it. */
21
+ export interface PhaseChildSnapshot {
22
+ id: string;
23
+ /** Kept for reporting only. No condition may branch on it (#298). */
24
+ role: string;
25
+ status: string;
26
+ }
27
+
28
+ /** Everything a condition may inspect. Pure data: no I/O, no clock. */
29
+ export interface PhaseConditionInput {
30
+ children: readonly PhaseChildSnapshot[];
31
+ /** Child ids whose failure has already been accounted for. */
32
+ handledIds?: readonly string[];
33
+ /** Contract file path recorded on the node, if any. */
34
+ contractPath?: string | null;
35
+ /** Whether the contract file exists and is non-empty. Resolved by the caller. */
36
+ contractPresent?: boolean;
37
+ /** Parsed `ok` from the most recent settled child verdict, when there is one. */
38
+ verdictOk?: boolean;
39
+ /** Stored verification result for this node. */
40
+ verificationPassed?: boolean;
41
+ reworkCount?: number;
42
+ maxRework?: number;
43
+ }
44
+
45
+ /** The closed condition vocabulary. */
46
+ export const PHASE_CONDITIONS = [
47
+ "always",
48
+ "contract_file",
49
+ "children_settled",
50
+ "children_present",
51
+ "children_none_failed",
52
+ "verdict_ok",
53
+ "verdict_not_ok",
54
+ "verification_passed",
55
+ "verification_failed",
56
+ "rework_available",
57
+ "children_all_failed",
58
+ ] as const;
59
+
60
+ export type PhaseCondition = (typeof PHASE_CONDITIONS)[number];
61
+
62
+ export function isPhaseCondition(value: unknown): value is PhaseCondition {
63
+ return typeof value === "string" && (PHASE_CONDITIONS as readonly string[]).includes(value);
64
+ }
65
+
66
+ function isActive(status: string): boolean {
67
+ return status === "starting" || status === "running" || status === "blocked";
68
+ }
69
+
70
+ /**
71
+ * Evaluate one condition. Throws on an unknown name so a machine that slipped
72
+ * past load-time validation fails loudly instead of silently allowing a
73
+ * transition (which would advance a team that has not met its own criteria).
74
+ */
75
+ export function checkPhaseCondition(
76
+ condition: PhaseCondition,
77
+ input: PhaseConditionInput,
78
+ ): boolean {
79
+ switch (condition) {
80
+ case "always":
81
+ return true;
82
+ case "contract_file":
83
+ // A recorded path is the contract; `contractPresent` is an OPTIONAL
84
+ // strengthening for callers that actually read the file. Defaulting it to
85
+ // "must be explicit" would silently refuse every caller that has a path
86
+ // but does not stat it — including the pre-#298 tests and the rpc path.
87
+ return Boolean(input.contractPath?.trim()) && input.contractPresent !== false;
88
+ case "children_settled":
89
+ return input.children.every((child) => !isActive(child.status));
90
+ case "children_present":
91
+ // Distinguishes "every child finished" from "there were no children".
92
+ // Without it, `children_settled` on an empty list is vacuously true, so a
93
+ // team that dispatched nobody could satisfy a requirement meant to check
94
+ // that its workers (or reviewers) had actually run.
95
+ return input.children.length > 0;
96
+ case "children_none_failed": {
97
+ const handled = new Set(input.handledIds ?? []);
98
+ return !input.children.some((child) => child.status === "failed" && !handled.has(child.id));
99
+ }
100
+ case "verdict_ok":
101
+ return input.verdictOk === true;
102
+ case "verdict_not_ok":
103
+ return input.verdictOk === false;
104
+ case "verification_passed":
105
+ return input.verificationPassed === true;
106
+ case "verification_failed":
107
+ return input.verificationPassed === false;
108
+ case "rework_available":
109
+ return (input.reworkCount ?? 0) < (input.maxRework ?? 2);
110
+ case "children_all_failed":
111
+ return input.children.length > 0 && input.children.every((child) => child.status === "failed");
112
+ default: {
113
+ const unknown: never = condition;
114
+ throw new TypeError(`unknown phase condition: ${String(unknown)}`);
115
+ }
116
+ }
117
+ }
118
+
119
+ /**
120
+ * A human-readable explanation of a failed condition.
121
+ *
122
+ * The condition id is precise but not readable, and this text reaches operators
123
+ * and models. Naming the CONDITION rather than a role is the point (#298): the
124
+ * message says what is missing, not who was supposed to provide it.
125
+ */
126
+ export function describeFailedCondition(condition: PhaseCondition): string {
127
+ switch (condition) {
128
+ case "contract_file":
129
+ return "a frozen contract file is required";
130
+ case "children_present":
131
+ return "no child has been dispatched yet";
132
+ case "children_settled":
133
+ return "child agents are still running; wait for them to settle";
134
+ case "children_none_failed":
135
+ return "a child failed and must be reworked or the team must fail";
136
+ case "verdict_ok":
137
+ return "no settled child reported a passing verdict";
138
+ case "verdict_not_ok":
139
+ return "the latest verdict was not a rejection, so there is nothing to rework";
140
+ case "verification_passed":
141
+ return "verification has not passed";
142
+ case "verification_failed":
143
+ return "verification did not fail, so there is nothing to rework";
144
+ case "rework_available":
145
+ return "the rework budget is exhausted";
146
+ case "children_all_failed":
147
+ return "not every child failed";
148
+ case "always":
149
+ return "the condition is always true";
150
+ default:
151
+ return `condition '${String(condition)}' is not met`;
152
+ }
153
+ }
154
+
155
+ /** Every condition in `require` holds. An empty list is vacuously true. */
156
+ export function checkPhaseRequirements(
157
+ require: readonly PhaseCondition[],
158
+ input: PhaseConditionInput,
159
+ ): { ok: true } | { ok: false; failed: PhaseCondition } {
160
+ for (const condition of require) {
161
+ if (!checkPhaseCondition(condition, input)) return { ok: false, failed: condition };
162
+ }
163
+ return { ok: true };
164
+ }
@@ -0,0 +1,403 @@
1
+ /**
2
+ * A declarative phase machine (#298).
3
+ *
4
+ * The machine used to be a hard-coded switch. It is now data: a list of phases,
5
+ * each naming its allowed successors and the conditions for taking each one. An
6
+ * undeclared team falls back to {@link DEFAULT_PHASE_MACHINE}, which reproduces
7
+ * the old table exactly — so this is behaviour-preserving until a card opts in.
8
+ *
9
+ * Why transitions carry the conditions rather than the phase:
10
+ *
11
+ * planning → implementing needs a contract file
12
+ * planning → failed must NOT need one
13
+ *
14
+ * A phase-level `require` therefore would have to hold for every exit, including
15
+ * the failure exits, which would make a team unable to fail before it had a
16
+ * contract. Conditions live on the edge; a phase-level `require` is sugar that
17
+ * ANDs into every edge leaving that phase.
18
+ */
19
+
20
+ import {
21
+ type PhaseCondition,
22
+ isPhaseCondition,
23
+ } from "./phase-conditions.ts";
24
+
25
+ /** One allowed exit from a phase. */
26
+ export interface PhaseTransitionRule {
27
+ /** Target phase id. */
28
+ to: string;
29
+ /** Conditions that must all hold. Empty/absent means unconditional. */
30
+ require?: PhaseCondition[];
31
+ /** When set, only this actor role may take the transition. */
32
+ actorRole?: string;
33
+ /** Counts as a rework step: increments `rework_count`, capped by `maxRework`. */
34
+ rework?: boolean;
35
+ /** Notify the parent when taken. */
36
+ notifyParent?: boolean;
37
+ }
38
+
39
+ export interface PhaseDefinition {
40
+ id: string;
41
+ /** ANDed into every rule that leaves this phase. */
42
+ require?: PhaseCondition[];
43
+ /** Allowed exits. A transition to anything else is refused. */
44
+ next: PhaseTransitionRule[];
45
+ /** Advisory clock only; never kills work. */
46
+ timeoutMs?: number;
47
+ /** Terminal phases may not transition out. */
48
+ terminal?: boolean;
49
+ }
50
+
51
+ export interface PhaseMachine {
52
+ phases: PhaseDefinition[];
53
+ /** Rework cap for the whole machine. */
54
+ maxRework?: number;
55
+ /** Which phase a brand-new team enters. Defaults to the first phase. */
56
+ start?: string;
57
+ }
58
+
59
+ /** Phases that are terminal in the built-in machine. */
60
+ export const DEFAULT_TERMINAL_PHASES = ["done", "failed", "aborted"] as const;
61
+
62
+ export const DEFAULT_MAX_REWORK_COUNT = 2;
63
+
64
+ /**
65
+ * The built-in machine: the four-phase pipeline legion has always used.
66
+ *
67
+ * Deliberately expressed with the SAME conditions the old code checked inline,
68
+ * so that removing a role name from a rule changes nothing about when a
69
+ * transition is allowed.
70
+ */
71
+ export const DEFAULT_PHASE_MACHINE: PhaseMachine = {
72
+ start: "planning",
73
+ maxRework: DEFAULT_MAX_REWORK_COUNT,
74
+ phases: [
75
+ {
76
+ id: "planning",
77
+ timeoutMs: 15 * 60_000,
78
+ next: [
79
+ { to: "implementing", require: ["contract_file"] },
80
+ { to: "failed" },
81
+ { to: "aborted" },
82
+ ],
83
+ },
84
+ {
85
+ id: "implementing",
86
+ timeoutMs: 60 * 60_000,
87
+ // Entering implementation means planning is over, so no planning child may
88
+ // still be running. Expressed as a phase-level requirement (ANDed into every
89
+ // exit, including `aborted`) rather than a role check: the old rule named
90
+ // `planner`, which stopped working the moment that card was removed.
91
+ // Phase-level: leaving implementation (in any direction, including a failure)
92
+ // requires that no child is still running. The old rule named `planner`
93
+ // specifically, but the intent was "planning is over, nothing is in flight";
94
+ // naming nobody also means the rule survives the planner card's removal.
95
+ require: ["children_settled"],
96
+ next: [
97
+ { to: "reviewing", require: ["children_present", "children_none_failed"] },
98
+ // Every child failed: the team cannot succeed, so fail it.
99
+ { to: "failed", require: ["children_all_failed"] },
100
+ { to: "aborted" },
101
+ ],
102
+ },
103
+ {
104
+ id: "reviewing",
105
+ timeoutMs: 30 * 60_000,
106
+ next: [
107
+ // `verdict_ok` alone would let a team that produced NO review at all walk
108
+ // straight through (the caller passes `verdictOk: undefined`, and an
109
+ // undefined verdict is not `false`). Pair it with `children_settled` so a
110
+ // review actually happened — the old rule required a settled reviewer.
111
+ { to: "verifying", require: ["children_present", "children_settled", "verdict_ok"] },
112
+ { to: "implementing", require: ["verdict_not_ok"], rework: true },
113
+ // A bad plan may be sent back by the team lead only.
114
+ { to: "planning", actorRole: "centurion", notifyParent: true },
115
+ { to: "failed" },
116
+ { to: "aborted" },
117
+ ],
118
+ },
119
+ {
120
+ id: "verifying",
121
+ timeoutMs: 15 * 60_000,
122
+ next: [
123
+ { to: "done", require: ["verification_passed", "children_settled"] },
124
+ { to: "implementing", require: ["verification_failed"], rework: true },
125
+ { to: "failed" },
126
+ { to: "aborted" },
127
+ ],
128
+ },
129
+ { id: "done", terminal: true, next: [] },
130
+ { id: "failed", terminal: true, next: [] },
131
+ { id: "aborted", terminal: true, next: [] },
132
+ ],
133
+ };
134
+
135
+ /** Look up a phase by id. */
136
+ export function findPhase(
137
+ machine: PhaseMachine,
138
+ id: string,
139
+ ): PhaseDefinition | undefined {
140
+ return machine.phases.find((phase) => phase.id === id);
141
+ }
142
+
143
+ export function isTerminalIn(machine: PhaseMachine, id: string): boolean {
144
+ const phase = findPhase(machine, id);
145
+ if (!phase) return false;
146
+ return phase.terminal === true || phase.next.length === 0;
147
+ }
148
+
149
+ export function machineTimeout(
150
+ machine: PhaseMachine,
151
+ id: string,
152
+ ): number | undefined {
153
+ return findPhase(machine, id)?.timeoutMs;
154
+ }
155
+
156
+ /**
157
+ * Ensure the three universal terminals exist (#298).
158
+ *
159
+ * A declared machine names its own workflow phases, but three things must never
160
+ * be impossible:
161
+ *
162
+ * - the parent must be able to ABORT a team (an emergency stop that a card
163
+ * could accidentally remove is a safety hole)
164
+ * - rework exhaustion and all-children-failed must be able to reach `failed`
165
+ * - the supervisor's verified-completion path targets `done`
166
+ *
167
+ * Rather than reject a machine that omits them (which would punish a card for
168
+ * something it had no reason to think about), they are injected as terminal
169
+ * phases with an unconditional edge from every non-terminal phase. Taking those
170
+ * edges is still gated: `failed` requires a terminal reason and `aborted`
171
+ * requires a parent retire, checked before any edge lookup, and `done` is
172
+ * refused outright for a centurion (only the supervisor may complete a
173
+ * team). So injection adds no new capability — it only removes the possibility of
174
+ * a stranded team.
175
+ */
176
+ export function normalizePhaseMachine(machine: PhaseMachine): PhaseMachine {
177
+ const declared = new Set(machine.phases.map((phase) => phase.id));
178
+ const missing = (["done", "failed", "aborted"] as const).filter((id) => !declared.has(id));
179
+ if (missing.length === 0) return machine;
180
+ const injected: PhaseDefinition[] = missing.map((id) => ({ id, terminal: true, next: [] }));
181
+ const phases = machine.phases.map((phase) => {
182
+ if (phase.terminal === true || phase.next.length === 0) return phase;
183
+ const existing = new Set(phase.next.map((rule) => rule.to));
184
+ const extra = missing.filter((id) => !existing.has(id)).map((to) => ({ to }));
185
+ return extra.length > 0 ? { ...phase, next: [...phase.next, ...extra] } : phase;
186
+ });
187
+ return { ...machine, phases: [...phases, ...injected] };
188
+ }
189
+
190
+ /** A load-time problem: reported with the card path, never swallowed. */
191
+ export interface PhaseMachineError {
192
+ path: string;
193
+ message: string;
194
+ }
195
+
196
+ /**
197
+ * Validate a machine. Returns every problem found rather than the first, so a
198
+ * card author fixes one round of issues instead of one at a time.
199
+ *
200
+ * A machine that is invalid must be REJECTED at load. Accepting it and failing
201
+ * mid-run would waste a team's tokens before surfacing a typo.
202
+ */
203
+ export function validatePhaseMachine(machine: PhaseMachine): PhaseMachineError[] {
204
+ const errors: PhaseMachineError[] = [];
205
+ const ids = new Map<string, PhaseDefinition>();
206
+
207
+ if (machine.phases.length === 0) {
208
+ errors.push({ path: "phases", message: "a phase machine needs at least one phase" });
209
+ return errors;
210
+ }
211
+
212
+ for (const [index, phase] of machine.phases.entries()) {
213
+ const path = `phases[${index}]`;
214
+ if (typeof phase.id !== "string" || !phase.id.trim()) {
215
+ errors.push({ path: `${path}.id`, message: "phase id must be a non-empty string" });
216
+ continue;
217
+ }
218
+ if (ids.has(phase.id)) {
219
+ errors.push({ path: `${path}.id`, message: `duplicate phase id '${phase.id}'` });
220
+ continue;
221
+ }
222
+ ids.set(phase.id, phase);
223
+ for (const condition of phase.require ?? []) {
224
+ if (!isPhaseCondition(condition)) {
225
+ errors.push({
226
+ path: `${path}.require`,
227
+ message: `unknown condition '${String(condition)}'`,
228
+ });
229
+ }
230
+ }
231
+ if (!Array.isArray(phase.next)) {
232
+ errors.push({ path: `${path}.next`, message: "next must be an array" });
233
+ }
234
+ }
235
+
236
+ // Second pass: edges need every id resolved, and the terminal set known.
237
+ for (const [index, phase] of machine.phases.entries()) {
238
+ const path = `phases[${index}]`;
239
+ const selfTerminal = phase.terminal === true;
240
+ if (selfTerminal && (phase.next ?? []).length > 0) {
241
+ errors.push({
242
+ path: `${path}.next`,
243
+ message: `terminal phase '${phase.id}' must not declare transitions`,
244
+ });
245
+ }
246
+ if (!selfTerminal && (phase.next ?? []).length === 0) {
247
+ errors.push({
248
+ path: `${path}.next`,
249
+ message: `phase '${phase.id}' has no transitions and is not marked terminal (unreachable dead end)`,
250
+ });
251
+ }
252
+ for (const [ruleIndex, rule] of (phase.next ?? []).entries()) {
253
+ const rulePath = `${path}.next[${ruleIndex}]`;
254
+ if (!ids.has(rule.to)) {
255
+ errors.push({
256
+ path: `${rulePath}.to`,
257
+ message: `unknown target phase '${String(rule.to)}'`,
258
+ });
259
+ }
260
+ if (rule.to === phase.id) {
261
+ errors.push({
262
+ path: `${rulePath}.to`,
263
+ message: `phase '${phase.id}' lists itself as a successor`,
264
+ });
265
+ }
266
+ for (const condition of rule.require ?? []) {
267
+ if (!isPhaseCondition(condition)) {
268
+ errors.push({
269
+ path: `${rulePath}.require`,
270
+ message: `unknown condition '${String(condition)}'`,
271
+ });
272
+ }
273
+ }
274
+ if (rule.actorRole !== undefined && typeof rule.actorRole !== "string") {
275
+ errors.push({ path: `${rulePath}.actorRole`, message: "actorRole must be a string" });
276
+ }
277
+ }
278
+ }
279
+
280
+ const start = machine.start ?? machine.phases[0]?.id;
281
+ if (start === undefined || !ids.has(start)) {
282
+ errors.push({
283
+ path: "start",
284
+ message: `start phase '${String(start)}' is not a declared phase`,
285
+ });
286
+ } else if (isTerminalIn(machine, start)) {
287
+ errors.push({
288
+ path: "start",
289
+ message: `start phase '${start}' is terminal, so the team could never progress`,
290
+ });
291
+ }
292
+
293
+ if (machine.maxRework !== undefined) {
294
+ if (!Number.isInteger(machine.maxRework) || machine.maxRework < 0) {
295
+ errors.push({
296
+ path: "maxRework",
297
+ message: "maxRework must be a non-negative integer",
298
+ });
299
+ }
300
+ }
301
+
302
+ // Reachability: a phase nobody can enter is dead config, and usually a typo.
303
+ if (start !== undefined) {
304
+ const reachable = new Set<string>([start]);
305
+ const queue = [start];
306
+ while (queue.length > 0) {
307
+ const current = queue.shift()!;
308
+ const phase = ids.get(current);
309
+ for (const rule of phase?.next ?? []) {
310
+ if (!reachable.has(rule.to)) {
311
+ reachable.add(rule.to);
312
+ queue.push(rule.to);
313
+ }
314
+ }
315
+ }
316
+ for (const id of ids.keys()) {
317
+ if (!reachable.has(id)) {
318
+ errors.push({
319
+ path: `phases.${id}`,
320
+ message: `phase '${id}' is unreachable from the start phase '${start}'`,
321
+ });
322
+ }
323
+ }
324
+ }
325
+
326
+ return errors;
327
+ }
328
+
329
+ /**
330
+ * Why a declarative transition was refused.
331
+ *
332
+ * The legacy reason codes are preserved verbatim, because they are asserted in
333
+ * tests and surface in operator-facing messages. New machines reuse the same
334
+ * vocabulary: a condition failure reports the condition name.
335
+ */
336
+ export type PhaseRefusalReason =
337
+ // structural (unchanged)
338
+ | "invalid_phase"
339
+ | "same_phase"
340
+ | "terminal_phase"
341
+ | "invalid_transition"
342
+ | "rework_limit_exceeded"
343
+ | "terminal_reason_required"
344
+ | "all_children_not_failed"
345
+ // legacy names for the default machine's condition failures
346
+ | "contract_path_required"
347
+ | "children_not_terminal"
348
+ | "failed_worker_unhandled"
349
+ | "all_workers_failed"
350
+ | "reviewer_not_settled"
351
+ | "review_verdict_not_accepted"
352
+ | "review_verdict_not_rejected"
353
+ | "verification_not_failed"
354
+ | "verification_not_passed"
355
+ | "centurion_required"
356
+ | "planner_still_active"
357
+ | "worker_still_active"
358
+ // a declared condition failed
359
+ | "condition_not_met"
360
+ | "actor_not_permitted";
361
+
362
+ /**
363
+ * Map a failed condition to the legacy reason code the default machine used.
364
+ *
365
+ * A custom machine reports `condition_not_met` with the condition name, because
366
+ * inventing a legacy-sounding code for a condition the built-in machine never
367
+ * had would be worse than saying what actually failed.
368
+ */
369
+ export function legacyReasonForCondition(
370
+ condition: PhaseCondition,
371
+ edge: { to: string },
372
+ ): PhaseRefusalReason {
373
+ switch (condition) {
374
+ case "contract_file":
375
+ return "contract_path_required";
376
+ case "children_settled":
377
+ // The default machine uses this condition on two different edges with
378
+ // distinct historical messages.
379
+ return edge.to === "reviewing" ? "worker_still_active" : "children_not_terminal";
380
+ case "children_present":
381
+ // Nothing ran, so there is no verdict to accept. The historical name for
382
+ // "you need an actual review before verifying" is reviewer_not_settled.
383
+ return edge.to === "verifying" ? "reviewer_not_settled" : "condition_not_met";
384
+ case "children_none_failed":
385
+ return "failed_worker_unhandled";
386
+ case "children_all_failed":
387
+ return "all_workers_failed";
388
+ case "verdict_ok":
389
+ // Distinguish "no review happened" from "the review said no". The old
390
+ // switch reported `reviewer_not_settled` for the first and
391
+ // `review_verdict_not_accepted` for the second, and callers rely on the
392
+ // difference: one means dispatch a reviewer, the other means rework.
393
+ return edge.to === "verifying" ? "review_verdict_not_accepted" : "condition_not_met";
394
+ case "verdict_not_ok":
395
+ return "review_verdict_not_rejected";
396
+ case "verification_passed":
397
+ return "verification_not_passed";
398
+ case "verification_failed":
399
+ return "verification_not_failed";
400
+ default:
401
+ return "condition_not_met";
402
+ }
403
+ }