/** * Process-matrix runtime composition: the pieces main.ts wires together for durable, * restart-surviving master/worker process supervision. * * Contract: * - A WORKER is any process launched with a known parent (`PI_PARENT_PID` -- set by the * `--parent-pid` CLI flag, or directly by a launcher such as tmux dispatch). It self-registers * its OWN entry (the single writer of that entry during normal operation) and watches its * parent's liveness. On parent death it winds down GRACEFULLY -- never silently -- leaving a * `resumable` payload, then exits on its own after a bounded grace window (during which it may * instead be adopted by a new parent). "No new turns" after that point is automatic: a dead * parent injects no further follow-ups, so the worker simply runs out of work to do. * - A MASTER is everything else (no known parent). On startup it scans the matrix for orphaned * workers (workers whose recorded parent is dead). Workers owned by this exact resumed session * are restored automatically; foreign workers remain owner-gated interactively and report-only * headlessly. * * Sanctioned exceptions to "a worker's entry is written only by that worker": an exact resumed * parent may restore its recorded ownership, and an owner may explicitly approve adoption or * cooperative cleanup of a foreign orphan. The worker later confirms/applies the directive via * `pollWorkerDirective` and re-writes its own entry -- see `docs/process-matrix.md`. Outside these * identity/approval-fenced handshakes, a master NEVER writes another session's entry, and nothing * here ever kills a process directly. */ import type { AgentIdentityContract } from "../orchestration/contracts.ts"; import type { ResolvedProcessMatrixSettings } from "../settings-manager.ts"; import type { ResumablePayload } from "./codes.ts"; import { listEntries, readEntry, removeEntryIfUnchanged, writeEntry, writeEntryIfUnchanged, writeEntryIfUnchangedSync } from "./store.ts"; export { getOrchestrationAgentId, getParentPid, getParentSessionId, getProcessTaskRef, PI_ORCHESTRATION_AGENT_ID_ENV, PI_PARENT_PID_ENV, PI_PARENT_SESSION_ENV, PI_TASK_REF_ENV, } from "../process-identity.ts"; /** Storage boundary for the process-matrix coordinator. Runtime state transitions depend on this * port, while the filesystem adapter remains the single production implementation. */ export interface ProcessMatrixStorePort { listEntries: typeof listEntries; readEntry: typeof readEntry; removeEntryIfUnchanged: typeof removeEntryIfUnchanged; writeEntry: typeof writeEntry; writeEntryIfUnchanged: typeof writeEntryIfUnchanged; writeEntryIfUnchangedSync: typeof writeEntryIfUnchangedSync; } export declare const localProcessMatrixStore: ProcessMatrixStorePort; export interface ProcessMatrixRuntimeConfig { agentDir: string; /** Canonical logical identity for this process and any exact-session resume. */ agent: AgentIdentityContract; /** Whether an interactive UI is available to ask the owner (see `promptConfirm`). */ hasUI: boolean; settings: ResolvedProcessMatrixSettings; isProcessAlive: (pid: number) => boolean; now?: () => number; /** Structural notice injection into the running session (host `sendCustomMessage` seam). */ notify: (text: string) => void | Promise; /** Diagnostics sink (never throws into the session). */ onDiagnostic?: (message: string) => void; /** The ask seam: resolves false on decline AND on any non-interactive/non-TTY caller. */ promptConfirm: (message: string) => Promise; /** Cooperative self-exit -- called by a worker once wound down (grace expiry or a * master-granted cleanup directive). Never called for the master's own lifecycle. */ requestExit: () => Promise; /** Stable goal/task identity. Automatic recovery requires an exact match. */ taskRef?: string; taskSummary?: string; /** False for terminal/blocked owner state; recovery then remains explicit-owner gated. */ allowAutomaticRecovery?: boolean; /** Starts a replacement OS process for a dead resumable worker. Completion is an event-driven * terminal signal; worker product remains in its persisted session/artifacts. */ resumeWorker?: (payload: ResumablePayload) => Promise; /** Injectable only at the storage boundary; defaults to the atomic local filesystem adapter. */ store?: ProcessMatrixStorePort; } export type ResumeWorkerLaunchOutcome = { started: true; /** OS identity of this specific replacement process, used to fence its terminal handoff. */ pid: number; completion: Promise<{ code: number | null; signal: NodeJS.Signals | null; }>; } | { started: false; reason: string; }; export interface ProcessMatrixRuntimeHandle { stop(): Promise | void; /** Resolve after every watcher task that is active at the call boundary has settled. */ waitForIdle(): Promise; } export declare const PROCESS_MATRIX_RESUMABLE_RETENTION_MS: number; /** * Start the per-session process-matrix runtime. No-op when disabled (byte-identical to not * calling this at all). Never throws: a broken store must surface as a diagnostic, not a startup * crash. */ export declare function startProcessMatrixRuntime(config: ProcessMatrixRuntimeConfig): Promise; //# sourceMappingURL=runtime.d.ts.map