UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

211 lines (210 loc) • 8.69 kB
import { t as assertTransactionUsable } from "./sqlite-transaction-BHHu6P5-.mjs"; import { a as deferSqliteWorkerCommitReceipt } from "./sqlite-worker-operation-settlement-OZQM-wtd.mjs"; import "./sqlite-worker-operation-admission-9E_X-oRy.mjs"; import { a as getSqliteWorkerStateContext } from "./sqlite-worker-state-context-CR8h__Vh.mjs"; import { l as runOpenClawStateWriteTransaction } from "./openclaw-state-db-ARhwdPJg.mjs"; import { t as isAgentDeletionBlocked } from "./agent-lifecycle-registry-CdPY7sE9.mjs"; import { n as requestSessionEntryCurrentAdmission } from "./session-entry-current-admission.worker-DJux32Nc.mjs"; import { c as snapshotFromExecApprovalsDatabase } from "./exec-approvals-sqlite-UEkhBKzL.mjs"; import { t as operatorApprovalPublication } from "./operator-approval-store.publication-BqIwpe_k.mjs"; import { _ as projectCronJobStateThroughStorageCodec, m as updateCronRuntimeRow, y as cronSchedulingInputsEqual } from "./row-codec-D4aymQkb.mjs"; import { n as captureCronJobConfigRevision, r as resolveCronJobConfigRevision } from "./config-revision-ChGg5L01.mjs"; import { o as cronStreamExecApprovalBinding } from "./stream-exec-authority-CRx6XpZo.mjs"; import { o as recordSkippedCronRequestInDatabase } from "./run-history.kernel-DNcW99NJ.mjs"; import { t as prepareCronRunReceiptWriteSchema } from "./run-receipt-write-admission-CFr_ZnYD.mjs"; import { t as CronJobsStoreChangedError } from "./save-error--5eRHFlj.mjs"; import { a as saveCronStoreChangesInDatabase, n as loadCronStoreForChanges, r as prepareCronStoreChanges } from "./save.kernel-CIEI6re-.mjs"; import { a as finishCronRunReceiptInDatabase, i as findActiveCronRunReceiptInDatabase } from "./run-receipt-store-xxvCGOD_.mjs"; import { n as CronAgentDeletionBlockedError, t as planCronJobMutation } from "./job-mutations-ByBYgxFn.mjs"; import { n as retireCronRunTriggerStateInDatabase, r as findCronRunRecoveryInDatabase } from "./run-receipt-trigger-state-DPJ-611O.mjs"; import { t as consumeOperatorApprovalAllowOnceInDatabase } from "./operator-approval-store.transitions.worker-CXc04Q-9.mjs"; import { n as installCronSchedulerSnapshot, t as getCronSchedulerSnapshot } from "./scheduler-config.worker-DBsnj1-L.mjs"; import { randomUUID } from "node:crypto"; import { isDeepStrictEqual } from "node:util"; //#region src/cron/store/guarded-mutation.worker.ts function retireCronMutationTriggerState(e, t, n, r) { const i = findActiveCronRunReceiptInDatabase({ database: e, storePath: t, jobId: n }); if (i) { retireCronRunTriggerStateInDatabase({ database: e, handle: i }); return; } const a = r?.state.runningAtMs; if (!r || a === void 0) return; const o = r.state.runningReceiptId ?? findCronRunRecoveryInDatabase({ database: e, jobId: n, storeKey: t, startedAt: a }).receiptId; if (o) retireCronRunTriggerStateInDatabase({ database: e, handle: { receiptId: o, storeKey: t, jobId: n, startedAtMs: a } }); } function applyCronReceiptMutation(e, t, n) { const r = t.receiptMutation; if (!r) return; if (r.scheduleChanged) { const e = n.nextById.get(r.jobId); if (!e) throw new Error("Cron schedule mutation has no target job"); if (n.previousById.get(r.jobId)?.state.runningAtMs !== void 0) e.state.runningScheduleChangeId = randomUUID(); else delete e.state.runningScheduleChangeId; } if (r.triggerStateChanged) retireCronMutationTriggerState(e, t.storeKey, r.jobId, n.previousById.get(r.jobId)); } function cancelChangedQueuedRuns(e, t, n, r, i, a) { const o = new Map(i.jobs.map((e) => [e.id, e])); for (const i of r) { if (i.state.queuedAtMs === void 0 || !n.changedIds.has(i.id)) continue; const r = o.get(i.id); const s = r && i.schedule.kind === "on-exit" && !i.enabled && r.enabled && cronSchedulingInputsEqual({ ...i, enabled: true }, r); if (r && (cronSchedulingInputsEqual(i, r) || s)) continue; const c = findActiveCronRunReceiptInDatabase({ database: e, storePath: t.storeKey, jobId: i.id }); const l = r ? "cron: queued job schedule changed or was disabled" : "cron: queued job was removed"; if (c && c.startedAtMs === i.state.queuedAtMs) { finishCronRunReceiptInDatabase({ database: e, receiptSchema: a, handle: c, pendingReceiptId: r?.state.runningAtMs === void 0 ? c.receiptId : r.state.runningReceiptId, status: "skipped", finishedAtMs: t.snapshot.nowMs, error: l }); recordSkippedCronRequestInDatabase(e, { storeKey: t.storeKey, jobId: c.jobId, receiptId: c.receiptId, agentId: c.agentId, startedAt: c.startedAtMs, endedAt: t.snapshot.nowMs, error: l, nextRunAtMs: r?.state.nextRunAtMs }); } if (r?.state.queuedAtMs === i.state.queuedAtMs) { delete r.state.queuedAtMs; updateCronRuntimeRow(e, t.storeKey, r); } } } function mutateCronJobsInWorker(e, t) { const n = { ...(t.snapshot.scheduler ? installCronSchedulerSnapshot(t.storeKey, t.snapshot.scheduler, e.path) : getCronSchedulerSnapshot(t.storeKey, e.path))?.config, nowMs: t.snapshot.nowMs }; const r = t.preconditionJob ? { ...t.preconditionJob, state: projectCronJobStateThroughStorageCodec(t.preconditionJob.state) } : void 0; let i; try { return runOpenClawStateWriteTransaction(({ db: a }) => { try { const i = prepareCronRunReceiptWriteSchema(a); const o = loadCronStoreForChanges(a, t.storeKey); const s = o.store; if (t.agentId !== void 0 && isAgentDeletionBlocked(t.agentId, {}, a)) throw new CronAgentDeletionBlockedError(t.agentId); const c = t.expectedJob; const l = c ? s.jobs.find((e) => e.id === c.id) : void 0; if (c && (!l || resolveCronJobConfigRevision(l) !== c.configRevision)) throw new CronJobsStoreChangedError(t.storeKey); if (r) { const e = s.jobs.find((e) => e.id === r.id); if (!e || !isDeepStrictEqual({ ...structuredClone(e), state: projectCronJobStateThroughStorageCodec(e.state) }, r)) throw new CronJobsStoreChangedError(t.storeKey); } const u = planCronJobMutation(s, t.jobMutation, n); const d = t.streamSecurity; const f = t.jobMutation.kind === "add" && u.created === false && u.updated === false; const p = d && u.job ? { mutation: { method: d.method, job: u.job, previousJob: s.jobs.find((e) => e.id === u.job?.id) }, unchangedDeclaration: f, ...d.checkExecPolicy && !f ? { execApprovals: snapshotFromExecApprovalsDatabase(a, getSqliteWorkerStateContext().environment).file } : {} } : void 0; if (d) { if (!p) throw new Error("Stream mutation has no planned job"); requestSessionEntryCurrentAdmission(d.sessionEntryCurrentSource, { stage: "transaction", facts: p }); } const m = prepareCronStoreChanges(s, u.next); const h = { receiptSchema: i, hooks: { beforeWrite: () => { applyCronReceiptMutation(a, t, m); } } }; const g = saveCronStoreChangesInDatabase(a, t.storeKey, t.storeKey, m, o, void 0, h); cancelChangedQueuedRuns(a, t, m, s.jobs, g, i); const _ = d ? operatorApprovalPublication.capture(a, () => { if (!d.approval || !p || p.unchangedDeclaration || d.approval.binding !== cronStreamExecApprovalBinding(p.mutation)) return; const t = consumeOperatorApprovalAllowOnceInDatabase({ ...d.approval.input, databaseOptions: { database: e } }); if (t.outcome !== "consumed") throw new Error("Stream approval expired or was already consumed; ask for approval again."); return t.record; }) : void 0; if (d) { deferSqliteWorkerCommitReceipt(a, { approvalFacts: operatorApprovalPublication.bound(_.receipt), consumedStreamApproval: _?.result }, "commit"); requestSessionEntryCurrentAdmission(d.sessionEntryCurrentSource, { stage: "commit", facts: p }); } return { outcome: { store: g, configRevisions: new Map(t.includeConfigRevisions ? g.jobs.map((e) => [e.id, captureCronJobConfigRevision(e)]) : []), ..._?.result ? { consumedStreamApproval: _.result } : {}, names: o.names, changed: m.changedIds.size > 0, notifications: u.notifications, logs: u.logs } }; } catch (e) { if (e instanceof CronJobsStoreChangedError) i = e; throw e; } }, { database: e, path: e.path, env: getSqliteWorkerStateContext().environment }, { operationLabel: "cron.config-mutation" }); } catch (t) { if (!i || t !== i) throw t; assertTransactionUsable(e.db); if (!e.db.isOpen || e.db.isTransaction) throw t; return { mutationRefusal: { kind: "store-changed" } }; } } //#endregion export { mutateCronJobsInWorker };