UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 14.7 kB
import{t as e}from"./transcript-subsystem-er2pCebD.mjs";import{t}from"./transcript-regexp--qgq5Lua.mjs";import{t as n}from"./transcript-usingCtx-03vOGQ3v.mjs";import{a as r,l as i,o as a,s as o}from"./transcript-runtime-worker-url-D1R86-Xb.mjs";import{d as s}from"./transcript-worker-task-pool-xHbAfEur.mjs";import{i as c,s as l}from"./transcript-pid-alive-CqZqK_fT.mjs";import{a as u,c as d,i as f,l as p,n as m,r as h,s as g,t as _,u as v}from"./transcript-gateway-process-argv-bCE8e8mm.mjs";import{n as y}from"./transcript-windows-install-roots-D5D0JgeT.mjs";import b from"node:path";import x from"node:os";import{fileURLToPath as S,pathToFileURL as C}from"node:url";import{spawnSync as w}from"node:child_process";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=w(`/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&&w(`/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=w(`/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 n=k(e);return n&&[n.executable,n.argv[0]??``].some(e=>a(e)||r(e)||b.basename(e)===`tsx`)?{...n,serviceMarkerUnavailable:!0}:n}}function readDarwinProcessCommand(t,n,r={}){try{let e=r.serviceMarker===!1?k(t):readCommandWithServiceMarker(t);if(!e)return;let n=e.environment.OPENCLAW_SERVICE_MARKER;return{argv:[...e.argv],executable:e.executable,...n===void 0?{}:{serviceMarker:n},...`serviceMarkerUnavailable`in e?{serviceMarkerUnavailable:!0}:{}}}catch(r){if(!(r instanceof T))throw r;if(r.code===`layout-mismatch`||r.code===`incomplete`){let e=readDarwinImagePath(t);if(e&&isRedactedSystemImage(e))return{argvUnavailable:!0,executable:e,...n?{owner:n}:{}};throw r}if(r.code===`unsupported-platform`&&process.platform===`darwin`)throw Error(`Cannot inspect Darwin process arguments under Rosetta; run OpenClaw with native arm64 Node.js.`,{cause:r});if(r.code!==`access-denied`&&r.code!==`operation-failed`)throw r;if(c(t))return;if(n===`different`)return j||(j=!0,e(`process/census`).debug(`Unreadable Darwin arguments for another UID do not establish capture custody.`,{pid:t,code:r.code})),{argvUnavailable:!0,owner:n};throw Error(`Could not classify PID ${t}: cannot inspect Darwin arguments (${r.code}).`,{cause:r})}}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:r,command:i})=>{if(e===0||e===4||r?.exited)return[];let a=i?.executable;if(!i&&r)try{try{var o=n();a=o.u(A.open(e,{access:`observe`,expect:{startTimeMicros:r.startTimeMicros}}))?.imagePath}catch(e){o.e=e}finally{o.d()}}catch{}let s;if(t===`unknown`&&!r&&!i)try{D(e)}catch(e){e instanceof T&&e.code===`access-denied`&&(s=!0)}return[{pid:e,...r?{parentPid:r.parentPid,startIdentity:String(Math.floor(r.startTimeMicros/1e3))}:{},...i?.commandLine===void 0?{}:{commandLine:i.commandLine},...a?{executable:a}:{},...i?.cwd===void 0?{}:{cwd:i.cwd},...t===`different`?{foreignOwner:!0}:{},...s?{accessDenied:s}:{}}]})}function readWindowsUserSid(){if(process.platform===`win32`)try{let e=w(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(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 F=[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 F.some(e=>n.endsWith(`/${e}`))||g(p([`node`,...e.slice(t.entryIndex)]),1)[0]===`gateway`}function classifyProcessArtifactCustody(e,t,n){if(!e)return{kind:`unknown`,reason:`process command is unavailable`};let i=`argv`in e?e.argv:[],a=[...i.map(e=>e.replace(/^--[^=]+=/u,``).replace(/^-r(?=.)/u,``)),n??``,`executable`in e?e.executable??``:``].filter(u);if(`argvUnavailable`in e||i.length===0)return a.length?{kind:`holder`}:{kind:`not-holder`};let{position:s,operands:c}=o(i),l={pid:t,cwd:n??``,serviceMarker:e.serviceMarker,serviceMarkerUnavailable:e.serviceMarkerUnavailable,additionalEntrypoints:P,inspectPackage:!0},d,f=a.flatMap(e=>{let t=m(e,l);return t.kind===`openclaw`&&t.artifactPath?[t.artifactPath]:(d??=t.kind===`unclassified`?t.reason:`runtime path is unresolved`,[])});if(a.length&&c.length===0)return d?{kind:`unknown`,reason:d}:{kind:`holder`,paths:f};let p=_(i,l),g=process.getuid?.(),v=process.platform===`linux`&&p.kind===`unclassified`&&p.cause===`service-marker`&&e.uid!==void 0&&g!==void 0&&e.uid!==g,y=isGatewayProcess(i,p),x=a.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 C=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`&&a.length===0)return{kind:`not-holder`};if(!v)return{kind:`unknown`,reason:d??`working directory is unavailable`}}let w=n&&b.isAbsolute(n)&&typeof s!=`number`&&s.kind!==`not-runtime`?h(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=S(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(r(i[0]??``)&&!/[\\/]/u.test(e)&&!b.extname(e)&&typeof T[e]==`string`&&T[e].trim())continue;let a=m(n,l);a.kind===`unclassified`?d??=a.reason:a.kind===`openclaw`&&(x||=a.artifactPath!==void 0,C=a.artifactPath&&(!C||C.paths)?{kind:`holder`,paths:[...C?.paths??[],a.artifactPath]}:unscoped())}return v&&!x&&!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&&u(e)))return{kind:`holder`};if(e.foreignOwner||e.accessDenied)return{kind:`not-holder`};let i=e.executable;return i&&!a(i)&&!r(i)&&!/openclaw/iu.test(b.win32.basename(i))?{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(e){let n=typeof e==`object`?e:void 0,r=typeof e==`string`?e:void 0,i=[],a={matchingPids:[],unverifiedPids:[]};try{if(process.platform===`linux`&&v())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 e=process.platform===`win32`,o=Date.now()+(n?15e3:1e3),u=e?[...readWindowsProcessCensus(15e3)]:void 0,m=new Map(u?.map(e=>[e.pid,e])),h=e&&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}})??[...s(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 w=u?new Map(u.map(({pid:e,cwd:t})=>[e,t])):f(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 t=x.get(A);if(!t?.command||O.has(A)){if(n||(t?!t.command:e))break;throw Error(`OpenClaw process ancestry is incomplete.`)}let r=m.get(A)?.startIdentity,i=m.get(k.pid)?.startIdentity;if(e&&(!r||!i||BigInt(r)>=BigInt(i)))break;if(O.add(A),`argv`in t.command){let{argv:e,serviceMarker:r,serviceMarkerUnavailable:i}=t.command,a=_(e,{pid:A,serviceMarker:r,serviceMarkerUnavailable:i,cwd:w.get(A)??``,additionalEntrypoints:P}),o=a.kind===`openclaw`&&a.entryIndex!==void 0?g(p([`node`,...e.slice(a.entryIndex)]),1)[0]:void 0;(o===(n?`update`:`doctor`)||D&&o===`update`&&A===(S.command?.ppid??S.ppid))&&T.add(A)}k=t,A=t.command.ppid}let normalize=t=>e?t.replaceAll(`\\`,`/`).replace(/\/{2,}\?\/UNC\//gi,`//`).replace(/\/{2,}\?\//g,``).replace(/\/{2,}/g,`/`).toLowerCase():t,literal=e=>t(normalize(e)),referencePattern=t=>{let r=t.flatMap(t=>[t.replace(/[\\/]+$/,``),C(t,{windows:e}).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=l(e)?.startedAt;return t!==void 0&&n.drivers.every(({startIdentity:e})=>BigInt(e)>BigInt(t))};for(let{pid:t,state:s,command:l}of y){if(n&&Date.now()>=o)throw Error(`Process census deadline exceeded`);if(t===process.pid||T.has(t)||(s?.startsWith(`Z`)||n)&&c(t))continue;let u=l&&`argv`in l?l.argv:void 0;if(n){if(!e&&u?.length===0)continue;let r=m.get(t),o=w.get(t),s=[...u??[],r?.commandLine??``,r?.executable??``,o??``],c=_(u??[r?.executable??``],{pid:t,cwd:o??``,serviceMarker:l&&`argv`in l?l.serviceMarker:void 0,additionalEntrypoints:P});if(s.some(e=>M.test(normalize(e)))||!n.artifactPathsOnly&&c.kind===`openclaw`){outsideHandoffFamily(t,s.some(e=>F.test(normalize(e)))?void 0:n.familyDeadline)||a.matchingPids.push(t);continue}let d=n.familyExited?`unrelated`:r&&classifyWindowsHandoffAncestry(r,m,n.drivers??[]);if(d===`descendant`){a.matchingPids.push(t);continue}if(d===`unrelated`||h&&r?.accessDenied)continue;let f=r?.foreignOwner||l?.owner===`different`||l?.uid!==void 0&&I!==void 0&&l.uid!==I;if((!u||o===void 0)&&!f&&!isStandardSystemdLoginProcess(l,x.get(l?.ppid??0)?.command,I)&&!outsideHandoffFamily(t)&&!predatesHandoffDrivers(t)){a.unverifiedPids.push(t);let n=r?.executable??u?.[0];i.push(`Could not verify handoff ancestry for PID ${t} (${n?(e?b.win32:b).basename(n):`image unavailable`}).`)}continue}let d=m.get(t),f=d&&!l?classifyOpaqueWindowsCustody(d,t):classifyProcessArtifactCustody(l,t,w.get(t));f.kind!==`not-holder`&&(f.kind===`holder`&&r&&(f.paths?!f.paths.some(e=>M.test(normalize(e))):f.gateway)||(a.matchingPids.push(t),f.kind===`unknown`&&i.push(`Could not verify runtime custody for PID ${t}: ${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};