@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
130 lines • 7.22 kB
JavaScript
/**
* `senpi host status --all`: one status row per endpoint an agent directory holds state for.
*
* Enumeration is the daemon directory (`listHostEndpoints`), and every row is the single-socket
* report read with `prune: false` - so this answer removes NOTHING. Dead generations stay visible as
* `alive: false` rows, an endpoint whose host exited stays listed through its `endpoint.json`, and
* the reclaiming of what ended is a separate, evidence-gated command rather than a side effect of
* looking. A directory that names no socket still gets a row (`socket: null`), built from the
* directory alone, because it is exactly what an operator asking "what is on this machine" needs to
* see.
*
* Every row carries the endpoint's `endpoint_kind` and the liveness verdict (`host-endpoint-liveness.ts`)
* as `alive` (routable) plus `reason` when it is not. `alive` is judged from what the socket itself
* answered, never from the directory's record of it: a socket that answers without naming an instance
* is not routable, whatever generation the directory last recorded. A `tui` row is read under
* TUI_PROBE_TIMEOUT_MS whatever budget the caller grants the hosts, so one suspended terminal costs the
* listing 1.5 s, not 10; it is sent `get_protocol_info` and `list_sessions` only - the two commands a
* terminal endpoint and a host both answer - and carries `owner`: the terminal process and the one
* session it holds. A host row also carries `stale_old_engine`: whether `senpi host gc` would stop it as a
* stale older-engine leftover (`host-stale-engine.ts`, #3268), judged with observing reads only.
*/
import { engineBuildIdentity } from "../../core/engine-build-identity.js";
import { isHostCrash, readHostCrashRecords } from "./host-crash-record.js";
import { hostDaemonDirectoryPaths } from "./host-daemon-paths.js";
import { endpointProbeTimeoutMs, judgeEndpointLiveness } from "./host-endpoint-liveness.js";
import { listHostEndpoints } from "./host-endpoints.js";
import { readGenerationRows } from "./host-generations.js";
import { withHostUnits } from "./host-unit-placement.js";
import { judgeStaleEngine } from "./host-stale-engine.js";
import { probeHostStatus } from "./host-status.js";
import { readClaimRows } from "./host-status-rows.js";
/**
* Endpoints read at once. Each read holds a socket and a few files open until its budget runs out, so
* an unbounded fan-out over hundreds of hung hosts hit EMFILE under a 256-descriptor hard limit and
* returned no rows at all; up to this many hung hosts still cost about one budget in total.
*/
export const STATUS_ALL_MAX_IN_FLIGHT = 64;
/**
* Endpoints are read concurrently, at most STATUS_ALL_MAX_IN_FLIGHT at a time, each under its own
* budget, so hung hosts cost about one budget per STATUS_ALL_MAX_IN_FLIGHT rather than one each; rows
* keep the enumeration order (sorted by directory name).
*/
export async function readAllHostStatus(options) {
const endpoints = await listHostEndpoints(options.agentDir);
const read = options._test?.readEndpoint ?? ((endpoint) => endpointStatus(endpoint, options));
const rows = new Array(endpoints.length);
let next = 0;
const worker = async () => {
for (let index = next++; index < endpoints.length; index = next++) {
const endpoint = endpoints[index];
if (endpoint !== undefined)
rows[index] = await read(endpoint);
}
};
await Promise.all(Array.from({ length: Math.min(STATUS_ALL_MAX_IN_FLIGHT, endpoints.length) }, worker));
return rows;
}
async function endpointStatus(endpoint, options) {
const located = { dir: endpoint.dir, identity: endpoint.identity, endpoint_kind: endpoint.endpoint_kind };
const paths = hostDaemonDirectoryPaths(endpoint.dir);
if (endpoint.socket === null) {
const verdict = livenessFields(await judgeEndpointLiveness(paths, undefined));
const status = await unaddressableStatus(endpoint.dir);
const owner = endpoint.endpoint_kind === "tui" ? tuiOwner(status, []) : null;
return { ...status, socket: null, ...located, ...verdict, owner, stale_old_engine: false };
}
const { report, answered, listing } = await probeHostStatus({ socket: endpoint.socket, agentDir: options.agentDir, includeWorkers: options.includeWorkers }, { prune: false, timeoutMs: endpointProbeTimeoutMs(endpoint.endpoint_kind, options.timeoutMs) });
const verdict = await judgeEndpointLiveness(paths, answered === undefined ? undefined : (answered.instanceId ?? null));
const owner = endpoint.endpoint_kind === "tui" ? tuiOwner(report, listing) : null;
const stale = endpoint.endpoint_kind === "rpc_host" && verdict === "routable"
? await staleOldEngine(endpoint.socket, options)
: false;
return { ...report, ...located, ...livenessFields(verdict), owner, stale_old_engine: stale };
}
async function staleOldEngine(socket, options) {
const staleEngine = options.staleEngine ?? {};
return (await judgeStaleEngine(socket, options.agentDir, staleEngine.client ?? engineBuildIdentity(), staleEngine))
.stale;
}
/** The recorded pid of the pointer's generation, and the one session row a terminal endpoint lists. */
function tuiOwner(status, listing) {
const row = listing[0];
return {
pid: status.generations.find((generation) => generation.current)?.pid ?? null,
cwd: row?.cwd ?? null,
session: row === undefined ? null : { id: row.id, path: row.session_path, name: row.name },
};
}
function livenessFields(verdict) {
return verdict === "routable" ? { alive: true, reason: null } : { alive: false, reason: verdict };
}
async function unaddressableStatus(dir) {
const paths = hostDaemonDirectoryPaths(dir);
const generations = await withHostUnits(await readGenerationRows(paths, { includeDead: true }));
const claims = await readClaimRows(paths, generations);
// Without the socket no generation can be proven to serve it, so nothing is elected or offered as a candidate.
const current = generations.find((generation) => generation.current);
return {
reachable: false,
lifecycle: null,
pid: null,
processStartTime: null,
instanceId: null,
generation: null,
engineVersion: null,
capabilities: [],
launchProfile: null,
runtimeBuildId: null,
handover: null,
sessions: { total: 0, interactive: 0, worker: 0, retained: 0, foreign_attached: 0, foreign_retained: 0 },
zombies: null,
rss_mb: null,
host_rss_mb: null,
host_footprint_mb: null,
open_fds: null,
memory_pressure: null,
path_identity_resolver: null,
env_keys: [],
generations,
pointer: current
? { state: "current", instanceId: current.instanceId, candidates: [] }
: { state: "missing", instanceId: null, candidates: [] },
crashes: readHostCrashRecords(dir).filter(isHostCrash).length,
shard: null,
session_rows: [],
claims_live: claims.filter((claim) => claim.live).length,
claims: [],
};
}
//# sourceMappingURL=host-status-all.js.map