@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
126 lines • 7.97 kB
TypeScript
/** Filesystem identity of one bound socket path entry. */
export interface SocketFileIdentity {
readonly dev: number;
readonly ino: number;
}
/**
* Name of the sidecar, inside the lifecycle supervisor's private scratch
* directory, that records which public socket entry that supervisor bound. The
* directory is per-supervisor, so a replacement supervisor's token can never be
* mistaken for the old one's.
*/
export declare const PUBLIC_SOCKET_IDENTITY_FILE = "public-socket.owner";
/** What the public path holds relative to the entry a generation bound there. */
export type EndpointOwnership = "held" | "replaced" | "absent" | "unknown";
/** Default bound on waiting for a supervisor to publish its ownership token. */
export declare const SOCKET_IDENTITY_WAIT_MS = 30000;
/**
* `sun_path` is 104 bytes INCLUDING its terminator on macOS, so a bindable path fits in 103.
* A generation's bind path is the longest name this daemon ever produces, which makes it the
* one that must be checked before a bind turns the overflow into a truncated, silently wrong
* endpoint.
*/
export declare const MAX_SOCKET_PATH_BYTES = 103;
/**
* `<socket>.bind-<hex>`: the private 0700 directory a host binds its socket in (as `BIND_SCRATCH_SOCKET`) before
* publishing it at `<socket>` with `link(2)` (#5319). Named after the socket so `host gc` can reclaim one a crash left.
*/
export declare const BIND_SCRATCH_INFIX = ".bind-";
export declare const BIND_SCRATCH_SOCKET = "s";
/** A bind directory's name, for any socket it was made beside (the public one, or a successor's `.next-<gen>`). */
export declare function isBindScratchName(name: string): boolean;
/**
* Where a successor generation binds before it owns the public name. It never binds the live
* public path: that path belongs to the running host until the successor is proven to work.
*/
export declare function generationBindPath(publicSocket: string, generation: number): string;
/** `<dev>:<ino>` as the ensure captured it; anything else is no identity at all, never a guess. */
export declare function parseSocketIdentity(value: string): SocketFileIdentity | undefined;
/**
* What one failed connect says about the process behind a socket entry. Only a refusal can mean
* "nobody": on Linux a full backlog answers EAGAIN, so ECONNREFUSED is proof of absence there,
* but macOS (and the BSDs) refuse a live listener whose backlog is full, so a refusal there needs
* a second witness. ENOENT means the entry is gone; every other error is a live owner (I1).
*/
export type ConnectVerdict = "live" | "dead" | "gone" | "confirm-holder";
export declare function classifyConnectError(code: string | undefined, platform: NodeJS.Platform): ConnectVerdict;
/** Whether any process holds the entry open: pids from `lsof -t`, none, or unknown. */
export type SocketHolders = (socketPath: string) => Promise<readonly number[] | undefined>;
/**
* `lsof -- <path>` matches a unix socket by the name it was bound at. An entry published with `link(2)` (nlink > 1)
* is bound elsewhere (#5319), so a path lookup that finds nobody falls back to matching by inode.
*/
export declare const lsofSocketHolders: SocketHolders;
/** Whether a live process stands behind a socket entry, and the evidence for the answer. */
export interface ListenerState {
readonly verdict: "live" | "dead" | "gone";
readonly evidence: string;
}
export declare function socketEntryListenerState(socketPath: string, platform?: NodeJS.Platform, holders?: SocketHolders): Promise<ListenerState>;
/**
* Reads the current identity of the entry AT a socket path; `undefined` once absent. Read with `lstat`: a symlink
* is never followed and answers with its own identity, which can never match a bound socket's, so every
* ownership check reads it as somebody else's entry rather than as the socket it names.
*/
export declare function statSocketIdentity(socketPath: string): Promise<SocketFileIdentity | undefined>;
/**
* What the public path now holds relative to the entry `identity` describes, in ONE stat so the
* answers cannot disagree with each other:
*
* - `held`: still this generation's own entry.
* - `replaced`: a successor renamed its socket over it, or something else took the name.
* - `absent`: no entry at all - somebody's `rm`, a swept temp directory, a deleted workspace.
* - `unknown`: nothing can be proven (no identity, win32, an abstract socket, a failed stat).
*
* `replaced` and `absent` are both losses of reachability, but they are NOT the same observation
* and callers must keep them apart: a successor is serving clients on that name, while an absent
* name serves nobody. `unknown` is never a claim.
*/
export declare function classifyEndpointOwnership(socketPath: string, identity: SocketFileIdentity | undefined): Promise<EndpointOwnership>;
/**
* Whether `socketPath` is served by a DIFFERENT entry than `identity` describes: a successor
* generation renamed its own socket over it, or something else took the name. A generation that
* finds this true no longer owns the endpoint and cannot be reached by path any more.
*
* An absent entry and an unknown identity both answer `false`. Supersession has to be POSITIVELY
* observed - a name that is merely missing is somebody's `rm`, not a newer host, and treating it as
* one would make a healthy daemon drain itself.
*/
export declare function socketEntryReplaced(socketPath: string, identity: SocketFileIdentity | undefined): Promise<boolean>;
/** Records a bound socket identity for teardown paths that cannot hold it in memory. */
export declare function writeSocketIdentityFile(path: string, identity: SocketFileIdentity): Promise<void>;
export declare function readSocketIdentityFile(path: string): Promise<SocketFileIdentity | undefined>;
/**
* Waits, bounded, for an identity file to appear. The lifecycle supervisor
* publishes its public-socket token after this host's internal listener is
* ready, so supervised hosts poll briefly instead of assuming ordering.
*/
export declare function waitForSocketIdentityFile(path: string, timeoutMs?: number, intervalMs?: number): Promise<SocketFileIdentity | undefined>;
/**
* Unlinks a socket path only while it still refers to the entry `identity`
* describes. An absent path is nothing to do; a mismatched identity means a
* newer host owns the name now and is left alone; an unknown identity can
* never be proven ours, so the path is left and the decision is logged.
*/
export declare function unlinkOwnedSocket(socketPath: string, identity: SocketFileIdentity | undefined, log: (message: string) => void): Promise<void>;
/**
* Runs `close` with the socket path's current entry shielded from libuv's
* close-time unlink.
*
* libuv unlinks the NAME a pipe was bound to when its listening handle
* closes - even when a takeover has renamed a DIFFERENT socket over that
* name, so an ordinary `server.close()` silently deletes the newer host's
* freshly published entry. Moving the current entry aside for the duration
* of the close (libuv's unlink then hits ENOENT) and restoring it afterwards
* keeps the filesystem exactly as it was; ownership-checked removal via
* `unlinkOwnedSocket` decides afterwards what may actually be deleted.
*/
export declare function shieldSocketDuringClose(socketPath: string, close: () => Promise<void>): Promise<void>;
/**
* Takes the public name over with a rename, once - and only while - that name still refers to the
* entry this handoff was decided against. A socket that changed underneath belongs to another
* process now: replacing it would unlink an endpoint this generation cannot prove it owns, so the
* successor aborts instead and leaves both the intruder's socket and its own bind entry alone.
*/
export declare function adoptPublicSocket(bindSocket: string, publicSocket: string, expected: SocketFileIdentity | undefined): Promise<void>;
//# sourceMappingURL=socket-ownership.d.ts.map