@sema-agent/core
Version:
Stateless, task-oriented AI agent core
69 lines (68 loc) • 5.77 kB
TypeScript
/**
* The engine's one cron grammar and its one next-fire search. The tool shell's CronCreate refuses an expression at intake
* with it, and a scheduler backend computes when an accepted expression fires next with it — so the set of expressions an
* intake accepts is, by construction, the set the firing side can compute a time for. A host that takes cron expressions
* at its own door asks these same two functions; it does not keep a second grammar.
*
* The reading follows the reference terminal's scheduler: the five-field grammar and its field expansion, the
* day-of-month / day-of-week union, "the first whole minute strictly after the base instant", one fire per repeated
* wall-clock time, and a bounded walk that answers "never" for a date that does not occur. What differs is where the
* clock is read: the reference reads the process's local zone (it runs on the user's machine); this function reads the
* zone it is handed and never the process's own, because the engine also runs in containers whose zone is not the user's.
*
* Pure: no I/O, no clock, no process time zone. Every fault of an input is answered as a value, never thrown.
*/
import { SchedulerError } from "./scheduler.js";
import type { Result } from "../internal/harness-types.js";
/** What separates two fields: any run of white space, as the reference reads an expression — the grammar's and every reader's
* that splits an expression into fields (the display too). */
export declare const CRON_FIELD_SEPARATOR: RegExp;
/**
* Does the engine's cron grammar accept `expr`? Exactly when {@link nextCronFireTime} would not refuse it as an expression:
* five fields of `*`, `*\/N`, `N`, `N-M`, `N-M/S` or comma lists of those, each value inside its field's range, at most
* 100 characters after trimming. The grammar's alphabet is digits, `* / , -` and white space, and any run of white space
* separates two fields — a line break or a carriage return among it, as the reference reads an expression. So an expression
* this accepts carries no quote, no substitution, no redirection and no word but its fields; it may carry a line break
* between two of them (a shell's command separator, a crontab's record end), so a consumer that writes it into one line of
* text writes the five fields joined by single spaces, not the text as given. An expression can be grammatical and still
* never fire (a 30th of February): the intake question "will it ever fire" is {@link nextCronFireTime} answering `null`.
*/
export declare function isValidCronExpr(expr: string): boolean;
/**
* When does the cron expression `expr` fire next, strictly after the instant `after`, on the wall clock of `timeZone`?
*
* - **Grammar.** Five fields `minute hour day-of-month month day-of-week`, each `*`, `*\/N`, `N`, `N-M`, `N-M/S` or a
* comma list of those; ranges 0-59 / 0-23 / 1-31 / 1-12 / 0-6 (7 is Sunday too); no seconds field, no month or day
* names, no `@` shorthands, no `L` / `W` / `#` / `?`; at most 100 characters after trimming ({@link isValidCronExpr}).
* - **Day.** When day-of-month and day-of-week are both restricted, a day matches either; a field whose values cover its
* whole range (`*`, `1-31`, `0-7`) is unrestricted, and the day is then decided by the other field alone.
* - **Time.** The fields are read on the wall clock of `timeZone` — required, and any zone `Intl` accepts: an IANA name
* such as `"Asia/Tokyo"`, or a fixed offset such as `"+05:30"` (a clock with no shifts) where the runtime reads one; the
* same acceptance as the engine's own zone check for the environment block. The process's own zone is never read. The answer is the first whole minute strictly after `after` whose wall time
* matches. A wall time a forward shift skips does not fire. A wall time a backward shift repeats fires once.
* - **`recurring`** (default `true`) decides one thing only: which pass of a repeated wall time counts. A recurring job
* fires a repeated time in its first pass and never in the repeat; with `recurring: false` (a job that fires once and is
* removed) the first pass or the repeat counts, whichever first follows `after`. Away from a backward shift the two
* answers are the same.
* - **Missed fires and offsets are the caller's.** Pass the last fire as `after` (the creation time when it never fired):
* an answer at or before now means one fire is due, however many slots were missed. No per-job offset is added.
* - **Answer.** `{ ok: true, value }` with the epoch milliseconds of the fire; `{ ok: true, value: null }` when no wall
* minute ever matches (a 30th of February), found by a walk bounded at the reference walker's 527 040 steps; `{ ok:
* false, error }` with a {@link SchedulerError} of code `"invalid_schedule"` naming the input at fault — an expression
* the grammar refuses, a zone `Intl` does not know, an `after` that is not a number of milliseconds from 0 to the end
* of `Date`'s range, or a `recurring` that is not a boolean. Nothing is thrown.
*/
export declare function nextCronFireTime(expr: string, opts: {
readonly after: number;
readonly timeZone: string;
readonly recurring?: boolean;
}): Result<number | null, SchedulerError>;
/**
* The sentence an intake refuses `expr` with, judged at `after` in `timeZone`, or `null` when it accepts it: the refusal of
* {@link nextCronFireTime} (the grammar's, the zone's or the instant's own words) when it refuses, a never-fires sentence
* when it answers `null`. The intake asks the matcher the firing side computes with, so it accepts exactly what can be timed.
*/
export declare function cronIntakeRefusal(expr: string, at: {
readonly after: number;
readonly timeZone: string;
}): string | null;