UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

58 lines (57 loc) • 3.45 kB
import { placementHonored, placementTargetOf, reportedPlacementTarget } from "../remote-env.js"; import { sliceHeadSafe, sliceTailSafe } from "../surrogate-safe-slice.js"; import { foreignLine } from "../protocol-http-auth.js"; export function mintPlacementFence(row, handedValue) { const fromRow = row !== undefined ? placementTargetOf(row) : undefined; const handed = handedFence(handedValue); const provider = fromRow?.provider ?? handed?.provider; if (provider === undefined) return undefined; const deviceId = fromRow?.deviceId ?? handed?.deviceId; return Object.freeze(deviceId !== undefined ? { provider, deviceId } : { provider }); } function handedFence(value) { if (value === undefined) return undefined; const provider = typeof value === "object" && value !== null ? value.provider : undefined; const deviceId = typeof value === "object" && value !== null ? value.deviceId : undefined; if (typeof provider !== "string" || provider === "" || (deviceId !== undefined && typeof deviceId !== "string")) { throw placementNotHonored("RunInternals.placementTarget cannot be read as a target (it needs a non-empty string `provider`, and a string `deviceId` when it names one) — a fence the engine cannot read cannot be honored; fix the lane that handed it instead of dropping it."); } return Object.freeze(deviceId !== undefined ? { provider, deviceId } : { provider }); } export function settleOwnTarget(env, fence, restoredRow) { const own = reportedPlacementTarget(env); if (!placementHonored(fence, own)) { const refusal = deviceMoveRefusal(restoredRow !== undefined ? "resume.env_failed" : "config.placement_not_honored", fence?.deviceId, own?.deviceId); throw restoredRow !== undefined && devicesDisprove(restoredRow.deviceId, own?.deviceId) ? Object.assign(refusal, { envDisowned: true }) : refusal; } return own; } export function devicesDisprove(rowDevice, envDevice) { return rowDevice !== undefined && envDevice !== undefined && rowDevice !== envDevice; } export function deviceMoveRefusal(code, fenceDevice, reportedDevice) { const fenced = fenceDevice !== undefined ? `device "${renderDeviceId(fenceDevice)}"` : "no device"; const reports = reportedDevice !== undefined ? `device "${renderDeviceId(reportedDevice)}"` : "no device"; return refusal(code, `${code}: refused before it ran — fenced to ${fenced}, but the environment reports ${reports}. Bring the workspace back to that device or open a new workspace for this work. (The factory must mint on the fenced device or throw.)`); } const DEVICE_ID_RENDER_MAX = 48; const DEVICE_ID_FOLD_WINDOW = 4 * DEVICE_ID_RENDER_MAX; const foldDeviceId = (s) => foreignLine(s, 4 * s.length + 8); function renderDeviceId(id) { const folded = id.length <= 2 * DEVICE_ID_FOLD_WINDOW ? foldDeviceId(id) : `${foldDeviceId(sliceHeadSafe(id, DEVICE_ID_FOLD_WINDOW))}…${foldDeviceId(sliceTailSafe(id, DEVICE_ID_FOLD_WINDOW))}`; if (folded.length <= DEVICE_ID_RENDER_MAX) return folded; const keep = DEVICE_ID_RENDER_MAX - 1; const head = Math.ceil(keep / 2); return `${sliceHeadSafe(folded, head)}…${sliceTailSafe(folded, keep - head)}`; } function placementNotHonored(message) { return refusal("config.placement_not_honored", message); } function refusal(code, message) { const e = new Error(message); e.code = code; return e; }