/** * Internalization State Machine (PRI-62) * * Pure decision functions for the Internalization Engine state machine. * These functions return structured proposals — they do NOT mutate store state, * do NOT call PDRuntimeAdapter, and do NOT execute createTask. * * Orchestrator (future PRI-62 scope) is responsible for consuming these * decisions and invoking RuntimeStateManager to apply transitions. * * Key design: * - Guard functions (task-guards.ts) validate individual conditions * - State machine functions compose guards into actionable decisions * - All functions are pure: same inputs → same outputs, no side effects * * @see docs/adr/0003-peer-agent-state-machine-orchestration.md */ import type { PDTaskStatus, TaskRecord } from '../task-status.js'; import type { PITaskRecord, PeerRunnerKind, RunnerKind, InternalizationChannel, PipelineTopologyMode, PIArtifact, ArtifactRef } from './peer-runner-contracts.js'; /** * Decision when evaluating if a task is ready to execute. * * proceed: All conditions met — task can be leased and executed * blocked: Dependencies not yet satisfied — wait for them * dependency_failed: At least one dependency has failed — escalate policy needed */ export type DependencyGateDecision = 'proceed' | 'blocked' | 'dependency_failed' | 'retry_wait_pending'; export interface DependencyGateResult { decision: DependencyGateDecision; /** Whether the task is ready to execute (same as decision === 'proceed') */ ready: boolean; /** Task IDs that are blocking execution (status != succeeded) */ blockedBy: string[]; /** Task IDs that have failed — for escalation policy decision */ failedDependencies: string[]; /** For retry_wait_pending: ISO timestamp when the task can be retried */ retryAfter?: string; } export interface TransitionValidation { valid: boolean; /** Human-readable reason if invalid */ reason?: string; } /** * Action to take when an artifact has been rejected. * * create_corrective_task: Re-run the same or corrective runner kind * escalate: Human review needed */ export type RejectionFeedbackAction = 'create_corrective_task' | 'escalate'; /** * Discriminated union for artifact rejection feedback. * * create_corrective_task: correctiveTaskKind is required * escalate: correctiveTaskKind is absent */ export type RejectionFeedbackResult = { action: 'create_corrective_task'; correctiveTaskKind: PeerRunnerKind; rejectedArtifactId: string; sourceTaskId: string; sourceTaskKind: PeerRunnerKind; rejectionReason?: string; } | { action: 'escalate'; rejectedArtifactId: string; sourceTaskId: string; sourceTaskKind: PeerRunnerKind; rejectionReason?: string; }; export interface NextTaskProposal { taskKind: RunnerKind; parentTaskId: string; dependencyTaskIds: string[]; inputArtifactRefs: ArtifactRef[]; channel: InternalizationChannel; /** PRI-720: inherited topology mode; ABSENT = legacy (pre-PRI-720) full-chain record. */ pipelineMode?: PipelineTopologyMode; correlationId?: string; } export type GraphErrorType = 'cycle' | 'disallowed_edge' | 'missing_dependency'; export interface GraphValidationError { type: GraphErrorType; message: string; taskId?: string; fromKind?: string; toKind?: string; } export interface GraphValidationResult { valid: boolean; errors: GraphValidationError[]; } /** * Validates whether a task is ready to be leased and executed. * * Combines: * 1. canAcquireLease — task status must be pending or retry_wait * 2. areDependenciesMet — all dependencyTaskIds must be succeeded * * Note: dependency failure (dependency_failed) does NOT automatically fail * the dependent task. The escalation policy (PRI-62 follow-up) decides * how to handle dependency failures. */ export declare function validateInternalizationTaskReady(task: PITaskRecord, dependencies: readonly TaskRecord[], nowMs?: number): DependencyGateResult; /** * Validates whether a task status transition is permitted. * * Uses canTransitionTo internally and adds human-readable reasons * for invalid transitions. */ export declare function validateTaskTransition(task: PITaskRecord, newStatus: PDTaskStatus): TransitionValidation; /** * Decides what action to take when an artifact has been rejected. * * Per ADR-0003 Section 3.7 rejection feedback loop: * - Artifact rejected ≠ task failed (they are separate concerns) * - Rejected artifacts can generate corrective task proposals * - Scribe/Artificer rejections → corrective task (re-run) * - Other runners → escalate for human review * * This function returns a proposal; the Orchestrator decides whether * to act on it. */ export declare function decideArtifactRejectionFeedback(artifact: PIArtifact, task: PITaskRecord): RejectionFeedbackResult; /** * Proposes the next task in the pipeline after a task succeeds. * * Uses the channel-aware job graph (getAllowedSuccessors, PRI-720) to * determine valid next steps: * - code_tool_hook/skill or full_chain mode: * dreamer → philosopher → scribe → artificer → evaluator → rollout_reviewer * - prompt/defer_archive (standard mode): * dreamer → philosopher → scribe → rollout_reviewer * * rollout_reviewer is the terminal peer runner; the trainer/model_training * surface was removed in PRI-449 (MVP-Gone). * * Requires currentTask.status === 'succeeded' — non-terminal tasks * must not generate successor proposals (prevents pipeline乱序). * * Resolves successors through the channel-aware job graph (PRI-720): the * task's channel + pipelineMode select the legal edge set (see * resolveChannelEdges). * * Returns null if the task is not succeeded or no channel-valid * successors exist. */ export declare function createNextTaskProposal(currentTask: PITaskRecord, _artifacts: PIArtifact[], channel?: InternalizationChannel): NextTaskProposal | null; /** * Validates an entire task graph for structural correctness. * * Checks: * 1. No cycles — uses isAcyclic() on extracted edges * 2. All edges are in ALLOWED_EDGES (via validateEdge) * 3. All dependencyTaskIds reference existing tasks (fail closed) * * Note: this does NOT check dependency status (use validateInternalizationTaskReady * for that) — only structural validity. */ export declare function validateInternalizationGraph(tasks: PITaskRecord[]): GraphValidationResult; //# sourceMappingURL=internalization-state-machine.d.ts.map