@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
250 lines • 12.2 kB
JavaScript
/**
* The supervisor's PUBLIC socket: the byte proxy every client reaches the host through, the bind and
* adoption of the public name, and the readiness probes of the private hop. Split out of
* `host-lifecycle.ts`, which keeps the supervisor's orchestration (senpi#2566).
*/
import { lstat, mkdir, unlink } from "node:fs/promises";
import { createConnection, createServer } from "node:net";
import { dirname } from "node:path";
import { proxyWithAdmission } from "./host-admission-proxy.js";
import { listen as bindOwnedSocket } from "./host-socket-bind.js";
import { connectInternalConnection } from "./internal-channel.js";
import { attachJsonlLineReader, MAX_RPC_LINE_CHARACTERS } from "./jsonl.js";
import { PublicRpcConnection } from "./rpc-public-auth.js";
import { MAX_SOCKET_PATH_BYTES, socketEntryListenerState, } from "./socket-ownership.js";
import { authenticateSocket, ensureSocketSecret, resolveSocketTransportAddress, sendSocketHandshake, socketSecretPath, } from "./socket-transport.js";
export function createPublicProxy(options) {
const { internalSocket, internalSecret, publicSecret, clients } = options;
return createServer((client) => {
const auth = options.publicEndpoint ? new PublicRpcConnection(client, options.publicEndpoint) : undefined;
const accept = async () => {
// Teardown is terminal even for an accept that raced socket shielding/server.close.
if (!clients.admit(client))
return;
client.pause();
// Draining peers can still probe and receive typed open refusals, without a child hop.
const internal = options.admission.stopping
? undefined
: createConnection(resolveSocketTransportAddress(internalSocket, process.platform, internalSecret));
if (internal && internalSecret)
sendSocketHandshake(internal, internalSecret);
// A readiness exchange can begin and end between ticks: the client is recorded the
// moment its first request line arrives, before a later tick can reuse the preceding
// idle window. An observing read (`status`) is never recorded (host-client-occupancy.ts).
const detach = () => {
// Classification resets activity for attachments. Defer the unknown-admission reset
// until release so a status read never resets owner grace, even between ticks.
if (clients.release(client))
options.onAdmit?.();
options.onDetach();
internal?.destroy();
client.destroy();
};
client.once("close", detach);
client.once("error", detach);
try {
const hop = internal && options.internalKey
? await connectInternalConnection(internal, options.internalKey)
: internal;
if (client.destroyed) {
hop?.destroy();
return;
}
proxyWithAdmission(client, hop, options.admission, auth, publicSecret !== undefined);
// An authenticated endpoint resumes the client itself once its key record is published.
if (!auth)
client.resume();
}
catch {
detach();
}
};
if (publicSecret)
authenticateSocket(client, publicSecret, () => void accept());
else
void accept();
});
}
/**
* Reuses an existing valid secret - including one ensureHost() just wrote - and
* creates one (with its parent directories, mode 0600) when it is missing or
* unusable. Reuse is required, not just an optimization: on win32 the pipe name
* is derived from the socket path AND the secret, so rotating it here would
* point this supervisor at a different endpoint than its caller published.
* A failure names the bootstrap step and the path it could not provision.
*/
export async function ensurePublicSocketSecret(publicSocket) {
const secretPath = socketSecretPath(publicSocket);
try {
return await ensureSocketSecret(secretPath);
}
catch (cause) {
throw new Error(`senpi rpc host supervisor: cannot provision public socket secret ${secretPath}`, { cause });
}
}
/**
* One sealed readiness probe: the internal handshake and the ready reply together, on one monotonic timer (senpi#3816
* review). A child that accepts the hop and stays silent that long is not a working host, so readiness fails at once
* instead of hanging the supervisor; a host that is not listening yet is retried until the overall deadline.
*/
export const INTERNAL_READINESS_PROBE_MS = 10_000;
export async function waitForListener(socketPath, timeoutMs, secret, internalKey,
/** A coordinated start retries a silent probe on a fresh connection; its launcher decides expiry (senpi#4602). */
options = {}) {
const deadline = performance.now() + timeoutMs;
while (performance.now() <= deadline) {
const probeMs = Math.max(1, Math.min(INTERNAL_READINESS_PROBE_MS, deadline - performance.now()));
const probe = await canConnect(socketPath, secret, internalKey, probeMs);
if (probe === "ready")
return;
if (probe === "silent" && !options.retrySilent)
throw new Error(`${socketPath}: host accepted the internal hop but did not answer within ${Math.round(probeMs)}ms`);
// A host that answered but could not prove the key will not start proving it: fail now, and say why.
if (probe !== "refused" && probe !== "silent")
throw new Error(`${socketPath}: ${probe.unproven}`);
await new Promise((resolveDelay) => setTimeout(resolveDelay, 50));
}
throw new Error(`${socketPath}: host did not start listening within ${timeoutMs}ms`);
}
async function canConnect(socketPath, secret, internalKey, probeMs) {
if (internalKey) {
const socket = createConnection(resolveSocketTransportAddress(socketPath, process.platform, secret));
let accepted = false;
let expired = false;
socket.once("connect", () => {
accepted = true;
});
// Destroying the socket ends the handshake or the ready wait, whichever is running, and drops the connection.
const timer = setTimeout(() => {
expired = true;
socket.destroy();
}, probeMs);
timer.unref();
const outcome = (ready) => (ready ? "ready" : accepted && expired ? "silent" : "refused");
try {
const stream = await connectInternalConnection(socket, internalKey);
return outcome(await new Promise((resolve) => {
const settle = (ready) => {
stream.destroy();
resolve(ready);
};
stream.once("error", () => settle(false));
stream.once("close", () => settle(false));
attachJsonlLineReader(stream, (line) => {
try {
const response = JSON.parse(line);
if (response.id === "internal-ready")
settle(response.success === true);
}
catch {
settle(false);
}
}, { maxLineLength: MAX_RPC_LINE_CHARACTERS });
stream.write('{"id":"internal-ready","type":"get_protocol_info","observe":true}\n');
}));
}
catch (cause) {
socket.destroy();
const reason = cause instanceof Error ? cause.message : "";
if (reason === "host_identity_unproven" || reason === "auth_failed")
return { unproven: reason };
return outcome(false);
}
finally {
clearTimeout(timer);
}
}
return new Promise((resolve) => {
const socket = createConnection(resolveSocketTransportAddress(socketPath, process.platform, secret));
if (secret)
sendSocketHandshake(socket, secret);
const settle = (connected) => {
socket.destroy();
resolve(connected ? "ready" : "refused");
};
socket.once("connect", () => settle(true));
socket.once("error", () => settle(false));
});
}
async function stampOf(socketPath) {
try {
const entry = await lstat(socketPath, { bigint: true });
return { dev: entry.dev, ino: entry.ino, ctimeNs: entry.ctimeNs, socket: entry.isSocket() };
}
catch (cause) {
if (cause instanceof Error && "code" in cause && cause.code === "ENOENT")
return undefined;
throw cause;
}
}
const sameStamp = (a, b) => a.dev === b.dev && a.ino === b.ino && a.ctimeNs === b.ctimeNs;
/**
* Frees `socketPath` for this process's bind, removing only a dead socket entry and refusing everything else
* (#5314). No portable fd-based check exists for a socket entry: `open()` fails on one (ENXIO on Linux,
* EOPNOTSUPP on macOS), `O_PATH` is Linux-only, and there is no unlink-by-fd. So every read is `lstat`, which
* never follows a link, and `unlink(2)` removes the name, never a link's target. The entry is re-stamped before
* the second reachability probe (which must not be steered through a swapped-in link) and again as the last step
* before the unlink; reachability is re-checked after the decision because a socket bound earlier can start
* listening without changing identity.
*/
export async function prepareSocketPath(socketPath, hooks = {}) {
if (process.platform === "win32" || socketPath.startsWith("\0"))
return;
// A path the kernel would truncate binds a DIFFERENT endpoint than the one every client was
// told about, and the failure surfaces much later as "the host does not answer". Refuse here.
if (Buffer.byteLength(socketPath) > MAX_SOCKET_PATH_BYTES) {
throw new Error(`${socketPath}: socket path is ${Buffer.byteLength(socketPath)} bytes, over the ${MAX_SOCKET_PATH_BYTES}-byte limit.`);
}
await mkdir(dirname(socketPath), { recursive: true, mode: 0o700 });
const probed = await stampOf(socketPath);
if (probed === undefined)
return;
const inUse = (evidence) => new Error(`${socketPath}: address already in use by a live server (${evidence}).`);
// Linux refuses a connect to a regular file too; never remove what is not a socket.
if (!probed.socket) {
throw new Error(`${socketPath}: address already in use, occupied by a non-socket entry (a symlink or other file; nothing listens there).`);
}
const probe = hooks.listenerState ?? socketEntryListenerState;
const state = await probe(socketPath);
if (state.verdict === "gone")
return;
if (state.verdict === "live")
throw inUse(state.evidence);
await hooks.afterStaleProbe?.();
// Something may have replaced the entry since the probe; unlink only the entry probed dead.
const unchanged = async () => {
const current = await stampOf(socketPath);
if (current === undefined)
return false;
if (!sameStamp(probed, current))
throw inUse("the entry changed after it was probed");
return true;
};
if (!(await unchanged()))
return;
const again = await probe(socketPath);
if (again.verdict === "gone")
return;
if (again.verdict === "live")
throw inUse(again.evidence);
if (!(await unchanged()))
return;
try {
await unlink(socketPath);
}
catch (cause) {
if (cause instanceof Error && "code" in cause && cause.code === "ENOENT")
return;
throw cause;
}
}
/** The supervisor's public bind: the same never-through-a-link publish as the socket host's (#5319). */
export function listen(server, socketPath, secret) {
return bindOwnedSocket(server, socketPath, secret);
}
export function closeServer(server) {
return new Promise((resolve) => {
server.close(() => resolve());
});
}
//# sourceMappingURL=host-lifecycle-proxy.js.map