@fgv/ts-agent-memory-sqlite-vec 5.1.0-49 → 5.1.0-51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/README.md +26 -1
  2. package/dist/packlets/sqlite-vec-index/connection.js +54 -0
  3. package/dist/packlets/sqlite-vec-index/connection.js.map +1 -0
  4. package/dist/packlets/sqlite-vec-index/model.js.map +1 -1
  5. package/dist/packlets/sqlite-vec-index/rebuildHelpers.js +48 -0
  6. package/dist/packlets/sqlite-vec-index/rebuildHelpers.js.map +1 -0
  7. package/dist/packlets/sqlite-vec-index/sqliteVecFragmentIndex.js +189 -8
  8. package/dist/packlets/sqlite-vec-index/sqliteVecFragmentIndex.js.map +1 -1
  9. package/dist/packlets/sqlite-vec-index/sqliteVecVectorIndex.js +107 -50
  10. package/dist/packlets/sqlite-vec-index/sqliteVecVectorIndex.js.map +1 -1
  11. package/dist/ts-agent-memory-sqlite-vec.d.ts +188 -13
  12. package/lib/packlets/sqlite-vec-index/connection.d.ts +42 -0
  13. package/lib/packlets/sqlite-vec-index/connection.d.ts.map +1 -0
  14. package/lib/packlets/sqlite-vec-index/connection.js +91 -0
  15. package/lib/packlets/sqlite-vec-index/connection.js.map +1 -0
  16. package/lib/packlets/sqlite-vec-index/model.d.ts +92 -0
  17. package/lib/packlets/sqlite-vec-index/model.d.ts.map +1 -1
  18. package/lib/packlets/sqlite-vec-index/model.js.map +1 -1
  19. package/lib/packlets/sqlite-vec-index/rebuildHelpers.d.ts +38 -0
  20. package/lib/packlets/sqlite-vec-index/rebuildHelpers.d.ts.map +1 -0
  21. package/lib/packlets/sqlite-vec-index/rebuildHelpers.js +53 -0
  22. package/lib/packlets/sqlite-vec-index/rebuildHelpers.js.map +1 -0
  23. package/lib/packlets/sqlite-vec-index/sqliteVecFragmentIndex.d.ts +52 -7
  24. package/lib/packlets/sqlite-vec-index/sqliteVecFragmentIndex.d.ts.map +1 -1
  25. package/lib/packlets/sqlite-vec-index/sqliteVecFragmentIndex.js +188 -7
  26. package/lib/packlets/sqlite-vec-index/sqliteVecFragmentIndex.js.map +1 -1
  27. package/lib/packlets/sqlite-vec-index/sqliteVecVectorIndex.d.ts +44 -11
  28. package/lib/packlets/sqlite-vec-index/sqliteVecVectorIndex.d.ts.map +1 -1
  29. package/lib/packlets/sqlite-vec-index/sqliteVecVectorIndex.js +108 -51
  30. package/lib/packlets/sqlite-vec-index/sqliteVecVectorIndex.js.map +1 -1
  31. package/package.json +7 -7
@@ -1 +1 @@
1
- {"version":3,"file":"sqliteVecFragmentIndex.js","sourceRoot":"","sources":["../../../src/packlets/sqlite-vec-index/sqliteVecFragmentIndex.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,IAAI,IAAI,aAAa,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAU,aAAa,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACrE,OAAO,EAQL,aAAa,EACd,MAAM,sBAAsB,CAAC;AAG9B,0DAA0D;AAC1D,MAAM,kBAAkB,GAAW,kBAAkB,CAAC;AAEtD,yGAAyG;AACzG,MAAM,aAAa,GAAW,0BAA0B,CAAC;AAEzD;;;;;GAKG;AACH,MAAM,iBAAiB,GAA0B,CAAC,WAAW,EAAE,SAAS,EAAE,aAAa,CAAC,CAAC;AAEzF;;;;GAIG;AACH,MAAM,mBAAmB,GAAW,gCAAgC,CAAC;AA8BrE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,MAAM,OAAO,sBAAsB;IAQjC,YAAoB,EAA0B,EAAE,KAAa,EAAE,SAA6B;QAC1F,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC;QACd,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAC5B,IAAI,CAAC,MAAM,GAAG,SAAS,KAAK,SAAS,CAAC,CAA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Fortune\n * SPDX-License-Identifier: MIT\n */\n\nimport type BetterSqlite3 from 'better-sqlite3';\nimport { load as loadSqliteVec } from 'sqlite-vec';\nimport { Result, captureResult, fail, succeed } from '@fgv/ts-utils';\nimport {\n IEdgeTarget,\n IEmbeddedFragment,\n IFragmentLocator,\n IFragmentVectorIndex,\n IVectorQueryHit,\n MemoryId,\n MemoryScopeKey,\n edgeTargetKey\n} from '@fgv/ts-agent-memory';\nimport { ISqliteVecFragmentIndexCreateParams } from './model';\n\n/** Default name for the fragment `vec0` virtual table. */\nconst DEFAULT_TABLE_NAME: string = 'memory_fragments';\n\n/** A simple SQL identifier — the only shape allowed for the table name (it is interpolated into DDL). */\nconst IDENTIFIER_RE: RegExp = /^[A-Za-z_][A-Za-z0-9_]*$/;\n\n/**\n * The auxiliary (`+`-prefixed) columns this version of the index writes. A table\n * created by an earlier version carries a different set; see\n * {@link SqliteVecFragmentIndex._readExistingDimension} for why that has to be\n * detected explicitly rather than migrated.\n */\nconst AUXILIARY_COLUMNS: ReadonlyArray<string> = ['start_off', 'end_off', 'fragment_id'];\n\n/**\n * Matches one `+name` auxiliary-column declaration in a `vec0` `CREATE VIRTUAL TABLE`\n * statement. Only ever consumed via `String.matchAll`, which iterates a clone rather\n * than advancing this instance's `lastIndex`, so the shared `/g` regex is reusable.\n */\nconst AUXILIARY_COLUMN_RE: RegExp = /\\+\\s*([A-Za-z_][A-Za-z0-9_]*)/g;\n\n/**\n * One KNN row as returned by the fragment `vec0` MATCH query. The offset columns are\n * typed `number | bigint` because `better-sqlite3` returns integer columns as\n * `bigint` when a consumer enables its safe-integer mode (`defaultSafeIntegers`);\n * {@link SqliteVecFragmentIndex._toOffset} coerces them to a plain `number` (and\n * fails loudly on an out-of-safe-range value) before they reach the public locator.\n * All three identity columns are nullable: a fragment stored without a locator has\n * `NULL` offsets, and one stored without a `fragmentId` has a `NULL` `fragment_id`.\n */\ninterface IKnnRow {\n readonly target_key: string;\n // eslint-disable-next-line @rushstack/no-new-null -- SQLite returns NULL (not undefined) for an absent locator offset\n readonly start_off: number | bigint | null;\n // eslint-disable-next-line @rushstack/no-new-null -- SQLite returns NULL (not undefined) for an absent locator offset\n readonly end_off: number | bigint | null;\n // eslint-disable-next-line @rushstack/no-new-null -- SQLite returns NULL (not undefined) for an absent fragment id\n readonly fragment_id: string | null;\n readonly distance: number;\n}\n\n/**\n * The identity fields of a fragment hit, in `IVectorQueryHit` shape: a field the\n * stored fragment did not carry is *absent*, never present-but-`undefined`, so a hit\n * for a fragment stored without a `fragmentId` is structurally identical to one this\n * index produced before `fragment_id` existed.\n */\ntype FragmentIdentity = Pick<IVectorQueryHit, 'locator' | 'fragmentId'>;\n\n/**\n * A persistent, `sqlite-vec`-backed `IFragmentVectorIndex` (from\n * `@fgv/ts-agent-memory`) — the fragment-granular sibling of\n * {@link SqliteVecVectorIndex}, and the **durable** counterpart to the in-memory\n * `InMemoryFragmentCosineIndex`.\n *\n * @remarks\n * Where {@link SqliteVecVectorIndex} keys one vector per record on a\n * `target_key` primary key, this index holds **many** vectors per record — one per\n * fragment — so it keys the `vec0` table on `target_key` as a **`PARTITION KEY`**\n * (many rows may share it) and stores each fragment's identity in three auxiliary\n * columns (`+start_off`, `+end_off`, `+fragment_id`) that ride alongside the vector\n * and are returned on query but never filtered — in particular `fragment_id` is\n * stored and returned verbatim, never parsed and never part of the query path. A\n * query is a brute-force `vec0` KNN scan across all partitions returning per-fragment\n * hits, each carrying its record `target` plus whichever identity fields the stored\n * fragment was added with (a fragment must carry at least one).\n *\n * **`vec0` schema changes require a drop-and-re-index.** A\n * `CREATE VIRTUAL TABLE IF NOT EXISTS` is a no-op against an existing table (SQLite\n * does not compare schemas) and `vec0` has no `ALTER TABLE ADD COLUMN`, so a database written by an\n * earlier version of this package keeps its old auxiliary columns. `create` detects\n * that by parsing the stored `CREATE VIRTUAL TABLE` SQL and fails with an actionable\n * message naming the expected and found columns, rather than letting a widened\n * `INSERT` surface an opaque `no such column` at statement-prepare time. There are no\n * in-place migrations: drop the table (or use a fresh `tableName`) and re-index.\n * Fragment vectors are re-derivable from the records, so this costs embedding time,\n * never data.\n *\n * Semantics match `InMemoryFragmentCosineIndex` exactly: `addFragments` is\n * whole-record-replace (a single transaction deletes every prior fragment of the\n * target, then inserts the new set), `remove` drops every fragment of a target,\n * and `query` applies the optional `maxPerRecord` cap **during selection, before\n * the topK cut** — so one long document cannot crowd others out. The dimension is\n * established by the first `addFragments` (the `vec0` column is fixed-width) and\n * recovered from the table schema when a persistent file is reopened; similarity is\n * cosine (`score = 1 - cosineDistance`), byte-identical to the in-memory index.\n * Large-N ANN indexing is explicitly out of scope, same regime as the record index.\n *\n * The `better-sqlite3` `Database` is consumer-owned (bring-your-own): this index\n * loads the `sqlite-vec` extension onto it and reads/writes the table, but never\n * opens or closes the connection.\n * @public\n */\nexport class SqliteVecFragmentIndex implements IFragmentVectorIndex {\n private readonly _db: BetterSqlite3.Database;\n private readonly _table: string;\n /** The dimension of every stored fragment vector; `undefined` until the table exists. */\n private _dimension: number | undefined;\n /** Prepared statements; created once the table exists (established or recovered). */\n private _stmts: IFragmentStatements | undefined;\n\n private constructor(db: BetterSqlite3.Database, table: string, dimension: number | undefined) {\n this._db = db;\n this._table = table;\n this._dimension = dimension;\n this._stmts = dimension === undefined ? undefined : this._prepare();\n }\n\n /** The number of records that currently have at least one stored fragment. Zero before the first add. */\n public get recordCount(): number {\n if (this._stmts === undefined) {\n return 0;\n }\n // `Number(...)` narrows the count in case the consumer enabled better-sqlite3\n // safe-integer mode (which returns `count(*)` as a `bigint`).\n return Number((this._stmts.recordCount.get() as { c: number | bigint }).c);\n }\n\n /** The total number of fragments currently held across all records. Zero before the first add. */\n public get fragmentCount(): number {\n if (this._stmts === undefined) {\n return 0;\n }\n return Number((this._stmts.fragmentCount.get() as { c: number | bigint }).c);\n }\n\n /**\n * Family-convention factory. Loads the `sqlite-vec` extension onto the supplied\n * `better-sqlite3` connection and, if the fragment table already exists (a\n * reopened persistent file), verifies its auxiliary-column set matches this\n * version's and recovers its established dimension so no re-embedding is needed on\n * open.\n *\n * @param params - See {@link ISqliteVecFragmentIndexCreateParams}.\n * @returns `Success` with the index, or `Failure` if the table name is not a\n * simple identifier, the extension fails to load, or the existing table was\n * written by a version with a different auxiliary-column set (which requires a\n * drop-and-re-index — `vec0` cannot be altered in place).\n */\n public static create(params: ISqliteVecFragmentIndexCreateParams): Promise<Result<SqliteVecFragmentIndex>> {\n const table: string = params.tableName ?? DEFAULT_TABLE_NAME;\n if (!IDENTIFIER_RE.test(table)) {\n return Promise.resolve(\n fail(`sqlite-vec fragment index: table name '${table}' is not a simple SQL identifier`)\n );\n }\n return Promise.resolve(\n captureResult(() => {\n loadSqliteVec(params.database);\n const dimension: number | undefined = SqliteVecFragmentIndex._readExistingDimension(\n params.database,\n table\n );\n return new SqliteVecFragmentIndex(params.database, table, dimension);\n }).withErrorFormat((e) => `sqlite-vec fragment index: failed to initialize: ${e}`)\n );\n }\n\n /** {@inheritDoc IFragmentVectorIndex.addFragments} */\n public addFragments(\n target: IEdgeTarget,\n fragments: ReadonlyArray<IEmbeddedFragment>\n ): Promise<Result<number>> {\n const key: string = edgeTargetKey(target);\n // Validate every fragment before touching the database, so a bad fragment never\n // leaves the record half-replaced or the dimension half-established (whole-record\n // replace is all-or-nothing). The effective dimension is the established one, or —\n // on a still-dimensionless index — the first fragment's length; it is committed\n // (via table creation) only once the whole batch validates.\n let dimension: number | undefined = this._dimension;\n for (const fragment of fragments) {\n if (fragment.vector.length === 0) {\n return Promise.resolve(fail(`fragment index: cannot add '${key}': empty fragment vector`));\n }\n // A fragment carrying neither identity cannot be resolved back to anything by a\n // consumer holding the hit — the same invariant `embeddedFragmentConverter`\n // enforces at the untyped boundary, re-checked here at the index seam.\n if (fragment.locator === undefined && fragment.fragmentId === undefined) {\n return Promise.resolve(\n fail(\n `fragment index: cannot add '${key}': fragment requires at least one of 'locator' or 'fragmentId'`\n )\n );\n }\n // Locator offsets are persisted as SQLite integers (bound via BigInt). Reject a\n // non-safe-integer offset up front with a clear message, rather than letting\n // `BigInt(nonInteger)` throw cryptically inside the write transaction OR storing\n // a value the read-side `_toOffset` guard would later reject on every query.\n // An absent locator persists as a NULL offset pair and skips the check.\n if (\n fragment.locator !== undefined &&\n (!Number.isSafeInteger(fragment.locator.start) || !Number.isSafeInteger(fragment.locator.end))\n ) {\n return Promise.resolve(\n fail(\n `fragment index: cannot add '${key}': locator [${fragment.locator.start}, ${fragment.locator.end}) offsets must be safe integers`\n )\n );\n }\n if (dimension === undefined) {\n dimension = fragment.vector.length;\n } else if (fragment.vector.length !== dimension) {\n return Promise.resolve(\n fail(\n `fragment index: cannot add '${key}': fragment dimension ${fragment.vector.length} does not match index dimension ${dimension}`\n )\n );\n }\n }\n return Promise.resolve(\n captureResult(() => {\n // A same-target re-author (or an empty batch) still needs the table to exist\n // to delete prior fragments; create it lazily on the first non-empty add.\n if (this._stmts === undefined) {\n if (fragments.length === 0) {\n // Nothing stored yet and nothing to store: no table, no work.\n return 0;\n }\n // `fragments` is non-empty here (the empty case returned above), so the\n // validation loop proved every fragment shares `fragments[0]`'s length —\n // which IS the dimension to establish. Read it straight from the first\n // fragment: no cast, no invariant-dependent narrowing.\n const established: number = fragments[0].vector.length;\n this._createTable(established);\n this._dimension = established;\n this._stmts = this._prepare();\n }\n this._stmts.replace(key, fragments);\n return fragments.length;\n }).withErrorFormat((e) => `fragment index: cannot add '${key}': ${e}`)\n );\n }\n\n /** {@inheritDoc IFragmentVectorIndex.remove} */\n public remove(target: IEdgeTarget): Promise<Result<IEdgeTarget>> {\n return Promise.resolve(\n captureResult(() => {\n // Idempotent: removing a target with no fragments (or before any add created\n // the table) still succeeds.\n if (this._stmts !== undefined) {\n this._stmts.deleteByTarget.run(edgeTargetKey(target));\n }\n return target;\n }).withErrorFormat((e) => `fragment index: cannot remove '${edgeTargetKey(target)}': ${e}`)\n );\n }\n\n /** {@inheritDoc IFragmentVectorIndex.query} */\n public query(\n vector: Float32Array,\n topK: number,\n maxPerRecord?: number\n ): Promise<Result<ReadonlyArray<IVectorQueryHit>>> {\n if (topK <= 0 || this._stmts === undefined) {\n return Promise.resolve(succeed([]));\n }\n if (vector.length !== this._dimension) {\n return Promise.resolve(\n fail(\n `fragment index: query dimension ${vector.length} does not match index dimension ${this._dimension}`\n )\n );\n }\n const stmts: IFragmentStatements = this._stmts;\n return Promise.resolve(\n captureResult<ReadonlyArray<IVectorQueryHit>>(() => {\n // With a per-record cap the topK winners may lie past the first topK rows (a\n // capped record's later fragments are skipped), so fetch the full ranked set\n // and apply the cap + topK cut here — exactly as the in-memory index does.\n // Uncapped, KNN's own `k = topK` is already the answer.\n const fetchK: number =\n maxPerRecord === undefined ? topK : Number((stmts.fragmentCount.get() as { c: number | bigint }).c);\n if (fetchK <= 0) {\n return [];\n }\n const rows: ReadonlyArray<IKnnRow> = stmts.query.all(\n SqliteVecFragmentIndex._toBlob(vector),\n fetchK\n ) as ReadonlyArray<IKnnRow>;\n // sqlite-vec returns rows ascending by distance (nearest first); score is\n // `1 - cosineDistance`, so this order is already descending score.\n const hits: IVectorQueryHit[] = [];\n const perRecord: Map<string, number> = new Map<string, number>();\n for (const row of rows) {\n if (hits.length >= topK) {\n break;\n }\n if (maxPerRecord !== undefined) {\n const used: number = perRecord.get(row.target_key) ?? 0;\n if (used >= maxPerRecord) {\n continue;\n }\n perRecord.set(row.target_key, used + 1);\n }\n const key: string = row.target_key;\n hits.push({\n target: SqliteVecFragmentIndex._parseKey(key),\n score: 1 - row.distance,\n ...SqliteVecFragmentIndex._toIdentity(row, key)\n });\n }\n return hits;\n }).withErrorFormat((e) => `fragment index: query failed: ${e}`)\n );\n }\n\n /**\n * Create the fragment `vec0` virtual table with the established dimension. The\n * auxiliary columns must stay in sync with `AUXILIARY_COLUMNS`, which\n * `create` compares against an existing table's stored DDL.\n */\n private _createTable(dimension: number): void {\n this._db.exec(\n `CREATE VIRTUAL TABLE IF NOT EXISTS \"${this._table}\" USING vec0(` +\n `target_key TEXT PARTITION KEY, embedding float[${dimension}] distance_metric=cosine, ` +\n `+start_off integer, +end_off integer, +fragment_id text)`\n );\n }\n\n /** Prepare the statements the index reuses. Requires the table to exist. */\n private _prepare(): IFragmentStatements {\n const del: BetterSqlite3.Statement = this._db.prepare(\n `DELETE FROM \"${this._table}\" WHERE target_key = ?`\n );\n const ins: BetterSqlite3.Statement = this._db.prepare(\n `INSERT INTO \"${this._table}\"(target_key, embedding, start_off, end_off, fragment_id) ` +\n `VALUES (?, ?, ?, ?, ?)`\n );\n // Whole-record replace: drop every prior fragment of the target, then insert the\n // new set, atomically. An empty set collapses to a pure delete.\n const replaceTxn: BetterSqlite3.Transaction<\n (key: string, fragments: ReadonlyArray<IEmbeddedFragment>) => void\n > = this._db.transaction((key: string, fragments: ReadonlyArray<IEmbeddedFragment>) => {\n del.run(key);\n for (const fragment of fragments) {\n ins.run(\n key,\n SqliteVecFragmentIndex._toBlob(fragment.vector),\n // vec0 typed columns reject a JS float; bind the offsets as integers. An\n // absent locator binds the pair as NULL — never a partial pair, so the read\n // side can treat a half-NULL pair as corruption rather than a legal shape.\n fragment.locator === undefined ? null : BigInt(fragment.locator.start),\n fragment.locator === undefined ? null : BigInt(fragment.locator.end),\n // Stored verbatim and never parsed; absent binds as NULL.\n fragment.fragmentId ?? null\n );\n }\n });\n return {\n deleteByTarget: del,\n replace: (key: string, fragments: ReadonlyArray<IEmbeddedFragment>): void => {\n replaceTxn(key, fragments);\n },\n query: this._db.prepare(\n `SELECT target_key, start_off, end_off, fragment_id, distance FROM \"${this._table}\" ` +\n `WHERE embedding MATCH ? AND k = ?`\n ),\n fragmentCount: this._db.prepare(`SELECT count(*) AS c FROM \"${this._table}\"`),\n recordCount: this._db.prepare(`SELECT count(DISTINCT target_key) AS c FROM \"${this._table}\"`)\n };\n }\n\n /**\n * Recover the established dimension of an existing fragment `vec0` table from its\n * stored `CREATE VIRTUAL TABLE` SQL (`float[<n>]`), after checking that the table's\n * auxiliary columns match `AUXILIARY_COLUMNS`. Returns `undefined` when the\n * table does not exist yet (a fresh database — dimension is set by the first add).\n *\n * Throws when a table of that name exists but is not a usable fragment index (a\n * mismatched auxiliary-column set, or no `vec0` embedding column); the caller runs\n * this inside `captureResult`, so it surfaces as a loud `Failure` from `create`.\n * The same stored DDL answers every one of those questions, so the checks cost\n * nothing extra.\n */\n private static _readExistingDimension(db: BetterSqlite3.Database, table: string): number | undefined {\n const row: { sql: string } | undefined = db\n .prepare(\"SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?\")\n .get(table) as { sql: string } | undefined;\n if (row === undefined) {\n return undefined;\n }\n SqliteVecFragmentIndex._verifyAuxiliaryColumns(row.sql, table);\n const match: RegExpMatchArray | null = row.sql.match(/float\\[(\\d+)\\]/);\n if (match === null) {\n // The auxiliary columns matched but there is no `float[<n>]` embedding column,\n // so this is not a usable fragment index table. Same remedy as a column\n // mismatch — and failing here beats handing back a dimensionless index whose\n // first add would `CREATE VIRTUAL TABLE IF NOT EXISTS` into a no-op.\n throw new Error(\n `existing table '${table}' has no vec0 embedding column, so it is not a usable fragment ` +\n `index table. Drop it (or pass a fresh tableName) and re-add every fragment.`\n );\n }\n return Number(match[1]);\n }\n\n /**\n * Compare an existing table's auxiliary columns against `AUXILIARY_COLUMNS`.\n *\n * `CREATE VIRTUAL TABLE IF NOT EXISTS` is a no-op against an existing table (SQLite\n * never compares schemas) and `vec0` has no `ALTER TABLE ADD COLUMN`, so a table\n * written by an earlier version of this package silently keeps its old columns and\n * only fails later — as an opaque `no such column` when the widened `INSERT` is\n * prepared. Detect it here instead and say what to do about it. Order is not\n * compared: every statement names its columns explicitly, so only the set matters.\n */\n private static _verifyAuxiliaryColumns(sql: string, table: string): void {\n const found: string[] = Array.from(sql.matchAll(AUXILIARY_COLUMN_RE), (m) => m[1]);\n const expected: ReadonlyArray<string> = AUXILIARY_COLUMNS;\n const matches: boolean =\n found.length === expected.length && expected.every((column) => found.includes(column));\n if (!matches) {\n throw new Error(\n `existing table '${table}' has auxiliary columns [${found.join(', ')}] but this index ` +\n `requires [${expected.join(', ')}] — it was written by a different version of ` +\n `@fgv/ts-agent-memory-sqlite-vec, or it is not a fragment index table at all. vec0 virtual ` +\n `tables cannot be altered in place, so this requires a drop-and-re-index: DROP TABLE ` +\n `\"${table}\" (or pass a fresh tableName) and re-add every fragment. Fragment vectors are ` +\n `re-derivable from the records, so this costs embedding time, never data.`\n );\n }\n }\n\n /**\n * Rebuild the identity fields of a hit from a persisted row, omitting each field\n * the stored fragment did not carry (so a hit is structurally identical to one this\n * index produced before `fragment_id` existed).\n *\n * A row carrying neither identity violates the write-side invariant and could not\n * be resolved by the caller, so it fails loudly instead of yielding an anonymous\n * hit.\n */\n private static _toIdentity(row: IKnnRow, key: string): FragmentIdentity {\n const locator: IFragmentLocator | undefined = SqliteVecFragmentIndex._toLocator(row, key);\n if (locator === undefined && row.fragment_id === null) {\n throw new Error(\n `fragment '${key}': row carries neither a locator nor a fragment id (corrupt persisted data)`\n );\n }\n return {\n ...(locator !== undefined ? { locator } : {}),\n ...(row.fragment_id !== null ? { fragmentId: row.fragment_id } : {})\n };\n }\n\n /**\n * Rebuild a fragment's locator from its persisted offsets, or `undefined` when the\n * fragment was stored without one (both offsets `NULL`).\n *\n * The pair is written all-or-nothing, so a half-`NULL` pair can only come from\n * corrupt / externally-edited data. Throw rather than coerce — `Number(null)` is\n * `0`, which would silently fabricate a span starting at the top of the body.\n */\n private static _toLocator(row: IKnnRow, key: string): IFragmentLocator | undefined {\n const start: number | bigint | null = row.start_off;\n const end: number | bigint | null = row.end_off;\n if (start === null && end === null) {\n return undefined;\n }\n if (start === null || end === null) {\n throw new Error(\n `fragment '${key}': locator has only one of its start/end offsets (corrupt persisted data)`\n );\n }\n return {\n start: SqliteVecFragmentIndex._toOffset(start, key),\n end: SqliteVecFragmentIndex._toOffset(end, key)\n };\n }\n\n /** Pack a `Float32Array` as the little-endian byte blob `vec0` stores. Copies, so the caller may reuse its buffer. */\n private static _toBlob(vector: Float32Array): Uint8Array {\n return new Uint8Array(Float32Array.from(vector).buffer);\n }\n\n /**\n * Reverse `edgeTargetKey` — the canonical key is `scope\\0id` with NUL excluded\n * from both components, so the first NUL splits it unambiguously. A key with no\n * NUL cannot have been written by `edgeTargetKey`; rather than fabricate a wrong\n * `(scope, id)` from corrupt / externally-edited table data, throw so the query\n * surfaces it as a loud `Failure`.\n */\n private static _parseKey(key: string): IEdgeTarget {\n const nul: number = key.indexOf('\\0');\n if (nul < 0) {\n throw new Error(`malformed target key '${key}': missing scope/id separator (corrupt persisted data)`);\n }\n return {\n scope: key.slice(0, nul) as unknown as MemoryScopeKey,\n id: key.slice(nul + 1) as unknown as MemoryId\n };\n }\n\n /**\n * Coerce a persisted locator offset to a plain `number`. `better-sqlite3` returns\n * integer columns as `bigint` under safe-integer mode, so an offset can arrive as\n * either; both narrow to `number` here. A value outside the safe-integer range\n * (only reachable via corrupt / externally-edited data — the index only ever\n * writes in-document offsets) throws rather than silently losing precision, so the\n * query surfaces it as a loud `Failure`.\n */\n private static _toOffset(value: number | bigint, key: string): number {\n const n: number = Number(value);\n if (!Number.isSafeInteger(n)) {\n throw new Error(\n `fragment '${key}': locator offset ${String(value)} is not a safe integer (corrupt persisted data)`\n );\n }\n return n;\n }\n}\n\n/** The prepared statements / helpers the fragment index reuses once its table exists. */\ninterface IFragmentStatements {\n readonly deleteByTarget: BetterSqlite3.Statement;\n readonly replace: (key: string, fragments: ReadonlyArray<IEmbeddedFragment>) => void;\n readonly query: BetterSqlite3.Statement;\n readonly fragmentCount: BetterSqlite3.Statement;\n readonly recordCount: BetterSqlite3.Statement;\n}\n"]}
1
+ {"version":3,"file":"sqliteVecFragmentIndex.js","sourceRoot":"","sources":["../../../src/packlets/sqlite-vec-index/sqliteVecFragmentIndex.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,IAAI,IAAI,aAAa,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAGL,aAAa,EACb,IAAI,EACJ,cAAc,EACd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,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAA8B;YACrD,EAAE,EAAE,GAAG,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,CAAwB;SAC9C,CAAC;IACJ,CAAC;IAED;;;;;;;OAOG;IACK,MAAM,CAAC,SAAS,CAAC,KAAsB,EAAE,GAAW;QAC1D,MAAM,CAAC,GAAW,MAAM,CAAC,KAAK,CAAC,CAAC;QAChC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,CAAC;YAC7B,MAAM,IAAI,KAAK,CACb,aAAa,GAAG,qBAAqB,MAAM,CAAC,KAAK,CAAC,iDAAiD,CACpG,CAAC;QACJ,CAAC;QACD,OAAO,CAAC,CAAC;IACX,CAAC;CACF","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nimport type BetterSqlite3 from 'better-sqlite3';\nimport { load as loadSqliteVec } from 'sqlite-vec';\nimport {\n DetailedResult,\n Result,\n captureResult,\n fail,\n failWithDetail,\n succeed,\n succeedWithDetail\n} from '@fgv/ts-utils';\nimport {\n FragmentEmbedder,\n IEdgeTarget,\n IEmbeddedFragment,\n IFragmentLocator,\n IFragmentVectorIndex,\n IFragmentVectorRebuildReport,\n IMemoryRecordListing,\n IMemoryRecordSource,\n ISkippedVectorRecord,\n IVectorQueryHit,\n IVectorRebuildOptions,\n Kind,\n IFragmentQueryOptions,\n MemoryId,\n MemoryScopeKey,\n edgeTargetKey\n} from '@fgv/ts-agent-memory';\nimport { invokeHook, tally, withRollbackNote } from './rebuildHelpers';\nimport { closeOwnedConnection, openOwnedConnection } from './connection';\nimport {\n ISqliteVecFragmentIndexCreateParams,\n ISqliteVecFragmentIndexHandle,\n ISqliteVecFragmentIndexOpenParams\n} from './model';\n\n/** Default name for the fragment `vec0` virtual table. */\nconst DEFAULT_TABLE_NAME: string = 'memory_fragments';\n\n/** Package-facing prefix for this class's failure messages. */\nconst LABEL: string = 'sqlite-vec fragment index';\n\n/** A simple SQL identifier — the only shape allowed for the table name (it is interpolated into DDL). */\nconst IDENTIFIER_RE: RegExp = /^[A-Za-z_][A-Za-z0-9_]*$/;\n\n/**\n * The auxiliary (`+`-prefixed) columns this version of the index writes. A table\n * created by an earlier version carries a different set; see\n * {@link SqliteVecFragmentIndex._readExistingDimension} for why that has to be\n * detected explicitly rather than migrated.\n */\nconst AUXILIARY_COLUMNS: ReadonlyArray<string> = ['start_off', 'end_off', 'fragment_id'];\n\n/**\n * Matches one `+name` auxiliary-column declaration in a `vec0` `CREATE VIRTUAL TABLE`\n * statement. Only ever consumed via `String.matchAll`, which iterates a clone rather\n * than advancing this instance's `lastIndex`, so the shared `/g` regex is reusable.\n */\nconst AUXILIARY_COLUMN_RE: RegExp = /\\+\\s*([A-Za-z_][A-Za-z0-9_]*)/g;\n\n/**\n * One KNN row as returned by the fragment `vec0` MATCH query. The offset columns are\n * typed `number | bigint` because `better-sqlite3` returns integer columns as\n * `bigint` when a consumer enables its safe-integer mode (`defaultSafeIntegers`);\n * {@link SqliteVecFragmentIndex._toOffset} coerces them to a plain `number` (and\n * fails loudly on an out-of-safe-range value) before they reach the public locator.\n * All three identity columns are nullable: a fragment stored without a locator has\n * `NULL` offsets, and one stored without a `fragmentId` has a `NULL` `fragment_id`.\n */\ninterface IKnnRow {\n readonly target_key: string;\n // eslint-disable-next-line @rushstack/no-new-null -- SQLite returns NULL (not undefined) for an absent locator offset\n readonly start_off: number | bigint | null;\n // eslint-disable-next-line @rushstack/no-new-null -- SQLite returns NULL (not undefined) for an absent locator offset\n readonly end_off: number | bigint | null;\n // eslint-disable-next-line @rushstack/no-new-null -- SQLite returns NULL (not undefined) for an absent fragment id\n readonly fragment_id: string | null;\n readonly distance: number;\n}\n\n/**\n * The identity fields of a fragment hit, in `IVectorQueryHit` shape: a field the\n * stored fragment did not carry is *absent*, never present-but-`undefined`, so a hit\n * for a fragment stored without a `fragmentId` is structurally identical to one this\n * index produced before `fragment_id` existed.\n */\ntype FragmentIdentity = Pick<IVectorQueryHit, 'locator' | 'fragmentId'>;\n\n/**\n * A persistent, `sqlite-vec`-backed `IFragmentVectorIndex` (from\n * `@fgv/ts-agent-memory`) — the fragment-granular sibling of\n * {@link SqliteVecVectorIndex}, and the **durable** counterpart to the in-memory\n * `InMemoryFragmentCosineIndex`.\n *\n * @remarks\n * Where {@link SqliteVecVectorIndex} keys one vector per record on a\n * `target_key` primary key, this index holds **many** vectors per record — one per\n * fragment — so it keys the `vec0` table on `target_key` as a **`PARTITION KEY`**\n * (many rows may share it) and stores each fragment's identity in three auxiliary\n * columns (`+start_off`, `+end_off`, `+fragment_id`) that ride alongside the vector\n * and are returned on query but never filtered — in particular `fragment_id` is\n * stored and returned verbatim, never parsed and never part of the query path. A\n * query is a brute-force `vec0` KNN scan across all partitions returning per-fragment\n * hits, each carrying its record `target` plus whichever identity fields the stored\n * fragment was added with (a fragment must carry at least one).\n *\n * **`vec0` schema changes require a drop-and-re-index.** A\n * `CREATE VIRTUAL TABLE IF NOT EXISTS` is a no-op against an existing table (SQLite\n * does not compare schemas) and `vec0` has no `ALTER TABLE ADD COLUMN`, so a database written by an\n * earlier version of this package keeps its old auxiliary columns. `create` detects\n * that by parsing the stored `CREATE VIRTUAL TABLE` SQL and fails with an actionable\n * message naming the expected and found columns, rather than letting a widened\n * `INSERT` surface an opaque `no such column` at statement-prepare time. There are no\n * in-place migrations: drop the table (or use a fresh `tableName`) and re-index.\n * Fragment vectors are re-derivable from the records, so this costs embedding time,\n * never data.\n *\n * Semantics match `InMemoryFragmentCosineIndex` exactly: `addFragments` is\n * whole-record-replace (a single transaction deletes every prior fragment of the\n * target, then inserts the new set), `remove` drops every fragment of a target,\n * and `query` applies the optional `maxPerRecord` cap **during selection, before\n * the topK cut** — so one long document cannot crowd others out. The dimension is\n * established by the first `addFragments` (the `vec0` column is fixed-width) and\n * recovered from the table schema when a persistent file is reopened; similarity is\n * cosine (`score = 1 - cosineDistance`), byte-identical to the in-memory index.\n * Large-N ANN indexing is explicitly out of scope, same regime as the record index.\n *\n * **Connection ownership depends on which factory you use.** With\n * {@link SqliteVecFragmentIndex.create} the `Database` is consumer-owned\n * (bring-your-own): this index loads the `sqlite-vec` extension onto it and\n * reads/writes the table, but never opens or closes the connection — and that is\n * the seam for backing this index and a record index with one connection. With\n * {@link SqliteVecFragmentIndex.open} this package opens the file itself and hands\n * back a handle carrying the disposer for the connection it created.\n * @public\n */\nexport class SqliteVecFragmentIndex implements IFragmentVectorIndex {\n private readonly _db: BetterSqlite3.Database;\n private readonly _table: string;\n /** The dimension of every stored fragment vector; `undefined` until the table exists. */\n private _dimension: number | undefined;\n /** Prepared statements; created once the table exists (established or recovered). */\n private _stmts: IFragmentStatements | undefined;\n\n private constructor(db: BetterSqlite3.Database, table: string, dimension: number | undefined) {\n this._db = db;\n this._table = table;\n this._dimension = dimension;\n this._stmts = dimension === undefined ? undefined : this._prepare();\n }\n\n /** The number of records that currently have at least one stored fragment. Zero before the first add. */\n public get recordCount(): number {\n if (this._stmts === undefined) {\n return 0;\n }\n // `Number(...)` narrows the count in case the consumer enabled better-sqlite3\n // safe-integer mode (which returns `count(*)` as a `bigint`).\n return Number((this._stmts.recordCount.get() as { c: number | bigint }).c);\n }\n\n /** The total number of fragments currently held across all records. Zero before the first add. */\n public get fragmentCount(): number {\n if (this._stmts === undefined) {\n return 0;\n }\n return Number((this._stmts.fragmentCount.get() as { c: number | bigint }).c);\n }\n\n /**\n * Family-convention factory. Loads the `sqlite-vec` extension onto the supplied\n * `better-sqlite3` connection and, if the fragment table already exists (a\n * reopened persistent file), verifies its auxiliary-column set matches this\n * version's and recovers its established dimension so no re-embedding is needed on\n * open.\n *\n * @param params - See {@link ISqliteVecFragmentIndexCreateParams}.\n * @returns `Success` with the index, or `Failure` if the table name is not a\n * simple identifier, the extension fails to load, or the existing table was\n * written by a version with a different auxiliary-column set (which requires a\n * drop-and-re-index — `vec0` cannot be altered in place).\n */\n public static create(params: ISqliteVecFragmentIndexCreateParams): Promise<Result<SqliteVecFragmentIndex>> {\n const table: string = params.tableName ?? DEFAULT_TABLE_NAME;\n if (!IDENTIFIER_RE.test(table)) {\n return Promise.resolve(\n fail(`sqlite-vec fragment index: table name '${table}' is not a simple SQL identifier`)\n );\n }\n return Promise.resolve(\n captureResult(() => {\n loadSqliteVec(params.database);\n const dimension: number | undefined = SqliteVecFragmentIndex._readExistingDimension(\n params.database,\n table\n );\n return new SqliteVecFragmentIndex(params.database, table, dimension);\n }).withErrorFormat((e) => `sqlite-vec fragment index: failed to initialize: ${e}`)\n );\n }\n\n /**\n * Path-based factory. Opens the database file itself and returns the index\n * together with a disposer for the connection it created.\n *\n * @remarks\n * The fragment-granular sibling of {@link SqliteVecVectorIndex.open}, and present\n * for the same reason: a consumer doing sub-document retrieval only would\n * otherwise still value-import `better-sqlite3` and hand-roll a `captureResult`\n * around a constructor that throws.\n *\n * **Use `create` instead when one connection must back both a fragment index and\n * a record index** — the intended shared-handle case. Two `open` calls on one path\n * give two independent connections, not a shared one.\n *\n * If initialization fails after the file is opened, the connection is closed\n * before returning, so a failed `open` does not leak the descriptor it created.\n * Should that close *itself* fail — the connection is then genuinely leaked — the\n * returned message says so rather than hiding it. That includes the\n * auxiliary-column mismatch failure, which is reported by `create` only after the\n * file is open.\n *\n * @param params - See {@link ISqliteVecFragmentIndexOpenParams}.\n * @returns `Success` with a {@link ISqliteVecFragmentIndexHandle}, or `Failure` if\n * the driver could not be loaded, the file could not be opened, the table name is\n * not a simple identifier, the extension fails to load, or the existing table was\n * written by a version with a different auxiliary-column set.\n */\n public static async open(\n params: ISqliteVecFragmentIndexOpenParams\n ): Promise<Result<ISqliteVecFragmentIndexHandle>> {\n return (await openOwnedConnection(params.path, LABEL)).thenOnSuccess(async (database) =>\n (await SqliteVecFragmentIndex.create({ database, tableName: params.tableName }))\n .onFailure((message) =>\n // This call opened the connection, so a failure to initialize on top of it\n // must not leave the file handle behind. A close that ALSO fails is said out\n // loud rather than swallowed — the same reasoning, and the same helper, as\n // `withRollbackNote`: silently discarding it would make the \"a failed open\n // leaks nothing\" guarantee untrue exactly when it stopped holding, with no\n // way for a caller to detect it.\n fail(withRollbackNote(message, closeOwnedConnection(database, LABEL)))\n )\n .onSuccess((index) =>\n succeed({\n index,\n close: () => closeOwnedConnection(database, LABEL)\n })\n )\n );\n }\n\n /** {@inheritDoc IFragmentVectorIndex.addFragments} */\n public addFragments(\n target: IEdgeTarget,\n fragments: ReadonlyArray<IEmbeddedFragment>\n ): Promise<Result<number>> {\n const key: string = edgeTargetKey(target);\n // Validate every fragment before touching the database, so a bad fragment never\n // leaves the record half-replaced or the dimension half-established (whole-record\n // replace is all-or-nothing). The effective dimension is the established one, or —\n // on a still-dimensionless index — the first fragment's length; it is committed\n // (via table creation) only once the whole batch validates.\n let dimension: number | undefined = this._dimension;\n for (const fragment of fragments) {\n if (fragment.vector.length === 0) {\n return Promise.resolve(fail(`fragment index: cannot add '${key}': empty fragment vector`));\n }\n // A fragment carrying neither identity cannot be resolved back to anything by a\n // consumer holding the hit — the same invariant `embeddedFragmentConverter`\n // enforces at the untyped boundary, re-checked here at the index seam.\n if (fragment.locator === undefined && fragment.fragmentId === undefined) {\n return Promise.resolve(\n fail(\n `fragment index: cannot add '${key}': fragment requires at least one of 'locator' or 'fragmentId'`\n )\n );\n }\n // Locator offsets are persisted as SQLite integers (bound via BigInt). Reject a\n // non-safe-integer offset up front with a clear message, rather than letting\n // `BigInt(nonInteger)` throw cryptically inside the write transaction OR storing\n // a value the read-side `_toOffset` guard would later reject on every query.\n // An absent locator persists as a NULL offset pair and skips the check.\n if (\n fragment.locator !== undefined &&\n (!Number.isSafeInteger(fragment.locator.start) || !Number.isSafeInteger(fragment.locator.end))\n ) {\n return Promise.resolve(\n fail(\n `fragment index: cannot add '${key}': locator [${fragment.locator.start}, ${fragment.locator.end}) offsets must be safe integers`\n )\n );\n }\n if (dimension === undefined) {\n dimension = fragment.vector.length;\n } else if (fragment.vector.length !== dimension) {\n return Promise.resolve(\n fail(\n `fragment index: cannot add '${key}': fragment dimension ${fragment.vector.length} does not match index dimension ${dimension}`\n )\n );\n }\n }\n return Promise.resolve(\n captureResult(() => {\n // A same-target re-author (or an empty batch) still needs the table to exist\n // to delete prior fragments; create it lazily on the first non-empty add.\n if (this._stmts === undefined) {\n if (fragments.length === 0) {\n // Nothing stored yet and nothing to store: no table, no work.\n return 0;\n }\n // `fragments` is non-empty here (the empty case returned above), so the\n // validation loop proved every fragment shares `fragments[0]`'s length —\n // which IS the dimension to establish. Read it straight from the first\n // fragment: no cast, no invariant-dependent narrowing.\n const established: number = fragments[0].vector.length;\n this._createTable(established);\n this._dimension = established;\n this._stmts = this._prepare();\n }\n this._stmts.replace(key, fragments);\n return fragments.length;\n }).withErrorFormat((e) => `fragment index: cannot add '${key}': ${e}`)\n );\n }\n\n /** {@inheritDoc IFragmentVectorIndex.remove} */\n public remove(target: IEdgeTarget): Promise<Result<IEdgeTarget>> {\n return Promise.resolve(\n captureResult(() => {\n // Idempotent: removing a target with no fragments (or before any add created\n // the table) still succeeds.\n if (this._stmts !== undefined) {\n this._stmts.deleteByTarget.run(edgeTargetKey(target));\n }\n return target;\n }).withErrorFormat((e) => `fragment index: cannot remove '${edgeTargetKey(target)}': ${e}`)\n );\n }\n\n /** {@inheritDoc IFragmentVectorIndex.has} */\n public has(target: IEdgeTarget): Promise<Result<boolean>> {\n return Promise.resolve(\n captureResult(() => {\n // Before any add has created the table there is nothing held — a truthful\n // `false`, matching `remove`'s idempotence and the zero counts.\n if (this._stmts === undefined) {\n return false;\n }\n return this._stmts.has.get(edgeTargetKey(target)) !== undefined;\n }).withErrorFormat((e) => `fragment index: cannot check '${edgeTargetKey(target)}': ${e}`)\n );\n }\n\n /** {@inheritDoc IFragmentVectorIndex.rebuild} */\n public async rebuild(\n source: IMemoryRecordSource,\n embed: FragmentEmbedder,\n options?: IVectorRebuildOptions\n ): Promise<DetailedResult<IFragmentVectorRebuildReport, IFragmentVectorRebuildReport>> {\n const lenient: boolean = (options?.onRecordError ?? 'fail') === 'skip';\n // `source` is consumer-supplied, so a throw or rejection becomes a `Failure`\n // here rather than escaping as an exception.\n const listed: Result<IMemoryRecordListing> = await invokeHook(() => source.list());\n if (listed.isFailure()) {\n // Deliberately BEFORE any clear, matching both siblings: a failed list is no\n // evidence about the fragments already held, and clearing here would destroy\n // a healthy PERSISTED index over a transient read error. No detail — there is\n // nothing this call disturbed to describe.\n return failWithDetail(`fragment index rebuild: failed to list records: ${listed.message}`);\n }\n const cleared: Result<true> = this._clear();\n if (cleared.isFailure()) {\n // Also nothing established: the table still holds whatever it held.\n return failWithDetail(`fragment index rebuild: failed to clear the index: ${cleared.message}`);\n }\n const indexed: Map<Kind, number> = new Map<Kind, number>();\n const fragments: Map<Kind, number> = new Map<Kind, number>();\n const declined: Map<Kind, number> = new Map<Kind, number>();\n const skipped: ISkippedVectorRecord[] = [];\n // Absent stays absent — only the source knows whether it filtered anything.\n const report = (): IFragmentVectorRebuildReport => ({\n indexed,\n fragments,\n declined,\n excluded: listed.value.excluded,\n skipped\n });\n for (const scoped of listed.value.records) {\n const kind: Kind = scoped.record.envelope.kind;\n // Capture-wrapped: an embedder that throws mid-loop would otherwise escape\n // past the `'fail'` rollback below, leaving this DURABLE table holding a\n // partial index that survives the process.\n const embedded: Result<ReadonlyArray<IEmbeddedFragment>> = await invokeHook(() => embed(scoped.record));\n if (embedded.isFailure()) {\n const error: string = `fragment index rebuild: embedding '${edgeTargetKey(scoped.target)}' failed: ${\n embedded.message\n }`;\n if (!lenient) {\n // A rollback that also fails is said out loud: the `'fail'` path\n // promises an empty index, and on a DURABLE table a botched rollback\n // survives the process.\n return failWithDetail(withRollbackNote(error, this._clear()), report());\n }\n skipped.push({ target: scoped.target, error });\n continue;\n }\n // An empty array is this lane's decline, and it is still WRITTEN — the\n // whole-record-replace is what clears any stale fragments.\n const added: Result<number> = await this.addFragments(scoped.target, embedded.value);\n if (added.isFailure()) {\n const error: string = `fragment index rebuild: ${added.message}`;\n if (!lenient) {\n return failWithDetail(withRollbackNote(error, this._clear()), report());\n }\n skipped.push({ target: scoped.target, error });\n continue;\n }\n if (added.value === 0) {\n tally(declined, kind);\n continue;\n }\n tally(indexed, kind);\n tally(fragments, kind, added.value);\n }\n return succeedWithDetail(report());\n }\n\n /**\n * **Empties the rows; does NOT release the table's declared dimension.** That\n * is a `vec0` constraint rather than a choice — the dimension is schema, and\n * there is no `ALTER TABLE` for it — so a rebuild at a new dimension fails\n * here where it would succeed on the in-memory sibling, which forgets its\n * dimension on reset. Changing dimension needs a drop-and-re-index; see the\n * note on `IVectorIndex.rebuild`. Tolerates a table that does not exist yet.\n */\n private _clear(): Result<true> {\n if (this._stmts === undefined) {\n return succeed(true);\n }\n // Capture-wrapped like every other statement path: a closed connection or an\n // I/O error is a `Failure`, not an exception out of a `Result`-returning method.\n return captureResult(() => this._db.prepare(`DELETE FROM \"${this._table}\"`).run()).onSuccess(() =>\n succeed(true)\n );\n }\n\n /** {@inheritDoc IFragmentVectorIndex.query} */\n public query(\n vector: Float32Array,\n topK: number,\n options?: IFragmentQueryOptions\n ): Promise<Result<ReadonlyArray<IVectorQueryHit>>> {\n const maxPerRecord: number | undefined = options?.maxPerRecord;\n const scope: MemoryScopeKey | undefined = options?.scope;\n const id: MemoryId | undefined = options?.id;\n if (topK <= 0 || this._stmts === undefined) {\n return Promise.resolve(succeed([]));\n }\n if (vector.length !== this._dimension) {\n return Promise.resolve(\n fail(\n `fragment index: query dimension ${vector.length} does not match index dimension ${this._dimension}`\n )\n );\n }\n const stmts: IFragmentStatements = this._stmts;\n return Promise.resolve(\n captureResult<ReadonlyArray<IVectorQueryHit>>(() => {\n // With a per-record cap the topK winners may lie past the first topK rows (a\n // capped record's later fragments are skipped), so fetch the full ranked set\n // and apply the cap + topK cut here — exactly as the in-memory index does.\n // Uncapped, KNN's own `k = topK` is already the answer.\n // A scope-only narrowing (a versioned kind's per-entity subtree) spans several\n // records, and `target_key` equality cannot express a prefix, so it is applied\n // over the full ranked set below. Correct either way — the caller's `topK` is\n // applied to the NARROWED set, which is the property that matters — but only\n // the single-record case gets the partition push-down.\n const recordKey: string | undefined =\n scope !== undefined && id !== undefined ? edgeTargetKey({ scope, id }) : undefined;\n // The cap forces the full ranked set ONLY when other records can fill from\n // behind a capped one. Under a single-record narrowing every row belongs to\n // that record, so the result is exactly `min(topK, maxPerRecord, fragments)`\n // and those are the first rows KNN returns — `k = topK` suffices, and\n // expanding to the table-wide `fragmentCount` would ask an\n // already-partition-restricted query for far more rows than it can use.\n const wholeSet: boolean =\n recordKey === undefined && (maxPerRecord !== undefined || scope !== undefined);\n const fetchK: number = wholeSet\n ? Number((stmts.fragmentCount.get() as { c: number | bigint }).c)\n : topK;\n if (fetchK <= 0) {\n return [];\n }\n const blob: Uint8Array = SqliteVecFragmentIndex._toBlob(vector);\n const rows: ReadonlyArray<IKnnRow> = (\n recordKey !== undefined\n ? stmts.queryScopedToRecord.all(blob, fetchK, recordKey)\n : stmts.query.all(blob, fetchK)\n ) as ReadonlyArray<IKnnRow>;\n // The scope prefix every record in `scope` shares. `edgeTargetKey` joins with\n // a NUL, so this cannot collide with a longer scope that merely starts the\n // same way.\n const scopePrefix: string | undefined =\n scope !== undefined && recordKey === undefined ? `${scope}\\0` : undefined;\n // sqlite-vec returns rows ascending by distance (nearest first); score is\n // `1 - cosineDistance`, so this order is already descending score.\n const hits: IVectorQueryHit[] = [];\n const perRecord: Map<string, number> = new Map<string, number>();\n for (const row of rows) {\n if (hits.length >= topK) {\n break;\n }\n if (scopePrefix !== undefined && !row.target_key.startsWith(scopePrefix)) {\n continue;\n }\n if (maxPerRecord !== undefined) {\n const used: number = perRecord.get(row.target_key) ?? 0;\n if (used >= maxPerRecord) {\n continue;\n }\n perRecord.set(row.target_key, used + 1);\n }\n const key: string = row.target_key;\n hits.push({\n target: SqliteVecFragmentIndex._parseKey(key),\n score: 1 - row.distance,\n ...SqliteVecFragmentIndex._toIdentity(row, key)\n });\n }\n return hits;\n }).withErrorFormat((e) => `fragment index: query failed: ${e}`)\n );\n }\n\n /**\n * Create the fragment `vec0` virtual table with the established dimension. The\n * auxiliary columns must stay in sync with `AUXILIARY_COLUMNS`, which\n * `create` compares against an existing table's stored DDL.\n */\n private _createTable(dimension: number): void {\n this._db.exec(\n `CREATE VIRTUAL TABLE IF NOT EXISTS \"${this._table}\" USING vec0(` +\n `target_key TEXT PARTITION KEY, embedding float[${dimension}] distance_metric=cosine, ` +\n `+start_off integer, +end_off integer, +fragment_id text)`\n );\n }\n\n /** Prepare the statements the index reuses. Requires the table to exist. */\n private _prepare(): IFragmentStatements {\n const del: BetterSqlite3.Statement = this._db.prepare(\n `DELETE FROM \"${this._table}\" WHERE target_key = ?`\n );\n const ins: BetterSqlite3.Statement = this._db.prepare(\n `INSERT INTO \"${this._table}\"(target_key, embedding, start_off, end_off, fragment_id) ` +\n `VALUES (?, ?, ?, ?, ?)`\n );\n // Whole-record replace: drop every prior fragment of the target, then insert the\n // new set, atomically. An empty set collapses to a pure delete.\n const replaceTxn: BetterSqlite3.Transaction<\n (key: string, fragments: ReadonlyArray<IEmbeddedFragment>) => void\n > = this._db.transaction((key: string, fragments: ReadonlyArray<IEmbeddedFragment>) => {\n del.run(key);\n for (const fragment of fragments) {\n ins.run(\n key,\n SqliteVecFragmentIndex._toBlob(fragment.vector),\n // vec0 typed columns reject a JS float; bind the offsets as integers. An\n // absent locator binds the pair as NULL — never a partial pair, so the read\n // side can treat a half-NULL pair as corruption rather than a legal shape.\n fragment.locator === undefined ? null : BigInt(fragment.locator.start),\n fragment.locator === undefined ? null : BigInt(fragment.locator.end),\n // Stored verbatim and never parsed; absent binds as NULL.\n fragment.fragmentId ?? null\n );\n }\n });\n return {\n deleteByTarget: del,\n replace: (key: string, fragments: ReadonlyArray<IEmbeddedFragment>): void => {\n replaceTxn(key, fragments);\n },\n query: this._db.prepare(\n `SELECT target_key, start_off, end_off, fragment_id, distance FROM \"${this._table}\" ` +\n `WHERE embedding MATCH ? AND k = ?`\n ),\n // The single-record narrowing constrains `target_key`, which is the table's\n // PARTITION KEY — so this is a partition-restricted KNN rather than a scan\n // plus a filter. That is the performance reason this narrowing belongs in the\n // library instead of in a bigger over-fetch on the caller's side.\n queryScopedToRecord: this._db.prepare(\n `SELECT target_key, start_off, end_off, fragment_id, distance FROM \"${this._table}\" ` +\n `WHERE embedding MATCH ? AND k = ? AND target_key = ?`\n ),\n fragmentCount: this._db.prepare(`SELECT count(*) AS c FROM \"${this._table}\"`),\n recordCount: this._db.prepare(`SELECT count(DISTINCT target_key) AS c FROM \"${this._table}\"`),\n // `LIMIT 1`: membership needs existence, not cardinality.\n has: this._db.prepare(`SELECT 1 FROM \"${this._table}\" WHERE target_key = ? LIMIT 1`)\n };\n }\n\n /**\n * Recover the established dimension of an existing fragment `vec0` table from its\n * stored `CREATE VIRTUAL TABLE` SQL (`float[<n>]`), after checking that the table's\n * auxiliary columns match `AUXILIARY_COLUMNS`. Returns `undefined` when the\n * table does not exist yet (a fresh database — dimension is set by the first add).\n *\n * Throws when a table of that name exists but is not a usable fragment index (a\n * mismatched auxiliary-column set, or no `vec0` embedding column); the caller runs\n * this inside `captureResult`, so it surfaces as a loud `Failure` from `create`.\n * The same stored DDL answers every one of those questions, so the checks cost\n * nothing extra.\n */\n private static _readExistingDimension(db: BetterSqlite3.Database, table: string): number | undefined {\n const row: { sql: string } | undefined = db\n .prepare(\"SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?\")\n .get(table) as { sql: string } | undefined;\n if (row === undefined) {\n return undefined;\n }\n SqliteVecFragmentIndex._verifyAuxiliaryColumns(row.sql, table);\n const match: RegExpMatchArray | null = row.sql.match(/float\\[(\\d+)\\]/);\n if (match === null) {\n // The auxiliary columns matched but there is no `float[<n>]` embedding column,\n // so this is not a usable fragment index table. Same remedy as a column\n // mismatch — and failing here beats handing back a dimensionless index whose\n // first add would `CREATE VIRTUAL TABLE IF NOT EXISTS` into a no-op.\n throw new Error(\n `existing table '${table}' has no vec0 embedding column, so it is not a usable fragment ` +\n `index table. Drop it (or pass a fresh tableName) and re-add every fragment.`\n );\n }\n return Number(match[1]);\n }\n\n /**\n * Compare an existing table's auxiliary columns against `AUXILIARY_COLUMNS`.\n *\n * `CREATE VIRTUAL TABLE IF NOT EXISTS` is a no-op against an existing table (SQLite\n * never compares schemas) and `vec0` has no `ALTER TABLE ADD COLUMN`, so a table\n * written by an earlier version of this package silently keeps its old columns and\n * only fails later — as an opaque `no such column` when the widened `INSERT` is\n * prepared. Detect it here instead and say what to do about it. Order is not\n * compared: every statement names its columns explicitly, so only the set matters.\n */\n private static _verifyAuxiliaryColumns(sql: string, table: string): void {\n const found: string[] = Array.from(sql.matchAll(AUXILIARY_COLUMN_RE), (m) => m[1]);\n const expected: ReadonlyArray<string> = AUXILIARY_COLUMNS;\n const matches: boolean =\n found.length === expected.length && expected.every((column) => found.includes(column));\n if (!matches) {\n throw new Error(\n `existing table '${table}' has auxiliary columns [${found.join(', ')}] but this index ` +\n `requires [${expected.join(', ')}] — it was written by a different version of ` +\n `@fgv/ts-agent-memory-sqlite-vec, or it is not a fragment index table at all. vec0 virtual ` +\n `tables cannot be altered in place, so this requires a drop-and-re-index: DROP TABLE ` +\n `\"${table}\" (or pass a fresh tableName) and re-add every fragment. Fragment vectors are ` +\n `re-derivable from the records, so this costs embedding time, never data.`\n );\n }\n }\n\n /**\n * Rebuild the identity fields of a hit from a persisted row, omitting each field\n * the stored fragment did not carry (so a hit is structurally identical to one this\n * index produced before `fragment_id` existed).\n *\n * A row carrying neither identity violates the write-side invariant and could not\n * be resolved by the caller, so it fails loudly instead of yielding an anonymous\n * hit.\n */\n private static _toIdentity(row: IKnnRow, key: string): FragmentIdentity {\n const locator: IFragmentLocator | undefined = SqliteVecFragmentIndex._toLocator(row, key);\n if (locator === undefined && row.fragment_id === null) {\n throw new Error(\n `fragment '${key}': row carries neither a locator nor a fragment id (corrupt persisted data)`\n );\n }\n return {\n ...(locator !== undefined ? { locator } : {}),\n ...(row.fragment_id !== null ? { fragmentId: row.fragment_id } : {})\n };\n }\n\n /**\n * Rebuild a fragment's locator from its persisted offsets, or `undefined` when the\n * fragment was stored without one (both offsets `NULL`).\n *\n * The pair is written all-or-nothing, so a half-`NULL` pair can only come from\n * corrupt / externally-edited data. Throw rather than coerce — `Number(null)` is\n * `0`, which would silently fabricate a span starting at the top of the body.\n */\n private static _toLocator(row: IKnnRow, key: string): IFragmentLocator | undefined {\n const start: number | bigint | null = row.start_off;\n const end: number | bigint | null = row.end_off;\n if (start === null && end === null) {\n return undefined;\n }\n if (start === null || end === null) {\n throw new Error(\n `fragment '${key}': locator has only one of its start/end offsets (corrupt persisted data)`\n );\n }\n return {\n start: SqliteVecFragmentIndex._toOffset(start, key),\n end: SqliteVecFragmentIndex._toOffset(end, key)\n };\n }\n\n /** Pack a `Float32Array` as the little-endian byte blob `vec0` stores. Copies, so the caller may reuse its buffer. */\n private static _toBlob(vector: Float32Array): Uint8Array {\n return new Uint8Array(Float32Array.from(vector).buffer);\n }\n\n /**\n * Reverse `edgeTargetKey` — the canonical key is `scope\\0id` with NUL excluded\n * from both components, so the first NUL splits it unambiguously. A key with no\n * NUL cannot have been written by `edgeTargetKey`; rather than fabricate a wrong\n * `(scope, id)` from corrupt / externally-edited table data, throw so the query\n * surfaces it as a loud `Failure`.\n */\n private static _parseKey(key: string): IEdgeTarget {\n const nul: number = key.indexOf('\\0');\n if (nul < 0) {\n throw new Error(`malformed target key '${key}': missing scope/id separator (corrupt persisted data)`);\n }\n return {\n scope: key.slice(0, nul) as unknown as MemoryScopeKey,\n id: key.slice(nul + 1) as unknown as MemoryId\n };\n }\n\n /**\n * Coerce a persisted locator offset to a plain `number`. `better-sqlite3` returns\n * integer columns as `bigint` under safe-integer mode, so an offset can arrive as\n * either; both narrow to `number` here. A value outside the safe-integer range\n * (only reachable via corrupt / externally-edited data — the index only ever\n * writes in-document offsets) throws rather than silently losing precision, so the\n * query surfaces it as a loud `Failure`.\n */\n private static _toOffset(value: number | bigint, key: string): number {\n const n: number = Number(value);\n if (!Number.isSafeInteger(n)) {\n throw new Error(\n `fragment '${key}': locator offset ${String(value)} is not a safe integer (corrupt persisted data)`\n );\n }\n return n;\n }\n}\n\n/** The prepared statements / helpers the fragment index reuses once its table exists. */\ninterface IFragmentStatements {\n /** KNN restricted to one record's partition; see `queryScopedToRecord` above. */\n readonly queryScopedToRecord: BetterSqlite3.Statement;\n readonly deleteByTarget: BetterSqlite3.Statement;\n readonly replace: (key: string, fragments: ReadonlyArray<IEmbeddedFragment>) => void;\n readonly query: BetterSqlite3.Statement;\n readonly fragmentCount: BetterSqlite3.Statement;\n readonly recordCount: BetterSqlite3.Statement;\n readonly has: BetterSqlite3.Statement;\n}\n"]}
@@ -3,38 +3,14 @@
3
3
  * SPDX-License-Identifier: MIT
4
4
  */
5
5
  import { load as loadSqliteVec } from 'sqlite-vec';
6
- import { captureAsyncResult, captureResult, fail, succeed } from '@fgv/ts-utils';
6
+ import { captureResult, fail, failWithDetail, succeed, succeedWithDetail } from '@fgv/ts-utils';
7
7
  import { edgeTargetKey } from '@fgv/ts-agent-memory';
8
- /**
9
- * Invoke a consumer-supplied hook that already returns a `Result`, converting a
10
- * synchronous throw or a promise rejection into a `Failure` rather than letting
11
- * it escape. `captureAsyncResult` wraps the hook's own `Result`, so the outcome
12
- * is flattened back to one level.
13
- *
14
- * @remarks
15
- * This is `@fgv/ts-utils`' own `_invokeDeferred` shape (see `mapResultsAsync`),
16
- * which is `@internal` there and so cannot be imported. `@fgv/ts-agent-memory`
17
- * carries an identical private copy for the in-memory index. Exporting a single
18
- * `AsyncDeferredResult`-invoking primitive from `ts-utils` is the right home and
19
- * is recorded in `docs/TECH_DEBT.md`; duplicating three lines twice is the
20
- * cheaper thing to do from inside this stream than widening it to a foundational
21
- * library.
22
- */
23
- async function invokeHook(hook) {
24
- return (await captureAsyncResult(hook)).onSuccess((inner) => inner);
25
- }
26
- /**
27
- * Compose the failure that aborted a rebuild with the outcome of the rollback
28
- * that followed it. A rollback that ALSO fails is worth saying out loud: the
29
- * `'fail'` path promises an empty index, and a caller that retries against a
30
- * table which is neither the old index nor empty is working from a state the
31
- * contract never described.
32
- */
33
- function withRollbackNote(error, rollback) {
34
- return rollback.isFailure() ? `${error} (rollback also failed: ${rollback.message})` : error;
35
- }
8
+ import { invokeHook, tally, withRollbackNote } from './rebuildHelpers';
9
+ import { closeOwnedConnection, openOwnedConnection } from './connection';
36
10
  /** Default name for the `vec0` virtual table. */
37
11
  const DEFAULT_TABLE_NAME = 'memory_vectors';
12
+ /** Package-facing prefix for this class's failure messages. */
13
+ const LABEL = 'sqlite-vec index';
38
14
  /** A simple SQL identifier — the only shape allowed for the table name (it is interpolated into DDL). */
39
15
  const IDENTIFIER_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
40
16
  /**
@@ -63,9 +39,13 @@ const IDENTIFIER_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
63
39
  * durable, appropriate for the same "thousands of records" regime the in-memory
64
40
  * index targets. Large-N ANN indexing is explicitly out of scope — see the README.
65
41
  *
66
- * The `better-sqlite3` `Database` is consumer-owned (bring-your-own): this index
67
- * loads the `sqlite-vec` extension onto it and reads/writes the table, but never
68
- * opens or closes the connection.
42
+ * **Connection ownership depends on which factory you use.** With
43
+ * {@link SqliteVecVectorIndex.create} the `Database` is consumer-owned
44
+ * (bring-your-own): this index loads the `sqlite-vec` extension onto it and
45
+ * reads/writes the table, but never opens or closes the connection — and that is
46
+ * the seam for backing a record index and a fragment index with one connection.
47
+ * With {@link SqliteVecVectorIndex.open} this package opens the file itself and
48
+ * hands back a handle carrying the disposer for the connection it created.
69
49
  * @public
70
50
  */
71
51
  export class SqliteVecVectorIndex {
@@ -80,7 +60,14 @@ export class SqliteVecVectorIndex {
80
60
  if (this._stmts === undefined) {
81
61
  return 0;
82
62
  }
83
- return this._stmts.count.get().c;
63
+ // `Number(...)` narrows the count in case the consumer enabled better-sqlite3
64
+ // safe-integer mode (`db.defaultSafeIntegers(true)`), which returns `count(*)`
65
+ // as a `bigint`. Without it a `bigint` leaks through a `number`-typed contract
66
+ // member — and now through `IIndexCoverage.indexSize`, which is also declared
67
+ // `number`, so the coverage report would carry a value of the wrong runtime
68
+ // type. `SqliteVecFragmentIndex`'s two counts have always converted; this one
69
+ // was the outlier.
70
+ return Number(this._stmts.count.get().c);
84
71
  }
85
72
  /**
86
73
  * Family-convention factory. Loads the `sqlite-vec` extension onto the supplied
@@ -104,6 +91,46 @@ export class SqliteVecVectorIndex {
104
91
  return new SqliteVecVectorIndex(params.database, table, dimension);
105
92
  }).withErrorFormat((e) => `sqlite-vec index: failed to initialize: ${e}`));
106
93
  }
94
+ /**
95
+ * Path-based factory. Opens the database file itself and returns the index
96
+ * together with a disposer for the connection it created.
97
+ *
98
+ * @remarks
99
+ * The convenience over {@link SqliteVecVectorIndex.create} is that the consumer
100
+ * neither value-imports `better-sqlite3` nor re-establishes `Result` discipline
101
+ * around a constructor that throws — this is the one place the package leaked its
102
+ * own dependency into consumer source.
103
+ *
104
+ * **Use `create` instead when one connection must back more than one index** (a
105
+ * record index and a fragment index in the same file, the intended shared-handle
106
+ * case). Two `open` calls on one path give two independent connections, not a
107
+ * shared one.
108
+ *
109
+ * If initialization fails after the file is opened, the connection is closed
110
+ * before returning, so a failed `open` does not leak the descriptor it created.
111
+ * Should that close *itself* fail — the connection is then genuinely leaked — the
112
+ * returned message says so rather than hiding it.
113
+ *
114
+ * @param params - See {@link ISqliteVecVectorIndexOpenParams}.
115
+ * @returns `Success` with a {@link ISqliteVecVectorIndexHandle}, or `Failure` if
116
+ * the driver could not be loaded, the file could not be opened, the table name is
117
+ * not a simple identifier, or the extension fails to load.
118
+ */
119
+ static async open(params) {
120
+ return (await openOwnedConnection(params.path, LABEL)).thenOnSuccess(async (database) => (await SqliteVecVectorIndex.create({ database, tableName: params.tableName }))
121
+ .onFailure((message) =>
122
+ // This call opened the connection, so a failure to initialize on top of it
123
+ // must not leave the file handle behind. A close that ALSO fails is said out
124
+ // loud rather than swallowed — the same reasoning, and the same helper, as
125
+ // `withRollbackNote`: silently discarding it would make the "a failed open
126
+ // leaks nothing" guarantee untrue exactly when it stopped holding, with no
127
+ // way for a caller to detect it.
128
+ fail(withRollbackNote(message, closeOwnedConnection(database, LABEL))))
129
+ .onSuccess((index) => succeed({
130
+ index,
131
+ close: () => closeOwnedConnection(database, LABEL)
132
+ })));
133
+ }
107
134
  /** {@inheritDoc IVectorIndex.add} */
108
135
  add(target, vector) {
109
136
  const key = edgeTargetKey(target);
@@ -123,6 +150,18 @@ export class SqliteVecVectorIndex {
123
150
  return key;
124
151
  }).withErrorFormat((e) => `vector index: cannot add '${key}': ${e}`));
125
152
  }
153
+ /** {@inheritDoc IVectorIndex.has} */
154
+ has(target) {
155
+ return Promise.resolve(captureResult(() => {
156
+ // Before any add has created the table there is nothing held, which is a
157
+ // truthful `false` rather than an error — same posture as `remove`'s
158
+ // idempotence and `size`'s zero.
159
+ if (this._stmts === undefined) {
160
+ return false;
161
+ }
162
+ return this._stmts.has.get(edgeTargetKey(target)) !== undefined;
163
+ }).withErrorFormat((e) => `vector index: cannot check '${edgeTargetKey(target)}': ${e}`));
164
+ }
126
165
  /** {@inheritDoc IVectorIndex.remove} */
127
166
  remove(target) {
128
167
  return Promise.resolve(captureResult(() => {
@@ -157,16 +196,27 @@ export class SqliteVecVectorIndex {
157
196
  // Deliberately BEFORE any clear: a failed list is no evidence about the
158
197
  // vectors already held, and no re-embedding has been attempted, so there is
159
198
  // no half-rebuilt state to protect against. Clearing here would destroy a
160
- // healthy persisted index over a transient read error.
161
- return fail(`vector index rebuild: failed to list records: ${listed.message}`);
199
+ // healthy persisted index over a transient read error. No report either, for
200
+ // the same reason there is nothing this call disturbed to describe.
201
+ return failWithDetail(`vector index rebuild: failed to list records: ${listed.message}`);
162
202
  }
163
203
  const cleared = this._clear();
164
204
  if (cleared.isFailure()) {
165
- return fail(`vector index rebuild: failed to clear the index: ${cleared.message}`);
205
+ // Also nothing established: the table still holds whatever it held.
206
+ return failWithDetail(`vector index rebuild: failed to clear the index: ${cleared.message}`);
166
207
  }
167
- let declined = 0;
208
+ const indexed = new Map();
209
+ const declined = new Map();
168
210
  const skipped = [];
169
- for (const scoped of listed.value) {
211
+ // Absent stays absent — only the source knows whether it filtered anything.
212
+ const report = () => ({
213
+ indexed,
214
+ declined,
215
+ excluded: listed.value.excluded,
216
+ skipped
217
+ });
218
+ for (const scoped of listed.value.records) {
219
+ const kind = scoped.record.envelope.kind;
170
220
  // Likewise capture-wrapped: an embedder that throws mid-loop would
171
221
  // otherwise escape past the `'fail'` rollback below, leaving this DURABLE
172
222
  // table holding a partial index that survives the process.
@@ -174,34 +224,38 @@ export class SqliteVecVectorIndex {
174
224
  if (embedded.isFailure()) {
175
225
  const error = `vector index rebuild: embedding '${edgeTargetKey(scoped.target)}' failed: ${embedded.message}`;
176
226
  if (!lenient) {
177
- return fail(withRollbackNote(error, this._clear()));
227
+ return failWithDetail(withRollbackNote(error, this._clear()), report());
178
228
  }
179
229
  skipped.push({ target: scoped.target, error });
180
230
  continue;
181
231
  }
182
232
  if (embedded.value === undefined) {
183
- declined++;
233
+ tally(declined, kind);
184
234
  continue;
185
235
  }
186
236
  const added = await this.add(scoped.target, embedded.value);
187
237
  if (added.isFailure()) {
188
238
  const error = `vector index rebuild: ${added.message}`;
189
239
  if (!lenient) {
190
- return fail(withRollbackNote(error, this._clear()));
240
+ return failWithDetail(withRollbackNote(error, this._clear()), report());
191
241
  }
192
242
  skipped.push({ target: scoped.target, error });
243
+ continue;
193
244
  }
245
+ // Tallied in the loop rather than read back off `size` at the end. That
246
+ // `COUNT` was also the only fallible step in assembling the report, so the
247
+ // per-kind tally removes a failure path as well as a rounding of the answer.
248
+ tally(indexed, kind);
194
249
  }
195
- return captureResult(() => this.size)
196
- .withErrorFormat((msg) => `vector index rebuild: failed to count the rebuilt index: ${msg}`)
197
- .onSuccess((indexed) => succeed({ indexed, declined, skipped }));
250
+ return succeedWithDetail(report());
198
251
  }
199
252
  /**
200
- * Empty the table. Deliberately does NOT drop it or forget the established
201
- * dimension: the `vec0` table's dimension is fixed at creation and a re-embed at
202
- * a different dimension needs a drop-and-re-index, which is a consumer decision
203
- * (see the package README on `vec0` schema changes), not something a rebuild
204
- * should do silently.
253
+ * **Empties the rows; does NOT release the table's declared dimension.** That
254
+ * is a `vec0` constraint rather than a choice the dimension is schema, and
255
+ * there is no `ALTER TABLE` for it so a rebuild at a new dimension fails
256
+ * here where it would succeed on the in-memory sibling, which forgets its
257
+ * dimension on reset. Changing dimension needs a drop-and-re-index; see the
258
+ * note on `IVectorIndex.rebuild`.
205
259
  */
206
260
  _clear() {
207
261
  if (this._stmts === undefined) {
@@ -251,7 +305,10 @@ export class SqliteVecVectorIndex {
251
305
  replaceTxn(key, blob);
252
306
  },
253
307
  query: this._db.prepare(`SELECT target_key, distance FROM "${this._table}" WHERE embedding MATCH ? AND k = ?`),
254
- count: this._db.prepare(`SELECT count(*) AS c FROM "${this._table}"`)
308
+ count: this._db.prepare(`SELECT count(*) AS c FROM "${this._table}"`),
309
+ // `LIMIT 1` rather than a count: membership needs existence, not cardinality,
310
+ // and vec0 can stop at the first row.
311
+ has: this._db.prepare(`SELECT 1 FROM "${this._table}" WHERE target_key = ? LIMIT 1`)
255
312
  };
256
313
  }
257
314
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"sqliteVecVectorIndex.js","sourceRoot":"","sources":["../../../src/packlets/sqlite-vec-index/sqliteVecVectorIndex.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,IAAI,IAAI,aAAa,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAU,kBAAkB,EAAE,aAAa,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACzF,OAAO,EAYL,aAAa,EACd,MAAM,sBAAsB,CAAC;AAG9B;;;;;;;;;;;;;;GAcG;AACH,KAAK,UAAU,UAAU,CAAI,IAA8B;IACzD,OAAO,CAAC,MAAM,kBAAkB,CAA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W,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACtC,OAAO;YACL,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAA8B;YACrD,EAAE,EAAE,GAAG,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,CAAwB;SAC9C,CAAC;IACJ,CAAC;CACF","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nimport type BetterSqlite3 from 'better-sqlite3';\nimport { load as loadSqliteVec } from 'sqlite-vec';\nimport { Result, captureAsyncResult, captureResult, fail, succeed } from '@fgv/ts-utils';\nimport {\n IEdgeTarget,\n IMemoryRecordSource,\n IScopedMemoryRecord,\n ISkippedVectorRecord,\n IVectorIndex,\n IVectorQueryHit,\n IVectorRebuildOptions,\n IVectorRebuildReport,\n MemoryEmbedder,\n MemoryId,\n MemoryScopeKey,\n edgeTargetKey\n} from '@fgv/ts-agent-memory';\nimport { ISqliteVecVectorIndexCreateParams } from './model';\n\n/**\n * Invoke a consumer-supplied hook that already returns a `Result`, converting a\n * synchronous throw or a promise rejection into a `Failure` rather than letting\n * it escape. `captureAsyncResult` wraps the hook's own `Result`, so the outcome\n * is flattened back to one level.\n *\n * @remarks\n * This is `@fgv/ts-utils`' own `_invokeDeferred` shape (see `mapResultsAsync`),\n * which is `@internal` there and so cannot be imported. `@fgv/ts-agent-memory`\n * carries an identical private copy for the in-memory index. Exporting a single\n * `AsyncDeferredResult`-invoking primitive from `ts-utils` is the right home and\n * is recorded in `docs/TECH_DEBT.md`; duplicating three lines twice is the\n * cheaper thing to do from inside this stream than widening it to a foundational\n * library.\n */\nasync function invokeHook<T>(hook: () => Promise<Result<T>>): Promise<Result<T>> {\n return (await captureAsyncResult(hook)).onSuccess((inner) => inner);\n}\n\n/**\n * Compose the failure that aborted a rebuild with the outcome of the rollback\n * that followed it. A rollback that ALSO fails is worth saying out loud: the\n * `'fail'` path promises an empty index, and a caller that retries against a\n * table which is neither the old index nor empty is working from a state the\n * contract never described.\n */\nfunction withRollbackNote(error: string, rollback: Result<true>): string {\n return rollback.isFailure() ? `${error} (rollback also failed: ${rollback.message})` : error;\n}\n\n/** Default name for the `vec0` virtual table. */\nconst DEFAULT_TABLE_NAME: string = 'memory_vectors';\n\n/** A simple SQL identifier — the only shape allowed for the table name (it is interpolated into DDL). */\nconst IDENTIFIER_RE: RegExp = /^[A-Za-z_][A-Za-z0-9_]*$/;\n\n/** One KNN row as returned by the `vec0` MATCH query. */\ninterface IKnnRow {\n readonly target_key: string;\n readonly distance: number;\n}\n\n/**\n * A persistent, `sqlite-vec`-backed `IVectorIndex` for `@fgv/ts-agent-memory`.\n *\n * @remarks\n * This is the **durable** counterpart to the in-memory `InMemoryCosineIndex`:\n * embeddings live in a `sqlite-vec` `vec0` virtual table inside a `better-sqlite3`\n * database, so they survive a process restart. A consumer that wires this index\n * into `FileTreeMemoryStore` (instead of the in-memory index) opens an existing\n * vault **without re-embedding it** — the vectors are already on disk. New writes\n * still flow through the store's incremental embed-on-write path; there is no core\n * store change.\n *\n * The index is keyed by the canonical `edgeTargetKey` of each record's\n * scope-qualified `(scope, id)` address (a `TEXT PRIMARY KEY` on the `vec0` table),\n * so two records that share a filename stem across scopes never collide. The\n * dimension is established by the first `add` (the `vec0` column is fixed-width) and\n * recovered from the table schema when a persistent file is reopened; every later\n * `add`/`query` must match it or fail loudly, exactly as the in-memory index does.\n * Similarity is cosine (`distance_metric=cosine`): the returned `score` is\n * `1 - cosineDistance`, i.e. cosine similarity in `[-1, 1]`, higher = more similar —\n * byte-for-byte the same scoring contract as `InMemoryCosineIndex`.\n *\n * Query is a brute-force `vec0` KNN scan (not an ANN structure): correct and\n * durable, appropriate for the same \"thousands of records\" regime the in-memory\n * index targets. Large-N ANN indexing is explicitly out of scope — see the README.\n *\n * The `better-sqlite3` `Database` is consumer-owned (bring-your-own): this index\n * loads the `sqlite-vec` extension onto it and reads/writes the table, but never\n * opens or closes the connection.\n * @public\n */\nexport class SqliteVecVectorIndex implements IVectorIndex {\n private readonly _db: BetterSqlite3.Database;\n private readonly _table: string;\n /** The dimension of every stored vector; `undefined` until the table exists (first `add` or a reopened non-empty file). */\n private _dimension: number | undefined;\n /** Prepared statements; created once the table exists (established or recovered). */\n private _stmts: ISqliteVecStatements | undefined;\n\n private constructor(db: BetterSqlite3.Database, table: string, dimension: number | undefined) {\n this._db = db;\n this._table = table;\n this._dimension = dimension;\n this._stmts = dimension === undefined ? undefined : this._prepare();\n }\n\n /** The number of vectors currently held. Zero before the first `add`. */\n public get size(): number {\n if (this._stmts === undefined) {\n return 0;\n }\n return (this._stmts.count.get() as { c: number }).c;\n }\n\n /**\n * Family-convention factory. Loads the `sqlite-vec` extension onto the supplied\n * `better-sqlite3` connection and, if the vector table already exists (a reopened\n * persistent file), recovers its established dimension so no re-embedding is\n * needed on open.\n *\n * @param params - See {@link ISqliteVecVectorIndexCreateParams}.\n * @returns `Success` with the index, or `Failure` if the table name is not a\n * simple identifier or the extension fails to load.\n */\n public static create(params: ISqliteVecVectorIndexCreateParams): Promise<Result<SqliteVecVectorIndex>> {\n const table: string = params.tableName ?? DEFAULT_TABLE_NAME;\n if (!IDENTIFIER_RE.test(table)) {\n return Promise.resolve(fail(`sqlite-vec index: table name '${table}' is not a simple SQL identifier`));\n }\n return Promise.resolve(\n captureResult(() => {\n loadSqliteVec(params.database);\n const dimension: number | undefined = SqliteVecVectorIndex._readExistingDimension(\n params.database,\n table\n );\n return new SqliteVecVectorIndex(params.database, table, dimension);\n }).withErrorFormat((e) => `sqlite-vec index: failed to initialize: ${e}`)\n );\n }\n\n /** {@inheritDoc IVectorIndex.add} */\n public add(target: IEdgeTarget, vector: Float32Array): Promise<Result<string>> {\n const key: string = edgeTargetKey(target);\n if (vector.length === 0) {\n return Promise.resolve(fail(`vector index: cannot add '${key}': empty vector`));\n }\n if (this._dimension !== undefined && vector.length !== this._dimension) {\n return Promise.resolve(\n fail(\n `vector index: cannot add '${key}': dimension ${vector.length} does not match index dimension ${this._dimension}`\n )\n );\n }\n return Promise.resolve(\n captureResult(() => {\n if (this._stmts === undefined) {\n this._createTable(vector.length);\n this._dimension = vector.length;\n this._stmts = this._prepare();\n }\n this._stmts.replace(key, SqliteVecVectorIndex._toBlob(vector));\n return key;\n }).withErrorFormat((e) => `vector index: cannot add '${key}': ${e}`)\n );\n }\n\n /** {@inheritDoc IVectorIndex.remove} */\n public remove(target: IEdgeTarget): Promise<Result<IEdgeTarget>> {\n return Promise.resolve(\n captureResult(() => {\n // Idempotent: removing a target with no embedding (or before any `add`\n // created the table) still succeeds.\n if (this._stmts !== undefined) {\n this._stmts.delete.run(edgeTargetKey(target));\n }\n return target;\n }).withErrorFormat((e) => `vector index: cannot remove '${edgeTargetKey(target)}': ${e}`)\n );\n }\n\n /**\n * Re-embed every record from `source` and rebuild the persisted index — see\n * `IVectorIndex.rebuild` for the mode semantics, which this implementation\n * matches exactly.\n *\n * @remarks\n * **Not atomic, and cannot be.** `better-sqlite3` transactions are synchronous,\n * so one cannot span the `await embed(...)` calls this loop makes — unlike\n * {@link SqliteVecVectorIndex.add}, which wraps its delete-then-insert. The\n * `'fail'` / `'skip'` modes therefore cover only failures JavaScript can catch:\n * a process kill mid-rebuild leaves the table holding neither the old index nor\n * the complete new one, and the remedy is to run `rebuild` again.\n */\n public async rebuild(\n source: IMemoryRecordSource,\n embed: MemoryEmbedder,\n options?: IVectorRebuildOptions\n ): Promise<Result<IVectorRebuildReport>> {\n const lenient: boolean = (options?.onRecordError ?? 'fail') === 'skip';\n // `source` is consumer-supplied, so a throw or rejection becomes a `Failure`\n // here rather than escaping as an exception.\n const listed: Result<ReadonlyArray<IScopedMemoryRecord>> = await invokeHook(() => source.list());\n if (listed.isFailure()) {\n // Deliberately BEFORE any clear: a failed list is no evidence about the\n // vectors already held, and no re-embedding has been attempted, so there is\n // no half-rebuilt state to protect against. Clearing here would destroy a\n // healthy persisted index over a transient read error.\n return fail(`vector index rebuild: failed to list records: ${listed.message}`);\n }\n const cleared: Result<true> = this._clear();\n if (cleared.isFailure()) {\n return fail(`vector index rebuild: failed to clear the index: ${cleared.message}`);\n }\n let declined: number = 0;\n const skipped: ISkippedVectorRecord[] = [];\n for (const scoped of listed.value) {\n // Likewise capture-wrapped: an embedder that throws mid-loop would\n // otherwise escape past the `'fail'` rollback below, leaving this DURABLE\n // table holding a partial index that survives the process.\n const embedded: Result<Float32Array | undefined> = await invokeHook(() => embed(scoped.record));\n if (embedded.isFailure()) {\n const error: string = `vector index rebuild: embedding '${edgeTargetKey(scoped.target)}' failed: ${\n embedded.message\n }`;\n if (!lenient) {\n return fail(withRollbackNote(error, this._clear()));\n }\n skipped.push({ target: scoped.target, error });\n continue;\n }\n if (embedded.value === undefined) {\n declined++;\n continue;\n }\n const added: Result<string> = await this.add(scoped.target, embedded.value);\n if (added.isFailure()) {\n const error: string = `vector index rebuild: ${added.message}`;\n if (!lenient) {\n return fail(withRollbackNote(error, this._clear()));\n }\n skipped.push({ target: scoped.target, error });\n }\n }\n return captureResult(() => this.size)\n .withErrorFormat((msg) => `vector index rebuild: failed to count the rebuilt index: ${msg}`)\n .onSuccess((indexed) => succeed({ indexed, declined, skipped }));\n }\n\n /**\n * Empty the table. Deliberately does NOT drop it or forget the established\n * dimension: the `vec0` table's dimension is fixed at creation and a re-embed at\n * a different dimension needs a drop-and-re-index, which is a consumer decision\n * (see the package README on `vec0` schema changes), not something a rebuild\n * should do silently.\n */\n private _clear(): Result<true> {\n if (this._stmts === undefined) {\n return succeed(true);\n }\n // Capture-wrapped like `add` / `remove` / `query`: a closed connection or an\n // I/O error here is a `Failure`, not an exception thrown out of a method\n // whose signature promises a `Result`.\n return captureResult(() => this._db.prepare(`DELETE FROM \"${this._table}\"`).run()).onSuccess(() =>\n succeed(true)\n );\n }\n\n /** {@inheritDoc IVectorIndex.query} */\n public query(vector: Float32Array, topK: number): Promise<Result<ReadonlyArray<IVectorQueryHit>>> {\n if (topK <= 0 || this._stmts === undefined) {\n return Promise.resolve(succeed([]));\n }\n if (vector.length !== this._dimension) {\n return Promise.resolve(\n fail(\n `vector index: query dimension ${vector.length} does not match index dimension ${this._dimension}`\n )\n );\n }\n return Promise.resolve(\n captureResult<ReadonlyArray<IVectorQueryHit>>(() => {\n const rows: ReadonlyArray<IKnnRow> = this._stmts!.query.all(\n SqliteVecVectorIndex._toBlob(vector),\n topK\n ) as ReadonlyArray<IKnnRow>;\n // sqlite-vec returns rows in ascending distance (nearest first); score is\n // `1 - cosineDistance` = cosine similarity, so descending score is preserved.\n return rows.map((row) => ({\n target: SqliteVecVectorIndex._parseKey(row.target_key),\n score: 1 - row.distance\n }));\n }).withErrorFormat((e) => `vector index: query failed: ${e}`)\n );\n }\n\n /** Create the `vec0` virtual table with the established dimension. */\n private _createTable(dimension: number): void {\n this._db.exec(\n `CREATE VIRTUAL TABLE IF NOT EXISTS \"${this._table}\" USING vec0(` +\n `target_key TEXT PRIMARY KEY, embedding float[${dimension}] distance_metric=cosine)`\n );\n }\n\n /** Prepare the statements the index reuses. Requires the table to exist. */\n private _prepare(): ISqliteVecStatements {\n const del: BetterSqlite3.Statement = this._db.prepare(\n `DELETE FROM \"${this._table}\" WHERE target_key = ?`\n );\n const ins: BetterSqlite3.Statement = this._db.prepare(\n `INSERT INTO \"${this._table}\"(target_key, embedding) VALUES (?, ?)`\n );\n // vec0 rejects INSERT OR REPLACE on a TEXT primary key, so replace is a\n // delete-then-insert inside a single transaction.\n const replaceTxn: BetterSqlite3.Transaction<(key: string, blob: Uint8Array) => void> =\n this._db.transaction((key: string, blob: Uint8Array) => {\n del.run(key);\n ins.run(key, blob);\n });\n return {\n delete: del,\n replace: (key: string, blob: Uint8Array): void => {\n replaceTxn(key, blob);\n },\n query: this._db.prepare(\n `SELECT target_key, distance FROM \"${this._table}\" WHERE embedding MATCH ? AND k = ?`\n ),\n count: this._db.prepare(`SELECT count(*) AS c FROM \"${this._table}\"`)\n };\n }\n\n /**\n * Recover the established dimension of an existing `vec0` table from its stored\n * `CREATE VIRTUAL TABLE` SQL (`float[<n>]`). Returns `undefined` when the table\n * does not exist yet (a fresh database — dimension is set by the first `add`).\n */\n private static _readExistingDimension(db: BetterSqlite3.Database, table: string): number | undefined {\n const row: { sql: string } | undefined = db\n .prepare(\"SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?\")\n .get(table) as { sql: string } | undefined;\n if (row === undefined) {\n return undefined;\n }\n const match: RegExpMatchArray | null = row.sql.match(/float\\[(\\d+)\\]/);\n return match === null ? undefined : Number(match[1]);\n }\n\n /** Pack a `Float32Array` as the little-endian byte blob `vec0` stores. Copies, so the caller may reuse its buffer. */\n private static _toBlob(vector: Float32Array): Uint8Array {\n return new Uint8Array(Float32Array.from(vector).buffer);\n }\n\n /**\n * Reverse `edgeTargetKey` — the canonical key is `scope\\0id` with NUL\n * excluded from both components, so the first NUL splits it unambiguously.\n */\n private static _parseKey(key: string): IEdgeTarget {\n const nul: number = key.indexOf('\\0');\n return {\n scope: key.slice(0, nul) as unknown as MemoryScopeKey,\n id: key.slice(nul + 1) as unknown as MemoryId\n };\n }\n}\n\n/** The prepared statements / helpers the index reuses once its table exists. */\ninterface ISqliteVecStatements {\n readonly delete: BetterSqlite3.Statement;\n readonly replace: (key: string, blob: Uint8Array) => void;\n readonly query: BetterSqlite3.Statement;\n readonly count: BetterSqlite3.Statement;\n}\n"]}
1
+ {"version":3,"file":"sqliteVecVectorIndex.js","sourceRoot":"","sources":["../../../src/packlets/sqlite-vec-index/sqliteVecVectorIndex.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,IAAI,IAAI,aAAa,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAGL,aAAa,EACb,IAAI,EACJ,cAAc,EACd,OAAO,EACP,iBAAiB,EAClB,MAAM,eAAe,CAAC;AACvB,OAAO,EAaL,aAAa,EACd,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,kB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AG,CAAC,GAAG,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAC;QACvE,OAAO,KAAK,KAAK,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IACvD,CAAC;IAED,sHAAsH;IAC9G,MAAM,CAAC,OAAO,CAAC,MAAoB;QACzC,OAAO,IAAI,UAAU,CAAC,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;IAC1D,CAAC;IAED;;;OAGG;IACK,MAAM,CAAC,SAAS,CAAC,GAAW;QAClC,MAAM,GAAG,GAAW,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACtC,OAAO;YACL,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAA8B;YACrD,EAAE,EAAE,GAAG,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,CAAwB;SAC9C,CAAC;IACJ,CAAC;CACF","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nimport type BetterSqlite3 from 'better-sqlite3';\nimport { load as loadSqliteVec } from 'sqlite-vec';\nimport {\n DetailedResult,\n Result,\n captureResult,\n fail,\n failWithDetail,\n succeed,\n succeedWithDetail\n} from '@fgv/ts-utils';\nimport {\n IEdgeTarget,\n IMemoryRecordListing,\n IMemoryRecordSource,\n ISkippedVectorRecord,\n IVectorIndex,\n IVectorQueryHit,\n IVectorRebuildOptions,\n IVectorRebuildReport,\n Kind,\n MemoryEmbedder,\n MemoryId,\n MemoryScopeKey,\n edgeTargetKey\n} from '@fgv/ts-agent-memory';\nimport { invokeHook, tally, withRollbackNote } from './rebuildHelpers';\nimport { closeOwnedConnection, openOwnedConnection } from './connection';\nimport {\n ISqliteVecVectorIndexCreateParams,\n ISqliteVecVectorIndexHandle,\n ISqliteVecVectorIndexOpenParams\n} from './model';\n\n/** Default name for the `vec0` virtual table. */\nconst DEFAULT_TABLE_NAME: string = 'memory_vectors';\n\n/** Package-facing prefix for this class's failure messages. */\nconst LABEL: string = 'sqlite-vec index';\n\n/** A simple SQL identifier — the only shape allowed for the table name (it is interpolated into DDL). */\nconst IDENTIFIER_RE: RegExp = /^[A-Za-z_][A-Za-z0-9_]*$/;\n\n/** One KNN row as returned by the `vec0` MATCH query. */\ninterface IKnnRow {\n readonly target_key: string;\n readonly distance: number;\n}\n\n/**\n * A persistent, `sqlite-vec`-backed `IVectorIndex` for `@fgv/ts-agent-memory`.\n *\n * @remarks\n * This is the **durable** counterpart to the in-memory `InMemoryCosineIndex`:\n * embeddings live in a `sqlite-vec` `vec0` virtual table inside a `better-sqlite3`\n * database, so they survive a process restart. A consumer that wires this index\n * into `FileTreeMemoryStore` (instead of the in-memory index) opens an existing\n * vault **without re-embedding it** — the vectors are already on disk. New writes\n * still flow through the store's incremental embed-on-write path; there is no core\n * store change.\n *\n * The index is keyed by the canonical `edgeTargetKey` of each record's\n * scope-qualified `(scope, id)` address (a `TEXT PRIMARY KEY` on the `vec0` table),\n * so two records that share a filename stem across scopes never collide. The\n * dimension is established by the first `add` (the `vec0` column is fixed-width) and\n * recovered from the table schema when a persistent file is reopened; every later\n * `add`/`query` must match it or fail loudly, exactly as the in-memory index does.\n * Similarity is cosine (`distance_metric=cosine`): the returned `score` is\n * `1 - cosineDistance`, i.e. cosine similarity in `[-1, 1]`, higher = more similar —\n * byte-for-byte the same scoring contract as `InMemoryCosineIndex`.\n *\n * Query is a brute-force `vec0` KNN scan (not an ANN structure): correct and\n * durable, appropriate for the same \"thousands of records\" regime the in-memory\n * index targets. Large-N ANN indexing is explicitly out of scope — see the README.\n *\n * **Connection ownership depends on which factory you use.** With\n * {@link SqliteVecVectorIndex.create} the `Database` is consumer-owned\n * (bring-your-own): this index loads the `sqlite-vec` extension onto it and\n * reads/writes the table, but never opens or closes the connection — and that is\n * the seam for backing a record index and a fragment index with one connection.\n * With {@link SqliteVecVectorIndex.open} this package opens the file itself and\n * hands back a handle carrying the disposer for the connection it created.\n * @public\n */\nexport class SqliteVecVectorIndex implements IVectorIndex {\n private readonly _db: BetterSqlite3.Database;\n private readonly _table: string;\n /** The dimension of every stored vector; `undefined` until the table exists (first `add` or a reopened non-empty file). */\n private _dimension: number | undefined;\n /** Prepared statements; created once the table exists (established or recovered). */\n private _stmts: ISqliteVecStatements | undefined;\n\n private constructor(db: BetterSqlite3.Database, table: string, dimension: number | undefined) {\n this._db = db;\n this._table = table;\n this._dimension = dimension;\n this._stmts = dimension === undefined ? undefined : this._prepare();\n }\n\n /** The number of vectors currently held. Zero before the first `add`. */\n public get size(): number {\n if (this._stmts === undefined) {\n return 0;\n }\n // `Number(...)` narrows the count in case the consumer enabled better-sqlite3\n // safe-integer mode (`db.defaultSafeIntegers(true)`), which returns `count(*)`\n // as a `bigint`. Without it a `bigint` leaks through a `number`-typed contract\n // member — and now through `IIndexCoverage.indexSize`, which is also declared\n // `number`, so the coverage report would carry a value of the wrong runtime\n // type. `SqliteVecFragmentIndex`'s two counts have always converted; this one\n // was the outlier.\n return Number((this._stmts.count.get() as { c: number | bigint }).c);\n }\n\n /**\n * Family-convention factory. Loads the `sqlite-vec` extension onto the supplied\n * `better-sqlite3` connection and, if the vector table already exists (a reopened\n * persistent file), recovers its established dimension so no re-embedding is\n * needed on open.\n *\n * @param params - See {@link ISqliteVecVectorIndexCreateParams}.\n * @returns `Success` with the index, or `Failure` if the table name is not a\n * simple identifier or the extension fails to load.\n */\n public static create(params: ISqliteVecVectorIndexCreateParams): Promise<Result<SqliteVecVectorIndex>> {\n const table: string = params.tableName ?? DEFAULT_TABLE_NAME;\n if (!IDENTIFIER_RE.test(table)) {\n return Promise.resolve(fail(`sqlite-vec index: table name '${table}' is not a simple SQL identifier`));\n }\n return Promise.resolve(\n captureResult(() => {\n loadSqliteVec(params.database);\n const dimension: number | undefined = SqliteVecVectorIndex._readExistingDimension(\n params.database,\n table\n );\n return new SqliteVecVectorIndex(params.database, table, dimension);\n }).withErrorFormat((e) => `sqlite-vec index: failed to initialize: ${e}`)\n );\n }\n\n /**\n * Path-based factory. Opens the database file itself and returns the index\n * together with a disposer for the connection it created.\n *\n * @remarks\n * The convenience over {@link SqliteVecVectorIndex.create} is that the consumer\n * neither value-imports `better-sqlite3` nor re-establishes `Result` discipline\n * around a constructor that throws — this is the one place the package leaked its\n * own dependency into consumer source.\n *\n * **Use `create` instead when one connection must back more than one index** (a\n * record index and a fragment index in the same file, the intended shared-handle\n * case). Two `open` calls on one path give two independent connections, not a\n * shared one.\n *\n * If initialization fails after the file is opened, the connection is closed\n * before returning, so a failed `open` does not leak the descriptor it created.\n * Should that close *itself* fail — the connection is then genuinely leaked — the\n * returned message says so rather than hiding it.\n *\n * @param params - See {@link ISqliteVecVectorIndexOpenParams}.\n * @returns `Success` with a {@link ISqliteVecVectorIndexHandle}, or `Failure` if\n * the driver could not be loaded, the file could not be opened, the table name is\n * not a simple identifier, or the extension fails to load.\n */\n public static async open(\n params: ISqliteVecVectorIndexOpenParams\n ): Promise<Result<ISqliteVecVectorIndexHandle>> {\n return (await openOwnedConnection(params.path, LABEL)).thenOnSuccess(async (database) =>\n (await SqliteVecVectorIndex.create({ database, tableName: params.tableName }))\n .onFailure((message) =>\n // This call opened the connection, so a failure to initialize on top of it\n // must not leave the file handle behind. A close that ALSO fails is said out\n // loud rather than swallowed — the same reasoning, and the same helper, as\n // `withRollbackNote`: silently discarding it would make the \"a failed open\n // leaks nothing\" guarantee untrue exactly when it stopped holding, with no\n // way for a caller to detect it.\n fail(withRollbackNote(message, closeOwnedConnection(database, LABEL)))\n )\n .onSuccess((index) =>\n succeed({\n index,\n close: () => closeOwnedConnection(database, LABEL)\n })\n )\n );\n }\n\n /** {@inheritDoc IVectorIndex.add} */\n public add(target: IEdgeTarget, vector: Float32Array): Promise<Result<string>> {\n const key: string = edgeTargetKey(target);\n if (vector.length === 0) {\n return Promise.resolve(fail(`vector index: cannot add '${key}': empty vector`));\n }\n if (this._dimension !== undefined && vector.length !== this._dimension) {\n return Promise.resolve(\n fail(\n `vector index: cannot add '${key}': dimension ${vector.length} does not match index dimension ${this._dimension}`\n )\n );\n }\n return Promise.resolve(\n captureResult(() => {\n if (this._stmts === undefined) {\n this._createTable(vector.length);\n this._dimension = vector.length;\n this._stmts = this._prepare();\n }\n this._stmts.replace(key, SqliteVecVectorIndex._toBlob(vector));\n return key;\n }).withErrorFormat((e) => `vector index: cannot add '${key}': ${e}`)\n );\n }\n\n /** {@inheritDoc IVectorIndex.has} */\n public has(target: IEdgeTarget): Promise<Result<boolean>> {\n return Promise.resolve(\n captureResult(() => {\n // Before any add has created the table there is nothing held, which is a\n // truthful `false` rather than an error — same posture as `remove`'s\n // idempotence and `size`'s zero.\n if (this._stmts === undefined) {\n return false;\n }\n return this._stmts.has.get(edgeTargetKey(target)) !== undefined;\n }).withErrorFormat((e) => `vector index: cannot check '${edgeTargetKey(target)}': ${e}`)\n );\n }\n\n /** {@inheritDoc IVectorIndex.remove} */\n public remove(target: IEdgeTarget): Promise<Result<IEdgeTarget>> {\n return Promise.resolve(\n captureResult(() => {\n // Idempotent: removing a target with no embedding (or before any `add`\n // created the table) still succeeds.\n if (this._stmts !== undefined) {\n this._stmts.delete.run(edgeTargetKey(target));\n }\n return target;\n }).withErrorFormat((e) => `vector index: cannot remove '${edgeTargetKey(target)}': ${e}`)\n );\n }\n\n /**\n * Re-embed every record from `source` and rebuild the persisted index — see\n * `IVectorIndex.rebuild` for the mode semantics, which this implementation\n * matches exactly.\n *\n * @remarks\n * **Not atomic, and cannot be.** `better-sqlite3` transactions are synchronous,\n * so one cannot span the `await embed(...)` calls this loop makes — unlike\n * {@link SqliteVecVectorIndex.add}, which wraps its delete-then-insert. The\n * `'fail'` / `'skip'` modes therefore cover only failures JavaScript can catch:\n * a process kill mid-rebuild leaves the table holding neither the old index nor\n * the complete new one, and the remedy is to run `rebuild` again.\n */\n public async rebuild(\n source: IMemoryRecordSource,\n embed: MemoryEmbedder,\n options?: IVectorRebuildOptions\n ): Promise<DetailedResult<IVectorRebuildReport, IVectorRebuildReport>> {\n const lenient: boolean = (options?.onRecordError ?? 'fail') === 'skip';\n // `source` is consumer-supplied, so a throw or rejection becomes a `Failure`\n // here rather than escaping as an exception.\n const listed: Result<IMemoryRecordListing> = await invokeHook(() => source.list());\n if (listed.isFailure()) {\n // Deliberately BEFORE any clear: a failed list is no evidence about the\n // vectors already held, and no re-embedding has been attempted, so there is\n // no half-rebuilt state to protect against. Clearing here would destroy a\n // healthy persisted index over a transient read error. No report either, for\n // the same reason — there is nothing this call disturbed to describe.\n return failWithDetail(`vector index rebuild: failed to list records: ${listed.message}`);\n }\n const cleared: Result<true> = this._clear();\n if (cleared.isFailure()) {\n // Also nothing established: the table still holds whatever it held.\n return failWithDetail(`vector index rebuild: failed to clear the index: ${cleared.message}`);\n }\n const indexed: Map<Kind, number> = new Map<Kind, number>();\n const declined: Map<Kind, number> = new Map<Kind, number>();\n const skipped: ISkippedVectorRecord[] = [];\n // Absent stays absent — only the source knows whether it filtered anything.\n const report = (): IVectorRebuildReport => ({\n indexed,\n declined,\n excluded: listed.value.excluded,\n skipped\n });\n for (const scoped of listed.value.records) {\n const kind: Kind = scoped.record.envelope.kind;\n // Likewise capture-wrapped: an embedder that throws mid-loop would\n // otherwise escape past the `'fail'` rollback below, leaving this DURABLE\n // table holding a partial index that survives the process.\n const embedded: Result<Float32Array | undefined> = await invokeHook(() => embed(scoped.record));\n if (embedded.isFailure()) {\n const error: string = `vector index rebuild: embedding '${edgeTargetKey(scoped.target)}' failed: ${\n embedded.message\n }`;\n if (!lenient) {\n return failWithDetail(withRollbackNote(error, this._clear()), report());\n }\n skipped.push({ target: scoped.target, error });\n continue;\n }\n if (embedded.value === undefined) {\n tally(declined, kind);\n continue;\n }\n const added: Result<string> = await this.add(scoped.target, embedded.value);\n if (added.isFailure()) {\n const error: string = `vector index rebuild: ${added.message}`;\n if (!lenient) {\n return failWithDetail(withRollbackNote(error, this._clear()), report());\n }\n skipped.push({ target: scoped.target, error });\n continue;\n }\n // Tallied in the loop rather than read back off `size` at the end. That\n // `COUNT` was also the only fallible step in assembling the report, so the\n // per-kind tally removes a failure path as well as a rounding of the answer.\n tally(indexed, kind);\n }\n return succeedWithDetail(report());\n }\n\n /**\n * **Empties the rows; does NOT release the table's declared dimension.** That\n * is a `vec0` constraint rather than a choice — the dimension is schema, and\n * there is no `ALTER TABLE` for it — so a rebuild at a new dimension fails\n * here where it would succeed on the in-memory sibling, which forgets its\n * dimension on reset. Changing dimension needs a drop-and-re-index; see the\n * note on `IVectorIndex.rebuild`.\n */\n private _clear(): Result<true> {\n if (this._stmts === undefined) {\n return succeed(true);\n }\n // Capture-wrapped like `add` / `remove` / `query`: a closed connection or an\n // I/O error here is a `Failure`, not an exception thrown out of a method\n // whose signature promises a `Result`.\n return captureResult(() => this._db.prepare(`DELETE FROM \"${this._table}\"`).run()).onSuccess(() =>\n succeed(true)\n );\n }\n\n /** {@inheritDoc IVectorIndex.query} */\n public query(vector: Float32Array, topK: number): Promise<Result<ReadonlyArray<IVectorQueryHit>>> {\n if (topK <= 0 || this._stmts === undefined) {\n return Promise.resolve(succeed([]));\n }\n if (vector.length !== this._dimension) {\n return Promise.resolve(\n fail(\n `vector index: query dimension ${vector.length} does not match index dimension ${this._dimension}`\n )\n );\n }\n return Promise.resolve(\n captureResult<ReadonlyArray<IVectorQueryHit>>(() => {\n const rows: ReadonlyArray<IKnnRow> = this._stmts!.query.all(\n SqliteVecVectorIndex._toBlob(vector),\n topK\n ) as ReadonlyArray<IKnnRow>;\n // sqlite-vec returns rows in ascending distance (nearest first); score is\n // `1 - cosineDistance` = cosine similarity, so descending score is preserved.\n return rows.map((row) => ({\n target: SqliteVecVectorIndex._parseKey(row.target_key),\n score: 1 - row.distance\n }));\n }).withErrorFormat((e) => `vector index: query failed: ${e}`)\n );\n }\n\n /** Create the `vec0` virtual table with the established dimension. */\n private _createTable(dimension: number): void {\n this._db.exec(\n `CREATE VIRTUAL TABLE IF NOT EXISTS \"${this._table}\" USING vec0(` +\n `target_key TEXT PRIMARY KEY, embedding float[${dimension}] distance_metric=cosine)`\n );\n }\n\n /** Prepare the statements the index reuses. Requires the table to exist. */\n private _prepare(): ISqliteVecStatements {\n const del: BetterSqlite3.Statement = this._db.prepare(\n `DELETE FROM \"${this._table}\" WHERE target_key = ?`\n );\n const ins: BetterSqlite3.Statement = this._db.prepare(\n `INSERT INTO \"${this._table}\"(target_key, embedding) VALUES (?, ?)`\n );\n // vec0 rejects INSERT OR REPLACE on a TEXT primary key, so replace is a\n // delete-then-insert inside a single transaction.\n const replaceTxn: BetterSqlite3.Transaction<(key: string, blob: Uint8Array) => void> =\n this._db.transaction((key: string, blob: Uint8Array) => {\n del.run(key);\n ins.run(key, blob);\n });\n return {\n delete: del,\n replace: (key: string, blob: Uint8Array): void => {\n replaceTxn(key, blob);\n },\n query: this._db.prepare(\n `SELECT target_key, distance FROM \"${this._table}\" WHERE embedding MATCH ? AND k = ?`\n ),\n count: this._db.prepare(`SELECT count(*) AS c FROM \"${this._table}\"`),\n // `LIMIT 1` rather than a count: membership needs existence, not cardinality,\n // and vec0 can stop at the first row.\n has: this._db.prepare(`SELECT 1 FROM \"${this._table}\" WHERE target_key = ? LIMIT 1`)\n };\n }\n\n /**\n * Recover the established dimension of an existing `vec0` table from its stored\n * `CREATE VIRTUAL TABLE` SQL (`float[<n>]`). Returns `undefined` when the table\n * does not exist yet (a fresh database — dimension is set by the first `add`).\n */\n private static _readExistingDimension(db: BetterSqlite3.Database, table: string): number | undefined {\n const row: { sql: string } | undefined = db\n .prepare(\"SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?\")\n .get(table) as { sql: string } | undefined;\n if (row === undefined) {\n return undefined;\n }\n const match: RegExpMatchArray | null = row.sql.match(/float\\[(\\d+)\\]/);\n return match === null ? undefined : Number(match[1]);\n }\n\n /** Pack a `Float32Array` as the little-endian byte blob `vec0` stores. Copies, so the caller may reuse its buffer. */\n private static _toBlob(vector: Float32Array): Uint8Array {\n return new Uint8Array(Float32Array.from(vector).buffer);\n }\n\n /**\n * Reverse `edgeTargetKey` — the canonical key is `scope\\0id` with NUL\n * excluded from both components, so the first NUL splits it unambiguously.\n */\n private static _parseKey(key: string): IEdgeTarget {\n const nul: number = key.indexOf('\\0');\n return {\n scope: key.slice(0, nul) as unknown as MemoryScopeKey,\n id: key.slice(nul + 1) as unknown as MemoryId\n };\n }\n}\n\n/** The prepared statements / helpers the index reuses once its table exists. */\ninterface ISqliteVecStatements {\n readonly delete: BetterSqlite3.Statement;\n readonly replace: (key: string, blob: Uint8Array) => void;\n readonly query: BetterSqlite3.Statement;\n readonly count: BetterSqlite3.Statement;\n readonly has: BetterSqlite3.Statement;\n}\n"]}