import { type BackgroundAgentReapOptions, type BackgroundAgentRecord, type BackgroundAgentRowSummary, type BackgroundAgentStore, type BackgroundAgentUpdateIfExpect } from "../../core/background-agent-store.js";
import { type RekeyConflict, type RekeyDone, type RekeyPlan, type ScopeRekeyInput } from "../../core/identity-rekey.js";
import { type LedgerSpec } from "./shared-ledger.js";
import { type CorruptReadInfo } from "./corrupt-read-sink.js";
/**
 * design/151 S1 — the zero-dependency FILE backend for {@link BackgroundAgentStore} (TOC-local durable
 * `a*` rows: the restart-surviving half of "后台子代详情全程可看"). Byte-for-byte the
 * {@link import("./workflow-run-store.js").FileWorkflowRunStore} mechanics (the design/97 S1b shape):
 *
 *  1. **In-process async mutex per (scope,handle)** — a `Map<key, Promise>` serialization chain, so no
 *     two interleaved async ops both read+commit the same row;
 *  2. **Authoritative state = an in-memory `Map<key, record>`**, rebuilt by replaying `agents.jsonl`
 *     (+ compacted snapshot) at boot, last-writer-wins by event order;
 *  3. **Commit ordering = fsync'd append BEFORE the in-memory flip** — a crash never leaves memory and
 *     disk disagreeing.
 *  4. **A line the replay cannot read refuses the store** — anything but an unfinished last append, which is read as not having
 *     happened: the fold is last-writer-wins, so folding past a damaged line served a settled agent as running, or a reaped one
 *     back (`FileLedgerDamagedError`, `store.ledger_damaged`, in `shared-ledger.ts`). `FileStorageBackend` builds this store, so its
 *     refusal is the backend's too; `inspectFileLedgers(root)` reads the ledger without opening it, and
 *     `setAsideLedgerDamage(root, "background-agents")` is the way out.
 *
 * The CAS/reap predicates are byte-for-byte the InMemory reference implementation's (the contract
 * tests run against both). No per-operation flock — one process owns the directory (the same
 * single-owner posture as every stores/file backend; a multi-process deployment wires pg/tidb), and
 * since 2026-08-22 that posture is ENFORCED rather than assumed: the first instance over the directory
 * takes its cross-process writer fence (`root/background-agents/LOCK`) and a second OS process is
 * refused by name (see `shared-ledger.ts`). A multi-process deployment therefore learns at construction
 * time instead of via a double-claim.
 */
type LedgerEvent = {
    t: "put";
    k: string;
    r: BackgroundAgentRecord;
} | {
    t: "update";
    k: string;
    r: BackgroundAgentRecord;
} | {
    t: "delete";
    k: string;
};
export interface FileBackgroundAgentStoreOptions {
    /** This store's half of {@link import("./shared-ledger.js").LedgerPaths.onCorruptRead} — the same seat,
     *  over the agent row model. */
    onCorruptRead?: (info: CorruptReadInfo) => void;
    /** When false, a ledger append is NOT fsync'd. Default true. */
    fsync?: boolean;
    /** Compact the ledger into a snapshot once it exceeds this many events (then truncate). Default 1000. */
    compactEvery?: number;
}
/** The agent ledger's files under a data root, and its fold ({@link import("./shared-ledger.js").LedgerSpec}). */
export declare const BACKGROUND_AGENT_LEDGER: LedgerSpec<BackgroundAgentRecord, LedgerEvent>;
/** What {@link rekeyBackgroundAgentDirectory} takes before its first step — the directory's authority, its writer fence and replay
 *  included, so a writer in another process or a damaged line refuses here — let go at once, no row written; a root with no ledger
 *  directory is not given one. */
export declare function lookBackgroundAgentDirectory(root: string): void;
/**
 * {@link FileBackgroundAgentStore.rekeyScope} over `<root>/background-agents` with no store built: the data root's adoption runs it
 * while its marker refuses every store constructor. The ledger is reached as
 * {@link import("./checkpoint-store.js").rekeyCheckpointDirectory} reaches the checkpoint ledger, with this store's default tuning.
 */
export declare function rekeyBackgroundAgentDirectory(root: string, input: ScopeRekeyInput): Promise<RekeyPlan | RekeyDone | RekeyConflict>;
export declare class FileBackgroundAgentStore implements BackgroundAgentStore {
    private readonly fsyncEnabled;
    private readonly compactEvery;
    /** RB-55: all mutable state is SHARED per canonical directory (see the note above the class). */
    private readonly ledger;
    /** RB-145: this instance's own close latch — the refcount belongs to the DIRECTORY, not to callers. The agent ledger's {@link HolderLatch}:
     *  {@link close} closes it, as does the dispose of the backend that built it. */
    private readonly latch;
    private readonly disclose;
    private get rows();
    constructor(root: string, options?: FileBackgroundAgentStoreOptions);
    /**
     * RB-60 (2026-07-25, red probe): the composite key must be INJECTIVE — `get`/`update` no longer re-check
     * scope, so tenant isolation rests entirely on this function. A SPACE separator is not injective
     * (`scope="a", handle="b c"` and `scope="a b", handle="c"` produce the same key), which let one tenant's
     * row collide with another's. The in-memory reference implementation (`core/background-agent-store.ts`)
     * uses `\u0000` for exactly this reason ("the composite key keeps the info-hiding property structural") —
     * a NUL can never occur in a scope or handle, so the mapping is injective. Matching it also restores this
     * backend's stated "byte-for-byte the InMemory reference implementation's" claim.
     *
     * MIGRATION: the key is IN-MEMORY only (the ledger stores whole records with their own `scope`/`handle`
     * fields, and replay rebuilds keys through this function), so changing the separator needs no on-disk
     * migration — an existing directory replays into the new key space on next open.
     */
    static key(handle: string, scope: string): string;
    private withLock;
    /** Durable append FIRST, in-memory flip AFTER, then best-effort compaction — all in the base
     *  (RB-150/RB-167: a failing housekeeping compaction must never be reported as this CAS failing). */
    private commit;
    put(record: BackgroundAgentRecord): Promise<void>;
    get(handle: string, scope: string): Promise<BackgroundAgentRecord | null>;
    update(handle: string, scope: string, record: BackgroundAgentRecord, expect: {
        rev: number;
    }): Promise<boolean>;
    updateIf(handle: string, scope: string, record: BackgroundAgentRecord, expect: BackgroundAgentUpdateIfExpect): Promise<boolean>;
    listBySession(scope: string, sessionId: string, opts?: {
        status?: BackgroundAgentRecord["status"];
        limit?: number;
    }): Promise<BackgroundAgentRowSummary[]>;
    listByScope(scope: string, opts?: {
        status?: BackgroundAgentRecord["status"];
        limit?: number;
    }): Promise<BackgroundAgentRowSummary[]>;
    listByRoot(rootSessionId: string, opts?: {
        status?: BackgroundAgentRecord["status"];
        limit?: number;
    }): Promise<BackgroundAgentRowSummary[]>;
    listScopes(): Promise<string[]>;
    delete(handle: string, scope: string, expect?: {
        rev: number;
    }): Promise<boolean>;
    /** The end of a batch of {@link delete} calls (interface: {@link BackgroundAgentStore.settleErasure}): the ledger compacted once. */
    settleErasure(): Promise<void>;
    /** The session's terminal rows, deleted each under its own key lock with the selection re-checked against the live row, then the
     *  ledger compacted ONCE so their earlier events leave the disk (interface: {@link BackgroundAgentStore.deleteBySession}). */
    deleteBySession(scope: string, sessionId: string): Promise<string[]>;
    rekeyScope(input: ScopeRekeyInput & {
        readonly mode: "plan";
    }): Promise<RekeyPlan>;
    rekeyScope(input: ScopeRekeyInput & {
        readonly mode: "arrive";
    }): Promise<RekeyDone | RekeyConflict>;
    rekeyScope(input: ScopeRekeyInput & {
        readonly mode: "leave" | "withdraw";
    }): Promise<RekeyDone>;
    reap(scope: string, now: number, opts?: BackgroundAgentReapOptions): Promise<number>;
    /** Force a compaction now (test/inspection). RB-167's closeForSwap recipe lives in the base. */
    compactNow(): void;
    /** Test/inspection helper: number of stored rows. */
    get size(): number;
    /** Release the append handle (best-effort). RB-55: refcounted — the directory's shared authority
     *  (rows/locks/log) is torn down only when the LAST instance over it closes, so a sibling instance
     *  is never left holding a closed fd or a half-dropped view. */
    close(): void;
}
export {};
