import { type DbExec, type DbExecStatement } from "../db/client.js"; import type { MigrationEntry } from "../db/migrations.js"; export type AutomationRunStatus = "running" | "success" | "error" | "interrupted" | "skipped"; export interface AutomationRun { id: string; owner: string; automation: string; path: string; scope: string | null; orgId: string | null; appId: string | null; runId: string | null; threadId: string | null; status: AutomationRunStatus; startedAt: number; finishedAt: number | null; error: string | null; errorCode: string | null; /** A queued "Run now" row that a worker claims before it starts. */ dispatchPending: boolean; /** When a worker last claimed this queued dispatch, if ever. */ claimedAt: number | null; } export interface StartAutomationRunInput { owner: string; automation: string; path: string; scope?: string | null; orgId?: string | null; appId?: string | null; notificationEmail?: string | null; runId?: string | null; threadId?: string | null; dispatchPending?: boolean; } /** * How long a queued dispatch's claim stays valid before another worker may take * it over (`claimAutomationRun`). The stale-lock recovery must leave a row * inside this lease to its own worker. */ export declare function automationRunClaimLeaseMs(): number; /** How many finished runs each automation retains, in addition to unfinished runs. */ export declare const RUNS_RETAINED_PER_AUTOMATION = 50; export declare const AUTOMATION_RUN_MIGRATIONS: MigrationEntry[]; /** Kept for callers of the public export; the store owns these migrations. */ export declare function runAutomationRunMigrations(_nitroApp?: unknown): Promise; export declare const automationRunHistoryStore: import("../db/store-registry.js").Store; export declare function ensureTable(): Promise; export interface StartAutomationRunOptions { afterInsert?: (tx: DbExec, historyId: string) => Promise; afterCommit?: () => void | Promise; } export declare function startAutomationRun(input: StartAutomationRunInput, options?: StartAutomationRunOptions): Promise; export declare function getAutomationRun(id: string): Promise; export declare function claimAutomationRun(id: string): Promise; export declare function listUnclaimedAutomationRuns(options?: { appId?: string | null; olderThanMs?: number; limit?: number; }): Promise; export declare function processPendingAutomationFailureAlerts(options?: { limit?: number; runId?: string; }): Promise<{ attempted: number; delivered: number; deferred: number; suppressed: number; uncertain: number; failed: number; }>; export declare class AutomationRunHistoryWriteError extends Error { readonly historyId: string; readonly errorCode = "background_automation_history_write_failed"; constructor(historyId: string); } export declare class AutomationRunHistoryClaimLostError extends Error { readonly historyId: string; readonly errorCode = "background_automation_claim_lost"; constructor(historyId: string); } export interface FinishAutomationRunOptions { requirePersisted?: boolean; /** * `notify: false` records the run without queueing the owner email. Used for * the early failures of a streak that will pause: the owner is told once, * by the run that pauses it. */ notify?: boolean; /** * Finish only if the stored claim still equals this snapshot. The stale-lock * recovery passes the `claimedAt` it read, so a worker that claims the run * between that read and this write keeps its run instead of losing it to a * stale terminal write. */ expectedClaimedAt?: number | null; /** Successors share the firing id, so recovery must also fence its worker. */ expectedRunId?: string | null; } export declare function finishAutomationRun(id: string, status: Exclude, error?: string, errorCode?: string, options?: FinishAutomationRunOptions): Promise; export declare function attachAutomationRunThread(id: string, threadId: string, runId: string, options?: { requirePersisted?: boolean; expectedRunId?: string | null; }): Promise; /** * Forget an automation's executions. * * History is keyed by the automation's name, which is reusable: deleting * "digest" and creating a new "digest" would otherwise show the old * definition's runs as the new one's history. */ export declare function ensureAutomationRunHistoryReady(): Promise; export declare function prepareAutomationRunsDelete(owner: string, automation: string, guard?: { sql: string; args: readonly unknown[]; }): DbExecStatement; export declare function deleteAutomationRunsWithDb(client: DbExec, owner: string, automation: string): Promise; export declare function deleteAutomationRuns(owner: string, automation: string): Promise; export declare function listAutomationRuns(options: { owners: string[]; automation: string; appId?: string | null; limit?: number; }): Promise; export declare function listLatestAutomationRuns(options: { owners: string[]; appId?: string | null; }): Promise;