// XState v5 machine object artifact compiled by gears2fsm from evaluate.gears.md. // It defines the machine, actor contracts, and typed inputs only; the linked // runtime provides every actor implementation. import { assign, fromPromise, setup } from 'xstate'; // --------------------------------------------------------------------------- // JSON boundary // --------------------------------------------------------------------------- export type JsonValue = | null | boolean | number | string | readonly JsonValue[] | { readonly [key: string]: JsonValue }; // --------------------------------------------------------------------------- // Identities // --------------------------------------------------------------------------- /** Canonical lowercase local ids of the source's declared roles. */ export type RoleId = 'evaluator' | 'reviewer'; export type SourceItemId = | 'EVALUATE-1' | 'EVALUATE-2' | 'EVALUATE-3' | 'EVALUATE-4' | 'EVALUATE-5' | 'EVALUATE-6'; /** Stable ids of the delegated-player working leaves. */ export type PlayerStateId = | 'evaluating' | 'reviewingRound1' | 'fixingRound1' | 'reviewingRound2AfterFix' | 'reviewingRound2AfterRejection' | 'fixingRound2'; export type ResumableStateId = PlayerStateId; export type InterruptTargetId = PlayerStateId; /** No source item carries parallel-group metadata. */ export const concurrentRoleSets: readonly (readonly RoleId[])[] = []; // --------------------------------------------------------------------------- // Context, events, machine input and output // --------------------------------------------------------------------------- export interface ErrorRecord { readonly name: string; readonly message: string; readonly stack?: string; } export interface PendingBossQuestion { readonly questionId: ResumableStateId; readonly resumeStateId: ResumableStateId; readonly sourceItem: SourceItemId; readonly asker: { readonly kind: 'role'; readonly roleId: RoleId }; readonly question: string; } /** Which terminal outcome ended the run; drives the machine output. */ export type TerminalOutcome = 'noFindings' | 'unreviewedFix' | 'unsettledFindings'; export interface EvaluateContext { /** The caller's input: the change to evaluate, the metrics that matter, and any thresholds and context. */ readonly callerInput?: string; /** The last commit Evaluator made, taken from the repository-effect receipt. */ readonly latestCommit?: string; readonly evaluatorOutput?: string; readonly reviewerOutput?: string; readonly overallVerdict?: string; readonly terminalOutcome?: TerminalOutcome; readonly lastError?: ErrorRecord; readonly pendingBossQuestion?: PendingBossQuestion; readonly bossReply?: string; } export type EvaluateEvent = | { readonly type: 'BOSS_INTENT'; readonly bossIntent: string } | { readonly type: 'BOSS_INTERRUPT'; readonly targetId: InterruptTargetId } | { readonly type: 'BOSS_REPLY'; readonly answer: string; readonly questionId?: string }; export interface EvaluateInput { /** Optional seed for the caller input; the entry event normally supplies it. */ readonly callerInput?: string; } export type EvaluateOutput = | { readonly status: 'noFindings'; readonly latestCommit: string; readonly overallVerdict: string; } | { readonly status: 'unreviewedFix'; readonly latestCommit: string; readonly overallVerdict: string; readonly evaluatorOutput: string; } | { readonly status: 'unsettledFindings'; readonly latestCommit: string; readonly overallVerdict: string; readonly evaluatorOutput: string; }; // --------------------------------------------------------------------------- // Player actor contract // --------------------------------------------------------------------------- export interface PlayerInput { readonly stateId: PlayerStateId; readonly role: RoleId; readonly sourceItem: SourceItemId; readonly prompt: string; readonly result: { readonly [guard: string]: string }; /** Relayed as ``. */ readonly callerInput?: string; /** Relayed as ``. */ readonly latestCommit?: string; /** Relayed as ``. */ readonly evaluatorOutput?: string; /** Relayed as ``. */ readonly reviewerOutput?: string; readonly pendingBossQuestion?: PendingBossQuestion; readonly bossReply?: string; } export interface NeedsBossReplyOutput { readonly guard: 'needsBossReply'; readonly question: string; } /** EVALUATE-1 (`evaluating`). */ export type EvaluatingOutput = | { readonly guard: 'committed'; readonly evaluatorOutput: string; readonly latestCommit: string; } | NeedsBossReplyOutput; /** EVALUATE-2, EVALUATE-3, EVALUATE-4 (every review round). */ export type ReviewOutput = | { readonly guard: 'hasFindings'; readonly reviewerOutput: string } | { readonly guard: 'noFindings'; readonly overallVerdict: string } | NeedsBossReplyOutput; /** EVALUATE-5 (`fixingRound1`). */ export type FixingRound1Output = | { readonly guard: 'fixed'; readonly evaluatorOutput: string; readonly latestCommit: string; } | { readonly guard: 'rejectedAll'; readonly evaluatorOutput: string } | NeedsBossReplyOutput; /** EVALUATE-6 (`fixingRound2`). */ export type FixingRound2Output = | { readonly guard: 'fixed'; readonly evaluatorOutput: string; readonly latestCommit: string; readonly overallVerdict: string; } | { readonly guard: 'rejectedAll'; readonly evaluatorOutput: string; readonly overallVerdict: string; } | NeedsBossReplyOutput; export type PlayerOutput = | EvaluatingOutput | ReviewOutput | FixingRound1Output | FixingRound2Output; // --------------------------------------------------------------------------- // Prompts (verbatim GEARS blockquotes) // --------------------------------------------------------------------------- const EVALUATE_1_PROMPT = [ 'Evaluate the change named in the request against every metric it names.', 'Read specs/ and the code to understand what the change does and what each metric means.', "Take each metric's threshold and direction (whether higher or lower is better) from the request, or from specs/ where the request gives none.", "If a metric's threshold is in neither, ask Boss for it before running anything.", '', 'Find the evaluation suite under evals/, and for each named metric that has no script there, write one.', "Each metric gets one runnable script that measures exactly what the metric claims and prints the metric's value.", 'Run every script against the change and against the baseline main branch.', 'Run the baseline from a separate worktree of main so the current branch is not disturbed, and remove that worktree afterwards.', 'For every run, record the exact commit it ran on, the command, and the printed value.', '', 'Write a report under docs/evals/ with a table holding one row per named metric and the columns: metric, threshold, baseline value, new value, pass or fail.', "Decide each pass or fail strictly from the metric's threshold, its direction, and the new value.", 'Below the table, state the overall verdict (pass if every metric passed, otherwise fail with the failing metrics listed) and the recorded commits and commands of every run.', '', 'Commit the suite and the report together in one new commit on the current branch; never amend or rewrite an existing commit.', 'Make the commit message say what was evaluated and the overall verdict.', 'Report the commit, the table, and the overall verdict.', '', '> Original request: ', ].join('\n'); const EVALUATE_2_PROMPT = [ 'A review begins of the evaluation committed for the request.', "Read the latest commit's message, the eval scripts under evals/, and the report under docs/evals/, and see Evaluator's report below.", '', 'Check that:', "- every metric the request names has a row in the report's table;", '- each eval script measures what its metric claims, on the right data, and prints that value;', '- the baseline was really run on main: the recorded main commit exists and its values come from running the scripts there, not from copying or estimating them;', "- each pass or fail follows from the metric's threshold, its direction, and the new value, and the overall verdict follows from the rows.", 'Re-run a script only where needed to settle a doubt about a recorded value.', '', 'Number the findings consistently across rounds, without duplication.', 'Flag only what makes a value, a verdict, or the table wrong, unsupported, or incomplete — not style.', 'For any rebuttal, accept or challenge it.', 'Do not edit files or commit; report findings only.', "If no findings remain, say so and state the report's overall verdict: pass, or fail with the failing metrics listed.", '', '> Original request: ', '> Latest commit: ', '> Evaluator output: ', ].join('\n'); const EVALUATE_3_PROMPT = [ 'The second and final review round begins, with particular attention to the latest fix commit.', "Read its message and see Evaluator's feedback below.", '', 'Check that:', "- every metric the request names has a row in the report's table;", '- each eval script measures what its metric claims, on the right data, and prints that value;', '- the baseline was really run on main: the recorded main commit exists and its values come from running the scripts there, not from copying or estimating them;', "- each pass or fail follows from the metric's threshold, its direction, and the new value, and the overall verdict follows from the rows.", 'Re-run a script only where needed to settle a doubt about a recorded value.', '', 'Number the findings consistently across rounds, without duplication.', 'Flag only what makes a value, a verdict, or the table wrong, unsupported, or incomplete — not style.', 'For any rebuttal, accept or challenge it.', 'Do not edit files or commit; report findings only.', "If no findings remain, say so and state the report's overall verdict: pass, or fail with the failing metrics listed.", '', '> Original request: ', '> Latest commit: ', '> Evaluator output: ', ].join('\n'); const EVALUATE_4_PROMPT = [ 'The second and final review round begins.', "No new commit was made because Evaluator rejected every finding; see Evaluator's feedback below.", '', 'Check that:', "- every metric the request names has a row in the report's table;", '- each eval script measures what its metric claims, on the right data, and prints that value;', '- the baseline was really run on main: the recorded main commit exists and its values come from running the scripts there, not from copying or estimating them;', "- each pass or fail follows from the metric's threshold, its direction, and the new value, and the overall verdict follows from the rows.", 'Re-run a script only where needed to settle a doubt about a recorded value.', '', 'Number the findings consistently across rounds, without duplication.', 'Flag only what makes a value, a verdict, or the table wrong, unsupported, or incomplete — not style.', 'For any rebuttal, accept or challenge it.', 'Do not edit files or commit; report findings only.', "If no findings remain, say so and state the report's overall verdict: pass, or fail with the failing metrics listed.", '', '> Original request: ', '> Evaluator output: ', ].join('\n'); const EVALUATE_5_PROMPT = [ 'For each numbered finding, accept or reject it.', 'If you accept a finding, fix its root cause in the eval scripts or the report; re-run, against both the change and main, only the scripts whose inputs changed, and bring the table, each pass or fail, and the overall verdict in line with the results.', 'If you reject a finding, give the reasoning and cite the script, the code, or the run output that supports it.', '', 'If you accept any finding, add one new commit with the fixes on the current branch; never amend or rewrite an existing commit.', 'If you reject every finding, change nothing and make no commit.', 'Report every disposition, every rebuttal, and the overall verdict the report now states.', '', '> Original request: ', '> Reviewer findings: ', ].join('\n'); const EVALUATE_6_PROMPT = [ 'For each numbered finding, accept or reject it.', 'If you accept a finding, fix its root cause in the eval scripts or the report; re-run, against both the change and main, only the scripts whose inputs changed, and bring the table, each pass or fail, and the overall verdict in line with the results.', 'If you reject a finding, give the reasoning and cite the script, the code, or the run output that supports it.', '', 'If you accept any finding, add one new commit with the fixes on the current branch; never amend or rewrite an existing commit.', 'If you reject every finding, change nothing and make no commit.', 'Report every disposition, every rebuttal, and the overall verdict the report now states.', '', '> Original request: ', '> Reviewer findings: ', ].join('\n'); // --------------------------------------------------------------------------- // Per-state result contracts (verbatim GEARS `Results:` bullets plus the // universal Boss-reply result) // --------------------------------------------------------------------------- const NEEDS_BOSS_REPLY = "The acting agent's prose surfaces a clarifying question for Boss that the agent cannot answer alone. Output shall include `question: `."; const EVALUATING_RESULT = { committed: 'Evaluator made one new commit holding the evaluation suite and the report. Output shall include `evaluatorOutput: ` and `latestCommit: `.', needsBossReply: NEEDS_BOSS_REPLY, } as const; const REVIEW_RESULT = { hasFindings: 'Reviewer raised or kept one or more numbered findings; a progress report, status update, or promise of a later result supports no outcome. Output shall include `reviewerOutput: `.', noFindings: "Reviewer affirmatively reported that no findings remain and stated the report's overall verdict; a progress report, status update, or promise of a later result supports no outcome. On this outcome `evaluate` ends and returns latestCommit, the last commit Evaluator made, and the overall verdict as overallVerdict. Output shall include `overallVerdict: `.", needsBossReply: NEEDS_BOSS_REPLY, } as const; const FIXING_ROUND_1_RESULT = { fixed: 'Evaluator accepted one or more findings and made one new fix commit. Output shall include `evaluatorOutput: ` and `latestCommit: `.', rejectedAll: 'Evaluator rejected every finding, changed nothing, and made no commit. Output shall include `evaluatorOutput: `.', needsBossReply: NEEDS_BOSS_REPLY, } as const; const FIXING_ROUND_2_RESULT = { fixed: "Evaluator accepted one or more findings and made one new fix commit. On this outcome `evaluate` ends and returns latestCommit, this unreviewed fix commit; the overall verdict the report now states, as overallVerdict; and Evaluator's dispositions of the last findings, as evaluatorOutput. Output shall include `evaluatorOutput: `, `latestCommit: `, and `overallVerdict: `.", rejectedAll: "Evaluator rejected every finding, changed nothing, and made no commit. On this outcome `evaluate` ends and returns latestCommit, the last commit Evaluator made; the overall verdict the report states, as overallVerdict; and Evaluator's rebuttals of the unsettled findings, as evaluatorOutput. Output shall include `evaluatorOutput: ` and `overallVerdict: `.", needsBossReply: NEEDS_BOSS_REPLY, } as const; // --------------------------------------------------------------------------- // Working-leaf table // --------------------------------------------------------------------------- /** Context fields relayed into prompts through placeholders. */ type RelayField = 'callerInput' | 'latestCommit' | 'evaluatorOutput' | 'reviewerOutput'; /** Context text fields a guard may require. */ type ContextTextField = RelayField | 'overallVerdict'; /** Actor-output fields copied into context on an accepted outcome. */ type RecordedField = 'evaluatorOutput' | 'latestCommit' | 'reviewerOutput' | 'overallVerdict'; /** Actor-output fields an outcome guard may require. */ type OutputField = RecordedField | 'question'; interface PlayerStateSpec { readonly role: RoleId; readonly sourceItem: SourceItemId; readonly prompt: string; readonly result: { readonly [guard: string]: string }; readonly relays: readonly RelayField[]; } const PLAYER_STATES = { evaluating: { role: 'evaluator', sourceItem: 'EVALUATE-1', prompt: EVALUATE_1_PROMPT, result: EVALUATING_RESULT, relays: ['callerInput'], }, reviewingRound1: { role: 'reviewer', sourceItem: 'EVALUATE-2', prompt: EVALUATE_2_PROMPT, result: REVIEW_RESULT, relays: ['callerInput', 'latestCommit', 'evaluatorOutput'], }, reviewingRound2AfterFix: { role: 'reviewer', sourceItem: 'EVALUATE-3', prompt: EVALUATE_3_PROMPT, result: REVIEW_RESULT, relays: ['callerInput', 'latestCommit', 'evaluatorOutput'], }, reviewingRound2AfterRejection: { role: 'reviewer', sourceItem: 'EVALUATE-4', prompt: EVALUATE_4_PROMPT, result: REVIEW_RESULT, relays: ['callerInput', 'evaluatorOutput'], }, fixingRound1: { role: 'evaluator', sourceItem: 'EVALUATE-5', prompt: EVALUATE_5_PROMPT, result: FIXING_ROUND_1_RESULT, relays: ['callerInput', 'reviewerOutput'], }, fixingRound2: { role: 'evaluator', sourceItem: 'EVALUATE-6', prompt: EVALUATE_6_PROMPT, result: FIXING_ROUND_2_RESULT, relays: ['callerInput', 'reviewerOutput'], }, } as const satisfies Record; /** * Context each interrupt target needs before it can run: its relayed values, * plus `latestCommit` wherever a reachable terminal outcome returns it. */ const INTERRUPT_REQUIREMENTS = { evaluating: ['callerInput'], reviewingRound1: ['callerInput', 'latestCommit', 'evaluatorOutput'], fixingRound1: ['callerInput', 'latestCommit', 'reviewerOutput'], reviewingRound2AfterFix: ['callerInput', 'latestCommit', 'evaluatorOutput'], reviewingRound2AfterRejection: ['callerInput', 'latestCommit', 'evaluatorOutput'], fixingRound2: ['callerInput', 'latestCommit', 'reviewerOutput'], } as const satisfies Record; function interruptRequirements(stateId: InterruptTargetId): readonly ContextTextField[] { return INTERRUPT_REQUIREMENTS[stateId]; } const WORKING_STATE_IDS = [ 'evaluating', 'reviewingRound1', 'fixingRound1', 'reviewingRound2AfterFix', 'reviewingRound2AfterRejection', 'fixingRound2', ] as const satisfies readonly PlayerStateId[]; // --------------------------------------------------------------------------- // Structural helpers // --------------------------------------------------------------------------- function isRecord(value: unknown): value is { readonly [key: string]: unknown } { return typeof value === 'object' && value !== null && !Array.isArray(value); } function isNonEmptyText(value: unknown): value is string { return typeof value === 'string' && value.trim().length > 0; } /** Narrows an unknown done event to its actor output record. */ function actorOutput(event: unknown): { readonly [key: string]: unknown } | undefined { if (!isRecord(event)) return undefined; const output = event['output']; return isRecord(output) ? output : undefined; } /** Returns the output when it selects `guard` and carries every required field. */ function acceptedOutput( event: unknown, guard: string, fields: readonly OutputField[], ): { readonly [key: string]: unknown } | undefined { const output = actorOutput(event); if (output === undefined || output['guard'] !== guard) return undefined; return fields.every((field) => isNonEmptyText(output[field])) ? output : undefined; } /** Narrows an unknown error event to its rejection value. */ function actorError(event: unknown): unknown { return isRecord(event) ? event['error'] : undefined; } function normalizeError(error: unknown): ErrorRecord { if (error instanceof Error) { return typeof error.stack === 'string' ? { name: error.name, message: error.message, stack: error.stack } : { name: error.name, message: error.message }; } if (typeof error === 'string') return { name: 'Error', message: error }; return { name: 'Error', message: 'The invoked actor failed without an Error value.' }; } function playerInput(stateId: PlayerStateId, context: EvaluateContext): PlayerInput { const spec: PlayerStateSpec = PLAYER_STATES[stateId]; const relayed: { -readonly [K in RelayField]?: string } = {}; for (const field of spec.relays) { const value = context[field]; if (value !== undefined) relayed[field] = value; } const pending = context.pendingBossQuestion; const resuming = pending !== undefined && pending.resumeStateId === stateId; return { stateId, role: spec.role, sourceItem: spec.sourceItem, prompt: spec.prompt, result: spec.result, ...relayed, ...(resuming ? { pendingBossQuestion: pending } : {}), ...(resuming && context.bossReply !== undefined ? { bossReply: context.bossReply } : {}), }; } function terminalOutput(context: EvaluateContext): EvaluateOutput { const { terminalOutcome, latestCommit, overallVerdict, evaluatorOutput } = context; if (!isNonEmptyText(latestCommit) || !isNonEmptyText(overallVerdict)) { throw new Error('evaluate reached a final state without latestCommit and overallVerdict'); } if (terminalOutcome === 'noFindings') { return { status: 'noFindings', latestCommit, overallVerdict }; } if (!isNonEmptyText(evaluatorOutput)) { throw new Error('evaluate reached a final state without evaluatorOutput'); } if (terminalOutcome === 'unreviewedFix') { return { status: 'unreviewedFix', latestCommit, overallVerdict, evaluatorOutput }; } if (terminalOutcome === 'unsettledFindings') { return { status: 'unsettledFindings', latestCommit, overallVerdict, evaluatorOutput }; } throw new Error('evaluate reached a final state without a terminal outcome'); } // --------------------------------------------------------------------------- // Generated transition helpers // --------------------------------------------------------------------------- /** One guarded `BOSS_REPLY` arm per resumable working leaf, then the malformed-reply fallback. */ function resumableStates(ids: T) { return [ ...ids.map((id) => ({ guard: { type: 'resumesAt' as const, params: { stateId: id } }, target: `#${id}` as const, reenter: true as const, actions: ['rememberBossReply' as const], })), { target: '#failed' as const, actions: ['rememberInvalidBossReply' as const] }, ]; } /** One guarded root `BOSS_INTERRUPT` arm per jumpable working leaf. */ function bossInterrupts(ids: T) { return ids.map((id) => ({ guard: { type: 'interruptsTo' as const, params: { stateId: id } }, target: `#${id}` as const, reenter: true as const, actions: ['clearBossReplyContext' as const], })); } // --------------------------------------------------------------------------- // Typed action/guard parameters // --------------------------------------------------------------------------- interface AcceptedOutcomeParams { readonly source: string; readonly target: string; readonly acceptedOutcome: string; } interface OutcomeGuardParams { readonly guard: string; readonly fields: readonly OutputField[]; readonly contextFields?: readonly ContextTextField[]; } interface RecordOutcomeParams { readonly fields: readonly RecordedField[]; readonly terminalOutcome?: TerminalOutcome; } interface StateParams { readonly stateId: PlayerStateId; } // --------------------------------------------------------------------------- // State descriptions // --------------------------------------------------------------------------- const DESCRIPTIONS = { ready: 'Idle hub waiting for the caller to give a change to evaluate and the metrics that matter.', evaluating: 'Evaluator evaluates the change against every named metric and commits the suite and the report in one new commit.', reviewingRound1: 'Reviewer runs the first review round of the committed evaluation.', fixingRound1: "Evaluator accepts or rejects each finding of the first review round, committing any fixes; the second review round follows.", reviewingRound2AfterFix: "Reviewer runs the second and final review round, with particular attention to Evaluator's latest fix commit.", reviewingRound2AfterRejection: 'Reviewer runs the second and final review round after Evaluator rejected every first-round finding without a commit.', fixingRound2: 'Evaluator accepts or rejects each finding of the second review round; evaluate then ends without a third review.', awaitBossReply: "Waiting for Boss to answer the acting agent's question.", failed: 'Recoverable failure; the error is retained in lastError and the caller can restart the evaluation.', done: 'A review round ended with no findings; evaluate returned the last commit Evaluator made and the overall verdict.', doneWithUnreviewedFix: "After the second review round Evaluator made an unreviewed fix commit; evaluate returned that commit, the overall verdict the report now states, and Evaluator's dispositions of the last findings.", doneWithUnsettledFindings: "After the second review round Evaluator rejected every finding without a commit; evaluate returned the last commit Evaluator made, the overall verdict the report states, and Evaluator's rebuttals of the unsettled findings.", } as const; // --------------------------------------------------------------------------- // Machine // --------------------------------------------------------------------------- export const evaluateMachine = setup({ types: { context: {} as EvaluateContext, events: {} as EvaluateEvent, input: {} as EvaluateInput, output: {} as EvaluateOutput, }, actors: { player: fromPromise(async () => { throw new Error('player actor must be provided by the runner'); }), }, guards: { hasBossIntent: ({ event }) => event.type === 'BOSS_INTENT' && isNonEmptyText(event.bossIntent), acceptsOutcome: ({ context, event }, params: OutcomeGuardParams) => acceptedOutput(event, params.guard, params.fields) !== undefined && (params.contextFields ?? []).every((field) => isNonEmptyText(context[field])), resumesAt: ({ context, event }, params: StateParams) => { if (event.type !== 'BOSS_REPLY') return false; const pending = context.pendingBossQuestion; return ( pending !== undefined && pending.resumeStateId === params.stateId && isNonEmptyText(event.answer) && (event.questionId === undefined || event.questionId === pending.questionId) ); }, interruptsTo: ({ context, event }, params: StateParams) => event.type === 'BOSS_INTERRUPT' && event.targetId === params.stateId && interruptRequirements(params.stateId).every((field) => isNonEmptyText(context[field])), }, actions: { 'playbook.acceptedOutcome': (_, params: AcceptedOutcomeParams) => { void params; }, startRun: assign(({ event }) => event.type === 'BOSS_INTENT' ? { callerInput: event.bossIntent, latestCommit: undefined, evaluatorOutput: undefined, reviewerOutput: undefined, overallVerdict: undefined, terminalOutcome: undefined, lastError: undefined, pendingBossQuestion: undefined, bossReply: undefined, } : {}, ), recordOutcome: assign(({ event }, params: RecordOutcomeParams) => { const output = actorOutput(event); if (output === undefined) return {}; const update: { -readonly [K in RecordedField]?: string } & { terminalOutcome?: TerminalOutcome; } = {}; for (const field of params.fields) { const value = output[field]; if (typeof value === 'string') update[field] = value; } if (params.terminalOutcome !== undefined) update.terminalOutcome = params.terminalOutcome; return update; }), setPendingBossQuestion: assign(({ event }, params: StateParams) => { const question = acceptedOutput(event, 'needsBossReply', ['question'])?.['question']; if (!isNonEmptyText(question)) return {}; const spec: PlayerStateSpec = PLAYER_STATES[params.stateId]; const pending: PendingBossQuestion = { questionId: params.stateId, resumeStateId: params.stateId, sourceItem: spec.sourceItem, asker: { kind: 'role', roleId: spec.role }, question, }; return { pendingBossQuestion: pending, bossReply: undefined }; }), rememberBossReply: assign(({ event }) => event.type === 'BOSS_REPLY' ? { bossReply: event.answer } : {}, ), clearBossReplyContext: assign({ pendingBossQuestion: undefined, bossReply: undefined, }), rememberActorError: assign(({ event }) => ({ lastError: normalizeError(actorError(event)), })), rememberMalformedOutput: assign(({ event }) => { const guard = actorOutput(event)?.['guard']; const lastError: ErrorRecord = { name: 'MalformedActorOutput', message: typeof guard === 'string' ? `The acting agent's output for guard "${guard}" matched no declared result with its required fields.` : 'The acting agent returned no output with a declared result guard.', }; return { lastError }; }), rememberInvalidBossReply: assign({ lastError: (): ErrorRecord => ({ name: 'InvalidBossReply', message: 'BOSS_REPLY carried an empty answer or named no pending question.', }), pendingBossQuestion: undefined, bossReply: undefined, }), }, }).createMachine({ id: 'evaluate', description: 'Verification stage that establishes whether a change clears its evaluation metrics, with at most two review rounds.', initial: 'ready', context: ({ input }) => input !== undefined && input.callerInput !== undefined ? { callerInput: input.callerInput } : {}, output: ({ context }) => terminalOutput(context), on: { BOSS_INTERRUPT: bossInterrupts(WORKING_STATE_IDS), }, states: { ready: { id: 'ready', description: DESCRIPTIONS.ready, tags: ['playbook.parked'], meta: { playbook: { stateId: 'ready', description: DESCRIPTIONS.ready } }, on: { BOSS_INTENT: { guard: 'hasBossIntent', target: 'evaluating', actions: 'startRun' }, }, }, // EVALUATE-1 evaluating: { id: 'evaluating', description: DESCRIPTIONS.evaluating, tags: ['playbook.busy'], meta: { playbook: { stateId: 'evaluating', description: DESCRIPTIONS.evaluating, role: 'evaluator' }, }, invoke: { src: 'player', input: ({ context }) => playerInput('evaluating', context), onDone: [ { guard: { type: 'acceptsOutcome', params: { guard: 'committed', fields: ['evaluatorOutput', 'latestCommit'] }, }, target: 'reviewingRound1', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'evaluating', target: 'reviewingRound1', acceptedOutcome: 'committed' }, }, { type: 'recordOutcome', params: { fields: ['evaluatorOutput', 'latestCommit'] } }, 'clearBossReplyContext', ], }, { guard: { type: 'acceptsOutcome', params: { guard: 'needsBossReply', fields: ['question'] } }, target: 'awaitBossReply', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'evaluating', target: 'awaitBossReply', acceptedOutcome: 'needsBossReply' }, }, { type: 'setPendingBossQuestion', params: { stateId: 'evaluating' } }, ], }, { target: 'failed', actions: ['rememberMalformedOutput', 'clearBossReplyContext'] }, ], onError: { target: 'failed', actions: ['rememberActorError', 'clearBossReplyContext'] }, }, }, // EVALUATE-2 reviewingRound1: { id: 'reviewingRound1', description: DESCRIPTIONS.reviewingRound1, tags: ['playbook.busy'], meta: { playbook: { stateId: 'reviewingRound1', description: DESCRIPTIONS.reviewingRound1, role: 'reviewer' }, }, invoke: { src: 'player', input: ({ context }) => playerInput('reviewingRound1', context), onDone: [ { guard: { type: 'acceptsOutcome', params: { guard: 'hasFindings', fields: ['reviewerOutput'] } }, target: 'fixingRound1', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'reviewingRound1', target: 'fixingRound1', acceptedOutcome: 'hasFindings' }, }, { type: 'recordOutcome', params: { fields: ['reviewerOutput'] } }, 'clearBossReplyContext', ], }, { guard: { type: 'acceptsOutcome', params: { guard: 'noFindings', fields: ['overallVerdict'], contextFields: ['latestCommit'] }, }, target: 'done', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'reviewingRound1', target: 'done', acceptedOutcome: 'noFindings' }, }, { type: 'recordOutcome', params: { fields: ['overallVerdict'], terminalOutcome: 'noFindings' }, }, 'clearBossReplyContext', ], }, { guard: { type: 'acceptsOutcome', params: { guard: 'needsBossReply', fields: ['question'] } }, target: 'awaitBossReply', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'reviewingRound1', target: 'awaitBossReply', acceptedOutcome: 'needsBossReply' }, }, { type: 'setPendingBossQuestion', params: { stateId: 'reviewingRound1' } }, ], }, { target: 'failed', actions: ['rememberMalformedOutput', 'clearBossReplyContext'] }, ], onError: { target: 'failed', actions: ['rememberActorError', 'clearBossReplyContext'] }, }, }, // EVALUATE-5 fixingRound1: { id: 'fixingRound1', description: DESCRIPTIONS.fixingRound1, tags: ['playbook.busy'], meta: { playbook: { stateId: 'fixingRound1', description: DESCRIPTIONS.fixingRound1, role: 'evaluator' }, }, invoke: { src: 'player', input: ({ context }) => playerInput('fixingRound1', context), onDone: [ { guard: { type: 'acceptsOutcome', params: { guard: 'fixed', fields: ['evaluatorOutput', 'latestCommit'] }, }, target: 'reviewingRound2AfterFix', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'fixingRound1', target: 'reviewingRound2AfterFix', acceptedOutcome: 'fixed' }, }, { type: 'recordOutcome', params: { fields: ['evaluatorOutput', 'latestCommit'] } }, 'clearBossReplyContext', ], }, { guard: { type: 'acceptsOutcome', params: { guard: 'rejectedAll', fields: ['evaluatorOutput'] } }, target: 'reviewingRound2AfterRejection', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'fixingRound1', target: 'reviewingRound2AfterRejection', acceptedOutcome: 'rejectedAll', }, }, { type: 'recordOutcome', params: { fields: ['evaluatorOutput'] } }, 'clearBossReplyContext', ], }, { guard: { type: 'acceptsOutcome', params: { guard: 'needsBossReply', fields: ['question'] } }, target: 'awaitBossReply', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'fixingRound1', target: 'awaitBossReply', acceptedOutcome: 'needsBossReply' }, }, { type: 'setPendingBossQuestion', params: { stateId: 'fixingRound1' } }, ], }, { target: 'failed', actions: ['rememberMalformedOutput', 'clearBossReplyContext'] }, ], onError: { target: 'failed', actions: ['rememberActorError', 'clearBossReplyContext'] }, }, }, // EVALUATE-3 reviewingRound2AfterFix: { id: 'reviewingRound2AfterFix', description: DESCRIPTIONS.reviewingRound2AfterFix, tags: ['playbook.busy'], meta: { playbook: { stateId: 'reviewingRound2AfterFix', description: DESCRIPTIONS.reviewingRound2AfterFix, role: 'reviewer', }, }, invoke: { src: 'player', input: ({ context }) => playerInput('reviewingRound2AfterFix', context), onDone: [ { guard: { type: 'acceptsOutcome', params: { guard: 'hasFindings', fields: ['reviewerOutput'] } }, target: 'fixingRound2', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'reviewingRound2AfterFix', target: 'fixingRound2', acceptedOutcome: 'hasFindings' }, }, { type: 'recordOutcome', params: { fields: ['reviewerOutput'] } }, 'clearBossReplyContext', ], }, { guard: { type: 'acceptsOutcome', params: { guard: 'noFindings', fields: ['overallVerdict'], contextFields: ['latestCommit'] }, }, target: 'done', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'reviewingRound2AfterFix', target: 'done', acceptedOutcome: 'noFindings' }, }, { type: 'recordOutcome', params: { fields: ['overallVerdict'], terminalOutcome: 'noFindings' }, }, 'clearBossReplyContext', ], }, { guard: { type: 'acceptsOutcome', params: { guard: 'needsBossReply', fields: ['question'] } }, target: 'awaitBossReply', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'reviewingRound2AfterFix', target: 'awaitBossReply', acceptedOutcome: 'needsBossReply', }, }, { type: 'setPendingBossQuestion', params: { stateId: 'reviewingRound2AfterFix' } }, ], }, { target: 'failed', actions: ['rememberMalformedOutput', 'clearBossReplyContext'] }, ], onError: { target: 'failed', actions: ['rememberActorError', 'clearBossReplyContext'] }, }, }, // EVALUATE-4 reviewingRound2AfterRejection: { id: 'reviewingRound2AfterRejection', description: DESCRIPTIONS.reviewingRound2AfterRejection, tags: ['playbook.busy'], meta: { playbook: { stateId: 'reviewingRound2AfterRejection', description: DESCRIPTIONS.reviewingRound2AfterRejection, role: 'reviewer', }, }, invoke: { src: 'player', input: ({ context }) => playerInput('reviewingRound2AfterRejection', context), onDone: [ { guard: { type: 'acceptsOutcome', params: { guard: 'hasFindings', fields: ['reviewerOutput'] } }, target: 'fixingRound2', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'reviewingRound2AfterRejection', target: 'fixingRound2', acceptedOutcome: 'hasFindings', }, }, { type: 'recordOutcome', params: { fields: ['reviewerOutput'] } }, 'clearBossReplyContext', ], }, { guard: { type: 'acceptsOutcome', params: { guard: 'noFindings', fields: ['overallVerdict'], contextFields: ['latestCommit'] }, }, target: 'done', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'reviewingRound2AfterRejection', target: 'done', acceptedOutcome: 'noFindings' }, }, { type: 'recordOutcome', params: { fields: ['overallVerdict'], terminalOutcome: 'noFindings' }, }, 'clearBossReplyContext', ], }, { guard: { type: 'acceptsOutcome', params: { guard: 'needsBossReply', fields: ['question'] } }, target: 'awaitBossReply', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'reviewingRound2AfterRejection', target: 'awaitBossReply', acceptedOutcome: 'needsBossReply', }, }, { type: 'setPendingBossQuestion', params: { stateId: 'reviewingRound2AfterRejection' } }, ], }, { target: 'failed', actions: ['rememberMalformedOutput', 'clearBossReplyContext'] }, ], onError: { target: 'failed', actions: ['rememberActorError', 'clearBossReplyContext'] }, }, }, // EVALUATE-6 fixingRound2: { id: 'fixingRound2', description: DESCRIPTIONS.fixingRound2, tags: ['playbook.busy'], meta: { playbook: { stateId: 'fixingRound2', description: DESCRIPTIONS.fixingRound2, role: 'evaluator' }, }, invoke: { src: 'player', input: ({ context }) => playerInput('fixingRound2', context), onDone: [ { guard: { type: 'acceptsOutcome', params: { guard: 'fixed', fields: ['evaluatorOutput', 'latestCommit', 'overallVerdict'] }, }, target: 'doneWithUnreviewedFix', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'fixingRound2', target: 'doneWithUnreviewedFix', acceptedOutcome: 'fixed' }, }, { type: 'recordOutcome', params: { fields: ['evaluatorOutput', 'latestCommit', 'overallVerdict'], terminalOutcome: 'unreviewedFix', }, }, 'clearBossReplyContext', ], }, { guard: { type: 'acceptsOutcome', params: { guard: 'rejectedAll', fields: ['evaluatorOutput', 'overallVerdict'], contextFields: ['latestCommit'], }, }, target: 'doneWithUnsettledFindings', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'fixingRound2', target: 'doneWithUnsettledFindings', acceptedOutcome: 'rejectedAll', }, }, { type: 'recordOutcome', params: { fields: ['evaluatorOutput', 'overallVerdict'], terminalOutcome: 'unsettledFindings', }, }, 'clearBossReplyContext', ], }, { guard: { type: 'acceptsOutcome', params: { guard: 'needsBossReply', fields: ['question'] } }, target: 'awaitBossReply', actions: [ { type: 'playbook.acceptedOutcome', params: { source: 'fixingRound2', target: 'awaitBossReply', acceptedOutcome: 'needsBossReply' }, }, { type: 'setPendingBossQuestion', params: { stateId: 'fixingRound2' } }, ], }, { target: 'failed', actions: ['rememberMalformedOutput', 'clearBossReplyContext'] }, ], onError: { target: 'failed', actions: ['rememberActorError', 'clearBossReplyContext'] }, }, }, awaitBossReply: { id: 'awaitBossReply', description: DESCRIPTIONS.awaitBossReply, tags: ['playbook.parked'], meta: { playbook: { stateId: 'awaitBossReply', description: DESCRIPTIONS.awaitBossReply } }, on: { BOSS_REPLY: resumableStates(WORKING_STATE_IDS), BOSS_INTENT: { guard: 'hasBossIntent', target: 'evaluating', actions: 'startRun' }, }, }, failed: { id: 'failed', description: DESCRIPTIONS.failed, tags: ['playbook.parked'], meta: { playbook: { stateId: 'failed', description: DESCRIPTIONS.failed } }, on: { BOSS_INTENT: { guard: 'hasBossIntent', target: 'evaluating', actions: 'startRun' }, }, }, done: { id: 'done', type: 'final', description: DESCRIPTIONS.done, meta: { playbook: { stateId: 'done', description: DESCRIPTIONS.done, terminal: 'success' } }, }, doneWithUnreviewedFix: { id: 'doneWithUnreviewedFix', type: 'final', description: DESCRIPTIONS.doneWithUnreviewedFix, meta: { playbook: { stateId: 'doneWithUnreviewedFix', description: DESCRIPTIONS.doneWithUnreviewedFix, terminal: 'success', }, }, }, doneWithUnsettledFindings: { id: 'doneWithUnsettledFindings', type: 'final', description: DESCRIPTIONS.doneWithUnsettledFindings, meta: { playbook: { stateId: 'doneWithUnsettledFindings', description: DESCRIPTIONS.doneWithUnsettledFindings, terminal: 'success', }, }, }, }, });