@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
236 lines • 12 kB
JavaScript
/**
* One host request, performed: ensure a daemon, report one, stop one, or hand one off.
*
* This is the whole behaviour behind `senpi host` - and behind the launchers that call the same
* entry point in-process - separated from the command line that expresses it. Every request answers
* with a JSON payload plus the exit code that payload means, so a CLI, a desktop and a task runner
* report the same outcome with the same vocabulary:
*
* 0 the request happened 3 refused: a running host this client may not act on,
* 4 no host is better than this one an unreachable socket, or a stop that would end
* one (fallback policy) somebody else's work
*
* The refusals are the point. One daemon holds every client's sessions on its endpoint, so a client
* that cannot get what it wants must SAY so and leave the running host alone (I1) - never stop it,
* never bind a second host over its socket. `decideHostAction`, `ensureHost`, `stopHost` and
* `handoffHost` already enforce that; this module only gives their outcomes a machine-readable shape.
*
* A terminal control endpoint (`endpoint_kind: "tui"`) is owned by its terminal process and is never
* ensured, handed off, drained or stopped by a host operator: `ensure`, `handoff` and `stop` against
* one refuse with `unsupported_endpoint_kind` (exit 3) from disk alone, before any connection.
*/
import { daemonEnvKeys, daemonEnvOverrides, writeDaemonEnvKeys } from "./host-daemon-env.js";
import { createHostDaemonPaths, shardKey, shardSocketPathForKey } from "./host-daemon-paths.js";
import { decideHostAction, HostEnsureRefusedError, } from "./host-decision.js";
import { endpointKindOfSocket } from "./host-endpoints.js";
import { ensureHost } from "./host-ensure.js";
import { gcHostEndpoints } from "./host-gc.js";
import { handoffHost } from "./host-handoff.js";
import { idleHandoverOutcome } from "./host-handover-request.js";
import { clientRuntimeBuildId, decisionClient, HOST_EXIT_OK, HOST_EXIT_REFUSED, identityPayload, refusal, } from "./host-outcome.js";
import { probeProtocolInfo } from "./host-probe.js";
import { readHostStatus, readSessionCounts } from "./host-status.js";
import { readAllHostStatus } from "./host-status-all.js";
import { staleStopGeneration, stopHost } from "./host-stop.js";
import { hostUnitPayload } from "./host-unit-placement.js";
export { HOST_EXIT_ERROR, HOST_EXIT_FALLBACK, HOST_EXIT_OK, HOST_EXIT_REFUSED, HOST_EXIT_USAGE, } from "./host-outcome.js";
import { hostRequestKeyRoot } from "./host-request-key-root.js";
import { withRpcKeyRoot } from "./rpc-auth-client.js";
const PROBE_TIMEOUT_MS = 10_000;
export async function runHostRequest(request, options = {}) {
return withRpcKeyRoot(hostRequestKeyRoot(request, options.keyRoot), () => runHostRequestInKeyRoot(request, options.presenterBootstrap, options.mcpPresenterEnforcement));
}
async function runHostRequestInKeyRoot(request, presenterBootstrap, mcpPresenterEnforcement) {
if (request.action === "ensure" || request.action === "handoff" || request.action === "stop") {
const { socket, agentDir } = request.target;
if ((await endpointKindOfSocket(socket, agentDir)) === "tui") {
return refusal("refuse", undefined, { reason: "unsupported_endpoint_kind", socket, endpoint_kind: "tui" });
}
}
switch (request.action) {
case "ensure":
return ensureOutcome(request.target, request.spec, request.policy, presenterBootstrap, mcpPresenterEnforcement);
case "status":
return request.all === true
? statusAllOutcome(request.target, request.includeWorkers)
: statusOutcome(request.target, request.includeWorkers);
case "stop":
return stopOutcome(request);
case "handoff":
return request.handover === undefined
? handoffOutcome(request, presenterBootstrap, mcpPresenterEnforcement)
: idleHandoverOutcome(request.target, request.spec, request.handover, mcpPresenterEnforcement);
case "shard_path":
return shardPathOutcome(request.kind, request.owner, request.root);
case "gc":
return {
exitCode: HOST_EXIT_OK,
payload: { ...(await gcHostEndpoints(request.agentDir, { staleEngine: {} })) },
};
default:
return assertNever(request);
}
}
async function ensureOutcome(target, spec, policy, presenterBootstrap, mcpPresenterEnforcement) {
const before = await probeProtocolInfo(target.socket, PROBE_TIMEOUT_MS);
if (policy === "fallback") {
// `fallback` is the caller saying it can live without a host. The decision is made HERE
// because an ensure always produces a host or fails: it has no fallback answer of its own.
const decision = decideHostAction(decisionClient(), before, "fallback");
if (decision.action === "fallback") {
return refusal("fallback", before, { reason: decision.reason, socket: target.socket });
}
}
let ensured;
try {
ensured = await ensureHost({
socket: target.socket,
presenterBootstrap,
mcpPresenterEnforcement,
agentDir: target.agentDir,
hostArgs: spec.hostArgs,
env: daemonEnvOverrides(process.env, spec.env),
policy: spec.policy,
upgrade: policy === "upgrade" ? "if-engine-differs" : "never",
});
}
catch (error) {
if (!(error instanceof HostEnsureRefusedError))
throw error;
return refusal("refuse", before, {
reason: error.reason,
socket: target.socket,
...(error.detail !== undefined && { detail: error.detail }),
...(error.holder !== undefined && { holder: error.holder }),
});
}
const host = await probeProtocolInfo(target.socket, PROBE_TIMEOUT_MS).finally(ensured.release);
// Only a spawn grants an environment; a reuse inherited whatever the client that started it did.
if (!ensured.reused)
await recordEnvScope(target, spec);
return {
exitCode: HOST_EXIT_OK,
payload: {
action: ensureAction(ensured.reused, before, host),
...identityPayload(target.socket, ensured.pid, host),
...(await hostUnitPayload(ensured.pid, host?.instanceId)),
clientRuntimeBuildId: await clientRuntimeBuildId(spec),
reused: ensured.reused,
},
};
}
/**
* What an ensure DID. A handoff and a fresh start both report `reused: false`, so the instance id is
* what tells them apart: a handoff replaced the process behind a socket somebody was already serving.
*/
function ensureAction(reused, before, after) {
if (reused)
return "reuse";
return before !== undefined && before.instanceId !== after?.instanceId ? "handoff" : "start";
}
async function statusOutcome(target, includeWorkers) {
const status = await readHostStatus({ socket: target.socket, agentDir: target.agentDir, includeWorkers });
return { exitCode: status.reachable ? HOST_EXIT_OK : HOST_EXIT_REFUSED, payload: { ...status } };
}
/** Same exit vocabulary as one socket: 0 while anything answers, 3 when nothing does (or nothing is there). */
async function statusAllOutcome(target, includeWorkers) {
const endpoints = await readAllHostStatus({ agentDir: target.agentDir, includeWorkers });
const reachable = endpoints.some((endpoint) => endpoint.reachable);
return { exitCode: reachable ? HOST_EXIT_OK : HOST_EXIT_REFUSED, payload: { endpoints } };
}
/** The naming contract answered without contacting any host, for a client that mirrors it locally. */
function shardPathOutcome(kind, owner, root) {
const key = shardKey(kind, owner);
return { exitCode: HOST_EXIT_OK, payload: { kind, key, socket: shardSocketPathForKey(root, kind, key) } };
}
/**
* A hard stop ends every session the daemon holds, including the ones other clients are attached to,
* so it is gated on the host's own count and refuses with those counts rather than a bare "busy".
* A DRAIN ends no work - the host stops accepting and leaves when it is empty - so it is never gated.
*/
async function stopOutcome(request) {
const { target, drain, force, instanceId, waitMs } = request;
if (request.expectedGeneration !== undefined) {
const stale = await staleStopGeneration(target.socket, target.agentDir, request.expectedGeneration);
if (stale !== undefined)
return refusal("refuse", undefined, { reason: stale.reason, socket: target.socket });
}
const host = await probeProtocolInfo(target.socket, PROBE_TIMEOUT_MS);
const sessions = await readSessionCounts(target.socket, true);
const occupied = sessions.foreign_attached + sessions.foreign_retained > 0;
if (!drain && !force && occupied) {
return refusal("refuse", host, { reason: "sessions_live", socket: target.socket, sessions });
}
const result = await stopHost({
socket: target.socket,
agentDir: target.agentDir,
drain,
force,
...(request.expectedGeneration !== undefined && { expectedGeneration: request.expectedGeneration }),
...(instanceId !== undefined && { instanceId }),
...(waitMs !== undefined && { waitMs }),
});
if (result.action === "refuse") {
return refusal("refuse", host, { reason: result.reason, socket: target.socket, sessions });
}
return {
exitCode: HOST_EXIT_OK,
payload: {
action: result.action,
socket: target.socket,
pid: result.pid,
...("instanceId" in result && result.instanceId !== undefined && { instanceId: result.instanceId }),
...("exited" in result && result.exited !== undefined && { exited: result.exited }),
sessions,
},
};
}
async function handoffOutcome(request, presenterBootstrap, mcpPresenterEnforcement) {
const { target, spec } = request;
const before = await probeProtocolInfo(target.socket, PROBE_TIMEOUT_MS);
const result = await handoffHost({
socket: target.socket,
presenterBootstrap,
mcpPresenterEnforcement,
agentDir: target.agentDir,
hostArgs: spec.hostArgs,
env: daemonEnvOverrides(process.env, spec.env),
policy: spec.policy,
...(request.allowDowngrade === true && { allowDowngrade: true }),
});
if (result.action === "refuse") {
return refusal("refuse", before, {
reason: upgradeRefusal(result.reason),
socket: target.socket,
detail: result.detail ?? result.reason,
upgradeable: result.upgradeable,
});
}
await recordEnvScope(target, spec);
const host = await probeProtocolInfo(target.socket, PROBE_TIMEOUT_MS);
return {
exitCode: HOST_EXIT_OK,
payload: {
action: "handoff",
...identityPayload(target.socket, result.pid, host),
...(await hostUnitPayload(result.pid, host?.instanceId)),
clientRuntimeBuildId: await clientRuntimeBuildId(spec),
reused: false,
},
};
}
/**
* A host that cannot drain and a platform that cannot hand off are one answer to the caller: this
* build may not replace the running generation. The refusal that produced it stays in `detail`.
*/
function upgradeRefusal(reason) {
return reason === "handoff_unsupported" ? "upgrade_unsupported" : reason;
}
async function recordEnvScope(target, spec) {
const paths = createHostDaemonPaths({ socket: target.socket, agentDir: target.agentDir });
await writeDaemonEnvKeys(paths, daemonEnvKeys(process.env, spec.env));
}
function assertNever(value) {
throw new Error(`unreachable host request: ${JSON.stringify(value)}`);
}
//# sourceMappingURL=host-runner.js.map