openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 8.52 kB
JavaScript
import{a as e,i as t,o as n,r,t as i}from"./git-operation.worker-fs-safe-2XqdJR5K.mjs";import{t as a}from"./git-operation.worker-path-guards-B73RPnzx.mjs";import{ir as o}from"./git-operation.worker.js";import{t as s}from"./git-operation.worker-directory-durability-VXyAGg5Z.mjs";import{n as c}from"./git-operation.worker-staged-inputs-DZiTgX91.mjs";import{o as l,s as u,t as d}from"./git-operation.worker-workspace-hash-memo-CMIph4rA.mjs";import{c as f,d as p,f as m,o as h,u as g}from"./git-operation.worker-workspace-manifest-DsRMcvMP.mjs";import{constants as _,fstatSync as v,lstatSync as y,realpathSync as b}from"node:fs";import x from"node:fs/promises";import S from"node:path";import{readFileWindowFully as C}from"@openclaw/fs-safe/advanced";import{createHash as w}from"node:crypto";import{isMainThread as T}from"node:worker_threads";import{setImmediate as E}from"node:timers/promises";var D=/* @__PURE__ */ n({isPortableRootContainedSymlink:()=>isPortableRootContainedSymlink,readActualWorkspaceManifestImpl:()=>readActualWorkspaceManifestImpl,readWorkspaceFileContentsWithLimit:()=>readWorkspaceFileContentsWithLimit,readWorkspaceFileSnapshotWithLimit:()=>readWorkspaceFileSnapshotWithLimit,scanActualWorkspaceManifest:()=>scanActualWorkspaceManifest});function isPortableRootContainedSymlink(e,t,n){if(!n||n.includes(`\\`)||S.posix.isAbsolute(n)||S.win32.parse(n).root!==``)return!1;let r=S.resolve(S.dirname(S.join(e,t)),n);return r===e||r.startsWith(`${e}${S.sep}`)}async function readWorkspaceFileSnapshotWithLimit(e,t,n,r,i){return await readWorkspaceFile({expectedPath:e,maxBytes:t,root:n,signal:r,contents:!1,onIdentity:i})}async function readWorkspaceFileContentsWithLimit(e,t){return await readWorkspaceFile({expectedPath:e,maxBytes:t,contents:!0})}async function readWorkspaceFile(e){let{expectedPath:n,maxBytes:a,root:o,signal:c}=e,f=Date.now();c?.throwIfAborted();let p=await x.open(n,_.O_RDONLY|_.O_NOFOLLOW|_.O_NONBLOCK),m;try{c?.throwIfAborted();let{memo:g,metrics:_,owner:y=`gateway`}=d()??{},b=T?await p.stat({bigint:!0}):v(p.fd,{bigint:!0}),x=await t(p,n);if(!b.isFile()||o&&!r(o,x))throw Error(`Gateway workspace file changed while it was being read`);let S=typeof a==`number`?a:a(Number(b.size));if(b.size>BigInt(S))return{type:`unsupported`};let E=u(y,b),D=l(b.ctimeNs,f),O=e.contents||!D?void 0:g?.get(E),k=Number(b.size);if(O)_&&(_.memoHitCount+=1);else{if(e.contents){if(m=Buffer.allocUnsafe(Number(b.size)+1),k=await C(p,m,0,{signal:c}),k>S)return{type:`unsupported`};O=w(`sha256`).update(m.subarray(0,k)).digest(`hex`)}else try{({bytes:k,digest:O}=await s(p,{maxBytes:S,signal:c}))}catch(e){if(c?.throwIfAborted(),!(e instanceof i)||e.code!==`too-large`)throw e;if(typeof a!=`number`)throw Error(`Gateway workspace file changed while it was being read`,{cause:e});return{type:`unsupported`}}_&&(_.contentHashCount+=1)}let A=T?await p.stat({bigint:!0}):v(p.fd,{bigint:!0});if(c?.throwIfAborted(),A.size!==BigInt(k)||u(y,A)!==E)throw Error(`Gateway workspace file changed while it was being read`);D&&g?.set(E,O),e.onIdentity?.(E,D);let j={type:`file`,mode:h(Number(A.mode&511n)),size:k,sha256:O};return e.contents&&m?{...j,content:m.subarray(0,k)}:j}finally{await p.close()}}async function scanActualWorkspaceManifest(t){let n=Date.now();t.signal?.throwIfAborted();let r,i;try{r=await x.realpath(t.root),i=c(await e(r))}catch(e){throw t.signal?.throwIfAborted(),e}let s=g(t.buildCacheInputs),f=t.gitIgnore?.paths,isExcluded=async e=>{let n=s.has(e)||await i(e);return p(e,n,t.buildCacheInputs!==void 0,t.legacyDerivedEligibilitySubset)?!0:!f?.size||n||t.gitIgnore?.retainedPaths.has(e)?!1:m(e,f)},_=0,v=0,C=0,w=0,D=0,addBytes=e=>{if(v+=e,v>4294967296)throw Error(`Gateway workspace manifest exceeds its eligible byte limit`)},addEntry=(e,n=0,r,i)=>{if(addBytes(n),C+=Buffer.byteLength(e.path),C>67108864)throw Error(`Gateway workspace manifest paths exceed their byte limit`);if(_+=1,_>25e4)throw Error(`Gateway workspace manifest has too many entries`);t.onEntry(e,r,i)},O=new AbortController,k=t.signal?AbortSignal.any([t.signal,O.signal]):O.signal,checkTraversal=e=>{if(k.throwIfAborted(),w+=1,D+=Buffer.byteLength(e),w>25e4)throw Error(`Gateway workspace manifest has too many entries`);if(D>67108864)throw Error(`Gateway workspace manifest paths exceed their byte limit`)},A=[],j=d(),M=0,N,P=T?e=>x.lstat(e,{bigint:!0}):async e=>{N&&await N,k.throwIfAborted();let t=y(e,{bigint:!0});return++M%32==0&&(N=E().then(()=>{N=void 0})),t},F=!1,runScans=async(e,n,r)=>{let i=e,a=await o({tasks:Array.from({length:Math.min(4,n-e)},()=>async()=>{for(;i<n&&!k.aborted;)await r(i++)}),limit:4,errorMode:`stop`,onTaskError:e=>{O.signal.aborted||(F=!t.signal?.aborted),O.abort(e)}});if(a.hasError)throw F||t.signal?.throwIfAborted(),a.firstError;k.throwIfAborted()},addFile=async e=>{let t,n,i=await readWorkspaceFileSnapshotWithLimit(S.join(r,e),e=>(addBytes(e),e),r,k,(e,r)=>{t=e,n=r});if(i.type===`file`)addEntry({path:e,...i},0,t,n);else throw Error(`Gateway workspace manifest exceeds its eligible byte limit`)},addLeaf=async(e,i,a)=>{if(a.isSymbolicLink()){let t=await x.readlink(i);if(!isPortableRootContainedSymlink(r,e,t))return!1;addEntry({path:e,type:`symlink`,mode:511,target:t},Buffer.byteLength(t))}else if(a.isFile()){let r=u(j?.owner??`gateway`,a),o=t.reuseFile?.(e,r,a),s=o?.sha256??(l(a.ctimeNs,n)?j?.memo.get(r):void 0);if(s){if(!o&&b.native(i)!==i)throw Error(`Gateway workspace file changed while it was being read`);addEntry(o??{path:e,type:`file`,mode:h(Number(a.mode&511n)),size:Number(a.size),sha256:s},Number(a.size),r),j?.metrics&&(j.metrics.memoHitCount+=1)}else t.unchangedProbe?await addFile(e):A.push(e)}else return!1;return!0},addIncludedPath=async(e,n,i)=>{if(await isExcluded(e))return`derived-only`;checkTraversal(e);let o=S.join(r,e),s=await P(o).catch(e=>{if(!(a(e,`ENOENT`)||a(e,`ENOTDIR`)))throw e});if(!s)return`absent`;if(s.isDirectory()&&!s.isSymbolicLink()){if(t.btrfsSnapshot&&s.ino===2n)throw Object.assign(/* @__PURE__ */ Error(`Btrfs snapshot omitted a nested subvolume`),{code:`WORKSPACE_SNAPSHOT_INCOMPLETE`});if(t.preserveDirectories?.has(e)||n.has(e))return addEntry({path:e,type:`directory`}),`included`;for await(let t of await x.opendir(o)){k.throwIfAborted();let n=`${e}/${t.name}`;if(await isExcluded(n)||i.has(n))return`derived-only`}return addEntry({path:e,type:`directory`}),`included`}return await addLeaf(e,o,s)?`included`:`absent`},walk=async e=>{let n=e?S.join(r,e):r,i=!1,a=!1,o=[],scanBatch=async()=>{let e=[];await runScans(0,o.length,async n=>{let s=o[n];if(await isExcluded(s)){i=!0;return}let c=S.join(r,s),l=await P(c);if(l.isDirectory()&&!l.isSymbolicLink()){if(t.btrfsSnapshot&&l.ino===2n)throw Object.assign(/* @__PURE__ */ Error(`Btrfs snapshot omitted a nested subvolume`),{code:`WORKSPACE_SNAPSHOT_INCOMPLETE`});e[n]=s}else a=!0,await addLeaf(s,c,l)});for(let n of e){if(n===void 0)continue;let e=await walk(n);e.included||t.preserveDirectories?.has(n)?(addEntry({path:n,type:`directory`}),a=!0):i||=e.hasDerivedEntry}o.length=0};for await(let t of await x.opendir(n)){let n=t.name,r=e?`${e}/${n}`:n;checkTraversal(r),(e||n!==`.git`)&&(o.push(r),o.length===32&&await scanBatch())}return o.length&&await scanBatch(),{hasDerivedEntry:i,included:a||!i}};if(t.includePaths){let e=/* @__PURE__ */ new Set,n=/* @__PURE__ */ new Set,r=Array.from(t.includePaths,e=>({relative:e,depth:e.split(`/`).length})).toSorted((e,t)=>t.depth-e.depth||e.relative.localeCompare(t.relative));for(let t=0;t<r.length;){let i=t+1;for(;i<r.length&&r[i].depth===r[t].depth;)i++;await runScans(t,i,async t=>{let{relative:i}=r[t],a=await addIncludedPath(i,e,n);a===`included`?e.add(S.posix.dirname(i)):a===`derived-only`&&n.add(i)}),t=i}}else await runScans(0,1,async()=>{await walk(``)});await runScans(0,A.length,e=>addFile(A[e])),k.throwIfAborted()}async function readActualWorkspaceManifestImpl(e,t,n){let r=[],i=[];await scanActualWorkspaceManifest({...e,reuseFile:t,onEntry(e,t,a){e.type===`directory`?i.push(e.path):r.push(e),t!==void 0&&n?.(e.path,t,a)}});let a={version:1,baseCommit:e.baseCommit,...e.buildCacheInputs===void 0?{}:{buildCacheInputs:[...g(e.buildCacheInputs)]},entries:r.toSorted((e,t)=>e.path.localeCompare(t.path)),directories:i.toSorted((e,t)=>e.localeCompare(t))},o=f(a);return{manifestRef:`sha256:${w(`sha256`).update(o).digest(`hex`)}`,manifest:a,rawManifest:o}}export{D as a,scanActualWorkspaceManifest as i,readActualWorkspaceManifestImpl as n,readWorkspaceFileContentsWithLimit as r,isPortableRootContainedSymlink as t};