openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 2.77 kB
JavaScript
import{t as resolveZstdCodec}from"./zstd-codec-DUOHW191.mjs";import{n as MAX_WORKSPACE_INVENTORY_ENTRIES}from"./workspace-inventory-limits-BmPI7yuU.mjs";import{m as workspaceCtimeIsReusable}from"./workspace-hash-memo-BIIzDhGx.mjs";
//!src/gateway/worker-environments/local-workspace-fingerprints.ts
const HEADER_BYTES=45;const FILE_BYTES=16;const FORMAT_VERSION=1;const RACY_CTIME_NS=-1n;function filePaths(e){const t=[];for(const{node:n}of e.values())if(n.type===`file`)t.push(n.path);t.sort();return t}function prepareLocalWorkspaceFingerprints(e,t,n,r,i){const sourceDevice=e=>i&&e===i.snapshotDev?i.sourceDev:e;const a=n===null||Buffer.isBuffer(n)?n:Buffer.from(n.buffer,n.byteOffset,n.byteLength);const o=filePaths(e);let s;if(n)try{const e=resolveZstdCodec().decompress(n,HEADER_BYTES+MAX_WORKSPACE_INVENTORY_ENTRIES*FILE_BYTES);if(e.length===HEADER_BYTES+o.length*FILE_BYTES&&e.readUInt8(0)===FORMAT_VERSION&&e.subarray(9,41).equals(Buffer.from(t.slice(7),`hex`))&&e.readUInt32LE(41)===o.length)s=e}catch{}const c=/* @__PURE__ */ new Map;for(let e=0;e<o.length;e++)c.set(o[e],HEADER_BYTES+e*FILE_BYTES);const l=s?.readBigUInt64LE(1);const u=/* @__PURE__ */ new Map;const d=/* @__PURE__ */ new Map;let f=s!==void 0;let p=0;return{reuseFile(t,n,i){const a=c.get(t);const o=e.get(t)?.node;if(!s||a===void 0||o?.type!==`file`){f=false;return}if(sourceDevice(i.dev)===l&&i.ino===s.readBigUInt64LE(a)&&i.ctimeNs===s.readBigInt64LE(a+8)&&o.size===Number(i.size)&&workspaceCtimeIsReusable(i.ctimeNs,r)){u.set(t,n);return o}f=false},observe(e,t,n){if(u.get(e)===t)p++;else{f=false;u.delete(e);d.set(e,{identity:t,reusable:n})}},encode(e,n,i){if(i&&!s)return null;if(n===t&&f&&p===o.length)return a;let m=o;if(n!==t){const t=o.filter(t=>e.get(t)?.node.type===`file`);const n=[];for(const{node:t}of e.values())if(t.type===`file`&&!c.has(t.path))n.push(t.path);n.sort();m=[];let r=0;for(const e of t){while(r<n.length&&n[r]<e)m.push(n[r++]);m.push(e)}while(r<n.length)m.push(n[r++])}const h=Buffer.alloc(HEADER_BYTES+m.length*FILE_BYTES);h.writeUInt8(FORMAT_VERSION,0);Buffer.from(n.slice(7),`hex`).copy(h,9);h.writeUInt32LE(m.length,41);let g=i?l:void 0;for(let e=0;e<m.length;e++){const t=m[e];const n=HEADER_BYTES+e*FILE_BYTES;if(u.has(t)||i&&c.has(t)&&!i.has(t)){const e=c.get(t);g??=l;if(g!==l)return null;s.copy(h,n,e,e+FILE_BYTES);continue}const a=d.get(t);if(!a){if(i){h.writeBigInt64LE(RACY_CTIME_NS,n+8);continue}return null}const o=a.identity.split(`:`);const f=sourceDevice(BigInt(o[1]));g??=f;if(f!==g)return null;const p=BigInt(o[5]);h.writeBigUInt64LE(BigInt(o[2]),n);h.writeBigInt64LE(a.reusable??workspaceCtimeIsReusable(p,r)?p:RACY_CTIME_NS,n+8)}h.writeBigUInt64LE(g??0n,1);return resolveZstdCodec().compress(h)}}}export{prepareLocalWorkspaceFingerprints as t};