import type { EphemeralSubagentExecutor } from "@henryqw/pi-subagent"; import { type AllocationKind, type CheckBatchEvidence, type CheckCommand, type ChangesetTaskRequest, type CleanupKind, type ExecuteRequest, type HostAllocationIntent, type HostAllocationPlan, type ModelClass, type RebaseTransition, type ReviewEvidence, type ResumeRequest, type RunState, type TaskAttempt, type TaskRequest, type TaskState, type WorktreeAllocationIntent, type WorktreeAllocationPlan, type WorkspaceIdentity } from "./schema.ts"; import { FileRunStore } from "./store.ts"; export declare const CLEANUP_SAFETY_BUDGET_MS = 30000; export declare const TERMINATION_SAFETY_BUDGET_MS = 15000; export declare const STATUS_INSPECTION_BUDGET_MS = 5000; export interface OperationContext { readonly signal: AbortSignal; readonly timeoutMs: number; readonly deadline: number; } export interface CommandResult extends CheckCommand { code: number; killed?: boolean; stdout: string; stderr: string; } type AllocationFailureResult = { kind: Kind; outcome: "absent"; failure: string; } | { kind: Kind; outcome: "unknown"; failure: string; possibleResources?: string[]; }; export type WorktreeAllocationResult = { kind: "worktree"; outcome: "owned"; } | AllocationFailureResult<"worktree">; export type WorkspaceAllocationResult = { kind: "workspace"; outcome: "owned"; workspaceId: string; rootTabId: string; rootPaneId: string; } | AllocationFailureResult<"workspace">; export type WorkerTabAllocationResult = { kind: "worker_tab"; outcome: "owned"; tabId: string; paneId: string; } | AllocationFailureResult<"worker_tab">; export type AgentAllocationResult = { kind: "agent"; outcome: "owned"; } | AllocationFailureResult<"agent">; export type HostAllocationResult = WorkspaceAllocationResult | WorkerTabAllocationResult | AgentAllocationResult; export type AllocationResult = WorktreeAllocationResult | HostAllocationResult; export type AllocationReconciliation = { kind: Kind; outcome: "absent"; } | { kind: Kind; outcome: "possible"; failure: string; possibleResources?: string[]; }; export type WorkerResult = { outcome: "candidate"; candidate: WorkspaceIdentity; diagnostic?: string; } | { outcome: "blocked" | "not_prompted"; diagnostic: string; } | { outcome: "unknown" | "interrupted"; diagnostic: string; }; export interface CheckRunResult { results: CommandResult[]; identityAfter: WorkspaceIdentity; } export interface ReviewResult { verdict: string; identityAfter: WorkspaceIdentity; } export type RebaseResult = { outcome: "ready" | "drift"; base: WorkspaceIdentity; candidate: WorkspaceIdentity; failure?: string; } | { outcome: "blocked"; failure: string; }; export type RebaseReconciliation = { outcome: "not_started"; } | { outcome: "rebased"; candidate: WorkspaceIdentity; } | { outcome: "unknown"; failure: string; }; export type IntegrationResult = { outcome: "integrated"; main: WorkspaceIdentity; } | { outcome: "failed" | "drift" | "unknown"; failure: string; }; /** A just-in-time launch whose argv contains only an ephemeral Role prompt path. */ export interface VerifiedLaunch { readonly role: string; readonly modelClass: ModelClass; readonly model: string; readonly thinkingLevel: string; readonly args: readonly string[]; readonly env: Readonly>; readonly tools: readonly string[]; } export interface TransientLaunchHandle { readonly launch: Launch; cleanup(): Promise; } /** Run one launch action and surface cleanup failure, including after action failure or abort. */ export declare function withTransientLaunch(handle: TransientLaunchHandle, operation: (launch: Launch) => Promise): Promise; /** * Productive hooks may inspect or change implementation state. Every call receives * the same request abort signal and a timeout capped by the persisted deadline. */ export interface CoordinatorRuntime { now(): number; randomToken(): string; preflight(input: { request: ExecuteRequest; cwd: string; }, context: OperationContext): Promise<{ root: string; main: WorkspaceIdentity; }>; acquireLaunch(role: string, modelClass: ModelClass, context: OperationContext): Promise>; } export type HostAllocationKind = Exclude; export type HostCleanupKind = Extract; export type GitCleanupKind = Extract; export interface HostRuntime { planHostAllocation(input: { readonly requestId: ExecuteRequest["id"]; readonly goal: ExecuteRequest["goal"]; kind: HostAllocationKind; task: TaskRequest; attempt: TaskAttempt; }, context: OperationContext): Promise; allocateHost(input: { requestId: ExecuteRequest["id"]; intent: HostAllocationIntent; task: TaskRequest; attempt: TaskAttempt; /** Invoked only after pane, lease, and startability checks at the final agent-start boundary. */ acquireLaunch?: () => Promise>; }, context: OperationContext): Promise; reconcileHostAllocation(input: { requestId: ExecuteRequest["id"]; intent: HostAllocationIntent; task: TaskRequest; attempt: TaskAttempt; }, context: OperationContext): Promise>; runWorker(input: { readonly goal: ExecuteRequest["goal"]; readonly contexts: readonly TextTaskContext[]; task: TaskRequest; attempt: TaskAttempt; workerId: string; kind: "initial" | "correction" | "followup"; preCandidate: WorkspaceIdentity; failure?: string; instruction?: string; }, context: OperationContext): Promise; terminateWorker(input: { task: TaskRequest; attempt: TaskAttempt; workerId: string; candidate: WorkspaceIdentity; }, context: OperationContext): Promise<{ outcome: "terminated"; } | { outcome: "unknown"; failure: string; }>; reconcileWorkerTermination(input: { task: TaskRequest; attempt: TaskAttempt; workerId: string; candidate: WorkspaceIdentity; }, context: OperationContext): Promise<{ outcome: "terminated" | "active"; } | { outcome: "unknown"; failure: string; }>; cleanupHost(input: { requestId: ExecuteRequest["id"]; kind: HostCleanupKind; task: TaskRequest; attempt: TaskAttempt; }, context: OperationContext): Promise<{ outcome: "completed" | "absent"; } | { outcome: "blocked"; failure: string; }>; } export interface TaskCandidateInspector { inspectTaskCandidate(input: { root: string; task: TaskRequest; attempt: TaskAttempt; }, context: OperationContext): Promise; } export interface InFlightTaskCandidateInspection { candidate: WorkspaceIdentity; clean: boolean; valid: boolean; } export interface InFlightTaskCandidateInspector { inspectInFlightTaskCandidate(input: { root: string; task: TaskRequest; attempt: TaskAttempt; }, context: OperationContext): Promise; } export interface GitRuntime { inspectMain(input: { root: string; }, context: OperationContext): Promise; allocateWorktree(input: { root: string; intent: WorktreeAllocationIntent; task: TaskRequest; attempt: TaskAttempt; onPrepared(worktree: WorktreeAllocationPlan): Promise; }, context: OperationContext): Promise; reconcileWorktreeAllocation(input: { root: string; intent: WorktreeAllocationIntent; task: TaskRequest; attempt: TaskAttempt; }, context: OperationContext): Promise>; runChecks(input: { root: string; scope: "task" | "final"; taskId?: string; attempt?: TaskAttempt; checks: CheckCommand[]; candidate: WorkspaceIdentity; }, context: OperationContext): Promise; review(input: { root: string; scope: "task" | "final"; phase: ReviewEvidence["phase"]; taskId?: string; attempt?: TaskAttempt; criterion: string; base: WorkspaceIdentity; tip: WorkspaceIdentity; acquireLaunch(): Promise>; }, context: OperationContext): Promise; inspectRetainedTask(input: { root: string; task: TaskRequest; attempt: TaskAttempt; }, context: OperationContext): Promise; rebase(input: { root: string; task: TaskRequest; attempt: TaskAttempt; candidate: WorkspaceIdentity; sourceBase: WorkspaceIdentity; onto: WorkspaceIdentity; }, context: OperationContext): Promise; reconcileRebase(input: { root: string; task: TaskRequest; attempt: TaskAttempt; transition: RebaseTransition; }, context: OperationContext): Promise; integrate(input: { root: string; task: TaskRequest; attempt: TaskAttempt; expectedMain: WorkspaceIdentity; candidate: WorkspaceIdentity; checks: CheckBatchEvidence; review?: ReviewEvidence; }, context: OperationContext): Promise; cleanupGit(input: { root: string; kind: GitCleanupKind; task: TaskRequest; attempt: TaskAttempt; }, context: OperationContext): Promise<{ outcome: "completed" | "absent"; } | { outcome: "blocked"; failure: string; }>; } export type MainStatus = { status: "current" | "drifted"; expected: WorkspaceIdentity; actual: WorkspaceIdentity; } | { status: "unavailable"; expected: WorkspaceIdentity; failure: string; }; export interface RunResponse { text: string; state: RunState; main?: MainStatus; continuation?: ResumeRequest; } export type TextTaskContext = { taskId: string; text: string; }; /** Build one bounded changeset assignment. Call this before acquiring its Role launch. */ export declare function buildChangesetTaskPrompt(input: { readonly goal: ExecuteRequest["goal"]; readonly contexts: readonly TextTaskContext[]; task: ChangesetTaskRequest; kind: "initial" | "correction" | "followup"; worktreeCwd: string; failure?: string; instruction?: string; }): string; /** Resolve completed text outputs in the consumer's declared context order. */ export declare function resolveTextTaskContexts(state: { readonly tasks: readonly TaskState[]; }, task: Pick): TextTaskContext[]; /** Format complete context blocks or fail; prompt data is never truncated. */ export declare function formatTextTaskContexts(contexts: readonly TextTaskContext[], maxBytes: number): string; /** Build a bounded text-task assignment with upstream output marked as task data. */ export declare function buildTextTaskPrompt(goal: ExecuteRequest["goal"], task: Pick, contexts: readonly TextTaskContext[]): string; export declare function readyPendingTasks(state: RunState): TaskState[]; export declare class OrchestratorRunner { private readonly coordinatorRuntime; private readonly hostRuntime; private readonly gitRuntime; private readonly store; private readonly textExecutor; private readonly productiveLifecycles; private readonly followupControls; constructor(coordinatorRuntime: CoordinatorRuntime, hostRuntime: HostRuntime, gitRuntime: GitRuntime & TaskCandidateInspector, store: FileRunStore | undefined, textExecutor: EphemeralSubagentExecutor); queueFollowup(root: string, requestId: string, taskId: string, instruction: string): string; private withProductiveRun; private withLifecycleLock; private saveProductive; private callProductive; private rethrowStopped; execute(value: unknown, cwd: string, outerSignal?: AbortSignal): Promise; resume(value: unknown, root: string, outerSignal?: AbortSignal): Promise; abort(id: string, root: string, outerSignal?: AbortSignal): Promise; status(id: string, root: string, outerSignal?: AbortSignal): Promise; private run; private dispatchTask; private dispatchTextTask; private driveWorkerSafely; private driveWorker; private terminationCandidate; private terminateWithSafety; private runCheckBatch; private runReview; private integrateTask; private prepareCorrectionAfterAuthoritativeFailure; private reconcilePendingRebase; private finalizeIntegratedTask; private runCleanup; private verifyCleanupOnly; private verifyRetainedTask; private retry; private finalize; private runFinal; private markFinalFailed; private markSuperseded; private recoverInterrupted; private ensureFollowupControl; private controlKey; private activeControl; private activeControls; private hasActiveControl; private hasAnyActiveControl; private closeFollowupControl; private closeRequestControls; private attention; private response; } export {};