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