UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 5.77 kB
import{V as e}from"./openclaw-state-read-map-size-DJbMcZYM.mjs";import{v as t}from"./openclaw-state-read-home-dir-BQZX1pD2.mjs";import{P as n}from"./openclaw-state-read-logger-Ix7jXPG1.mjs";import{v as r}from"./openclaw-state-read-read-open-flags-CtNL1rJs.mjs";import{t as i}from"./openclaw-state-read-subsystem-DSi0tJn8.mjs";import{n as a,t as o}from"./openclaw-state-read-execa-protocol-BrSZNRnu.mjs";import{accessSync as s,constants as c,statSync as l}from"node:fs";import u from"node:path";import{execFile as d,spawnSync as f}from"node:child_process";import p from"node:fs/promises";const m=t(Symbol.for(`openclaw.childRuntimeViability`),()=>({initialPath:process.execPath,initialIdentity:executableIdentity(process.execPath)})),h=/(?:dyld[^\n]*:\s*Library not loaded:|error while loading shared libraries:)/i,g=/(?:dyld[^\n]*:\s*Symbol not found:|symbol lookup error:|Error loading shared library|version [`'"][^`'"\r\n]+[`'"] not found)/i;function executableIdentity(e){try{return formatExecutableIdentity(l(e))}catch{return}}function formatExecutableIdentity({dev:e,ino:t,size:n,mtimeMs:r,mode:i}){return`${e}:${t}:${n}:${r}:${i}`}function publishRuntimeViability(e,t){return(r(t,`ENOENT`)||r(t,`ENOTDIR`))&&recordUnavailableRuntime(e,t),{execPath:e,available:!(m.execPath===e&&m.failure)}}function readChildRuntimeViability(e){let t=e?.execPath??process.execPath;if(m.execPath===t&&m.failure)return{execPath:t,available:!1};try{s(t,c.X_OK)}catch(e){return publishRuntimeViability(t,e)}return publishRuntimeViability(t)}async function prepareChildRuntimeViability(e){let t=e?.execPath??process.execPath,n=!(m.execPath===t&&m.failure);if(n)try{let e=await probeChildRuntimeExecutable(t);n=!classifyRuntimeProbe(t,e,await p.stat(t).then(formatExecutableIdentity,()=>void 0),e.error)}catch(e){n=publishRuntimeViability(t,await p.stat(t).then(()=>e,e=>e)).available}return{publish:()=>({execPath:t,available:n&&publishRuntimeViability(t).available})}}function recoverUnavailableRuntime(){if(m.execPath!==process.execPath||!m.failure||!m.recover||m.recoveryStarted)return;m.recoveryStarted=!0;let t=m.recover;e(()=>Promise.resolve().then(t).catch(()=>({status:`report-only`,reason:`Automatic runtime recovery is report-only: service or restart preparation failed. Inspect the service runtime and restart through its owner.`})).then(e=>{i(`gateway/runtime`).warn(`${e.status}: ${e.reason}`)}))}function runtimeProbeOptions(e={}){return{encoding:`utf8`,cwd:e.cwd??u.parse(process.execPath).root,env:e.env??{...process.env,NODE_OPTIONS:void 0},timeout:1e3,maxBuffer:8192}}async function probeChildRuntimeExecutable(e,t={}){let n=a();if(n){let r=n.spawnExeca([e,`--version`],{...runtimeProbeOptions(t),extendEnv:!1,stripFinalNewline:!1,stdin:`ignore`});r.child.once(`spawn`,()=>{r.child.stdout?.resume(),r.child.stderr?.resume()});let i=o(await r.result);return await r.child.waitForClose(),{stdout:typeof i.stdout==`string`?i.stdout:``,stderr:typeof i.stderr==`string`?i.stderr:``,error:i instanceof Error?i:void 0,status:i.exitCode??null,signal:i.signal??null}}return new Promise(n=>{d(e,[`--version`],runtimeProbeOptions(t),(e,t,r)=>n({stdout:t,stderr:r,error:e??void 0,status:e?typeof e.code==`number`?e.code:null:0,signal:e?.signal??null}))})}function formatMissingChildRuntimeWarning(e){return e.available?void 0:`The Node.js runtime this Gateway started with (${n(e.execPath)}) can no longer start worker processes, likely because it was upgraded or removed. Restart the Gateway.`}function assertChildRuntimeAvailable(){if(m.execPath===process.execPath&&m.failure)throw m.failure}function recordUnavailableRuntime(e,t,n){if(m.execPath===e&&m.failure)return m.failure;m.execPath=e;let r=formatMissingChildRuntimeWarning({execPath:e,available:!1}),a=n?.trim();return m.failure=Object.assign(Error(a?`${r}\n${a}`:r,{cause:t}),{code:`NODE_RUNTIME_UNAVAILABLE`}),i(`gateway/runtime`).error(m.failure.message),recoverUnavailableRuntime(),m.failure}function needsRuntimeProbe(e){return!!(e.error||e.signal===`SIGABRT`||g.test(e.stderr??``))}function classifyRuntimeProbe(e,t,n,i){if(m.execPath===e&&m.failure)return m.failure;let a=e!==m.initialPath||n!==m.initialIdentity;return h.test(t.stderr)||g.test(t.stderr)||r(t.error,`ENOENT`)||r(t.error,`ENOTDIR`)||a&&(t.signal===`SIGABRT`||r(t.error,`ENOEXEC`)||r(t.error,`EACCES`)||r(t.error,`EPERM`))?recordUnavailableRuntime(e,i,t.stderr):void 0}function classifyChildRuntimeFailure(e){if(e.started)return;let t=e.execPath??process.execPath;if(m.execPath===t&&m.failure)return m.failure;if(h.test(e.stderr??``)||e.error&&!readChildRuntimeViability({execPath:t}).available)return recordUnavailableRuntime(t,e.error,e.stderr);if(needsRuntimeProbe(e))return classifyRuntimeProbe(t,f(t,[`--version`],runtimeProbeOptions()),t===m.initialPath?executableIdentity(t):void 0,e.error)}async function prepareChildRuntimeFailure(e){if(e.started)return;let t=e.execPath??process.execPath;if(m.execPath===t&&m.failure)return m.failure;if(h.test(e.stderr??``)||e.error&&!(await prepareChildRuntimeViability({execPath:t})).publish().available)return recordUnavailableRuntime(t,e.error,e.stderr);if(!e.error&&needsRuntimeProbe(e))return classifyRuntimeProbe(t,await probeChildRuntimeExecutable(t),t===m.initialPath?await p.stat(t).then(formatExecutableIdentity,()=>void 0):void 0,e.error)}function observeChildRuntime(e,t=process.execPath){let n=!1,r,i=``;return e.once(`message`,()=>{n=!0,i=``}),e.on(`error`,e=>{r??=e}),e.stderr?.on(`data`,e=>{n||(i=(i+e.toString()).slice(-8192))}),async()=>e.exitCode===0&&!r?void 0:prepareChildRuntimeFailure({execPath:t,error:r,stderr:i,signal:e.signalCode,started:n})}export{prepareChildRuntimeFailure as i,classifyChildRuntimeFailure as n,observeChildRuntime as r,assertChildRuntimeAvailable as t};