import { AppSyncAction, type AppSyncLastRun, type AppSyncRunSummary as AppSyncRunSummaryType } from "@opengeni/contracts"; import { type Database } from "./database.js"; /** * App data sync persistence (docs/app-sync.md). A sync is an ordinary * scheduled task whose action is `app_sync`; this module keeps what only * syncs need: the executor queue, personal opt-ins and each person's last * run. Everything is read and written under the workspace's row-level * security, except `listDueAppSyncRuns`, which reads ids only. */ type Scope = { accountId: string; workspaceId: string; }; /** How long a claimed run is held before another executor may take it over; runs renew it. */ export declare const APP_SYNC_LEASE_SECONDS = 300; /** Parse a stored action; null when it is not a well-formed sync. */ export declare function appSyncActionOf(action: unknown): AppSyncAction | null; /** The ids of a Site's live syncs, oldest first. */ export declare function listAppSyncTaskIds(db: Database, scope: Scope, siteId: string): Promise; /** Each sync's newest run. */ export declare function latestAppSyncRuns(db: Database, workspaceId: string, taskIds: readonly string[]): Promise>; /** * Queue a sync run for the executor; queuing the same run again does nothing. * `chainDepth` is the run's automation chain depth when an automation's agent * started it (its run's depth plus one); scheduled and event runs keep the * depth recorded on the run itself. */ export declare function enqueueAppSyncRun(db: Database, input: Scope & { taskId: string; runId: string; chainDepth?: number; }): Promise; export type DueAppSyncRun = { runId: string; accountId: string; workspaceId: string; }; /** Runs waiting for the executor across workspaces (ids only). */ export declare function listDueAppSyncRuns(db: Database, limit?: number): Promise; export type ClaimedAppSyncRun = { runId: string; taskId: string; accountId: string; workspaceId: string; attempts: number; chainDepth: number; triggerType: string; /** Another run of the same sync holds a live lease: this one must not run (overlap `skip`). */ overlapping: boolean; /** This claim's lease token: renewing and settling need it (see `renewAppSyncLease`). */ leaseToken: string; }; /** * Lease one queued run and mark it running. Null when another executor holds * it, it already finished, or it is no longer queued. Claims of the same sync * are serialized, so of two runs of one sync only the first runs; the other * comes back `overlapping` while the first's lease is live. */ export declare function claimAppSyncRun(db: Database, input: { workspaceId: string; runId: string; leaseSeconds?: number; }): Promise; /** * A claim's fence: the lease token and attempt count it got. A claim by a * release without tokens leaves the token unchanged but still counts an * attempt, so both are checked. */ export type AppSyncLease = { leaseToken: string; attempts: number; }; /** * Extend a running sync's lease, so a long run is not claimed again. False * when the run is gone or, given the claim's lease, another executor has * claimed it since: the caller must then stop calling the tool and writing, * and leave the run to the current holder. */ export declare function renewAppSyncLease(db: Database, input: { workspaceId: string; runId: string; leaseSeconds?: number; lease?: AppSyncLease; }): Promise; /** Settle a sync run and remove it from the queue. False when it had already settled. */ export declare function finishAppSyncRun(db: Database, input: { workspaceId: string; runId: string; status: "succeeded" | "failed" | "skipped"; error: string | null; summary: AppSyncRunSummaryType | null; /** Settle only while this claim still holds the run; false (and nothing changes) otherwise. */ lease?: AppSyncLease; }): Promise; export type AppSyncSubscription = { taskId: string; subjectId: string; createdAt: string; lastRun: AppSyncLastRun | null; /** The account (route server id) the person chose; null when they didn't. */ account: string | null; }; /** * Opt a person in to (or out of) a personal sync, on the account they chose * (kept as it is when `account` is omitted). Returns whether they are opted in. */ export declare function setAppSyncSubscription(db: Database, input: Scope & { taskId: string; subjectId: string; optedIn: boolean; account?: string | null; }): Promise; /** Remove everyone's opt-in to a sync (its tool or target changed); returns how many. */ export declare function clearAppSyncSubscriptions(db: Database, workspaceId: string, taskId: string): Promise; /** Everyone opted in to one sync, oldest first. */ export declare function listAppSyncSubscriptions(db: Database, workspaceId: string, taskId: string, limit?: number): Promise; /** One person's opt-ins among these syncs, by sync. */ export declare function getAppSyncSubscriptionsForSubject(db: Database, workspaceId: string, taskIds: readonly string[], subjectId: string): Promise>; /** How many people opted in to each sync. */ export declare function countAppSyncSubscriptions(db: Database, workspaceId: string, taskIds: readonly string[]): Promise>; /** Record one person's run of a personal sync. */ export declare function recordAppSyncSubscriptionRun(db: Database, input: { workspaceId: string; taskId: string; subjectId: string; status: "succeeded" | "failed"; error: string | null; summary: AppSyncRunSummaryType | null; }): Promise; /** The ledger's person for a shared sync's keys. */ export declare const APP_SYNC_SHARED_KEYS = ""; /** * Record that this run pass saw (and wrote) these keys, for one person of a * personal sync or `APP_SYNC_SHARED_KEYS`. `hashes` (same order as `keys`) * are a table sync's fingerprints of the values written. */ export declare function recordAppSyncKeys(db: Database, input: Scope & { taskId: string; subjectId: string; pass: string; keys: readonly string[]; hashes?: readonly (string | null)[]; }): Promise; /** A key this sync wrote earlier, with a table sync's fingerprint of what it wrote. */ export type AppSyncMissingKey = { key: string; hash: string | null; }; /** Keys this sync wrote earlier that the pass didn't see, oldest first. */ export declare function listMissingAppSyncKeys(db: Database, input: { workspaceId: string; taskId: string; subjectId: string; pass: string; limit: number; }): Promise; /** Forget keys whose records were removed. */ export declare function deleteAppSyncKeys(db: Database, input: { workspaceId: string; taskId: string; subjectId: string; keys: readonly string[]; }): Promise; /** Forget every key a sync wrote (it now writes somewhere else); returns how many. */ export declare function clearAppSyncKeys(db: Database, workspaceId: string, taskId: string, subjectId?: string): Promise; /** * For an event run: whose event sources fired it. `workspaceWide` when any * of its triggers is on a workspace source; otherwise `owners` are the * people whose own (private) sources fired it, none when they no longer * exist. Null for any other run. */ export declare function appSyncRunEventSources(db: Database, workspaceId: string, runId: string): Promise<{ workspaceWide: boolean; owners: string[]; } | null>; export {};