import { type EngineBuildIdentity } from "../../core/engine-build-identity.ts";
import type { Duplex } from "node:stream";
import type { HostLifecyclePolicyInput } from "./host-lifecycle.ts";
export interface HandoffHostOptions {
    /**
     * The launch owner's enrollment channel. A provider (a running host's deferred handover) is called only when the
     * successor reaches its presenter phase, so its permit is fresh for the exchange (senpi#4602).
     */
    readonly presenterBootstrap?: Duplex | (() => Promise<Duplex | undefined>);
    /** Deployment opt-in. An enforcing predecessor never downgrades during replacement. */
    readonly mcpPresenterEnforcement?: boolean;
    readonly socket: string;
    readonly agentDir?: string;
    /** Extra CLI arguments the successor's host child is launched with (provider pinning, extensions). */
    readonly hostArgs?: readonly string[];
    /** Environment for the successor; a `null` value removes an inherited variable. */
    readonly env?: Readonly<Record<string, string | null>>;
    /**
     * The engine the successor runs. Defaults to THIS process's build; a running host performing an idle handover
     * passes the engine the requesting CLI stated. A successor strictly older than the running host is refused
     * `older_engine` unless `allowDowngrade` (senpi#7018).
     */
    readonly successorEngine?: EngineBuildIdentity;
    /** The caller's explicit consent to hand a newer host down to an older engine. */
    readonly allowDowngrade?: boolean;
    /** The runtimeBuildId the successor must compute; set on its environment, never inherited by later daemons. */
    readonly expectedRuntimeBuildId?: string;
    readonly policy?: HostLifecyclePolicyInput;
    /**
     * How the successor is launched from supervisor argv. Defaults to THIS process's runtime; a
     * running host performing an idle handover passes the runtime of the CLI that asked for it.
     */
    readonly launch?: (args: readonly string[]) => SuccessorLaunch;
    readonly _test?: {
        readonly readinessTimeoutMs?: number;
        /** Builds the spawnable command from supervisor argv; tests point it at the source entry. */
        readonly launch?: (args: readonly string[]) => SuccessorLaunch;
        /** Runs after the public socket identity is captured and before the successor is spawned. */
        readonly beforeSpawn?: () => Promise<void>;
        /** Runs once the successor is spawned and its generation recorded, before its answer is awaited. */
        readonly afterSpawn?: (pid: number) => Promise<void>;
        /** Runs once the successor answered on the public socket, before the pointer is moved onto it. */
        readonly beforeRegistration?: () => Promise<void>;
        readonly platform?: NodeJS.Platform;
    };
}
/** `reportsStartup`: the command is the engine's own supervisor, which reports its startup phases (senpi#4602). */
export interface SuccessorLaunch {
    readonly command: string;
    readonly args: readonly string[];
    readonly reportsStartup?: boolean;
}
/** Why a handoff did not happen. Every one of them leaves the running host untouched. */
export type HandoffRefusal = 
/** Nothing is serving the socket: there is no generation to hand off from. */
"no_host"
/** The running host predates the drain handler; signalling it would kill it. */
 | "handoff_unsupported"
/** win32: a named pipe can be neither renamed nor drained. */
 | "upgrade_unsupported"
/** No record proves which process serves this socket, and that host holds sessions (or will not say). */
 | "unknown_owner"
/** The running host belongs to another agent dir; only a client of that dir may replace it (#3560). */
 | "foreign_agent_dir"
/** A host from before layout 2, proven by its flat record, holds sessions; `detail` says how to retire it. */
 | "legacy_host"
/** The public socket stopped being the one this handoff was decided against. */
 | "socket_replaced"
/** `<public>.next-<gen>` would exceed the platform's socket path limit. */
 | "socket_path_too_long"
/** The successor never answered on the public socket; it was stopped and nothing was replaced. */
 | "successor_unavailable"
/** The successor's engine is older than the running host's and the caller did not allow a downgrade. */
 | "older_engine";
export type HandoffResult = {
    readonly action: "handoff";
    readonly pid: number;
    readonly socket: string;
    readonly generation: number;
    readonly instanceId: string;
} | {
    readonly action: "refuse";
    readonly reason: HandoffRefusal;
    readonly upgradeable: boolean;
    readonly detail?: string;
};
/**
 * Hands the socket to a new generation of this build. Forced by design: the caller decides
 * whether an upgrade is warranted (`decideHostAction`); this performs the one it asked for, except
 * a downgrade: a successor whose engine is older than the running host's is refused unless the caller
 * says `allowDowngrade` (senpi#7018), so no caller can hand a newer host down by accident.
 */
export declare function handoffHost(options: HandoffHostOptions): Promise<HandoffResult>;
/** The handoff itself, for a caller that already holds this socket's ensure lock: an ensure's upgrade. */
export declare function handoffHostLocked(requested: HandoffHostOptions): Promise<HandoffResult>;
//# sourceMappingURL=host-handoff.d.ts.map