Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

13.0 KB
Raw Blame
import { CronExpressionParser } from 'cron-parser';

export interface IsExecutionTimeInput {
  /**
   * When the schedule was (re)armed. An occurrence before it is never fired:
   * arming after a slot has passed waits for the next one, even when the task
   * has never run or its last run is older than the arming.
   */
  armedAt?: Date | null;
  /** Cron pattern in standard 5-field form: `minute hour day month weekday`. */
  cronPattern: string;
  /** Defaults to `Date.now()` when omitted — exposed for tests. */
  currentTime?: Date;
  /**
   * How long after a scheduled occurrence a dispatcher tick may still fire it.
   * Covers a late or skipped tick of the central dispatcher; an occurrence
   * older than this is skipped rather than replayed.
   */
  graceMinutes?: number;
  /** Last successful execution; an occurrence at or before it is already covered. */
  lastExecutedAt?: Date | null;
  /** IANA timezone (e.g. `Asia/Shanghai`); defaults to `UTC` when null/empty. */
  timezone: string | null;
}

/**
 * Cadence of the central schedule dispatcher (`lobe-task-schedule-dispatch`,
 * `*\/10 * * * *` in `scripts/serverLauncher/startServer.js`).
 */
export const SCHEDULE_DISPATCH_INTERVAL_MINUTES = 10;

/**
 * An occurrence can be up to one interval old on the tick that should fire it;
 * tolerate one more missed tick plus delivery jitter. Anything older is skipped
 * rather than replayed, so a stale slot never fires long after the fact.
 */
export const DEFAULT_SCHEDULE_GRACE_MINUTES = SCHEDULE_DISPATCH_INTERVAL_MINUTES * 2 + 5;

const CRON_FIELD_COUNT = 5;
const MINUTE_MS = 60 * 1000;

export const isValidTimezone = (tz: string): boolean => {
  try {
    new Intl.DateTimeFormat('en-US', { timeZone: tz });
    return true;
  } catch {
    return false;
  }
};

const parseCron = (cronPattern: string, timezone: string | null, currentDate: Date) => {
  const pattern = cronPattern.trim();
  // Only the standard 5-field form is accepted: 6-field (seconds) and `@daily`
  // style aliases are rejected so every stored pattern means the same thing to
  // the dispatcher, the task UI and the agent that wrote it.
  if (pattern.split(/\s+/).length !== CRON_FIELD_COUNT) {
    throw new Error(
      `expected 5 fields "minute hour day-of-month month day-of-week", got "${cronPattern}"`,
    );
  }
  const tz = timezone || 'UTC';
  if (!isValidTimezone(tz)) throw new Error(`unknown timezone "${tz}"`);

  return CronExpressionParser.parse(pattern, { currentDate, tz });
};

/** Expand cron weekdays for a weekly picker. Sunday is 0 in the picker. */
export const parseCronWeekdays = (cronPattern: string): number[] => {
  try {
    const { dayOfWeek } = parseCron(cronPattern, null, new Date()).fields;
    if (dayOfWeek.nthDay || dayOfWeek.hasLastChar) return [];

    return [...new Set(dayOfWeek.values.map((day) => Number(day) % 7))].sort((a, b) => a - b);
  } catch {
    return [];
  }
};

/**
 * Find the occurrence a cron pattern is due for on this dispatcher tick, or
 * `null` when nothing is due. The returned instant identifies the occurrence,
 * so the dispatcher can reserve it and never publish it twice.
 *
 * The central dispatcher polls on a fixed cadence (every 10 minutes), so the
 * question is not "does `now` match the pattern" but "is there a scheduled
 * occurrence that has passed and has not run yet". The matcher:
 *
 * - Finds `prev`, the latest occurrence at or before `now` within the grace
 *   window, in the pattern's timezone with full cron semantics (day-of-month,
 *   month, ranges, steps, lists, names).
 * - Fires only when such an occurrence exists, so a task never fires
 *   ahead of its occurrence.
 * - Fires only when `prev` is at or after `armedAt`, so arming a task after
 *   today's slot has passed waits for the next slot instead of replaying the
 *   missed one.
 * - Fires only when `lastExecutedAt < prev`, so each occurrence runs at most
 *   once no matter how many ticks fall inside the grace window. A manual run
 *   before the occurrence does not consume it.
 *
 * The grace window spans more than one tick, so `lastExecutedAt` alone cannot
 * stop a second tick from re-firing an occurrence whose run has not started
 * yet; callers pass the latest reserved occurrence into it as well.
 *
 * Invalid patterns or timezones never fire; use `validateCronPattern` on the
 * write path to reject them up front.
 */
export const findDueOccurrence = (input: IsExecutionTimeInput): Date | null => {
  const {
    armedAt,
    cronPattern,
    timezone,
    lastExecutedAt,
    currentTime = new Date(),
    graceMinutes = DEFAULT_SCHEDULE_GRACE_MINUTES,
  } = input;

  let expression: ReturnType<typeof parseCron>;
  try {
    expression = parseCron(cronPattern, timezone, currentTime);
  } catch {
    return null;
  }

  // Walk the grace window backwards minute by minute instead of calling
  // `prev()`: the dispatcher evaluates every scheduled task on each tick, and
  // `prev()` on a sparse pattern (e.g. a yearly date) scans a whole year.
  const nowMinute = Math.floor(currentTime.getTime() / MINUTE_MS) * MINUTE_MS;
  let prev: number | undefined;
  for (let offset = 0; offset <= graceMinutes; offset += 1) {
    const candidate = nowMinute - offset * MINUTE_MS;
    if (expression.includesDate(new Date(candidate))) {
      prev = candidate;
      break;
    }
  }

  if (prev === undefined) return null;
  if (armedAt && new Date(armedAt).getTime() > prev) return null;
  if (lastExecutedAt && new Date(lastExecutedAt).getTime() >= prev) return null;

  return new Date(prev);
};

export type FindUpcomingOccurrenceInput = Omit<IsExecutionTimeInput, 'graceMinutes'>;

/**
 * Find the occurrence that falls strictly between this dispatcher tick and the
 * next one, or `null`. Such an occurrence would otherwise fire up to one
 * interval late, so the dispatcher reserves it now and delays its delivery to
 * the exact minute. An occurrence on a tick boundary is left to that tick.
 */
export const findUpcomingOccurrence = (input: FindUpcomingOccurrenceInput): Date | null => {
  const { armedAt, cronPattern, currentTime = new Date(), lastExecutedAt, timezone } = input;

  let expression: ReturnType<typeof parseCron>;
  try {
    expression = parseCron(cronPattern, timezone, currentTime);
  } catch {
    return null;
  }

  const now = currentTime.getTime();
  const intervalMs = SCHEDULE_DISPATCH_INTERVAL_MINUTES * MINUTE_MS;
  const nextTick = (Math.floor(now / intervalMs) + 1) * intervalMs;

  for (
    let candidate = (Math.floor(now / MINUTE_MS) + 1) * MINUTE_MS;
    candidate < nextTick;
    candidate += MINUTE_MS
  ) {
    if (!expression.includesDate(new Date(candidate))) continue;
    if (armedAt && new Date(armedAt).getTime() > candidate) return null;
    if (lastExecutedAt && new Date(lastExecutedAt).getTime() >= candidate) return null;
    return new Date(candidate);
  }

  return null;
};

/** Whether `date` is an occurrence of the pattern. Invalid input is never one. */
export const isCronOccurrence = (
  cronPattern: string,
  timezone: string | null,
  date: Date,
): boolean => {
  if (Number.isNaN(date.getTime())) return false;
  try {
    return parseCron(cronPattern, timezone, date).includesDate(date);
  } catch {
    return false;
  }
};

const STANDARD_SCHEDULE_MINUTES = new Set([0, 30]);

const getCronMinuteField = (cronPattern: string): string => cronPattern.trim().split(/\s+/)[0];

/**
 * Whether a pattern pins a clock minute other than :00 / :30, the custom exact
 * time a deployment may reserve for paid plans. Every-minute and `*\/N` interval
 * patterns repeat rather than pick a time, so they do not count.
 */
export const isPreciseSchedulePattern = (cronPattern: string): boolean => {
  const minute = getCronMinuteField(cronPattern);
  if (!minute || minute === '*' || /^\*\/\d+$/.test(minute)) return false;

  return !minute
    .split(',')
    .every((item) => /^\d+$/.test(item) && STANDARD_SCHEDULE_MINUTES.has(Number(item)));
};

/** Whether two patterns keep the same minute field, so an edit picks no new exact time. */
export const hasSameScheduleMinute = (a: string, b: string): boolean =>
  getCronMinuteField(a) === getCronMinuteField(b);

/** Move a precise pattern to the nearest of :00 / :30 in its hour; others pass through. */
export const snapScheduleToStandardMinute = (cronPattern: string): string => {
  if (!isPreciseSchedulePattern(cronPattern)) return cronPattern;

  const [minute, ...rest] = cronPattern.trim().split(/\s+/);
  const raw = Number.parseInt(minute, 10);
  const snapped = raw >= 15 && raw < 45 ? 30 : 0;

  return [snapped, ...rest].join(' ');
};

/** Whether `findDueOccurrence` finds an occurrence to fire on this tick. */
export const isExecutionTime = (input: IsExecutionTimeInput): boolean =>
  findDueOccurrence(input) !== null;

export type CronValidationResult =
  { error: string; valid: false } | { nextRuns: Date[]; valid: true };

/**
 * Validate a cron pattern for the task scheduler and preview its next runs.
 *
 * Rejects anything the dispatcher cannot evaluate (wrong field count, out of
 * range values, unknown timezone) and patterns that never occur (e.g.
 * `0 0 30 2 *`), so callers can refuse the write instead of storing a
 * schedule that silently misfires.
 */
export const validateCronPattern = (
  cronPattern: string,
  timezone: string | null,
  options: { count?: number; from?: Date } = {},
): CronValidationResult => {
  const { count = 3, from = new Date() } = options;
  try {
    const expression = parseCron(cronPattern, timezone, from);
    const nextRuns = expression.take(count).map((date) => new Date(date.getTime()));
    if (nextRuns.length === 0) return { error: 'the pattern never occurs', valid: false };
    return { nextRuns, valid: true };
  } catch (error) {
    return { error: error instanceof Error ? error.message : String(error), valid: false };
  }
};

/** "next runs (Asia/Shanghai) → Mon 2026-09-28 09:00; …" so the agent can check the schedule it set. */
export const formatScheduleNextRuns = (runs: Date[], timezone: string | null): string => {
  const tz = timezone || 'UTC';
  const format = new Intl.DateTimeFormat('en-CA', {
    day: '2-digit',
    hour: '2-digit',
    hourCycle: 'h23',
    minute: '2-digit',
    month: '2-digit',
    timeZone: tz,
    weekday: 'short',
    year: 'numeric',
  });
  const label = (date: Date) => {
    const parts = Object.fromEntries(format.formatToParts(date).map((p) => [p.type, p.value]));
    return `${parts.weekday} ${parts.year}-${parts.month}-${parts.day} ${parts.hour}:${parts.minute}`;
  };
  return `next runs (${tz}) → ${runs.map(label).join('; ')}`;
};

export type SchedulePreviewResult =
  { error: string; valid: false } | { preview: string; valid: true };

/**
 * Validate the schedule a task will end up with and describe its next runs,
 * shared by every runtime of the `setTaskSchedule` tool so they all return the
 * same confirmation (or refusal) to the agent.
 */
export const previewSchedule = (
  cronPattern: string,
  timezone: string | null,
  options: { count?: number; from?: Date } = {},
): SchedulePreviewResult => {
  const result = validateCronPattern(cronPattern, timezone, options);
  if (!result.valid) return result;
  return { preview: formatScheduleNextRuns(result.nextRuns, timezone), valid: true };
};

/** The refusal both runtimes return when `setTaskSchedule` is given an unusable schedule. */
export const formatInvalidScheduleMessage = (identifier: string, error: string): string =>
  `Invalid schedule for task ${identifier}: ${error}. Use a standard 5-field cron expression "minute hour day-of-month month day-of-week" (e.g. "0 9 * * 1-5") with an IANA timezone. Nothing was updated.`;

export interface ScheduleUpdatePatch {
  automationMode?: string | null;
  schedulePattern?: string | null;
  scheduleTimezone?: string | null;
}

/**
 * Validate the pattern/timezone pair a task will end up with after `patch` is
 * applied to its stored schedule. A field the patch leaves out keeps its
 * stored value, so a pattern-only change is checked against the stored
 * timezone and enabling schedule mode re-checks the stored pair: a legacy
 * invalid value is refused instead of being carried into a schedule the
 * dispatcher can never run.
 *
 * Returns `undefined` when the patch does not touch the schedule or leaves no
 * pattern to check, otherwise the same result as `previewSchedule`.
 */
export const validateScheduleUpdate = (
  stored: { pattern?: string | null; timezone?: string | null } | null | undefined,
  patch: ScheduleUpdatePatch,
  options: { count?: number; from?: Date } = {},
): SchedulePreviewResult | undefined => {
  const touched =
    patch.schedulePattern !== undefined ||
    patch.scheduleTimezone !== undefined ||
    patch.automationMode === 'schedule';
  if (!touched) return undefined;

  const pattern = patch.schedulePattern !== undefined ? patch.schedulePattern : stored?.pattern;
  const timezone = patch.scheduleTimezone !== undefined ? patch.scheduleTimezone : stored?.timezone;

  if (pattern) return previewSchedule(pattern, timezone ?? null, options);
  if (timezone && !isValidTimezone(timezone)) {
    return { error: `unknown timezone "${timezone}"`, valid: false };
  }
  return undefined;
};