import { n as PostgresSqlConnection, t as EngineConnection } from "./sql-connection-DiMIHOpE.js";
import "./gateway-lock-payload-CHvO7eLi.js";
import { DatabaseSync } from "node:sqlite";
//#region src/infra/sqlite-worker-identity.d.ts
type DatabaseFileIdentity = Readonly<{
  key: string;
  birthtime?: string;
}>;
type DatabasePathIdentity = DatabaseFileIdentity & Readonly<{
  canonicalPath: string;
}>;
//#endregion
//#region src/infra/sqlite-clean-close-seal.d.ts
type SqliteCleanCloseSealRefusal = "no-proof" | "malformed-proof" | "expired-proof" | "file-mismatch" | "journal-present" | "identity-mismatch" | "feature-mismatch" | "schema-mismatch" | "policy-mismatch";
//#endregion
//#region src/infra/sqlite-database-admission-record.d.ts
declare const decodedValue: unique symbol;
type AdmissionFactValue = {
  value: unknown;
  [decodedValue]?: {
    value: unknown;
  };
};
type AdmissionFact = AdmissionFactValue & {
  revision: number;
  schemaDependent: boolean;
  lifetime?: "process";
  publication: string;
  current: SharedArrayBuffer;
};
type ScopedWriteRevision = {
  identity: string;
  state: Int32Array<SharedArrayBuffer>;
};
type Admission = {
  identity: string;
  physicalIdentity: DatabaseFileIdentity;
  location: string;
  descriptor: number;
  descriptorOwner: number;
  generationId: string;
  generation: SharedArrayBuffer;
  /** First-load diagnostic, shared with workers without rereading the seal. */
  cleanCloseRefusal?: SqliteCleanCloseSealRefusal;
  /** Last complete host snapshot; relays preserve this value without advancing it. */
  hostRevision?: number;
  writeScopes: Map<string, ScopedWriteRevision>;
  writeScopeRevision: number;
  /** Each worker shares its handle count and unsettled native-write count. */
  references: Map<number, SharedArrayBuffer>;
  facts: Map<string, AdmissionFact>;
};
type AdmissionSnapshot = Omit<Admission, "writeScopes"> & {
  writeScopes: WriteScopeBuckets;
};
type SqliteDatabaseAdmissions = AdmissionSnapshot[];
type SqliteDatabaseAdmissionKey<T> = {
  name: string;
  read(this: void, value: unknown): T | undefined;
  schemaDependent?: boolean;
  /** Physical validation and owner-published decisions survive ordinary native close. */
  lifetime?: "process";
  /** Physical cache facts can be recaptured locally; their transport never gates a live connection. */
  writer?: "host" | "connection";
};
type SqliteDatabaseAdmissionLocation = {
  identity: string;
  canonicalPath: string;
};
/** Internal typed publications need no decoder; public/custom keys retain their read contract. */
type SqliteDatabaseFactKey<T> = Omit<SqliteDatabaseAdmissionKey<T>, "read"> & {
  read?: SqliteDatabaseAdmissionKey<T>["read"];
};
declare const EMPTY_WRITE_SCOPE_BUCKETS: Map<string, ScopedWriteRevision>[];
type WriteScopeBuckets = typeof EMPTY_WRITE_SCOPE_BUCKETS;
//#endregion
//#region src/infra/sqlite-database-admission.d.ts
declare const { prepareSqliteDatabaseAdmissionAsync, prepareSqliteDatabaseAdmission, retainSqliteDatabaseAdmissionLocation }: {
  prepareSqliteDatabaseAdmissionAsync: (params: {
    location: string;
    assertCurrent: () => void;
    identity?: DatabasePathIdentity;
    expectedIdentity?: DatabaseFileIdentity;
    observedLocation?: SqliteDatabaseAdmissionLocation;
    create?: {
      authorize(canonicalPath: string): void | Promise<void>;
      retain(completion: Promise<void>): void;
    };
  }) => Promise<DatabasePathIdentity | undefined>;
  retainSqliteDatabaseAdmissionLocation: (location: string, expectedIdentity?: DatabaseFileIdentity, observedLocation?: SqliteDatabaseAdmissionLocation) => void;
  prepareSqliteDatabaseAdmission: (location: string, options?: {
    create?: boolean;
  }) => string | undefined;
  pathAdmission: (location: string, observedIdentity?: string) => Admission | undefined;
};
declare const { getSqliteDatabaseAdmission, getSqliteDatabaseSchemaRevision, getOrLoadSqliteDatabaseAdmissionForPath, peekSqliteDatabaseAdmissionForPath, prepareSqliteDatabaseAdmissionForPath, hasSqliteDatabaseSchemaAdmissionForPath, getSqliteDatabaseAdmissionIdentityForPath, hasSqliteDatabaseSchemaAdmissionForIdentity }: {
  getSqliteDatabaseAdmission: <T>(database: DatabaseSync | PostgresSqlConnection, key: SqliteDatabaseFactKey<T>, options?: {
    existingOnly?: boolean;
    refresh?: boolean;
  }) => T | undefined;
  getSqliteDatabaseSchemaRevision: (connection: EngineConnection, options?: {
    existingOnly?: boolean;
  }) => number | undefined;
  getOrLoadSqliteDatabaseAdmissionForPath: <T>(location: string, key: SqliteDatabaseFactKey<T>, load: () => T | undefined) => T | undefined;
  peekSqliteDatabaseAdmissionForPath: <T>(location: string, key: SqliteDatabaseFactKey<T>) => T | undefined;
  prepareSqliteDatabaseAdmissionForPath: <T>(location: string, key: SqliteDatabaseFactKey<T>) => () => T | undefined;
  hasSqliteDatabaseSchemaAdmissionForPath: (location: string) => boolean;
  getSqliteDatabaseAdmissionIdentityForPath: (location: string) => DatabaseFileIdentity | undefined;
  hasSqliteDatabaseSchemaAdmissionForIdentity: (physicalIdentity: DatabaseFileIdentity) => boolean;
};
declare const { onSqliteDatabaseWriteSettled, retireSqliteDatabaseWriteScopes, retainSqliteDatabaseWriteScopesForPath, withSqliteDatabaseWriteScope, withoutSqliteDatabaseWriteScope, readSqliteDatabaseScopedWriteToken, readSqliteDatabaseScopedWriteTokenForPath, readSqliteDatabaseScopedWriteTokensForPath, readSqliteDatabasePendingScopedWriteToken, readSqliteDatabaseWriteTokenForPath, readSqliteDatabasePendingWriteToken }: {
  cancel(database: DatabaseSync): void;
  begin(database: DatabaseSync, unscoped: boolean): void;
  finish(database: DatabaseSync, record: Admission | undefined): void;
  settleWorker: (record: Admission, worker: number) => void;
  onSqliteDatabaseWriteSettled: (connection: EngineConnection, listener: () => void) => (() => void) | undefined;
  retireSqliteDatabaseWriteScopes: (connection: EngineConnection, scope: readonly (string | {
    sessionId: string;
    partition?: string;
  })[]) => void;
  retainSqliteDatabaseWriteScopesForPath: (location: string, scope: readonly (string | {
    sessionId: string;
    partition?: string;
  })[]) => (() => void) | undefined;
  withSqliteDatabaseWriteScope: <T>(connection: EngineConnection, scope: readonly (string | {
    sessionId: string;
    partition?: string;
  })[], run: () => T) => T;
  withoutSqliteDatabaseWriteScope: <T>(connection: EngineConnection, run: () => T) => T;
  readSqliteDatabaseScopedWriteToken: (connection: EngineConnection, scope: string | readonly (string | {
    sessionId: string;
    partition?: string;
  })[]) => string | undefined;
  readSqliteDatabaseScopedWriteTokenForPath: (location: string, scope: string | readonly (string | {
    sessionId: string;
    partition?: string;
  })[]) => string | undefined;
  readSqliteDatabaseScopedWriteTokensForPath: (location: string, scopes: readonly (string | readonly (string | {
    sessionId: string;
    partition?: string;
  })[])[]) => string[] | undefined;
  readSqliteDatabasePendingScopedWriteToken: (connection: EngineConnection, scope: string | readonly (string | {
    sessionId: string;
    partition?: string;
  })[]) => string | undefined;
  readSqliteDatabaseWriteTokenForPath: (location: string) => string | undefined;
  readSqliteDatabasePendingWriteToken: (connection: EngineConnection) => string | undefined;
};
//#endregion
//#region src/state/openclaw-state-maintenance-context.d.ts
type MaintenanceResource = {
  phase: "agent-resources" | "agent-handles" | "shared-leases" | "shared-resources" | "shared-references" | "shared-handles";
  close: () => void | Promise<void>;
};
type AgentSchemaMigration = {
  agentId: string;
  path: string;
  foundVersion: number;
  supportedVersion: number;
};
type OpenClawDatabaseMaintenanceScope = {
  readonly ownsSchemaMaintenance: boolean;
  /** SQLite admission coverage for this offline Doctor operation; absent at runtime. */
  readonly sqliteIntegrityCheck?: "quick" | "full";
  assertOwnerCurrent(this: void, access?: "read"): void;
  assertDatabaseAccess(this: void, databasePath: string): void;
  assertCleanupDatabaseAccess(this: void, databasePath: string): void;
  assertAdmission(this: void): void;
  assertReadAdmission(this: void): void;
  addAgentSchemaMigrationCheck(check: (migration: AgentSchemaMigration) => void): void;
  assertAgentSchemaMigration(migration: AgentSchemaMigration): void;
  run<T>(operation: () => T): T;
  track<T>(operation: Promise<T>): Promise<T>;
  own(resource: object, phase: MaintenanceResource["phase"], close: MaintenanceResource["close"]): void;
  close(beforeResources?: () => void | Promise<void>): Promise<void>;
};
//#endregion
//#region src/state/openclaw-state-db-async-lifecycle.d.ts
type OpenClawStateDatabaseReadAdmission = {
  readonly databasePath: string;
  /** Stable across first creation and aliases; coordinates work but grants no authority. */
  readonly coordinationKey: string;
  readonly identity: DatabasePathIdentity;
  captureIntegrity?: () => OpenClawStateIntegrityAdmission;
  assertCurrent: () => void;
};
type OpenClawStateIntegrityAdmission = {
  identity: DatabasePathIdentity;
  /** Physical-owner lifetime survives renewable schema and integrity proof. */
  readonly lifetime: {
    readonly revision: SharedArrayBuffer;
    readonly epoch: bigint;
  };
  readonly revision: SharedArrayBuffer;
  readonly epoch: bigint;
  readonly proof: SharedArrayBuffer;
};
//#endregion
//#region src/infra/gateway-state-owner.d.ts
type StateDatabaseSchemaLease = {
  readonly path: string;
  assertCurrent(this: void): void;
  assertDatabaseAccess(this: void, databasePath: string): void;
  run<T>(this: void, operation: () => T): T;
  release(this: void): void;
};
type PreparedStateDatabaseReadAccess = {
  assertReadAllowed(captured?: Parameters<typeof assertStateDatabaseAccessAllowed>[1]): void;
};
/** Ordinary SQLite access observes maintenance; it never borrows schema authority. */
declare function assertStateDatabaseAccessAllowed(databasePath: string, captured?: {
  maintenanceScope?: OpenClawDatabaseMaintenanceScope;
  schemaLease?: Pick<StateDatabaseSchemaLease, "assertDatabaseAccess">;
}): void;
//#endregion
export { readSqliteDatabaseScopedWriteToken as a, DatabasePathIdentity as c, OpenClawDatabaseMaintenanceScope as i, OpenClawStateDatabaseReadAdmission as n, SqliteDatabaseAdmissions as o, OpenClawStateIntegrityAdmission as r, DatabaseFileIdentity as s, PreparedStateDatabaseReadAccess as t };