UNPKG

@code-yeongyu/senpi

Version:

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

196 lines • 8.76 kB
/** * The terminal manifest's strict fail-closed parse: every field is checked, unknown keys are * ignored (so optional additions never need a version bump), and any malformed value throws * InvalidTerminalManifestError so a restore fails closed instead of guessing. */ import { InvalidSidecarStoreError } from "../../../session-sidecar-store.js"; import { TERMINAL_MANIFEST_VERSION, } from "./terminal-manifest-model.js"; export class InvalidTerminalManifestError extends InvalidSidecarStoreError { constructor(message, options) { super(message, options); this.name = "InvalidTerminalManifestError"; } } const RUNTIME_KINDS = ["command", "file", "rpc"]; const DURABILITY_CLASSES = ["ephemeral", "restartable-command", "checkpointed-file", "checkpointed-rpc"]; const FILE_EVENTS = ["create", "modify"]; const isStr = (value) => typeof value === "string" && value.length > 0; const isNum = (value) => typeof value === "number" && Number.isFinite(value); const isBool = (value) => typeof value === "boolean"; const isObj = (value) => typeof value === "object" && value !== null && !Array.isArray(value); function invalid(message) { throw new InvalidTerminalManifestError(`terminal manifest is invalid: ${message}`); } function str(raw, field) { if (!isStr(raw[field])) invalid(`field ${field} must be a non-empty string`); return raw[field]; } function num(raw, field) { if (!isNum(raw[field])) invalid(`field ${field} must be a finite number`); return raw[field]; } function digest(raw, field) { if (typeof raw[field] !== "string") invalid(`field ${field} must be a string`); return raw[field]; } function bool(raw, field) { if (!isBool(raw[field])) invalid(`field ${field} must be a boolean`); return raw[field]; } function opt(raw, field, required) { return raw[field] === undefined ? undefined : required(raw, field); } function wakeRate(raw, field) { const value = raw[field]; if (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > 3600) invalid(`field ${field} must be an integer from 1 to 3600`); return value; } function oneOf(values, raw, field) { const value = raw[field]; if (typeof value !== "string" || !values.includes(value)) invalid(`field ${field} must be one of: ${values.join(", ")}`); return value; } function checkpoint(raw) { if (!isObj(raw)) invalid("field lastCheckpoint must be an object"); return { dev: num(raw, "dev"), ino: num(raw, "ino"), size: num(raw, "size"), mtimeMs: num(raw, "mtimeMs"), // A checkpoint without a digest cannot detect a same-size, same-mtime rewrite, so the field is // required — but an absent file legitimately checkpoints an empty digest, so "" is valid. digest: digest(raw, "digest"), present: bool(raw, "present"), }; } function fireWindow(raw) { if (!isObj(raw)) invalid("field fireWindow must be an object"); return { startMs: num(raw, "startMs"), count: num(raw, "count") }; } function runtimeIdentity(raw, field) { const value = raw[field]; if (!isObj(value)) invalid(`field ${field} must be an object`); const argv = value.argv; if (!Array.isArray(argv) || !argv.every(isStr)) invalid(`field ${field}.argv must be an array of strings`); const pid = value.pid; if (!isNum(pid)) invalid(`field ${field}.pid must be a finite number`); const groupId = value.processGroupId; if (groupId !== undefined && !isNum(groupId)) invalid(`field ${field}.processGroupId must be a finite number`); return { pid, ...(groupId !== undefined ? { processGroupId: groupId } : {}), startedAtMs: num(value, "startedAtMs"), bootAtMs: num(value, "bootAtMs"), argv, }; } function strings(entry, field) { const value = entry[field]; if (!Array.isArray(value) || !value.every((item) => typeof item === "string")) invalid(`field ${field} must be an array of strings`); return value; } function rpcCheckpoint(value) { if (!isObj(value) || !isObj(value.hosts) || !isObj(value.statuses)) invalid("field rpcCheckpoint must be an object"); const hosts = {}; for (const [socket, host] of Object.entries(value.hosts)) { if (!isObj(host)) invalid("field rpcCheckpoint.hosts entries must be objects"); hosts[socket] = { instanceId: str(host, "instanceId"), generation: str(host, "generation") }; } const statuses = {}; for (const [key, status] of Object.entries(value.statuses)) { if (typeof status !== "string") invalid("field rpcCheckpoint.statuses values must be strings"); statuses[key] = status; } return value.remote === undefined ? { hosts, statuses } : { hosts, statuses, remote: remoteCheckpoint(value.remote) }; } function remoteCheckpoint(value) { if (!isObj(value) || !isObj(value.settled)) invalid("field rpcCheckpoint.remote must be an object"); const settled = {}; for (const [key, turns] of Object.entries(value.settled)) { if (!Array.isArray(turns) || !turns.every((turn) => typeof turn === "string")) invalid("field rpcCheckpoint.remote.settled values must be arrays of strings"); settled[key] = turns; } return value.cursor === undefined ? { settled } : { cursor: str(value, "cursor"), settled }; } function parseMonitor(entry) { if (!isObj(entry)) invalid("a monitor entry must be an object"); return { monitorId: str(entry, "monitorId"), sessionId: str(entry, "sessionId"), description: str(entry, "description"), runtimeKind: oneOf(RUNTIME_KINDS, entry, "runtimeKind"), durabilityClass: oneOf(DURABILITY_CLASSES, entry, "durabilityClass"), command: opt(entry, "command", str), path: opt(entry, "path", str), event: opt(entry, "event", (raw, field) => oneOf(FILE_EVENTS, raw, field)), filter: opt(entry, "filter", str), humanInput: opt(entry, "humanInput", bool), ...(entry.wakesPerHour === undefined ? {} : { wakesPerHour: wakeRate(entry, "wakesPerHour") }), cwd: opt(entry, "cwd", str), approvedParent: opt(entry, "approvedParent", str), target: opt(entry, "target", str), events: entry.events === undefined ? undefined : entry.events === "all" ? "all" : strings(entry, "events"), exclude: entry.exclude === undefined ? undefined : strings(entry, "exclude"), ...(entry.rpcCheckpoint === undefined ? {} : { rpcCheckpoint: rpcCheckpoint(entry.rpcCheckpoint) }), createdAt: num(entry, "createdAt"), expiresAt: entry.expiresAt === undefined || entry.expiresAt === null ? null : num(entry, "expiresAt"), persistent: bool(entry, "persistent"), suspended: bool(entry, "suspended"), lastCheckpoint: entry.lastCheckpoint === null ? null : checkpoint(entry.lastCheckpoint), deliveryPaused: bool(entry, "deliveryPaused"), // Unknown keys are ignored by this field-by-field parse, so a manifest written before // `wakeCount` was dropped still reads back cleanly; no version bump is needed. The same // rule makes `runtime` / `deadlineMs` optional additions that v1 readers skip. fireWindow: fireWindow(entry.fireWindow), ...(entry.runtime === undefined ? {} : { runtime: runtimeIdentity(entry, "runtime") }), ...(entry.deadlineMs === undefined ? {} : { deadlineMs: num(entry, "deadlineMs") }), }; } function parseBackgroundSession(entry) { if (!isObj(entry)) invalid("a background session entry must be an object"); return { id: str(entry, "id"), command: str(entry, "command"), startedAtMs: num(entry, "startedAtMs"), ...(entry.runtime === undefined ? {} : { runtime: runtimeIdentity(entry, "runtime") }), }; } /** Strict fail-closed domain parse; the sidecar store has already checked version and session. */ export function parseTerminalManifest(raw, ref) { if (!isObj(raw)) invalid("the payload must be an object"); if (!Array.isArray(raw.monitors)) invalid("field monitors must be an array"); if (!Array.isArray(raw.backgroundSessions)) invalid("field backgroundSessions must be an array"); return { version: TERMINAL_MANIFEST_VERSION, sessionId: ref.sessionId, monitors: raw.monitors.map(parseMonitor), backgroundSessions: raw.backgroundSessions.map(parseBackgroundSession), updatedAt: num(raw, "updatedAt"), }; } //# sourceMappingURL=terminal-manifest-parse.js.map