UNPKG

@code-yeongyu/senpi

Version:

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

104 lines • 5.22 kB
import { holdAttachment } from "./host-attach-hold.js"; import { parseHostProtocolInfo } from "./host-decision.js"; import { OBSERVE_REQUEST_FIELD } from "./host-observe-request.js"; import { connectAndAsk, isRecord, PROBE_REQUEST_ID, readAnswer, requestOnSocket } from "./host-socket-request.js"; export { requestOnSocket }; /** Default probe budget: long enough for a host under load, short enough to keep an ensure moving. */ export const DEFAULT_PROBE_TIMEOUT_MS = 10_000; /** The running host's identity, or `undefined` when nothing is serving the endpoint. */ export function probeHost(options) { return probeProtocolInfo(options.socket, options.timeoutMs ?? DEFAULT_PROBE_TIMEOUT_MS, options); } export async function probeProtocolInfo(socketPath, timeoutMs, options = {}) { const reply = await requestOnSocket(socketPath, { type: "get_protocol_info" }, timeoutMs, options); return reply === undefined ? undefined : parseHostProtocolInfo(reply); } /** `probeProtocolInfo` for a read that must not count as host activity, such as `host status`. */ export async function observeProtocolInfo(socketPath, timeoutMs, options = {}) { const reply = await requestOnSocket(socketPath, { type: "get_protocol_info", [OBSERVE_REQUEST_FIELD]: true }, timeoutMs, options); return reply === undefined ? undefined : parseHostProtocolInfo(reply); } /** * The host's identity together with an attach hold on the connection that answered, or `undefined` * when nothing answers. The answer and the claim share one connection, so there is no instant * between "the host is ready" and "the host is held" in which an idle window could close. */ export async function holdProtocolInfo(socketPath, timeoutMs, options = {}) { const outcome = await connectAndAsk(socketPath, { id: PROBE_REQUEST_ID, type: "get_protocol_info" }, timeoutMs, true, options); const info = outcome.answer === undefined ? undefined : parseHostProtocolInfo(outcome.answer); if (outcome.kept === undefined) return undefined; const hold = holdAttachment(outcome.kept); if (info !== undefined) return { info, hold }; hold.release(); return undefined; } /** * Whether the socket ACCEPTED a connection during the last probe, regardless of whether an answer * arrived in time. A host under load can miss a probe budget while it is perfectly alive, and the * difference decides whether an ensure may end it: a silent-but-connectable socket has an owner * serving sessions behind it, and killing that owner destroys every one of them. */ export async function probeSocketReachable(socketPath, timeoutMs, options = {}) { return (await connectAndAsk(socketPath, { id: PROBE_REQUEST_ID, type: "get_protocol_info" }, timeoutMs, false, options)).connected; } /** * How many sessions the host holds right now, worker sessions included, or `undefined` when it * does not answer. A stop decision needs the number the host reports, not the one a client remembers. */ export async function probeSessionCount(socketPath, timeoutMs, options = {}) { return sessionCountOf(await requestOnSocket(socketPath, { type: "list_sessions", include_workers: true }, timeoutMs, options)); } export async function holdSessionCount(socketPath, timeoutMs, options = {}) { const outcome = await connectAndAsk(socketPath, { id: PROBE_REQUEST_ID, type: "list_sessions", include_workers: true }, timeoutMs, true, options); const kept = outcome.kept; if (kept === undefined) return undefined; const sessions = sessionCountOf(outcome.answer); if (sessions === undefined) { kept.destroy(); return undefined; } let asked = 0; return { sessions, recount: (recountTimeoutMs) => askAgain(kept, `${PROBE_REQUEST_ID}-recount-${++asked}`, recountTimeoutMs), close: () => kept.destroy(), }; } function askAgain(socket, id, timeoutMs) { return new Promise((resolveCount) => { let buffer = ""; const finish = (count) => { clearTimeout(timeout); socket.off("data", onData); socket.off("close", onClose); resolveCount(count); }; const onClose = () => finish(undefined); const onData = (chunk) => { buffer += chunk.toString("utf8"); for (let newline = buffer.indexOf("\n"); newline !== -1; newline = buffer.indexOf("\n")) { const answer = readAnswer(buffer.slice(0, newline), id); buffer = buffer.slice(newline + 1); if (answer !== undefined) { finish(sessionCountOf(answer)); return; } } }; const timeout = setTimeout(() => finish(undefined), timeoutMs); if (socket.destroyed) { finish(undefined); return; } socket.on("data", onData); socket.once("close", onClose); socket.write(`${JSON.stringify({ id, type: "list_sessions", include_workers: true })}\n`); }); } function sessionCountOf(reply) { return isRecord(reply) && Array.isArray(reply.sessions) ? reply.sessions.length : undefined; } //# sourceMappingURL=host-probe.js.map