openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
592 lines (591 loc) • 23.8 kB
JavaScript
import { d as normalizeOptionalThreadValue, l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { c as normalizeOptionalAgentId } from "./session-key-CqAyZeVk.mjs";
import { r as normalizeOptionalAccountId } from "./account-id-BqKT3GuY.mjs";
import { n as cronJobUsesToolRuntime, t as applyDefaultCronToolsAllow } from "./tools-allow-CYmLDCc7.mjs";
import { a as hasCanonicalCronDeliveryMode, n as assertCanonicalCronDeliveryMode } from "./delivery-codec-BaSNePUi.mjs";
import { n as shouldDefaultCronDeliveryToAnnounce, t as resolveCronDeliveryMode } from "./delivery-defaults-K6HKsSBE.mjs";
import { n as normalizeRequiredName } from "./normalize-4WPiSYN5.mjs";
import { r as resolveDefaultCronStaggerMs, t as normalizeCronStaggerMs } from "./stagger-Bj0_2RgJ.mjs";
import { i as assertCronJobStateTimestamps } from "./job-config-codec-DgvmEo7U.mjs";
import { t as createCronSourceIdentity } from "./source-schedule-DlDKIQwQ.mjs";
import { s as reconcileToolsAllowAuthority } from "./jobs-tool-policy-zcItpTHY.mjs";
import { n as computeJobNextRunAtMs, p as normalizeStreamScheduleBounds, v as resolveEveryAnchorMs } from "./jobs-scheduling-D3arlYs4.mjs";
import { a as assertFailureDestinationSupport, c as assertSupportedJobSpec, i as assertExternalSourceScheduleSupport, l as assertTimeScheduleSatisfiable, n as assertDeliverySupport, o as assertPacingSupport, r as assertExecutionPolicy, s as assertScriptPayloadSupport, t as assertAnnounceDeliveryChannelSupport, u as assertTriggerSupport } from "./jobs-validation-ZHr0VZjQ.mjs";
import crypto from "node:crypto";
//#region src/cron/service/jobs-declarative.ts
const CRON_DECLARATIVE_LABEL_MAX_LENGTH = 200;
function normalizeDeclarativeLabel(e, t, n = false) {
const r = normalizeOptionalString(e);
if (!(n && e == null) && e !== void 0 && !r) throw new Error(`cron ${t} must not be blank`);
if (r && r.length > CRON_DECLARATIVE_LABEL_MAX_LENGTH) throw new Error(`cron ${t} must be at most ${CRON_DECLARATIVE_LABEL_MAX_LENGTH} characters`);
return r;
}
function declarativeFields(e, t) {
return {
pluginCommand: e.pluginCommand,
schedule: e.schedule,
streamSandbox: e.streamSandbox,
pacing: e.pacing,
activeHours: e.activeHours,
idleOnly: e.idleOnly,
trigger: e.trigger,
payload: e.payload,
scheduledToolPolicy: e.scheduledToolPolicy,
toolsAllowProvenance: e.toolsAllowProvenance,
toolsAllowExecTarget: e.toolsAllowExecTarget,
runtimeAuthority: e.runtimeAuthority,
operatorSource: e.operatorSource,
runtimeAuthorityRecoveryRequired: e.runtimeAuthorityRecoveryRequired,
delivery: e.delivery,
displayName: e.displayName,
...t ? {
enabled: e.enabled,
autoDisabled: e.state.autoDisabled,
streamRestartExhausted: e.state.streamRestartExhausted
} : {}
};
}
//#endregion
//#region src/cron/script-payload.ts
const DEFAULT_CRON_SCRIPT_TIMEOUT_SECONDS = 300;
const MAX_CRON_SCRIPT_TIMEOUT_SECONDS = 900;
const DEFAULT_CRON_SCRIPT_TOOL_BUDGET = 50;
const MAX_CRON_SCRIPT_TOOL_BUDGET = 200;
function clampPositiveInteger(e, t, n) {
if (typeof e !== "number" || !Number.isFinite(e) || e <= 0) return t;
return Math.min(n, Math.max(1, Math.floor(e)));
}
/** Applies the persisted defaults and hard caps for unattended script payloads. */
function normalizeCronScriptPayload(e) {
return {
...e,
script: e.script.trim(),
timeoutSeconds: clampPositiveInteger(e.timeoutSeconds, 300, 900),
toolBudget: clampPositiveInteger(e.toolBudget, 50, 200)
};
}
//#endregion
//#region src/cron/service/delivery-merge.ts
function mergeCronDelivery(e, t, n) {
const r = "completionDestination" in t;
const i = {
mode: e ? e.mode : n,
target: e?.target,
directPolicy: e?.directPolicy,
channel: e?.channel,
to: e?.to,
threadId: e?.threadId,
accountId: e?.accountId,
bestEffort: e?.bestEffort,
completionDestination: e?.completionDestination,
failureDestination: e?.failureDestination
};
if (typeof t.mode === "string") {
const e = i.mode;
i.mode = t.mode;
if (e !== i.mode && (e === "webhook" || i.mode === "webhook")) i.to = void 0;
if (i.mode === "webhook") {
i.target = void 0;
i.directPolicy = void 0;
i.channel = void 0;
i.threadId = void 0;
i.accountId = void 0;
}
if (!r && (i.mode === "none" || i.mode === "webhook")) i.completionDestination = void 0;
}
if ("channel" in t) i.channel = normalizeOptionalString(t.channel);
if ("target" in t) i.target = t.target ?? void 0;
if ("directPolicy" in t) i.directPolicy = t.directPolicy ?? void 0;
if ("to" in t) i.to = normalizeOptionalString(t.to);
if ("threadId" in t) i.threadId = normalizeOptionalThreadValue(t.threadId);
if ("accountId" in t) i.accountId = normalizeOptionalString(t.accountId);
if (typeof t.bestEffort === "boolean") i.bestEffort = t.bestEffort;
if (r) {
if (t.completionDestination == null) i.completionDestination = void 0;
else {
const e = normalizeOptionalString(t.completionDestination.to);
i.completionDestination = {
mode: "webhook",
...e ? { to: e } : {}
};
}
}
if ("failureDestination" in t) {
if (t.failureDestination == null) i.failureDestination = void 0;
else {
const e = i.failureDestination;
const n = t.failureDestination;
const r = {};
if (e) {
for (const t of [
"channel",
"to",
"accountId"
]) if (Object.hasOwn(e, t)) r[t] = e[t];
if (Object.hasOwn(e, "mode")) r.mode = e.mode;
}
if (n) {
for (const e of [
"channel",
"to",
"accountId"
]) if (e in n) r[e] = normalizeOptionalString(n[e]);
if ("mode" in n) {
const e = normalizeOptionalString(n.mode);
r.mode = e === "announce" || e === "webhook" ? e : void 0;
}
}
i.failureDestination = Object.hasOwn(r, "channel") || Object.hasOwn(r, "to") || Object.hasOwn(r, "accountId") || Object.hasOwn(r, "mode") ? r : void 0;
}
}
if (e === void 0 && !("mode" in t) && i.target === void 0 && i.directPolicy === void 0 && i.channel === void 0 && i.to === void 0 && i.threadId === void 0 && i.accountId === void 0 && i.bestEffort === void 0 && i.completionDestination === void 0 && i.failureDestination === void 0) return;
return i;
}
function mergeCronFailureAlert(e, t) {
if (t === false) return false;
if (t === null) return;
if (t === void 0) return e;
const n = { ...e === false || e === void 0 ? {} : e };
if ("after" in t) {
const e = typeof t.after === "number" && Number.isFinite(t.after) ? t.after : 0;
n.after = e > 0 ? Math.floor(e) : void 0;
}
if ("channel" in t) n.channel = normalizeOptionalString(t.channel);
if ("to" in t) n.to = normalizeOptionalString(t.to);
if ("cooldownMs" in t) {
const e = typeof t.cooldownMs === "number" && Number.isFinite(t.cooldownMs) ? t.cooldownMs : -1;
n.cooldownMs = e >= 0 ? Math.floor(e) : void 0;
}
if ("includeSkipped" in t) n.includeSkipped = typeof t.includeSkipped === "boolean" ? t.includeSkipped : void 0;
if ("mode" in t) {
const e = normalizeOptionalString(t.mode);
n.mode = e === "announce" || e === "webhook" ? e : void 0;
}
if ("accountId" in t) n.accountId = normalizeOptionalString(t.accountId);
return n;
}
//#endregion
//#region src/cron/service/initial-delivery.ts
/** Resolves create-time default delivery for new cron jobs. */
/**
* Resolves default cron delivery for new jobs when callers omit explicit delivery config.
* This is the direct-service contract: supported creation paths (gateway `cron.add`,
* agent cron tool) already fill delivery in `normalizeCronJobCreate`, so this default
* only governs callers that reach `CronService.add`/declarative convergence directly.
* The shared predicate keeps this contract consistent across write-time,
* read-time, and service-bypass paths.
*/
function resolveInitialCronDelivery(e) {
if (e.delivery !== void 0) return e.delivery;
if (shouldDefaultCronDeliveryToAnnounce({
payloadKind: e.payload.kind,
sessionTarget: e.sessionTarget
})) return { mode: "announce" };
}
//#endregion
//#region src/cron/service/payload-merge.ts
function applyToolsAllowPatch(e, t, n) {
if (Array.isArray(t.toolsAllow)) {
const r = t.toolsAllow;
e.toolsAllow = r;
const i = n?.toolsAllowIsDefault === true && Array.isArray(n.toolsAllow) && n.toolsAllow.length === r.length && n.toolsAllow.every((e, t) => e === r[t]);
const a = t.toolsAllowIsDefault === true && n?.toolsAllowIsDefault !== true;
if (i || a) e.toolsAllowIsDefault = true;
else delete e.toolsAllowIsDefault;
} else if (t.toolsAllow === null) {
delete e.toolsAllow;
delete e.toolsAllowIsDefault;
}
}
function mergeCronPayload(e, t) {
if (t.kind !== e.kind) {
const n = mergePayloadFields(buildPayloadFromPatch(t), t);
if (t.toolsAllow === void 0 && Array.isArray(e.toolsAllow)) {
n.toolsAllow = [...e.toolsAllow];
if (e.toolsAllowIsDefault === true) n.toolsAllowIsDefault = true;
}
return n;
}
return mergePayloadFields(e, t);
}
function mergePayloadFields(e, t) {
const n = { ...e };
if (t.kind === "systemEvent" && n.kind === "systemEvent") {
if (typeof t.text === "string") n.text = t.text;
} else if (t.kind === "command" && n.kind === "command") {
if (Array.isArray(t.argv)) n.argv = t.argv;
if (typeof t.cwd === "string") n.cwd = t.cwd;
if (t.env && typeof t.env === "object" && !Array.isArray(t.env)) n.env = t.env;
if (typeof t.input === "string") n.input = t.input;
if (typeof t.noOutputTimeoutSeconds === "number") n.noOutputTimeoutSeconds = t.noOutputTimeoutSeconds;
if (typeof t.outputMaxBytes === "number") n.outputMaxBytes = t.outputMaxBytes;
} else if (t.kind === "script" && n.kind === "script") {
if (typeof t.script === "string") n.script = t.script;
if (typeof t.toolBudget === "number") n.toolBudget = t.toolBudget;
} else if (t.kind === "agentTurn" && n.kind === "agentTurn") {
if (typeof t.message === "string") n.message = t.message;
for (const e of ["model", "thinking"]) if (typeof t[e] === "string") n[e] = t[e];
else if (t[e] === null) delete n[e];
if (Array.isArray(t.fallbacks)) n.fallbacks = t.fallbacks;
else if (t.fallbacks === null) delete n.fallbacks;
if (typeof t.lightContext === "boolean") n.lightContext = t.lightContext;
if (typeof t.skipIfScratchEmpty === "boolean") n.skipIfScratchEmpty = t.skipIfScratchEmpty;
if (typeof t.includeReasoning === "boolean") n.includeReasoning = t.includeReasoning;
if (typeof t.allowUnsafeExternalContent === "boolean") n.allowUnsafeExternalContent = t.allowUnsafeExternalContent;
}
applyToolsAllowPatch(n, t, e);
if ("timeoutSeconds" in t && n.kind !== "systemEvent") {
if (typeof t.timeoutSeconds === "number") n.timeoutSeconds = t.timeoutSeconds;
else if (t.timeoutSeconds === null) delete n.timeoutSeconds;
}
return n;
}
function buildPayloadFromPatch(e) {
if (e.kind === "systemEvent") {
if (typeof e.text !== "string" || e.text.length === 0) throw new Error("cron.update payload.kind=\"systemEvent\" requires text");
return {
kind: "systemEvent",
text: e.text
};
}
if (e.kind === "command") {
if (!Array.isArray(e.argv) || e.argv.length === 0) throw new Error("cron.update payload.kind=\"command\" requires argv");
return {
kind: "command",
argv: e.argv
};
}
if (e.kind === "script") {
if (typeof e.script !== "string" || e.script.trim().length === 0) throw new Error("cron.update payload.kind=\"script\" requires script");
return {
kind: "script",
script: e.script
};
}
if (typeof e.message !== "string" || e.message.length === 0) throw new Error("cron.update payload.kind=\"agentTurn\" requires message");
return {
kind: "agentTurn",
message: e.message
};
}
//#endregion
//#region src/cron/service/jobs.ts
/** Cron job scheduling, validation, creation, and patch helpers. */
function resetJobFailureState(e) {
delete e.state.autoDisabled;
e.state.consecutiveErrors = 0;
e.state.scheduleErrorCount = 0;
if (e.schedule.kind === "stream") {
e.state.streamRestartExhausted = void 0;
e.state.streamConsecutiveFailures = 0;
e.state.streamError = void 0;
}
}
function normalizeJobSchedule(e, t) {
if (e.kind === "every") {
if (t.kind === "patch") return e;
if (t.kind === "create") return {
...e,
anchorMs: resolveEveryAnchorMs({
schedule: e,
fallbackAnchorMs: t.nowMs
})
};
if (e.anchorMs !== void 0) return e;
const n = t.previous.kind === "every" && t.previous.everyMs === e.everyMs ? resolveEveryAnchorMs({
schedule: t.previous,
fallbackAnchorMs: t.fallbackAnchorMs
}) : t.nowMs;
return {
...e,
anchorMs: n
};
}
if (e.kind === "cron") {
const n = normalizeCronStaggerMs(e.staggerMs);
if (n !== void 0) return {
...e,
staggerMs: n
};
if (t.kind !== "create" && t.previous.kind === "cron" && t.previous.expr === e.expr) return {
...e,
staggerMs: t.previous.staggerMs
};
const r = resolveDefaultCronStaggerMs(e.expr);
if (r !== void 0) return {
...e,
staggerMs: r
};
return t.kind === "declarative" ? { ...e } : e;
}
const n = t.kind === "declarative" ? structuredClone(e) : e;
return normalizeStreamScheduleBounds(n);
}
function validateFullJob(e, t, n) {
const r = t.cronConfig;
const i = t.kind === "create" ? e.trigger !== void 0 : t.kind === "patch" ? t.patch.trigger != null : t.input.trigger !== void 0;
const a = t.kind === "create" ? e.payload.kind === "script" : t.kind === "patch" ? t.patch.payload?.kind === "script" : t.input.payload.kind === "script";
const o = t.kind !== "patch" || t.patch.enabled === true || t.patch.schedule?.kind === "stream" || t.patch.schedule?.kind === "event";
const validateCapabilities = () => {
assertTriggerSupport(e, {
cronConfig: r,
validateAuthoredTrigger: i
});
assertScriptPayloadSupport(e, {
cronConfig: r,
requireEnabled: a,
...t.kind === "patch" ? { validateSyntax: t.patch.payload !== void 0 } : {}
});
assertExternalSourceScheduleSupport(e, {
cronConfig: r,
requireEnabled: o
});
};
if (t.kind === "declarative") validateCapabilities();
assertSupportedJobSpec(e);
assertPacingSupport(e);
assertExecutionPolicy(e);
if (t.kind !== "declarative") validateCapabilities();
assertDeliverySupport(e);
assertAnnounceDeliveryChannelSupport(e, n, t.kind === "patch" ? t.patch : void 0);
assertFailureDestinationSupport(e);
const s = t.kind !== "patch" || t.patch.schedule !== void 0 || t.patch.enabled === true;
if (t.nowMs !== void 0 && s) assertTimeScheduleSatisfiable(e, t.nowMs);
}
/** Creates a normalized cron job row from public add input and computes its initial schedule. */
function createJob(e, t, n) {
const r = e.deps.nowMs();
const i = normalizeOptionalString(t.id) ?? crypto.randomUUID();
const a = normalizeJobSchedule(t.schedule, {
kind: "create",
nowMs: r
});
const o = typeof t.deleteAfterRun === "boolean" ? t.deleteAfterRun : a.kind === "at" ? true : void 0;
const s = typeof t.enabled === "boolean" ? t.enabled : true;
const c = normalizeDeclarativeLabel(t.declarationKey, "declarationKey");
const l = normalizeDeclarativeLabel(t.displayName, "displayName");
const u = normalizeOptionalAgentId(t.owner?.agentId);
const d = normalizeOptionalString(t.owner?.sessionKey);
const f = normalizeOptionalAccountId(t.owner?.accountId);
const p = { ...t.state };
delete p.scheduleActivatedAtMs;
delete p.runningScheduleChangeId;
delete p.autoDisabled;
delete p.lastMeaningfulResult;
delete p.sourceIdentity;
delete p.streamSourceIdentity;
assertCronJobStateTimestamps(p);
const m = {
id: i,
...c ? { declarationKey: c } : {},
...l ? { displayName: l } : {},
...u || d || f ? { owner: {
...u ? { agentId: u } : {},
...d ? { sessionKey: d } : {},
...f ? { accountId: f } : {}
} } : {},
agentId: normalizeOptionalAgentId(t.agentId),
sessionKey: normalizeOptionalString(t.sessionKey),
name: normalizeRequiredName(t.name),
description: normalizeOptionalString(t.description),
enabled: s,
deleteAfterRun: o,
createdAtMs: n?.createdAtMs ?? r,
updatedAtMs: r,
schedule: a,
...t.pacing !== void 0 ? { pacing: structuredClone(t.pacing) } : {},
...t.activeHours !== void 0 ? { activeHours: structuredClone(t.activeHours) } : {},
...t.idleOnly !== void 0 ? { idleOnly: t.idleOnly } : {},
sessionTarget: t.sessionTarget,
wakeMode: t.wakeMode,
payload: t.payload.kind === "script" ? normalizeCronScriptPayload(structuredClone(t.payload)) : structuredClone(t.payload),
delivery: resolveInitialCronDelivery(t),
failureAlert: t.failureAlert,
...t.trigger ? { trigger: structuredClone(t.trigger) } : {},
state: {
...p,
...a.kind === "stream" ? { streamSourceIdentity: createCronSourceIdentity() } : a.kind === "event" ? { sourceIdentity: createCronSourceIdentity() } : {}
}
};
applyDefaultCronToolsAllow(m);
reconcileToolsAllowAuthority({
job: m,
previouslyUsedToolRuntime: false,
explicitlyMutatesToolsAllow: true,
scheduledToolPolicy: n?.scheduledToolPolicy,
toolsAllowProvenance: n?.toolsAllowProvenance,
toolsAllowExecTarget: n?.toolsAllowExecTarget
});
validateFullJob(m, {
kind: "create",
cronConfig: e.deps.cronConfig,
defaultAgentId: e.deps.defaultAgentId,
nowMs: r
}, n?.configuredChannels);
m.state.nextRunAtMs = computeJobNextRunAtMs(m, r);
return m;
}
/** Applies a public cron patch in-place, preserving omitted nested fields and validating the result. */
function applyJobPatch(e, t, n) {
if (!hasCanonicalCronDeliveryMode(e.delivery)) assertCanonicalCronDeliveryMode(t.delivery ?? e.delivery);
const r = cronJobUsesToolRuntime(e);
const i = t.payload?.toolsAllow === null;
const a = e.schedule.kind;
if ("name" in t) e.name = normalizeRequiredName(t.name);
if ("description" in t) e.description = normalizeOptionalString(t.description);
if ("displayName" in t) {
const n = normalizeDeclarativeLabel(t.displayName, "displayName", true);
if (n) e.displayName = n;
else delete e.displayName;
}
if (typeof t.enabled === "boolean") e.enabled = t.enabled;
if (typeof t.deleteAfterRun === "boolean") e.deleteAfterRun = t.deleteAfterRun;
else if (t.schedule?.kind === "at" && (a === "every" || a === "cron")) e.deleteAfterRun = true;
else if ((a === "at" || a === "on-exit") && (t.schedule?.kind === "every" || t.schedule?.kind === "cron")) delete e.deleteAfterRun;
if (t.schedule) e.schedule = normalizeJobSchedule(t.schedule, {
kind: "patch",
previous: e.schedule
});
if ("trigger" in t) {
if (t.trigger === null || t.trigger === void 0) delete e.trigger;
else e.trigger = structuredClone(t.trigger);
}
if ("pacing" in t) {
if (t.pacing === null || t.pacing === void 0) delete e.pacing;
else e.pacing = structuredClone(t.pacing);
}
if (t.sessionTarget) e.sessionTarget = t.sessionTarget;
if ("activeHours" in t) {
if (t.activeHours == null) delete e.activeHours;
else e.activeHours = structuredClone(t.activeHours);
}
if ("idleOnly" in t) {
if (t.idleOnly == null) delete e.idleOnly;
else e.idleOnly = t.idleOnly;
}
if (t.wakeMode) e.wakeMode = t.wakeMode;
if (t.payload) {
e.payload = mergeCronPayload(e.payload, t.payload);
if (e.payload.kind === "script") e.payload = normalizeCronScriptPayload(e.payload);
}
if (cronJobUsesToolRuntime(e) && (!r || i)) applyDefaultCronToolsAllow(e);
reconcileToolsAllowAuthority({
job: e,
previouslyUsedToolRuntime: r,
explicitlyMutatesToolsAllow: t.payload !== void 0 && Object.hasOwn(t.payload, "toolsAllow"),
scheduledToolPolicy: n?.scheduledToolPolicy,
toolsAllowProvenance: n?.toolsAllowProvenance,
toolsAllowExecTarget: n?.toolsAllowExecTarget
});
if (t.delivery) {
const n = t.delivery.mode ?? resolveCronDeliveryMode(e);
e.delivery = mergeCronDelivery(e.delivery, t.delivery, n);
}
if ("failureAlert" in t) e.failureAlert = mergeCronFailureAlert(e.failureAlert, t.failureAlert);
if (e.sessionTarget === "main" && e.payload.kind !== "agentTurn" && e.delivery?.mode !== "webhook") {
assertFailureDestinationSupport(e);
const n = t.delivery && mergeCronDelivery(void 0, t.delivery, "announce");
if (n && (t.delivery?.mode !== void 0 || n.target !== void 0 || n.directPolicy !== void 0 || n.channel !== void 0 || n.to !== void 0 || n.threadId !== void 0 || n.accountId !== void 0 || n.completionDestination !== void 0)) assertDeliverySupport({
sessionTarget: e.sessionTarget,
payload: e.payload,
delivery: n
});
const r = e.delivery?.failureDestination;
e.delivery = r ? {
mode: "none",
failureDestination: r
} : void 0;
}
if (t.state) {
const n = { ...t.state };
delete n.scheduleActivatedAtMs;
delete n.runningScheduleChangeId;
delete n.autoDisabled;
delete n.lastMeaningfulResult;
delete n.sourceIdentity;
delete n.streamSourceIdentity;
assertCronJobStateTimestamps(n);
e.state = {
...e.state,
...n
};
}
if (t.enabled === true) resetJobFailureState(e);
if ("agentId" in t) e.agentId = normalizeOptionalAgentId(t.agentId);
if ("sessionKey" in t) e.sessionKey = normalizeOptionalString(t.sessionKey);
if (a === "stream" && e.schedule.kind !== "stream") {
e.state.streamStatus = void 0;
e.state.streamError = void 0;
e.state.streamConsecutiveFailures = void 0;
e.state.streamRestartExhausted = void 0;
e.state.streamSourceIdentity = void 0;
e.state.streamDroppedBatches = void 0;
e.state.streamCoalescedBatches = void 0;
e.state.streamLastStartedAtMs = void 0;
e.state.streamLastExitAtMs = void 0;
}
validateFullJob(e, {
kind: "patch",
patch: t,
defaultAgentId: n?.defaultAgentId,
nowMs: n?.scheduleValidationNowMs,
cronConfig: n?.cronConfig
}, n?.configuredChannels);
}
/** Converges the declared schedule, payload, delivery, and display label only. */
function applyDeclarativeJobSpec(e, t, n) {
if (!hasCanonicalCronDeliveryMode(e.delivery)) assertCanonicalCronDeliveryMode(t.delivery ?? e.delivery);
const r = cronJobUsesToolRuntime(e);
const i = t.payload.toolsAllow !== void 0;
const a = e.payload.toolsAllow;
const o = e.payload.toolsAllowIsDefault;
const s = normalizeDeclarativeLabel(t.displayName, "displayName");
if (s) e.displayName = s;
else delete e.displayName;
e.schedule = normalizeJobSchedule(t.schedule, {
kind: "declarative",
previous: e.schedule,
nowMs: n.nowMs,
fallbackAnchorMs: e.createdAtMs
});
if (t.pacing !== void 0) e.pacing = structuredClone(t.pacing);
else delete e.pacing;
if (t.activeHours !== void 0) e.activeHours = structuredClone(t.activeHours);
else delete e.activeHours;
if (t.idleOnly !== void 0) e.idleOnly = t.idleOnly;
else delete e.idleOnly;
e.payload = t.payload.kind === "script" ? normalizeCronScriptPayload(structuredClone(t.payload)) : structuredClone(t.payload);
if (t.trigger) e.trigger = structuredClone(t.trigger);
else delete e.trigger;
if (cronJobUsesToolRuntime(e) && e.payload.toolsAllow === void 0) {
if (a !== void 0) {
e.payload.toolsAllow = [...a];
if (o === true) e.payload.toolsAllowIsDefault = true;
} else if (!r) applyDefaultCronToolsAllow(e);
}
reconcileToolsAllowAuthority({
job: e,
previouslyUsedToolRuntime: r,
explicitlyMutatesToolsAllow: i,
scheduledToolPolicy: n.scheduledToolPolicy,
toolsAllowProvenance: n.toolsAllowProvenance,
toolsAllowExecTarget: n.toolsAllowExecTarget
});
const c = resolveInitialCronDelivery(t);
if (c) e.delivery = structuredClone(c);
else delete e.delivery;
if (n.enabledExplicit) {
if (t.enabled && (!e.enabled || e.state.autoDisabled || e.state.streamRestartExhausted)) resetJobFailureState(e);
e.enabled = t.enabled;
}
assertCronJobStateTimestamps(t.state ?? {});
validateFullJob(e, {
kind: "declarative",
input: t,
defaultAgentId: n.defaultAgentId,
nowMs: n.nowMs,
cronConfig: n.cronConfig
}, n.configuredChannels);
}
//#endregion
export { declarativeFields as i, applyJobPatch as n, createJob as r, applyDeclarativeJobSpec as t };