|
| 1 | +/** Shared state and owner-notification policy for cron auto-disable transitions. */ |
| 2 | +import { truncateUtf16Safe } from "@openclaw/normalization-core/utf16-slice"; |
| 3 | +import { parseAgentSessionKey } from "../../routing/session-key.js"; |
| 4 | +import type { CronJob, CronJobState } from "../types.js"; |
| 5 | +import { normalizeOptionalAgentId } from "./normalize.js"; |
| 6 | +import type { CronServiceState } from "./state.js"; |
| 7 | + |
| 8 | +type CronAutoDisableReason = NonNullable<CronJobState["autoDisabled"]>["reason"]; |
| 9 | + |
| 10 | +export type DeferredAutoDisableNotifications = Array<() => void>; |
| 11 | + |
| 12 | +/** |
| 13 | + * Run failures get more room than schedule errors (10 vs. 3) because provider |
| 14 | + * and network errors are often transient, and restart-interrupted runs count too. |
| 15 | + */ |
| 16 | +const MAX_CONSECUTIVE_RUN_FAILURES = 10; |
| 17 | + |
| 18 | +function autoDisableReasonLabel(reason: CronAutoDisableReason): string { |
| 19 | + return reason === "consecutive-failures" ? "run failures" : "schedule errors"; |
| 20 | +} |
| 21 | + |
| 22 | +/** Records one canonical auto-disable fact and queues its owning-agent notification. */ |
| 23 | +export function autoDisableCronJob(params: { |
| 24 | + state: CronServiceState; |
| 25 | + job: CronJob; |
| 26 | + reason: CronAutoDisableReason; |
| 27 | + atMs: number; |
| 28 | + consecutiveErrors: number; |
| 29 | + error: unknown; |
| 30 | + deferredNotifications?: DeferredAutoDisableNotifications; |
| 31 | +}): boolean { |
| 32 | + const { state, job } = params; |
| 33 | + if (!job.enabled || job.state.autoDisabled) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + job.enabled = false; |
| 38 | + job.state.nextRunAtMs = undefined; |
| 39 | + job.state.autoDisabled = { |
| 40 | + reason: params.reason, |
| 41 | + atMs: params.atMs, |
| 42 | + consecutiveErrors: params.consecutiveErrors, |
| 43 | + }; |
| 44 | + |
| 45 | + const name = truncateUtf16Safe((job.name || job.id).replace(/\s+/g, " ").trim(), 120); |
| 46 | + const error = truncateUtf16Safe(String(params.error).trim() || "unknown reason", 200); |
| 47 | + const text = [ |
| 48 | + `⚠️ Automation "${name}" (${job.id}) was auto-disabled after ${params.consecutiveErrors} consecutive ${autoDisableReasonLabel(params.reason)}.`, |
| 49 | + `Last error: ${error}`, |
| 50 | + `Fix the underlying cause, then run \`openclaw automations enable ${job.id}\` to re-enable it.`, |
| 51 | + ].join("\n"); |
| 52 | + const notify = () => { |
| 53 | + const agentId = |
| 54 | + normalizeOptionalAgentId(job.agentId) ?? |
| 55 | + normalizeOptionalAgentId(parseAgentSessionKey(job.sessionKey)?.agentId) ?? |
| 56 | + normalizeOptionalAgentId(state.deps.resolveDefaultAgentId?.()) ?? |
| 57 | + normalizeOptionalAgentId(state.deps.defaultAgentId); |
| 58 | + const deliveryContext = |
| 59 | + agentId || job.sessionKey |
| 60 | + ? state.deps.resolveOriginDeliveryContext?.({ |
| 61 | + agentId, |
| 62 | + sessionKey: job.sessionKey, |
| 63 | + }) |
| 64 | + : undefined; |
| 65 | + state.deps.enqueueSystemEvent(text, { |
| 66 | + agentId, |
| 67 | + sessionKey: job.sessionKey, |
| 68 | + contextKey: `cron:${job.id}:auto-disabled`, |
| 69 | + ...(deliveryContext ? { deliveryContext } : {}), |
| 70 | + }); |
| 71 | + state.deps.requestHeartbeat({ |
| 72 | + source: "cron", |
| 73 | + intent: "event", |
| 74 | + reason: `cron:${job.id}:auto-disabled`, |
| 75 | + agentId, |
| 76 | + sessionKey: job.sessionKey, |
| 77 | + }); |
| 78 | + }; |
| 79 | + |
| 80 | + if (params.deferredNotifications) { |
| 81 | + params.deferredNotifications.push(notify); |
| 82 | + } else { |
| 83 | + notify(); |
| 84 | + } |
| 85 | + return true; |
| 86 | +} |
| 87 | + |
| 88 | +/** Auto-disables only time-based recurring jobs once their run-error streak reaches the limit. */ |
| 89 | +export function maybeAutoDisableCronJobAfterRunFailure(params: { |
| 90 | + state: CronServiceState; |
| 91 | + job: CronJob; |
| 92 | + atMs: number; |
| 93 | + error: unknown; |
| 94 | + deferredNotifications?: DeferredAutoDisableNotifications; |
| 95 | +}): boolean { |
| 96 | + const consecutiveErrors = params.job.state.consecutiveErrors ?? 0; |
| 97 | + if ( |
| 98 | + (params.job.schedule.kind !== "cron" && params.job.schedule.kind !== "every") || |
| 99 | + consecutiveErrors < MAX_CONSECUTIVE_RUN_FAILURES |
| 100 | + ) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + return autoDisableCronJob({ |
| 104 | + ...params, |
| 105 | + reason: "consecutive-failures", |
| 106 | + consecutiveErrors, |
| 107 | + }); |
| 108 | +} |
0 commit comments