UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

10 lines • 3.63 kB
import{l as resolveGlobalSingleton}from"./string-coerce-BBqv4GXj.mjs";import"./errno-DUBt6SQQ.mjs";import"./plugin-instance-invocation-D2U32-yV.mjs";import{n as parseNodeReleaseVersion}from"./node-version-D5m3z9Di.mjs";import{AsyncLocalStorage}from"node:async_hooks";import{promisify}from"node:util";import fsSync,{readFile,readFileSync}from"node:fs";import path from"node:path";import{createHash}from"node:crypto";import{readdir}from"node:fs/promises";import os from"node:os"; //!src/process/child-memory.ts const rssField=Buffer.from(` VmRSS:`);const swapField=Buffer.from(` VmSwap:`);const pssField=Buffer.from(` Pss:`);const swapPssField=Buffer.from(` SwapPss:`);const nodeVersion=parseNodeReleaseVersion(process.versions.node);const reuseReadBuffer=!process.versions.bun&&nodeVersion&&(nodeVersion.major===24&&nodeVersion.minor>=19||nodeVersion.major===26&&nodeVersion.minor>=4&&nodeVersion.minor<8);const nativeReadFile=promisify(readFile);const owner=resolveGlobalSingleton(Symbol.for(`openclaw.hostExecMemory`),()=>({watches:/* @__PURE__ */ new Set,sampling:false,assessmentPending:false})); //!src/process/linux-exec-cgroup.ts const cgroupMount=`/sys/fs/cgroup`;const controllers=[`cpu`,`io`,`memory`];function prepareAgentExecCgroupScope(e,t){const n=t?.trim();const r=path.join(e,n?`session-${createHash(`sha256`).update(n).digest(`hex`).slice(0,32)}`:`unscoped`);fsSync.mkdirSync(r,{recursive:true});const i=fsSync.readFileSync(path.join(e,`io.max`),`utf8`).trim();for(const e of i.split(` `)){const[t,...n]=e.split(/\s+/u);if(!t)continue;const i=n.map(e=>{const[t,n]=e.split(`=`);const r=Number(n);return`${t}=${r>0&&Number.isFinite(r)?Math.max(1,Math.floor(r/2)):n}`});fsSync.writeFileSync(path.join(r,`io.max`),[t,...i].join(` `))}return r}function resolveGatewayServiceCgroupDirectory(){const e=/^0::(.+)$/mu.exec(fsSync.readFileSync(`/proc/self/cgroup`,`utf8`))?.[1];const t=e&&/^(\/(?:[^/]+\/)*[^/]+\.service)\/(?:gateway|agent-exec(?:\/(?:session-[a-f0-9]{32}|unscoped))?)$/u.exec(e);if(!t)return;const n=path.join(cgroupMount,t[1]);fsSync.accessSync(n,fsSync.constants.W_OK|fsSync.constants.X_OK);fsSync.accessSync(path.join(n,`cgroup.subtree_control`),fsSync.constants.W_OK);fsSync.accessSync(path.join(n,`gateway`,`cgroup.procs`),fsSync.constants.W_OK);const r=new Set(fsSync.readFileSync(path.join(n,`cgroup.controllers`),`utf8`).trim().split(/\s+/u));if(!controllers.every(e=>r.has(e)))throw new Error(`the service has not delegated cpu, io, and memory controllers`);if(fsSync.readFileSync(path.join(n,`cgroup.procs`),`utf8`).trim())throw new Error(`the service root still contains processes; move its launcher into the gateway leaf`);return n}function getAgentExecCgroup(e){if(process.platform!==`linux`)return;try{const t=resolveGatewayServiceCgroupDirectory();if(!t)return;const n=new Set(fsSync.readFileSync(path.join(t,`cgroup.subtree_control`),`utf8`).trim().split(/\s+/u));const r=path.join(t,`agent-exec`);fsSync.accessSync(r,fsSync.constants.W_OK|fsSync.constants.X_OK);fsSync.accessSync(path.join(r,`cgroup.procs`),fsSync.constants.W_OK);const i=fsSync.readFileSync(path.join(r,`io.max`),`utf8`).trim();const a=fsSync.readFileSync(path.join(r,`cpu.weight`),`utf8`).trim();if(controllers.every(e=>n.has(e))&&i&&a===`10`){const t=new Set(fsSync.readFileSync(path.join(r,`cgroup.subtree_control`),`utf8`).trim().split(/\s+/u));if(!controllers.every(e=>t.has(e)))return r;try{return prepareAgentExecCgroupScope(r,e)}catch(e){process.emitWarning(`Session exec IO policy unavailable; using the shared exec budget: ${String(e)}`);return path.join(r,`unscoped`)}}}catch{}}export{getAgentExecCgroup as t};