11/** Loads, normalizes, quarantines, and persists cron service store state. */
2+ import { isRecord } from "@openclaw/normalization-core/record-coerce" ;
23import { normalizeCronJobIdentityFields } from "../normalize-job-identity.js" ;
34import { normalizeCronJobInput } from "../normalize.js" ;
45import { getInvalidPersistedCronJobReason } from "../persisted-shape.js" ;
@@ -134,6 +135,12 @@ function warnInvalidPersistedCronJob(params: {
134135 ) ;
135136}
136137
138+ function isValidatedCronJob (
139+ value : Record < string , unknown > ,
140+ ) : value is CronJob & Record < string , unknown > {
141+ return getInvalidPersistedCronJobReason ( value ) === null ;
142+ }
143+
137144/** Loads and normalizes the cron store, quarantining invalid persisted rows before runtime use. */
138145export async function ensureLoaded (
139146 state : CronServiceState ,
@@ -163,7 +170,7 @@ export async function ensureLoaded(
163170 const loadNowMs = state . deps . nowMs ( ) ;
164171 // Persisted cron rows are validated lazily, so treat them as raw records at the
165172 // store boundary and only trust the CronJob shape after validation below.
166- const loadedJobs = ( loaded . store . jobs ?? [ ] ) as unknown as Record < string , unknown > [ ] ;
173+ const loadedJobs = ( loaded . store . jobs ?? [ ] ) . filter ( isRecord ) ;
167174 const jobs : CronJob [ ] = [ ] ;
168175 const durableNextRunAtMsByJobId = new Map < string , number | undefined > ( ) ;
169176 const quarantinedConfigJobs : QuarantinedCronConfigJob [ ] = [ ...loaded . invalidConfigRows ] ;
@@ -190,11 +197,16 @@ export async function ensureLoaded(
190197 }
191198 const hydratedRaw = normalized ?? raw ;
192199 let invalidReason = rawInvalidReason ?? getInvalidPersistedCronJobReason ( hydratedRaw ) ;
193- const hydratedSchedule = ( hydratedRaw . schedule ?? { } ) as Record < string , unknown > ;
194- if ( ! invalidReason && hydratedRaw . enabled !== false && hydratedSchedule . kind === "every" ) {
200+ const hydratedSchedule = isRecord ( hydratedRaw . schedule ) ? hydratedRaw . schedule : { } ;
201+ if (
202+ ! invalidReason &&
203+ isValidatedCronJob ( hydratedRaw ) &&
204+ hydratedRaw . enabled &&
205+ hydratedSchedule . kind === "every"
206+ ) {
195207 try {
196208 assertTimeScheduleSatisfiable (
197- { ...( hydratedRaw as unknown as CronJob ) , state : { } } ,
209+ { ...hydratedRaw , state : { } } ,
198210 loadNowMs ,
199211 computeJobNextRunAtMs ,
200212 ) ;
@@ -226,7 +238,10 @@ export async function ensureLoaded(
226238 continue ;
227239 }
228240 // Validated above, so the raw record is now a trusted CronJob.
229- const hydrated = hydratedRaw as unknown as CronJob ;
241+ if ( ! isValidatedCronJob ( hydratedRaw ) ) {
242+ continue ;
243+ }
244+ const hydrated = hydratedRaw ;
230245 jobs . push ( hydrated ) ;
231246 // Capture the value SQLite actually held before schedule-identity repair
232247 // mutates the runtime view. A later save can then publish that transition.
0 commit comments