UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

6 lines • 3.12 kB
import{h as e}from"./service-child-group-anchor-error-coercion-BOmGaARv.mjs";import{i as t}from"./service-child-group-anchor-linux-oom-score-spawn-CsK-1L0B.mjs";import"./service-child-group-anchor-errno-CsW5NI8i.mjs";import n,{readFile as r}from"node:fs";import{createHash as i}from"node:crypto";import a from"node:path";import"node:async_hooks";import"node:os";import{promisify as o}from"node:util";Buffer.from(` VmRSS:`),Buffer.from(` VmSwap:`),Buffer.from(` Pss:`),Buffer.from(` SwapPss:`);const s=t(process.versions.node);!process.versions.bun&&s&&(s.major===24&&s.minor>=19||s.major===26&&s.minor>=4&&s.minor),o(r),e(Symbol.for(`openclaw.hostExecMemory`),()=>({watches:/* @__PURE__ */ new Set,sampling:!1,assessmentPending:!1}));const c=[`cpu`,`io`,`memory`];function prepareAgentExecCgroupScope(e,t){let r=t?.trim(),o=a.join(e,r?`session-${i(`sha256`).update(r).digest(`hex`).slice(0,32)}`:`unscoped`);n.mkdirSync(o,{recursive:!0});let s=n.readFileSync(a.join(e,`io.max`),`utf8`).trim();for(let e of s.split(` `)){let[t,...r]=e.split(/\s+/u);if(!t)continue;let i=r.map(e=>{let[t,n]=e.split(`=`),r=Number(n);return`${t}=${r>0&&Number.isFinite(r)?Math.max(1,Math.floor(r/2)):n}`});n.writeFileSync(a.join(o,`io.max`),[t,...i].join(` `))}return o}function resolveGatewayServiceCgroupDirectory(){let e=/^0::(.+)$/mu.exec(n.readFileSync(`/proc/self/cgroup`,`utf8`))?.[1],t=e&&/^(\/(?:[^/]+\/)*[^/]+\.service)\/(?:gateway|agent-exec(?:\/(?:session-[a-f0-9]{32}|unscoped))?)$/u.exec(e);if(!t)return;let r=a.join(`/sys/fs/cgroup`,t[1]);n.accessSync(r,n.constants.W_OK|n.constants.X_OK),n.accessSync(a.join(r,`cgroup.subtree_control`),n.constants.W_OK),n.accessSync(a.join(r,`gateway`,`cgroup.procs`),n.constants.W_OK);let i=new Set(n.readFileSync(a.join(r,`cgroup.controllers`),`utf8`).trim().split(/\s+/u));if(!c.every(e=>i.has(e)))throw Error(`the service has not delegated cpu, io, and memory controllers`);if(n.readFileSync(a.join(r,`cgroup.procs`),`utf8`).trim())throw Error(`the service root still contains processes; move its launcher into the gateway leaf`);return r}function getAgentExecCgroup(e){if(process.platform===`linux`)try{let t=resolveGatewayServiceCgroupDirectory();if(!t)return;let r=new Set(n.readFileSync(a.join(t,`cgroup.subtree_control`),`utf8`).trim().split(/\s+/u)),i=a.join(t,`agent-exec`);n.accessSync(i,n.constants.W_OK|n.constants.X_OK),n.accessSync(a.join(i,`cgroup.procs`),n.constants.W_OK);let o=n.readFileSync(a.join(i,`io.max`),`utf8`).trim(),s=n.readFileSync(a.join(i,`cpu.weight`),`utf8`).trim();if(c.every(e=>r.has(e))&&o&&s===`10`){let t=new Set(n.readFileSync(a.join(i,`cgroup.subtree_control`),`utf8`).trim().split(/\s+/u));if(!c.every(e=>t.has(e)))return i;try{return prepareAgentExecCgroupScope(i,e)}catch(e){return process.emitWarning(`Session exec IO policy unavailable; using the shared exec budget: ${String(e)}`),a.join(i,`unscoped`)}}}catch{}}function enterAgentExecCgroup(e){let t=getAgentExecCgroup(e);if(t)try{n.writeFileSync(a.join(t,`cgroup.procs`),`0`)}catch(e){process.emitWarning(`Agent exec cgroup placement failed: ${String(e)}`)}}export{enterAgentExecCgroup as t};