@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
65 lines • 3.93 kB
JavaScript
/**
* The `get_protocol_info` answer as a CLIENT reads it: the shape, and the tolerant parse that
* produces it.
*
* This is a BOUNDARY: everything past it is typed, and nothing past it re-validates. Every identity
* field is optional because a host from an older release answers without them, and an absent field
* stays absent rather than being defaulted - a guessed `0` generation or a guessed ordinal would be
* a comparison a client could act on, which is precisely what must not happen (I2). The one field
* that IS defaulted is `protocolVersion`, to `0`, because no client speaks protocol 0: a reply from
* before the field existed therefore fails closed instead of matching.
*/
import { parsePathIdentityResolverStatus } from "./path-identity-status.js";
import { parseRuntimeBuildId } from "./runtime-build-id.js";
/** Parses the `data` of a `get_protocol_info` reply. Unknown or malformed identity fields are dropped, not guessed. */
export function parseHostProtocolInfo(data) {
if (!isRecord(data) || typeof data.serverVersion !== "string")
return undefined;
if (!Array.isArray(data.capabilities) || !data.capabilities.every((entry) => typeof entry === "string")) {
return undefined;
}
const ordinal = parseEngineOrdinal(data.engineOrdinal);
const launchProfile = parseLaunchProfile(data.launch_profile);
const runtimeBuildId = parseRuntimeBuildId(data.runtimeBuildId);
const pathIdentityResolver = parsePathIdentityResolverStatus(data.path_identity_resolver);
return {
// A reply without a protocol version predates the field; 0 never matches, so it fails closed.
protocolVersion: typeof data.protocolVersion === "number" ? data.protocolVersion : 0,
serverVersion: data.serverVersion,
capabilities: data.capabilities,
...((data.lifecycle === "ready" || data.lifecycle === "stopping") && { lifecycle: data.lifecycle }),
...(typeof data.instanceId === "string" && { instanceId: data.instanceId }),
...(typeof data.generation === "number" &&
Number.isSafeInteger(data.generation) && { generation: data.generation }),
...(typeof data.engineVersion === "string" && { engineVersion: data.engineVersion }),
...(ordinal && { engineOrdinal: ordinal }),
...(launchProfile && { launch_profile: launchProfile }),
...(typeof data.memory_pressure === "boolean" && { memory_pressure: data.memory_pressure }),
...(pathIdentityResolver !== undefined && { path_identity_resolver: pathIdentityResolver }),
...(runtimeBuildId !== undefined && { runtimeBuildId }),
...(typeof data.agentDir === "string" && data.agentDir !== "" && { agentDir: data.agentDir }),
...(isRecord(data.handover) && { handover: data.handover }),
};
}
/** An engine ordinal on the wire: exactly five numbers, else `undefined` (never a guessed ordinal). */
export function parseEngineOrdinal(value) {
if (!Array.isArray(value) || value.length !== 5 || !value.every((part) => typeof part === "number"))
return undefined;
return [value[0], value[1], value[2], value[3], value[4]];
}
function parseLaunchProfile(value) {
if (!isRecord(value) || typeof value.profile_id !== "string" || !isRecord(value.core))
return undefined;
const { extensions, multi_session, session_runtime } = value.core;
if (!Array.isArray(extensions) || !extensions.every((entry) => typeof entry === "string"))
return undefined;
if (typeof multi_session !== "boolean")
return undefined;
if (session_runtime !== "in-process" && session_runtime !== "worker")
return undefined;
return { profile_id: value.profile_id, core: { extensions, multi_session, session_runtime } };
}
function isRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
//# sourceMappingURL=host-protocol-info.js.map