import type { Kysely } from 'kysely';
import type { Database } from '../db/types.js';
import type { StepProgressEntry } from './check-run-summary.js';
/**
 * Composite key identifying a single check-run row.
 *
 * Matches the table primary key `(provider, owner, repo, sha, check_name)`.
 * Used by the L1 in-memory cache and as the parameter shape for every
 * store method.
 */
export interface CheckRunTrackingKey {
    provider: string;
    owner: string;
    repo: string;
    sha: string;
    checkName: string;
}
/**
 * Snapshot of all per-key check-run state. Mirrors the columns of the
 * `check_run_tracking` table with the in-memory shapes the consumer
 * already uses.
 */
export interface CheckRunTrackingState {
    /** GitHub Checks API check-run ID. Undefined when not yet created. */
    checkRunId?: number;
    /** Build check-run lifecycle marker. */
    buildCreationState?: 'pending' | 'completed';
    /** Live step-progress entries shown in the check run's `output.summary`. */
    stepProgress: StepProgressEntry[];
    /** Timestamp the first running-step transition was sent to GitHub. */
    inProgressSentAt?: Date;
    /** KiCI run this check-run belongs to. Used by `cleanupRun`. */
    runId?: string;
    /** Last persisted update time; powers debounce-after-failover recovery. */
    updatedAt?: Date;
}
/**
 * DB persistence for `CheckRunReporter` check-run state.
 *
 * Backed by the `check_run_tracking` table — one row per
 * `(provider, owner, repo, sha, check_name)`. Replaces six in-memory
 * `Map`s previously held inside `CheckRunReporter`:
 *
 *   - `checkRunIds`            → `check_run_id` column
 *   - `pendingBuildCreations`  → `build_creation_state` column
 *   - `stepProgress`           → `step_progress_json` column
 *   - `inProgressSent`         → `in_progress_sent_at` column
 *   - `runIdToKeys`            → indexed `run_id` column + `listKeysByRunId`
 *
 * The `progressTimers` Map is intentionally NOT persisted — debounce
 * timers are reconstructed on demand. After a coord failover the very
 * next `updateStepProgress` either flushes immediately (debounce window
 * elapsed) or starts a fresh timer.
 *
 * The consumer keeps an L1 in-memory cache in front of this store; the
 * store itself is stateless beyond the connection it holds.
 */
export declare class CheckRunTrackingStore {
    private readonly db;
    constructor(db: Kysely<Database>);
    /**
     * Atomically set / overwrite the check-run ID for a key.
     *
     * Performed as an upsert so a re-issued setPending after a coord
     * failover replaces the prior ID rather than silently leaving a row
     * mismatched with the GitHub-side state.
     */
    setCheckRunId(key: CheckRunTrackingKey, checkRunId: number): Promise<void>;
    /**
     * Lookup the check-run ID for a key. Returns undefined if no row exists
     * yet OR the row exists but the GitHub create has not finished
     * persisting an ID (the build-creation in-flight window).
     */
    getCheckRunId(key: CheckRunTrackingKey): Promise<number | undefined>;
    /**
     * Mark a build check-run as having an in-flight create. Returns true if
     * this caller won the race (no prior row, or row had no in-flight state).
     * Used to prevent a replacement coord from re-issuing a `checks.create()`
     * against the same SHA when the original create is still pending.
     */
    markBuildCreationPending(key: CheckRunTrackingKey, runId?: string): Promise<void>;
    /**
     * Mark a build check-run create as complete. Idempotent.
     */
    markBuildCreationComplete(key: CheckRunTrackingKey): Promise<void>;
    /**
     * Replace the step-progress array for a key.
     */
    setStepProgress(key: CheckRunTrackingKey, steps: StepProgressEntry[], runId?: string): Promise<void>;
    /**
     * Mark the first in-progress transition as sent. Used to keep the
     * single "did we already kick this check run into in_progress?" guard
     * cluster-wide.
     */
    markInProgressSent(key: CheckRunTrackingKey, runId?: string): Promise<void>;
    /**
     * Get the full state snapshot for a key. Used by the L1 cache to
     * hydrate on miss and by tests to verify the on-disk layout. Returns
     * undefined when no row exists.
     */
    getState(key: CheckRunTrackingKey): Promise<CheckRunTrackingState | undefined>;
    /**
     * Delete a single row. Returns true if the row existed.
     */
    deleteRow(key: CheckRunTrackingKey): Promise<boolean>;
    /**
     * List every key currently tracked for a runId. Used by `cleanupRun`
     * to reproduce the runId → keys reverse index that the in-memory map
     * provided. Index `idx_check_run_tracking_run_id` keeps this O(matches).
     */
    listKeysByRunId(runId: string): Promise<CheckRunTrackingKey[]>;
    /**
     * Delete every row for a runId. Mirrors the bulk-cleanup semantics of
     * `cleanupRun` so a single call from execution-tracker prune releases
     * all rows for the run.
     */
    deleteByRunId(runId: string): Promise<number>;
    private selectRow;
    private upsertRow;
}
/**
 * Convert a raw DB row to the in-memory `CheckRunTrackingState` shape.
 * Exported for direct use from tests that bypass the store.
 */
export declare function rowToState(row: {
    check_run_id: number | string | null;
    build_creation_state: string | null;
    step_progress_json: unknown;
    in_progress_sent_at: Date | null;
    run_id: string | null;
    updated_at: Date;
}): CheckRunTrackingState;
//# sourceMappingURL=check-run-tracking-store.d.ts.map