/**
 * @module        worker-manager
 * @description   WorkerManager ACP 接口定义，引擎层与 Worker 进程管理之间的契约
 *
 * @author        eddy
 * @organization  wykj
 * @ownership     wykj/eddy
 *
 * @created       2026-03-28
 * @updated       2026-04-03
 *
 * @role          manager
 * @layer         manager
 * @boundedContext worker-lifecycle
 */
import type { CompletionResult } from './completion-judge.js';
export type WorkerState = 'idle' | 'starting' | 'running' | 'waiting_input' | 'needs_confirmation' | 'completed' | 'failed';
export type WorkerPhase = 'development' | 'integration';
export interface WMPendingInput {
    type: 'text' | 'confirmation';
    prompt: string;
    options?: string[];
    detectedAt: string;
}
export interface TaskRunRequest {
    taskId: string;
    cardId: string;
    project: string;
    phase: WorkerPhase;
    /** YAML stage name (develop/qa/integrate/...). Used to build COMPLETED-<stageName> label. */
    stageName: string;
    /** Human-readable card title (passed to worker as SPS_CARD_TITLE env). */
    cardTitle?: string;
    prompt: string;
    cwd: string;
    branch: string;
    targetBranch: string;
    tool: 'claude';
    transport: 'acp-sdk';
    outputFile: string;
    timeoutSec?: number;
    maxRetries?: number;
    env?: Record<string, string>;
    /** Completion strategy: git-evidence (default), fast-forward-merge, exit-code */
    completionStrategy?: string;
}
export interface TaskResumeRequest {
    taskId: string;
    cardId: string;
    project: string;
    phase: WorkerPhase;
    prompt: string;
    cwd: string;
    branch: string;
    targetBranch: string;
    tool: 'claude';
    transport: 'acp-sdk';
    outputFile: string;
    sessionId?: string;
}
export interface TaskCancelRequest {
    taskId: string;
    project: string;
    reason: 'timeout' | 'user_cancel' | 'anomaly';
}
export interface TaskInputRequest {
    taskId: string;
    project: string;
    input: string;
}
export interface TaskConfirmRequest {
    taskId: string;
    project: string;
    action: 'confirm' | 'reject';
    message?: string;
}
export interface InspectQuery {
    project?: string;
    taskId?: string;
    slot?: string;
}
export type RejectReason = 'resource_exhausted' | 'duplicate_task' | 'invalid_request' | 'spawn_failed';
export interface TaskRunResponse {
    accepted: boolean;
    slot: string | null;
    workerId: string | null;
    pid?: number;
    sessionId?: string;
    queued?: boolean;
    queuePosition?: number;
    rejectReason?: RejectReason;
}
export interface WorkerSnapshot {
    slot: string;
    taskId: string | null;
    cardId: string | null;
    project: string;
    state: WorkerState;
    phase: WorkerPhase | null;
    pid: number | null;
    sessionId: string | null;
    cwd: string | null;
    branch: string | null;
    startedAt: string | null;
    updatedAt: string;
    outputTail: string | null;
    pendingInput: WMPendingInput | null;
}
export type WorkerEventType = 'status.update' | 'run.completed' | 'run.failed' | 'input.required';
export interface WorkerEvent {
    type: WorkerEventType;
    taskId: string;
    cardId: string;
    project: string;
    phase: WorkerPhase;
    slot: string;
    workerId: string;
    timestamp: string;
    state: WorkerState;
    /** Present on run.completed / run.failed */
    exitCode?: number;
    completionResult?: CompletionResult;
    /** Present on input.required */
    pendingInput?: WMPendingInput;
    /** Last lines of worker output */
    outputTail?: string;
    /** Error message on failure */
    error?: string;
}
export type WorkerEventHandler = (event: WorkerEvent) => void;
export interface RecoveryContext {
    project: string;
    stateFile: string;
    baseBranch: string;
}
export interface RecoveryResult {
    scanned: number;
    alive: number;
    completed: number;
    failed: number;
    released: number;
    rebuilt: number;
    queueRebuilt: number;
    events: WorkerEvent[];
}
export interface WorkerManager {
    /**
     * Launch a new worker for a task.
     * Allocates slot, spawns process, registers exit callback chain.
     */
    run(request: TaskRunRequest): Promise<TaskRunResponse>;
    /**
     * Resume a previously started task (same worktree, optionally same session).
     */
    resume(request: TaskResumeRequest): Promise<TaskRunResponse>;
    /**
     * Cancel a running or queued task.
     * SIGTERM → 5s grace → SIGKILL. Releases slot + resources.
     */
    cancel(request: TaskCancelRequest): Promise<void>;
    /**
     * Send text input to a worker waiting for stdin.
     * Only supported for ACP transport; proc returns unsupported error.
     */
    sendInput(request: TaskInputRequest): Promise<void>;
    /**
     * Confirm or reject a worker's confirmation prompt.
     * Only supported for ACP transport.
     */
    confirm(request: TaskConfirmRequest): Promise<void>;
    /**
     * Inspect current worker state.
     * Returns snapshots filtered by query (project, taskId, or slot).
     */
    inspect(query: InspectQuery): WorkerSnapshot[];
    /**
     * Register an event handler for worker lifecycle events.
     * Events are persisted to state.json before callback invocation.
     */
    onEvent(handler: WorkerEventHandler): void;
    /**
     * Recover workers after tick restart.
     * Scans state.json, checks PID survival, rebuilds projections.
     */
    recover(contexts: RecoveryContext[]): Promise<RecoveryResult>;
    /**
     * Clean up internal resources (timers, monitors) on tick shutdown.
     * Must be called before the process exits to prevent timer leaks.
     */
    cleanup(): void;
}
//# sourceMappingURL=worker-manager.d.ts.map