openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.3 kB
JavaScript
import{i as readConfigMachineStateRowInDatabase}from"./config-machine-state-row-DRffEZur.mjs";import{a as executeSqliteQueryTakeFirstSync,s as getNodeSqliteKysely}from"./kysely-sync-R08Pt8qn.mjs";import{t as GATEWAY_STARTUP_MAINTENANCE_REQUIRED_REASON}from"./startup-maintenance-required-Cz4Y_ofl.mjs";import{r as TAILSCALE_BACKEND_AUTH_REQUIRED_REASON,t as TAILSCALE_BACKEND_STOPPED_REASON}from"./tailscale-backend-stopped-error-DKvnIfGJ.mjs";import{t as GATEWAY_CRASH_STATE_KEY}from"./gateway-crash-DCoZGkY8.mjs";
//!src/infra/gateway-boot-lifecycle.kernel.ts
function readGatewayCrashStateInDatabase(e){const t=readConfigMachineStateRowInDatabase(e,GATEWAY_CRASH_STATE_KEY);return t?JSON.parse(t.value_json):{recent:[]}}const GATEWAY_BOOT_LOOP_UNCLEAN_THRESHOLD=3;const GATEWAY_BOOT_LOOP_WINDOW_MS=3e5;const GATEWAY_SIGNAL_REPEAT_WINDOW_MS=3e5;const GATEWAY_CRASH_LOOP_BREAKER_REASON=`gateway.crash_loop_breaker`;const GATEWAY_CRASH_LOOP_RECOVERED_REASON=`gateway.crash_loop_recovered`;const maintenanceStartupReasons=[GATEWAY_STARTUP_MAINTENANCE_REQUIRED_REASON,`gateway.agent_media_migration_required`];function buildGatewayCrashLoopBreakerDecision(e){const t=e.uncleanBoots>=GATEWAY_BOOT_LOOP_UNCLEAN_THRESHOLD;const n=typeof e.latestBreakerStartedAtMs===`number`&&(typeof e.latestRecoveryStartedAtMs!==`number`||e.latestRecoveryStartedAtMs<e.latestBreakerStartedAtMs);return{tripped:t,recoveryPausedUntilMs:n&&e.latestUncleanAtMs!=null?e.latestUncleanAtMs+GATEWAY_BOOT_LOOP_WINDOW_MS+1:void 0,uncleanBoots:e.uncleanBoots,windowMs:GATEWAY_BOOT_LOOP_WINDOW_MS,shouldWriteStabilityBundle:t&&!n,recovered:!t&&n}}function inspectGatewayCrashLoopBreakerInDatabase(e,t){const n=getNodeSqliteKysely(e);const r=t-GATEWAY_BOOT_LOOP_WINDOW_MS;const i=executeSqliteQueryTakeFirstSync(e,n.selectFrom(`gateway_boot_lifecycle`).select(e=>[e.fn.countAll().as(`count`),e.fn.max(e.fn.coalesce(`completed_at_ms`,`started_at_ms`)).as(`latestUncleanAtMs`),e.selectFrom(`gateway_boot_lifecycle`).select(`started_at_ms`).where(`startup_reason`,`=`,GATEWAY_CRASH_LOOP_BREAKER_REASON).orderBy(`started_at_ms`,`desc`).limit(1).as(`latestBreakerStartedAtMs`),e.selectFrom(`gateway_boot_lifecycle`).select(`started_at_ms`).where(`startup_reason`,`=`,GATEWAY_CRASH_LOOP_RECOVERED_REASON).orderBy(`started_at_ms`,`desc`).limit(1).as(`latestRecoveryStartedAtMs`)]).where(e=>e.or([e(`startup_reason`,`is`,null),e(`startup_reason`,`not in`,maintenanceStartupReasons)])).where(e=>e.or([e.and([e(`completed_at_ms`,`is`,null),e(`started_at_ms`,`>=`,r)]),e.and([e(`outcome`,`=`,`startup_failed`),e.or([e(`startup_reason`,`is`,null),e(`startup_reason`,`!=`,TAILSCALE_BACKEND_STOPPED_REASON)]),e(`completed_at_ms`,`>=`,r),e.or([e(`startup_reason`,`is`,null),e(`startup_reason`,`!=`,TAILSCALE_BACKEND_AUTH_REQUIRED_REASON)])])])));return buildGatewayCrashLoopBreakerDecision({uncleanBoots:i?.count??0,latestUncleanAtMs:i?.latestUncleanAtMs,latestBreakerStartedAtMs:i?.latestBreakerStartedAtMs,latestRecoveryStartedAtMs:i?.latestRecoveryStartedAtMs})}export{inspectGatewayCrashLoopBreakerInDatabase as a,buildGatewayCrashLoopBreakerDecision as i,GATEWAY_CRASH_LOOP_RECOVERED_REASON as n,maintenanceStartupReasons as o,GATEWAY_SIGNAL_REPEAT_WINDOW_MS as r,readGatewayCrashStateInDatabase as s,GATEWAY_CRASH_LOOP_BREAKER_REASON as t};