UNPKG

@adonis-agora/agent

Version:

Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.

1,053 lines • 55.3 kB
import { ensureDatabaseSchema, qualifiedName, qualifiedSql, validateSchema } from './db-schema.js';
import { dialectOf, isMySql, portableSql } from './sql-dialect.js';
/**
 * The twelve agent table names. They match the cross-adapter snake_case contract the reference Drizzle
 * store uses, so a dashboard or migration can point at any adapter and see the same physical schema.
 */
export const AGENT_TABLES = {
    /** Independent action proposals, including durable execution work and its lease. */
    actionProposals: 'agent_action_proposal',
    threads: 'agent_thread',
    messages: 'agent_message',
    toolCalls: 'agent_tool_call',
    tokenUsage: 'agent_token_usage',
    modelPricing: 'agent_model_pricing',
    runs: 'agent_run',
    /** Messages sent while a turn was running, waiting to run after it (the chat queue). */
    queuedMessages: 'agent_queued_message',
    /** Confirm tokens already spent, by hash (`LucidConfirmTokenStore`, single-use confirmed writes). */
    confirmTokens: 'agent_confirm_token',
    /** A run's live stream, buffered for replay by the Lucid token sink (`tokenSinks.lucid()`). */
    streamFrames: 'agent_stream_frame',
    /**
     * Text channels' short-lived state (`lucidChannelStore`): the provider message ids already taken,
     * questions waiting for an answer, outcomes already relayed — each row expiring at `expires_at`.
     */
    channelState: 'agent_channel_state',
    /**
     * Generative-UI telemetry: one row per UI tool call (`recordGenuiCalls`) — metadata only, the
     * call's input only when its tool samples payloads. Read by the dashboard's Generative UI page.
     */
    genuiCalls: 'agent_genui_call',
};
/**
 * The agent tables as Lucid's query builder takes them under `schema`: `schema.agent_thread` (knex
 * quotes each part), or the bare names without a schema — then exactly {@link AGENT_TABLES}.
 */
export function agentTables(schema) {
    if (schema === undefined)
        return AGENT_TABLES;
    return Object.fromEntries(Object.entries(AGENT_TABLES).map(([key, table]) => [key, qualifiedName(table, schema)]));
}
/**
 * The agent tables as raw SQL names them under `schema`: `"schema"."agent_thread"`, or
 * `"agent_thread"` without a schema. Double-quoted like every identifier of these statements.
 */
export function agentTablesSql(schema) {
    return Object.fromEntries(Object.entries(AGENT_TABLES).map(([key, table]) => [key, qualifiedSql(table, schema)]));
}
/**
 * One statement of this module, as `db`'s dialect has to read it. The statements are written once,
 * Postgres/SQLite-style; MySQL needs four things changed, and every one of them used to fail or
 * silently misbehave there:
 *
 * - identifiers in backticks — a `"quoted"` name is a string literal to MySQL, so every statement
 *   failed to parse;
 * - `TEXT` as `LONGTEXT` — MySQL's `TEXT` holds 64 KB, and a long answer, a tool's output or a stream
 *   frame past that failed the insert;
 * - no `IF NOT EXISTS` on `CREATE INDEX`, which MySQL does not have (the catalog is read first, see
 *   {@link existingIndexes});
 * - InnoDB with a case-sensitive collation: the foreign keys need InnoDB, and under MySQL's default
 *   `utf8mb4_0900_ai_ci` an actor `alice` read the threads of an actor `ALICE`. Postgres and SQLite
 *   compare those exactly; `utf8mb4_bin` makes MySQL do the same.
 */
export function forDialect(statement, mysql) {
    if (!mysql)
        return statement;
    let out = statement.replaceAll('"', '`').replace(/\bTEXT\b/g, 'LONGTEXT');
    out = out.replace(/^CREATE INDEX IF NOT EXISTS /, 'CREATE INDEX ');
    if (/^CREATE TABLE /.test(out)) {
        out = `${out} ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin`;
    }
    return out;
}
/**
 * `CREATE TABLE IF NOT EXISTS` for the Lucid token sink's frame buffer, under `table` (the sink takes
 * a `tableName`). One row per frame of a run's stream, numbered from 1 in write order; the row whose
 * `frame` is `NULL` is the end marker. `created_at` is what the sink's TTL purge reads. No foreign
 * key: a sink is usable with any store, and a delegated run writes under its ANCESTOR's run id.
 */
export function streamFrameTableStatement(table = AGENT_TABLES.streamFrames, schema) {
    return `CREATE TABLE IF NOT EXISTS ${qualifiedSql(table, schema)} (
      "run_id" VARCHAR(255) NOT NULL,
      "seq" BIGINT NOT NULL,
      "frame" TEXT NULL,
      "created_at" BIGINT NOT NULL,
      PRIMARY KEY ("run_id", "seq")
    )`;
}
/**
 * The frame buffer's `created_at` index, under `table`: what the sink's TTL purge reads to find the
 * runs with rows past their window without scanning every row. `run_id` rides along so that lookup
 * never touches the table itself.
 */
export function streamFrameIndexStatement(table = AGENT_TABLES.streamFrames, schema) {
    return `CREATE INDEX IF NOT EXISTS "${table}_created_idx" ON ${qualifiedSql(table, schema)} ("created_at", "run_id")`;
}
/**
 * `CREATE TABLE IF NOT EXISTS` for text channels' state (`lucidChannelStore`), under `table`. The
 * primary key IS the dedupe lock: two deliveries of one message race on the insert and one loses.
 * No foreign key: the rows name provider ids, not agent rows.
 */
export function channelStateTableStatements(table = AGENT_TABLES.channelState, schema) {
    const ref = qualifiedSql(table, schema);
    return [
        `CREATE TABLE IF NOT EXISTS ${ref} (
      "key" VARCHAR(255) PRIMARY KEY NOT NULL,
      "value" TEXT NULL,
      "expires_at" BIGINT NOT NULL,
      "created_at" BIGINT NOT NULL
    )`,
        `CREATE INDEX IF NOT EXISTS "${table}_expires_idx" ON ${ref} ("expires_at")`,
    ];
}
/**
 * The usage ledger, `agent_token_usage`: one row per billed call — every model call an agent turn
 * makes, and whatever an app records itself through `AgentService.recordUsage` (transcription, TTS,
 * batch embeddings, a direct SDK call). `thread_id` is NULL on a row recorded outside any
 * conversation; the foreign key still cascades a deleted thread's rows away. `feature` says what the
 * spend was for, `audio_seconds` / `characters` / `requests` hold the units a row bills by when they
 * are not tokens, and `metadata` is the caller's own JSON.
 *
 * Its own function so the SQLite rebuild that makes an old ledger's `thread_id` nullable (see
 * {@link createAgentTables}) creates exactly this shape under a scratch name.
 */
export function tokenUsageTableStatement(table = AGENT_TABLES.tokenUsage, schema) {
    return `CREATE TABLE IF NOT EXISTS ${qualifiedSql(table, schema)} (
      "id" VARCHAR(255) PRIMARY KEY NOT NULL,
      "thread_id" VARCHAR(255) NULL,
      "actor_ref" VARCHAR(255) NOT NULL,
      "message_id" VARCHAR(255) NULL,
      "model_id" VARCHAR(255) NOT NULL,
      "purpose" VARCHAR(255) NOT NULL,
      "input_tokens" INTEGER NOT NULL,
      "output_tokens" INTEGER NOT NULL,
      "cache_write_tokens" INTEGER NULL,
      "cache_read_tokens" INTEGER NULL,
      "cost_usd" DOUBLE PRECISION NULL,
      "cost_source" VARCHAR(16) NULL,
      "run_id" VARCHAR(255) NULL,
      "created_at" BIGINT NOT NULL,
      "feature" VARCHAR(255) NULL,
      "provider" VARCHAR(255) NULL,
      "tenant_ref" VARCHAR(255) NULL,
      "audio_seconds" DOUBLE PRECISION NULL,
      "characters" BIGINT NULL,
      "requests" INTEGER NULL,
      "metadata" TEXT NULL,
      "counts_towards_quota" INTEGER NOT NULL DEFAULT 1,
      FOREIGN KEY ("thread_id") REFERENCES ${qualifiedSql(AGENT_TABLES.threads, schema)} ("id") ON DELETE CASCADE
    )`;
}
/**
 * `CREATE TABLE IF NOT EXISTS` DDL for the twelve agent tables plus their indexes, one statement per
 * array element so each can be issued through Lucid's `rawQuery`. Portable across SQLite / Postgres /
 * MySQL: quoted identifiers, epoch-ms `BIGINT` timestamps, `INTEGER` booleans (0/1) and `TEXT` JSON
 * columns — no dialect-only types. A real deployment should prefer the bundled migration stub so the
 * schema is versioned; this helper lets a store stand itself up in tests and scripts.
 *
 * A tool call is addressed by the model-supplied `toolCallId`, kept as-is in `call_id`. The row's PK
 * is that id scoped to its assistant message (see `toolCallRowId` in `lucid.ts`): a provider only
 * promises the id is unique within one response, and some (short `call_<digits>` ids behind
 * OpenRouter) repeat one across runs. A row from before `call_id` existed has it NULL and its PK is
 * the bare model id.
 *
 * The `agent_run` table records each run (turn) lifecycle; `agent_message` / `agent_tool_call` /
 * `agent_token_usage` each carry a nullable `run_id` correlation column (logically referencing
 * `agent_run.id`, deliberately WITHOUT a DB-level foreign key — like the reference — so the additive
 * migration can `ALTER TABLE ADD COLUMN` portably and a row recorded before run tracking shipped can
 * keep a `null` run_id).
 */
export function createTableStatements(schema) {
    const t = AGENT_TABLES;
    const q = agentTablesSql(schema);
    return [
        `CREATE TABLE IF NOT EXISTS ${q.actionProposals} (
      "id" VARCHAR(64) PRIMARY KEY NOT NULL,
      "scope_key" VARCHAR(64) NOT NULL,
      "logical_sort" VARCHAR(1020) NOT NULL,
      "insert_token" VARCHAR(64) NOT NULL,
      "decision" VARCHAR(32) NOT NULL,
      "execution_status" VARCHAR(32) NULL,
      "lease_expires_at" BIGINT NULL,
      "proposal_expires_at" BIGINT NULL,
      "discovery_index_version" INTEGER NOT NULL DEFAULT 0,
      "replacement_group_key" VARCHAR(64) NULL,
      "outcome_key" VARCHAR(64) NULL,
      "delivery_status" VARCHAR(32) NULL,
      "delivery_lease_expires_at" BIGINT NULL,
      "payload" TEXT NOT NULL,
      "version" BIGINT NOT NULL DEFAULT 0,
      "created_at" BIGINT NOT NULL,
      "updated_at" BIGINT NOT NULL
    )`,
        `CREATE INDEX IF NOT EXISTS "${t.actionProposals}_work_idx" ON ${q.actionProposals} ("discovery_index_version", "decision", "execution_status", "lease_expires_at", "created_at")`,
        `CREATE INDEX IF NOT EXISTS "${t.actionProposals}_expiry_idx" ON ${q.actionProposals} ("discovery_index_version", "decision", "proposal_expires_at", "created_at")`,
        `CREATE INDEX IF NOT EXISTS "${t.actionProposals}_backfill_idx" ON ${q.actionProposals} ("discovery_index_version", "created_at")`,
        `CREATE INDEX IF NOT EXISTS "${t.actionProposals}_replacement_idx" ON ${q.actionProposals} ("scope_key", "replacement_group_key", "decision")`,
        `CREATE INDEX IF NOT EXISTS "${t.actionProposals}_delivery_idx" ON ${q.actionProposals} ("delivery_status", "delivery_lease_expires_at", "created_at")`,
        `CREATE INDEX IF NOT EXISTS "${t.actionProposals}_outcome_idx" ON ${q.actionProposals} ("outcome_key")`,
        `CREATE INDEX IF NOT EXISTS "${t.actionProposals}_scope_idx" ON ${q.actionProposals} ("scope_key", "created_at")`,
        `CREATE TABLE IF NOT EXISTS ${q.threads} (
      "id" VARCHAR(255) PRIMARY KEY NOT NULL,
      "actor_ref" VARCHAR(255) NOT NULL,
      "tenant_ref" VARCHAR(255) NULL,
      "title" TEXT NOT NULL,
      "persona" VARCHAR(255) NOT NULL DEFAULT 'default',
      "transient" INTEGER NOT NULL DEFAULT 0,
      "pinned_at" BIGINT NULL,
      "summary" TEXT NULL,
      "summary_message_count" INTEGER NOT NULL DEFAULT 0,
      "active_stream_id" VARCHAR(255) NULL,
      "model" VARCHAR(255) NULL,
      "queue_pause" TEXT NULL,
      "default_agent" VARCHAR(255) NULL,
      "created_at" BIGINT NOT NULL,
      "updated_at" BIGINT NOT NULL,
      "deleted_at" BIGINT NULL
    )`,
        `CREATE INDEX IF NOT EXISTS "${t.threads}_actor_updated_idx" ON ${q.threads} ("actor_ref", "updated_at")`,
        `CREATE TABLE IF NOT EXISTS ${q.messages} (
      "id" VARCHAR(255) PRIMARY KEY NOT NULL,
      "thread_id" VARCHAR(255) NOT NULL,
      "role" VARCHAR(255) NOT NULL,
      "content" TEXT NOT NULL,
      "tool_calls" TEXT NULL,
      "tool_results" TEXT NULL,
      "attachments" TEXT NULL,
      "follow_ups" TEXT NULL,
      "usage" TEXT NULL,
      "persona" VARCHAR(255) NULL,
      "run_id" VARCHAR(255) NULL,
      "reasoning" TEXT NULL,
      "reasoning_ms" INTEGER NULL,
      "ui" TEXT NULL,
      "action_proposal_outcome" TEXT NULL,
      "feedback" TEXT NULL,
      "agent_name" VARCHAR(255) NULL,
      "seq" BIGINT NOT NULL DEFAULT 0,
      "sent_at" BIGINT NULL,
      "created_at" BIGINT NOT NULL,
      FOREIGN KEY ("thread_id") REFERENCES ${q.threads} ("id") ON DELETE CASCADE
    )`,
        `CREATE INDEX IF NOT EXISTS "${t.messages}_thread_created_idx" ON ${q.messages} ("thread_id", "created_at")`,
        // A transcript is read in append order (`seq`), which the index above cannot serve.
        `CREATE INDEX IF NOT EXISTS "${t.messages}_thread_seq_idx" ON ${q.messages} ("thread_id", "seq")`,
        `CREATE INDEX IF NOT EXISTS "${t.messages}_run_idx" ON ${q.messages} ("run_id")`,
        `CREATE TABLE IF NOT EXISTS ${q.toolCalls} (
      "id" VARCHAR(255) PRIMARY KEY NOT NULL,
      "message_id" VARCHAR(255) NOT NULL,
      "tool_name" VARCHAR(255) NOT NULL,
      "tool_type" VARCHAR(255) NOT NULL,
      "proposal_id" VARCHAR(255) NULL,
      "input" TEXT NULL,
      "output" TEXT NULL,
      "status" VARCHAR(255) NOT NULL,
      "executed_by_ref" VARCHAR(255) NULL,
      "execution_ms" INTEGER NULL,
      "error" TEXT NULL,
      "run_id" VARCHAR(255) NULL,
      "created_at" BIGINT NOT NULL,
      "executed_at" BIGINT NULL,
      "approver" VARCHAR(255) NULL,
      "confirmation" TEXT NULL,
      "expires_at" BIGINT NULL,
      "remember" INTEGER NULL,
      "decided_via" VARCHAR(64) NULL,
      "call_id" VARCHAR(255) NULL,
      FOREIGN KEY ("message_id") REFERENCES ${q.messages} ("id") ON DELETE CASCADE
    )`,
        `CREATE INDEX IF NOT EXISTS "${t.toolCalls}_run_idx" ON ${q.toolCalls} ("run_id")`,
        `CREATE INDEX IF NOT EXISTS "${t.toolCalls}_call_idx" ON ${q.toolCalls} ("call_id", "run_id")`,
        `CREATE INDEX IF NOT EXISTS "${t.toolCalls}_status_created_idx" ON ${q.toolCalls} ("status", "created_at")`,
        `CREATE INDEX IF NOT EXISTS "${t.toolCalls}_message_idx" ON ${q.toolCalls} ("message_id")`,
        tokenUsageTableStatement(t.tokenUsage, schema),
        `CREATE INDEX IF NOT EXISTS "${t.tokenUsage}_actor_created_idx" ON ${q.tokenUsage} ("actor_ref", "created_at")`,
        `CREATE INDEX IF NOT EXISTS "${t.tokenUsage}_run_idx" ON ${q.tokenUsage} ("run_id")`,
        `CREATE INDEX IF NOT EXISTS "${t.tokenUsage}_thread_idx" ON ${q.tokenUsage} ("thread_id")`,
        `CREATE INDEX IF NOT EXISTS "${t.tokenUsage}_created_idx" ON ${q.tokenUsage} ("created_at")`,
        `CREATE INDEX IF NOT EXISTS "${t.tokenUsage}_feature_created_idx" ON ${q.tokenUsage} ("feature", "created_at")`,
        `CREATE INDEX IF NOT EXISTS "${t.tokenUsage}_tenant_created_idx" ON ${q.tokenUsage} ("tenant_ref", "created_at")`,
        `CREATE TABLE IF NOT EXISTS ${q.modelPricing} (
      "id" VARCHAR(255) PRIMARY KEY NOT NULL,
      "model_id" VARCHAR(255) NOT NULL,
      "input_price_per_1m" DOUBLE PRECISION NOT NULL,
      "output_price_per_1m" DOUBLE PRECISION NOT NULL,
      "cache_write_price_per_1m" DOUBLE PRECISION NULL,
      "cache_read_price_per_1m" DOUBLE PRECISION NULL,
      "effective_from" BIGINT NOT NULL,
      "is_current" INTEGER NOT NULL,
      "created_at" BIGINT NULL,
      "updated_at" BIGINT NULL
    )`,
        `CREATE TABLE IF NOT EXISTS ${q.runs} (
      "id" VARCHAR(255) PRIMARY KEY NOT NULL,
      "thread_id" VARCHAR(255) NOT NULL,
      "agent_name" VARCHAR(255) NULL,
      "parent_run_id" VARCHAR(255) NULL,
      "actor_ref" VARCHAR(255) NOT NULL,
      "tenant_ref" VARCHAR(255) NULL,
      "status" VARCHAR(255) NOT NULL,
      "started_at" BIGINT NOT NULL,
      "finished_at" BIGINT NULL,
      "step_count" INTEGER NOT NULL DEFAULT 0,
      "input_tokens" INTEGER NOT NULL DEFAULT 0,
      "output_tokens" INTEGER NOT NULL DEFAULT 0,
      "cost_usd" DOUBLE PRECISION NULL,
      "error" TEXT NULL,
      "durable" INTEGER NOT NULL DEFAULT 0,
      "ttft_ms" INTEGER NULL,
      "error_code" VARCHAR(64) NULL,
      "model_id" VARCHAR(255) NULL,
      "queue_ms" INTEGER NULL,
      "model_ms" INTEGER NULL,
      "tools_ms" INTEGER NULL,
      "model_calls" INTEGER NULL,
      "tool_calls" INTEGER NULL,
      "timings" TEXT NULL,
      FOREIGN KEY ("thread_id") REFERENCES ${q.threads} ("id") ON DELETE CASCADE
    )`,
        `CREATE INDEX IF NOT EXISTS "${t.runs}_started_idx" ON ${q.runs} ("started_at")`,
        `CREATE INDEX IF NOT EXISTS "${t.runs}_actor_started_idx" ON ${q.runs} ("actor_ref", "started_at")`,
        `CREATE INDEX IF NOT EXISTS "${t.runs}_status_started_idx" ON ${q.runs} ("status", "started_at")`,
        `CREATE INDEX IF NOT EXISTS "${t.runs}_thread_started_idx" ON ${q.runs} ("thread_id", "started_at")`,
        `CREATE TABLE IF NOT EXISTS ${q.queuedMessages} (
      "id" VARCHAR(255) PRIMARY KEY NOT NULL,
      "thread_id" VARCHAR(255) NOT NULL,
      "actor" TEXT NOT NULL,
      "content" TEXT NOT NULL,
      "attachments" TEXT NULL,
      "agent_name" VARCHAR(255) NULL,
      "persona" VARCHAR(255) NULL,
      "model" VARCHAR(255) NULL,
      "page_context" TEXT NULL,
      "ui_capabilities" TEXT NULL,
      "host_context" TEXT NULL,
      "sent_at" BIGINT NULL,
      "interrupt" INTEGER NOT NULL DEFAULT 0,
      "position" INTEGER NOT NULL,
      "created_at" BIGINT NOT NULL,
      "updated_at" BIGINT NOT NULL,
      FOREIGN KEY ("thread_id") REFERENCES ${q.threads} ("id") ON DELETE CASCADE
    )`,
        `CREATE INDEX IF NOT EXISTS "${t.queuedMessages}_thread_position_idx" ON ${q.queuedMessages} ("thread_id", "position")`,
        // The primary key IS the lock: two confirmations of one token race on the insert and one loses.
        `CREATE TABLE IF NOT EXISTS ${q.confirmTokens} (
      "hash" VARCHAR(64) PRIMARY KEY NOT NULL,
      "actor_ref" VARCHAR(255) NOT NULL,
      "tool" VARCHAR(255) NOT NULL,
      "expires_at" BIGINT NOT NULL,
      "created_at" BIGINT NOT NULL
    )`,
        `CREATE INDEX IF NOT EXISTS "${t.confirmTokens}_expires_idx" ON ${q.confirmTokens} ("expires_at")`,
        streamFrameTableStatement(t.streamFrames, schema),
        streamFrameIndexStatement(t.streamFrames, schema),
        ...channelStateTableStatements(t.channelState, schema),
        `CREATE TABLE IF NOT EXISTS ${q.genuiCalls} (
      "id" VARCHAR(255) PRIMARY KEY NOT NULL,
      "run_id" VARCHAR(255) NOT NULL,
      "thread_id" VARCHAR(255) NOT NULL,
      "actor_ref" VARCHAR(255) NOT NULL,
      "agent_name" VARCHAR(255) NULL,
      "step" INTEGER NOT NULL,
      "tool_name" VARCHAR(255) NOT NULL,
      "channel" VARCHAR(64) NOT NULL,
      "mode" VARCHAR(32) NOT NULL,
      "model_id" VARCHAR(255) NOT NULL,
      "attempt" INTEGER NOT NULL,
      "outcome" VARCHAR(16) NOT NULL,
      "validity" VARCHAR(16) NOT NULL,
      "error_category" VARCHAR(32) NULL,
      "error_message" TEXT NULL,
      "input_tokens" INTEGER NOT NULL,
      "output_tokens" INTEGER NOT NULL,
      "cost_usd" DOUBLE PRECISION NULL,
      "cost_source" VARCHAR(16) NULL,
      "first_frame_ms" BIGINT NULL,
      "complete_frame_ms" BIGINT NULL,
      "payload_bytes" INTEGER NOT NULL,
      "payload" TEXT NULL,
      "created_at" BIGINT NOT NULL,
      FOREIGN KEY ("thread_id") REFERENCES ${q.threads} ("id") ON DELETE CASCADE
    )`,
        `CREATE INDEX IF NOT EXISTS "${t.genuiCalls}_created_idx" ON ${q.genuiCalls} ("created_at")`,
        `CREATE INDEX IF NOT EXISTS "${t.genuiCalls}_run_idx" ON ${q.genuiCalls} ("run_id")`,
    ];
}
/**
 * The `run_id` correlation columns run tracking added to three pre-existing tables. They are inline in
 * {@link createTableStatements}, which is all a fresh database needs — but `CREATE TABLE IF NOT EXISTS`
 * cannot add a column to a table that already exists, so a database provisioned before run tracking
 * shipped needs them ALTERed in. Deliberately not DB foreign keys (SQLite cannot add a column WITH a
 * FK constraint), so a row written before run tracking keeps a `null` run_id.
 */
const RUN_ID_COLUMNS = [
    AGENT_TABLES.messages,
    AGENT_TABLES.toolCalls,
    AGENT_TABLES.tokenUsage,
];
/**
 * Columns added to a table that already exists in a deployed database, so `CREATE TABLE IF NOT
 * EXISTS` can never reach them. Repaired the same way the `run_id` columns are, and for the same
 * reason: the store writes them on every turn.
 */
const ADDITIVE_COLUMNS = [
    { table: AGENT_TABLES.queuedMessages, column: 'ui_capabilities', type: 'TEXT NULL' },
    { table: AGENT_TABLES.queuedMessages, column: 'host_context', type: 'TEXT NULL' },
    { table: AGENT_TABLES.toolCalls, column: 'proposal_id', type: 'VARCHAR(255) NULL' },
    {
        table: AGENT_TABLES.actionProposals,
        column: 'replacement_group_key',
        type: 'VARCHAR(64) NULL',
    },
    { table: AGENT_TABLES.actionProposals, column: 'outcome_key', type: 'VARCHAR(64) NULL' },
    { table: AGENT_TABLES.actionProposals, column: 'delivery_status', type: 'VARCHAR(32) NULL' },
    { table: AGENT_TABLES.actionProposals, column: 'delivery_lease_expires_at', type: 'BIGINT NULL' },
    { table: AGENT_TABLES.messages, column: 'action_proposal_outcome', type: 'TEXT NULL' },
    { table: AGENT_TABLES.actionProposals, column: 'execution_status', type: 'VARCHAR(32) NULL' },
    { table: AGENT_TABLES.actionProposals, column: 'lease_expires_at', type: 'BIGINT NULL' },
    { table: AGENT_TABLES.actionProposals, column: 'proposal_expires_at', type: 'BIGINT NULL' },
    {
        table: AGENT_TABLES.actionProposals,
        column: 'discovery_index_version',
        type: 'INTEGER NOT NULL DEFAULT 0',
    },
    { table: AGENT_TABLES.runs, column: 'parent_run_id', type: 'VARCHAR(255) NULL' },
    { table: AGENT_TABLES.messages, column: 'reasoning', type: 'TEXT NULL' },
    { table: AGENT_TABLES.messages, column: 'reasoning_ms', type: 'INTEGER NULL' },
    { table: AGENT_TABLES.messages, column: 'ui', type: 'TEXT NULL' },
    { table: AGENT_TABLES.messages, column: 'feedback', type: 'TEXT NULL' },
    { table: AGENT_TABLES.messages, column: 'agent_name', type: 'VARCHAR(255) NULL' },
    // A message's place in its thread, assigned on append. 0 on a row from before: those sort first.
    { table: AGENT_TABLES.messages, column: 'seq', type: 'BIGINT NOT NULL DEFAULT 0' },
    { table: AGENT_TABLES.threads, column: 'model', type: 'VARCHAR(255) NULL' },
    { table: AGENT_TABLES.threads, column: 'queue_pause', type: 'TEXT NULL' },
    { table: AGENT_TABLES.threads, column: 'default_agent', type: 'VARCHAR(255) NULL' },
    // The persona a queued message's send resolved, so it starts under that one.
    { table: AGENT_TABLES.queuedMessages, column: 'persona', type: 'VARCHAR(255) NULL' },
    { table: AGENT_TABLES.toolCalls, column: 'confirmation', type: 'TEXT NULL' },
    { table: AGENT_TABLES.toolCalls, column: 'approver', type: 'VARCHAR(255) NULL' },
    { table: AGENT_TABLES.toolCalls, column: 'expires_at', type: 'BIGINT NULL' },
    { table: AGENT_TABLES.toolCalls, column: 'remember', type: 'INTEGER NULL' },
    { table: AGENT_TABLES.toolCalls, column: 'decided_via', type: 'VARCHAR(64) NULL' },
    // The model's own tool call id, now that the PK is scoped to the run. NULL on a row from before:
    // its PK is that id.
    { table: AGENT_TABLES.toolCalls, column: 'call_id', type: 'VARCHAR(255) NULL' },
    // Where a usage row's `cost_usd` came from: 'provider' | 'estimate'. NULL on rows from before it
    // existed, which only ever held a provider-reported cost.
    { table: AGENT_TABLES.tokenUsage, column: 'cost_source', type: 'VARCHAR(16) NULL' },
    // A run's time to first token, its failure's stream error code and its last model. NULL on a run
    // from before they were recorded, and on the runs they don't apply to.
    { table: AGENT_TABLES.runs, column: 'ttft_ms', type: 'INTEGER NULL' },
    { table: AGENT_TABLES.runs, column: 'error_code', type: 'VARCHAR(64) NULL' },
    { table: AGENT_TABLES.runs, column: 'model_id', type: 'VARCHAR(255) NULL' },
    // When the person sent a message / a queued send (epoch ms), when not when it was stored. NULL on
    // a row from before, and on every message nobody named a send time for: `created_at` stands in.
    { table: AGENT_TABLES.messages, column: 'sent_at', type: 'BIGINT NULL' },
    { table: AGENT_TABLES.queuedMessages, column: 'sent_at', type: 'BIGINT NULL' },
    // The ledger beyond agent turns (`AgentService.recordUsage`): what the spend was for, who served
    // it, the tenant, the units it bills by when they are not tokens, and the caller's JSON. NULL on
    // every row from before — all of them agent-turn rows, which reports read as feature `agent`.
    { table: AGENT_TABLES.tokenUsage, column: 'feature', type: 'VARCHAR(255) NULL' },
    { table: AGENT_TABLES.tokenUsage, column: 'provider', type: 'VARCHAR(255) NULL' },
    { table: AGENT_TABLES.tokenUsage, column: 'tenant_ref', type: 'VARCHAR(255) NULL' },
    { table: AGENT_TABLES.tokenUsage, column: 'audio_seconds', type: 'DOUBLE PRECISION NULL' },
    { table: AGENT_TABLES.tokenUsage, column: 'characters', type: 'BIGINT NULL' },
    { table: AGENT_TABLES.tokenUsage, column: 'requests', type: 'INTEGER NULL' },
    { table: AGENT_TABLES.tokenUsage, column: 'metadata', type: 'TEXT NULL' },
    // 0 keeps a row out of the agent's quota windows (`recordUsage({ countsTowardsQuota: false })`).
    // Every row from before counted, hence the default.
    {
        table: AGENT_TABLES.tokenUsage,
        column: 'counts_towards_quota',
        type: 'INTEGER NOT NULL DEFAULT 1',
    },
    // Where a run's time went: queue wait, model and tool totals, how many of each, and the per-step
    // breakdown (JSON). NULL on a run from before, and (but `queue_ms`) on one that did not complete.
    { table: AGENT_TABLES.runs, column: 'queue_ms', type: 'INTEGER NULL' },
    { table: AGENT_TABLES.runs, column: 'model_ms', type: 'INTEGER NULL' },
    { table: AGENT_TABLES.runs, column: 'tools_ms', type: 'INTEGER NULL' },
    { table: AGENT_TABLES.runs, column: 'model_calls', type: 'INTEGER NULL' },
    { table: AGENT_TABLES.runs, column: 'tool_calls', type: 'INTEGER NULL' },
    { table: AGENT_TABLES.runs, column: 'timings', type: 'TEXT NULL' },
    // When a price row was written and last changed (epoch ms; retiring a row moves `updated_at`).
    // NULL-able with no default, so adding them is instant and old rows are left as they were: a
    // row from before reads back without either.
    { table: AGENT_TABLES.modelPricing, column: 'created_at', type: 'BIGINT NULL' },
    { table: AGENT_TABLES.modelPricing, column: 'updated_at', type: 'BIGINT NULL' },
];
/**
 * Columns that were created `NOT NULL` and are nullable now. `ADD COLUMN` cannot reach them: the
 * column exists, it only refuses a NULL. `agent_token_usage.thread_id` is the one — usage recorded
 * outside any conversation has no thread. Its foreign key to `agent_thread` stays (a NULL never
 * violates it, and a deleted thread still takes its rows with it).
 */
const NULLABLE_COLUMNS = [
    { table: AGENT_TABLES.tokenUsage, column: 'thread_id', type: 'VARCHAR(255) NULL' },
];
/**
 * Whether `table.column` accepts NULL, read from the catalog — `true` also when the catalog cannot
 * say (an unknown dialect is left alone rather than rebuilt on a guess). Postgres and MySQL through
 * `information_schema.columns`, SQLite through `PRAGMA table_info`. A read: no lock is taken.
 */
async function isNullable(db, table, column, schema) {
    if (isMySql(db)) {
        const rows = rowsOf(await db.rawQuery('SELECT is_nullable AS nullable FROM information_schema.columns ' +
            'WHERE table_schema = COALESCE(?, DATABASE()) AND table_name = ? AND column_name = ?', [schema ?? null, table, column]));
        const value = rows[0]?.nullable ?? rows[0]?.NULLABLE;
        return value === undefined || String(value).toUpperCase() === 'YES';
    }
    try {
        const rows = rowsOf(await db.rawQuery('SELECT is_nullable AS nullable FROM information_schema.columns ' +
            // The configured schema, else every schema on the path (where an unqualified name resolves).
            `WHERE ${schema === undefined ? 'table_schema = ANY (current_schemas(false))' : 'table_schema = ?'} ` +
            'AND table_name = ? AND column_name = ?', schema === undefined ? [table, column] : [schema, table, column]));
        const value = rows[0]?.nullable;
        return value === undefined || String(value).toUpperCase() === 'YES';
    }
    catch {
        // Not Postgres — SQLite has no information_schema.
    }
    try {
        const rows = rowsOf(await db.rawQuery(`PRAGMA table_info("${table}")`));
        const row = rows.find((each) => String(each.name) === column);
        return row === undefined || Number(row.notnull) === 0;
    }
    catch {
        return true;
    }
}
/** Is this a SQLite client? The one dialect that cannot drop a NOT NULL in place. */
function isSqlite(db) {
    return /sqlite|libsql/i.test(dialectOf(db));
}
/**
 * Make `table.column` nullable. Postgres and MySQL change the column in place; SQLite cannot, so the
 * ledger is rebuilt — created under a scratch name in its current shape, the rows copied over, the
 * old table dropped and the new one renamed — inside a savepoint, so a failure leaves the old table
 * as it was. Only the usage ledger is ever rebuilt: no table references it, so dropping it breaks no
 * foreign key, and its indexes are recreated by the index phase that follows.
 */
async function makeNullable(db, table, column, type, schema) {
    const mysql = isMySql(db);
    const ref = qualifiedSql(table, schema);
    if (mysql) {
        await db.rawQuery(forDialect(`ALTER TABLE ${ref} MODIFY "${column}" ${type}`, true));
        return;
    }
    if (!isSqlite(db)) {
        await db.rawQuery(`ALTER TABLE ${ref} ALTER COLUMN "${column}" DROP NOT NULL`);
        return;
    }
    if (table !== AGENT_TABLES.tokenUsage) {
        throw new Error(`[@adonis-agora/agent] cannot make ${table}.${column} nullable on SQLite`);
    }
    const scratch = `${table}__rebuild`;
    const columns = rowsOf(await db.rawQuery(`PRAGMA table_info("${table}")`)).map((row) => String(row.name));
    const list = columns.map((name) => `"${name}"`).join(', ');
    await db.rawQuery('SAVEPOINT agent_ledger_rebuild');
    try {
        await db.rawQuery(`DROP TABLE IF EXISTS "${scratch}"`);
        await db.rawQuery(tokenUsageTableStatement(scratch));
        await db.rawQuery(`INSERT INTO "${scratch}" (${list}) SELECT ${list} FROM "${table}"`);
        await db.rawQuery(`DROP TABLE "${table}"`);
        await db.rawQuery(`ALTER TABLE "${scratch}" RENAME TO "${table}"`);
        await db.rawQuery('RELEASE SAVEPOINT agent_ledger_rebuild');
    }
    catch (error) {
        await db.rawQuery('ROLLBACK TO SAVEPOINT agent_ledger_rebuild').catch(() => undefined);
        await db.rawQuery('RELEASE SAVEPOINT agent_ledger_rebuild').catch(() => undefined);
        throw error;
    }
}
/**
 * Does `table` already have `column`? Probed with a zero-row `SELECT` rather than `information_schema`
 * (absent on SQLite) or a `PRAGMA` (SQLite-only): every dialect rejects an unknown column at parse
 * time, and `WHERE 1 = 0` means no rows are ever read.
 *
 * Deliberately NOT "attempt the ALTER and swallow the error": that pattern also swallows a permission
 * failure, a lock timeout, and a typo, leaving a schema that is still wrong and a migration that
 * reported success.
 */
async function hasColumn(db, table, column, schema) {
    try {
        await db.rawQuery(portableSql(db, `SELECT "${column}" FROM ${qualifiedSql(table, schema)} WHERE 1 = 0`));
        return true;
    }
    catch {
        return false;
    }
}
/**
 * Whether `agent_tool_call` has its `call_id` column (#183). What `LucidAgentStore` checks when the
 * app owns the schema (`autoCreateTables: false`) and so may boot a new version before its migration
 * has added the column. Probed on {@link schemaRunner}'s client, outside any global transaction.
 */
export function hasToolCallIdColumn(db, schema) {
    return hasColumn(schemaRunner(db), AGENT_TABLES.toolCalls, 'call_id', schema);
}
/**
 * Whether `agent_message` and `agent_queued_message` have their `sent_at` columns — what
 * `LucidAgentStore` checks under `autoCreateTables: false`, where the app may boot this version before
 * its migration has added them. Probed like {@link hasToolCallIdColumn}.
 */
export async function hasSentAtColumns(db, schema) {
    const runner = schemaRunner(db);
    return ((await hasColumn(runner, AGENT_TABLES.messages, 'sent_at', schema)) &&
        (await hasColumn(runner, AGENT_TABLES.queuedMessages, 'sent_at', schema)));
}
/**
 * Whether `agent_token_usage` has the columns `AgentService.recordUsage` writes, and a nullable
 * `thread_id` — what `LucidAgentStore` checks under `autoCreateTables: false`. Probed like
 * {@link hasToolCallIdColumn}.
 */
export async function hasLedgerColumns(db, schema) {
    const runner = schemaRunner(db);
    return ((await hasColumn(runner, AGENT_TABLES.tokenUsage, 'feature', schema)) &&
        (await hasColumn(runner, AGENT_TABLES.tokenUsage, 'metadata', schema)));
}
/** Whether `agent_token_usage` has `counts_towards_quota`. Probed like {@link hasLedgerColumns}. */
export function hasQuotaFlagColumn(db, schema) {
    return hasColumn(schemaRunner(db), AGENT_TABLES.tokenUsage, 'counts_towards_quota', schema);
}
/** Whether `agent_run` has its timing columns. Probed like {@link hasLedgerColumns}. */
export async function hasRunTimingColumns(db, schema) {
    const runner = schemaRunner(db);
    return ((await hasColumn(runner, AGENT_TABLES.runs, 'queue_ms', schema)) &&
        (await hasColumn(runner, AGENT_TABLES.runs, 'timings', schema)));
}
/**
 * Add each of `columns` its table is missing (`ALTER TABLE … ADD COLUMN`), the same lock-free,
 * race-safe repair {@link createAgentTables} runs — for the other tables this package provisions
 * (A2A, Poppy). Returns the `<table>.<column>` repairs issued.
 */
export async function addMissingColumns(db, columns, schema) {
    const mysql = isMySql(db);
    const repairs = [];
    for (const { table, column, type } of columns) {
        if (await hasColumn(db, table, column, schema))
            continue;
        await issue(db, forDialect(`ALTER TABLE ${qualifiedSql(table, schema)} ADD COLUMN "${column}" ${type}`, mysql), () => hasColumn(db, table, column, schema));
        repairs.push(`${table}.${column}`);
    }
    return repairs;
}
/**
 * Whether `agent_model_pricing` has its `created_at` / `updated_at` columns — what
 * `LucidPricingStore` checks under `autoCreateTables: false`. Probed like {@link hasLedgerColumns}.
 */
export async function hasPricingTimestampColumns(db, schema) {
    const runner = schemaRunner(db);
    return ((await hasColumn(runner, AGENT_TABLES.modelPricing, 'created_at', schema)) &&
        (await hasColumn(runner, AGENT_TABLES.modelPricing, 'updated_at', schema)));
}
/** Rows of a raw query result, whatever the driver wraps them in (`{ rows }`, `[rows, fields]`, rows). */
export function rowsOf(result) {
    if (Array.isArray(result)) {
        return Array.isArray(result[0])
            ? result[0]
            : result;
    }
    const rows = result?.rows;
    return Array.isArray(rows) ? rows : [];
}
/**
 * The names of the indexes this database already has, or `null` when the dialect offers no catalog
 * this knows how to read (then every `CREATE INDEX IF NOT EXISTS` is simply issued).
 *
 * This is what keeps an up-to-date schema LOCK-FREE. Postgres takes the table lock for
 * `CREATE INDEX IF NOT EXISTS` before it looks whether the index is there, and that lock conflicts
 * with any open transaction that wrote to the table — so the "no-op" statement waits for it. A test
 * suite that wraps everything in one global transaction never commits, and the wait never ends. A
 * catalog read takes no such lock, so an index that exists is never touched.
 *
 * Postgres first: it is the one dialect where a failed statement poisons an ambient transaction, and
 * there the first probe succeeds.
 */
async function existingIndexes(db, schema) {
    // MySQL first there: it has no `pg_indexes`, and its own catalog is the only probe that answers.
    if (isMySql(db)) {
        const rows = rowsOf(await db.rawQuery('SELECT index_name AS name FROM information_schema.statistics ' +
            'WHERE table_schema = COALESCE(?, DATABASE())', [schema ?? null]));
        return new Set(rows.map((row) => String(row.name ?? row.NAME)));
    }
    const probes = [
        // The configured schema — an index lives in its table's schema. Without one, every schema on
        // the search path, not only the first: an unqualified `CREATE INDEX … ON "t"` finds `t`
        // wherever the path does, so that is where its index has to be looked for.
        schema === undefined
            ? [
                'SELECT indexname AS name FROM pg_indexes WHERE schemaname = ANY (current_schemas(false))',
                [],
            ]
            : ['SELECT indexname AS name FROM pg_indexes WHERE schemaname = ?', [schema]],
        [`SELECT name FROM sqlite_master WHERE type = 'index'`, []],
    ];
    for (const [probe, bindings] of probes) {
        try {
            const rows = rowsOf(await db.rawQuery(probe, bindings));
            return new Set(rows.map((row) => String(row.name)));
        }
        catch {
            // Not this dialect — try the next catalog.
        }
    }
    return null;
}
/** Does `table` exist? Same zero-row probe as {@link hasColumn}. */
async function hasTable(db, table, schema) {
    try {
        await db.rawQuery(portableSql(db, `SELECT 1 FROM ${qualifiedSql(table, schema)} WHERE 1 = 0`));
        return true;
    }
    catch {
        return false;
    }
}
/**
 * Run one DDL statement, tolerating exactly one failure: another process got there first. Several
 * processes boot at once (web + worker, or a rolling deploy) and each provisions the schema; `IF NOT
 * EXISTS` is not atomic across sessions (two concurrent `CREATE TABLE IF NOT EXISTS` on Postgres can
 * both pass the check and one then fails on the catalog's unique index), and `ADD COLUMN` has no
 * guard at all. So a failure is re-checked against the database: if what the statement was meant to
 * create is there now, the race was lost and the job is done. Anything else — a permission error, a
 * lock timeout, a typo — still throws.
 */
async function issue(db, statement, done, run = () => db.rawQuery(statement)) {
    for (let attempt = 1;; attempt += 1) {
        try {
            await run();
            return;
        }
        catch (error) {
            if (await done())
                return;
            // MySQL settles two sessions building the same index at once by rolling one back as a deadlock
            // victim — and the winner's index may not be in the catalog yet when the loser looks. That
            // statement did nothing and is safe to send again; the next try either lands or finds it done.
            if (attempt >= DDL_ATTEMPTS || !isTransientLockFailure(error))
                throw error;
            await new Promise((resolve) => setTimeout(resolve, 25 * attempt));
        }
    }
}
const DDL_ATTEMPTS = 5;
/** A statement the database rolled back to break a lock conflict: MySQL 1213/1205, Postgres 40P01/40001. */
function isTransientLockFailure(error) {
    for (let current = error; current !== null && typeof current === 'object';) {
        const { code, errno } = current;
        if (code === 'ER_LOCK_DEADLOCK' ||
            code === 'ER_LOCK_WAIT_TIMEOUT' ||
            errno === 1213 ||
            errno === 1205 ||
            code === '40P01' ||
            code === '40001') {
            return true;
        }
        current = current.cause;
    }
    return false;
}
/**
 * Idempotently provision the twelve agent tables through Lucid's async raw runner (`CREATE TABLE IF
 * NOT EXISTS`), then additively repair a database that predates run tracking by ALTERing in the
 * `run_id` columns its three older tables are missing.
 *
 * Both halves matter, and for different populations. A fresh database gets everything from the
 * `CREATE TABLE` statements and never reaches a repair branch — which is what makes a non-empty
 * `repairs` result mean "this schema was out of date", not "boot happened". A database created before
 * run tracking shipped has all three tables already, so every `CREATE TABLE IF NOT EXISTS` no-ops and
 * only the repair adds the columns the store now writes on every turn.
 *
 * Against a schema that is already current it issues no DDL that takes a table lock: an existing
 * index is found in the catalog instead of re-issued (see {@link existingIndexes}). And it is safe
 * with several processes provisioning at once (see {@link issue}).
 *
 * Returns the `<table>.<column>` repairs actually issued, so a caller can report them. Works on every
 * Lucid dialect. For an AdonisJS app prefer the published migration
 * (`node ace configure @adonis-agora/agent`); this helper is what that migration calls, and what the
 * stores call themselves when `autoCreateTables` is on.
 *
 * `schema` puts the tables in that database schema (a Postgres schema, a MySQL database): it is
 * created first when missing, and every statement — DDL, catalog reads, probes — names it. Refused on
 * SQLite, which has no schemas.
 */
export async function createAgentTables(db, options = {}) {
    const schema = validateSchema(db, options.schema, 'createAgentTables');
    const statements = createTableStatements(schema);
    const mysql = isMySql(db);
    if (schema !== undefined)
        await ensureDatabaseSchema(db, schema);
    // Order matters, in three phases rather than one pass. Tables first, then the `run_id` repair, then
    // indexes — because `createTableStatements` includes `CREATE INDEX ... ON "agent_message" ("run_id")`,
    // and on a legacy database that index would be issued against a table whose `run_id` the repair has
    // not added yet. `CREATE INDEX IF NOT EXISTS` does not save it: the guard is on the INDEX existing,
    // not on the column, so the statement still fails to parse with "no such column: run_id".
    for (const stmt of statements) {
        if (!stmt.startsWith('CREATE TABLE'))
            continue;
        const table = /^CREATE TABLE IF NOT EXISTS (?:"[^"]+"\.)?"([^"]+)"/.exec(stmt)?.[1];
        await issue(db, forDialect(stmt, mysql), () => hasTable(db, table, schema));
    }
    const repairs = [];
    const addColumn = async (table, column, type) => {
        if (await hasColumn(db, table, column, schema))
            return;
        await issue(db, forDialect(`ALTER TABLE ${qualifiedSql(table, schema)} ADD COLUMN "${column}" ${type}`, mysql), () => hasColumn(db, table, column, schema));
        repairs.push(`${table}.${column}`);
    };
    for (const table of RUN_ID_COLUMNS) {
        await addColumn(table, 'run_id', 'VARCHAR(255) NULL');
    }
    for (const { table, column, type } of ADDITIVE_COLUMNS) {
        await addColumn(table, column, type);
    }
    // After the added columns (a SQLite rebuild copies every column the table has by then) and before
    // the indexes (the rebuild drops the old table's, and the index phase puts them back).
    for (const { table, column, type } of NULLABLE_COLUMNS) {
        if (await isNullable(db, table, column, schema))
            continue;
        // Several statements on SQLite, so `issue` is handed the change rather than one statement.
        await issue(db, `${table}.${column} NULL`, () => isNullable(db, table, column, schema), () => makeNullable(db, table, column, type, schema));
        repairs.push(`${table}.${column}`);
    }
    const indexes = await existingIndexes(db, schema);
    for (const stmt of statements) {
        if (!stmt.startsWith('CREATE INDEX'))
            continue;
        const index = /^CREATE INDEX IF NOT EXISTS "([^"]+)"/.exec(stmt)?.[1];
        if (indexes?.has(index))
            continue;
        await issue(db, forDialect(stmt, mysql), async () => (await existingIndexes(db, schema))?.has(index) ?? false);
    }
    return repairs;
}
/**
 * The governance console's list indexes: what its feeds and the runs list seek on, newest first
 * (tool calls by `created_at`, threads by `updated_at`, runs by agent or tenant and `started_at`).
 * Not part of {@link createTableStatements}: on a table that already holds years of rows an index
 * build is long, so it ships as its own opt-in migration ({@link createAgentGovernanceIndexes}).
 */
export function governanceIndexStatements(schema) {
    const t = AGENT_TABLES;
    const q = agentTablesSql(schema);
    return [
        `CREATE INDEX IF NOT EXISTS "${t.toolCalls}_created_id_idx" ON ${q.toolCalls} ("created_at", "id")`,
        `CREATE INDEX IF NOT EXISTS "${t.threads}_updated_id_idx" ON ${q.threads} ("updated_at", "id")`,
        `CREATE INDEX IF NOT EXISTS "${t.threads}_tenant_updated_idx" ON ${q.threads} ("tenant_ref", "updated_at")`,
        `CREATE INDEX IF NOT EXISTS "${t.runs}_agent_started_idx" ON ${q.runs} ("agent_name", "started_at")`,
        `CREATE INDEX IF NOT EXISTS "${t.runs}_tenant_started_idx" ON ${q.runs} ("tenant_ref", "started_at")`,
    ];
}
/** The names of {@link governanceIndexStatements}' indexes. */
function governanceIndexNames() {
    return governanceIndexStatements().map((stmt) => /^CREATE INDEX IF NOT EXISTS "([^"]+)"/.exec(stmt)?.[1]);
}
/**
 * Create the governance list indexes ({@link governanceIndexStatements}) that are missing — what
 * the opt-in `add_agent_governance_list_indexes` migration runs, once, on an existing install. On
 * Postgres each one is built `CONCURRENTLY`, so writes go on while it builds (run it outside a
 * transaction). Returns the names it created.
 */
export async function createAgentGovernanceIndexes(db, options = {}) {
    const schema = validateSchema(db, options.schema, 'createAgentGovernanceIndexes');
    const mysql = isMySql(db);
    const postgres = /postgres|pg|redshift/i.test(dialectOf(db));
    const existing = await existingIndexes(db, schema);
    const created = [];
    for (const stmt of governanceIndexStatements(schema)) {
        const index = /^CREATE INDEX IF NOT EXISTS "([^"]+)"/.exec(stmt)?.[1];
        if (existing?.has(index))
            continue;
        const statement = postgres
            ? stmt.replace(/^CREATE INDEX /, 'CREATE INDEX CONCURRENTLY ')
            : forDialect(stmt, mysql);
        await issue(db, statement, async () => (await existingIndexes(db, schema))?.has(index) ?? false);
        created.push(index);
    }
    return created;
}
/** Drop the governance list indexes (the opt-in migration's `down()`). */
export async function dropAgentGovernanceIndexes(db, options = {}) {
    const schema = validateSchema(db, options.schema, 'dropAgentGovernanceIndexes');
    const mysql = isMySql(db);
    const postgres = /postgres|pg|redshift/i.test(dialectOf(db));
    const t = AGENT_TABLES;
    const tableOf = (name) => name.startsWith(t.toolCalls) ? t.toolCalls : name.startsWith(t.threads) ? t.threads : t.runs;
    for (const name of governanceIndexNames()) {
        if (mysql) {
            // MySQL has no `DROP INDEX IF EXISTS`, and an index belongs to its table.
            if (!(await existingIndexes(db, schema))?.has(name))
                continue;
            await db.rawQuery(portableSql(db, `DROP INDEX "${name}" ON ${qualifiedSql(tableOf(name), schema)}`));
            continue;
        }
        const statement = `DROP INDEX${postgres ? ' CONCURRENTLY' : ''} IF EXISTS ${qualifiedSql(name, schema)}`;
        await db.rawQuery(statement);
    }
}
/**
 * `DROP TABLE IF EXISTS` for the twelve agent tables, in reverse dependency order so a dialect that
 * enforces the `REFERENCES` clauses never refuses a drop for a child that still exists. The mirror of
 * {@link createAgentTables}, and what the published migration's `down()` calls.
 */
export function dropTableStatements(schema) {
    const t = AGENT_TABLES;
    return [
        t.genuiCalls,
        t.actionProposals,
        t.channelState,
        t.confirmTokens,
        t.streamFrames,
        t.queuedMessages,
        t.runs,
        t.tokenUsage,
        t.toolCalls,
        t.messages,
        t.modelPricing,
        t.threads,
    ].map((table) => `DROP TABLE IF EXISTS ${qualifiedSql(table, schema)}`);
}
/**
 * Drop the twelve agent tables (in `schema` when given; the schema itself is left in place).
 * Destructive and irreversible — this erases every thread and every ledger row.
 */
export async function dropAgentTables(db, options = {}) {
    const schema = validateSchema(db, options.schema, 'dropAgentTables');
    for (const stmt of dropTableStatements(schema)) {
        await db.rawQuery(portableSql(db, stmt));
    }
}
function isManager(db) {
    const candidate = db;
    return (candidate !== null &&
        typeof candidate === 'object' &&
        candidate.connectionGlobalTransactions instanceof Map &&
        typeof candidate.connection === 'function' &&
        typeof candidate.primaryConnectionName === 'string');
}
/**
 * The client schema DDL should run on: one of its OWN, outside a global transaction the caller has
 * open (`db.beginGlobalTransaction()` — what `testUtils.db().withGlobalTransaction()` does for a Japa
 * suite). Inside that transaction the DDL would be rolled back with the test while the process still
 * remembers the schema as provisioned, and on Postgres the column probes — which fail by design on a
 * missing column — would abort the whole transaction.
 *
 * Lucid's manager answers every `connection()` with the global transaction while one is registered,
 * so the registration is lifted for the one synchronous call that builds a plain client and put back
 * before anything else can run.
 *
 * SQLite is left where it is: it has one writer, so a second connection could only wait for the very
 * transaction it is trying to avoid (and with a pool of one there is no second connection at all).
 * Its DDL is transactional and a failed probe does not abort anything, so running inside is correct.
 *
 * Anything that is not the `Database` manager — a client from `db.connection(name)`, a fake — is
 * returned as is: a plain client is already outside, and a transaction client cannot be escaped.
 */
export function schemaRunner(db) {
    if (!isManager(db))
        return db;
    const name = db.primaryConnectionName;
    const transaction = db.connectionGlobalTransactions.get(name);
    if (transaction === undefined)
        return db;
    if (/sqlite|libsql/i.test(transaction.dialect?.name ?? ''))
        return db;
    db.connectionGlobalTransactions.delete(name);
    try {
        return db.connection(name);
    }
    finally {
        db.connectionGlobalTransactions.set(name, transaction);
    }
}
/**
 * Provisioning promise memoized per db client, so the three Lucid-backed stores (the agent store,
 * the pricing store, the governance read-model) that share these tables run {@link createAgentTables}
 * exactly once against a given connection instead of racing six `CREATE TABLE IF NOT EXISTS` each.
 * Correctness never depends on the memo — the DDL is idempotent — it only avoids redundant round
 * trips. A failed provisioning is evicted so the next call retries rather than caching the rejection.
 */
const provisioned = new WeakMap();
/**
 * Idempotently ensure the twelve agent tables exist, memoized per db client. This is what the stores
 * run when `autoCreateTables` is on (the default): the agent provider calls each store's
 * `ensureSchema()` once as the app starts, and a store used without the provider (a script, a test
 * that builds one by hand) falls back to it on first use — whichever store touches the connection
 * first provisions the shared schema, so pricing seeds and governance reads work even before the
 * first agent run. The DDL runs on {@link schemaRunner}'s client, never inside a global transaction.
 */
export function ensureAgentTables(db, options = {}) {
    // Per client AND schema: two schemas on one connection are two sets of tables.
    let bySchema = provisioned.get(db);
    if (bySchema === undefined) {
        bySchema = new Map();
        provisioned.set(db, bySchema);
    }
    const key = options.schema ?? '';
    const memo = bySchema;
    let ready = memo.get(key);
    if (ready === undefined) {
        ready = createAgentTables(schemaRunner(db), options)
            .then(() => undefined)
            .catch((error) => {
            memo.delete(key);
            throw error;
        });
        memo.set(key, ready);
    }
    return ready;
}
/**
 * Idempotently create the Lucid token sink's frame table and its purge index, under whatever name the
 * sink was given.
 * What the sink runs for itself (`autoCreateTables`), so it stands up next to ANY store — the
 * in-memory one included — without provisioning the other seven tables. {@link createAgentTables}
 * creates the same table under its default name, so the published migration covers it too.
 */
export async function ensureStreamFrameTable(db, table = AGENT_TABLES.streamFrames, options = {}) {
    const runner = schemaRunner(db);
    const schema = validateSchema(runner, options.schema, 'tokenSinks.lucid()');
    const mysql = isMySql(runner);
    if (schema !== undefined)
        await ensureDatabaseSchema(runner, schema);
    if (!(await hasTable(runner, table, schema))) {
        await issue(runner, forDialect(streamFrameTableStatement(table, schema), mysql), () => hasTable(runner, table, schema));
    }
    const name = `${table}_created_idx`;
    if ((await existingIndexes(runner, schema))?.has(name))
        return;
    await issue(runner, forDialect(streamFrameIndexStatement(table, schema), mysql), async () => (await existingIndexes(runner, schema))?.has(name) ?? false);
}
/**
 * Idempotently create text channels' state table, under whatever name the store was given — what
 * `lucidChannelStore` runs for itself (`autoCreateTables`), so it stands up next to any agent store.
 * {@link createAgentTables} creates the same table under its default name, so the published
 * migration covers it too.
 */
export async function ensureChannelStateTable(db, table = AGENT_TABLES.channelState, options = {}) {
    const runner = schemaRunner(db);
    const schema = validateSchema(runner, options.schema, 'lucidChannelStore()');
    const mysql = isMySql(runner);
    if (schema !== undefined)
        await ensureDatabaseSchema(runner, schema);
    const [create, index] = channelStateTableStatements(table, schema);
    if (!(await hasTable(runner, table, schema))) {
        await issue(runner, forDialect(create, mysql), () => hasTable(runner, table, schema));
    }
    const name = `${table}_expires_idx`;
    if ((await existingIndexes(runner, schema))?.has(name))
        return;
    await issue(runner, forDialect(index, mysql), async () => (await existingIndexes(runner, schema))?.has(name) ?? false);
}
//# sourceMappingURL=lucid-schema.js.map