openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
545 lines (544 loc) • 23.1 kB
JavaScript
import { o as asDateTimestampMs } from "./number-coercion-D6u-4YD8.mjs";
import "./src-H3RMXyFL.mjs";
import { t as expectDefined } from "./expect-DDakjvvQ.mjs";
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { t as pruneMapToMaxSize } from "./map-size-BBr20tbc.mjs";
import { n as formatErrorMessageWithCode } from "./errors-BKzCeTYb.mjs";
import { c as normalizeOptionalAgentId, j as parseAgentSessionKey } from "./session-key-CqAyZeVk.mjs";
import { p as isCronJobActive } from "./active-jobs-DAhIn24S.mjs";
import { o as resolveCronDeliverySessionKey } from "./session-target-C-4MLah6.mjs";
import { n as resolveCronTriggerMinIntervalMs } from "./cron-limits-ek-0PdRl.mjs";
import { a as hasCanonicalCronDeliveryMode } from "./delivery-codec-BaSNePUi.mjs";
import { t as coerceFiniteScheduleNumber } from "./schedule-number-uhcarNZz.mjs";
import { n as resolveCronStaggerMs } from "./stagger-Bj0_2RgJ.mjs";
import { s as resolveCronStreamBatching } from "./stream-schedule-BFX6oe0X.mjs";
import { n as cronSourceIdentity, o as setCronSourceIdentity, t as createCronSourceIdentity } from "./source-schedule-DlDKIQwQ.mjs";
import { n as computePreviousRunAtMs, t as computeNextRunAtMs } from "./schedule-DAqfX_yg.mjs";
import { i as resolveForcePreservedOneShotAtMs, n as computeOneShotNextRunAtMs, t as clearInvalidForcePreservedNextRun } from "./one-shot-schedule-Ff0uCKas.mjs";
import crypto from "node:crypto";
//#region src/cron/failure-notification-text.ts
const GENERIC_FAILURE_DETAIL = "Check automation history for details.";
const SCRIPT_FAILURE_COPY = {
aborted: "was aborted",
invalid_input: "received invalid input",
runtime_unavailable: "runtime is unavailable",
timeout: "timed out",
output_limit_exceeded: "exceeded its output limit",
snapshot_limit_exceeded: "exceeded its state limit",
internal_error: "failed internally",
tool_budget_exceeded: "exceeded its tool budget"
};
/** Renders only classified reasons or closed producer-authored failure facts. */
function cronFailureDetailLines(e, t) {
if (e) return e === "model_not_found" ? [
`Cause: ${e}`,
"Run `openclaw doctor --fix` to repair provider-declared retired model references.",
"Choose a supported model for this automation or remove its model override to use the agent default. If the agent default is unavailable, update it too."
] : [`Cause: ${e}`];
if (!t) return [GENERIC_FAILURE_DETAIL];
if (t.kind === "invalid-target") return ["Cause: invalid automation target. Update agentId or sessionTarget before re-enabling."];
if (t.kind === "command-exit") return [`Cause: command exited with code ${t.exitCode}`];
if (t.kind === "command-timeout") return [t.mode === "wall-clock" ? "Cause: command timed out" : "Cause: command stopped after producing no output"];
const n = t.source === "payload" ? "automation script" : "trigger script";
if (t.code === "plugin_reload_failed") return [
"Cause: tools could not be refreshed after a plugin reload.",
`The ${n} did not run. Automatic setup recovery failed.`,
"Check automation history and plugin status, then retry the automation."
];
return [`Cause: ${n} ${SCRIPT_FAILURE_COPY[t.code]}`];
}
//#endregion
//#region src/cron/service/notification-intents.ts
function captureCronNotificationRouting(e, t) {
const n = normalizeOptionalAgentId(e) ?? normalizeOptionalAgentId(t);
return n === void 0 ? {} : { defaultAgentId: n };
}
function cronNotificationJob(e) {
return {
id: e.id,
name: e.name,
agentId: e.agentId,
sessionTarget: e.sessionTarget,
sessionKey: e.sessionKey,
owner: e.owner ? { ...e.owner } : void 0,
wakeMode: e.wakeMode,
state: {
lastRunAtMs: e.state.lastRunAtMs,
lastFailureAlertAtMs: e.state.lastFailureAlertAtMs,
lastFailureNotificationId: e.state.lastFailureNotificationId
}
};
}
function resolveCronNotificationQueueOwner(e, t) {
const n = t === "auto-disabled" ? e.owner : void 0;
const r = n?.sessionKey?.trim() || void 0;
const i = t === "failure-alert" ? resolveCronDeliverySessionKey(e) : r ?? e.sessionKey;
return {
agentId: normalizeOptionalAgentId(n?.agentId) ?? normalizeOptionalAgentId(parseAgentSessionKey(r)?.agentId) ?? normalizeOptionalAgentId(e.agentId) ?? normalizeOptionalAgentId(parseAgentSessionKey(i)?.agentId),
sessionKey: i
};
}
//#endregion
//#region src/cron/service/auto-disable.ts
/** Shared state and owner-notification policy for cron auto-disable transitions. */
/**
* Run failures get more room than schedule errors (10 vs. 3) because provider
* and network errors are often transient.
*/
const MAX_CONSECUTIVE_RUN_FAILURES = 10;
function autoDisableReasonLabel(e) {
return e === "consecutive-failures" ? "run failures" : "schedule errors";
}
/** The safety notice text: the same words reach the failure route and the owning agent. */
function buildCronAutoDisableNotice(e) {
const { job: t } = e;
const n = truncateUtf16Safe((t.name || t.id).replace(/\s+/g, " ").trim(), 120);
const r = e.reason === "consecutive-failures" ? t.state.lastErrorReason : void 0;
return [
`⚠️ Automation "${n}" was auto-disabled after ${e.consecutiveErrors} consecutive ${autoDisableReasonLabel(e.reason)}.`,
...cronFailureDetailLines(r, e.failureNotificationDetail),
`Fix the underlying cause, then run \`openclaw automations enable ${t.id}\` to re-enable it.`
].join("\n");
}
/** Queues the in-app safety notice for the owning agent. */
function queueCronAutoDisableNotification(e) {
e.deferredNotifications.push({
kind: "auto-disabled",
job: cronNotificationJob(e.job),
text: buildCronAutoDisableNotice(e)
});
}
/** Records one canonical auto-disable fact and queues its owning-agent notification. */
function autoDisableCronJob(e) {
const { job: t } = e;
if (!t.enabled && !e.terminalOneShot || t.state.autoDisabled) return false;
t.enabled = false;
t.state.nextRunAtMs = void 0;
t.state.autoDisabled = {
reason: e.reason,
atMs: e.atMs,
consecutiveErrors: e.consecutiveErrors
};
if (e.notify === false) return true;
queueCronAutoDisableNotification(e);
return true;
}
/** Auto-disables only time-based recurring jobs once their run-error streak reaches the limit. */
function maybeAutoDisableCronJobAfterRunFailure(e) {
const t = e.job.state.consecutiveErrors ?? 0;
if (e.job.schedule.kind !== "cron" && e.job.schedule.kind !== "every" || t < MAX_CONSECUTIVE_RUN_FAILURES) return false;
return autoDisableCronJob({
...e,
reason: "consecutive-failures",
consecutiveErrors: t
});
}
//#endregion
//#region src/cron/service/timer-constants.ts
const MAX_CRON_TIMER_DELAY_MS = 6e4;
/**
* Minimum gap between consecutive fires of the same cron job. This is a
* safety net that prevents spin-loops when `computeJobNextRunAtMs` returns
* a value within the same second as the just-completed run. The guard
* is intentionally generous (2 s) so it never masks a legitimate schedule
* but always breaks an infinite re-trigger cycle. (See #17821)
*/
const MIN_REFIRE_GAP_MS = 2e3;
const DEFAULT_MISSED_JOB_STAGGER_MS = 5e3;
const DEFAULT_STARTUP_DEFERRED_MISSED_AGENT_JOB_DELAY_MS = 12e4;
//#endregion
//#region src/cron/service/trigger-interval.ts
function hasPendingCronTriggerInterval(e, t) {
const n = asDateTimestampMs(e.state.nextRunAtMs);
const r = Math.max(e.updatedAtMs, e.state.lastTriggerEvalAtMs ?? 0);
return e.trigger !== void 0 && n !== void 0 && n > 0 && t < n && n <= r + resolveCronTriggerMinIntervalMs();
}
//#endregion
//#region src/cron/service/jobs-scheduling.ts
/** Scheduling state and next-run computation for cron jobs. */
/** A new timer receipt owns continuation; never reopen the interrupted occurrence. */
function resolveCronRestartRetryAtMs(e, t, n) {
if (!isJobEnabled(e) || e.payload.kind !== "agentTurn" || ![
"at",
"every",
"cron"
].includes(e.schedule.kind)) return;
const r = e.schedule.kind === "at" ? MIN_REFIRE_GAP_MS : errorBackoffMs(normalizeConsecutiveErrors(e.state.consecutiveErrors));
return Math.max(n, t) + r;
}
const staggerOffsetCache = /* @__PURE__ */ new Map();
const TIME_SCHEDULE_STATE_FIELDS = [
"startupCatchupAtMs",
"pacedNextRunAtMs",
"forcePreservedNextRunAtMs",
"nextRunAtMs"
];
function ownsCronRunMarker(e, t, n, r = false) {
const i = e.reservations.get(t);
return i?.markerAtMs === n && (!r || i.preserveWhenDisabled);
}
function normalizeStreamScheduleBounds(e) {
if (e.kind !== "stream") return e;
const t = resolveCronStreamBatching(e);
return {
...e,
...e.batchMs !== void 0 ? { batchMs: t.batchMs } : {},
...e.maxBatchBytes !== void 0 ? { maxBatchBytes: t.maxBatchBytes } : {}
};
}
/** Default retry delays applied after consecutive cron execution errors. */
const DEFAULT_ERROR_BACKOFF_SCHEDULE_MS = [
3e4,
6e4,
3e5,
9e5,
36e5
];
function isFiniteTimestamp(e) {
return asDateTimestampMs(e) !== void 0;
}
/** Returns whether a stored next-run timestamp is finite and schedulable. */
function hasScheduledNextRunAtMs(e) {
return isFiniteTimestamp(e) && e > 0;
}
/** Rejects outcome-generated schedule timestamps before they can persist or arm a timer. */
function resolveNextRunAtMsOrDisable(e) {
const t = asDateTimestampMs(e.candidate);
if (t !== void 0 && t > 0) return t;
autoDisableCronJob({
job: e.job,
reason: "schedule-errors",
atMs: e.state.deps.nowMs(),
consecutiveErrors: 1,
deferredNotifications: e.deferredNotifications
});
}
/** Resolves the newest persisted cron run status while older state is still readable. */
function resolveJobLastRunStatus(e) {
return e.state.lastRunStatus ?? e.state.lastStatus;
}
/** Resolves run end using the same persisted duration semantics as retry backoff. */
function resolveJobLastRunEndedAtMs(e) {
if (!isFiniteTimestamp(e.state.lastRunAtMs)) return;
const t = typeof e.state.lastDurationMs === "number" && Number.isFinite(e.state.lastDurationMs) ? Math.max(0, Math.floor(e.state.lastDurationMs)) : 0;
return e.state.lastRunAtMs + t;
}
/** Resolves the retry backoff delay for a one-based consecutive error count. */
function errorBackoffMs(e, t = DEFAULT_ERROR_BACKOFF_SCHEDULE_MS) {
const n = Math.min(e - 1, t.length - 1);
return expectDefined(t[Math.max(0, n)], "schedule ms entry at math.max(0, idx)");
}
function normalizeConsecutiveErrors(e) {
return typeof e === "number" && Number.isFinite(e) ? Math.max(0, Math.floor(e)) : 0;
}
/** Returns the earliest retry timestamp after a failed cron run and its runtime duration. */
function resolveJobErrorBackoffUntilMs(e, t = DEFAULT_ERROR_BACKOFF_SCHEDULE_MS) {
if (resolveJobLastRunStatus(e) !== "error") return;
const n = resolveJobLastRunEndedAtMs(e);
if (n === void 0) return;
const r = normalizeConsecutiveErrors(e.state.consecutiveErrors);
return asDateTimestampMs(n + errorBackoffMs(r, t));
}
function resolveStableCronOffsetMs(e, t) {
if (t <= 1) return 0;
const n = `${t}:${e}`;
const r = staggerOffsetCache.get(n);
if (r !== void 0) return r;
const i = crypto.createHash("sha256").update(e).digest().readUInt32BE(0) % t;
pruneMapToMaxSize(staggerOffsetCache, 4095);
staggerOffsetCache.set(n, i);
return i;
}
function computeStaggeredCronNextRunAtMs(e, t) {
if (e.schedule.kind !== "cron") return computeNextRunAtMs(e.schedule, t);
const n = resolveCronStaggerMs(e.schedule);
const r = resolveStableCronOffsetMs(e.id, n);
if (r <= 0) return computeNextRunAtMs(e.schedule, t);
let i = Math.max(0, t - r);
for (let n = 0; n < 4; n += 1) {
const n = computeNextRunAtMs(e.schedule, i);
if (n === void 0) return;
const a = n + r;
if (isFiniteTimestamp(a) && a > t) return a;
i = Math.max(i + 1, n + 1e3);
}
}
function computeStaggeredCronPreviousRunAtMs(e, t) {
if (e.schedule.kind !== "cron") return;
const n = resolveCronStaggerMs(e.schedule);
const r = resolveStableCronOffsetMs(e.id, n);
if (r <= 0) return computePreviousRunAtMs(e.schedule, t);
let i = Math.max(0, t - r);
for (let n = 0; n < 4; n += 1) {
const n = computePreviousRunAtMs(e.schedule, i);
if (n === void 0) return;
const a = n + r;
if (isFiniteTimestamp(a) && a <= t) return a;
i = Math.max(0, n - 1e3);
}
}
function computeStaggeredCronPreviousRunAtOrBeforeMs(e, t) {
const n = computeStaggeredCronPreviousRunAtMs(e, t);
const r = t + 1e3;
if (!isFiniteTimestamp(r)) return n;
const i = computeStaggeredCronPreviousRunAtMs(e, r);
if (isFiniteTimestamp(i) && i <= t && (!isFiniteTimestamp(n) || i > n)) return i;
return n;
}
function isStaggeredCronRunAtMs(e, t) {
if (e.schedule.kind !== "cron" || !isFiniteTimestamp(t)) return false;
return computeStaggeredCronPreviousRunAtOrBeforeMs(e, t) === t;
}
function isStaleFutureCronSlot(e, t) {
const n = e.state.nextRunAtMs;
if (e.schedule.kind !== "cron" || !hasScheduledNextRunAtMs(n) || t >= n || typeof e.state.queuedAtMs === "number" || typeof e.state.runningAtMs === "number") return false;
const r = resolveJobErrorBackoffUntilMs(e, DEFAULT_ERROR_BACKOFF_SCHEDULE_MS);
if (r !== void 0 && t < r && n <= r || hasPendingCronTriggerInterval(e, t)) return false;
let i;
try {
i = computeStaggeredCronNextRunAtMs(e, t);
} catch {
return false;
}
if (!isFiniteTimestamp(i) || n === i) return false;
let a;
try {
a = isStaggeredCronRunAtMs(e, n);
} catch {
return false;
}
if (a) return false;
if (n < i) return e.payload.kind !== "agentTurn";
let o;
try {
o = computeStaggeredCronNextRunAtMs(e, i);
} catch {
return false;
}
if (!isFiniteTimestamp(o)) return false;
const s = o - i;
return s > 0 && n >= o + s;
}
function resolveEveryAnchorMs(e) {
const t = coerceFiniteScheduleNumber(e.schedule.anchorMs);
if (t !== void 0) return Math.max(0, Math.floor(t));
if (isFiniteTimestamp(e.fallbackAnchorMs)) return Math.max(0, Math.floor(e.fallbackAnchorMs));
return 0;
}
function hasInvalidExplicitEveryAnchor(e) {
if (e.anchorMs === void 0) return false;
const t = coerceFiniteScheduleNumber(e.anchorMs);
return t === void 0 || t < 0;
}
/** Finds an in-memory cron job or throws the public unknown-id error. */
function findJobOrThrow(e, t) {
const n = e.store?.jobs.find((e) => e.id === t);
if (!n) throw new Error(`unknown cron job id: ${t}`);
return n;
}
/** Returns the effective enabled flag, defaulting missing values to enabled. */
function isJobEnabled(e) {
return e.enabled ?? true;
}
function isTimeScheduledJob(e) {
return e.schedule.kind !== "on-exit" && e.schedule.kind !== "stream" && e.schedule.kind !== "event";
}
/** Computes the next run timestamp for enabled jobs across every/at/cron schedules. */
function computeJobNextRunAtMs(e, t, n) {
if (!isJobEnabled(e)) return;
if (e.schedule.kind === "every") {
if (hasInvalidExplicitEveryAnchor(e.schedule)) return;
const r = coerceFiniteScheduleNumber(e.schedule.everyMs);
if (r === void 0) return;
const i = Math.floor(r);
if (i < 1) return;
const a = isFiniteTimestamp(e.createdAtMs) ? e.createdAtMs : t;
const o = resolveEveryAnchorMs({
schedule: e.schedule,
fallbackAnchorMs: a
});
const s = n?.preserveEveryAnchor ? void 0 : e.state.lastRunAtMs;
if (isFiniteTimestamp(s) && s >= o) {
const e = Math.floor(s) + i;
if (!isFiniteTimestamp(e)) return;
if (e > t) return e;
}
const c = computeNextRunAtMs({
...e.schedule,
everyMs: i,
anchorMs: o
}, t);
return isFiniteTimestamp(c) ? c : void 0;
}
if (e.schedule.kind === "at") return computeOneShotNextRunAtMs(e, resolveJobLastRunStatus(e));
const r = computeStaggeredCronNextRunAtMs(e, t);
if (r === void 0 && e.schedule.kind === "cron") return computeStaggeredCronNextRunAtMs(e, Math.floor(t / 1e3) * 1e3 + 1e3);
return isFiniteTimestamp(r) ? r : void 0;
}
/** Computes the latest effective cron timestamp at or before the supplied time. */
function computeJobPreviousRunAtOrBeforeMs(e, t) {
if (!isJobEnabled(e) || e.schedule.kind !== "cron") return;
return asDateTimestampMs(computeStaggeredCronPreviousRunAtOrBeforeMs(e, t));
}
/** Maximum consecutive schedule errors before auto-disabling a job. */
const MAX_SCHEDULE_ERRORS = 3;
/** Records a schedule-computation failure and auto-disables after repeated errors. */
function recordScheduleComputeError(e) {
const { state: t, job: n, err: r } = e;
const i = (n.state.scheduleErrorCount ?? 0) + 1;
const a = formatErrorMessageWithCode(r);
n.state.scheduleErrorCount = i;
n.state.nextRunAtMs = void 0;
n.state.lastError = `schedule error: ${a}`;
if (i >= MAX_SCHEDULE_ERRORS) {
autoDisableCronJob({
job: n,
reason: "schedule-errors",
atMs: t.deps.nowMs(),
consecutiveErrors: i,
deferredNotifications: e.deferredNotifications
});
t.deps.log.error({
jobId: n.id,
name: n.name,
errorCount: i,
err: a
}, "cron: auto-disabled job after repeated schedule errors");
} else t.deps.log.warn({
jobId: n.id,
name: n.name,
errorCount: i,
err: a
}, "cron: failed to compute next run for job (skipping)");
return true;
}
function normalizeJobTickState(e) {
const { job: t, ownership: n } = e;
let r = false;
if (!t.state) {
t.state = {};
r = true;
}
if ((t.schedule.kind === "stream" || t.schedule.kind === "event") && !cronSourceIdentity(t)?.trim()) {
setCronSourceIdentity(t, createCronSourceIdentity());
r = true;
}
if (!isJobEnabled(t) && t.state.forcePreservedNextRunAtMs !== t.state.nextRunAtMs || !isTimeScheduledJob(t)) for (const e of TIME_SCHEDULE_STATE_FIELDS) {
if (e === "forcePreservedNextRunAtMs" && resolveForcePreservedOneShotAtMs(t) !== void 0) continue;
if (t.state[e] !== void 0) {
t.state[e] = void 0;
r = true;
}
}
if (!isJobEnabled(t)) {
if (t.state.runningAtMs !== void 0 && !ownsCronRunMarker(n, t.id, t.state.runningAtMs, true) && !n.isJobActive(t.id)) {
Object.assign(t.state, {
runningAtMs: void 0,
runningReceiptId: void 0
});
delete t.state.runningScheduleChangeId;
r = true;
}
return {
changed: r,
skip: true
};
}
if (!hasScheduledNextRunAtMs(t.state.nextRunAtMs) && t.state.nextRunAtMs !== void 0) {
t.state.nextRunAtMs = void 0;
r = true;
}
r = clearInvalidForcePreservedNextRun(t) || r;
return {
changed: r,
skip: false
};
}
function recomputeJobNextRunAtMs(e) {
let t = false;
try {
let n = computeJobNextRunAtMs(e.job, e.nowMs);
if (e.job.schedule.kind !== "at" && resolveJobLastRunStatus(e.job) === "error" && isFiniteTimestamp(e.job.state.lastRunAtMs)) {
const t = resolveJobErrorBackoffUntilMs(e.job, DEFAULT_ERROR_BACKOFF_SCHEDULE_MS);
if (n !== void 0) n = t !== void 0 ? Math.max(n, t) : n;
}
if (e.job.state.nextRunAtMs !== n) {
e.job.state.nextRunAtMs = n;
t = true;
}
if (e.job.state.scheduleErrorCount) {
e.job.state.scheduleErrorCount = void 0;
t = true;
}
} catch (n) {
if (e.skipScheduleErrorHandling) return false;
if (recordScheduleComputeError({
state: e.state,
job: e.job,
err: n,
deferredNotifications: e.deferredNotifications
})) t = true;
}
return t;
}
function isExpiredCronScheduleRepairCandidate(e, t, n) {
const r = e.state.nextRunAtMs;
const i = resolveJobErrorBackoffUntilMs(e, DEFAULT_ERROR_BACKOFF_SCHEDULE_MS);
return hasScheduledNextRunAtMs(r) && t >= r && typeof e.state.queuedAtMs !== "number" && typeof e.state.runningAtMs !== "number" && !n && e.state.startupCatchupAtMs !== r && e.state.forcePreservedNextRunAtMs !== r && (isFiniteTimestamp(e.state.lastRunAtMs) && e.state.lastRunAtMs >= r || i !== void 0 && t < i && r < i);
}
/** Avoid acquiring the writer when the persisted schedules need no repair. */
function needsCronTimerMaintenance(e, t, n) {
if (hasActiveCronRun(e, false)) return false;
if (!isTimeScheduledJob(e)) return TIME_SCHEDULE_STATE_FIELDS.some((t) => e.state[t] !== void 0);
if (!isJobEnabled(e)) return e.state.forcePreservedNextRunAtMs !== e.state.nextRunAtMs && TIME_SCHEDULE_STATE_FIELDS.some((t) => e.state[t] !== void 0);
return n?.recomputeExpired !== false && isExpiredCronScheduleRepairCandidate(e, t, false) || n?.repairFutureCronNextRunAtMs !== false && isStaleFutureCronSlot(e, t) || isJobEnabled(e) && !hasScheduledNextRunAtMs(e.state.nextRunAtMs);
}
function recomputeSingleJobForMaintenance(e, t, n, r) {
const i = n.nowMs ?? e.deps.nowMs();
const a = normalizeJobTickState({
state: e,
job: t,
ownership: r
});
let o = a.changed;
if (a.skip) return o;
const s = n.recomputeExpired ?? false;
const c = n.repairFutureCronNextRunAtMs ?? true;
const recomputeJob = () => recomputeJobNextRunAtMs({
state: e,
job: t,
nowMs: i,
deferredNotifications: n.deferredNotifications,
skipScheduleErrorHandling: n.skipScheduleErrorHandling
});
const l = t.state.startupCatchupAtMs;
const u = t.state.pacedNextRunAtMs;
const d = t.state.nextRunAtMs;
const f = isFiniteTimestamp(t.state.forcePreservedNextRunAtMs) && hasScheduledNextRunAtMs(d) && t.state.forcePreservedNextRunAtMs === d;
const p = isFiniteTimestamp(l) && hasScheduledNextRunAtMs(d) && l === d;
if (l !== void 0 && !p) {
t.state.startupCatchupAtMs = void 0;
o = true;
}
const m = isFiniteTimestamp(u) && hasScheduledNextRunAtMs(d) && u === d;
if (u !== void 0 && !m) {
t.state.pacedNextRunAtMs = void 0;
o = true;
}
if (!hasScheduledNextRunAtMs(t.state.nextRunAtMs)) o = recomputeJob() || o;
else if (c && !p && !m && !f && isStaleFutureCronSlot(t, i)) o = recomputeJob() || o;
else if (s && isExpiredCronScheduleRepairCandidate(t, i, r.isJobActive(t.id))) o = recomputeJob() || o;
return o;
}
/** Returns the next enabled wake timestamp from committed job rows. */
function nextWakeAtMs(e) {
let t;
for (const n of e.store?.jobs ?? []) if (isJobEnabled(n) && hasCanonicalCronDeliveryMode(n.delivery) && isTimeScheduledJob(n) && hasScheduledNextRunAtMs(n.state.nextRunAtMs)) t = Math.min(t ?? Infinity, n.state.nextRunAtMs);
return t;
}
/** Applies one canonical server-authored authority envelope to a tool-bearing job. */
function hasActiveCronRun(e, t) {
return typeof e.state.queuedAtMs === "number" || typeof e.state.runningAtMs === "number" || (t ?? isCronJobActive(e.id));
}
//#endregion
export { maybeAutoDisableCronJobAfterRunFailure as A, hasPendingCronTriggerInterval as C, MIN_REFIRE_GAP_MS as D, MAX_CRON_TIMER_DELAY_MS as E, cronFailureDetailLines as F, captureCronNotificationRouting as M, cronNotificationJob as N, autoDisableCronJob as O, resolveCronNotificationQueueOwner as P, resolveNextRunAtMsOrDisable as S, DEFAULT_STARTUP_DEFERRED_MISSED_AGENT_JOB_DELAY_MS as T, resolveCronRestartRetryAtMs as _, findJobOrThrow as a, resolveJobLastRunEndedAtMs as b, isJobEnabled as c, nextWakeAtMs as d, normalizeConsecutiveErrors as f, recordScheduleComputeError as g, recomputeSingleJobForMaintenance as h, errorBackoffMs as i, queueCronAutoDisableNotification as j, buildCronAutoDisableNotice as k, isTimeScheduledJob as l, recomputeJobNextRunAtMs as m, computeJobNextRunAtMs as n, hasActiveCronRun as o, normalizeStreamScheduleBounds as p, computeJobPreviousRunAtOrBeforeMs as r, hasScheduledNextRunAtMs as s, DEFAULT_ERROR_BACKOFF_SCHEDULE_MS as t, needsCronTimerMaintenance as u, resolveEveryAnchorMs as v, DEFAULT_MISSED_JOB_STAGGER_MS as w, resolveJobLastRunStatus as x, resolveJobErrorBackoffUntilMs as y };