@hviana/sema 0.1.5 → 0.1.6

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 (160) hide show
  1. package/AGENTS.md +6 -5
  2. package/CITATION.cff +49 -0
  3. package/HOW_IT_WORKS.md +11 -12
  4. package/README.md +7 -5
  5. package/dist/example/demo.js +25 -25
  6. package/dist/example/train_base.d.ts +19 -21
  7. package/dist/example/train_base.js +1687 -1952
  8. package/dist/src/alphabet.d.ts +3 -3
  9. package/dist/src/alphabet.js +24 -27
  10. package/dist/src/alu/src/alu.d.ts +169 -194
  11. package/dist/src/alu/src/alu.js +374 -414
  12. package/dist/src/alu/src/expr.d.ts +36 -43
  13. package/dist/src/alu/src/expr.js +252 -278
  14. package/dist/src/alu/src/index.d.ts +9 -98
  15. package/dist/src/alu/src/index.js +10 -64
  16. package/dist/src/alu/src/kernel-arith.d.ts +1 -5
  17. package/dist/src/alu/src/kernel-arith.js +195 -285
  18. package/dist/src/alu/src/kernel-bits.d.ts +1 -5
  19. package/dist/src/alu/src/kernel-bits.js +81 -115
  20. package/dist/src/alu/src/kernel-logic.js +33 -63
  21. package/dist/src/alu/src/kernel-nd.js +155 -195
  22. package/dist/src/alu/src/kernel-numeric.d.ts +16 -80
  23. package/dist/src/alu/src/kernel-numeric.js +259 -331
  24. package/dist/src/alu/src/operation.d.ts +112 -132
  25. package/dist/src/alu/src/operation.js +149 -163
  26. package/dist/src/alu/src/parser.d.ts +168 -175
  27. package/dist/src/alu/src/parser.js +382 -421
  28. package/dist/src/alu/src/resonance.d.ts +16 -25
  29. package/dist/src/alu/src/resonance.js +49 -56
  30. package/dist/src/alu/src/text.d.ts +3 -7
  31. package/dist/src/alu/src/text.js +31 -37
  32. package/dist/src/alu/src/value.d.ts +14 -14
  33. package/dist/src/alu/src/value.js +150 -160
  34. package/dist/src/alu/test/alu.test.js +543 -656
  35. package/dist/src/bytes.d.ts +1 -5
  36. package/dist/src/bytes.js +33 -40
  37. package/dist/src/config.d.ts +99 -102
  38. package/dist/src/config.js +78 -83
  39. package/dist/src/derive/src/deduction.d.ts +60 -62
  40. package/dist/src/derive/src/deduction.js +103 -109
  41. package/dist/src/derive/src/index.d.ts +1 -7
  42. package/dist/src/derive/src/priority-queue.d.ts +8 -8
  43. package/dist/src/derive/src/priority-queue.js +57 -61
  44. package/dist/src/derive/src/rewrite.d.ts +15 -18
  45. package/dist/src/derive/src/rewrite.js +59 -67
  46. package/dist/src/derive/src/trie.d.ts +56 -56
  47. package/dist/src/derive/src/trie.js +175 -179
  48. package/dist/src/derive/test/derive.test.js +96 -100
  49. package/dist/src/extension.d.ts +13 -16
  50. package/dist/src/geometry.d.ts +17 -42
  51. package/dist/src/geometry.js +235 -266
  52. package/dist/src/index.d.ts +2 -17
  53. package/dist/src/index.js +2 -12
  54. package/dist/src/ingest-cache.d.ts +25 -30
  55. package/dist/src/ingest-cache.js +108 -127
  56. package/dist/src/mind/articulation.d.ts +1 -5
  57. package/dist/src/mind/articulation.js +77 -112
  58. package/dist/src/mind/attention.d.ts +40 -108
  59. package/dist/src/mind/attention.js +749 -871
  60. package/dist/src/mind/canonical.d.ts +4 -19
  61. package/dist/src/mind/canonical.js +27 -31
  62. package/dist/src/mind/graph-search.d.ts +201 -225
  63. package/dist/src/mind/graph-search.js +742 -821
  64. package/dist/src/mind/index.d.ts +2 -10
  65. package/dist/src/mind/junction.d.ts +31 -58
  66. package/dist/src/mind/junction.js +172 -237
  67. package/dist/src/mind/learning.d.ts +16 -52
  68. package/dist/src/mind/learning.js +143 -165
  69. package/dist/src/mind/match.d.ts +20 -69
  70. package/dist/src/mind/match.js +259 -318
  71. package/dist/src/mind/mechanisms/alu.js +16 -16
  72. package/dist/src/mind/mechanisms/cast.d.ts +9 -13
  73. package/dist/src/mind/mechanisms/cast.js +363 -456
  74. package/dist/src/mind/mechanisms/confluence.d.ts +8 -12
  75. package/dist/src/mind/mechanisms/confluence.js +152 -183
  76. package/dist/src/mind/mechanisms/cover.d.ts +2 -8
  77. package/dist/src/mind/mechanisms/cover.js +148 -210
  78. package/dist/src/mind/mechanisms/extraction.d.ts +8 -34
  79. package/dist/src/mind/mechanisms/extraction.js +234 -288
  80. package/dist/src/mind/mechanisms/recall.d.ts +6 -10
  81. package/dist/src/mind/mechanisms/recall.js +126 -185
  82. package/dist/src/mind/mind.d.ts +129 -154
  83. package/dist/src/mind/mind.js +258 -288
  84. package/dist/src/mind/pipeline-mechanism.d.ts +112 -124
  85. package/dist/src/mind/pipeline-mechanism.js +161 -172
  86. package/dist/src/mind/pipeline.d.ts +4 -14
  87. package/dist/src/mind/pipeline.js +125 -189
  88. package/dist/src/mind/primitives.d.ts +8 -32
  89. package/dist/src/mind/primitives.js +99 -117
  90. package/dist/src/mind/rationale.d.ts +86 -98
  91. package/dist/src/mind/rationale.js +113 -121
  92. package/dist/src/mind/reasoning.d.ts +2 -13
  93. package/dist/src/mind/reasoning.js +129 -166
  94. package/dist/src/mind/recognition.d.ts +1 -4
  95. package/dist/src/mind/recognition.js +183 -208
  96. package/dist/src/mind/resonance.d.ts +5 -22
  97. package/dist/src/mind/resonance.js +0 -0
  98. package/dist/src/mind/trace.d.ts +6 -25
  99. package/dist/src/mind/trace.js +50 -58
  100. package/dist/src/mind/traverse.d.ts +16 -58
  101. package/dist/src/mind/traverse.js +304 -357
  102. package/dist/src/mind/types.d.ts +120 -127
  103. package/dist/src/mind/types.js +60 -69
  104. package/dist/src/rabitq-ivf/src/database.d.ts +113 -0
  105. package/dist/src/rabitq-ivf/src/database.js +201 -0
  106. package/dist/src/rabitq-ivf/src/index.d.ts +7 -0
  107. package/dist/src/{rabitq-hnsw → rabitq-ivf}/src/index.js +1 -3
  108. package/dist/src/rabitq-ivf/src/ivf.d.ts +200 -0
  109. package/dist/src/rabitq-ivf/src/ivf.js +1165 -0
  110. package/dist/src/rabitq-ivf/src/prng.d.ts +19 -0
  111. package/dist/src/rabitq-ivf/src/prng.js +36 -0
  112. package/dist/src/rabitq-ivf/src/rabitq.d.ts +95 -0
  113. package/dist/src/rabitq-ivf/src/rabitq.js +283 -0
  114. package/dist/src/sema.d.ts +9 -13
  115. package/dist/src/sema.js +26 -40
  116. package/dist/src/store-sqlite.d.ts +169 -166
  117. package/dist/src/store-sqlite.js +762 -662
  118. package/dist/src/store.d.ts +586 -630
  119. package/dist/src/store.js +1423 -1581
  120. package/dist/src/vec.d.ts +5 -9
  121. package/dist/src/vec.js +61 -73
  122. package/example/train_base.ts +13 -10
  123. package/package.json +1 -1
  124. package/src/alu/README.md +1 -1
  125. package/src/alu/src/index.ts +1 -1
  126. package/src/config.ts +19 -27
  127. package/src/index.ts +6 -11
  128. package/src/rabitq-ivf/README.md +56 -0
  129. package/src/rabitq-ivf/src/database.ts +276 -0
  130. package/src/{rabitq-hnsw → rabitq-ivf}/src/index.ts +2 -5
  131. package/src/rabitq-ivf/src/ivf.ts +1330 -0
  132. package/src/{rabitq-hnsw → rabitq-ivf}/src/prng.ts +1 -1
  133. package/src/store-sqlite.ts +196 -9
  134. package/src/store.ts +8 -32
  135. package/test/08-storage.test.mjs +3 -3
  136. package/test/14-scaling.test.mjs +2 -2
  137. package/test/35-ivf.test.mjs +263 -0
  138. package/test/36-bloom.test.mjs +123 -0
  139. package/dist/src/rabitq-hnsw/src/database.d.ts +0 -202
  140. package/dist/src/rabitq-hnsw/src/database.js +0 -405
  141. package/dist/src/rabitq-hnsw/src/heap.d.ts +0 -22
  142. package/dist/src/rabitq-hnsw/src/heap.js +0 -94
  143. package/dist/src/rabitq-hnsw/src/hnsw.d.ts +0 -125
  144. package/dist/src/rabitq-hnsw/src/hnsw.js +0 -500
  145. package/dist/src/rabitq-hnsw/src/index.d.ts +0 -15
  146. package/dist/src/rabitq-hnsw/src/prng.d.ts +0 -19
  147. package/dist/src/rabitq-hnsw/src/prng.js +0 -38
  148. package/dist/src/rabitq-hnsw/src/rabitq.d.ts +0 -95
  149. package/dist/src/rabitq-hnsw/src/rabitq.js +0 -299
  150. package/dist/src/rabitq-hnsw/src/store.d.ts +0 -162
  151. package/dist/src/rabitq-hnsw/src/store.js +0 -916
  152. package/dist/src/rabitq-hnsw/test/hnsw.test.d.ts +0 -1
  153. package/dist/src/rabitq-hnsw/test/hnsw.test.js +0 -1197
  154. package/src/rabitq-hnsw/README.md +0 -303
  155. package/src/rabitq-hnsw/src/database.ts +0 -492
  156. package/src/rabitq-hnsw/src/heap.ts +0 -90
  157. package/src/rabitq-hnsw/src/hnsw.ts +0 -514
  158. package/src/rabitq-hnsw/src/store.ts +0 -994
  159. package/src/rabitq-hnsw/test/hnsw.test.ts +0 -1213
  160. /package/src/{rabitq-hnsw → rabitq-ivf}/src/rabitq.ts +0 -0
@@ -0,0 +1,201 @@
1
+ import { IvfIndex } from "./ivf.js";
2
+ /**
3
+ * A persistent vector database: an adaptive PARTITIONED (IVF) index over
4
+ * 1-bit RaBitQ codes (cosine), stored entirely in SQLite at `dbPath`. See
5
+ * ivf.ts for the index design. The original float vectors are never
6
+ * retained — only the sign codes — so a 1024-d vector costs 128 bytes.
7
+ *
8
+ * Inserting is route-and-append: cost is essentially FLAT in collection size
9
+ * (one RAM scan of the pivot table + one chunk append — no per-insert graph
10
+ * walk). Query cost is bounded by nprobe × cluster size — constant in N
11
+ * once the collection is past its first splits.
12
+ */
13
+ export class VectorDatabase {
14
+ index;
15
+ constructor(options) {
16
+ if (!options || typeof options.dbPath !== "string" ||
17
+ options.dbPath.length === 0) {
18
+ throw new Error("DatabaseOptions.dbPath (string) is required");
19
+ }
20
+ this.index = new IvfIndex(options.dbPath, options);
21
+ }
22
+ get dim() {
23
+ return this.index.dim;
24
+ }
25
+ /** Number of live (non-deleted) vectors. */
26
+ get size() {
27
+ return this.index.size;
28
+ }
29
+ /** Physical slot count including tombstones from deletes/updates. */
30
+ get physicalSize() {
31
+ return this.index.physicalSize;
32
+ }
33
+ /** Number of clusters (partitions) currently in the index. */
34
+ get clusterCount() {
35
+ return this.index.clusterCount;
36
+ }
37
+ get efSearch() {
38
+ return this.index.efSearch;
39
+ }
40
+ set efSearch(value) {
41
+ this.index.efSearch = value;
42
+ }
43
+ /** Distance computations performed during the most recent query. */
44
+ get lastQueryDistanceComputations() {
45
+ return this.index.lastQueryDistComps;
46
+ }
47
+ /** Storage row reads issued by the most recent query — the honest,
48
+ * cache-independent scalability metric. */
49
+ get lastQueryStorageReads() {
50
+ return this.index.lastQueryStorageReads;
51
+ }
52
+ get storage() {
53
+ const f32 = this.dim * 4;
54
+ const bpv = this.index.bytesPerVector;
55
+ return {
56
+ float32BytesPerVector: f32,
57
+ codeBytesPerVector: bpv,
58
+ bytesPerVector: bpv,
59
+ compressionRatio: f32 / bpv,
60
+ };
61
+ }
62
+ checkId(id) {
63
+ // 32-bit signed range: chunk blobs store exts as int32 — a wider id
64
+ // would silently truncate, so it is rejected at the door instead.
65
+ if (!Number.isInteger(id) || id > 0x7fffffff || id < -0x80000000) {
66
+ throw new Error(`External id must be a 32-bit integer, got ${id}`);
67
+ }
68
+ return id;
69
+ }
70
+ /** Convert a value to code bytes, selecting by length: `codeWords`
71
+ * elements → an existing 1-bit code; otherwise a raw `dim`-vector. */
72
+ toCodeBytes(value) {
73
+ if (value.length === this.index.codeWords) {
74
+ return this.index.codeToBytes(value);
75
+ }
76
+ if (value.length !== this.dim) {
77
+ throw new Error(`Vector dimension mismatch: expected ${this.dim}, got ${value.length}`);
78
+ }
79
+ return this.index.encodeToBytes(value);
80
+ }
81
+ has(id) {
82
+ return this.index.has(this.checkId(id));
83
+ }
84
+ /** Stream every live external id (bounded memory). */
85
+ *keys() {
86
+ yield* this.index.keys();
87
+ }
88
+ /** Stream live entries whose INTERNAL id is > `after` — a durable
89
+ * incremental watermark (internal ids are monotone at insert and preserved
90
+ * by update and compact). */
91
+ *keysSince(after) {
92
+ yield* this.index.keysSince(after);
93
+ }
94
+ /** Read the stored 1-bit code for an id (a copy as a Uint32Array), or null. */
95
+ get(id) {
96
+ const bytes = this.index.codeOf(this.checkId(id));
97
+ return bytes ? this.index.bytesToCode(bytes) : null;
98
+ }
99
+ insert(id, value) {
100
+ this.index.begin();
101
+ try {
102
+ this.index.insert(this.checkId(id), this.toCodeBytes(value));
103
+ this.index.commit();
104
+ }
105
+ catch (e) {
106
+ this.index.rollback();
107
+ throw e;
108
+ }
109
+ }
110
+ update(id, value) {
111
+ this.index.begin();
112
+ try {
113
+ this.index.update(this.checkId(id), this.toCodeBytes(value));
114
+ this.index.commit();
115
+ }
116
+ catch (e) {
117
+ this.index.rollback();
118
+ throw e;
119
+ }
120
+ }
121
+ upsert(id, value) {
122
+ this.upsertMany([{ id, vector: value }]);
123
+ }
124
+ /**
125
+ * Upsert many vectors under ONE transaction — one WAL commit for the whole
126
+ * batch instead of one per vector, with chunk appends to the same cluster
127
+ * coalesced in the index's write-back buffer.
128
+ */
129
+ upsertMany(entries) {
130
+ if (entries.length === 0)
131
+ return;
132
+ this.index.begin();
133
+ try {
134
+ for (const e of entries) {
135
+ // One shared vmap probe decides insert vs update — the per-row point
136
+ // query is the dominant bulk-load cost, so it is never duplicated.
137
+ this.index.upsert(this.checkId(e.id), this.toCodeBytes(e.vector));
138
+ }
139
+ this.index.commit();
140
+ }
141
+ catch (e) {
142
+ this.index.rollback();
143
+ throw e;
144
+ }
145
+ }
146
+ /** Delete many ids under ONE transaction. Absent ids are skipped.
147
+ * Returns the number of vectors actually removed. */
148
+ deleteMany(ids) {
149
+ if (ids.length === 0)
150
+ return 0;
151
+ let removed = 0;
152
+ this.index.begin();
153
+ try {
154
+ for (const id of ids) {
155
+ if (this.index.remove(this.checkId(id)))
156
+ removed++;
157
+ }
158
+ this.index.commit();
159
+ }
160
+ catch (e) {
161
+ this.index.rollback();
162
+ throw e;
163
+ }
164
+ return removed;
165
+ }
166
+ /** Delete the vector bound to an id. Returns false if absent. */
167
+ delete(id) {
168
+ return this.deleteMany([id]) === 1;
169
+ }
170
+ /** Heat the SQLite page cache with sequential scans (latency only). */
171
+ warmCache() {
172
+ return this.index.warmCache();
173
+ }
174
+ /** Reclaim tombstoned slots by rewriting the clusters that carry any, then
175
+ * VACUUM. Internal ids and cluster assignment are preserved. */
176
+ compact() {
177
+ this.index.compact();
178
+ }
179
+ /**
180
+ * k-NN search. The argument's length selects the mode:
181
+ * - `dim` elements → a raw vector (accurate estimator)
182
+ * - `codeWords` elements → a 1-bit code, by sign-bit Hamming
183
+ */
184
+ query(query, k = 10, opts) {
185
+ let hits;
186
+ if (query.length === this.index.codeWords) {
187
+ hits = this.index.searchKnnByCode(this.index.codeToBytes(query), k, opts?.ef);
188
+ }
189
+ else {
190
+ if (query.length !== this.dim) {
191
+ throw new Error(`Vector dimension mismatch: expected ${this.dim}, got ${query.length}`);
192
+ }
193
+ hits = this.index.searchKnn(query, k, opts?.ef);
194
+ }
195
+ return hits;
196
+ }
197
+ /** Close the underlying database. The instance must not be used afterwards. */
198
+ close() {
199
+ this.index.close();
200
+ }
201
+ }
@@ -0,0 +1,7 @@
1
+ export { VectorDatabase } from "./database.js";
2
+ export type { DatabaseOptions, ExternalId, QueryResult, StorageStats, } from "./database.js";
3
+ export { IvfIndex } from "./ivf.js";
4
+ export type { IvfConfig, IvfHit } from "./ivf.js";
5
+ export { RaBitQuantizer } from "./rabitq.js";
6
+ export type { QueryContext, RaBitQOptions } from "./rabitq.js";
7
+ export { Prng } from "./prng.js";
@@ -1,6 +1,4 @@
1
1
  export { VectorDatabase } from "./database.js";
2
- export { HnswIndex } from "./hnsw.js";
2
+ export { IvfIndex } from "./ivf.js";
3
3
  export { RaBitQuantizer } from "./rabitq.js";
4
- export { Store } from "./store.js";
5
- export { Heap } from "./heap.js";
6
4
  export { Prng } from "./prng.js";
@@ -0,0 +1,200 @@
1
+ import { DatabaseSync } from "node:sqlite";
2
+ export interface IvfConfig {
3
+ dim: number;
4
+ efSearch: number;
5
+ queryBits: number;
6
+ rotationRounds: number;
7
+ seed: number;
8
+ centroid: Float64Array | null;
9
+ codeWords: number;
10
+ paddedDim: number;
11
+ }
12
+ export interface IvfHit {
13
+ /** external (user) id */
14
+ id: number;
15
+ /** estimated cosine distance (1 - cosine) */
16
+ distance: number;
17
+ }
18
+ export declare class IvfIndex {
19
+ readonly db: DatabaseSync;
20
+ private readonly quantizer;
21
+ private readonly codeBytes;
22
+ private readonly chunkBytes;
23
+ private _efSearch;
24
+ /** Pivot codes, K × codeBytes, contiguous — the insert/query routing scan
25
+ * is one linear pass over this array. */
26
+ private centCodes;
27
+ /** Aligned 32-bit word view of {@link centCodes} — the routing scan's
28
+ * operand (4 bytes per XOR+popcount instead of byte re-composition). */
29
+ private centWords;
30
+ /** Per-cluster entry count (live + dead) — the split trigger. */
31
+ private centEntries;
32
+ /** Per-cluster chunk count (tail seq = count − 1). */
33
+ private centChunks;
34
+ private K;
35
+ private live;
36
+ private totalSlots;
37
+ private nextId;
38
+ /** Storage row reads (chunk blob fetches) — the cache-independent
39
+ * scalability witness, same discipline as the graph index it replaces. */
40
+ reads: number;
41
+ lastQueryDistComps: number;
42
+ lastQueryStorageReads: number;
43
+ private readonly chunkCache;
44
+ private readonly dirtyChunks;
45
+ private readonly dirtyCents;
46
+ private metaDirty;
47
+ private readonly cacheEnabled;
48
+ private readonly chunkCacheMax;
49
+ private sSelChunk;
50
+ private sUpsChunk;
51
+ private sDelChunk;
52
+ private sSelVmap;
53
+ private sInsVmap;
54
+ private sUpdVmap;
55
+ private sDelVmap;
56
+ private sUpsCent;
57
+ private sMeta;
58
+ constructor(dbPath: string, options: {
59
+ dim?: number;
60
+ efSearch?: number;
61
+ queryBits?: number;
62
+ rotationRounds?: number;
63
+ seed?: number;
64
+ centroid?: ArrayLike<number>;
65
+ cacheSizeMb?: number;
66
+ });
67
+ readonly dim: number;
68
+ private prepareAll;
69
+ /** Load the pivot table into RAM — K rows, ~(codeBytes + 8) bytes each. */
70
+ private loadCents;
71
+ private growCents;
72
+ get size(): number;
73
+ get physicalSize(): number;
74
+ get clusterCount(): number;
75
+ get bytesPerVector(): number;
76
+ get efSearch(): number;
77
+ set efSearch(v: number);
78
+ resetReads(): void;
79
+ /** Fetch a chunk, serving dirty/cached copies first. `retain` controls
80
+ * whether a clean fetch enters the cache — read-only consumers pass the
81
+ * cacheEnabled flag so the `cacheSizeMb: 0` mode stays honestly uncached;
82
+ * writers always retain (the copy is about to become dirty). */
83
+ private chunkAt;
84
+ private cacheChunk;
85
+ private markDirty;
86
+ /** Write every dirty chunk row; drop the clean cache only when retention
87
+ * is off (no budget) — under a budget the warm set survives the flush.
88
+ * Runs inside the caller's transaction. */
89
+ private flushChunks;
90
+ private flushCents;
91
+ /** Persist all buffered state. MUST be called before the enclosing
92
+ * transaction commits (upsertMany/deleteMany do; single-op paths too). */
93
+ private flushAll;
94
+ private static readonly SUPER_G;
95
+ private static readonly SUPER_TOP;
96
+ /** Majority super-pivot codes, one per group of SUPER_G clusters. */
97
+ private superWords;
98
+ /** Group ids whose super-pivot is stale (member pivot changed). */
99
+ private readonly superStale;
100
+ /** Scratch word view of the code being routed (avoids per-call copies). */
101
+ private routeScratch;
102
+ private markSuperStale;
103
+ /** Recompute stale super-pivots (majority bit over member pivots). */
104
+ private freshenSupers;
105
+ /** Word-popcount Hamming between the routing scratch and a words-row. */
106
+ private hamAt;
107
+ /** Nearest cluster to a code — exact over member pivots of the SUPER_TOP
108
+ * nearest super-groups (or over everything while K is small). Pure RAM,
109
+ * no allocation, no storage reads. */
110
+ private nearestCid;
111
+ /** The nprobe nearest clusters to a QUERY (accurate estimator on pivots),
112
+ * as cids ordered nearest-first. */
113
+ private probeOrder;
114
+ private probeOrderByCode;
115
+ /** Whether an external id is present (live). */
116
+ has(ext: number): boolean;
117
+ /** The stored code for an ext (a copy), or null. */
118
+ codeOf(ext: number): Uint8Array | null;
119
+ /** Insert a code under `ext`. Caller owns the transaction; buffered rows
120
+ * are flushed by {@link commitFlush}. Throws if ext is already live. */
121
+ insert(ext: number, code: Uint8Array): void;
122
+ /** Insert-or-update with ONE vmap probe. The point probe is a real cost
123
+ * at bulk-load rates (statement dispatch + a B-tree descent per row —
124
+ * profiled as the single largest insert-path term), so the presence check
125
+ * and the update lookup share it. */
126
+ upsert(ext: number, code: Uint8Array): void;
127
+ /** The insert core — presence already established by the caller. */
128
+ private insertNew;
129
+ private newCluster;
130
+ private appendToCluster;
131
+ /** Tombstone a live ext. Returns false when absent. */
132
+ remove(ext: number): boolean;
133
+ /** Update the code bound to a live ext. A byte-identical code is a no-op
134
+ * (content-addressed callers re-upsert unchanged vectors wholesale after a
135
+ * restart; each no-op otherwise costs a tombstone + reinsert). */
136
+ update(ext: number, code: Uint8Array): void;
137
+ private updateAt;
138
+ /** Flush buffered chunk/cent/meta rows. Call before COMMIT. */
139
+ commitFlush(): void;
140
+ /** Split a full cluster in two, deterministically. Dead slots are dropped
141
+ * (splits double as incremental compaction). Both halves get fresh
142
+ * majority-bit pivots, so routing sharpens as the corpus grows. */
143
+ private split;
144
+ /** Bit-majority sample size per split side. A pivot is a routing aid, not
145
+ * a stored value: the majority bit of a 512-member deterministic sample
146
+ * agrees with the full-population majority except on near-tied bits,
147
+ * where either choice routes equally well — and counting every member of
148
+ * a 4096-entry cluster was 25% of bulk-load CPU (profiled). */
149
+ private static readonly MAJORITY_SAMPLE;
150
+ /** Majority-bit code of the side's members (the binary centroid), or null
151
+ * when the side is empty. Members are sampled on a deterministic stride
152
+ * when the side exceeds {@link MAJORITY_SAMPLE}. */
153
+ private majorityCode;
154
+ /** Rewrite a cluster's chunks from a member list (one side of a split or a
155
+ * compaction survivor set), updating vmap rows. `dropFrom` deletes any
156
+ * leftover chunk rows beyond the new count. */
157
+ private rebuildCluster;
158
+ /** Multi-row vmap statements, prepared per row count (powers of a fixed
159
+ * batch width) — REPLACE semantics on the ext primary key. */
160
+ private readonly vmapBatchStmts;
161
+ private static readonly VMAP_BATCH;
162
+ private vmapBatchStmt;
163
+ /** Write flat (ext, id, cid, seq, slot) tuples in wide batches. */
164
+ private flushVmapRows;
165
+ /** ef → clusters probed. ef is the familiar "candidate breadth" knob; a
166
+ * probe scans one whole cluster, so nprobe = ef/4 keeps the default
167
+ * (ef 64 → 16 probes) both accurate on trained stores and bounded. */
168
+ private nprobeOf;
169
+ /** k-NN with a full-precision query (accurate RaBitQ estimator). */
170
+ searchKnn(vec: ArrayLike<number>, k: number, ef?: number): IvfHit[];
171
+ /** k-NN with an already-quantized code (sign-bit Hamming distance). */
172
+ searchKnnByCode(code: Uint8Array, k: number, ef?: number): IvfHit[];
173
+ private scanClusters;
174
+ /** Stream live entries whose internal id is > `after`, in id order.
175
+ * Internal ids are monotone at insert and PRESERVED by update/compact, so
176
+ * the largest id a caller has seen is a durable incremental watermark. */
177
+ keysSince(after: number, batch?: number): IterableIterator<{
178
+ ext: number;
179
+ internal: number;
180
+ }>;
181
+ /** Stream every live external id. */
182
+ keys(): IterableIterator<number>;
183
+ /** Drop tombstoned slots by rewriting each cluster that carries any, then
184
+ * VACUUM. Internal ids and cluster assignment are preserved — routing
185
+ * quality is untouched, only dead space is reclaimed. */
186
+ compact(): void;
187
+ /** Heat SQLite's page cache with sequential scans of the chunk and vmap
188
+ * tables — a cold session otherwise warms it through random point reads.
189
+ * Purely a latency optimisation; returns rows touched. */
190
+ warmCache(): number;
191
+ begin(): void;
192
+ commit(): void;
193
+ rollback(): void;
194
+ /** Encode a raw vector to its 1-bit code bytes. */
195
+ encodeToBytes(vec: ArrayLike<number>): Uint8Array;
196
+ codeToBytes(code: ArrayLike<number>): Uint8Array;
197
+ bytesToCode(bytes: Uint8Array): Uint32Array;
198
+ get codeWords(): number;
199
+ close(): void;
200
+ }