-
Notifications
You must be signed in to change notification settings - Fork 14
feat(automation): close PR1-5 backend gaps before frontend slice #1045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6b26cc0
feat(automation): close PR1-5 backend gaps before frontend slice
Astro-Han fcba589
fix(automation): preserve nextFireAt on metadata edits + allow cleari…
Astro-Han ac04734
refactor(automation): share cron utility + freeze derived on stopped …
Astro-Han 2558473
fix(ci): register test/automation under windows server-tools shard
Astro-Han 6123bef
test(automation): advance scheduler-owned stopped nextFireAt + route …
Astro-Han c47e70d
fix(automation): break scheduler self-loop on stopped refresh + typec…
Astro-Han dd54126
refactor(automation): tighten input contract and converge cron valida…
Astro-Han 4902a18
fix(automation): retry recordRunOutcome on revision conflict
Astro-Han c6c7b5b
fix(automation): keep unsupported_stop_condition detail + cover real …
Astro-Han 14fa998
fix(tool): keep condition in automate Stop schema to match Automation…
Astro-Han 3af5bae
test(automation): harden 422 model test + replace fixed sleep with po…
Astro-Han 8751e5f
refactor(automation): move recordRunOutcome test hook out of public o…
Astro-Han 5e177ba
refactor(automation): isolate test seam to internal module, off Autom…
Astro-Han File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/opencode/migration/20260601100000_automation_model_required/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| -- Automation definitions and runs persisted before PR5.5 lack the now-required | ||
| -- `model` field on AutomationDefinition. Since automations are still gated | ||
| -- behind the env-flagged `automate` tool and no UI entry exists yet, any rows | ||
| -- in these tables are pre-release dev/QA data. Drop them so the new schema | ||
| -- parses cleanly without per-row backfill. | ||
| DELETE FROM `automation_run`;--> statement-breakpoint | ||
| DELETE FROM `automation_definition`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import type { Automation } from "./index" | ||
|
|
||
| /** | ||
| * @internal Test-only injection points for the automation module. | ||
| * | ||
| * Production code MUST NOT import or write to this module — the only reader | ||
| * is `recordRunOutcome` in `./index`, which checks `beforeReplaceDefinition` | ||
| * to support a deterministic ConflictError retry test. By living outside the | ||
| * `Automation` namespace, this seam is invisible to anyone consuming the | ||
| * public `Automation.*` API surface. | ||
| * | ||
| * Tests assign hooks here and MUST clear them in a `finally` block so a | ||
| * failing test cannot leak state to a sibling test. | ||
| */ | ||
| export const internalTestHooks: { | ||
| beforeReplaceDefinition?: (previous: Automation.Definition) => void | ||
| } = {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { DateTime } from "luxon" | ||
|
|
||
| export type CronSchedule = { | ||
| minutes: Set<number> | ||
| hours: Set<number> | ||
| days: Set<number> | ||
| months: Set<number> | ||
| weekdays: Set<number> | ||
| dayRestricted: boolean | ||
| weekdayRestricted: boolean | ||
| } | ||
|
|
||
| function cronValues(field: string, min: number, max: number, options?: { sundayAlias?: boolean }) { | ||
| const values = new Set<number>() | ||
| for (const item of field.split(",")) { | ||
| const [base, stepRaw] = item.split("/") | ||
| if (!base || item.split("/").length > 2) throw new Error(`Invalid cron field: ${item}`) | ||
| const step = stepRaw === undefined ? 1 : Number(stepRaw) | ||
| if (!Number.isInteger(step) || step <= 0) throw new Error(`Invalid cron step: ${item}`) | ||
| const range = base === "*" ? [min, max] : base.split("-").map(Number) | ||
| if (range.length === 0 || range.length > 2 || range.some((value) => !Number.isInteger(value))) { | ||
| throw new Error(`Invalid cron field: ${item}`) | ||
| } | ||
| const start = range[0] | ||
| const end = base === "*" || (range.length === 1 && stepRaw !== undefined) ? max : range.length === 1 ? range[0] : range[1] | ||
| if (start < min || end > max || start > end) throw new Error(`Invalid cron range: ${item}`) | ||
| for (let value = start; value <= end; value += step) { | ||
| values.add(options?.sundayAlias && value === 7 ? 0 : value) | ||
| } | ||
| } | ||
| return values | ||
| } | ||
|
|
||
| export function parseCronSchedule(expression: string): CronSchedule { | ||
| const fields = expression.trim().split(/\s+/) | ||
| if (fields.length !== 5) throw new Error(`Invalid cron expression: ${expression}`) | ||
| const [minuteField, hourField, dayField, monthField, weekdayField] = fields | ||
| return { | ||
| minutes: cronValues(minuteField, 0, 59), | ||
| hours: cronValues(hourField, 0, 23), | ||
| days: cronValues(dayField, 1, 31), | ||
| months: cronValues(monthField, 1, 12), | ||
| weekdays: cronValues(weekdayField, 0, 7, { sundayAlias: true }), | ||
| dayRestricted: dayField !== "*", | ||
| weekdayRestricted: weekdayField !== "*", | ||
| } | ||
| } | ||
|
|
||
| const monthMaxDays = new Map([ | ||
| [1, 31], | ||
| [2, 29], | ||
| [3, 31], | ||
| [4, 30], | ||
| [5, 31], | ||
| [6, 30], | ||
| [7, 31], | ||
| [8, 31], | ||
| [9, 30], | ||
| [10, 31], | ||
| [11, 30], | ||
| [12, 31], | ||
| ]) | ||
|
|
||
| function hasReachableDayMonth(schedule: CronSchedule) { | ||
| for (const month of schedule.months) { | ||
| const maxDay = monthMaxDays.get(month) | ||
| if (maxDay === undefined) continue | ||
| for (const day of schedule.days) { | ||
| if (day <= maxDay) return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| export function isValidCronExpression(expression: string): boolean { | ||
| try { | ||
| const schedule = parseCronSchedule(expression) | ||
| if (!schedule.weekdayRestricted && !hasReachableDayMonth(schedule)) return false | ||
| return true | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| export function cronMatches(schedule: CronSchedule, time: DateTime) { | ||
| const weekday = time.weekday === 7 ? 0 : time.weekday | ||
| const dayMatches = schedule.days.has(time.day) | ||
| const weekdayMatches = schedule.weekdays.has(weekday) | ||
| const calendarMatches = | ||
| schedule.dayRestricted && schedule.weekdayRestricted ? dayMatches || weekdayMatches : dayMatches && weekdayMatches | ||
| return ( | ||
| schedule.minutes.has(time.minute) && | ||
| schedule.hours.has(time.hour) && | ||
| schedule.months.has(time.month) && | ||
| calendarMatches | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { DateTime } from "luxon" | ||
| import type { Automation } from "." | ||
| import { type CronSchedule, parseCronSchedule } from "./cron" | ||
|
|
||
| const CRON_LOOKAHEAD_MINUTES = 527_040 * 5 | ||
| const NEXT_FIRES_PREVIEW = 5 | ||
|
|
||
| type RecurringDefinition = Extract<Automation.Definition, { kind: "recurring" }> | ||
|
|
||
| const PLUS_ONE_MONTH = { months: 1 } | ||
| const PLUS_ONE_DAY = { days: 1 } | ||
| const PLUS_ONE_HOUR = { hours: 1 } | ||
| const PLUS_ONE_MINUTE = { minutes: 1 } | ||
|
|
||
| function nextCronFires(definition: RecurringDefinition, from: number, count: number): number[] { | ||
| if (definition.rhythm.kind !== "cron" || count <= 0) return [] | ||
| let schedule: CronSchedule | ||
| try { | ||
| schedule = parseCronSchedule(definition.rhythm.expression) | ||
| } catch { | ||
| return [] | ||
| } | ||
| const fires: number[] = [] | ||
| const maxTimestamp = from + CRON_LOOKAHEAD_MINUTES * 60 * 1000 | ||
| let cursor = DateTime.fromMillis(from, { zone: definition.timezone }).plus(PLUS_ONE_MINUTE).startOf("minute") | ||
| while (cursor.toMillis() < maxTimestamp && fires.length < count) { | ||
| if (!schedule.months.has(cursor.month)) { | ||
| cursor = cursor.plus(PLUS_ONE_MONTH).startOf("month") | ||
| continue | ||
| } | ||
| const weekday = cursor.weekday === 7 ? 0 : cursor.weekday | ||
| const dayMatches = schedule.days.has(cursor.day) | ||
| const weekdayMatches = schedule.weekdays.has(weekday) | ||
| const calendarMatches = | ||
| schedule.dayRestricted && schedule.weekdayRestricted ? dayMatches || weekdayMatches : dayMatches && weekdayMatches | ||
| if (!calendarMatches) { | ||
| cursor = cursor.plus(PLUS_ONE_DAY).startOf("day") | ||
| continue | ||
| } | ||
| if (!schedule.hours.has(cursor.hour)) { | ||
| cursor = cursor.plus(PLUS_ONE_HOUR).startOf("hour") | ||
| continue | ||
| } | ||
| if (schedule.minutes.has(cursor.minute)) fires.push(cursor.toMillis()) | ||
| cursor = cursor.plus(PLUS_ONE_MINUTE) | ||
| } | ||
| return fires | ||
| } | ||
|
|
||
| function nextIntervalFires(definition: RecurringDefinition, from: number, count: number): number[] { | ||
| if (definition.rhythm.kind !== "interval" || count <= 0) return [] | ||
| const fires: number[] = [] | ||
| let cursor = from + definition.rhythm.everyMs | ||
| for (let index = 0; index < count; index++) { | ||
| fires.push(cursor) | ||
| cursor += definition.rhythm.everyMs | ||
| } | ||
| return fires | ||
| } | ||
|
|
||
| export function computeDerivedFields( | ||
| definition: RecurringDefinition, | ||
| from: number, | ||
| completedRunCount: number, | ||
| ): { nextFireAt: number | null; nextFires: number[] } { | ||
| if (definition.paused) return { nextFireAt: null, nextFires: [] } | ||
| if (definition.stop.kind === "condition") return { nextFireAt: null, nextFires: [] } | ||
| const remaining = | ||
| definition.stop.kind === "count" ? Math.max(0, definition.stop.count - completedRunCount) : NEXT_FIRES_PREVIEW | ||
| if (remaining <= 0) return { nextFireAt: null, nextFires: [] } | ||
| const count = Math.min(NEXT_FIRES_PREVIEW, remaining) | ||
| const fires = | ||
| definition.rhythm.kind === "cron" ? nextCronFires(definition, from, count) : nextIntervalFires(definition, from, count) | ||
| return { nextFireAt: fires[0] ?? null, nextFires: fires } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.