openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 14.8 kB
JavaScript
import{t as e}from"./fs-safe-copy.worker-regexp-ixTN3SDS.mjs";import{n as t}from"./fs-safe-copy.worker-runtime-process-entrypoints-DQ2NMquC.mjs";import{n,r,t as i}from"./fs-safe-copy.worker-runtime-binary-Dgxk5ZBN.mjs";import{t as a}from"./fs-safe-copy.worker-subsystem-CSYDX9_J.mjs";import{t as o}from"./fs-safe-copy.worker-usingCtx-03vOGQ3v.mjs";import{g as s,l as c,p as l}from"./fs-safe-copy.worker-service-child-group-ownership-D9CheQE8.mjs";import{a as u,c as d,f,i as p,l as m,n as h,r as g,s as _,t as v}from"./fs-safe-copy.worker-gateway-process-argv-BiDr_SAo.mjs";import{r as y}from"./fs-safe-copy.worker-windows-install-roots-BA2Oy9MH.mjs";import b from"node:path";import x from"node:os";import{spawnSync as S}from"node:child_process";import{fileURLToPath as C,pathToFileURL as w}from"node:url";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,readProcessCommand as k}from"@openclaw/proc-safe/inspect";import{PinnedProcess as A}from"@openclaw/proc-safe/windows-job";let j=!1;const M=/\[Key\] com\.apple\.private\.no-read-procargs\s*\[Value\]\s*\[Bool\] true|<key>com\.apple\.private\.no-read-procargs<\/key>\s*<true\/>/u;function readDarwinImagePath(e){let t={encoding:`utf8`,timeout:2e3},n=S(`/usr/sbin/lsof`,[`-a`,`-p`,String(e),`-d`,`txt`,`-Fn`],t),r=n.status===0?/^n(\/.+)$/mu.exec(n.stdout)?.[1]:void 0,i=r&&S(`/bin/ps`,[`-o`,`ucomm=`,`-p`,String(e)],t);return i&&i.status===0&&i.stdout.trim()===b.basename(r).slice(0,16)?r:void 0}function isRedactedSystemImage(e){if(!e.startsWith(`/System/`))return!1;let t=S(`/usr/bin/codesign`,[`-d`,`--entitlements`,`-`,e],{encoding:`utf8`,timeout:2e3});return t.status===0&&M.test(t.stdout)}function readCommandWithServiceMarker(e){try{return k(e,{environmentKeys:[`OPENCLAW_SERVICE_MARKER`]})}catch(t){if(!(t instanceof T)||t.code!==`incomplete`)throw t;let r=k(e);return r&&[r.executable,r.argv[0]??``].some(e=>n(e)||i(e)||b.basename(e)===`tsx`)?{...r,serviceMarkerUnavailable:!0}:r}}function readDarwinProcessCommand(e,t,n={}){try{let t=n.serviceMarker===!1?k(e):readCommandWithServiceMarker(e);if(!t)return;let r=t.environment.OPENCLAW_SERVICE_MARKER;return{argv:[...t.argv],executable:t.executable,...r===void 0?{}:{serviceMarker:r},...`serviceMarkerUnavailable`in t?{serviceMarkerUnavailable:!0}:{}}}catch(n){if(!(n instanceof T))throw n;if(n.code===`layout-mismatch`||n.code===`incomplete`){let r=readDarwinImagePath(e);if(r&&isRedactedSystemImage(r))return{argvUnavailable:!0,executable:r,...t?{owner:t}:{}};throw n}if(n.code===`unsupported-platform`&&process.platform===`darwin`)throw Error(`Cannot inspect Darwin process arguments under Rosetta; run OpenClaw with native arm64 Node.js.`,{cause:n});if(n.code!==`access-denied`&&n.code!==`operation-failed`)throw n;if(l(e))return;if(t===`different`)return j||(j=!0,a(`process/census`).debug(`Unreadable Darwin arguments for another UID do not establish capture custody.`,{pid:e,code:n.code})),{argvUnavailable:!0,owner:t};throw Error(`Could not classify PID ${e}: cannot inspect Darwin arguments (${n.code}).`,{cause:n})}}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 N;function isRosettaTranslatedProcess(){return process.platform!==`darwin`||process.arch!==`x64`?!1:(N??=x.cpus()[0]?.model.includes(`Apple`)===!0,N)}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=o();i=a.u(A.open(e,{access:`observe`,expect:{startTimeMicros:n.startTimeMicros}}))?.imagePath}catch(e){a.e=e}finally{a.d()}}catch{}let s;if(t===`unknown`&&!n&&!r)try{D(e)}catch(e){e instanceof T&&e.code===`access-denied`&&(s=!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}:{},...s?{accessDenied:s}:{}}]})}function readWindowsUserSid(){if(process.platform===`win32`)try{let e=S(y(),[`-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 P=Object.values(t).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 F=[t.spawnBroker,t.directoryWatchHost,t.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 F.some(e=>n.endsWith(`/${e}`))||_(m([`node`,...e.slice(t.entryIndex)]),1)[0]===`gateway`}function classifyProcessArtifactCustody(e,t,n){if(!e)return{kind:`unknown`,reason:`process command is unavailable`};let a=`argv`in e?e.argv:[],o=[...a.map(e=>e.replace(/^--[^=]+=/u,``).replace(/^-r(?=.)/u,``)),n??``,`executable`in e?e.executable??``:``].filter(u);if(`argvUnavailable`in e||a.length===0)return o.length?{kind:`holder`}:{kind:`not-holder`};let{position:s,operands:c}=r(a),l={pid:t,cwd:n??``,serviceMarker:e.serviceMarker,serviceMarkerUnavailable:e.serviceMarkerUnavailable,additionalEntrypoints:P,inspectPackage:!0},d,f=o.flatMap(e=>{let t=h(e,l);return t.kind===`openclaw`&&t.artifactPath?[t.artifactPath]:(d??=t.kind===`unclassified`?t.reason:`runtime path is unresolved`,[])});if(o.length&&c.length===0)return d?{kind:`unknown`,reason:d}:{kind:`holder`,paths:f};let p=v(a,l),m=process.getuid?.(),_=process.platform===`linux`&&p.kind===`unclassified`&&p.cause===`service-marker`&&e.uid!==void 0&&m!==void 0&&e.uid!==m,y=isGatewayProcess(a,p),x=o.length>0||f.length>0,unscoped=()=>x||!y?{kind:`holder`}:{kind:`holder`,gateway:!0};if(p.kind===`openclaw`){if(!p.artifactPath)return c.some(({module:e})=>e)?{kind:`holder`}:unscoped();f.push(p.artifactPath),x=!0}let S=f.length?{kind:`holder`,paths:f}:void 0;if(d??=p.kind===`unclassified`&&p.cause!==`runtime-syntax`?p.reason:void 0,!n||!b.isAbsolute(n)){if(p.kind===`other`&&typeof s!=`number`&&s.kind===`not-runtime`&&o.length===0)return{kind:`not-holder`};if(!_)return{kind:`unknown`,reason:d??`working directory is unavailable`}}let w=n&&b.isAbsolute(n)&&typeof s!=`number`&&s.kind!==`not-runtime`?g(n,!0):void 0;w?.kind===`package`&&w.name===`openclaw`&&(S=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=C(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(i(a[0]??``)&&!/[\\/]/u.test(e)&&!b.extname(e)&&typeof T[e]==`string`&&T[e].trim())continue;let r=h(n,l);r.kind===`unclassified`?d??=r.reason:r.kind===`openclaw`&&(x||=r.artifactPath!==void 0,S=r.artifactPath&&(!S||S.paths)?{kind:`holder`,paths:[...S?.paths??[],r.artifactPath]}:unscoped())}return _&&!x&&!S?{kind:`not-holder`}:d?{kind:`unknown`,reason:d}:S??{kind:`not-holder`}}function classifyOpaqueWindowsCustody(e,t){let r=e.commandLine?parseWindowsNativeCommandLine(e.commandLine):null;if(r?.length)return classifyProcessArtifactCustody({argv:r,...e.executable?{executable:e.executable}:{}},t,e.cwd);if([e.executable,e.cwd].some(e=>e&&u(e)))return{kind:`holder`};if(e.foreignOwner||e.accessDenied)return{kind:`not-holder`};let a=e.executable;return a&&!n(a)&&!i(a)&&!/openclaw/iu.test(b.win32.basename(a))?{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(t){let n=typeof t==`object`?t:void 0,r=typeof t==`string`?t:void 0,i=[],a={matchingPids:[],unverifiedPids:[]};try{if(process.platform===`linux`&&f())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 t=process.platform===`win32`,o=Date.now()+(n?15e3:1e3),u=t?[...readWindowsProcessCensus(15e3)]:void 0,h=new Map(u?.map(e=>[e.pid,e])),g=t&&n?.windowsUserSid!==void 0&&readWindowsUserSid()===n.windowsUserSid,y=u?.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}})??[...c(n?15e3:1e3,{readDarwinCommand:n?(e,t)=>readDarwinProcessCommand(e,t,{serviceMarker:!1}):readDarwinProcessCommand})],x=new Map(y.map(e=>[e.pid,e])),S=x.get(process.pid);if(!S||!n&&(!S.command&&process.platform!==`darwin`||process.platform===`linux`&&y.some(e=>e.command&&!(`argv`in e.command))))throw Error(`OpenClaw process census is incomplete.`);let C=u?new Map(u.map(({pid:e,cwd:t})=>[e,t])):p(y.map(({pid:e})=>e)),T=/* @__PURE__ */ new Set,D=!n&&d(process.argv),O=/* @__PURE__ */ new Set([process.pid]),k=S,A=S.command?.ppid??S.ppid??0;for(;A>0;){let e=x.get(A);if(!e?.command||O.has(A)){if(n||(e?!e.command:t))break;throw Error(`OpenClaw process ancestry is incomplete.`)}let r=h.get(A)?.startIdentity,i=h.get(k.pid)?.startIdentity;if(t&&(!r||!i||BigInt(r)>=BigInt(i)))break;if(O.add(A),`argv`in e.command){let{argv:t,serviceMarker:r,serviceMarkerUnavailable:i}=e.command,a=v(t,{pid:A,serviceMarker:r,serviceMarkerUnavailable:i,cwd:C.get(A)??``,additionalEntrypoints:P}),o=a.kind===`openclaw`&&a.entryIndex!==void 0?_(m([`node`,...t.slice(a.entryIndex)]),1)[0]:void 0;(o===(n?`update`:`doctor`)||D&&o===`update`&&A===(S.command?.ppid??S.ppid))&&T.add(A)}k=e,A=e.command.ppid}let normalize=e=>t?e.replaceAll(`\\`,`/`).replace(/\/{2,}\?\/UNC\//gi,`//`).replace(/\/{2,}\?\//g,``).replace(/\/{2,}/g,`/`).toLowerCase():e,literal=t=>e(normalize(t)),referencePattern=e=>{let r=e.flatMap(e=>[e.replace(/[\\/]+$/,``),w(e,{windows:t}).href]);return RegExp(`(?:^|[^\\w-])(?:${n?literal(n.runId):`(?!)`}(?=$|[^\\w-])|(?:${r.map(literal).join(`|`)||`(?!)`})(?=$|[/\\s"'\x60);,\\]]))`)},j=n?.artifactPaths??(r?[r]:[]),M=referencePattern(j),rootKey=e=>normalize(e.replace(/[\\/]+$/,``)),N=new Set(n?.sharedRoots?.map(rootKey)),F=referencePattern(j.filter(e=>!N.has(rootKey(e)))),I=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=x.get(e.pid)?.command;return e.startTimeMicros>t*1e3||process.platform===`linux`&&n?.uid===I&&isStandardSystemdUserManager(n)})},predatesHandoffDrivers=e=>{if(process.platform!==`linux`||!n?.drivers?.length)return!1;let t=s(e)?.startedAt;return t!==void 0&&n.drivers.every(({startIdentity:e})=>BigInt(e)>BigInt(t))};for(let{pid:e,state:s,command:c}of y){if(n&&Date.now()>=o)throw Error(`Process census deadline exceeded`);if(e===process.pid||T.has(e)||(s?.startsWith(`Z`)||n)&&l(e))continue;let u=c&&`argv`in c?c.argv:void 0;if(n){if(!t&&u?.length===0)continue;let r=h.get(e),o=C.get(e),s=[...u??[],r?.commandLine??``,r?.executable??``,o??``],l=v(u??[r?.executable??``],{pid:e,cwd:o??``,serviceMarker:c&&`argv`in c?c.serviceMarker:void 0,additionalEntrypoints:P});if(s.some(e=>M.test(normalize(e)))||!n.artifactPathsOnly&&l.kind===`openclaw`){outsideHandoffFamily(e,s.some(e=>F.test(normalize(e)))?void 0:n.familyDeadline)||a.matchingPids.push(e);continue}let d=n.familyExited?`unrelated`:r&&classifyWindowsHandoffAncestry(r,h,n.drivers??[]);if(d===`descendant`){a.matchingPids.push(e);continue}if(d===`unrelated`||g&&r?.accessDenied)continue;let f=r?.foreignOwner||c?.owner===`different`||c?.uid!==void 0&&I!==void 0&&c.uid!==I;if((!u||o===void 0)&&!f&&!isStandardSystemdLoginProcess(c,x.get(c?.ppid??0)?.command,I)&&!outsideHandoffFamily(e)&&!predatesHandoffDrivers(e)){a.unverifiedPids.push(e);let n=r?.executable??u?.[0];i.push(`Could not verify handoff ancestry for PID ${e} (${n?(t?b.win32:b).basename(n):`image unavailable`}).`)}continue}let d=h.get(e),f=d&&!c?classifyOpaqueWindowsCustody(d,e):classifyProcessArtifactCustody(c,e,C.get(e));f.kind!==`not-holder`&&(f.kind===`holder`&&r&&(f.paths?!f.paths.some(e=>M.test(normalize(e))):f.gateway)||(a.matchingPids.push(e),f.kind===`unknown`&&i.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:``);n&&t&&a.unverifiedPids.push(Number(t[1])),a.error=n?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 n?{...a,...i.length?{warnings:i}:{}}:a.error?{error:a.error}:{pids:a.matchingPids,...i.length?{warnings:i}:{}}}export{inspectOtherOpenClawProcesses};