openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 7.14 kB
JavaScript
import{a as e}from"./openclaw-state-read-record-coerce-D1s3ReAs.mjs";import{M as t,N as n,c as r,n as i,o as a}from"./openclaw-state-read-workspace-path-exclusions-Cjo9Lmns.mjs";import o from"node:path";import{createHash as s}from"node:crypto";const c=5e4,l=25e4,u=67108864,d=4294967296;var WorkspaceResultLimitError=class extends Error{constructor(e){super(`${e} Remove generated dependencies/build output from Git tracking and add it to .gitignore, exclude it from the workspace, or split intentional changes before retrying. Saved workspace files were preserved.`),this.code=`WORKSPACE_RESULT_LIMIT`}};const f=l*2,p=67108864,m=805306368,h=268435456,g=/^sha256:([a-f0-9]{64})$/u,_=/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/u;var UnsafeWorkspaceManifestPathError=class extends Error{constructor(){super(`Worker workspace manifest contains an unsafe path`)}};function manifestPath(e){if(typeof e!=`string`||!e||e.includes(`\\`)||o.posix.isAbsolute(e)||o.posix.normalize(e)!==e||e===`.`||e===`..`||e.startsWith(`../`))throw new UnsafeWorkspaceManifestPathError;return e}function manifestMode(e){if(typeof e!=`number`||!Number.isInteger(e)||e<0||e>511)throw Error(`Worker workspace manifest contains an invalid mode`);return e}function gitFileMode(e){return e&73?493:420}function compareManifestPaths(e,t){return e.path<t.path?-1:+(e.path>t.path)}function createManifestBudget(){let e=0,t=0;return n=>{if(e+=Buffer.byteLength(n.path),t+=n.type===`file`?n.size:n.type===`symlink`?Buffer.byteLength(n.target):0,e>67108864)throw new WorkspaceResultLimitError(`Worker workspace manifest paths exceed their byte limit.`);if(t>4294967296)throw new WorkspaceResultLimitError(`Worker workspace manifest exceeds its eligible byte limit.`)}}function parseRawEntry(t){if(!e(t))throw Error(`Worker workspace manifest contains an invalid entry`);let n=manifestPath(t.path),r=manifestMode(t.mode);if(t.type===`directory`)return{path:n,type:`directory`,mode:r};if(t.type===`file`){if(typeof t.size!=`number`||!Number.isSafeInteger(t.size)||t.size<0||typeof t.sha256!=`string`||!/^[a-f0-9]{64}$/u.test(t.sha256))throw Error(`Worker workspace manifest contains invalid file metadata`);return{path:n,type:`file`,mode:gitFileMode(r),size:t.size,sha256:t.sha256}}if(t.type===`symlink`){if(typeof t.target!=`string`||!t.target||t.target.includes(`\\`)||o.posix.isAbsolute(t.target)||o.win32.parse(t.target).root!==``)throw Error(`Worker workspace manifest contains an unsafe symlink`);let e=`/workspace`,r=o.posix.resolve(o.posix.dirname(`${e}/${n}`),t.target);if(r!==e&&!r.startsWith(`${e}/`))throw Error(`Worker workspace manifest symlink escapes its root`);return{path:n,type:`symlink`,mode:511,target:t.target}}throw Error(`Worker workspace manifest contains an unsupported entry type`)}function validateAndProjectEntries(e,o,s=`current`){if(e.length>25e4)throw new WorkspaceResultLimitError(`Worker workspace manifest has too many entries: ${e.length} files/directories (limit ${l}).`);let c=e.map(parseRawEntry),u=``,d=createManifestBudget(),f=/* @__PURE__ */ new Map;for(let e of c){if(u>=e.path)throw Error(`Worker workspace manifest paths are not unique and sorted`);for(let t of r(e.path))if(f.get(t)?.type!==`directory`)throw Error(`Worker workspace manifest entry has a non-directory parent`);d(e),f.set(e.path,e),u=e.path}let p=n(c),m=i(o),h=s===`stored`?c:a(c,e=>m.has(e)||t(e,p),o!==void 0);return{entries:h.filter(e=>e.type!==`directory`),directories:h.filter(e=>e.type===`directory`).map(e=>e.path)}}function serializeWorkerWorkspaceManifest(e,r=`current`){let o=[...(e.directories??[]).map(e=>({path:e,type:`directory`,mode:448})),...e.entries].toSorted(compareManifestPaths),s=n(o),c=i(e.buildCacheInputs),u=r===`stored`?o:a(o,e=>c.has(e)||t(e,s),e.buildCacheInputs!==void 0);if(u.length>25e4)throw new WorkspaceResultLimitError(`Worker workspace manifest has too many entries: ${u.length} files/directories (limit ${l}).`);let d=createManifestBudget(),f=0,p=Buffer.byteLength(JSON.stringify({version:e.version,baseCommit:e.baseCommit,entries:[]}));for(let e of u)if(f+=Buffer.byteLength(JSON.stringify(e)),d(e),p+f+Math.max(0,u.length-1)>67108864)throw new WorkspaceResultLimitError(`Worker workspace manifest exceeds the 64 MiB safety limit.`);return JSON.stringify({version:e.version,baseCommit:e.baseCommit,entries:u})}function parseWorkerWorkspaceManifest(t,n,r,i=`current`){if(Buffer.byteLength(t)>67108864)throw new WorkspaceResultLimitError(`Worker workspace manifest exceeds the 64 MiB safety limit.`);let a=g.exec(n);if(!a)throw Error(`Worker workspace manifest reference is invalid`);if(s(`sha256`).update(t).digest(`hex`)!==a[1])throw Error(`Worker workspace manifest digest does not match its reference`);let o=JSON.parse(t);if(!e(o))throw Error(`Worker workspace manifest is invalid`);if(o.version!==1||o.baseCommit!==null&&(typeof o.baseCommit!=`string`||!_.test(o.baseCommit))||!Array.isArray(o.entries))throw Error(`Worker workspace manifest has an unsupported shape`);return{version:1,baseCommit:o.baseCommit,...validateAndProjectEntries(o.entries,r,i),...r===void 0?{}:{buildCacheInputs:r}}}function parseJournalEntry(e){let t=parseRawEntry(e);if(t.type===`directory`)throw Error(`Worker workspace reconciliation journal contains a directory entry`);return t}function parseWorkerWorkspaceReconciliationPlan(t){let n=JSON.parse(t);if(!e(n))throw Error(`Worker workspace reconciliation journal is invalid`);let{baseDirectories:r=[],appliedDirectories:i=[]}=n;if(n.version!==1||typeof n.temporaryNonce!=`string`||!/^[a-f0-9]{32}$/u.test(n.temporaryNonce)||typeof n.baseManifestRef!=`string`||!g.test(n.baseManifestRef)||typeof n.currentManifestRef!=`string`||!g.test(n.currentManifestRef)||typeof n.baseTree!=`string`||!/^[a-f0-9]{40}$/u.test(n.baseTree)||typeof n.basePackSha256!=`string`||!/^[a-f0-9]{64}$/u.test(n.basePackSha256)||!Array.isArray(n.baseEntries)||!Array.isArray(n.appliedEntries)||!Array.isArray(r)||!Array.isArray(i)||n.appliedManifestRef!==void 0&&(typeof n.appliedManifestRef!=`string`||!g.test(n.appliedManifestRef))||n.baseEntries.length+n.appliedEntries.length+r.length+i.length>f)throw Error(`Worker workspace reconciliation journal has an unsupported shape`);let a=n.baseEntries.map(parseJournalEntry),o=n.appliedEntries.map(parseJournalEntry),s=r.map(manifestPath),c=i.map(manifestPath);for(let e of[a,o]){let t=e.map(e=>e.path);if(new Set(t).size!==t.length)throw Error(`Worker workspace reconciliation journal has duplicate paths`)}for(let e of[s,c])if(new Set(e).size!==e.length)throw Error(`Worker workspace reconciliation journal has duplicate directories`);return{version:1,temporaryNonce:n.temporaryNonce,baseManifestRef:n.baseManifestRef,currentManifestRef:n.currentManifestRef,baseEntries:a,appliedEntries:o,baseDirectories:s,appliedDirectories:c,appliedManifestRef:n.appliedManifestRef,baseTree:n.baseTree,basePackSha256:n.basePackSha256}}export{UnsafeWorkspaceManifestPathError as a,parseWorkerWorkspaceReconciliationPlan as c,u as d,d as f,m as i,serializeWorkerWorkspaceManifest as l,WorkspaceResultLimitError as m,p as n,gitFileMode as o,c as p,h as r,parseWorkerWorkspaceManifest as s,f as t,l as u};