Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

43.6 KB
Raw Blame
// The binding loads on first use through a dynamic require so esbuild never bundles it (the packaged app ships no
// node_modules). DuckDB's interrupt() is per connection, so each connection runs one statement at a time.
import { existsSync, mkdirSync } from "node:fs";
import { rm } from "node:fs/promises";
import { createRequire } from "node:module";
import { join } from "node:path";
import type * as DuckDB from "@duckdb/node-api";

import { DATA_LIMITS, DATA_RESULTS_SCHEMA } from "../../shared/data-surface.ts";
import { botFolder as defaultBotFolder } from "../bot-folder.ts";
import { boxCpus } from "../box-cpus.ts";
import { boxMemory } from "../box-memory.ts";
import { DATA_DIR } from "../config.ts";
import { tableIdentifierParts } from "./identifiers.ts";
import { preparePageQuery } from "./page-query.ts";
import {
  DataFailure,
  type BotDatabase,
  type DataColumn,
  type DataColumnStats,
  type DataConnection,
  type DataEngine,
  type DataExtension,
  type DataHeld,
  type DataHistogram,
  type DataPage,
  type DescribeResult,
  type MaterialiseResult,
  type RunOptions,
  type RunResult,
} from "./types.ts";

type Binding = typeof DuckDB;
type DataShut = { closed: true } | { closed: false; why: DataHeld["why"]; detail?: string };
const CLOSED: DataShut = { closed: true };
type Cell = string | number | boolean | null;

export const DATA_DB_FILE = "data.duckdb";
export const DATA_TMP_DIR = "data-tmp";
const IDLE_CLOSE_MS = 10 * 60_000;
export const EXTENSION_INSTALL_TIMEOUT_MS = 5 * 60_000;
const HISTOGRAM_BINS = 20;
const SAMPLE_SCAN_ROWS = 1_000;
const STATS_CACHE_MAX = 64;
const PAGE_LIMIT_MAX = 10 * DATA_LIMITS.pageSize;
const INTERRUPT_DELIVERY_MS = 10;
const RESULT_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
const PLAIN_IDENTIFIER = /^[a-z_][a-z0-9_]*$/;

export interface DataEngineOptions {
  botFolder?: (botId: string) => string;
  dataDir?: string;
  env?: NodeJS.ProcessEnv;
  idleMs?: number;
  binding?: Binding;
  /** Tests: the wait before a status read's answer is confirmed by loading the binding. */
  confirmInstalledMs?: number;
  /** Tests: the look for the binding's files. */
  filesFound?: (directory: string | null) => boolean;
}

/** Null means the server's own node_modules. */
export function duckdbDirectory(env: NodeJS.ProcessEnv = process.env): string | null {
  if (env.OMB_DUCKDB_DIR) return env.OMB_DUCKDB_DIR;
  if (env.OMB_RESOURCES_PATH) {
    const staged = join(env.OMB_RESOURCES_PATH, "duckdb");
    if (existsSync(staged)) return staged;
  }
  return null;
}

/** createRequire resolves upwards from the anchor, so one inside <dir>/node_modules finds the staged @duckdb and @resvg first. */
export function nativeRequire(directory: string | null = duckdbDirectory()): NodeJS.Require {
  return createRequire(directory ? join(directory, "node_modules", "openmausbot-duckdb-anchor.js") : import.meta.url);
}

function loadBinding(directory: string | null): Binding {
  return nativeRequire(directory)("@duckdb/node-api") as Binding;
}

/** A status read asks before anything needs the binding; this long after, the binding is loaded to confirm the answer. */
export const CONFIRM_INSTALLED_MS = 5_000;

/** Whether this platform's binding files are where the loader would look, found without loading them. */
export function bindingFilesFound(directory: string | null): boolean {
  try {
    const bindings = createRequire(nativeRequire(directory).resolve("@duckdb/node-api")).resolve("@duckdb/node-bindings");
    const from = createRequire(bindings);
    const native = `@duckdb/node-bindings-${process.platform}-${process.arch}`;
    return [native, `${native}-musl`].some((name) => { try { from.resolve(`${name}/duckdb.node`); return true; } catch { return false; } });
  } catch {
    return false;
  }
}

/** DuckDB's defaults assume it owns the machine (80 % of RAM, every core), and a native OOM takes the server down. A
 * container's limits count, not its host's: each thread also counts against the container's pids limit. */
export function instanceSettings(folder: string, dataDir: string, duckdbVersion: string, env: NodeJS.ProcessEnv = process.env,
  box: { cpus: number; memoryBytes: number } = { cpus: boxCpus(), memoryBytes: boxMemory().bytes }): Record<string, string> {
  const quarterOfRam = Math.floor(box.memoryBytes / 4);
  const memoryMiB = Math.max(256, Math.floor(Math.min(2 * 1024 ** 3, quarterOfRam) / 1024 ** 2));
  return {
    memory_limit: env.OMB_DUCKDB_MEMORY_LIMIT || `${memoryMiB}MiB`,
    temp_directory: join(folder, DATA_TMP_DIR),
    max_temp_directory_size: env.OMB_DUCKDB_MAX_TEMP_SIZE || "20GB",
    threads: String(Math.max(1, Math.min(4, Math.floor(box.cpus) || 1))),
    // Extensions are tied to the exact DuckDB version.
    extension_directory: join(dataDir, "duckdb-extensions", duckdbVersion),
    autoinstall_known_extensions: "false",
    autoload_known_extensions: "true",
    // Persistent secrets sit unencrypted on disk; OMB's broker issues temporary ones.
    allow_persistent_secrets: "false",
    preserve_insertion_order: "false",
  };
}

export function quoteIdentifier(name: string): string {
  return `"${name.replaceAll('"', '""')}"`;
}

function quoteLiteral(text: string): string {
  return `'${text.replaceAll("'", "''")}'`;
}

/** INSTALL is a local no-op once cached; extension names come only from our tools. */
export async function loadExtensions(connection: Pick<DuckDB.DuckDBConnection, "run">, extensions: DataExtension[]): Promise<void> {
  for (const extension of extensions) {
    try {
      // The core repository bootstraps without httpfs; HTTPS repositories need it first.
      await connection.run(`INSTALL ${extension} FROM core`);
      await connection.run(`LOAD ${extension}`);
    } catch (error) {
      throw new DataFailure({
        code: "sql_error",
        message: error instanceof Error ? error.message : String(error),
        retryable: true,
        hint: `DuckDB needs the ${extension} extension. Connect to the internet and retry; allow access to http://extensions.duckdb.org. Once installed, the extension is cached for offline use.`,
      });
    }
  }
}

export function likePattern(filter: string): string {
  return `%${filter.replace(/[\\%_]/g, (char) => `\\${char}`)}%`;
}

export function levenshtein(a: string, b: string): number {
  if (a === b) return 0;
  if (!a.length || !b.length) return Math.max(a.length, b.length);
  let previous = Array.from({ length: b.length + 1 }, (_, i) => i);
  for (let i = 1; i <= a.length; i++) {
    const current = [i];
    for (let j = 1; j <= b.length; j++) {
      const cost = a[i - 1] === b[j - 1] ? 0 : 1;
      current[j] = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + cost);
    }
    previous = current;
  }
  return previous[b.length];
}

export function closeNames(wanted: string, names: readonly string[], max = 5): string[] {
  const target = wanted.toLowerCase();
  const budget = Math.max(2, Math.floor(target.length / 3));
  return names
    .map((name) => {
      const folded = name.toLowerCase();
      const distance = levenshtein(target, folded);
      const prefix = folded.startsWith(target) || target.startsWith(folded);
      const related = prefix || folded.includes(target);
      return { name, distance, prefix, related };
    })
    .filter((entry) => entry.related || entry.distance <= budget)
    .sort((x, y) => Number(y.prefix) - Number(x.prefix) || x.distance - y.distance || x.name.localeCompare(y.name))
    .slice(0, max)
    .map((entry) => entry.name);
}

const COMPARABLE_TYPE = /^(?!STRUCT|MAP|UNION|JSON|GEOMETRY|VARIANT)[^[\]]*$/;
const NUMERIC_TYPE = /^(?:U?TINYINT|U?SMALLINT|U?INTEGER|U?BIGINT|U?HUGEINT|FLOAT|DOUBLE|REAL|DECIMAL\(|BIGNUM)/;
const TEMPORAL_TYPE = /^(?:DATE|TIMESTAMP)/;

function parseLine(message: string): number | undefined {
  const match = /\bLINE (\d+):/.exec(message);
  return match ? Number(match[1]) : undefined;
}

function missingTableName(message: string): string | null {
  const match = /Table with name "?([^"\n]+?)"? does not exist/.exec(message);
  return match ? match[1] : null;
}

type InterruptCause = "timeout" | "cancelled";

type RunState = { interrupted: InterruptCause | null; deadline: number; retry?: NodeJS.Timeout };

function failureFrom(error: unknown, sql: string, interrupted: InterruptCause | null, timeoutMs: number): DataFailure {
  if (error instanceof DataFailure) return error;
  const message = error instanceof Error ? error.message : String(error);
  if (interrupted === "timeout") {
    return new DataFailure({ code: "timeout", message, sql, retryable: true, hint: `Stopped after ${timeoutMs} ms. Narrow the query or aggregate first.` });
  }
  if (interrupted === "cancelled") return new DataFailure({ code: "cancelled", message, sql });
  const line = parseLine(message);
  if (missingTableName(message)) return new DataFailure({ code: "table_not_found", message, sql, line });
  return new DataFailure({ code: "sql_error", message, sql, line });
}

export class Lane {
  private queue: Promise<unknown> = Promise.resolve();
  private running: RunState | null = null;

  readonly connection: DuckDB.DuckDBConnection;
  constructor(connection: DuckDB.DuckDBConnection) { this.connection = connection; }

  run<T>(sql: string, options: { timeoutMs?: number; signal?: AbortSignal }, work: (connection: DuckDB.DuckDBConnection) => Promise<T>): Promise<T> {
    const turn = this.queue.then(() => this.execute(sql, options, work));
    this.queue = turn.catch(() => undefined);
    return turn;
  }

  private async execute<T>(sql: string, options: { timeoutMs?: number; signal?: AbortSignal }, work: (connection: DuckDB.DuckDBConnection) => Promise<T>): Promise<T> {
    const timeoutMs = options.timeoutMs ?? DATA_LIMITS.sqlTimeoutMs;
    if (options.signal?.aborted) throw new DataFailure({ code: "cancelled", message: "Cancelled before it ran.", sql });
    const state: RunState = { interrupted: null, deadline: Date.now() + timeoutMs };
    this.running = state;
    const timer = setTimeout(() => this.stop(state, "timeout"), timeoutMs);
    const onAbort = () => this.stop(state, "cancelled");
    options.signal?.addEventListener("abort", onAbort, { once: true });
    try {
      return await work(this.connection);
    } catch (error) {
      throw failureFrom(error, sql, state.interrupted, timeoutMs);
    } finally {
      clearTimeout(timer);
      if (state.retry) clearInterval(state.retry);
      options.signal?.removeEventListener("abort", onAbort);
      this.running = null;
    }
  }

  interrupt(): void {
    if (this.running) this.stop(this.running, "cancelled");
  }

  // DuckDB honours an interrupt only once its statement is dispatched, so a cause keeps re-delivering
  // until the statement settles: one that lands first is not lost, and no cause blocks the next.
  private stop(state: RunState, cause: InterruptCause): void {
    state.interrupted ??= cause;
    this.connection.interrupt();
    if (state.retry || Date.now() >= state.deadline) return;
    const retry = setInterval(() => {
      if (Date.now() >= state.deadline) {
        clearInterval(retry);
        state.retry = undefined;
        return;
      }
      this.connection.interrupt();
    }, INTERRUPT_DELIVERY_MS);
    state.retry = retry;
  }
}

interface Open {
  binding: Binding;
  instance: DuckDB.DuckDBInstance;
  lanes: Record<DataConnection, Lane>;
  reserved: Set<string>;
}

export const CLOSE_DRAIN_MS = 10_000;

/** A statement DuckDB did not stop (seen on Windows) would otherwise hold a close, and shutdown, forever. */
export async function drainLanes(lanes: Array<{ run<T>(sql: string, options: object, work: () => Promise<T>): Promise<T> }>, ms: number): Promise<boolean> {
  let timer: NodeJS.Timeout | undefined;
  const drained = Promise.all(lanes.map((lane) => lane.run("", {}, async () => undefined).catch(() => undefined))).then(() => true);
  const expired = new Promise<boolean>((resolve) => { timer = setTimeout(() => resolve(false), ms); timer.unref(); });
  try { return await Promise.race([drained, expired]); } finally { clearTimeout(timer); }
}

interface CatalogEntry {
  catalog: string;
  schema: string;
  name: string;
  type: string;
  qualifyCatalog?: true;
}

function toCount(cell: Cell): number {
  return typeof cell === "string" ? Number(cell) : typeof cell === "number" ? cell : 0;
}

function formatNumber(value: number): string {
  return Number.isInteger(value) ? String(value) : String(Number(value.toPrecision(6)));
}

function formatEpoch(seconds: number, dateOnly: boolean): string {
  const iso = new Date(seconds * 1000).toISOString();
  return dateOnly ? iso.slice(0, 10) : iso.slice(0, 16).replace("T", " ");
}

class BotDb implements BotDatabase {
  private state: Promise<Open> | null = null;
  private closing: Promise<DataShut> | null = null;
  /** The instance a close could not let go of: still open on the file, so the next statement uses it, never a second one. */
  private kept: Promise<Open> | null = null;
  private idleTimer: NodeJS.Timeout | null = null;
  private inFlight = 0;
  private readonly statsCache = new Map<string, DataColumnStats>();
  private statsRevision = 0;

  readonly botId: string;
  private readonly engine: Engine;
  constructor(botId: string, engine: Engine) { this.botId = botId; this.engine = engine; }

  private open(): Promise<Open> {
    if (!this.state) {
      // The file admits one instance per process: a reopen waits for a close that may still be draining, and after a close
      // that could not let go of the instance it goes on with that one.
      const previous = this.closing ?? Promise.resolve(CLOSED);
      const opening = previous.then((shut) => (shut.closed || !this.kept ? this.openNow() : this.kept));
      this.state = opening;
      opening.catch(() => {
        if (this.state === opening) this.state = null;
      });
    }
    this.touch();
    return this.state;
  }

  private async openNow(): Promise<Open> {
    const binding = this.engine.binding();
    const folder = this.engine.folder(this.botId);
    mkdirSync(folder, { recursive: true, mode: 0o700 });
    const settings = instanceSettings(folder, this.engine.dataDir, binding.version(), this.engine.env);
    const instance = await binding.DuckDBInstance.create(join(folder, DATA_DB_FILE), settings);
    let bot: DuckDB.DuckDBConnection | undefined;
    let panel: DuckDB.DuckDBConnection | undefined;
    let page: DuckDB.DuckDBConnection | undefined;
    try {
      bot = await instance.connect();
      panel = await instance.connect();
      page = await instance.connect();
      // A real schema: temp tables would be invisible to the panel's connection.
      await bot.run(`CREATE SCHEMA IF NOT EXISTS ${DATA_RESULTS_SCHEMA}`);
      const keywords = await bot.runAndReadAll("SELECT keyword_name FROM duckdb_keywords() WHERE keyword_category = 'reserved'");
      const reserved = new Set(keywords.getRowsJson().map((row) => String(row[0]).toLowerCase()));
      return { binding, instance, lanes: { bot: new Lane(bot), panel: new Lane(panel), page: new Lane(page) }, reserved };
    } catch (error) {
      bot?.closeSync();
      panel?.closeSync();
      page?.closeSync();
      instance.closeSync();
      throw error;
    }
  }

  private touch(): void {
    if (this.idleTimer) clearTimeout(this.idleTimer);
    this.idleTimer = setTimeout(() => {
      this.idleTimer = null;
      if (this.inFlight > 0) this.touch();
      else void this.close();
    }, this.engine.idleMs);
    this.idleTimer.unref();
  }

  private async statement<T>(connection: DataConnection, sql: string, options: { timeoutMs?: number; signal?: AbortSignal }, work: (open: Open, conn: DuckDB.DuckDBConnection) => Promise<T>): Promise<T> {
    this.inFlight++;
    try {
      const open = await this.open();
      return await open.lanes[connection].run(sql, options, (conn) => work(open, conn));
    } catch (error) {
      if (error instanceof DataFailure && error.error.code === "table_not_found" && !error.error.candidates) {
        const wanted = missingTableName(error.error.message);
        if (wanted) error.error.candidates = await this.candidates(wanted).catch(() => []);
      }
      throw error;
    } finally {
      this.inFlight--;
      this.touch();
    }
  }

  private cells(binding: Binding, chunk: DuckDB.DuckDBDataChunk): Cell[][] {
    const json = binding.JsonDuckDBValueConverter;
    return chunk.convertRows<Cell>((value, type) => {
      if (value === null) return null;
      if (type.typeId === binding.DuckDBTypeId.BLOB) return `<blob ${(value as DuckDB.DuckDBBlobValue).bytes.length} bytes>`;
      const converted = json(value, type, json);
      return converted !== null && typeof converted === "object" ? JSON.stringify(converted) : converted;
    }) as Cell[][];
  }

  private resultColumns(result: DuckDB.DuckDBResult): DataColumn[] {
    return Array.from({ length: result.columnCount }, (_, i) => ({ name: result.columnName(i), type: result.columnType(i).toString() }));
  }

  private async read(open: Open, conn: DuckDB.DuckDBConnection, sql: string, maxRows = Infinity): Promise<RunResult> {
    const started = performance.now();
    const result = await conn.run(sql);
    if (result.statementType !== open.binding.StatementType.SELECT && result.statementType !== open.binding.StatementType.EXPLAIN) {
      this.statsCache.clear();
      this.statsRevision++;
    }
    const rows: Cell[][] = [];
    for (let i = 0; i < result.chunkCount && rows.length < maxRows; i++) {
      for (const row of this.cells(open.binding, result.getChunk(i))) {
        if (rows.length >= maxRows) break;
        rows.push(row);
      }
    }
    return { columns: this.resultColumns(result), rows, rowCount: result.rowCount, truncated: rows.length < result.rowCount, elapsedMs: Math.round(performance.now() - started) };
  }

  async run(sql: string, options: RunOptions): Promise<RunResult> {
    const maxRows = options.maxRows === undefined ? Infinity : Math.max(0, options.maxRows);
    await this.loadExtensions(options);
    return this.statement(options.connection, sql, options, (open, conn) => this.read(open, conn, sql, maxRows));
  }

  async pageQuery(sql: string, params: unknown[] | Record<string, unknown> | undefined, options: { maxRows: number; timeoutMs: number; signal?: AbortSignal }): Promise<RunResult> {
    return this.statement("page", sql, options, async (open, conn) => {
      const started = performance.now();
      // A transaction a cancelled query left open ends before the next one begins.
      await conn.run("ROLLBACK").catch(() => undefined);
      await conn.run("BEGIN TRANSACTION READ ONLY");
      try {
        const prepared = await preparePageQuery(open.binding, conn, sql, params);
        const result = await prepared.stream();
        const rows: Cell[][] = [];
        for (let chunk = await result.fetchChunk(); chunk && chunk.rowCount > 0 && rows.length <= options.maxRows; chunk = await result.fetchChunk()) {
          rows.push(...this.cells(open.binding, chunk));
        }
        const kept = rows.slice(0, options.maxRows);
        return { columns: this.resultColumns(result), rows: kept, rowCount: kept.length, truncated: rows.length > options.maxRows, elapsedMs: Math.round(performance.now() - started) };
      } finally {
        await conn.run("ROLLBACK").catch(() => undefined);
      }
    });
  }

  /** A first-time INSTALL is a download, so extensions load as their own step, off the statement's timeout. */
  private async loadExtensions(options: RunOptions): Promise<void> {
    const extensions = options.extensions ?? [];
    if (!extensions.length) return;
    const label = extensions.map((extension) => `INSTALL ${extension} FROM core`).join("; ");
    await this.statement(options.connection, label, { timeoutMs: EXTENSION_INSTALL_TIMEOUT_MS, signal: options.signal }, (_open, conn) => loadExtensions(conn, extensions));
  }

  async materialise(sql: string, name: string, options: RunOptions): Promise<MaterialiseResult> {
    if (!RESULT_NAME.test(name)) throw new DataFailure({ code: "invalid_input", message: `Result name "${name}" must be a plain identifier (letters, digits, underscore).` });
    const table = `${DATA_RESULTS_SCHEMA}.${name}`;
    const body = sql.trim().replace(/;+\s*$/, "");
    const create = `CREATE OR REPLACE TABLE ${table} AS ${body}`;
    return this.statement(options.connection, sql, options, async (open, conn) => {
      const started = performance.now();
      const created = await this.read(open, conn, create);
      const described = await this.read(open, conn, `DESCRIBE ${table}`);
      return {
        table,
        columns: described.rows.map((row) => ({ name: String(row[0]), type: String(row[1]) })),
        rowCount: toCount(created.rows[0]?.[0] ?? 0),
        elapsedMs: Math.round(performance.now() - started),
      };
    });
  }

  async dropResult(name: string, connection: DataConnection = "bot"): Promise<void> {
    if (!RESULT_NAME.test(name)) throw new DataFailure({ code: "invalid_input", message: `Result name "${name}" must be a plain identifier (letters, digits, underscore).` });
    const sql = `DROP TABLE IF EXISTS ${DATA_RESULTS_SCHEMA}.${name}`;
    await this.statement(connection, sql, {}, (open, conn) => this.read(open, conn, sql));
  }

  private async catalog(connection: DataConnection = "panel", temporary = false): Promise<CatalogEntry[]> {
    const catalogs = temporary ? "(table_catalog = current_database() OR table_catalog = 'temp')" : "table_catalog = current_database()";
    const sql = `SELECT table_catalog, table_schema, table_name, table_type FROM information_schema.tables WHERE ${catalogs} AND table_schema NOT IN ('information_schema', 'pg_catalog') ORDER BY table_schema, table_name`;
    const result = await this.statement(connection, sql, {}, (open, conn) => this.read(open, conn, sql));
    return result.rows.map((row) => ({ catalog: String(row[0]), schema: String(row[1]), name: String(row[2]), type: String(row[3]) }));
  }

  private displayName(entry: CatalogEntry): string {
    return entry.schema === "main" ? entry.name : `${entry.schema}.${entry.name}`;
  }

  private qualified(entry: CatalogEntry, reserved: Set<string>): string {
    const part = (name: string) => (PLAIN_IDENTIFIER.test(name) && !reserved.has(name) ? name : quoteIdentifier(name));
    return `${entry.qualifyCatalog || entry.catalog === "temp" ? `${part(entry.catalog)}.` : ""}${part(entry.schema)}.${part(entry.name)}`;
  }

  private splitIdentifier(text: string): string[] {
    const parts: string[] = [];
    let current = "";
    let quoted = false;
    for (let i = 0; i < text.length; i++) {
      const char = text[i];
      if (char === '"') {
        if (quoted && text[i + 1] === '"') { current += '"'; i++; }
        else quoted = !quoted;
      } else if (char === "." && !quoted) {
        parts.push(current);
        current = "";
      } else current += char;
    }
    parts.push(current);
    return parts.map((part) => part.trim());
  }

  private async lookup(name: string, connection: DataConnection = "panel"): Promise<CatalogEntry> {
    const parts = tableIdentifierParts(name) ?? this.splitIdentifier(name.trim());
    const table = parts[parts.length - 1];
    const schema = parts.length > 1 ? parts[parts.length - 2] : undefined;
    const catalog = parts.length === 3 ? parts[0] : undefined;
    if (!table) throw new DataFailure({ code: "invalid_input", message: "A table name is required." });
    if (parts.length > 3) throw new DataFailure({ code: "invalid_input", message: "A table name has at most three identifier parts." });
    const entries = await this.catalog(connection, true);
    const inCatalog = catalog === undefined ? entries : entries.filter((entry) => entry.catalog.toLowerCase() === catalog.toLowerCase());
    const inSchema = schema ? inCatalog.filter((entry) => entry.schema === schema || entry.schema.toLowerCase() === schema.toLowerCase()) : inCatalog;
    const rank = (entry: CatalogEntry) => (entry.schema === "main" ? 0 : entry.schema === DATA_RESULTS_SCHEMA ? 1 : 2);
    const exact = inSchema.filter((entry) => entry.name === table).sort((x, y) => rank(x) - rank(y));
    const folded = inSchema.filter((entry) => entry.name.toLowerCase() === table.toLowerCase()).sort((x, y) => rank(x) - rank(y));
    const found = folded.find((entry) => entry.catalog === "temp") ?? exact[0] ?? folded[0];
    if (found) return catalog === undefined ? found : { ...found, qualifyCatalog: true };
    throw new DataFailure({
      code: "table_not_found",
      message: `Table ${name.trim()} does not exist.`,
      candidates: closeNames(table, entries.map((entry) => this.displayName(entry))),
    });
  }

  private async candidates(wanted: string): Promise<string[]> {
    const entries = await this.catalog();
    const table = this.splitIdentifier(wanted).pop() ?? wanted;
    return closeNames(table, entries.map((entry) => this.displayName(entry)));
  }

  async resolveTable(name: string, connection: DataConnection = "panel"): Promise<string> {
    const entry = await this.lookup(name, connection);
    const open = await this.open();
    return this.qualified(entry, open.reserved);
  }

  private async columnsOf(connection: DataConnection, from: string, options: { timeoutMs?: number; signal?: AbortSignal }): Promise<DataColumn[]> {
    // DESCRIBE takes a table name or a query, not a parenthesised subquery.
    const sql = from.startsWith("(") ? `DESCRIBE SELECT * FROM ${from}` : `DESCRIBE ${from}`;
    const result = await this.statement(connection, sql, options, (open, conn) => this.read(open, conn, sql));
    return result.rows.map((row) => ({ name: String(row[0]), type: String(row[1]) }));
  }

  /** Two passes rather than SUMMARIZE, which is 10-25x slower. */
  private async aggregate(connection: DataConnection, from: string, columns: DataColumn[], options: { timeoutMs?: number; signal?: AbortSignal }): Promise<{ rowCount: number; columns: DataColumn[] }> {
    const countSql = `SELECT count(*) FROM ${from}`;
    const count = await this.statement(connection, countSql, options, (open, conn) => this.read(open, conn, countSql));
    const rowCount = toCount(count.rows[0]?.[0] ?? 0);
    const enriched: DataColumn[] = columns.map((column) => ({ ...column }));
    if (!columns.length) return { rowCount, columns: enriched };

    const nullsSql = `SELECT ${columns.map((column) => `count(${quoteIdentifier(column.name)}), approx_count_distinct(${quoteIdentifier(column.name)})`).join(", ")} FROM ${from}`;
    const nulls = await this.statement(connection, nullsSql, options, (open, conn) => this.read(open, conn, nullsSql));
    const nullRow = nulls.rows[0] ?? [];
    enriched.forEach((column, i) => {
      const nonNull = toCount(nullRow[i * 2] ?? 0);
      column.nullPct = rowCount ? Math.round(((rowCount - nonNull) / rowCount) * 1000) / 10 : 0;
      column.approxUnique = toCount(nullRow[i * 2 + 1] ?? 0);
    });

    const comparable = enriched.filter((column) => COMPARABLE_TYPE.test(column.type));
    if (comparable.length) {
      const rangeSql = `SELECT ${comparable.map((column) => `CAST(min(${quoteIdentifier(column.name)}) AS VARCHAR), CAST(max(${quoteIdentifier(column.name)}) AS VARCHAR)`).join(", ")} FROM ${from}`;
      const range = await this.statement(connection, rangeSql, options, (open, conn) => this.read(open, conn, rangeSql));
      const rangeRow = range.rows[0] ?? [];
      comparable.forEach((column, i) => {
        column.min = rangeRow[i * 2] === undefined ? null : (rangeRow[i * 2] as string | null);
        column.max = rangeRow[i * 2 + 1] === undefined ? null : (rangeRow[i * 2 + 1] as string | null);
      });
    }
    return { rowCount, columns: enriched };
  }

  private async source(target: string, connection: DataConnection): Promise<string> {
    const text = target.trim().replace(/;+\s*$/, "");
    // Keep accepting the display names callers could pass before quoted SQL
    // identifiers were recognized here (for example, Order Items).
    const displayName = /^[\w."$ -]+$/.test(text) && !/\s(?:from|select|with|values)\s/i.test(text) && !/^(?:select|with|from|values|pivot|unpivot|show|describe)\b/i.test(text);
    if (tableIdentifierParts(text) || displayName) return this.resolveTable(text, connection);
    return `(${text}) AS omb_described`;
  }

  async describe(target: string, options: RunOptions): Promise<DescribeResult> {
    const from = await this.source(target, options.connection);
    const all = await this.columnsOf(options.connection, from, options);
    const profiled = all.slice(0, DATA_LIMITS.describeColumnsMax);
    const { rowCount, columns } = await this.aggregate(options.connection, from, profiled, options);
    if (profiled.length) {
      const samplesSql = `SELECT ${profiled.map((column) => `list(DISTINCT CAST(${quoteIdentifier(column.name)} AS VARCHAR)) FILTER (WHERE ${quoteIdentifier(column.name)} IS NOT NULL)[1:${DATA_LIMITS.sampleValues}]`).join(", ")} FROM (SELECT * FROM ${from} LIMIT ${SAMPLE_SCAN_ROWS})`;
      const samples = await this.statement(options.connection, samplesSql, options, (open, conn) => this.read(open, conn, samplesSql));
      const sampleRow = samples.rows[0] ?? [];
      columns.forEach((column, i) => {
        const cell = sampleRow[i];
        let values: unknown;
        try { values = typeof cell === "string" ? JSON.parse(cell) : cell; } catch { values = []; }
        column.sample = Array.isArray(values) ? values.map((value) => String(value).slice(0, DATA_LIMITS.sampleChars)) : [];
      });
    }
    return { rowCount, columns: [...columns, ...all.slice(DATA_LIMITS.describeColumnsMax)] };
  }

  async stats(table: string, options: { signal?: AbortSignal } = {}): Promise<DataColumnStats> {
    const revision = this.statsRevision;
    const entry = await this.lookup(table);
    const open = await this.open();
    const from = this.qualified(entry, open.reserved);
    const countSql = `SELECT count(*) FROM ${from}`;
    const count = await this.statement("panel", countSql, options, (o, conn) => this.read(o, conn, countSql));
    const rowCount = toCount(count.rows[0]?.[0] ?? 0);
    const key = `${from}\u0000${rowCount}`;
    const cacheable = entry.type !== "VIEW";
    const cached = cacheable ? this.statsCache.get(key) : undefined;
    if (cached) return cached;
    const columns = await this.columnsOf("panel", from, options);
    const aggregated = await this.aggregate("panel", from, columns, options);
    const stats: DataColumnStats = { table: this.displayName(entry), rowCount: aggregated.rowCount, columns: aggregated.columns };
    if (cacheable && revision === this.statsRevision) {
      if (this.statsCache.size >= STATS_CACHE_MAX) this.statsCache.clear();
      this.statsCache.set(key, stats);
    }
    return stats;
  }

  async histogram(table: string, column: string, options: { bins?: number; signal?: AbortSignal } = {}): Promise<DataHistogram> {
    const entry = await this.lookup(table);
    const open = await this.open();
    const from = this.qualified(entry, open.reserved);
    const columns = await this.columnsOf("panel", from, options);
    const found = columns.find((c) => c.name === column) ?? columns.find((c) => c.name.toLowerCase() === column.toLowerCase());
    if (!found) throw new DataFailure({ code: "invalid_input", message: `Column "${column}" is not in ${this.displayName(entry)}.`, candidates: closeNames(column, columns.map((c) => c.name)) });
    const bins = Math.max(1, Math.min(200, Math.floor(options.bins ?? HISTOGRAM_BINS)));
    const id = quoteIdentifier(found.name);
    const numeric = NUMERIC_TYPE.test(found.type);
    const temporal = !numeric && TEMPORAL_TYPE.test(found.type);
    const result: DataHistogram = { table: this.displayName(entry), column: found.name, bins: [] };

    if (numeric || temporal) {
      const sourceId = `omb_source.${id}`;
      const value = numeric ? `CAST(${sourceId} AS DOUBLE)` : `epoch(CAST(${sourceId} AS TIMESTAMP))`;
      // NaN/infinity cannot define a finite bucket. Infinite dates produce
      // NULL epochs; they must not be counted in the constant/first bucket.
      const finite = `isfinite(${value})`;
      const sql = `WITH omb_range AS (SELECT min(${value}) AS lo, max(${value}) AS hi FROM ${from} AS omb_source WHERE ${finite})
SELECT LEAST(coalesce(floor((${value} - omb_range.lo) / nullif(omb_range.hi - omb_range.lo, 0) * ${bins}), 0), ${bins - 1})::INTEGER AS bin, count(*) AS n, min(omb_range.lo), min(omb_range.hi)
FROM ${from} AS omb_source, omb_range WHERE ${finite} GROUP BY 1 ORDER BY bin`;
      const rows = (await this.statement("panel", sql, options, (o, conn) => this.read(o, conn, sql))).rows;
      if (!rows.length) return result;
      const lo = Number(rows[0][2]);
      const hi = Number(rows[0][3]);
      const width = (hi - lo) / bins;
      const counts = new Map(rows.map((row) => [toCount(row[0]), toCount(row[1])]));
      const label = (i: number) => {
        const start = lo + i * width;
        if (temporal) return formatEpoch(start, found.type.startsWith("DATE"));
        return width ? `${formatNumber(start)} – ${formatNumber(start + width)}` : formatNumber(lo);
      };
      const shown = width ? bins : 1;
      result.bins = Array.from({ length: shown }, (_, i) => ({ label: label(i), count: counts.get(i) ?? 0 }));
      return result;
    }

    const sql = `SELECT CAST(${id} AS VARCHAR) AS v, count(*) AS n FROM ${from} WHERE ${id} IS NOT NULL GROUP BY 1 ORDER BY n DESC, v LIMIT ${bins}`;
    const rows = (await this.statement("panel", sql, options, (o, conn) => this.read(o, conn, sql))).rows;
    result.bins = rows.map((row) => ({ label: String(row[0]).slice(0, 2 * DATA_LIMITS.sampleChars), count: toCount(row[1]) }));
    return result;
  }

  async page(target: string, options: { offset: number; limit: number; sort?: { column: string; direction: "asc" | "desc" }; filter?: string; filterColumn?: string; signal?: AbortSignal }): Promise<DataPage> {
    const entry = await this.lookup(target);
    const open = await this.open();
    const from = this.qualified(entry, open.reserved);
    const columns = await this.columnsOf("panel", from, options);
    const offset = Math.max(0, Math.floor(options.offset) || 0);
    const limit = Math.max(1, Math.min(PAGE_LIMIT_MAX, Math.floor(options.limit) || DATA_LIMITS.pageSize));

    const filterColumns = options.filterColumn === undefined ? columns : columns.filter((column) => column.name === options.filterColumn);
    if (options.filterColumn !== undefined && !filterColumns.length) throw new DataFailure({ code: "invalid_input", message: `Cannot filter by "${options.filterColumn}": not a column of ${this.displayName(entry)}.`, candidates: closeNames(options.filterColumn, columns.map((column) => column.name)) });
    const filter = options.filter?.trim();
    const where = filter
      ? ` WHERE ${filterColumns.map((column) => `CAST(${quoteIdentifier(column.name)} AS VARCHAR) ILIKE ${quoteLiteral(likePattern(filter))} ESCAPE '\\'`).join(" OR ")}`
      : "";

    // With preserve_insertion_order off a scan's order is not fixed: rowid keeps a table's pages stable. Views have no rowid.
    const tiebreak = entry.type === "VIEW" ? [] : ["rowid"];
    const order: string[] = [];
    if (options.sort) {
      const sortColumn = columns.find((c) => c.name === options.sort!.column) ?? columns.find((c) => c.name.toLowerCase() === options.sort!.column.toLowerCase());
      if (!sortColumn) throw new DataFailure({ code: "invalid_input", message: `Cannot sort by "${options.sort.column}": not a column of ${this.displayName(entry)}.`, candidates: closeNames(options.sort.column, columns.map((c) => c.name)) });
      order.push(`${quoteIdentifier(sortColumn.name)} ${options.sort.direction === "desc" ? "DESC" : "ASC"} NULLS LAST`);
    }
    order.push(...tiebreak);
    const orderBy = order.length ? ` ORDER BY ${order.join(", ")}` : "";

    const countSql = `SELECT count(*) FROM ${from}${where}`;
    const pageSql = `SELECT * FROM ${from}${where}${orderBy} LIMIT ${limit} OFFSET ${offset}`;
    const sql = `${countSql};\n${pageSql}`;
    return this.statement("panel", sql, options, async (o, conn) => {
      const count = await this.read(o, conn, countSql);
      const rows = await this.read(o, conn, pageSql);
      return { columns, rows: rows.rows, rowCount: toCount(count.rows[0]?.[0] ?? 0), offset };
    });
  }

  async listTables(): Promise<Array<{ name: string; sqlName?: string; rowCount: number; columns: DataColumn[] }>> {
    const entries = (await this.catalog()).filter((entry) => entry.schema !== DATA_RESULTS_SCHEMA);
    if (!entries.length) return [];
    const open = await this.open();
    const columnsSql = "SELECT table_schema, table_name, column_name, data_type FROM information_schema.columns WHERE table_catalog = current_database() ORDER BY table_schema, table_name, ordinal_position";
    const columnRows = await this.statement("panel", columnsSql, {}, (o, conn) => this.read(o, conn, columnsSql));
    const columnsByTable = new Map<string, DataColumn[]>();
    for (const row of columnRows.rows) {
      const key = `${row[0]}\u0000${row[1]}`;
      const list = columnsByTable.get(key) ?? [];
      list.push({ name: String(row[2]), type: String(row[3]) });
      columnsByTable.set(key, list);
    }
    const countSql = `SELECT ${entries.map((entry) => `(SELECT count(*) FROM ${this.qualified(entry, open.reserved)})`).join(", ")}`;
    const counts = await this.statement("panel", countSql, {}, (o, conn) => this.read(o, conn, countSql));
    const countRow = counts.rows[0] ?? [];
    return entries.map((entry, i) => {
      const qualified = this.qualified(entry, open.reserved);
      return {
        name: this.displayName(entry),
        sqlName: entry.schema === "main" ? qualified.slice("main.".length) : qualified,
        rowCount: toCount(countRow[i] ?? 0),
        columns: columnsByTable.get(`${entry.schema}\u0000${entry.name}`) ?? [],
      };
    });
  }

  interrupt(connection: DataConnection): void {
    if (!this.state) return;
    void this.state.then((open) => open.lanes[connection].interrupt(), () => undefined);
  }

  async close(): Promise<void> {
    await this.shut();
  }

  /** Closed, or why the instance is still open: a statement that did not stop, or a close that failed. */
  async shut(): Promise<DataShut> {
    if (this.idleTimer) {
      clearTimeout(this.idleTimer);
      this.idleTimer = null;
    }
    const pending = this.state;
    if (!pending) return this.closing ?? CLOSED;
    this.state = null;
    this.statsCache.clear();
    const closing = (async (): Promise<DataShut> => {
      let open: Open;
      try { open = await pending; } catch { return CLOSED; }
      const shut = await this.closeOpen(open);
      if (shut.closed) this.kept = null;
      else {
        this.kept = pending;
        if (!this.state) this.state = pending;
      }
      return shut;
    })();
    this.closing = closing;
    try { return await closing; } finally { if (this.closing === closing) this.closing = null; }
  }

  private async closeOpen(open: Open): Promise<DataShut> {
    // Closing a connection with a statement in flight is undefined: interrupt first, then let the lanes drain.
    for (const lane of Object.values(open.lanes)) { try { lane.interrupt(); } catch { /* the drain below decides */ } }
    if (!await drainLanes(Object.values(open.lanes), CLOSE_DRAIN_MS)) {
      console.warn(`data: ${this.botId}'s database still had a statement running after ${CLOSE_DRAIN_MS} ms; its handles are left to the process`);
      return { closed: false, why: "running" };
    }
    try {
      for (const lane of Object.values(open.lanes)) lane.connection.closeSync();
      // The last close writes the WAL into the file and removes it: the file then holds every table on its own.
      open.instance.closeSync();
    } catch (error) {
      const detail = (error instanceof Error ? error.message : String(error)).split("\n")[0]!.slice(0, 200);
      console.warn(`data: ${this.botId}'s database did not close: ${detail}`);
      return { closed: false, why: "failed", detail };
    }
    return CLOSED;
  }

  get isOpen(): boolean {
    return this.state !== null;
  }
}

class Engine implements DataEngine {
  private readonly databases = new Map<string, BotDb>();
  private loaded: { binding: Binding } | { reason: string } | null = null;
  private confirming: ReturnType<typeof setTimeout> | null = null;
  private readonly installedListeners = new Set<() => void>();
  private readonly confirmInstalledMs: number;
  private readonly filesFound: (directory: string | null) => boolean;
  readonly env: NodeJS.ProcessEnv;
  readonly dataDir: string;
  readonly idleMs: number;
  readonly folder: (botId: string) => string;

  constructor(options: DataEngineOptions) {
    this.env = options.env ?? process.env;
    this.dataDir = options.dataDir ?? DATA_DIR;
    this.idleMs = options.idleMs ?? IDLE_CLOSE_MS;
    this.confirmInstalledMs = options.confirmInstalledMs ?? CONFIRM_INSTALLED_MS;
    this.filesFound = options.filesFound ?? bindingFilesFound;
    this.folder = options.botFolder ?? defaultBotFolder;
    if (options.binding) this.loaded = { binding: options.binding };
  }

  private load(): { binding: Binding } | { reason: string } {
    if (this.loaded) return this.loaded;
    const directory = duckdbDirectory(this.env);
    try {
      this.loaded = { binding: loadBinding(directory) };
    } catch (error) {
      const detail = (error instanceof Error ? error.message : String(error)).split("\n")[0].trim();
      const where = directory ? `from ${directory}` : "from the server's node_modules";
      const action = directory
        ? "Reinstall OpenMausBot, or point OMB_DUCKDB_DIR at a folder holding node_modules/@duckdb."
        : "Run pnpm install, or set OMB_DUCKDB_DIR to a folder holding node_modules/@duckdb.";
      this.loaded = { reason: `DuckDB did not load ${where}: ${detail}. ${action}` };
    }
    return this.loaded;
  }

  binding(): Binding {
    const loaded = this.load();
    if ("binding" in loaded) return loaded.binding;
    throw new DataFailure({ code: "engine_unavailable", message: loaded.reason });
  }

  unavailable(): string | null {
    const loaded = this.load();
    return "reason" in loaded ? loaded.reason : null;
  }

  installed(): boolean {
    if (this.loaded) return "binding" in this.loaded;
    const found = this.filesFound(duckdbDirectory(this.env));
    if (!this.confirming) {
      this.confirming = setTimeout(() => { if ((this.unavailable() === null) !== found) for (const listener of this.installedListeners) listener(); }, this.confirmInstalledMs);
      this.confirming.unref?.();
    }
    return found;
  }

  onInstalledChange(listener: () => void): void {
    this.installedListeners.add(listener);
  }

  async forBot(botId: string): Promise<BotDatabase> {
    if (!botId || /[\\/]|^\.\.?$/.test(botId)) throw new DataFailure({ code: "invalid_input", message: `Not a bot id: "${botId}".` });
    this.binding();
    let db = this.databases.get(botId);
    if (!db) {
      db = new BotDb(botId, this);
      this.databases.set(botId, db);
    }
    return db;
  }

  async deleteBot(botId: string): Promise<void> {
    const db = this.databases.get(botId);
    this.databases.delete(botId);
    await db?.close();
    const folder = this.folder(botId);
    for (const name of [DATA_DB_FILE, `${DATA_DB_FILE}.wal`, DATA_TMP_DIR]) {
      // Windows keeps a just-closed file busy for a moment; rm retries EBUSY/EPERM.
      await rm(join(folder, name), { recursive: true, force: true, maxRetries: 20, retryDelay: 100 });
    }
  }

  /** Every bot's database closed and kept, to open again on its next statement; the ones still open, and why. */
  async release(): Promise<DataHeld[]> {
    const open = [...this.databases.values()];
    const shut = await Promise.all(open.map((db) => db.shut()));
    return open.flatMap((db, i) => { const one = shut[i]!; return one.closed ? [] : [{ botId: db.botId, why: one.why, ...(one.detail ? { detail: one.detail } : {}) }]; });
  }

  async closeAll(): Promise<void> {
    const open = [...this.databases.values()];
    this.databases.clear();
    await Promise.all(open.map((db) => db.close()));
  }
}

export function createDataEngine(options: DataEngineOptions = {}): DataEngine {
  return new Engine(options);
}

export const dataEngine: DataEngine = createDataEngine();