| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698 |
- import type { CronEventBusPort } from './host-ports.js';
- import {
- logger,
- backgroundCtx,
- getMetricsReporter,
- AppError,
- getCronStore,
- type CronHostUtils,
- } from './host-utils.js';
- import type { CronExecutor, ExecuteResult } from './executor.js';
- import type { CronConfig, CronTaskState, CronConfigUpdate, SessionConfig } from './types.js';
- /** Bounded origin of a cron config mutation. Keep this low-cardinality. */
- export type CronMutationSource = 'ui' | 'agent_tool' | 'http_api' | 'system';
- /** Bounded origin of a cron trigger attempt. */
- export type CronTriggerSource = 'schedule' | 'ui' | 'agent_tool' | 'http_api' | 'system';
- /** Semantic delivery target. Never put the concrete session id into metrics labels. */
- export type CronDeliveryTarget = 'new' | 'root' | 'me' | 'session_id';
- export interface CronMutationMetricsContext {
- mutationSource?: CronMutationSource;
- deliveryTarget?: CronDeliveryTarget;
- }
- export interface CronTriggerMetricsContext {
- triggerSource?: CronTriggerSource;
- deliveryTarget?: CronDeliveryTarget;
- }
- export interface CronRegistryOptions {
- /**
- * Unreference Croner timers so short-lived embedded hosts can finish after
- * serving a one-shot request. Long-lived daemon hosts keep the default false.
- */
- unrefTimers?: boolean;
- scheduler: CronSchedulerPort;
- cronHost?: CronHostUtils;
- }
- export interface CronRegistryStartOptions {
- /**
- * Whether tasks restored from disk may execute immediately. A quarantined
- * clone still registers tasks for inspection and later user mutations.
- */
- restoredTaskExecution?: 'enabled' | 'quarantined';
- }
- export interface CronJobPort {
- nextRun(): Date | null;
- stop(): void;
- }
- export interface CronSchedulerPort {
- createJob(
- config: CronConfig,
- options: { unref?: boolean | undefined },
- onTick: () => void,
- ): CronJobPort;
- assertSchedulable(config: CronConfig): void;
- }
- export class CronRegistry {
- private readonly tasks: Map<string, { state: CronTaskState; job: CronJobPort }> = new Map();
- private globalEnabled = true;
- private eventBus: CronEventBusPort | null = null;
- constructor(
- executor: CronExecutor,
- eventBus: CronEventBusPort | undefined,
- options: CronRegistryOptions,
- );
- constructor(
- private readonly executor: CronExecutor,
- eventBus?: CronEventBusPort,
- private readonly options: Partial<CronRegistryOptions> = {},
- ) {
- this.eventBus = eventBus ?? null;
- }
- private get scheduler(): CronSchedulerPort {
- const scheduler = this.options.scheduler;
- if (!scheduler) {
- throw new AppError(
- '[@mavis/cron] CronRegistry requires a host-provided CronSchedulerPort. ' +
- 'Runtime hosts must inject a scheduler instead of relying on agent-core timers.',
- 'CRON_SCHEDULER_MISSING',
- 500,
- );
- }
- return scheduler;
- }
- private get cronStore() {
- return this.options.cronHost?.cronStore ?? getCronStore();
- }
- /** Scan disk, register all cron jobs. Called once at startup. */
- async start(startOptions: CronRegistryStartOptions = {}): Promise<void> {
- this.assertSchedulerConfigured();
- const cronStore = this.cronStore;
- const configs = await cronStore.listAll();
- for (const { agentName, cronName, config, cronId } of configs) {
- const key = this.taskKey(agentName, cronName);
- const state: CronTaskState = {
- agentName,
- cronName,
- config,
- ...(cronId ? { cronId } : {}),
- enabled: startOptions.restoredTaskExecution !== 'quarantined' && !config.disabled,
- lastRun: null,
- lastResult: null,
- lastError: null,
- nextRun: null,
- status: 'idle',
- };
- try {
- const job = this.createJob(config, state);
- state.nextRun = job.nextRun()?.getTime() ?? null;
- this.tasks.set(key, { state, job });
- logger.info(
- { agentName, cronName, schedule: config.schedule, nextRun: state.nextRun },
- '[cron] Task registered',
- );
- } catch (err) {
- logger.error(
- { agentName, cronName, schedule: config.schedule, err: (err as Error).message },
- '[cron] Failed to register task — invalid schedule?',
- );
- }
- }
- logger.info(backgroundCtx(), `[cron] Registry started, count=${this.tasks.size}`);
- getMetricsReporter().incr('cron_engine_started_total');
- // Fire-and-forget: at startup, sweep every cron's persisted session
- // history to apply retention policy that was previously dependent on
- // an in-memory map and therefore lost across daemon restarts.
- const sweepCtx = backgroundCtx();
- logger.info(sweepCtx, '[cron] Startup session retention sweep — begin');
- void this.executor
- .cleanupAllCronSessions()
- .then(() => {
- logger.info(sweepCtx, '[cron] Startup session retention sweep — done');
- })
- .catch((err: unknown) => {
- logger.warn(
- sweepCtx,
- `[cron] Startup session retention sweep failed (non-fatal), err=${(err as Error).message}`,
- );
- });
- // Fire-and-forget: delete sessions whose purpose matches a cron task
- // that no longer exists (orphans from older deletion logic that didn't
- // sweep the session store by purpose).
- //
- // Build aliveKeys from `configs` (config files present on disk) rather
- // than `this.tasks` (jobs that successfully constructed). A cron whose
- // schedule/timezone temporarily fails Croner validation must NOT have its
- // session history wiped — the config file is still present, the user
- // hasn't deleted the task, only registration failed.
- const aliveKeys = new Set(
- configs.map(({ agentName, cronName }) => this.taskKey(agentName, cronName)),
- );
- void this.executor
- .cleanupOrphanCronSessions(aliveKeys)
- .then(() => {
- logger.debug(backgroundCtx(), '[cron] Orphan cron session cleanup — done');
- })
- .catch((err: unknown) => {
- logger.warn(
- backgroundCtx(),
- `[cron] Orphan cron session cleanup failed (non-fatal), err=${(err as Error).message}`,
- );
- });
- }
- /** Stop all cron jobs. */
- stop(): void {
- for (const [key, { job }] of this.tasks) {
- job.stop();
- logger.debug(backgroundCtx(), `[cron] Job stopped, key=${key}`);
- }
- this.tasks.clear();
- logger.info(backgroundCtx(), '[cron] Registry stopped');
- }
- /** List all tasks for a given agent. */
- listTasks(agentName: string): CronTaskState[] {
- const result: CronTaskState[] = [];
- for (const { state, job } of this.tasks.values()) {
- if (state.agentName === agentName) {
- state.nextRun = job.nextRun()?.getTime() ?? null;
- result.push(state);
- }
- }
- return result;
- }
- /** Get a specific task by agent name and cron name. */
- getTask(agentName: string, cronName: string): CronTaskState | undefined {
- const entry = this.tasks.get(this.taskKey(agentName, cronName));
- if (entry) {
- entry.state.nextRun = entry.job.nextRun()?.getTime() ?? null;
- }
- return entry?.state;
- }
- /** Trigger a task immediately (manual trigger via API). */
- async triggerTask(
- agentName: string,
- cronName: string,
- context: CronTriggerMetricsContext = {},
- ): Promise<ExecuteResult> {
- const entry = this.tasks.get(this.taskKey(agentName, cronName));
- if (!entry) {
- throw new AppError(
- `Cron task '${cronName}' not found for agent '${agentName}'`,
- 'CRON_TASK_NOT_FOUND',
- 404,
- );
- }
- const result = await this.executeAndRecordTrigger(entry.state, {
- triggerSource: context.triggerSource ?? 'http_api',
- deliveryTarget: context.deliveryTarget,
- force: true,
- });
- if (entry.state.config.scheduleType === 'once') {
- await this.completeOnceAfterExecution(agentName, cronName, result);
- }
- return result;
- }
- /**
- * Update cron task configuration — persists to config.yaml and updates in-memory state.
- * If schedule or timezone changed, recreates the Cron job (new job created before old one stopped).
- */
- async updateConfig(
- agentName: string,
- cronName: string,
- update: CronConfigUpdate,
- context: CronMutationMetricsContext = {},
- ): Promise<CronTaskState | undefined> {
- const cronStore = this.cronStore;
- const key = this.taskKey(agentName, cronName);
- const entry = this.tasks.get(key);
- if (!entry) return undefined;
- const oldConfig = entry.state.config;
- const needsRecreate = update.schedule !== undefined || update.timezone !== undefined;
- if (needsRecreate) {
- this.assertCronConfigSchedulable(applyCronScheduleUpdate(oldConfig, update));
- }
- const newConfig = await cronStore.update(agentName, cronName, update);
- if (needsRecreate) {
- // Create new job first — if this throws (invalid schedule), old job remains running
- const newJob = this.createJob(newConfig, entry.state);
- // Success — stop old job and replace
- entry.job.stop();
- entry.job = newJob;
- entry.state.nextRun = newJob.nextRun()?.getTime() ?? null;
- }
- // Update in-memory state
- entry.state.config = newConfig;
- entry.state.enabled = !newConfig.disabled;
- this.eventBus?.emit('cron.updated', 'cron-registry', { agentName, cronName });
- getMetricsReporter().incr(
- 'cron_task_updated_total',
- cronMutationMetricLabels(entry.state, context),
- );
- return entry.state;
- }
- async createTask(
- agentName: string,
- cronName: string,
- config: CronConfig,
- context: CronMutationMetricsContext = {},
- ): Promise<CronTaskState> {
- const cronStore = this.cronStore;
- const key = this.taskKey(agentName, cronName);
- if (this.tasks.has(key)) {
- throw new AppError(
- `Cron task already registered: ${agentName}/${cronName}`,
- 'CRON_TASK_EXISTS',
- 409,
- );
- }
- this.assertCronConfigSchedulable(config);
- await cronStore.create(agentName, cronName, config);
- // Surface the store-minted cron_id (archon_biz cron contract parity).
- // `create` returns only the config, so resolve the id via a best-effort
- // read-back; failure is non-fatal and simply leaves cronId undefined
- // (engine keying is unchanged either way).
- const cronId = await this.resolveCreatedCronId(agentName, cronName);
- const state: CronTaskState = {
- agentName,
- cronName,
- config,
- ...(cronId ? { cronId } : {}),
- enabled: !config.disabled,
- lastRun: null,
- lastResult: null,
- lastError: null,
- nextRun: null,
- status: 'idle',
- };
- const job = this.createJob(config, state);
- state.nextRun = job.nextRun()?.getTime() ?? null;
- this.tasks.set(key, { state, job });
- logger.info(
- { agentName, cronName, schedule: config.schedule, nextRun: state.nextRun },
- '[cron] Task created',
- );
- this.eventBus?.emit('cron.created', 'cron-registry', { agentName, cronName });
- getMetricsReporter().incr('cron_task_created_total', cronMutationMetricLabels(state, context));
- return state;
- }
- /** Delete a cron task — stops the job and removes from disk. */
- async deleteTask(
- agentName: string,
- cronName: string,
- context: CronMutationMetricsContext = {},
- ): Promise<boolean> {
- const cronStore = this.cronStore;
- const key = this.taskKey(agentName, cronName);
- const entry = this.tasks.get(key);
- if (!entry) return false;
- entry.job.stop();
- this.tasks.delete(key);
- const cancelledQueued = this.executor.cancelQueuedCron?.(agentName, cronName) ?? 0;
- if (cancelledQueued > 0) {
- logger.info(
- backgroundCtx(),
- `[cron] Cancelled queued messages for deleted task, agentName=${agentName}, cronName=${cronName}, count=${cancelledQueued}`,
- );
- }
- // Archive cron-created sessions before removing history
- try {
- await this.executor.cleanupSessionsOnDelete(agentName, cronName);
- } catch (err) {
- logger.warn(
- backgroundCtx(),
- `[cron] Session cleanup on delete failed (non-fatal), agentName=${agentName}, cronName=${cronName}, err=${(err as Error).message}`,
- );
- }
- // Delegate file deletion to the store
- await cronStore.delete(agentName, cronName);
- logger.info(
- backgroundCtx(),
- `[cron] Task deleted, agentName=${agentName}, cronName=${cronName}`,
- );
- this.eventBus?.emit('cron.deleted', 'cron-registry', { agentName, cronName });
- getMetricsReporter().incr(
- 'cron_task_deleted_total',
- cronMutationMetricLabels(entry.state, context),
- );
- return true;
- }
- /** Get session run history for a cron task (oldest-first). */
- async getSessionHistory(
- agentName: string,
- cronName: string,
- ): ReturnType<ReturnType<typeof getCronStore>['getSessionHistory']> {
- const cronStore = this.cronStore;
- return cronStore.getSessionHistory(agentName, cronName);
- }
- /** List all tasks across all agents. */
- listAllTasks(): CronTaskState[] {
- const result: CronTaskState[] = [];
- for (const { state, job } of this.tasks.values()) {
- state.nextRun = job.nextRun()?.getTime() ?? null;
- result.push(state);
- }
- return result;
- }
- /** Set global enabled flag. */
- setGlobalEnabled(enabled: boolean): void {
- this.globalEnabled = enabled;
- logger.info(backgroundCtx(), `[cron] Global enabled flag changed, enabled=${enabled}`);
- }
- /** Get global enabled flag. */
- isGlobalEnabled(): boolean {
- return this.globalEnabled;
- }
- // ── Harness registration ──
- /**
- * Register cron tasks from a harness's crons directory.
- */
- async registerHarnessCrons(harnessName: string, agentNameList: string[]): Promise<string[]> {
- this.assertSchedulerConfigured();
- const cronStore = this.cronStore;
- const registered: string[] = [];
- for (const agentName of agentNameList) {
- const configs = await cronStore.listByAgent(agentName);
- for (const { cronName, config, cronId } of configs) {
- try {
- // Register or update in-memory timer
- const key = this.taskKey(agentName, cronName);
- if (this.tasks.has(key)) {
- // Task already loaded — re-sync config and possibly recreate timer
- const existing = this.tasks.get(key)!;
- const oldConfig = existing.state.config;
- const needsRecreate =
- oldConfig.schedule !== config.schedule || oldConfig.timezone !== config.timezone;
- if (needsRecreate) {
- const newJob = this.createJob(config, existing.state);
- existing.job.stop();
- existing.job = newJob;
- existing.state.nextRun = newJob.nextRun()?.getTime() ?? null;
- }
- existing.state.config = config;
- existing.state.enabled = !config.disabled;
- this.eventBus?.emit('cron.updated', 'cron-registry', { agentName, cronName });
- } else {
- // New task — create in-memory timer
- const state: CronTaskState = {
- agentName,
- cronName,
- config,
- ...(cronId ? { cronId } : {}),
- enabled: !config.disabled,
- lastRun: null,
- lastResult: null,
- lastError: null,
- nextRun: null,
- status: 'idle',
- };
- const job = this.createJob(config, state);
- state.nextRun = job.nextRun()?.getTime() ?? null;
- this.tasks.set(key, { state, job });
- logger.info(
- { agentName, cronName, schedule: config.schedule, nextRun: state.nextRun },
- '[cron] Task created',
- );
- this.eventBus?.emit('cron.created', 'cron-registry', { agentName, cronName });
- }
- registered.push(cronName);
- } catch (err) {
- logger.error(
- { harnessName, cronName, err: (err as Error).message },
- '[cron] Failed to register harness cron timer',
- );
- }
- }
- }
- if (registered.length > 0) {
- logger.info(
- backgroundCtx(),
- `[cron] Harness crons registered, harnessName=${harnessName}, count=${registered.length}`,
- );
- }
- return registered;
- }
- /**
- * Remove all cron tasks registered by a specific harness.
- * Delegates symlink cleanup to CronStore, then stops in-memory timers.
- */
- async unregisterHarnessCrons(harnessName: string): Promise<string[]> {
- // 1. Stop in-memory timers for matching tasks
- const exactPrefix = `${harnessName}:`;
- const reinPrefix = `${harnessName}--`;
- const toRemove: Array<{ agentName: string; cronName: string }> = [];
- for (const [key, { state }] of this.tasks) {
- if (key.startsWith(exactPrefix) || key.startsWith(reinPrefix)) {
- toRemove.push({ agentName: state.agentName, cronName: state.cronName });
- }
- }
- const removed: string[] = [];
- for (const { agentName, cronName } of toRemove) {
- const key = this.taskKey(agentName, cronName);
- const entry = this.tasks.get(key);
- if (entry) {
- entry.job.stop();
- this.tasks.delete(key);
- this.eventBus?.emit('cron.deleted', 'cron-registry', { agentName, cronName });
- removed.push(cronName);
- }
- }
- if (removed.length > 0) {
- logger.info(
- backgroundCtx(),
- `[cron] Harness crons unregistered, harnessName=${harnessName}, count=${removed.length}`,
- );
- }
- return removed;
- }
- // ── Private ──
- private createJob(config: CronConfig, state: CronTaskState): CronJobPort {
- const { agentName, cronName } = state;
- return this.scheduler.createJob(config, { unref: this.options.unrefTimers }, () => {
- if (!this.globalEnabled) return;
- this.executeAndRecordTrigger(state, { triggerSource: 'schedule' })
- .then((result) => {
- if (state.config.scheduleType === 'once') {
- void this.completeOnceAfterExecution(agentName, cronName, result).catch(
- (err: unknown) => {
- logger.error(
- { agentName, cronName, err: (err as Error).message },
- '[cron] Failed to complete once task',
- );
- },
- );
- }
- })
- .catch((err: unknown) => {
- logger.error(
- { agentName, cronName, err: (err as Error).message },
- '[cron] Unhandled execution error',
- );
- });
- });
- }
- private async executeAndRecordTrigger(
- state: CronTaskState,
- context: CronTriggerMetricsContext & { force?: boolean },
- ): Promise<ExecuteResult> {
- const triggerSource = context.triggerSource ?? 'schedule';
- const result = await this.executor.execute(state, { force: context.force });
- getMetricsReporter().incr('cron_task_triggered_total', {
- trigger_source: triggerSource,
- trigger_outcome: cronTriggerOutcome(result),
- ...cronTaskMetricLabels(state, context.deliveryTarget),
- });
- return result;
- }
- private async completeOnceAfterExecution(
- agentName: string,
- cronName: string,
- result: ExecuteResult,
- ): Promise<void> {
- if (result.executed) {
- await this.completeOnceTask(agentName, cronName);
- return;
- }
- if (result.reason !== 'enqueued' || !result.queuedSend) return;
- void result.queuedSend.then((drainResult) => {
- if (drainResult !== 'sent') return;
- return this.completeOnceTask(agentName, cronName).catch((err: unknown) => {
- logger.error(
- { agentName, cronName, err: (err as Error).message },
- '[cron] Failed to complete once task after queued send',
- );
- });
- });
- }
- private async completeOnceTask(agentName: string, cronName: string): Promise<void> {
- const key = this.taskKey(agentName, cronName);
- const entry = this.tasks.get(key);
- if (!entry || entry.state.config.scheduleType !== 'once') return;
- if (entry.state.config.deleteAfterRun === false) return;
- entry.job.stop();
- this.tasks.delete(key);
- await this.cronStore.delete(agentName, cronName);
- getMetricsReporter().incr(
- 'cron_task_deleted_total',
- cronMutationMetricLabels(entry.state, { mutationSource: 'system' }),
- );
- logger.info(
- backgroundCtx(),
- `[cron] Once task completed and deleted, agentName=${agentName}, cronName=${cronName}`,
- );
- this.eventBus?.emit('cron.deleted', 'cron-registry', { agentName, cronName });
- }
- private assertCronConfigSchedulable(config: CronConfig): void {
- try {
- this.scheduler.assertSchedulable(config);
- } catch (err) {
- if (err instanceof AppError) throw err;
- throw new AppError(err instanceof Error ? err.message : String(err), 'VALIDATION_ERROR', 400);
- }
- }
- private assertSchedulerConfigured(): void {
- void this.scheduler;
- }
- private taskKey(agentName: string, cronName: string): string {
- return `${agentName}:${cronName}`;
- }
- /**
- * Best-effort resolution of a freshly created cron's stable cron_id.
- * `CronStorePort.create` returns only the config, so we read the id back
- * from `listByAgent` (the only required port method that carries cronId).
- * Any failure is swallowed — cronId is purely additive metadata and the
- * engine never keys on it.
- */
- private async resolveCreatedCronId(
- agentName: string,
- cronName: string,
- ): Promise<string | undefined> {
- try {
- const list = await this.cronStore.listByAgent(agentName);
- return list.find((entry) => entry.cronName === cronName)?.cronId;
- } catch {
- return undefined;
- }
- }
- }
- function cronMutationMetricLabels(
- state: CronTaskState,
- context: CronMutationMetricsContext,
- ): Record<string, string> {
- return {
- mutation_source: context.mutationSource ?? 'http_api',
- ...cronTaskMetricLabels(state, context.deliveryTarget),
- };
- }
- function cronTaskMetricLabels(
- state: CronTaskState,
- deliveryTarget?: CronDeliveryTarget,
- ): Record<string, string> {
- return {
- owner_agent: state.agentName,
- schedule_type: state.config.scheduleType ?? 'cron',
- delivery_target: deliveryTarget ?? deliveryTargetForSession(state.config.session),
- };
- }
- function deliveryTargetForSession(session: SessionConfig): CronDeliveryTarget {
- if (session.mode === 'new') return 'new';
- if (session.mode === 'root') return 'root';
- return 'session_id';
- }
- function cronTriggerOutcome(result: ExecuteResult): string {
- if (result.executed) return 'executed';
- if (result.reason === 'enqueued') return 'enqueued';
- if (result.reason === 'error') return 'error';
- return 'skipped';
- }
- function applyCronScheduleUpdate(config: CronConfig, update: CronConfigUpdate): CronConfig {
- return {
- ...config,
- ...(update.schedule !== undefined ? { schedule: update.schedule } : {}),
- ...(update.timezone !== undefined
- ? update.timezone === null
- ? { timezone: undefined }
- : { timezone: update.timezone }
- : {}),
- };
- }
|