UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

63 lines • 3.08 kB
import { VERSION } from "../../config.js"; import { consumeHostWatchdogEnv, scrubHostWatchdogEnv } from "./host-watchdog.js"; import { getInternalHostBootstrap } from "./internal-bootstrap.js"; import { acceptInternalConnection } from "./internal-channel.js"; import { PublicRpcConnection } from "./rpc-public-auth.js"; import { RpcPublicEndpoint } from "./rpc-public-key.js"; import { authenticateSocket } from "./socket-transport.js"; let binding; /** * The supervisor binding, read once per process. The watch variables are removed as they are read (senpi#3815 review * L4), so no tool, shell or nested host this host starts inherits a binding to a supervisor that is not its own. */ export function hostWatchdogBinding() { binding ??= { config: consumeHostWatchdogEnv() }; return binding.config; } export function internalHostTransport(configured, secret, endpoint) { const bootstrap = getInternalHostBootstrap(); scrubHostWatchdogEnv(); const watchdogConfig = configured && { ...configured, ...(bootstrap?.lifetimeFd === undefined ? {} : { lifetimeFd: bootstrap.lifetimeFd }), }; // Supervised means holding the internal key, which only the launching supervisor can hand over and no descendant // inherits; a host without it is a public endpoint whatever its environment says (senpi#3815 review L4). const publicEndpoint = endpoint !== undefined && process.platform !== "win32" && bootstrap === undefined ? new RpcPublicEndpoint(endpoint.agentDir, endpoint.socket) : undefined; return { watchdogConfig, admit(socket, accept) { if (bootstrap) void acceptInternalConnection(socket, bootstrap.key, { protocolVersion: 1, serverVersion: VERSION }) .then((hop) => accept(hop, "in_band")) .catch((cause) => { // One line per refused connection: the refusal code only (auth_required, auth_failed, a timeout). process.stderr.write(`senpi rpc host: internal connection refused: ${cause instanceof Error ? cause.message : "unknown"}\n`); socket.destroy(); }); else if (secret) authenticateSocket(socket, secret, () => accept(undefined, "proven")); else if (publicEndpoint) accept(new PublicRpcConnection(socket, publicEndpoint).stream, "in_band"); else accept(); }, async publish(onFailure) { try { await publicEndpoint?.publish(); } catch (cause) { endpoint?.log(cause instanceof Error ? cause.message : String(cause)); await onFailure(); } }, async dispose() { await publicEndpoint?.dispose().catch((cause) => { endpoint?.log(`endpoint key cleanup failed: ${cause instanceof Error ? cause.message : String(cause)}`); }); }, }; } //# sourceMappingURL=host-internal-transport.js.map