openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 11.1 kB
JavaScript
import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{t as formatCliCommand}from"./command-format-D7KhNLP0.mjs";import{y as reserveGatewayStateRestoration}from"./gateway-state-owner-D_klysuh.mjs";import{s as resolveOpenClawStateSqlitePath}from"./openclaw-state-db.paths-CbgXfEg1.mjs";import{a as hasCommandProcessCleanupError}from"./exec-result-TD4w1Z1i.mjs";import{o as UpdateDoctorError,t as DoctorMaintenanceRefusalError}from"./update-doctor-result-B09W3zwf.mjs";import{a as ServiceInspectionError,d as findServiceOwnershipRefusal}from"./service-inspection-error-D006EyWC.mjs";import{r as createUpdateFailureFact}from"./update-failure-facts-Bwgg6oY-.mjs";import{n as GatewayServiceAuthorityError}from"./service-update-authority-DqjrBTQ9.mjs";import{a as withGatewayServiceRebindCapture,n as currentGatewayServiceRebindReceipt,r as fingerprintGatewayServiceDefinition}from"./service-rebind-B7VzSdFi.mjs";import{t as isManagedGatewayServiceOffline}from"./update-command-service-publication-BxkGOjXg.mjs";import{n as assertDoctorServiceSelection}from"./doctor-service-repair-policy-gYXaFNLg.mjs";
//!src/commands/doctor-maintenance-restoration.ts
async function finishDoctorGatewayMaintenance(e){let t=e.config;const{state:n}=e;const r=n.owner;const i=e.stopped&&r?reserveGatewayStateRestoration(resolveOpenClawStateSqlitePath(n.env),()=>r.assertCurrent()):void 0;const assertCurrent=()=>{e.assertCustody?.();i?.assertCurrent()};try{const n=await e.release(assertCurrent);if(!t&&e.stopped)try{const{readConfigFileSnapshot:e}=await import(`./config/config.js`);t=(await e({skipPluginValidation:true,observe:false})).config}catch(e){throw new DoctorMaintenanceRefusalError(`Doctor could not restore the Gateway because persisted repair state is not ready: ${formatErrorMessage(e)}`,{kind:`data-at-risk`,reason:`incomplete-migration`},{cause:e})}if(t)await e.restore(t,assertCurrent);if(n)throw n}finally{if(!e.isCleanupUncertain()&&!n.resources)i?.release()}}async function resolveUnstoppedGatewayRestoration(e){const{before:t,serviceEnv:n}=e;const r=t.serviceUpdateVerdict;if(r?.kind===`owned`&&r.requiresInstallRootRefresh){const{inspectGatewayServiceInstallationDrift:i}=await import(`./service-layout-DqzESB0i.mjs`);const a=await i({packageRootReal:r.root},e.root);if(a){const{formatGatewayServiceInstallationDrift:e}=await import(`./shared-KEMAEjD0.mjs`);return{restore:false,warning:e(a,n,{stopped:true,port:t.servicePort})}}}if(!e.leaveStopped&&t.offline===true&&r?.kind===`owned`&&!r.requiresInstallRootRefresh)return{restore:true};if(e.reportStopped&&r?.kind===`owned`&&t.runtimeInspected&&!t.running)return{restore:false,warning:`Gateway service is stopped and remains stopped; update repair does not start a Gateway that a failed update or the operator stopped. Start it with \`${formatCliCommand(`openclaw gateway start`,n)}\`, then check \`${formatCliCommand(`openclaw gateway status --deep`,n)}\`.`};return{restore:false}}async function restoreDoctorGatewayService(e){const{before:t,serviceEnv:n,root:r,env:i,writeConfig:a,warnings:o,settle:s,assertCustody:c,assertRestoreAdmission:l}=e;let u=e.cfg;const[{readGatewayServiceState:d,resolveGatewayService:f},{withGatewayServiceOperationLock:p},{revalidateManagedGatewayServiceAfterUpdate:m}]=await Promise.all([import(`./service-flxrucEo.mjs`),import(`./service-operation-lock-BR4RIMlV.mjs`),import(`./update-command-service-maintenance-2FxULYpZ.mjs`)]);const h=f();return{service:h,state:await p(n,async f=>{const assertInspectionCurrent=()=>{c?.();f()};const assertMaintenanceCurrent=()=>{assertInspectionCurrent();l?.()};const assertAutoStartCurrent=async()=>{assertMaintenanceCurrent();if(t.serviceAutoStartEnabled===void 0)return;const e=h.isEnabled;if(!e)throw new Error(`Gateway native autostart policy could not be verified after Doctor repair.`);const r=await s(()=>e({env:n}));assertMaintenanceCurrent();if(r!==t.serviceAutoStartEnabled)throw new Error(`Gateway native autostart policy changed during Doctor repair; automatic restoration was refused. Inspect gateway status --deep before starting it.`)};await assertAutoStartCurrent();const readCurrent=()=>s(()=>d(h,{env:n,requireEffective:true,requireLoadedCommand:true,...process.platform===`linux`&&t.serviceManagerUid!==void 0?{loadForInspection:{managerUid:t.serviceManagerUid,assertCurrent:assertInspectionCurrent}}:{}}));let p;let g;let _;const readInspection=async()=>{p=void 0;g=void 0;try{p=await readCurrent();if(p.inspectionReason){g=new ServiceInspectionError(p.inspectionReason);const e=findServiceOwnershipRefusal(g);if(e)throw e}else if(p.loadState.status===`unknown`||p.runtime?.status!==`running`&&p.runtime?.status!==`stopped`)g=new Error(p.loadState.status===`unknown`?p.loadState.detail:p.runtime?.inspectionFailure?.detail??`Gateway runtime inspection was inconclusive.`)}catch(e){if(hasCommandProcessCleanupError(e))throw e;const t=findServiceOwnershipRefusal(e);if(t)throw t;g=e}assertMaintenanceCurrent();return p};await readInspection();assertMaintenanceCurrent();let v=t.serviceUpdateVerdict;const inspectActivation=async()=>{const e=p;if(g||!e?.command)return{kind:`inconclusive`,reason:g?formatErrorMessage(g):`The current service launcher could not be verified.`};if(v?.kind!==`owned`||v.requiresInstallRootRefresh)return{kind:`not-ours`,reason:`The service does not target this installation.`};if(e.running||!await s(()=>isManagedGatewayServiceOffline(e)))return{kind:`inconclusive`,reason:`The service is not verified offline.`};return{kind:`owned-offline`}};const mayRestore=async()=>{if(t.stopped)return true;const n=await inspectActivation();assertMaintenanceCurrent();if(n.kind===`owned-offline`)return true;const r=`Warning: Gateway activation skipped (${n.kind}): ${n.reason} Run ${formatCliCommand(`openclaw gateway status --deep`,i)} before starting it.`;o.push(r);e.runtime.log(r);return false};const revalidate=async(e,n=false)=>{try{return await s(()=>m({state:e,root:r,preManagedServiceStop:t,allowIncompleteInspection:n,expectedDefinitionFingerprint:_}))}catch(e){if(t.stopped||hasCommandProcessCleanupError(e)||findServiceOwnershipRefusal(e))throw e;g=e;return{kind:`unavailable`,message:formatErrorMessage(e)}}};if(p){assertDoctorServiceSelection(i,p.env);const e=await revalidate(p,t.stopped);if(e.kind===`unavailable`)g??=new Error(e.message);else v=e}if(!await mayRestore())return;if(v?.kind===`owned`&&(v.requiresInstallRootRefresh||a)){const n=v;if(!g){const assertInstallationCurrent=()=>{assertMaintenanceCurrent();e.assertInstallationAdmission?.()};assertInstallationCurrent();await assertAutoStartCurrent();const[{maybeRepairGatewayServiceConfig:r},{createDoctorPrompter:c}]=await Promise.all([import(`./doctor-gateway-services--ExSSbkI.mjs`),import(`./doctor-prompter-BOnP8RlH.mjs`)]);assertInstallationCurrent();const l=await fingerprintGatewayServiceDefinition(p?.command??null);assertInstallationCurrent();try{u=await withGatewayServiceRebindCapture(l,async()=>{try{return await s(()=>r(u,`local`,e.runtime,c({runtime:e.runtime,options:e.options,signal:e.signal}),{async writeConfig(e){assertInstallationCurrent();if(!a)throw new Error(`Doctor config writer is unavailable during service restoration.`);const t=await a(e);assertInstallationCurrent();return t},serviceMaintenance:{managerUid:t.serviceManagerUid,assertCurrent:assertInspectionCurrent,assertReadCurrent:assertInspectionCurrent,beforeMutation:async()=>{await assertAutoStartCurrent();e.assertInstallationAdmission?.()}}}))}finally{_=currentGatewayServiceRebindReceipt()?.after??l}})}catch(t){if(n.requiresInstallRootRefresh||hasCommandProcessCleanupError(t)||findServiceOwnershipRefusal(t)||!(t instanceof GatewayServiceAuthorityError)||t.outcome!==`unchanged`&&t.outcome!==`restored`)throw t;const r=`Warning: Gateway service repair failed; its previous definition was ${t.outcome}. Restoring the previously running service after verifying its definition and ownership. Reason: ${formatErrorMessage(t)}`;o.push(r);e.runtime.log(r)}assertInstallationCurrent();const d=t.stopped?await readCurrent():await readInspection();assertInstallationCurrent();if(!d){await mayRestore();return}assertDoctorServiceSelection(i,d.env);const f=await revalidate(d);assertInstallationCurrent();if(f.kind===`owned`&&!f.requiresInstallRootRefresh){if(n.requiresInstallRootRefresh||d.runtime?.status===`running`)return d;p=d}else if(t.stopped&&!n.requiresInstallRootRefresh)throw new Error(`Gateway service ownership changed during Doctor repair; inspect the service before restarting it.`);if(!t.stopped){p=d;v=f;if(!await mayRestore())return}}if(n.requiresInstallRootRefresh){const t=`Gateway service still targets ${n.root}; Doctor could not reconcile it with ${r}. The previous installation remains stopped because state compatibility is unverified. Run ${formatCliCommand(`openclaw gateway install --force`,i)} from the intended install.`;o.push(t);e.runtime.log(t);return}}if(g){const t=`Warning: Gateway restoration inspection was inconclusive: ${formatErrorMessage(g)} Starting the managed Gateway stopped by Doctor and verifying readiness.`;o.push(t);e.runtime.log(t)}assertMaintenanceCurrent();if(process.platform===`win32`&&h.isEnabled){const t=h.isEnabled;const r=await s(()=>t({env:p?.env??n}));assertMaintenanceCurrent();if(!r){const t=`Gateway Scheduled Task is disabled, so Doctor left the Gateway stopped. Start it with \`${formatCliCommand(`openclaw gateway start`,n)}\` (this re-enables the task), then check \`${formatCliCommand(`openclaw gateway status --deep`,n)}\`.`;o.push(t);e.runtime.log(t);return}}const y=p&&!g?h.restart:h.start;await s(async()=>{await y({env:p?.env??n,stdout:e.options.json?process.stderr:process.stdout,preserveDefinition:true,preserveAutoStart:true,beforeMutation:assertAutoStartCurrent,beforeEffect:assertMaintenanceCurrent,assertCurrent:assertInspectionCurrent,...t.serviceSystemdIdentity?{systemdIdentity:t.serviceSystemdIdentity}:{}})});assertMaintenanceCurrent();return p??{env:n,command:null}}),cfg:u}}async function recordDoctorGatewayRestorationRefusal({serviceEnv:e,error:t,before:n,warnings:r,failureFacts:i,runtime:a}){const{formatDaemonServiceInstallCommand:o}=await import(`./shared-KEMAEjD0.mjs`);const s=t.outcome??`recovery-pending`;const c=`Doctor could not finish Gateway installation or activation under its maintenance authority (${s}). ${s===`unchanged`?`The previous service definition was left unchanged.`:s===`restored`?`The previous service definition was restored from its captured backup.`:`Restoration was not verified; inspect the current service before replacing it.`} Run \`${formatCliCommand(`openclaw gateway status --deep`,e)}\`; after the active maintenance or update finishes, run \`${o(e,n.servicePort)}\` from the active CLI. Reason: ${t.message}`;i.push(createUpdateFailureFact({check:`gateway-restoration`,code:`${t.code}-${s}`,message:c},e));r.push(c);a.error(c);if(s===`recovery-pending`)throw new UpdateDoctorError(c,i,{cause:t})}export{finishDoctorGatewayMaintenance,recordDoctorGatewayRestorationRefusal,resolveUnstoppedGatewayRestoration,restoreDoctorGatewayService};