openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
2 lines • 7.17 kB
JavaScript
import{t as e}from"./fs-safe-copy.worker-errno-CsW5NI8i.mjs";import{n as t}from"./fs-safe-copy.worker-worker-native-error-CjDMMt9F.mjs";import{n}from"./fs-safe-copy.worker-path-existence-DvAA8vT7.mjs";import"./fs-safe-copy.worker-path-guards-CHyQsMxp.mjs";import{n as r}from"./fs-safe-copy.worker-state-owner-path-B5SH78vP.mjs";import"./fs-safe-copy.worker-fs-safe-filesystem-worker-BMcghtFh.mjs";import{i,t as a}from"./fs-safe-copy.worker-fs-safe-BFxtX2RT.mjs";import{E as o,O as s}from"./fs-safe-copy.worker-redact-BQxLxSX2.mjs";import"./fs-safe-copy.worker-number-coercion-1FVMw2ic.mjs";import{a as c}from"./fs-safe-copy.worker-home-dir-CeEbrKea.mjs";import"./fs-safe-copy.worker-utils-C58aD3xL.mjs";import{t as l}from"./fs-safe-copy.worker-workspace-state-identity-D0mApcp7.mjs";import{t as u}from"./fs-safe-copy.worker-subsystem-CSYDX9_J.mjs";import"./fs-safe-copy.worker-sqlite-database-admission-CoQADBJP.mjs";import{t as d}from"./fs-safe-copy.worker-directory-durability-CoFeWTxf.mjs";import"./fs-safe-copy.worker-sqlite-integrity-wA_pGRsW.mjs";import{t as f}from"./fs-safe-copy.worker-workspace-state-guard-BpfbQ11b.mjs";import{t as p}from"./fs-safe-copy.worker-retry-BGpYCcSw.mjs";import m,{closeSync as h,lstatSync as g}from"node:fs";import _ from"node:fs/promises";import v from"node:path";import"node:worker_threads";import"@openclaw/fs-safe/config";import"@openclaw/fs-safe/advanced";import"node:crypto";import"minimatch";const y=[`AGENTS.md`,`SOUL.md`,`IDENTITY.md`,`USER.md`],b=2097152;async function readWorkspaceBootstrapFile(e){return(await s(e,b)).toString(`utf-8`)}u(`workspace`);const x=/* @__PURE__ */ new Set([`EAGAIN`,`EWOULDBLOCK`,`EINTR`]),S=/* @__PURE__ */ new Set([-11,-4]),C=/Unknown system error -(?:11|4)\b/i;function retryWorkspaceRead(e){return p(e,{attempts:3,minDelayMs:50,maxDelayMs:50,shouldRetry:e=>isTransientWorkspaceReadError(e)})}async function readLocalWorkspaceFileWithGuards(e){try{return await retryWorkspaceRead(async()=>{let t=await o({absolutePath:e.filePath,rootPath:e.workspaceDir,boundaryLabel:`workspace root`,symlinks:e.rejectAliases?`reject`:`follow-parents-within-root`});if(!t.ok){if(isTransientWorkspaceReadError(t.error))throw t.error;return t}let n=[t.path,t.exactIdentity];try{return{ok:!0,content:await readWorkspaceBootstrapFile(t.fd),sourceIdentity:n}}finally{m.closeSync(t.fd)}})}catch(e){return{ok:!1,reason:e instanceof RangeError?`validation`:`io`,error:e}}}function isTransientWorkspaceReadError(e){return e&&typeof e==`object`&&(`code`in e&&typeof e.code==`string`&&x.has(e.code)||`errno`in e&&typeof e.errno==`number`&&S.has(e.errno))?!0:e instanceof Error&&C.test(e.message)}const w=/* @__PURE__ */ new Set;function resetLegacyWorkspaceStateCheckForTest(){w.clear()}process.env.VITEST&&(globalThis[Symbol.for(`openclaw.workspaceLegacyStateTestApi`)]={resetLegacyWorkspaceStateCheckForTest});const T=u(`sandbox-workspace`);async function removeLegacyState(t,n){let r={type:`workspace-legacy-reset`,removedPaths:[],warnings:[]};for(let a of t.plan.candidates){let{root:o,sourcePath:s}=a;try{if(g(o.workspaceDir),`error`in o){r.warnings.push({sourcePath:s,action:`inspect`,error:o.error});continue}}catch(t){e(t,`ENOENT`)||r.warnings.push({sourcePath:s,action:`inspect`,error:String(t)});continue}let c=!1;try{let e=v.relative(o.workspaceDir,s),l=await i(o.canonical,{hardlinks:`reject`,maxBytes:2048,symlinks:`reject`});if(!await l.exists(e)||a.requireAttestationHeader&&(await l.read(e)).buffer.subarray(0,34).toString(`utf8`)!==`openclaw-workspace-attestation:v1
`)continue;t.dryRun?f(o):await l.remove(e,{assertBeforeMutation:()=>{f(o);try{n()}catch(e){throw c=!0,e}}}),r.removedPaths.push(s)}catch(e){r.warnings.push({sourcePath:s,action:`remove`,error:String(e),authorityRefused:c})}}return r}async function executeWorkspaceFilesystemOperation(e,s){try{let{operation:t,filesystem:l}=e;if(t.kind===`legacy-reset`)return await removeLegacyState(t,s);let u=l.find(e=>e.workspaceDir===v.resolve(c(t.dir)));if(!u)throw Error(`Workspace filesystem effect has no original target`);let assertCurrent=()=>{for(let e of l)f(e);e.stateOwnerPath&&r(e.stateOwnerPath),s()},p=u.canonical,m=!1,publish=async(e,t,r=!1)=>{if(await n(v.join(p,e),`ENOENT`))return assertCurrent(),!1;let o=await i(p),s=typeof t==`string`?t:Buffer.from(t);try{return r?await o.write(e,s,{overwrite:!1,assertBeforeMutation:assertCurrent}):await o.create(e,s,{atomic:!0,durable:`file`,mkdir:!1,mode:384,assertBeforeMutation:assertCurrent}),!0}catch(e){if(e instanceof a&&e.code===`already-exists`&&!e.details)return!1;throw!r&&d(e instanceof a?e.cause:e)?Error(`Workspace filesystem does not support atomic bootstrap publication. Use a workspace on a filesystem with hard-link support.`,{cause:e}):e}};switch(t.kind){case`mkdir`:assertCurrent(),await _.mkdir(p,{recursive:!0});break;case`publish`:m=await publish(t.name,t.content);break;case`seed`:case`purpose`:if(m=await publish(t.name,t.content,t.kind===`seed`),!m){let e=await readLocalWorkspaceFileWithGuards({filePath:v.join(p,t.name),workspaceDir:p});if(assertCurrent(),!e.ok||!Buffer.from(e.content,`utf8`).equals(Buffer.from(t.content)))throw Object.assign(/* @__PURE__ */ Error(t.kind===`purpose`?`Existing AGENTS.md was preserved. Choose a new workspace to seed the approved custom purpose.`:e.ok?`Existing BOOTSTRAP.md differs from the consented Template bootstrap.`:`Existing BOOTSTRAP.md could not be read safely.`),{code:`WORKSPACE_BOOTSTRAP_SEED_CONFLICT`})}break;case`remove`:await(await i(p)).remove(t.name,{force:!0,assertBeforeMutation:assertCurrent});break;case`copy`:assertCurrent(),await _.mkdir(p,{recursive:!0});for(let e of l)f(e);if(t.seed)for(let e of[...y,`BOOTSTRAP.md`]){if(await _.access(v.join(p,e)).then(()=>!0,()=>!1))continue;let n=await o({absolutePath:v.join(t.seed,e),rootPath:t.seed,boundaryLabel:`sandbox seed workspace`});if(!n.ok)continue;let r;try{r=await readWorkspaceBootstrapFile(n.fd)}catch(n){if(n instanceof RangeError){T.warn(`Ignoring oversized sandbox seed file ${v.join(t.seed,e)}: file exceeds the ${b}-byte limit`);continue}throw n}finally{h(n.fd)}await publish(e,r)}break;case`git-init`:{let{getProcessSupervisor:e}=await import(`./fs-safe-copy.worker-supervisor-DDdlgz46.mjs`),t;try{let n=await e().spawn({mode:`child`,argv:[`git`,`init`],cwd:p,timeoutMs:1e4,beforeSpawn:()=>{try{assertCurrent()}catch(e){throw t??={error:e},e}}});try{await n.wait()}finally{await n.waitForExtinction?.()}}catch{}if(t)throw t.error;break}}for(let e of l)f(e);return{type:`workspace`,created:m,filesystem:l}}catch(e){return{type:`workspace-failed`,error:t(e),...e instanceof a?{fsCode:e.code,details:e.details}:{},...e instanceof l?{alias:{aliasPath:e.aliasPath,storedWorkspacePath:e.storedWorkspacePath,currentWorkspacePath:e.currentWorkspacePath}}:{}}}}async function executeWorkspaceFilesystemRead(e){let n=await readLocalWorkspaceFileWithGuards(e.input);return n.ok?{type:`workspace-read`,result:n}:{type:`workspace-read`,result:{ok:!1,reason:n.reason,...n.error===void 0?{}:{error:t(n.error)},...n.error instanceof a?{fsCode:n.error.code,details:n.error.details}:{}}}}export{executeWorkspaceFilesystemOperation,executeWorkspaceFilesystemRead};