11/** Normalizes cron create/patch payloads before validation and persistence. */
22import { parseBoolean } from "@openclaw/normalization-core/boolean-coercion" ;
33import { timestampMsToIsoString } from "@openclaw/normalization-core/number-coercion" ;
4+ import { isRecord } from "@openclaw/normalization-core/record-coerce" ;
45import {
56 normalizeLowercaseStringOrEmpty ,
67 normalizeOptionalLowercaseString ,
78 normalizeOptionalString ,
89} from "@openclaw/normalization-core/string-coerce" ;
910import { normalizeOptionalAccountId } from "../routing/account-id.js" ;
1011import { sanitizeAgentId } from "../routing/session-key.js" ;
11- import { isRecord } from "../utils.js" ;
1212import { shouldDefaultCronDeliveryToAnnounce } from "./delivery-defaults.js" ;
1313import { parseDeliveryInput } from "./delivery-field-schemas.js" ;
1414import { normalizeCronCommandArgv , normalizeCronPayload } from "./normalize-payload.js" ;
15+ import { snapshotOwnCronRecord } from "./own-record.js" ;
1516import { parseAbsoluteTimeMs } from "./parse.js" ;
1617import { normalizeCronRuntimeAuthority } from "./runtime-authority.js" ;
1718import { coerceFiniteScheduleNumber } from "./schedule-number.js" ;
@@ -43,8 +44,8 @@ const DEFAULT_OPTIONS: NormalizeOptions = {
4344} ;
4445
4546function coerceSchedule ( schedule : UnknownRecord ) {
46- const next : UnknownRecord = { ... schedule } ;
47- const rawKind = normalizeLowercaseStringOrEmpty ( schedule . kind ) ;
47+ const next = snapshotOwnCronRecord ( schedule ) ;
48+ const rawKind = normalizeLowercaseStringOrEmpty ( next . kind ) ;
4849 const kind =
4950 rawKind === "at" ||
5051 rawKind === "every" ||
@@ -53,16 +54,16 @@ function coerceSchedule(schedule: UnknownRecord) {
5354 rawKind === "stream"
5455 ? rawKind
5556 : undefined ;
56- const exprRaw = normalizeOptionalString ( schedule . expr ) ?? "" ;
57- const timezone = normalizeOptionalString ( schedule . tz ) ;
58- const commandRaw = normalizeOptionalString ( schedule . command ) ?? "" ;
59- const streamCommand = normalizeCronCommandArgv ( schedule . command ) ;
60- const cwdRaw = normalizeOptionalString ( schedule . cwd ) ?? "" ;
61- const streamMode = normalizeOptionalLowercaseString ( schedule . mode ) ;
62- const streamMatch = typeof schedule . match === "string" ? schedule . match : undefined ;
63- const everyMs = coerceFiniteScheduleNumber ( schedule . everyMs ) ;
64- const anchorMs = coerceFiniteScheduleNumber ( schedule . anchorMs ) ;
65- const atString = normalizeOptionalString ( schedule . at ) ?? "" ;
57+ const exprRaw = normalizeOptionalString ( next . expr ) ?? "" ;
58+ const timezone = normalizeOptionalString ( next . tz ) ;
59+ const commandRaw = normalizeOptionalString ( next . command ) ?? "" ;
60+ const streamCommand = normalizeCronCommandArgv ( next . command ) ;
61+ const cwdRaw = normalizeOptionalString ( next . cwd ) ?? "" ;
62+ const streamMode = normalizeOptionalLowercaseString ( next . mode ) ;
63+ const streamMatch = typeof next . match === "string" ? next . match : undefined ;
64+ const everyMs = coerceFiniteScheduleNumber ( next . everyMs ) ;
65+ const anchorMs = coerceFiniteScheduleNumber ( next . anchorMs ) ;
66+ const atString = normalizeOptionalString ( next . at ) ?? "" ;
6667 const parsedAtMs = atString ? parseAbsoluteTimeMs ( atString ) : null ;
6768
6869 if ( kind ) {
@@ -118,7 +119,7 @@ function coerceSchedule(schedule: UnknownRecord) {
118119 }
119120 normalizeCronStreamBatching ( next ) ;
120121 }
121- const staggerMs = normalizeCronStaggerMs ( schedule . staggerMs ) ;
122+ const staggerMs = normalizeCronStaggerMs ( next . staggerMs ) ;
122123 if ( staggerMs !== undefined ) {
123124 next . staggerMs = staggerMs ;
124125 } else if ( "staggerMs" in next ) {
@@ -175,48 +176,49 @@ function coerceSchedule(schedule: UnknownRecord) {
175176 delete next . maxBatchBytes ;
176177 }
177178
178- return next ;
179+ return { ... next } ;
179180}
180181
181182function coerceTrigger ( trigger : UnknownRecord ) : UnknownRecord {
182- const script = typeof trigger . script === "string" ? trigger . script . trim ( ) : "" ;
183- const once = parseBoolean ( trigger . once ) ;
183+ const input = snapshotOwnCronRecord ( trigger ) ;
184+ const script = typeof input . script === "string" ? input . script . trim ( ) : "" ;
185+ const once = parseBoolean ( input . once ) ;
184186 return {
185187 script,
186188 ...( once !== undefined ? { once } : { } ) ,
187189 } ;
188190}
189191
190192function coerceDelivery ( delivery : UnknownRecord ) {
191- const next : UnknownRecord = { ... delivery } ;
192- const parsed = parseDeliveryInput ( delivery ) ;
193+ const next = snapshotOwnCronRecord ( delivery ) ;
194+ const parsed = parseDeliveryInput ( next ) ;
193195 if ( parsed . mode !== undefined ) {
194196 next . mode = parsed . mode ;
195197 } else if ( "mode" in next ) {
196198 delete next . mode ;
197199 }
198- if ( "channel" in delivery && delivery . channel === null ) {
200+ if ( "channel" in next && next . channel === null ) {
199201 next . channel = null ;
200202 } else if ( parsed . channel !== undefined ) {
201203 next . channel = parsed . channel ;
202204 } else if ( "channel" in next ) {
203205 delete next . channel ;
204206 }
205- if ( "to" in delivery && delivery . to === null ) {
207+ if ( "to" in next && next . to === null ) {
206208 next . to = null ;
207209 } else if ( parsed . to !== undefined ) {
208210 next . to = parsed . to ;
209211 } else if ( "to" in next ) {
210212 delete next . to ;
211213 }
212- if ( "threadId" in delivery && delivery . threadId === null ) {
214+ if ( "threadId" in next && next . threadId === null ) {
213215 next . threadId = null ;
214216 } else if ( parsed . threadId !== undefined ) {
215217 next . threadId = parsed . threadId ;
216218 } else if ( "threadId" in next ) {
217219 delete next . threadId ;
218220 }
219- if ( "accountId" in delivery && delivery . accountId === null ) {
221+ if ( "accountId" in next && next . accountId === null ) {
220222 next . accountId = null ;
221223 } else if ( parsed . accountId !== undefined ) {
222224 next . accountId = parsed . accountId ;
@@ -249,12 +251,13 @@ function coerceDelivery(delivery: UnknownRecord) {
249251 }
250252 }
251253 }
252- return next ;
254+ return { ... next } ;
253255}
254256
255257function coerceCompletionDestination ( value : UnknownRecord ) {
256- const mode = normalizeOptionalLowercaseString ( value . mode ) ;
257- const to = normalizeOptionalString ( value . to ) ;
258+ const input = snapshotOwnCronRecord ( value ) ;
259+ const mode = normalizeOptionalLowercaseString ( input . mode ) ;
260+ const to = normalizeOptionalString ( input . to ) ;
258261 if ( mode !== "webhook" ) {
259262 return null ;
260263 }
@@ -265,7 +268,7 @@ function coerceCompletionDestination(value: UnknownRecord) {
265268}
266269
267270function coerceFailureDestination ( value : UnknownRecord ) {
268- const next : UnknownRecord = { ... value } ;
271+ const next = snapshotOwnCronRecord ( value ) ;
269272 if ( "channel" in next ) {
270273 if ( next . channel === null ) {
271274 next . channel = null ;
@@ -322,7 +325,7 @@ function coerceFailureDestination(value: UnknownRecord) {
322325 }
323326 }
324327 }
325- return next ;
328+ return { ... next } ;
326329}
327330
328331function normalizeSessionTarget ( raw : unknown ) {
@@ -361,8 +364,8 @@ export function normalizeCronJobInput(
361364 if ( ! isRecord ( raw ) ) {
362365 return null ;
363366 }
364- const base = raw ;
365- const next : UnknownRecord = { ... base } ;
367+ const base = snapshotOwnCronRecord ( raw ) ;
368+ const next = snapshotOwnCronRecord ( base ) ;
366369
367370 for ( const field of [ "declarationKey" , "displayName" ] as const ) {
368371 if ( field in base && typeof base [ field ] === "string" ) {
@@ -376,10 +379,11 @@ export function normalizeCronJobInput(
376379 }
377380
378381 if ( isRecord ( base . owner ) ) {
379- const agentId = normalizeOptionalString ( base . owner . agentId ) ;
380- const sessionKey = normalizeOptionalString ( base . owner . sessionKey ) ;
382+ const owner = snapshotOwnCronRecord ( base . owner ) ;
383+ const agentId = normalizeOptionalString ( owner . agentId ) ;
384+ const sessionKey = normalizeOptionalString ( owner . sessionKey ) ;
381385 const accountId = normalizeOptionalAccountId (
382- typeof base . owner . accountId === "string" ? base . owner . accountId : undefined ,
386+ typeof owner . accountId === "string" ? owner . accountId : undefined ,
383387 ) ;
384388 if ( agentId || sessionKey || accountId ) {
385389 next . owner = {
@@ -402,7 +406,9 @@ export function normalizeCronJobInput(
402406 }
403407
404408 if ( "toolsAllowProvenance" in base ) {
405- const provenance = base . toolsAllowProvenance ;
409+ const provenance = isRecord ( base . toolsAllowProvenance )
410+ ? snapshotOwnCronRecord ( base . toolsAllowProvenance )
411+ : undefined ;
406412 if (
407413 isRecord ( provenance ) &&
408414 provenance . version === 1 &&
@@ -623,7 +629,7 @@ export function normalizeCronJobInput(
623629 }
624630 }
625631
626- return next ;
632+ return { ... next } ;
627633}
628634
629635/** Normalizes a raw cron create request and applies create-time defaults. */
0 commit comments