UNPKG

@code-yeongyu/senpi

Version:

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

137 lines • 7.46 kB
/** * `senpi host handoff --when idle --operation <id> --if-instance <id> --if-generation <n> --target-build <id>`: * the client half of the conditional idle handover. * * The CLI may only ask for its OWN runtime: the successor is launched from this process's runtime * with the launch spec's extensions, so `--target-build` must be the `runtimeBuildId` this process * computes for that spec (`target_build_mismatch` otherwise). It checks what it can before asking - * the host serving the socket is the named generation, and it advertises the handover - and then * hands the operation to the HOST (`begin_handover`), which owns it from there (`host-idle-handover.ts`). * * Answers, exit 0: `handover_completed` (the host was idle and the successor now serves, or the target * already serves the socket - a repeated request after completion), `handover_pending` (the host is * waiting for its running work; `host status` shows the operation). Exit 3: `stale_generation`, * `handover_blocked`, `handover_in_progress`, `target_build_mismatch`, `handover_unsupported`, `no_host`. * Exit 1 `handover_reply_lost`: the host did not answer and the socket does not serve the target, so * the operation's fate is unknown until `host status` is read again. */ import { engineBuildIdentity } from "../../core/engine-build-identity.js"; import { daemonEnvironment, daemonEnvOverrides } from "./host-daemon-env.js"; import { createHostDaemonPaths } from "./host-daemon-paths.js"; import { readHostRegistration } from "./host-daemon-registration.js"; import { foreignAgentDirRefusal } from "./host-handoff-agent-dir.js"; import { BEGIN_HANDOVER_COMMAND } from "./host-handover-wire.js"; import { defaultHostLaunch } from "./host-launch.js"; import { clientRuntimeBuildId, HOST_EXIT_ERROR, HOST_EXIT_OK, identityPayload, refusal, } from "./host-outcome.js"; import { probeProtocolInfo, requestOnSocket } from "./host-probe.js"; import { childStalledStopMaxMs } from "./host-stalled-evidence.js"; import { startupEnvelope } from "./host-startup-policy.js"; import { RUNTIME_IDENTITY_HANDOVER_CAPABILITY } from "./runtime-build-id.js"; const PROBE_TIMEOUT_MS = 10_000; export async function idleHandoverOutcome(target, spec, terms, mcpPresenterEnforcement) { const { socket } = target; if (process.platform === "win32") { return refusal("refuse", undefined, { reason: "upgrade_unsupported", socket, operationId: terms.operationId }); } const before = await probeProtocolInfo(socket, PROBE_TIMEOUT_MS); const refuse = (reason, extra = {}) => refusal("refuse", before, { reason, socket, operationId: terms.operationId, ...extra }); const foreign = before === undefined ? undefined : await foreignAgentDirRefusal(socket, target.agentDir, before); if (foreign) return refuse(foreign.reason, { detail: foreign.detail }); const client = await clientRuntimeBuildId(spec); if (client !== terms.targetRuntimeBuildId) { return refuse("target_build_mismatch", { detail: `this CLI launches ${client ?? "a runtime it cannot read"}`, clientRuntimeBuildId: client, }); } if (before === undefined) return refuse("no_host"); if (before.runtimeBuildId === terms.targetRuntimeBuildId) { return answered(target, "handover_completed", before, terms, null); } if (before.instanceId !== terms.ifInstanceId || before.generation !== terms.ifGeneration) { return refuse("stale_generation", { detail: `the socket is served by ${before.instanceId ?? "an unnamed host"} generation ${before.generation ?? "?"}`, }); } if (!before.capabilities.includes(RUNTIME_IDENTITY_HANDOVER_CAPABILITY)) return refuse("handover_unsupported"); const reply = await requestOnSocket(socket, beginHandoverCommand(target, spec, terms, mcpPresenterEnforcement), // I: a host idle at once hands over before answering, so the reply covers its lock wait W, the // handoff it then holds (H) and a reply margin (senpi#4602). startupEnvelope(childStalledStopMaxMs()).handoverReply); if (!isRecord(reply)) return reconcileLostReply(target, terms, before); if (typeof reply.refused === "string") { return refuse(reply.refused, typeof reply.detail === "string" ? { detail: reply.detail } : {}); } if (reply.state === "handover_blocked") { return refuse("handover_blocked", { detail: reply.reason ?? null, handover: reply }); } if (reply.state === "handover_completed") { return answered(target, "handover_completed", await probeProtocolInfo(socket, PROBE_TIMEOUT_MS), terms, reply); } return answered(target, "handover_pending", before, terms, reply); } function beginHandoverCommand(target, spec, terms, mcpPresenterEnforcement) { const env = {}; for (const [name, value] of Object.entries(daemonEnvironment(process.env, daemonEnvOverrides(process.env, spec.env)))) { if (value !== undefined) env[name] = value; } const launch = defaultHostLaunch([]); return { type: BEGIN_HANDOVER_COMMAND, operation_id: terms.operationId, if_instance_id: terms.ifInstanceId, if_generation: terms.ifGeneration, target_runtime_build_id: terms.targetRuntimeBuildId, // The successor runs THIS CLI's runtime, so this CLI's engine is the one the host judges (senpi#7018). target_engine: { text: engineBuildIdentity().text, ordinal: engineBuildIdentity().ordinal }, ...(terms.allowDowngrade === true && { allow_downgrade: true }), agent_dir: target.agentDir, launch: { command: launch.command, args: launch.args, reports_startup: true }, host_args: spec.hostArgs, env, ...(mcpPresenterEnforcement === true && { mcp_presenter_enforcement: true }), ...(spec.policy !== undefined && { policy: spec.policy }), }; } /** No answer is not a verdict: the socket says whether the target serves it now. */ async function reconcileLostReply(target, terms, before) { const now = await probeProtocolInfo(target.socket, PROBE_TIMEOUT_MS); if (now?.runtimeBuildId === terms.targetRuntimeBuildId) { return answered(target, "handover_completed", now, terms, null); } return { exitCode: HOST_EXIT_ERROR, payload: { action: "error", reason: "handover_reply_lost", operationId: terms.operationId, ...identityPayload(target.socket, await servingPid(target), now ?? before), }, }; } async function answered(target, action, host, terms, handover) { return { exitCode: HOST_EXIT_OK, payload: { action, ...identityPayload(target.socket, await servingPid(target), host), operationId: typeof handover?.operation_id === "string" ? handover.operation_id : terms.operationId, clientRuntimeBuildId: terms.targetRuntimeBuildId, handover, }, }; } /** The supervisor pid the daemon directory names as serving the socket; 0 when none is recorded. */ async function servingPid(target) { const registered = await readHostRegistration(createHostDaemonPaths(target)); return registered?.record.pid ?? 0; } function isRecord(value) { return typeof value === "object" && value !== null && !Array.isArray(value); } //# sourceMappingURL=host-handover-request.js.map