UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 12.8 kB
import{i as e}from"./openclaw-state-read-map-size-DJbMcZYM.mjs";import{t}from"./openclaw-state-read-container-environment-Bx3VJYay.mjs";import{a as n,c as r}from"./openclaw-state-read-pid-alive-_-Y1IvGj.mjs";import{n as i}from"./openclaw-state-read-runtime-process-entrypoints-ItwYqjQs.mjs";import{n as a,r as o,t as s}from"./openclaw-state-read-runtime-binary-Dgxk5ZBN.mjs";import{t as c}from"./openclaw-state-read-usingCtx-03vOGQ3v.mjs";import{a as l,c as u,i as d,l as f,n as p,r as m,s as h,t as g}from"./openclaw-state-read-gateway-process-argv-CwtqUewA.mjs";import{s as _}from"./openclaw-state-read-service-child-group-ownership-CynTG6K2.mjs";import{r as v}from"./openclaw-state-read-windows-install-roots-nshDFN0K.mjs";import{t as y}from"./openclaw-state-read-darwin-process-command-CcgIy_8D.mjs";import b from"node:path";import{fileURLToPath as x,pathToFileURL as S}from"node:url";import{spawnSync as C}from"node:child_process";import w from"node:os";import{ProcSafeError as T}from"@openclaw/proc-safe/errors";import{readProcessAncestry as E,readProcessIdentity as D}from"@openclaw/proc-safe/identity";import{listProcesses as O}from"@openclaw/proc-safe/inspect";import{PinnedProcess as k}from"@openclaw/proc-safe/windows-job";function parseWindowsNativeArguments(e){let t=[],n=0;for(;n<e.length;){for(;/[ \t]/.test(e.charAt(n));)n++;if(n===e.length)break;let r=``,i=!1;for(;n<e.length;){let t=0;for(;e[n]===`\\`;)t++,n++;if(e[n]===`"`)r+=`\\`.repeat(Math.floor(t/2)),t%2==1?(r+=`"`,n++):i&&e[n+1]===`"`?(r+=`"`,n+=2):(i=!i,n++);else{if(r+=`\\`.repeat(t),n===e.length||!i&&/[ \t]/.test(e.charAt(n)))break;r+=e.charAt(n++)}}t.push(r)}return t}function parseWindowsNativeCommandLine(e){if(e.includes(`\0`))return null;let t=0;for(;/[ \t]/.test(e.charAt(t));)t++;if(t===e.length)return[];let n=``,r=!1;for(;t<e.length&&(r||!/[ \t]/.test(e.charAt(t)));){let i=e.charAt(t++);i===`"`?r=!r:n+=i}return r||!n?null:[n,...parseWindowsNativeArguments(e.slice(t))]}let A;function isRosettaTranslatedProcess(){return process.platform!==`darwin`||process.arch!==`x64`?!1:(A??=w.cpus()[0]?.model.includes(`Apple`)===!0,A)}function readWindowsProcessCensus(e){return O({timeoutMs:e,includeCommand:!0}).flatMap(({pid:e,owner:t,identity:n,command:r})=>{if(e===0||e===4||n?.exited)return[];let i=r?.executable;if(!r&&n)try{try{var a=c();i=a.u(k.open(e,{access:`observe`,expect:{startTimeMicros:n.startTimeMicros}}))?.imagePath}catch(e){a.e=e}finally{a.d()}}catch{}let o;if(t===`unknown`&&!n&&!r)try{D(e)}catch(e){e instanceof T&&e.code===`access-denied`&&(o=!0)}return[{pid:e,...n?{parentPid:n.parentPid,startIdentity:String(Math.floor(n.startTimeMicros/1e3))}:{},...r?.commandLine===void 0?{}:{commandLine:r.commandLine},...i?{executable:i}:{},...r?.cwd===void 0?{}:{cwd:r.cwd},...t===`different`?{foreignOwner:!0}:{},...o?{accessDenied:o}:{}}]})}function readWindowsUserSid(){if(process.platform===`win32`)try{let e=C(v(),[`-NoProfile`,`-NonInteractive`,`-Command`,`[Security.Principal.WindowsIdentity]::GetCurrent().User.Value`],{encoding:`utf8`,windowsHide:!0,timeout:5e3}),t=e.stdout?.trim();return!e.error&&e.status===0&&/^S-1-\d+(?:-\d+)+$/.test(t)?t:void 0}catch{return}}const j=Object.values(i).flatMap(e=>[b.posix.normalize(`src/infra/${e.sourceWorkerName}.ts`),`dist/${e.distWorkerPath}`]);function isStandardSystemdUserManager(e){let t=e&&`argv`in e?e.argv:[];return t.length<=3&&t[1]===`--user`&&t.slice(2).every(e=>/^--deserialize=\d+$/u.test(e))&&(t[0]===`/usr/lib/systemd/systemd`||t[0]===`/lib/systemd/systemd`)}function isStandardSystemdLoginProcess(e,t,n){return process.platform!==`linux`||n===void 0||e?.uid!==n||!(`argv`in e)?!1:isStandardSystemdUserManager(e)||e.argv.length===1&&e.argv[0]===`(sd-pam)`&&t?.uid===e.uid&&isStandardSystemdUserManager(t)}const M=[i.spawnBroker,i.directoryWatchHost,i.serviceChildWindowsJobAnchor].flatMap(e=>[b.posix.normalize(`src/infra/${e.sourceWorkerName}.ts`),`dist/${e.distWorkerPath}`]);function isGatewayProcess(e,t){if(b.basename((e[0]??``).replaceAll(`\\`,`/`)).replace(/\.(exe|cmd|bat)$/iu,``)===`openclaw-gateway`)return!0;if(t.kind!==`openclaw`||t.entryIndex===void 0)return!1;let n=(e[t.entryIndex]??``).replaceAll(`\\`,`/`);return M.some(e=>n.endsWith(`/${e}`))||h(f([`node`,...e.slice(t.entryIndex)]),1)[0]===`gateway`}function classifyProcessArtifactCustody(e,t,n){if(!e)return{kind:`unknown`,reason:`process command is unavailable`};let r=`argv`in e?e.argv:[],i=[...r.map(e=>e.replace(/^--[^=]+=/u,``).replace(/^-r(?=.)/u,``)),n??``,`executable`in e?e.executable??``:``].filter(l);if(`argvUnavailable`in e||r.length===0)return i.length?{kind:`holder`}:{kind:`not-holder`};let{position:a,operands:c}=o(r),u={pid:t,cwd:n??``,serviceMarker:e.serviceMarker,serviceMarkerUnavailable:e.serviceMarkerUnavailable,additionalEntrypoints:j,inspectPackage:!0},d,f=i.flatMap(e=>{let t=p(e,u);return t.kind===`openclaw`&&t.artifactPath?[t.artifactPath]:(d??=t.kind===`unclassified`?t.reason:`runtime path is unresolved`,[])});if(i.length&&c.length===0)return d?{kind:`unknown`,reason:d}:{kind:`holder`,paths:f};let h=g(r,u),_=process.getuid?.(),v=process.platform===`linux`&&h.kind===`unclassified`&&h.cause===`service-marker`&&e.uid!==void 0&&_!==void 0&&e.uid!==_,y=isGatewayProcess(r,h),S=i.length>0||f.length>0,unscoped=()=>S||!y?{kind:`holder`}:{kind:`holder`,gateway:!0};if(h.kind===`openclaw`){if(!h.artifactPath)return c.some(({module:e})=>e)?{kind:`holder`}:unscoped();f.push(h.artifactPath),S=!0}let C=f.length?{kind:`holder`,paths:f}:void 0;if(d??=h.kind===`unclassified`&&h.cause!==`runtime-syntax`?h.reason:void 0,!n||!b.isAbsolute(n)){if(h.kind===`other`&&typeof a!=`number`&&a.kind===`not-runtime`&&i.length===0)return{kind:`not-holder`};if(!v)return{kind:`unknown`,reason:d??`working directory is unavailable`}}let w=n&&b.isAbsolute(n)&&typeof a!=`number`&&a.kind!==`not-runtime`?m(n,!0):void 0;w?.kind===`package`&&w.name===`openclaw`&&(C=unscoped());let T=w?.kind===`package`?w.scripts:{};w?.kind===`unknown`&&(d??=w.reason);for(let{value:e,module:t}of c){let n=e;if(t){if(e.startsWith(`file:`))try{n=x(e)}catch(e){d??=String(e);continue}else if(!b.isAbsolute(e)&&!e.startsWith(`./`)&&!e.startsWith(`../`)){d??=`runtime module package identity is unavailable`;continue}}else if(s(r[0]??``)&&!/[\\/]/u.test(e)&&!b.extname(e)&&typeof T[e]==`string`&&T[e].trim())continue;let i=p(n,u);i.kind===`unclassified`?d??=i.reason:i.kind===`openclaw`&&(S||=i.artifactPath!==void 0,C=i.artifactPath&&(!C||C.paths)?{kind:`holder`,paths:[...C?.paths??[],i.artifactPath]}:unscoped())}return v&&!S&&!C?{kind:`not-holder`}:d?{kind:`unknown`,reason:d}:C??{kind:`not-holder`}}function classifyOpaqueWindowsCustody(e,t){let n=e.commandLine?parseWindowsNativeCommandLine(e.commandLine):null;if(n?.length)return classifyProcessArtifactCustody({argv:n,...e.executable?{executable:e.executable}:{}},t,e.cwd);if([e.executable,e.cwd].some(e=>e&&l(e)))return{kind:`holder`};if(e.foreignOwner||e.accessDenied)return{kind:`not-holder`};let r=e.executable;return r&&!a(r)&&!s(r)&&!/openclaw/iu.test(b.win32.basename(r))?{kind:`not-holder`}:{kind:`unknown`,reason:`process command line is unavailable`}}function classifyWindowsHandoffAncestry(e,t,n){let r=e.startIdentity;if(r&&n.length&&n.every(({startIdentity:e})=>/^\d+$/.test(e)&&BigInt(e)>BigInt(r)))return`unrelated`;let i=/* @__PURE__ */ new Set,a=e;for(;a.startIdentity&&!i.has(a.pid);){let e=a.startIdentity;if(i.add(a.pid),n.some(({pid:t,startIdentity:n})=>t===a.pid&&n===e||t===a.parentPid&&/^\d+$/.test(n)&&BigInt(n)<=BigInt(e)))return`descendant`;if(a.parentPid===0||a.parentPid===4)return`unrelated`;let r=t.get(a.parentPid??0);if(!r?.startIdentity||BigInt(r.startIdentity)>BigInt(e))break;a=r}return`unknown`}function inspectOtherOpenClawProcesses(i){let a=typeof i==`object`?i:void 0,o=typeof i==`string`?i:void 0,s=[],c={matchingPids:[],unverifiedPids:[]};try{if(process.platform===`linux`&&t())throw Error(`Host process visibility cannot be established from this container. Run Doctor on the host after stopping OpenClaw containers that share its temporary directory.`);let i=process.platform===`win32`,l=Date.now()+(a?15e3:1e3),p=i?[...readWindowsProcessCensus(15e3)]:void 0,m=new Map(p?.map(e=>[e.pid,e])),v=i&&a?.windowsUserSid!==void 0&&readWindowsUserSid()===a.windowsUserSid,x=p?.map(({pid:e,parentPid:t,commandLine:n})=>{let r=n?parseWindowsNativeCommandLine(n):null;return{pid:e,state:``,command:r?.length&&t!==void 0?{ppid:t,argv:r}:void 0}})??[..._(a?15e3:1e3,{readDarwinCommand:a?(e,t)=>y(e,t,{serviceMarker:!1}):y})],C=new Map(x.map(e=>[e.pid,e])),w=C.get(process.pid);if(!w||!a&&(!w.command&&process.platform!==`darwin`||process.platform===`linux`&&x.some(e=>e.command&&!(`argv`in e.command))))throw Error(`OpenClaw process census is incomplete.`);let T=p?new Map(p.map(({pid:e,cwd:t})=>[e,t])):d(x.map(({pid:e})=>e)),D=/* @__PURE__ */ new Set,O=!a&&u(process.argv),k=/* @__PURE__ */ new Set([process.pid]),A=w,M=w.command?.ppid??w.ppid??0;for(;M>0;){let e=C.get(M);if(!e?.command||k.has(M)){if(a||(e?!e.command:i))break;throw Error(`OpenClaw process ancestry is incomplete.`)}let t=m.get(M)?.startIdentity,n=m.get(A.pid)?.startIdentity;if(i&&(!t||!n||BigInt(t)>=BigInt(n)))break;if(k.add(M),`argv`in e.command){let{argv:t,serviceMarker:n,serviceMarkerUnavailable:r}=e.command,i=g(t,{pid:M,serviceMarker:n,serviceMarkerUnavailable:r,cwd:T.get(M)??``,additionalEntrypoints:j}),o=i.kind===`openclaw`&&i.entryIndex!==void 0?h(f([`node`,...t.slice(i.entryIndex)]),1)[0]:void 0;(o===(a?`update`:`doctor`)||O&&o===`update`&&M===(w.command?.ppid??w.ppid))&&D.add(M)}A=e,M=e.command.ppid}let normalize=e=>i?e.replaceAll(`\\`,`/`).replace(/\/{2,}\?\/UNC\//gi,`//`).replace(/\/{2,}\?\//g,``).replace(/\/{2,}/g,`/`).toLowerCase():e,literal=t=>e(normalize(t)),referencePattern=e=>{let t=e.flatMap(e=>[e.replace(/[\\/]+$/,``),S(e,{windows:i}).href]);return RegExp(`(?:^|[^\\w-])(?:${a?literal(a.runId):`(?!)`}(?=$|[^\\w-])|(?:${t.map(literal).join(`|`)||`(?!)`})(?=$|[/\\s"'\x60);,\\]]))`)},N=a?.artifactPaths??(o?[o]:[]),P=referencePattern(N),rootKey=e=>normalize(e.replace(/[\\/]+$/,``)),F=new Set(a?.sharedRoots?.map(rootKey)),I=referencePattern(N.filter(e=>!F.has(rootKey(e)))),L=process.getuid?.(),outsideHandoffFamily=(e,t)=>{let n;try{n=E(e,{throughPid:process.pid})}catch{return!1}return n?.stoppedBy===`through-pid`||t!==void 0&&n?.stoppedBy===`root`&&n.chain.every(e=>{let n=C.get(e.pid)?.command;return e.startTimeMicros>t*1e3||process.platform===`linux`&&n?.uid===L&&isStandardSystemdUserManager(n)})},predatesHandoffDrivers=e=>{if(process.platform!==`linux`||!a?.drivers?.length)return!1;let t=r(e)?.startedAt;return t!==void 0&&a.drivers.every(({startIdentity:e})=>BigInt(e)>BigInt(t))};for(let{pid:e,state:t,command:r}of x){if(a&&Date.now()>=l)throw Error(`Process census deadline exceeded`);if(e===process.pid||D.has(e)||(t?.startsWith(`Z`)||a)&&n(e))continue;let u=r&&`argv`in r?r.argv:void 0;if(a){if(!i&&u?.length===0)continue;let t=m.get(e),n=T.get(e),o=[...u??[],t?.commandLine??``,t?.executable??``,n??``],l=g(u??[t?.executable??``],{pid:e,cwd:n??``,serviceMarker:r&&`argv`in r?r.serviceMarker:void 0,additionalEntrypoints:j});if(o.some(e=>P.test(normalize(e)))||!a.artifactPathsOnly&&l.kind===`openclaw`){outsideHandoffFamily(e,o.some(e=>I.test(normalize(e)))?void 0:a.familyDeadline)||c.matchingPids.push(e);continue}let d=a.familyExited?`unrelated`:t&&classifyWindowsHandoffAncestry(t,m,a.drivers??[]);if(d===`descendant`){c.matchingPids.push(e);continue}if(d===`unrelated`||v&&t?.accessDenied)continue;let f=t?.foreignOwner||r?.owner===`different`||r?.uid!==void 0&&L!==void 0&&r.uid!==L;if((!u||n===void 0)&&!f&&!isStandardSystemdLoginProcess(r,C.get(r?.ppid??0)?.command,L)&&!outsideHandoffFamily(e)&&!predatesHandoffDrivers(e)){c.unverifiedPids.push(e);let n=t?.executable??u?.[0];s.push(`Could not verify handoff ancestry for PID ${e} (${n?(i?b.win32:b).basename(n):`image unavailable`}).`)}continue}let d=m.get(e),f=d&&!r?classifyOpaqueWindowsCustody(d,e):classifyProcessArtifactCustody(r,e,T.get(e));f.kind!==`not-holder`&&(f.kind===`holder`&&o&&(f.paths?!f.paths.some(e=>P.test(normalize(e))):f.gateway)||(c.matchingPids.push(e),f.kind===`unknown`&&s.push(`Could not verify runtime custody for PID ${e}: ${f.reason}`)))}}catch(e){let t=/Could not classify PID (\d+):/.exec(e instanceof Error?e.message:``);a&&t&&c.unverifiedPids.push(Number(t[1])),c.error=a?isRosettaTranslatedProcess()?`Host process census is unavailable under Rosetta; run openclaw update repair with native arm64 Node.js.`:`Host process census is incomplete; verify process-inspection permissions and retry update repair.`:`Could not inspect OpenClaw processes: ${String(e)}`}return a?{...c,...s.length?{warnings:s}:{}}:c.error?{error:c.error}:{pids:c.matchingPids,...s.length?{warnings:s}:{}}}export{inspectOtherOpenClawProcesses};