UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 5 kB
import{xt as e}from"./openclaw-state-read-subsystem-Dwoo5bcz.mjs";import{a as t}from"./openclaw-state-read-sqlite-schema-facts-Dtolu7-X.mjs";import{a as n,r}from"./openclaw-state-read-kysely-sync-Bcu0Qb7F.mjs";import{o as i}from"./openclaw-state-read-openclaw-state-db-schema-helpers-QFZd8aJI.mjs";import{t as a}from"./openclaw-state-read-state-lease-process-owner-CQOeyOb9.mjs";import"./openclaw-state-read-registry.kernel-DxBhY13A.mjs";import{isDeepStrictEqual as o}from"node:util";function dockerPersistentExecContainerName(e){if(!/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/u.test(e))throw Error(`Invalid sandbox stream attempt identity; custody retained.`);return`openclaw-stream-${e}`}function parseDockerPersistentExecTarget(t){if(!e(t)||t.kind!==`docker`||typeof t.endpoint!=`string`||!t.endpoint.startsWith(`unix:///`)||t.endpoint.length>4096||t.endpoint.includes(`\0`)||typeof t.daemonId!=`string`||!/^[a-zA-Z0-9][a-zA-Z0-9:._-]{0,255}$/u.test(t.daemonId))throw Error(`Invalid persistent sandbox Docker target; custody must be retained.`);return{kind:`docker`,endpoint:t.endpoint,daemonId:t.daemonId}}function parseDockerPersistentExecReceipt(t){if(!e(t)||t.version!==1||typeof t.containerId!=`string`||!/^[a-f0-9]{64}$/u.test(t.containerId)||typeof t.parentContainerId!=`string`||!/^[a-f0-9]{64}$/u.test(t.parentContainerId)||t.containerId===t.parentContainerId||typeof t.attemptId!=`string`)throw Error(`Invalid persistent sandbox stop receipt; custody must be retained.`);return dockerPersistentExecContainerName(t.attemptId),{version:1,target:parseDockerPersistentExecTarget(t.target),containerId:t.containerId,parentContainerId:t.parentContainerId,attemptId:t.attemptId}}const s=[`reserved`,`prepared`,`running`,`stopping`,`uncertain`,`stopped`];function boundedText(e,t=4096){return typeof e==`string`&&e.length>0&&e.length<=t&&!e.includes(`\0`)}function invalidCustody(){throw Error(`Invalid persistent sandbox custody; retain the record for recovery.`)}function parseWorkspaceBinding(t){return(!e(t)||!boundedText(t.workspaceDir)||t.workspaceSource!==null&&t.workspaceSource!==`managed-worktree`||typeof t.registryCreatedAtMs!=`number`||!Number.isSafeInteger(t.registryCreatedAtMs)||t.registryCreatedAtMs<0)&&invalidCustody(),{workspaceDir:t.workspaceDir,workspaceSource:t.workspaceSource,registryCreatedAtMs:t.registryCreatedAtMs}}function validateReservation(e){(!boundedText(e.ownerKey)||!boundedText(e.sourceIdentity)||!boundedText(e.attemptId,128)||e.backendId!==`docker`||!/^[a-f0-9]{64}$/u.test(e.containerId)||!boundedText(e.containerName,256)||!boundedText(e.containerStartedAt,128)||!Number.isFinite(Date.parse(e.containerStartedAt)))&&invalidCustody(),dockerPersistentExecContainerName(e.attemptId),parseDockerPersistentExecTarget(e.backendTarget)}function decode(t){let n=JSON.parse(t.owner_json),r=a(t.owner_json),i=e(n)&&e(n.owner)?n.owner:null,o=s.find(e=>e===t.phase);(!i||!r||!boundedText(i.token,128)||i.namespace!==null&&!boundedText(i.namespace)||!o||!Number.isSafeInteger(t.revision)||t.revision<1||!Number.isSafeInteger(t.created_at_ms)||t.created_at_ms<0||!Number.isSafeInteger(t.updated_at_ms)||t.updated_at_ms<0||t.error!==null&&(typeof t.error!=`string`||t.error.length>2048)||t.backend_id!==`docker`)&&invalidCustody();let c={ownerKey:t.owner_key,sourceIdentity:t.source_identity,attemptId:t.attempt_id,backendId:t.backend_id,backendTarget:parseDockerPersistentExecTarget(JSON.parse(t.backend_target_json)),workspaceBinding:parseWorkspaceBinding(JSON.parse(t.workspace_binding_json)),containerId:t.container_id,containerName:t.container_name,containerStartedAt:t.container_started_at,owner:{...r,token:i.token,namespace:i.namespace},revision:t.revision,phase:o,receipt:t.receipt_json===null?null:parseDockerPersistentExecReceipt(JSON.parse(t.receipt_json)),error:t.error,createdAtMs:t.created_at_ms,updatedAtMs:t.updated_at_ms};return validateReservation(c),assertReceiptBinding(c,c.receipt),(o===`prepared`||o===`running`)&&!c.receipt&&invalidCustody(),c}function assertReceiptBinding(e,t){if(t&&(t.parentContainerId!==e.containerId||t.attemptId!==e.attemptId||!o(t.target,e.backendTarget)))throw Error(`Persistent sandbox stop receipt changed its reserved target.`)}function readSandboxPersistentExecsInDatabase(e,a){if(!t(e))throw Error(`Persistent sandbox custody requires admitted schema facts.`);if(!i(e,`sandbox_persistent_execs`))return[];let o=n(e).selectFrom(`sandbox_persistent_execs`).selectAll();return a.ownerKey!==void 0&&(o=o.where(`owner_key`,`=`,a.ownerKey)),a.sourceIdentity!==void 0&&(o=o.where(`source_identity`,`=`,a.sourceIdentity)),a.containerId!==void 0&&(o=o.where(`container_id`,`=`,a.containerId)),a.containerName!==void 0&&(o=o.where(`container_name`,`=`,a.containerName)),r(e,o.orderBy(`owner_key`)).rows.map(decode)}const c={"sandboxPersistentExec.list":(e,t)=>({type:`sandboxPersistentExec.list`,records:readSandboxPersistentExecsInDatabase(t,e)})};export{c as sandboxPersistentExecReadOperations};