openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 6.55 kB
JavaScript
import{l as resolveGlobalSingleton}from"./string-coerce-BBqv4GXj.mjs";import{t as hasErrnoCode}from"./errno-DUBt6SQQ.mjs";import{s as sanitizeTerminalText}from"./utils-CfSlIBx_.mjs";import{t as createSubsystemLogger}from"./subsystem-BMw5Czn-.mjs";import{a as runInDetachedAsyncContext}from"./plugin-instance-invocation-D2U32-yV.mjs";import{n as getSpawnBroker,t as restoreExecaResult}from"./execa-protocol-ByCLnP2X.mjs";import{execFile,spawnSync}from"node:child_process";import{accessSync,constants,statSync}from"node:fs";import path from"node:path";import fsPromises from"node:fs/promises";
//!src/infra/child-runtime-viability.ts
const runtime=resolveGlobalSingleton(Symbol.for(`openclaw.childRuntimeViability`),()=>({initialPath:process.execPath,initialIdentity:executableIdentity(process.execPath)}));const loaderFailure=/(?:dyld[^\n]*:\s*Library not loaded:|error while loading shared libraries:)/i;const ambiguousLoaderFailure=/(?: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(statSync(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){if(hasErrnoCode(t,`ENOENT`)||hasErrnoCode(t,`ENOTDIR`))recordUnavailableRuntime(e,t);return{execPath:e,available:!(runtime.execPath===e&&runtime.failure)}}function readChildRuntimeViability(e){const t=e?.execPath??process.execPath;if(runtime.execPath===t&&runtime.failure)return{execPath:t,available:false};try{accessSync(t,constants.X_OK)}catch(e){return publishRuntimeViability(t,e)}return publishRuntimeViability(t)}async function prepareChildRuntimeViability(e){const t=e?.execPath??process.execPath;let n=!(runtime.execPath===t&&runtime.failure);if(n)try{const e=await probeChildRuntimeExecutable(t);n=!classifyRuntimeProbe(t,e,await fsPromises.stat(t).then(formatExecutableIdentity,()=>void 0),e.error)}catch(e){n=publishRuntimeViability(t,await fsPromises.stat(t).then(()=>e,e=>e)).available}return{publish:()=>({execPath:t,available:n&&publishRuntimeViability(t).available})}}function recoverUnavailableRuntime(){if(runtime.execPath!==process.execPath||!runtime.failure||!runtime.recover||runtime.recoveryStarted)return;runtime.recoveryStarted=true;const e=runtime.recover;runInDetachedAsyncContext(()=>Promise.resolve().then(e).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=>{createSubsystemLogger(`gateway/runtime`).warn(`${e.status}: ${e.reason}`)}))}function runtimeProbeOptions(e={}){return{encoding:`utf8`,cwd:e.cwd??path.parse(process.execPath).root,env:e.env??{...process.env,NODE_OPTIONS:void 0},timeout:1e3,maxBuffer:8192}}async function probeChildRuntimeExecutable(e,t={}){const n=getSpawnBroker();if(n){const r=n.spawnExeca([e,`--version`],{...runtimeProbeOptions(t),extendEnv:false,stripFinalNewline:false,stdin:`ignore`});r.child.once(`spawn`,()=>{r.child.stdout?.resume();r.child.stderr?.resume()});const i=restoreExecaResult(await r.result);await r.child.waitForClose();return{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=>{execFile(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 (${sanitizeTerminalText(e.execPath)}) can no longer start worker processes, likely because it was upgraded or removed. Restart the Gateway.`}function assertChildRuntimeAvailable(){if(runtime.execPath===process.execPath&&runtime.failure)throw runtime.failure}function recordUnavailableRuntime(e,t,n){if(runtime.execPath===e&&runtime.failure)return runtime.failure;runtime.execPath=e;const r=formatMissingChildRuntimeWarning({execPath:e,available:false});const i=n?.trim();runtime.failure=Object.assign(new Error(i?`${r}\n${i}`:r,{cause:t}),{code:`NODE_RUNTIME_UNAVAILABLE`});createSubsystemLogger(`gateway/runtime`).error(runtime.failure.message);recoverUnavailableRuntime();return runtime.failure}function needsRuntimeProbe(e){return Boolean(e.error||e.signal===`SIGABRT`||ambiguousLoaderFailure.test(e.stderr??``))}function classifyRuntimeProbe(e,t,n,r){if(runtime.execPath===e&&runtime.failure)return runtime.failure;const i=e!==runtime.initialPath||n!==runtime.initialIdentity;return loaderFailure.test(t.stderr)||ambiguousLoaderFailure.test(t.stderr)||hasErrnoCode(t.error,`ENOENT`)||hasErrnoCode(t.error,`ENOTDIR`)||i&&(t.signal===`SIGABRT`||hasErrnoCode(t.error,`ENOEXEC`)||hasErrnoCode(t.error,`EACCES`)||hasErrnoCode(t.error,`EPERM`))?recordUnavailableRuntime(e,r,t.stderr):void 0}function classifyChildRuntimeFailure(e){if(e.started)return;const t=e.execPath??process.execPath;if(runtime.execPath===t&&runtime.failure)return runtime.failure;if(loaderFailure.test(e.stderr??``)||e.error&&!readChildRuntimeViability({execPath:t}).available)return recordUnavailableRuntime(t,e.error,e.stderr);if(!needsRuntimeProbe(e))return;return classifyRuntimeProbe(t,spawnSync(t,[`--version`],runtimeProbeOptions()),t===runtime.initialPath?executableIdentity(t):void 0,e.error)}async function prepareChildRuntimeFailure(e){if(e.started)return;const t=e.execPath??process.execPath;if(runtime.execPath===t&&runtime.failure)return runtime.failure;if(loaderFailure.test(e.stderr??``)||e.error&&!(await prepareChildRuntimeViability({execPath:t})).publish().available)return recordUnavailableRuntime(t,e.error,e.stderr);if(e.error||!needsRuntimeProbe(e))return;return classifyRuntimeProbe(t,await probeChildRuntimeExecutable(t),t===runtime.initialPath?await fsPromises.stat(t).then(formatExecutableIdentity,()=>void 0):void 0,e.error)}function observeChildRuntime(e,t=process.execPath){let n=false;let r;let i=``;e.once(`message`,()=>{n=true;i=``});e.on(`error`,e=>{r??=e});const captureStderr=e=>{if(!n)i=(i+e.toString()).slice(-8192)};e.stderr?.on(`data`,captureStderr);return 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};