/**
 * The conditional idle handover, as the RUNNING host performs it.
 *
 * `senpi host handoff --when idle` names the generation it means (`ifInstanceId`, `ifGeneration`),
 * the runtime it wants serving (`targetRuntimeBuildId`) and one `operationId`. The host owns the
 * operation from then on, because only it can see its own turns and only it outlives the CLI call:
 *
 *   1. it checks, synchronously and before anything else, that it IS the named generation, and
 *      refuses `stale_generation` otherwise;
 *   2. it stops admitting new input (`admits`): prompts, steering, follow-ups, user bash,
 *      compaction, edits and wake deliveries are answered with a `handover_pending` error, while
 *      every turn already running keeps running and keeps its control traffic;
 *   3. it waits for its next safe idle point - no open session busy, no request in flight. An idle
 *      session that is merely retained is not work. The wait has NO deadline: a long turn delays the
 *      handover, it is never aborted for it;
 *   4. it starts the successor through the ordinary generation handoff (successor proven on the
 *      public socket, then this generation drains and parks its idle sessions for the successor to
 *      reopen by path). A successor that does not come up leaves this generation serving and admits
 *      input again (`handover_blocked { reason }`).
 *
 * A repeated `operationId` with the same terms answers with the operation that already exists,
 * whatever its state, so a caller that lost a reply can ask again without starting a second one; the
 * same id with other terms is refused (`operation_conflict`). Another id for the same target joins
 * the pending operation and is answered with ITS id; another target is refused
 * (`handover_in_progress`) until the pending operation ends.
 */
import { type EngineBuildIdentity } from "../../core/engine-build-identity.ts";
import type { HostLifecyclePolicyInput } from "./host-lifecycle.ts";
/**
 * Set on a successor launched by an idle handover: the runtime it must report. A successor that
 * computes another id exits before it listens, so the socket never moves to the wrong runtime.
 */
export declare const EXPECTED_RUNTIME_BUILD_ID_ENV = "SENPI_RPC_HOST_EXPECTED_RUNTIME_BUILD_ID";
export type HandoverState = "handover_pending" | "handover_switching" | "handover_completed" | "handover_blocked";
export interface HandoverLaunch {
    readonly command: string;
    readonly args: readonly string[];
    /** The requester's supervisor reports its startup phases, so the successor runs the bounded phase policy. */
    readonly reportsStartup?: boolean;
}
export interface IdleHandoverRequest {
    /** Successor deployment policy only; never enrollment authority. */
    readonly mcpPresenterEnforcement?: boolean;
    readonly operationId: string;
    readonly ifInstanceId: string;
    readonly ifGeneration: number;
    readonly targetRuntimeBuildId: string;
    /** How the CLI that asked re-enters its own runtime as a supervisor: the successor runs THAT runtime. */
    readonly launch: HandoverLaunch;
    readonly hostArgs: readonly string[];
    /** The complete, already allowlisted daemon environment of the CLI that asked. */
    readonly env: Readonly<Record<string, string>>;
    readonly policy?: HostLifecyclePolicyInput;
    /** The agent dir of the CLI that asked; absent from CLIs before #3560. Another dir than the host's is refused. */
    readonly agentDir?: string;
    /**
     * The engine of the CLI that asked, which is the engine the successor runs. Absent from CLIs before senpi#7018:
     * such a CLI predates the downgrade guard, so its engine is older than any host that enforces it.
     */
    readonly targetEngine?: EngineBuildIdentity;
    /** The requester's explicit consent to hand this host down to an older engine. */
    readonly allowDowngrade?: boolean;
}
export interface HandoverSuccessor {
    readonly pid: number;
    readonly instanceId: string;
    readonly generation: number;
    readonly runtimeBuildId: string | null;
}
export interface HandoverView {
    readonly operationId: string;
    readonly state: HandoverState;
    readonly targetRuntimeBuildId: string;
    readonly reason?: string;
    readonly successor?: HandoverSuccessor;
}
export type HandoverAnswer = {
    readonly kind: "operation";
    readonly view: HandoverView;
} | {
    readonly kind: "refused";
    readonly reason: string;
    readonly detail?: string;
};
export type HandoverOutcome = {
    readonly ok: true;
    readonly successor: HandoverSuccessor;
} | {
    readonly ok: false;
    readonly reason: string;
};
export interface HostIdentityView {
    readonly instanceId: string;
    readonly generation: number;
    readonly runtimeBuildId: string | undefined;
    /** This host's own engine; a request for an older one is refused `older_engine` (senpi#7018). */
    readonly engine?: EngineBuildIdentity;
}
export interface HostIdleHandoverOptions {
    readonly identity: () => HostIdentityView;
    readonly isIdle: () => boolean;
    readonly perform: (request: IdleHandoverRequest) => Promise<HandoverOutcome>;
    /**
     * Called in the same synchronous step as the idle check that starts the switch, before anything is awaited, so work
     * the idle check does not see (machine ops, senpi#6752) cannot start between that check and the end of this host;
     * `releaseNewWork` reopens it when the handover is blocked and this host keeps serving.
     */
    readonly holdNewWork?: () => void;
    readonly releaseNewWork?: () => void;
    /** How often a pending operation re-checks for idle between request settlements. */
    readonly pollMs?: number;
    readonly log?: (line: string) => void;
}
export declare class HostIdleHandover {
    private readonly options;
    private operation;
    private timer;
    constructor(options: HostIdleHandoverOptions);
    /** The operation this host holds, for `get_protocol_info` and therefore `host status`. */
    view(): HandoverView | undefined;
    /** False while a handover holds new work back; a blocked one admits again. */
    admits(commandType: string): boolean;
    begin(request: IdleHandoverRequest): Promise<HandoverAnswer>;
    /** Re-checks a pending operation; the host calls it whenever a request settles. */
    observe(): void;
    dispose(): void;
    private idleNow;
    private switchOver;
    private stopTimer;
}
//# sourceMappingURL=host-idle-handover.d.ts.map