|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { ScheduleSchema } from '../system/job.zod'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Two start-node `config` shapes that parse and then do nothing (#31224 item |
| 7 | + * B, ruling B1 option (a)). |
| 8 | + * |
| 9 | + * A flow's start node `config` is an open record (`FlowNodeSchema.config`), so |
| 10 | + * the parse never judged these two keys, and the readers that do read them |
| 11 | + * skip what they cannot use without telling the author: |
| 12 | + * |
| 13 | + * - **`config.filter` beside `config.timeRelative`.** A time-relative sweep |
| 14 | + * reads its row filter from `timeRelative.filter` (`time-relative-trigger.ts` |
| 15 | + * in `@objectstack/trigger-schedule`); nothing reads a `filter` on the start |
| 16 | + * node's own `config`, so a sweep written that way launches the flow for |
| 17 | + * every row in the date window. Refused with the move as the prescription. |
| 18 | + * |
| 19 | + * - **A `config.schedule` the schedule readers cannot use.** Both readers |
| 20 | + * (`ScheduleTrigger` for a schedule flow, `TimeRelativeTrigger` for a sweep's |
| 21 | + * cadence) pass it through `normalizeSchedule` (`schedule-trigger.ts`). A |
| 22 | + * descriptor it reads nothing from leaves a schedule flow unbound (one warn |
| 23 | + * in the server log) and a sweep on its default daily 08:00 UTC cadence, |
| 24 | + * either way not what was written. Three readings, one per form: |
| 25 | + * - a bare cron STRING is not judged here (the reader takes it verbatim); |
| 26 | + * - an object that carries `type` is judged against the published |
| 27 | + * {@link ScheduleSchema} (the discriminant, a cron `timezone` that is a |
| 28 | + * member of `iana_time_zone`, a positive integer `intervalMs`, an ISO |
| 29 | + * `at`), and then, as an untyped one is, for being readable at all; |
| 30 | + * - any other value is refused only when the reader reads nothing from it |
| 31 | + * ({@link flowStartScheduleReadsNothing}). The untyped aliases the |
| 32 | + * reader accepts (`{ cron }`, `{ expression }`, `{ every }`, |
| 33 | + * `{ intervalMs }`, `{ at }`) run today and stay accepted. |
| 34 | + * |
| 35 | + * Top-level `start` nodes only: a start node is never a region member. |
| 36 | + */ |
| 37 | + |
| 38 | +export interface FlowStartConfigRefusal { |
| 39 | + readonly path: ReadonlyArray<string | number>; |
| 40 | + readonly message: string; |
| 41 | +} |
| 42 | + |
| 43 | +function isRecord(v: unknown): v is Record<string, unknown> { |
| 44 | + return !!v && typeof v === 'object' && !Array.isArray(v); |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * `true` when the flow schedule readers read NOTHING from `raw`: the exact |
| 49 | + * complement of what `normalizeSchedule` (`@objectstack/trigger-schedule`) |
| 50 | + * turns into a job schedule, which pins the two together over a table of |
| 51 | + * shapes. `null` / `undefined` read as "not declared" and answer `false`: an |
| 52 | + * absent schedule is not a written one. |
| 53 | + */ |
| 54 | +export function flowStartScheduleReadsNothing(raw: unknown): boolean { |
| 55 | + if (raw == null) return false; |
| 56 | + if (typeof raw === 'string') return raw.trim().length === 0; |
| 57 | + if (typeof raw !== 'object') return true; |
| 58 | + const s = raw as Record<string, unknown>; |
| 59 | + const type = typeof s.type === 'string' ? s.type : undefined; |
| 60 | + // Each branch is `normalizeSchedule`'s, step for step (truthiness included): |
| 61 | + // the first candidate present decides, and a decided value that is not |
| 62 | + // usable reads nothing even when a later alias would have been. |
| 63 | + if (type === 'cron' || (!type && (typeof s.cron === 'string' || typeof s.expression === 'string'))) { |
| 64 | + const expression = |
| 65 | + (typeof s.expression === 'string' && s.expression) || (typeof s.cron === 'string' && s.cron) || undefined; |
| 66 | + return !expression; |
| 67 | + } |
| 68 | + if (type === 'interval' || (!type && (typeof s.intervalMs === 'number' || typeof s.every === 'number'))) { |
| 69 | + const intervalMs = |
| 70 | + (typeof s.intervalMs === 'number' && s.intervalMs) || (typeof s.every === 'number' && s.every) || undefined; |
| 71 | + return !intervalMs || intervalMs <= 0; |
| 72 | + } |
| 73 | + if (type === 'once' || (!type && typeof s.at === 'string')) { |
| 74 | + return !(typeof s.at === 'string' && s.at); |
| 75 | + } |
| 76 | + return true; |
| 77 | +} |
| 78 | + |
| 79 | +function scheduleRefusal(raw: unknown): string | undefined { |
| 80 | + if (raw == null || typeof raw === 'string') { |
| 81 | + return typeof raw === 'string' && flowStartScheduleReadsNothing(raw) |
| 82 | + ? 'The start node\'s `config.schedule` is an empty string, so no schedule is read from it: a schedule ' + |
| 83 | + 'flow stays unbound and a time-relative sweep falls back to daily 08:00 UTC. Write a cron expression ' + |
| 84 | + "(e.g. '0 8 * * *') or remove the key." |
| 85 | + : undefined; |
| 86 | + } |
| 87 | + if (isRecord(raw) && Object.prototype.hasOwnProperty.call(raw, 'type')) { |
| 88 | + const parsed = ScheduleSchema.safeParse(raw); |
| 89 | + if (!parsed.success) { |
| 90 | + const issue = parsed.error.issues[0]; |
| 91 | + const at = issue && issue.path.length > 0 ? ` at \`schedule.${issue.path.join('.')}\`` : ''; |
| 92 | + return ( |
| 93 | + `The start node's \`config.schedule\` is not a schedule${at}: ${issue?.message ?? 'refused'}. The schedule ` + |
| 94 | + 'readers cannot use it, so a schedule flow stays unbound and a time-relative sweep falls back to daily ' + |
| 95 | + "08:00 UTC. Write `{ type: 'cron', expression: '0 8 * * *', timezone: 'UTC' }`, " + |
| 96 | + "`{ type: 'interval', intervalMs: 60000 }` or `{ type: 'once', at: '<ISO datetime>' }`, or a bare cron string." |
| 97 | + ); |
| 98 | + } |
| 99 | + } |
| 100 | + if (!flowStartScheduleReadsNothing(raw)) return undefined; |
| 101 | + return ( |
| 102 | + 'The start node\'s `config.schedule` names no schedule the readers can use (a cron `expression` as a ' + |
| 103 | + 'string, a positive `intervalMs`, or an `at` datetime), so a schedule flow stays unbound and a ' + |
| 104 | + 'time-relative sweep falls back to daily 08:00 UTC. Write a cron string such as \'0 8 * * *\', or ' + |
| 105 | + "`{ type: 'cron', expression: '0 8 * * *' }`, `{ type: 'interval', intervalMs: 60000 }` or " + |
| 106 | + "`{ type: 'once', at: '<ISO datetime>' }`." |
| 107 | + ); |
| 108 | +} |
| 109 | + |
| 110 | +/** The refusals of every top-level start node's `config` in `flow`. */ |
| 111 | +export function flowStartConfigRefusals(flow: unknown): FlowStartConfigRefusal[] { |
| 112 | + if (!isRecord(flow) || !Array.isArray(flow.nodes)) return []; |
| 113 | + const out: FlowStartConfigRefusal[] = []; |
| 114 | + flow.nodes.forEach((node, index) => { |
| 115 | + if (!isRecord(node) || node.type !== 'start' || !isRecord(node.config)) return; |
| 116 | + const config = node.config; |
| 117 | + if (config.timeRelative !== undefined && Object.prototype.hasOwnProperty.call(config, 'filter')) { |
| 118 | + out.push({ |
| 119 | + path: ['nodes', index, 'config', 'filter'], |
| 120 | + message: |
| 121 | + 'The start node declares `config.filter` beside `config.timeRelative`, and nothing reads it: a ' + |
| 122 | + 'time-relative sweep takes its row filter from `timeRelative.filter`, so as written the flow ' + |
| 123 | + 'launches for every record in the date window. Move it into `config.timeRelative.filter`.', |
| 124 | + }); |
| 125 | + } |
| 126 | + if (Object.prototype.hasOwnProperty.call(config, 'schedule')) { |
| 127 | + const message = scheduleRefusal(config.schedule); |
| 128 | + if (message) out.push({ path: ['nodes', index, 'config', 'schedule'], message }); |
| 129 | + } |
| 130 | + }); |
| 131 | + return out; |
| 132 | +} |
0 commit comments