UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 3.93 kB
import{i as listAgentIds,u as tryResolveDefaultAgentId}from"./agent-roster-4XrgUgPK.mjs";import"./agent-scope-config-CG9CFpGX.mjs";import{a as getOpenClawDatabaseMaintenanceScope}from"./openclaw-state-maintenance-context-D-pYfco8.mjs";import{t as createSubsystemLogger}from"./subsystem-DLCDFvCH.mjs";import{i as executeSqliteQuerySync,s as getNodeSqliteKysely}from"./kysely-sync-R08Pt8qn.mjs";import{a as captureGatewayStateOwner}from"./gateway-state-owner-D_klysuh.mjs";import"./openclaw-state-db-async-lifecycle-DlV0ztcV.mjs";import{l as runOpenClawStateWriteTransaction}from"./openclaw-state-db-BlgxYkYw.mjs";import{t as DoctorMaintenanceRefusalError}from"./update-doctor-result-B09W3zwf.mjs";import{i as tryResolveCronJobEffectiveAgentId}from"./agent-id-DdcCg4fI.mjs";import{t as prepareCronRunReceiptWriteSchema}from"./run-receipt-write-admission-B6E5el88.mjs";import{r as receiptFromRow}from"./run-receipt-read-Cr31NISy.mjs";import{n as cloneCronJobForMutation}from"./jobs-mutation-DVY-k6BU.mjs";import{t as createCronMutationLogger}from"./runtime-mutation.worker-72fWR0tW.mjs";import{t as recoverCronJobInDatabase}from"./run-recovery.kernel-8pDih6JG.mjs"; //!src/commands/doctor-heartbeat-run-recovery.ts var DoctorHeartbeatMigrationDeferredError=class extends DoctorMaintenanceRefusalError{constructor(e){super(e,{kind:`deferred`,reason:`admission-unavailable`});this.name=`DoctorHeartbeatMigrationDeferredError`}};function recoverDoctorHeartbeatRun(e){const{database:t,storeKey:n,job:r,cfg:i,nowMs:a}=e;const o=getOpenClawDatabaseMaintenanceScope();const s=captureGatewayStateOwner(t.path);if(!o?.ownsSchemaMaintenance||s?.role!==`sqlite-maintenance`)throw new DoctorHeartbeatMigrationDeferredError(`Heartbeat migration deferred: automation ${r.id} is running or its runtime owner is unproved. Its job and configuration were retained; stop the Gateway and rerun openclaw doctor --fix.`);o.assertAdmission();s.assertCurrent();const c=cloneCronJobForMutation({...r,payload:r.payload.kind===`heartbeat`?{...r.payload,kind:`systemEvent`,text:``}:r.payload});const l=executeSqliteQuerySync(t.db,getNodeSqliteKysely(t.db).selectFrom(`cron_run_receipts`).selectAll().where(`store_key`,`=`,n).where(`job_id`,`=`,r.id).where(`status`,`=`,`running`)).rows[0];const u=l?receiptFromRow(l):void 0;if(u&&(u.startedAtMs!==r.state.runningAtMs||r.state.runningReceiptId!==void 0&&u.receiptId!==r.state.runningReceiptId))throw new DoctorHeartbeatMigrationDeferredError(`Heartbeat migration deferred: automation ${r.id} has a receipt that does not match its running marker. Its job and run were retained.`);const d=u?.agentId??tryResolveCronJobEffectiveAgentId(r)??(i.agents?.ownership===`explicit`?void 0:tryResolveDefaultAgentId(i));if(!d||!listAgentIds(i).includes(d)){const e=d?`run owner "${d}" is not configured`:`run owner is missing`;throw new DoctorHeartbeatMigrationDeferredError(`Heartbeat migration deferred: automation ${r.id} ${e}. Its job and run were retained.`)}const f=[];const p=[];const m=/* @__PURE__ */ new Error(`Cron recovery requires Gateway notification dispatch`);let h;try{h=runOpenClawStateWriteTransaction(()=>{const e=recoverCronJobInDatabase({database:t,storeKey:n,job:c,receipt:u,receiptSchema:prepareCronRunReceiptWriteSchema(t.db),state:{deps:{nowMs:()=>a,cronConfig:i.cron,defaultAgentId:d,log:createCronMutationLogger(p)}},notifications:f});if(f.length)throw m;return e},{database:t},{operationLabel:`doctor.heartbeat-run-recovery`})}catch(e){if(e!==m)throw e;createSubsystemLogger(`cron`).warn(`Automation ${r.id} converted; run recovery and its notifications will complete at the next Gateway start.`);return r}const g=createSubsystemLogger(`cron`);for(const e of p)g[e.level](e.message??`cron: heartbeat run recovery`,{fields:e.fields});if(h.removed)return;return r.payload.kind===`heartbeat`?{...c,payload:r.payload}:c}export{recoverDoctorHeartbeatRun as n,DoctorHeartbeatMigrationDeferredError as t};