/** * SQLite store for the project-scope BRAIN domain via drizzle-orm/node-sqlite + * node:sqlite (DatabaseSync). * * ## E6-L2 — thin-facade migration (T11522) * * `getBrainDb()` is now a thin facade that delegates the database open to * {@link openDualScopeDb}('project', cwd) — the canonical dual-scope chokepoint * introduced by E3/E4 (T11512/T11517) and already adopted by the tasks domain * (E6-L1, T11521). This ensures: * * - Every brain-domain open flows through the single pragma SSoT (ADR-068/069). * - The brain tables now live inside the consolidated project `cleo.db` — NOT a * separate `brain.db` file — co-existing with `tasks_*` / `conduit_*` / etc. * - DB Open Guard Gate 3 (`scripts/lint-no-direct-db-open.mjs`) stays green: the * only native open is inside `dual-scope-db.ts`. * * The legacy `drizzle-brain` migrations are still applied to this handle during * the E3→E6 transition (every brain migration is `CREATE TABLE IF NOT EXISTS` / * additive `ALTER TABLE`, so re-applying onto the consolidated `cleo.db` is * idempotent). This creates the legacy runtime-queried physical tables — most * notably `deriver_queue` (unprefixed; the consolidated schema carries the * prefixed `brain_deriver_queue`) — alongside the consolidated `brain_*` tables. * The exodus migration (T11248) renames them; E6-L7/L8 remove the legacy ones. * * ## Post-hoc DDL removal (T11522 acceptance criteria) * * Every `ensureColumns` band-aid (~15) and raw `CREATE TABLE IF NOT EXISTS` * (~8) that previously lived in {@link runBrainMigrations} has been removed. All * of them were redundant safety-nets fully covered by the `drizzle-brain` * migration files (the journal reconciler `probeAndMarkApplied` is robust enough * to detect already-applied DDL — see migration-manager.ts, T632). The ONE table * with no migration anywhere — `brain_task_observations` (T1615, a runtime-only * join cache mapped to `null` by exodus) — was converted to a forward Drizzle * migration under `migrations/drizzle-cleo-project/20260601000002_t11522-brain-task-observations`, * matching the T9179 precedent (ensureColumns → forward migration). * * @epic T5149 * @task T5128 * @task T11522 - E6-L2: route getBrainDb through openDualScopeDb (SG-DB-SUBSTRATE-V2) */ import type { DatabaseSync } from 'node:sqlite'; import type { NodeSQLiteDatabase } from 'drizzle-orm/node-sqlite'; import { type ProjectStore } from './dual-scope-db.js'; import { type DomainBinding } from './ports/domain-binding.js'; import * as brainSchema from './schema/memory-schema.js'; /** Schema version for newly created brain databases. Single source of truth. */ export declare const BRAIN_SCHEMA_VERSION = "1.0.0"; /** * The brain domain's bound value: its Drizzle handle plus the per-database * `sqlite-vec` availability flag. * * @task T12038 (E6-L14) */ export interface BrainDomainHandle { /** Drizzle instance typed against the legacy brain schema. */ readonly drizzle: NodeSQLiteDatabase; /** Whether the `sqlite-vec` extension loaded for THIS database. */ readonly vecLoaded: boolean; } /** * Get the path to the brain-domain SQLite database file. * * ## E6-L2 (T11522) * * After the dual-scope migration, `getBrainDb()` opens the consolidated project * `cleo.db` via {@link openDualScopeDb} — not the legacy standalone `brain.db`. * This function therefore returns the dual-scope `cleo.db` path so that callers * checking for the file `getBrainDb()` created (existence / backup / health * probes) point at the correct file. */ export declare function getBrainDbPath(cwd?: string): string; /** * Resolve the absolute path to the drizzle-brain migrations folder inside * @cleocode/core, using ESM-native module resolution (T1177). * * Delegates to {@link resolveCorePackageMigrationsFolder} which handles * bundled dist/, workspace dev, and global-install layouts uniformly via * `import.meta.resolve()` + `createRequire().resolve()` fallback. */ export declare function resolveBrainMigrationsFolder(): string; /** * Check whether the `sqlite-vec` extension is loaded for a project's brain * database. * * ## E6-L14 (T12038) — now per-database * * This used to read a single process-wide boolean, so a project whose vec * extension failed to load could report `true` because a DIFFERENT project * had loaded it. The flag is now per-binding. * * Returns `false` when the brain domain is not bound for `cwd` yet — callers * must `await getBrainDb(cwd)` / {@link bindBrainDomain} first. * * @param cwd - Project working directory. Defaults to the ambient project. */ export declare function isBrainVecLoaded(cwd?: string): boolean; /** * Initialize the project-scope BRAIN domain SQLite database (lazy, singleton). * * ## E6-L2 façade (T11522) * * Delegates the physical DB open to {@link openDualScopeDb}('project', cwd) — * the canonical dual-scope chokepoint. The returned `NodeSQLiteDatabase` wraps * the same `DatabaseSync` handle as the consolidated project `cleo.db` but is * typed against the legacy brain schema (`brainSchema`, physical tables * `brain_decisions`, …) so all existing brain callers compile and run without * change. The legacy `drizzle-brain` migrations are still applied to this handle * during the E3→E6 transition (additive / `IF NOT EXISTS` — idempotent on the * consolidated DB) so the runtime-queried legacy physical tables (notably * `deriver_queue`) co-exist with the consolidated `brain_*` tables. * * Brain-specific malformation auto-recovery (T10303 / Saga T10281) previously * ran here against the standalone `brain.db` file. That file no longer backs the * brain domain after this leaf — the brain tables live inside `cleo.db`, whose * malformation recovery is a dual-scope-level concern (the brain-only * quarantine/snapshot-restore pipeline would corrupt the co-resident `tasks_*` / * `conduit_*` domains). The recovery primitive itself (`recoverMalformedBrainDb`) * is retained for `doctor` use; only its wiring into this chokepoint is removed. * * Uses a promise guard so concurrent callers wait for the same initialization to * complete (migrations are async). */ export declare function getBrainDb(cwd?: string): Promise; /** * Bind the legacy brain schema to the {@link ProjectStore} for `cwd` and * return the full binding (store + native handle + {@link BrainDomainHandle}). * * ## E6-L14 cutover (T12038) * * The ProjectStore-bound typed port that replaces this module's singleton * quintet (`_db` / `_nativeDb` / `_dbPath` / `_initPromise` / `_vecLoaded`). * * The brain domain shares ONE `DatabaseSync` with the tasks domain (both are * tables inside the same consolidated `cleo.db`). Under the old design that * sharing was implicit and fragile: the tasks side could close the connection * while the brain singleton still referenced it, and `observeBrain`'s * cross-domain write-guard swallowed the resulting `database is not open` as * "session absent" — silently nulling `sourceSessionId` (T12019/T12020). * Binding both domains to the SAME store instance makes the sharing explicit: * an eviction invalidates both bindings at once, by identity. * * Prefer this over {@link getBrainDb} when you also need the native handle, * the per-database vec flag, or an explicit store for a cross-domain * transaction with the tasks domain. * * @param cwd - Project working directory. * @returns The live brain-domain binding. * * @task T12038 (E6-L14) */ export declare function bindBrainDomain(cwd?: string): Promise>; /** * Close the brain-domain database connection and release resources. * * ## E6-L2 (T11522) * * The brain domain now SHARES the consolidated project `cleo.db` handle with the * tasks domain (both open it via {@link openDualScopeDb}, same cache key). This * function therefore must NOT close the underlying `DatabaseSync` nor evict the * dual-scope cache — doing so would break in-flight tasks-domain queries with * "database is not open". It only drops the brain-domain singleton references; * the shared handle's lifecycle is owned by `openDualScopeDb` and torn down by a * coordinated reset (`closeAllDatabases` → `closeDb` → `_resetDualScopeDbCache`). */ export declare function closeBrainDb(): void; /** * Reset brain-domain singleton state without saving. * Used during tests or when the database file is recreated. * Safe to call multiple times. * * ## E6-L2 (T11522) * * Drops only the brain-domain singleton references — does NOT close the shared * dual-scope `cleo.db` handle nor evict the dual-scope cache (that handle is * shared with the tasks domain). Mirrors {@link closeBrainDb}. */ export declare function resetBrainDbState(): void; /** * Get the underlying node:sqlite DatabaseSync instance for brain.db. * Useful for direct PRAGMA calls or raw SQL operations. * Returns null if the database hasn't been initialized. */ export declare function getBrainNativeDb(cwd?: string): DatabaseSync | null; export type { NodeSQLiteDatabase }; /** * Re-export brain schema for external use. */ export { brainSchema }; //# sourceMappingURL=memory-sqlite.d.ts.map