@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
113 lines • 6.86 kB
TypeScript
/**
* What a client does when it finds - or does not find - a host on the shared RPC socket.
*
* One machine-wide host serves several client surfaces, so the process on the socket is
* usually NOT this process's child. Two invariants follow, and this module is where they
* are decided once instead of in every client:
*
* I1 - never terminate, signal or replace a host you did not start. The only sanctioned
* replacement is the drain-based generation handoff, which ends no running work.
* I2 - compatibility is `protocolVersion` + capabilities, NEVER a version-string compare,
* and "is my build newer?" is `compareEngineOrdinal`, never `serverVersion`. An
* ordinal that cannot be compared (a build without git metadata, a host that reports
* none, an ordinal outside senpi CalVer such as a compiled omo's `[5, 1, 78]`, senpi#7040) is
* EQUAL, and since a handoff needs STRICTLY greater, such a pair attaches.
*
* The profile rule is the third guard: a handoff additionally requires the candidate's
* extension set to be a SUPERSET of the running host's. Handing off to a narrower generation
* would silently drop whatever the running host loaded (an editor plugin, a member bundle)
* from every session that reopens in the new generation - so a narrower candidate attaches
* and says so instead.
*/
import { type EngineBuildIdentity } from "../../core/engine-build-identity.ts";
import { type HostHolder } from "./host-holder.ts";
import type { HostProtocolInfo } from "./host-protocol-info.ts";
import type { RpcLaunchProfile } from "./rpc-types.ts";
export { type HostProtocolInfo, parseHostProtocolInfo } from "./host-protocol-info.ts";
/** The wire protocol this build speaks. A host answering with any other number is refused, not replaced. */
export declare const HOST_PROTOCOL_VERSION = 1;
/**
* HOST capability: this host drains into a successor generation on SIGUSR1 instead of dying with
* its sessions. A host without it is attach-only - it is never renamed, signalled or upgraded.
*/
export declare const GENERATION_HANDOFF_CAPABILITY = "generation_handoff";
/**
* HOST capability (senpi#4602): every rename or remove this host makes on its public socket entry runs under the
* endpoint's mutation fence and re-reads the endpoint owner token. Only a predecessor that advertises it is handed
* off with the owner-token guard; an older one gets today's handoff and its late-rename race.
*/
export declare const ENDPOINT_OWNER_FENCE_CAPABILITY = "endpoint_owner_fence";
/** What a daemon session needs from a host before any client may attach to it. */
export declare const REQUIRED_HOST_CAPABILITIES: readonly ["multi_session", "extension_events", "session_context", "session_kind"];
export interface HostDecisionClient {
readonly protocolVersion: number;
readonly requiredCapabilities: readonly string[];
readonly identity: EngineBuildIdentity;
/**
* What THIS client would launch a host with. Absent = no launch spec (a bare CLI attaching to
* whatever is running): such a client can prove nothing about extensions, so it never hands off.
*/
readonly launchProfile?: RpcLaunchProfile;
/** True when the running host's pidfile records this process as its writer (the I1 question). */
readonly startedByUs: boolean;
/** The platform both sides run on - one daemon is machine-local, so the host shares it. */
readonly platform: NodeJS.Platform;
}
/** `upgrade` may hand off, `fallback` prefers no host over a mismatched one, `never` only attaches. */
export type HostDecisionPolicy = "upgrade" | "fallback" | "never";
export type HostAction = HostDecision["action"];
/** Attached anyway, with something the caller should say out loud. */
export type HostDecisionWarning =
/** This client's extension set is missing something the host loads; upgrading would drop it. */
"profile_narrower_attached"
/** The profiles differ (or cannot be compared) and no upgrade was possible. */
| "profile_mismatch_attached";
/** Every action carries only the reasons that can produce it, so an impossible pair cannot be built. */
export type HostDecision = {
readonly action: "start";
/** `no_host`: nothing answered. `restart_own_host`: nothing answered and the pidfile is ours. */
readonly reason: "no_host" | "restart_own_host";
readonly upgradeable: false;
} | {
readonly action: "refuse";
readonly reason: "protocol" | "capability" | "host_stopping";
readonly upgradeable: false;
} | {
readonly action: "fallback";
readonly reason: "capability" | "engine_mismatch";
readonly upgradeable: boolean;
} | {
readonly action: "reuse";
/** `handoff_unsupported`: the host predates the drain handoff. `win32_attach_only`: named pipes cannot be renamed and win32 has no SIGUSR1. */
readonly reason: "compatible" | "handoff_unsupported" | "win32_attach_only";
/** Whether a generation handoff against this host is possible at all, independent of the policy. */
readonly upgradeable: boolean;
readonly warning?: HostDecisionWarning;
} | {
readonly action: "handoff";
readonly reason: "newer_engine" | "profile";
readonly upgradeable: true;
};
/** What a `never` policy can answer: it attaches, starts or refuses, and never touches a running host. */
export type AttachOnlyDecision = Extract<HostDecision, {
action: "start" | "reuse" | "refuse";
}>;
/** Why an ensure refused. The decision's own refusals plus the one only a pidfile can produce. */
export type HostRefusalReason = "protocol" | "capability" | "foreign_writer" | "legacy_host" | "host_busy" | "host_stopping" | "host_stalled" | "endpoint_occupied";
/**
* An ensure that found an unusable host and refused to act on it. A refusal is FINAL by design:
* this process may not signal a host it did not start (I1), and starting a second host on an
* endpoint another process already owns is exactly the failure the refusal exists to prevent.
*/
export declare class HostEnsureRefusedError extends Error {
readonly socket: string;
readonly reason: HostRefusalReason;
/** What only the caller knows: which process is in the way, and how to clear it. */
readonly detail: string | undefined;
/** The generation in the way, when its registration names one (`foreign_writer`). */
readonly holder: HostHolder | undefined;
constructor(socket: string, reason: HostRefusalReason, host: HostProtocolInfo | undefined, detail?: string, holder?: HostHolder);
}
export declare function decideHostAction(client: HostDecisionClient, host: HostProtocolInfo | undefined, policy: "never"): AttachOnlyDecision;
export declare function decideHostAction(client: HostDecisionClient, host: HostProtocolInfo | undefined, policy: HostDecisionPolicy): HostDecision;
//# sourceMappingURL=host-decision.d.ts.map