@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
71 lines • 4.14 kB
TypeScript
/**
* Ending a generation on purpose - the one path where a client may signal a host it did not spawn.
*
* Two different acts hide behind "stop the daemon". A DRAIN ends no work: the host stops accepting,
* finishes what it is doing and exits when it is empty, so it is always permitted against a host
* that advertises it can survive the signal at all. A HARD STOP ends whatever the host is doing,
* including the sessions of every other client sharing it, so it is permitted only against a host
* that reports nothing open - or behind an explicit `force`, which is the operator saying they know.
*
* Both are gated on the same proof: the registration must name the process serving THIS socket
* (pid plus identity guard). An owner nobody can prove is refused rather than signalled (I1). A host
* from before layout 2 is known only through its flat record (`host-legacy.ts`); that record proves
* it for a drain, never for a hard stop (#2423).
*
* The proof cannot close one race, and `signalGeneration` is where that is handled: a host is free
* to exit between the moment its identity is proven and the moment the signal is sent - a drained
* generation reaches its empty-exit within milliseconds of being asked to drain - so the delivery
* failure ESRCH means "already gone", not "something went wrong", and must never surface as a raw
* error to a caller that asked for exactly that outcome.
*/
import { type DrainHostResult } from "./host-stop-drain.ts";
import { type HostStopGeneration } from "./host-stop-generation.ts";
export interface StopHostOptions {
readonly socket: string;
readonly agentDir?: string;
/** Ask the host to finish its work and exit instead of terminating it (host-stop-drain.ts). */
readonly drain?: boolean;
/** With `drain`: the generation to ask, by instance id, instead of the one the pointer names. */
readonly instanceId?: string;
/** With `drain`: wait up to this long for the generation to exit, and report `exited`. */
readonly waitMs?: number;
/** Terminate even when the host still reports open sessions. */
readonly force?: boolean;
readonly timeoutMs?: number;
/** Refuse unless the host answers both reads: a caller that judged it idle must not stop it unseen. */
readonly requireAnswer?: boolean;
/** Refuse rather than signal a generation other than the one the operator confirmed. */
readonly expectedGeneration?: HostStopGeneration;
}
export type StopHostResult = DrainHostResult | {
readonly action: "stopped";
readonly pid: number;
} | {
readonly action: "refuse";
readonly reason: "unknown_owner" | "drain_unsupported" | "sessions_live" | "stale_generation";
};
/**
* Ends a running generation. `drain` is always permitted - it ends no work, it only stops the
* host from taking new work - while a hard stop requires either an empty host or `force`, so an
* operator never silently kills another client's sessions.
*/
export declare function stopHost(options: StopHostOptions): Promise<StopHostResult>;
type StopRefusal = Extract<StopHostResult, {
action: "refuse";
}>;
/**
* Refuses a binding the endpoint no longer matches, before any probe or census (desktop#3561 review): a
* caller learns its confirmation is stale rather than that the host it no longer names is busy.
*/
export declare function staleStopGeneration(socket: string, agentDir: string | undefined, expected: HostStopGeneration): Promise<StopRefusal | undefined>;
/**
* Signals a generation whose owner was just proven, and reports whether the signal was delivered.
*
* `false` means the process was gone before the signal landed - the one outcome the ownership proof
* cannot rule out, because nothing stops a host from exiting in between. Callers want that reported,
* not thrown: every caller here is asking the host to leave, and a host that already left satisfied
* the request. Any OTHER signalling failure (EPERM against somebody else's process) still throws.
*/
export declare function signalGeneration(pid: number, signal: NodeJS.Signals): boolean;
export {};
//# sourceMappingURL=host-stop.d.ts.map