UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 2.78 kB
import{a as e,i as t}from"./openclaw-state-read-kysely-sync-Bcu0Qb7F.mjs";import{t as n}from"./openclaw-state-read-startup-maintenance-required-DR0e3-Lc.mjs";import{r}from"./openclaw-state-read-config-machine-state-row-DjFRA9Zl.mjs";function readGatewayCrashStateInDatabase(e){let t=r(e,`gateway.crashes`);return t?JSON.parse(t.value_json):{recent:[]}}const i=3e5,a=[n,`gateway.agent_media_migration_required`];function buildGatewayCrashLoopBreakerDecision(e){let t=e.uncleanBoots>=3,n=typeof e.latestBreakerStartedAtMs==`number`&&(typeof e.latestRecoveryStartedAtMs!=`number`||e.latestRecoveryStartedAtMs<e.latestBreakerStartedAtMs);return{tripped:t,recoveryPausedUntilMs:n&&e.latestUncleanAtMs!=null?e.latestUncleanAtMs+i+1:void 0,uncleanBoots:e.uncleanBoots,windowMs:i,shouldWriteStabilityBundle:t&&!n,recovered:!t&&n}}function inspectGatewayCrashLoopBreakerInDatabase(n,r){let o=e(n),s=r-i,c=t(n,o.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`).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`).orderBy(`started_at_ms`,`desc`).limit(1).as(`latestRecoveryStartedAtMs`)]).where(e=>e.or([e(`startup_reason`,`is`,null),e(`startup_reason`,`not in`,a)])).where(e=>e.or([e.and([e(`completed_at_ms`,`is`,null),e(`started_at_ms`,`>=`,s)]),e.and([e(`outcome`,`=`,`startup_failed`),e.or([e(`startup_reason`,`is`,null),e(`startup_reason`,`!=`,`gateway.tailscale_backend_stopped`)]),e(`completed_at_ms`,`>=`,s)])])));return buildGatewayCrashLoopBreakerDecision({uncleanBoots:c?.count??0,latestUncleanAtMs:c?.latestUncleanAtMs,latestBreakerStartedAtMs:c?.latestBreakerStartedAtMs,latestRecoveryStartedAtMs:c?.latestRecoveryStartedAtMs})}function readGatewayLastShutdownInDatabase(n){let r=t(n,e(n).selectFrom(`gateway_boot_lifecycle`).select([`pid`,`reason`,`completed_at_ms as completedAtMs`]).where(`outcome`,`in`,[`clean_stop`,`planned_restart`,`forced_stop`]).where(`completed_at_ms`,`is not`,null).orderBy(`completed_at_ms`,`desc`).limit(1));return r?.completedAtMs==null?void 0:{...r,completedAtMs:r.completedAtMs}}const o={"gatewayBoot.crashes":(e,t)=>({type:`gatewayBoot.crashes`,crashes:readGatewayCrashStateInDatabase(t).recent}),"gatewayBoot.lastShutdown":(e,t)=>({type:`gatewayBoot.lastShutdown`,shutdown:readGatewayLastShutdownInDatabase(t)}),"gatewayBoot.breaker":(e,t)=>({type:`gatewayBoot.breaker`,decision:inspectGatewayCrashLoopBreakerInDatabase(t,e)})};export{o as gatewayBootReadOperations};