openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 6.03 kB
JavaScript
import{t as createSubsystemLogger}from"./subsystem-DLCDFvCH.mjs";import{a as executeSqliteQueryTakeFirstSync,i as executeSqliteQuerySync,s as getNodeSqliteKysely}from"./kysely-sync-R08Pt8qn.mjs";import{l as runOpenClawStateWriteTransaction}from"./openclaw-state-db-BlgxYkYw.mjs";import{i as updateConfigMachineStateInDatabase,o as writeConfigMachineStateInDatabase}from"./config-machine-state-write-y_DUumZ_.mjs";import{t as GATEWAY_CRASH_STATE_KEY}from"./gateway-crash-DCoZGkY8.mjs";import{a as inspectGatewayCrashLoopBreakerInDatabase,i as buildGatewayCrashLoopBreakerDecision,n as GATEWAY_CRASH_LOOP_RECOVERED_REASON,r as GATEWAY_SIGNAL_REPEAT_WINDOW_MS,s as readGatewayCrashStateInDatabase,t as GATEWAY_CRASH_LOOP_BREAKER_REASON}from"./gateway-boot-lifecycle.kernel-y04qjfxr.mjs";import{randomUUID}from"node:crypto";
//!src/infra/gateway-boot-lifecycle.worker.ts
const GATEWAY_BOOT_LIFECYCLE_RETENTION_MS=864e5;const RESTART_RECOVERY_KEY=`gateway.restartRecovery`;const gatewayLifecycleLog=createSubsystemLogger(`gateway/lifecycle`);const gatewayBootOperations={"gatewayBoot.begin":(e,t)=>{const{nowMs:n,metadata:r,previousOwner:i}=e;let a=buildGatewayCrashLoopBreakerDecision({uncleanBoots:0});try{a=inspectGatewayCrashLoopBreakerInDatabase(t.open().db,n)}catch(e){gatewayLifecycleLog.warn(`crash-loop breaker state unavailable; fail-open: ${String(e)}`)}const o=a.tripped?a.shouldWriteStabilityBundle?GATEWAY_CRASH_LOOP_BREAKER_REASON:void 0:a.recovered?GATEWAY_CRASH_LOOP_RECOVERED_REASON:void 0;const s=randomUUID();try{const e=runOpenClawStateWriteTransaction(({db:e})=>{const t=getNodeSqliteKysely(e);const a=t.selectFrom(`gateway_boot_lifecycle`).select([`boot_id`,`pid`,`started_at_ms`,`outcome`,`startup_reason`,`reason`]);const c=executeSqliteQueryTakeFirstSync(e,a.orderBy(`started_at_ms`,`desc`).limit(1));let l;if(c&&(c.outcome===null||c.outcome===`forced_stop`)){let t=c;while(t.startup_reason===GATEWAY_CRASH_LOOP_RECOVERED_REASON){const n=executeSqliteQueryTakeFirstSync(e,a.where(`pid`,`=`,t.pid).where(`outcome`,`=`,`safe_mode_stable`).where(`completed_at_ms`,`=`,t.started_at_ms).where(`started_at_ms`,`<`,t.started_at_ms).orderBy(`started_at_ms`,`desc`).limit(1));if(!n)break;t=n}l={startedAt:t.started_at_ms,endedAt:n}}const u=readGatewayCrashStateInDatabase(e);let d;if(c&&(l||c.outcome===`startup_failed`)){const e=u.active?.bootId===c.boot_id?u.active:void 0;const t=i?.pid===c.pid?i.heartbeatAt:void 0;d={...e,bootId:c.boot_id,pid:c.pid,startedAtMs:l?.startedAt??c.started_at_ms,detectedAtMs:n,...c.reason?{reason:c.reason}:{},lastHeartbeatAtMs:t!==void 0&&t>=(l?.startedAt??c.started_at_ms)&&t<=n?t:void 0};u.recent=[d,...u.recent].slice(0,10)}u.active={...r,bootId:s};writeConfigMachineStateInDatabase(e,GATEWAY_CRASH_STATE_KEY,u);executeSqliteQuerySync(e,t.deleteFrom(`gateway_boot_lifecycle`).where(`started_at_ms`,`<`,n-GATEWAY_BOOT_LIFECYCLE_RETENTION_MS));executeSqliteQuerySync(e,t.insertInto(`gateway_boot_lifecycle`).values({boot_id:s,pid:process.pid,started_at_ms:n,completed_at_ms:null,outcome:null,startup_reason:o??null,reason:null}));const f=executeSqliteQuerySync(e,t.selectFrom(`agent_databases`).select(`agent_id`).distinct()).rows;const p=updateConfigMachineStateInDatabase(e,RESTART_RECOVERY_KEY,(e={})=>{const t=Object.fromEntries(f.flatMap(({agent_id:t})=>{const n=Object.hasOwn(e,t)?e[t]:[];if(l){const e=n.at(-1);if(e&&e.endedAt===l.startedAt)e.endedAt=l.endedAt;else n.push(l)}return n.length?[[t,n]]:[]}));return Object.keys(t).length?t:void 0},n);return{restartResumeWindow:l,restartResumeWindows:p,previousCrash:d,crashes:u.recent}},t.stateOptions());return{decision:a,id:s,...e}}catch(e){gatewayLifecycleLog.warn(`failed to persist gateway boot start; fail-open: ${String(e)}`);return{decision:a,id:void 0}}},"gatewayBoot.recoveredAgent":(e,t)=>runOpenClawStateWriteTransaction(({db:t})=>{updateConfigMachineStateInDatabase(t,RESTART_RECOVERY_KEY,t=>{if(!t||!Object.hasOwn(t,e.agentId))return t;const n=t[e.agentId]?.filter(t=>t.endedAt>e.through);if(n?.length)t[e.agentId]=n;else delete t[e.agentId];return Object.keys(t).length?t:void 0},Date.now())},t.stateOptions()),"gatewayBoot.enrichCrash":(e,t)=>runOpenClawStateWriteTransaction(({db:t})=>{const n=readGatewayCrashStateInDatabase(t);const r=n.recent.find(t=>t.bootId===e.bootId);if(r){Object.assign(r,e.evidence);writeConfigMachineStateInDatabase(t,GATEWAY_CRASH_STATE_KEY,n)}return{crash:r,crashes:n.recent}},t.stateOptions()),"gatewayBoot.complete":(e,t)=>runOpenClawStateWriteTransaction(({db:t})=>{const{bootId:n,completion:r,nowMs:i}=e;const a=getNodeSqliteKysely(t);executeSqliteQuerySync(t,a.updateTable(`gateway_boot_lifecycle`).set({completed_at_ms:i,outcome:r.outcome,...r.startupReason?{startup_reason:r.startupReason}:{},reason:r.reason??null}).where(`boot_id`,`=`,n));return e.countSignalStops?executeSqliteQueryTakeFirstSync(t,a.selectFrom(`gateway_boot_lifecycle`).select(e=>e.fn.countAll().as(`count`)).where(`outcome`,`=`,`clean_stop`).where(`reason`,`=`,r.reason??null).where(`completed_at_ms`,`>=`,i-GATEWAY_SIGNAL_REPEAT_WINDOW_MS))?.count:void 0},t.stateOptions()),"gatewayBoot.recover":(e,t)=>runOpenClawStateWriteTransaction(({db:t})=>{const n=e.nowMs??Date.now();const r=inspectGatewayCrashLoopBreakerInDatabase(t,n);if(!r.recovered||r.uncleanBoots!==0)return;const i=getNodeSqliteKysely(t);if(e.bootId){if(executeSqliteQuerySync(t,i.updateTable(`gateway_boot_lifecycle`).set({completed_at_ms:n,outcome:`safe_mode_stable`,reason:null}).where(`boot_id`,`=`,e.bootId).where(`completed_at_ms`,`is`,null)).numAffectedRows!==1n)return}const a=randomUUID();const o=readGatewayCrashStateInDatabase(t);if(o.active&&o.active.bootId===e.bootId){o.active.bootId=a;writeConfigMachineStateInDatabase(t,GATEWAY_CRASH_STATE_KEY,o)}executeSqliteQuerySync(t,i.insertInto(`gateway_boot_lifecycle`).values({boot_id:a,pid:process.pid,started_at_ms:n,completed_at_ms:null,outcome:null,startup_reason:GATEWAY_CRASH_LOOP_RECOVERED_REASON,reason:null}));return a},t.stateOptions())};export{gatewayBootOperations};