@gmickel/gno 2.15.0 → 2.17.0
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.
- package/README.md +1 -1
- package/assets/skill/SKILL.md +27 -0
- package/assets/skill/cli-reference.md +24 -15
- package/assets/spa-production.json.gz +0 -0
- package/browser-extension/artifacts/{gno-browser-clipper-v2.15.0.zip → gno-browser-clipper-v2.17.0.zip} +0 -0
- package/browser-extension/artifacts/gno-browser-clipper-v2.17.0.zip.sha256 +1 -0
- package/browser-extension/dist/manifest.json +1 -1
- package/package.json +1 -1
- package/spec/cli.md +43 -3
- package/spec/db/schema.sql +53 -1
- package/spec/mcp.md +9 -3
- package/spec/output-schemas/doctor.schema.json +27 -0
- package/spec/output-schemas/retrieval-trace-payloads.schema.json +14 -0
- package/spec/output-schemas/search-results.schema.json +14 -0
- package/spec/output-schemas/status.schema.json +22 -0
- package/src/cli/commands/agents/commands.ts +1 -1
- package/src/cli/commands/ask.ts +1 -1
- package/src/cli/commands/context-build.ts +1 -1
- package/src/cli/commands/doctor.ts +101 -29
- package/src/cli/commands/embed.ts +2 -2
- package/src/cli/commands/links.ts +30 -6
- package/src/cli/commands/mcp/install.ts +2 -2
- package/src/cli/commands/mcp/status.ts +1 -1
- package/src/cli/commands/mcp/uninstall.ts +1 -1
- package/src/cli/commands/query.ts +1 -1
- package/src/cli/commands/shared.ts +4 -4
- package/src/cli/commands/skill/install.ts +1 -1
- package/src/cli/commands/skill/paths-cmd.ts +1 -1
- package/src/cli/commands/skill/uninstall.ts +1 -1
- package/src/cli/commands/status.ts +75 -58
- package/src/cli/commands/vec.ts +180 -8
- package/src/cli/config-cache.ts +86 -0
- package/src/cli/globals.ts +64 -0
- package/src/cli/program.ts +63 -36
- package/src/cli/run.ts +121 -67
- package/src/cli/runtime-health.ts +132 -122
- package/src/cli/session-binding.ts +11 -2
- package/src/config/saver.ts +8 -0
- package/src/config/types.ts +6 -0
- package/src/converters/adapters/browser-export/formats.ts +25 -15
- package/src/converters/adapters/email/adapter.ts +37 -53
- package/src/converters/adapters/email/html.ts +37 -20
- package/src/converters/adapters/email/mime.ts +71 -39
- package/src/converters/adapters/ical/adapter.ts +20 -12
- package/src/converters/adapters/jsonl/adapter.ts +8 -6
- package/src/converters/adapters/shared/html-text.ts +55 -37
- package/src/converters/adapters/shared/ooxml-protection.ts +5 -13
- package/src/converters/adapters/shared/utf8-lines.ts +69 -31
- package/src/converters/adapters/transcript/adapter.ts +12 -7
- package/src/converters/adapters/xlsx/adapter.ts +10 -7
- package/src/converters/adapters/xlsx/sheet-markdown.ts +93 -50
- package/src/converters/canonicalize.ts +17 -3
- package/src/converters/native/markdown.ts +24 -17
- package/src/converters/native/plaintext.ts +5 -6
- package/src/converters/registry.ts +130 -8
- package/src/core/activation-status.ts +4 -8
- package/src/core/change-diff.ts +38 -11
- package/src/core/context-compiler.ts +17 -18
- package/src/core/context-evidence.ts +116 -25
- package/src/core/context-verifier.ts +70 -31
- package/src/core/file-lock.ts +10 -2
- package/src/core/graph-analysis.ts +3 -3
- package/src/core/link-inventory-markdown.ts +2 -2
- package/src/core/links.ts +20 -87
- package/src/core/memory-fence.ts +16 -9
- package/src/core/memory-recall.ts +168 -34
- package/src/core/path-rules.ts +143 -0
- package/src/core/retrieval-trace-session.ts +3 -0
- package/src/core/retrieval-trace.ts +9 -0
- package/src/core/section-parse.ts +16 -6
- package/src/core/typed-metadata.ts +68 -0
- package/src/core/vector-partition-status.ts +13 -0
- package/src/core/wiki-link-syntax.ts +88 -0
- package/src/embed/backlog.ts +142 -60
- package/src/embed/retry.ts +39 -33
- package/src/embed/variant-backlog.ts +582 -116
- package/src/embed/variant-retry.ts +157 -48
- package/src/index.ts +3 -3
- package/src/ingestion/budget-error-codes.ts +13 -0
- package/src/ingestion/chunker.ts +60 -49
- package/src/ingestion/compiled-context.ts +4 -5
- package/src/ingestion/convert-file.ts +1 -1
- package/src/ingestion/document-metadata.ts +16 -9
- package/src/ingestion/excluded-ranges.ts +97 -0
- package/src/ingestion/file-processor.ts +122 -37
- package/src/ingestion/frontmatter.ts +35 -7
- package/src/ingestion/graph-reconciliation.ts +187 -51
- package/src/ingestion/language.ts +66 -61
- package/src/ingestion/position.ts +5 -5
- package/src/ingestion/prepare-file.ts +50 -29
- package/src/ingestion/record-adapter-canonical.ts +37 -26
- package/src/ingestion/record-adapter.ts +46 -26
- package/src/ingestion/record-container.ts +121 -53
- package/src/ingestion/record-path.ts +5 -2
- package/src/ingestion/source-availability/darwin-io.ts +10 -7
- package/src/ingestion/source-availability/darwin-path.ts +21 -3
- package/src/ingestion/source-availability/readers.ts +51 -0
- package/src/ingestion/strip.ts +39 -105
- package/src/ingestion/sync.ts +692 -386
- package/src/ingestion/typed-metadata.ts +10 -4
- package/src/ingestion/walker.ts +485 -158
- package/src/llm/cache.ts +128 -84
- package/src/llm/http-inference.ts +112 -35
- package/src/llm/http-policy.ts +101 -0
- package/src/llm/httpEmbedding.ts +8 -1
- package/src/llm/httpGeneration.ts +5 -1
- package/src/llm/httpRerank.ts +5 -1
- package/src/llm/inference-scope.ts +15 -2
- package/src/llm/lazy-ports.ts +33 -3
- package/src/llm/lockfile.ts +34 -7
- package/src/llm/model-cleanup.ts +24 -0
- package/src/llm/native-worker/client.ts +23 -12
- package/src/llm/native-worker/dispatcher.ts +19 -9
- package/src/llm/native-worker/embedding-identity.ts +42 -16
- package/src/llm/native-worker/entry.ts +2 -1
- package/src/llm/native-worker/owner.ts +1 -0
- package/src/llm/native-worker/ports.ts +86 -23
- package/src/llm/native-worker/protocol.ts +386 -55
- package/src/llm/nodeLlamaCpp/adapter.ts +46 -29
- package/src/llm/nodeLlamaCpp/embedding.ts +93 -40
- package/src/llm/nodeLlamaCpp/generation.ts +194 -30
- package/src/llm/nodeLlamaCpp/lifecycle.ts +2 -0
- package/src/llm/nodeLlamaCpp/rerank-capacity.ts +101 -23
- package/src/llm/nodeLlamaCpp/rerank.ts +253 -44
- package/src/llm/types.ts +14 -0
- package/src/mcp/context.ts +342 -23
- package/src/mcp/http-transport.ts +21 -3
- package/src/mcp/tool-profile-types.ts +62 -0
- package/src/mcp/tool-profile.ts +32 -56
- package/src/mcp/tools/context.ts +5 -1
- package/src/mcp/tools/index.ts +269 -191
- package/src/mcp/tools/links.ts +14 -6
- package/src/mcp/tools/query.ts +60 -37
- package/src/mcp/tools/vsearch.ts +25 -14
- package/src/pipeline/answer.ts +133 -40
- package/src/pipeline/claim-verification.ts +293 -189
- package/src/pipeline/claim-verifier.ts +6 -7
- package/src/pipeline/egress-lineage.ts +37 -26
- package/src/pipeline/exclude.ts +34 -11
- package/src/pipeline/expansion.ts +233 -19
- package/src/pipeline/explain.ts +17 -2
- package/src/pipeline/filters.ts +169 -33
- package/src/pipeline/graph-retrieval.ts +117 -61
- package/src/pipeline/hybrid.ts +626 -330
- package/src/pipeline/hydration.ts +86 -24
- package/src/pipeline/intent.ts +15 -7
- package/src/pipeline/owner-fusion.ts +61 -19
- package/src/pipeline/query-language.ts +15 -2
- package/src/pipeline/rerank.ts +15 -22
- package/src/pipeline/search.ts +193 -72
- package/src/pipeline/snippet.ts +15 -2
- package/src/pipeline/temporal.ts +51 -10
- package/src/pipeline/types.ts +7 -0
- package/src/pipeline/vsearch.ts +269 -154
- package/src/publish/attachment-raster.ts +2 -16
- package/src/publish/encrypted-export.ts +15 -5
- package/src/serve/browse-tree.ts +50 -42
- package/src/serve/connectors.ts +2 -1
- package/src/serve/context.ts +13 -4
- package/src/serve/doc-events.ts +3 -0
- package/src/serve/public/app.tsx +49 -21
- package/src/serve/public/components/BrowseTreeSidebar.tsx +163 -23
- package/src/serve/public/components/CaptureModal.tsx +14 -10
- package/src/serve/public/components/ai-elements/code-block.tsx +87 -50
- package/src/serve/public/components/editor/MarkdownPreview.tsx +99 -67
- package/src/serve/public/globals.built.css +1 -1
- package/src/serve/public/globals.css +19 -0
- package/src/serve/public/hooks/useCaptureModal.tsx +23 -10
- package/src/serve/public/lib/metadata-filter.ts +234 -9
- package/src/serve/public/pages/Browse.tsx +61 -27
- package/src/serve/public/pages/DocView.tsx +44 -25
- package/src/serve/public/pages/GraphView.tsx +8 -3
- package/src/serve/resident-runtime.ts +199 -48
- package/src/serve/routes/api.ts +98 -67
- package/src/serve/routes/links.ts +25 -5
- package/src/serve/server.ts +1370 -1180
- package/src/serve/spa-bundle-source.ts +52 -27
- package/src/serve/status.ts +101 -52
- package/src/serve/watch-reconciliation-fallback.ts +11 -7
- package/src/serve/watch-reconciliation-shared.ts +42 -4
- package/src/serve/watch-reconciliation.ts +21 -8
- package/src/serve/watch-service-events.ts +7 -1
- package/src/serve/watch-service-hosts.ts +3 -1
- package/src/serve/watch-service.ts +46 -21
- package/src/serve/watch-snapshot-scan.ts +6 -15
- package/src/sessions/archive.ts +20 -8
- package/src/sessions/sanitize.ts +38 -18
- package/src/sessions/service.ts +180 -102
- package/src/store/migrations/037-performance-indexes.ts +56 -0
- package/src/store/migrations/038-vector-mirror-null-cosine.ts +47 -0
- package/src/store/migrations/039-vector-mirror-v2.ts +94 -0
- package/src/store/migrations/index.ts +6 -0
- package/src/store/sqlite/adapter.ts +1260 -651
- package/src/store/sqlite/chunking-policy.ts +15 -1
- package/src/store/sqlite/eligibility.ts +110 -70
- package/src/store/sqlite/graph-neighbors.ts +71 -46
- package/src/store/sqlite/graph-reference-state.ts +27 -3
- package/src/store/sqlite/graph-similarity.ts +59 -12
- package/src/store/sqlite/metadata-predicate.ts +123 -28
- package/src/store/sqlite/retrieval-trace-management-store.ts +126 -33
- package/src/store/sqlite/retrieval-trace-retention.ts +88 -76
- package/src/store/sqlite/retrieval-trace-rows.ts +7 -3
- package/src/store/types.ts +32 -0
- package/src/store/vector/coarse-router.ts +312 -0
- package/src/store/vector/eligibility.ts +39 -0
- package/src/store/vector/lazy.ts +17 -0
- package/src/store/vector/partition-index.ts +333 -59
- package/src/store/vector/proof-cache.ts +209 -0
- package/src/store/vector/runtime-compat.ts +6 -0
- package/src/store/vector/sqlite-vec.ts +270 -162
- package/src/store/vector/stats.ts +37 -73
- package/src/store/vector/status.ts +169 -53
- package/src/store/vector/stored-vectors.ts +131 -45
- package/src/store/vector/types.ts +19 -0
- package/src/store/vector/v2-mirror.ts +669 -0
- package/src/store/vector/variant-search.ts +1187 -111
- package/src/store/vector/variants.ts +964 -160
- package/browser-extension/artifacts/gno-browser-clipper-v2.15.0.zip.sha256 +0 -1
|
@@ -18,8 +18,11 @@ import type {
|
|
|
18
18
|
} from "./types";
|
|
19
19
|
|
|
20
20
|
import { err, ok } from "../types";
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
21
|
+
import { buildEligibleVectorChunksQuery } from "./eligibility";
|
|
22
|
+
import {
|
|
23
|
+
searchVectorVariants,
|
|
24
|
+
searchVectorVariantsBatch,
|
|
25
|
+
} from "./variant-search";
|
|
23
26
|
|
|
24
27
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
25
28
|
// BLOB Encoding Helpers (avoid Buffer.buffer footgun)
|
|
@@ -164,6 +167,8 @@ export async function createVectorIndexPort(
|
|
|
164
167
|
|
|
165
168
|
// A query-only (read) connection cannot create the table. A missing table
|
|
166
169
|
// there means no vectors were ever stored for this model: search is empty.
|
|
170
|
+
// Once a v1 partition is active for this model, never create an unused legacy
|
|
171
|
+
// vec_<hash> table; variant search runs through vec_v1_<partition> instead.
|
|
167
172
|
const tableExists =
|
|
168
173
|
db
|
|
169
174
|
.query<{ found: number }, [string]>(
|
|
@@ -173,11 +178,24 @@ export async function createVectorIndexPort(
|
|
|
173
178
|
const queryOnly =
|
|
174
179
|
db.query<{ query_only: number }, []>("PRAGMA query_only").get()
|
|
175
180
|
?.query_only === 1;
|
|
181
|
+
const hasActivatedPartition =
|
|
182
|
+
db
|
|
183
|
+
.query<{ found: number }, []>(
|
|
184
|
+
"SELECT 1 AS found FROM sqlite_master WHERE type = 'table' AND name = 'vector_partitions'"
|
|
185
|
+
)
|
|
186
|
+
.get() !== null &&
|
|
187
|
+
db
|
|
188
|
+
.query<{ found: number }, [string]>(
|
|
189
|
+
"SELECT 1 AS found FROM vector_partitions WHERE model = ? AND state = 'active' AND activated_epoch IS NOT NULL LIMIT 1"
|
|
190
|
+
)
|
|
191
|
+
.get(model) !== null;
|
|
176
192
|
const noVectorsYet = searchAvailable && !tableExists && queryOnly;
|
|
193
|
+
const partitionActiveNoLegacy =
|
|
194
|
+
searchAvailable && !tableExists && hasActivatedPartition;
|
|
177
195
|
|
|
178
|
-
// Create per-model vec0 table if extension available
|
|
196
|
+
// Create per-model vec0 table if extension available and no v1 partition is active
|
|
179
197
|
// Graceful degradation: if table creation fails, storage still works
|
|
180
|
-
if (searchAvailable && !tableExists && !queryOnly) {
|
|
198
|
+
if (searchAvailable && !tableExists && !queryOnly && !hasActivatedPartition) {
|
|
181
199
|
try {
|
|
182
200
|
db.exec(`
|
|
183
201
|
CREATE VIRTUAL TABLE IF NOT EXISTS ${tableName} USING vec0(
|
|
@@ -204,33 +222,29 @@ export async function createVectorIndexPort(
|
|
|
204
222
|
DELETE FROM content_vectors WHERE mirror_hash = ? AND model = ?
|
|
205
223
|
`);
|
|
206
224
|
|
|
207
|
-
// Prepared statements for vec0 table (if available)
|
|
208
|
-
const
|
|
209
|
-
searchAvailable && !noVectorsYet
|
|
210
|
-
|
|
211
|
-
|
|
225
|
+
// Prepared statements for vec0 table (if available and legacy table exists)
|
|
226
|
+
const hasLegacyVecTable =
|
|
227
|
+
searchAvailable && !noVectorsYet && !partitionActiveNoLegacy;
|
|
228
|
+
const deleteVecChunkStmt = hasLegacyVecTable
|
|
229
|
+
? db.prepare(`DELETE FROM ${tableName} WHERE chunk_id = ?`)
|
|
230
|
+
: null;
|
|
212
231
|
|
|
213
|
-
const insertVecStmt =
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
`INSERT INTO ${tableName} (chunk_id, embedding) VALUES (?, ?)`
|
|
217
|
-
)
|
|
218
|
-
: null;
|
|
232
|
+
const insertVecStmt = hasLegacyVecTable
|
|
233
|
+
? db.prepare(`INSERT INTO ${tableName} (chunk_id, embedding) VALUES (?, ?)`)
|
|
234
|
+
: null;
|
|
219
235
|
|
|
220
|
-
const searchStmt =
|
|
221
|
-
|
|
222
|
-
? db.prepare(`
|
|
236
|
+
const searchStmt = hasLegacyVecTable
|
|
237
|
+
? db.prepare(`
|
|
223
238
|
SELECT chunk_id, distance
|
|
224
239
|
FROM ${tableName}
|
|
225
240
|
WHERE embedding MATCH ?
|
|
226
241
|
AND k = ?
|
|
227
242
|
`)
|
|
228
|
-
|
|
243
|
+
: null;
|
|
229
244
|
|
|
230
|
-
const deleteVecStmt =
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
: null;
|
|
245
|
+
const deleteVecStmt = hasLegacyVecTable
|
|
246
|
+
? db.prepare(`DELETE FROM ${tableName} WHERE chunk_id LIKE ? || ':%'`)
|
|
247
|
+
: null;
|
|
234
248
|
|
|
235
249
|
// Track if vec0 writes failed this session (needs sync)
|
|
236
250
|
let vecDirty = false;
|
|
@@ -240,20 +254,59 @@ export async function createVectorIndexPort(
|
|
|
240
254
|
rows: VectorRow[],
|
|
241
255
|
checkpoint?: (rows: VectorRow[]) => VectorRow[]
|
|
242
256
|
): Promise<StoreResult<number>> {
|
|
243
|
-
//
|
|
257
|
+
// Combine content_vectors write and best-effort vec0 update into one
|
|
258
|
+
// outer transaction; a SAVEPOINT isolates vec0 failures so committed
|
|
259
|
+
// content_vectors rows are preserved identically.
|
|
244
260
|
try {
|
|
245
|
-
db.transaction(() => {
|
|
261
|
+
const count = db.transaction(() => {
|
|
246
262
|
rows = checkpoint?.(rows) ?? rows;
|
|
247
|
-
|
|
263
|
+
const encoded = rows.map(
|
|
264
|
+
(row) =>
|
|
265
|
+
new Uint8Array(
|
|
266
|
+
row.embedding.buffer,
|
|
267
|
+
row.embedding.byteOffset,
|
|
268
|
+
row.embedding.byteLength
|
|
269
|
+
)
|
|
270
|
+
);
|
|
271
|
+
for (let i = 0; i < rows.length; i++) {
|
|
272
|
+
const row = rows[i]!;
|
|
248
273
|
upsertVectorStmt.run(
|
|
249
274
|
row.mirrorHash,
|
|
250
275
|
row.seq,
|
|
251
276
|
row.model,
|
|
252
277
|
row.embedFingerprint,
|
|
253
|
-
|
|
278
|
+
encoded[i]!
|
|
254
279
|
);
|
|
255
280
|
}
|
|
281
|
+
|
|
282
|
+
if (deleteVecChunkStmt && insertVecStmt && rows.length > 0) {
|
|
283
|
+
db.exec("SAVEPOINT vec0_upsert");
|
|
284
|
+
try {
|
|
285
|
+
for (let i = 0; i < rows.length; i++) {
|
|
286
|
+
const row = rows[i]!;
|
|
287
|
+
const chunkId = `${row.mirrorHash}:${row.seq}`;
|
|
288
|
+
// sqlite-vec vec0 tables do not reliably support OR REPLACE semantics.
|
|
289
|
+
// Delete first, then insert the fresh vector row.
|
|
290
|
+
deleteVecChunkStmt.run(chunkId);
|
|
291
|
+
insertVecStmt.run(chunkId, encoded[i]!);
|
|
292
|
+
}
|
|
293
|
+
db.exec("RELEASE SAVEPOINT vec0_upsert");
|
|
294
|
+
} catch (e) {
|
|
295
|
+
db.exec("ROLLBACK TO SAVEPOINT vec0_upsert");
|
|
296
|
+
db.exec("RELEASE SAVEPOINT vec0_upsert");
|
|
297
|
+
// Vec0 write failed - storage succeeded, search needs sync
|
|
298
|
+
vecDirty = true;
|
|
299
|
+
if (!vecErrorLogged) {
|
|
300
|
+
vecErrorLogged = true;
|
|
301
|
+
console.warn(
|
|
302
|
+
`[vec] Index write failed, will sync after embed: ${e instanceof Error ? e.message : String(e)}`
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return rows.length;
|
|
256
308
|
})();
|
|
309
|
+
return Promise.resolve(ok(count));
|
|
257
310
|
} catch (e) {
|
|
258
311
|
return Promise.resolve(
|
|
259
312
|
err(
|
|
@@ -263,32 +316,6 @@ export async function createVectorIndexPort(
|
|
|
263
316
|
)
|
|
264
317
|
);
|
|
265
318
|
}
|
|
266
|
-
|
|
267
|
-
// 2. Best-effort update vec0 (graceful degradation)
|
|
268
|
-
if (deleteVecChunkStmt && insertVecStmt) {
|
|
269
|
-
try {
|
|
270
|
-
db.transaction(() => {
|
|
271
|
-
for (const row of rows) {
|
|
272
|
-
const chunkId = `${row.mirrorHash}:${row.seq}`;
|
|
273
|
-
// sqlite-vec vec0 tables do not reliably support OR REPLACE semantics.
|
|
274
|
-
// Delete first, then insert the fresh vector row.
|
|
275
|
-
deleteVecChunkStmt.run(chunkId);
|
|
276
|
-
insertVecStmt.run(chunkId, encodeEmbedding(row.embedding));
|
|
277
|
-
}
|
|
278
|
-
})();
|
|
279
|
-
} catch (e) {
|
|
280
|
-
// Vec0 write failed - storage succeeded, search needs sync
|
|
281
|
-
vecDirty = true;
|
|
282
|
-
if (!vecErrorLogged) {
|
|
283
|
-
vecErrorLogged = true;
|
|
284
|
-
console.warn(
|
|
285
|
-
`[vec] Index write failed, will sync after embed: ${e instanceof Error ? e.message : String(e)}`
|
|
286
|
-
);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
return Promise.resolve(ok(rows.length));
|
|
292
319
|
}
|
|
293
320
|
|
|
294
321
|
const port: VectorIndexPort = {
|
|
@@ -341,7 +368,10 @@ export async function createVectorIndexPort(
|
|
|
341
368
|
k: number,
|
|
342
369
|
searchOptions?: VectorSearchOptions
|
|
343
370
|
): Promise<StoreResult<VectorSearchResult[]>> {
|
|
344
|
-
if (
|
|
371
|
+
if (
|
|
372
|
+
!searchAvailable ||
|
|
373
|
+
!(searchStmt || noVectorsYet || partitionActiveNoLegacy)
|
|
374
|
+
) {
|
|
345
375
|
return Promise.resolve(
|
|
346
376
|
err("VEC_SEARCH_UNAVAILABLE", formatUnavailableMessage(loadError))
|
|
347
377
|
);
|
|
@@ -363,7 +393,7 @@ export async function createVectorIndexPort(
|
|
|
363
393
|
);
|
|
364
394
|
if (variants !== null) return Promise.resolve(ok(variants));
|
|
365
395
|
if (searchOptions?.eligibility) {
|
|
366
|
-
const eligible =
|
|
396
|
+
const eligible = buildEligibleVectorChunksQuery(db, searchOptions);
|
|
367
397
|
const distanceFunction =
|
|
368
398
|
distanceMetric === "cosine"
|
|
369
399
|
? "vec_distance_cosine"
|
|
@@ -375,11 +405,16 @@ export async function createVectorIndexPort(
|
|
|
375
405
|
>(`
|
|
376
406
|
SELECT v.mirror_hash AS mirrorHash, v.seq,
|
|
377
407
|
${distanceFunction}(v.embedding, ?) AS distance
|
|
378
|
-
FROM
|
|
379
|
-
|
|
408
|
+
FROM (
|
|
409
|
+
${eligible.sql}
|
|
410
|
+
) e
|
|
411
|
+
JOIN content_vectors v
|
|
412
|
+
ON v.mirror_hash = e.mirror_hash
|
|
413
|
+
AND v.seq = e.seq
|
|
414
|
+
AND v.model = ?
|
|
380
415
|
ORDER BY distance, v.mirror_hash, v.seq LIMIT ?
|
|
381
416
|
`)
|
|
382
|
-
.all(encodeEmbedding(embedding),
|
|
417
|
+
.all(encodeEmbedding(embedding), ...eligible.params, model, k);
|
|
383
418
|
return Promise.resolve(
|
|
384
419
|
ok(
|
|
385
420
|
rows.filter(
|
|
@@ -400,50 +435,87 @@ export async function createVectorIndexPort(
|
|
|
400
435
|
distanceMetric === "cosine"
|
|
401
436
|
? "vec_distance_cosine"
|
|
402
437
|
: "vec_distance_L2";
|
|
403
|
-
const
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
438
|
+
const queryVector = encodeEmbedding(embedding);
|
|
439
|
+
const scopedStmt = db.query<
|
|
440
|
+
{
|
|
441
|
+
mirror_hash: string;
|
|
442
|
+
seq: number;
|
|
443
|
+
distance: number;
|
|
444
|
+
},
|
|
445
|
+
[Uint8Array, string, string, number]
|
|
446
|
+
>(
|
|
447
|
+
`SELECT mirror_hash, seq,
|
|
448
|
+
${distanceFunction}(embedding, ?) AS distance
|
|
449
|
+
FROM content_vectors
|
|
450
|
+
WHERE model = ?
|
|
451
|
+
AND mirror_hash IN (SELECT value FROM json_each(?))
|
|
452
|
+
ORDER BY distance, mirror_hash, seq
|
|
453
|
+
LIMIT ?`
|
|
454
|
+
);
|
|
455
|
+
const rows = scopedStmt.all(
|
|
456
|
+
queryVector,
|
|
457
|
+
model,
|
|
458
|
+
JSON.stringify(hashes),
|
|
459
|
+
k
|
|
460
|
+
);
|
|
461
|
+
const minScore = searchOptions.minScore;
|
|
462
|
+
// SQLite sorts NULL distances (e.g. zero vectors under cosine) first.
|
|
463
|
+
// When hashes span multiple legacy batches and a NULL distance appears
|
|
464
|
+
// in the top-k, preserve exact per-batch SQL selection + JS merge order.
|
|
465
|
+
if (
|
|
466
|
+
hashes.length > ALLOWED_HASH_BATCH_SIZE &&
|
|
467
|
+
rows.length > 0 &&
|
|
468
|
+
rows[0]!.distance === null
|
|
408
469
|
) {
|
|
409
|
-
const
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
.
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
470
|
+
const scoped: VectorSearchResult[] = [];
|
|
471
|
+
for (
|
|
472
|
+
let index = 0;
|
|
473
|
+
index < hashes.length;
|
|
474
|
+
index += ALLOWED_HASH_BATCH_SIZE
|
|
475
|
+
) {
|
|
476
|
+
const batch = hashes.slice(
|
|
477
|
+
index,
|
|
478
|
+
index + ALLOWED_HASH_BATCH_SIZE
|
|
479
|
+
);
|
|
480
|
+
const batchRows = scopedStmt.all(
|
|
481
|
+
queryVector,
|
|
482
|
+
model,
|
|
483
|
+
JSON.stringify(batch),
|
|
484
|
+
k
|
|
485
|
+
);
|
|
486
|
+
scoped.push(
|
|
487
|
+
...batchRows.map((row) => ({
|
|
488
|
+
mirrorHash: row.mirror_hash,
|
|
489
|
+
seq: row.seq,
|
|
490
|
+
distance: row.distance,
|
|
491
|
+
}))
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
return Promise.resolve(
|
|
495
|
+
ok(
|
|
496
|
+
scoped
|
|
497
|
+
.sort(compareVectorResults)
|
|
498
|
+
.filter(
|
|
499
|
+
(row) =>
|
|
500
|
+
minScore === undefined || 1 - row.distance >= minScore
|
|
501
|
+
)
|
|
502
|
+
.slice(0, k)
|
|
427
503
|
)
|
|
428
|
-
.all(encodeEmbedding(embedding), model, ...batch, k);
|
|
429
|
-
scoped.push(
|
|
430
|
-
...rows.map((row) => ({
|
|
431
|
-
mirrorHash: row.mirror_hash,
|
|
432
|
-
seq: row.seq,
|
|
433
|
-
distance: row.distance,
|
|
434
|
-
}))
|
|
435
504
|
);
|
|
436
505
|
}
|
|
437
|
-
const minScore = searchOptions.minScore;
|
|
438
506
|
return Promise.resolve(
|
|
439
507
|
ok(
|
|
440
|
-
|
|
508
|
+
rows
|
|
509
|
+
.map((row) => ({
|
|
510
|
+
mirrorHash: row.mirror_hash,
|
|
511
|
+
seq: row.seq,
|
|
512
|
+
distance: row.distance,
|
|
513
|
+
}))
|
|
441
514
|
.sort(compareVectorResults)
|
|
442
515
|
.filter(
|
|
443
516
|
(row) =>
|
|
444
517
|
minScore === undefined || 1 - row.distance >= minScore
|
|
445
518
|
)
|
|
446
|
-
.slice(0, k)
|
|
447
519
|
)
|
|
448
520
|
);
|
|
449
521
|
}
|
|
@@ -486,41 +558,97 @@ export async function createVectorIndexPort(
|
|
|
486
558
|
}
|
|
487
559
|
},
|
|
488
560
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
561
|
+
async searchNearestBatch(
|
|
562
|
+
embeddings: Float32Array[],
|
|
563
|
+
limits: number[],
|
|
564
|
+
searchOptions?: VectorSearchOptions
|
|
565
|
+
): Promise<StoreResult<VectorSearchResult[][]>> {
|
|
566
|
+
if (
|
|
567
|
+
!searchAvailable ||
|
|
568
|
+
!(searchStmt || noVectorsYet || partitionActiveNoLegacy)
|
|
569
|
+
) {
|
|
570
|
+
return err(
|
|
571
|
+
"VEC_SEARCH_UNAVAILABLE",
|
|
572
|
+
formatUnavailableMessage(loadError)
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
if (embeddings.length !== limits.length) {
|
|
576
|
+
return err(
|
|
577
|
+
"INVALID_INPUT",
|
|
578
|
+
"Vector batch search embeddings and limits must have equal length"
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
for (const k of limits) {
|
|
582
|
+
if (!Number.isSafeInteger(k) || k < 1) {
|
|
583
|
+
return err(
|
|
584
|
+
"INVALID_INPUT",
|
|
585
|
+
"Vector search limit must be a positive integer"
|
|
586
|
+
);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
if (embeddings.length === 0) {
|
|
590
|
+
return ok([]);
|
|
591
|
+
}
|
|
592
|
+
if (embeddings.length === 1) {
|
|
593
|
+
const single = await port.searchNearest(
|
|
594
|
+
embeddings[0]!,
|
|
595
|
+
limits[0]!,
|
|
596
|
+
searchOptions
|
|
597
|
+
);
|
|
598
|
+
return single.ok ? ok([single.value]) : single;
|
|
492
599
|
}
|
|
493
600
|
|
|
494
601
|
try {
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
602
|
+
const batch = searchVectorVariantsBatch(
|
|
603
|
+
db,
|
|
604
|
+
model,
|
|
605
|
+
dimensions,
|
|
606
|
+
embeddings,
|
|
607
|
+
limits,
|
|
608
|
+
searchOptions
|
|
609
|
+
);
|
|
610
|
+
if (batch !== null) return ok(batch);
|
|
611
|
+
} catch (e) {
|
|
612
|
+
return err(
|
|
613
|
+
"VEC_SEARCH_FAILED",
|
|
614
|
+
`Vector search failed: ${e instanceof Error ? e.message : String(e)}`
|
|
615
|
+
);
|
|
616
|
+
}
|
|
503
617
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
618
|
+
const legacyResults: VectorSearchResult[][] = [];
|
|
619
|
+
for (let i = 0; i < embeddings.length; i++) {
|
|
620
|
+
const single = await port.searchNearest(
|
|
621
|
+
embeddings[i]!,
|
|
622
|
+
limits[i]!,
|
|
623
|
+
searchOptions
|
|
624
|
+
);
|
|
625
|
+
if (!single.ok) return single;
|
|
626
|
+
legacyResults.push(single.value);
|
|
627
|
+
}
|
|
628
|
+
return ok(legacyResults);
|
|
629
|
+
},
|
|
514
630
|
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
631
|
+
rebuildVecIndex(): Promise<StoreResult<void>> {
|
|
632
|
+
if (!searchAvailable || partitionActiveNoLegacy) {
|
|
633
|
+
return Promise.resolve(ok(undefined)); // No-op if no vec support or v1 active without legacy table
|
|
634
|
+
}
|
|
518
635
|
|
|
636
|
+
try {
|
|
637
|
+
// Drop, recreate, and repopulate vec table in pure SQL inside one transaction
|
|
519
638
|
db.transaction(() => {
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
639
|
+
db.exec(`DROP TABLE IF EXISTS ${tableName}`);
|
|
640
|
+
db.exec(`
|
|
641
|
+
CREATE VIRTUAL TABLE ${tableName} USING vec0(
|
|
642
|
+
chunk_id TEXT PRIMARY KEY,
|
|
643
|
+
embedding FLOAT[${dimensions}] distance_metric=${distanceMetric}
|
|
644
|
+
);
|
|
645
|
+
`);
|
|
646
|
+
db.query(
|
|
647
|
+
`INSERT INTO ${tableName} (chunk_id, embedding)
|
|
648
|
+
SELECT mirror_hash || ':' || seq, embedding
|
|
649
|
+
FROM content_vectors
|
|
650
|
+
WHERE model = ?`
|
|
651
|
+
).run(model);
|
|
524
652
|
})();
|
|
525
653
|
|
|
526
654
|
return Promise.resolve(ok(undefined));
|
|
@@ -535,57 +663,37 @@ export async function createVectorIndexPort(
|
|
|
535
663
|
},
|
|
536
664
|
|
|
537
665
|
syncVecIndex(): Promise<StoreResult<{ added: number; removed: number }>> {
|
|
538
|
-
if (!searchAvailable) {
|
|
666
|
+
if (!searchAvailable || partitionActiveNoLegacy) {
|
|
539
667
|
return Promise.resolve(ok({ added: 0, removed: 0 }));
|
|
540
668
|
}
|
|
541
669
|
|
|
542
670
|
try {
|
|
543
|
-
// Wrap entire sync in transaction for atomicity
|
|
671
|
+
// Wrap entire sync in transaction for atomicity, pure SQL without pulling BLOBs into JS
|
|
544
672
|
const result = db.transaction(() => {
|
|
545
673
|
// 1. Remove orphans from vec table (not in content_vectors for this model)
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
)
|
|
557
|
-
.run(model);
|
|
558
|
-
const removed = orphanResult.changes;
|
|
674
|
+
db.query(
|
|
675
|
+
`DELETE FROM ${tableName}
|
|
676
|
+
WHERE chunk_id NOT IN (
|
|
677
|
+
SELECT mirror_hash || ':' || seq
|
|
678
|
+
FROM content_vectors
|
|
679
|
+
WHERE model = ?
|
|
680
|
+
)`
|
|
681
|
+
).run(model);
|
|
682
|
+
const removed =
|
|
683
|
+
db.query<{ c: number }, []>("SELECT changes() AS c").get()?.c ?? 0;
|
|
559
684
|
|
|
560
685
|
// 2. Add missing entries (in content_vectors but not in vec table)
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
)
|
|
572
|
-
.all(model) as {
|
|
573
|
-
mirror_hash: string;
|
|
574
|
-
seq: number;
|
|
575
|
-
embedding: Uint8Array;
|
|
576
|
-
}[];
|
|
577
|
-
|
|
578
|
-
let added = 0;
|
|
579
|
-
if (missing.length > 0) {
|
|
580
|
-
const insertStmt = db.prepare(`
|
|
581
|
-
INSERT INTO ${tableName} (chunk_id, embedding) VALUES (?, ?)
|
|
582
|
-
`);
|
|
583
|
-
for (const row of missing) {
|
|
584
|
-
const chunkId = `${row.mirror_hash}:${row.seq}`;
|
|
585
|
-
insertStmt.run(chunkId, row.embedding);
|
|
586
|
-
}
|
|
587
|
-
added = missing.length;
|
|
588
|
-
}
|
|
686
|
+
db.query(
|
|
687
|
+
`INSERT INTO ${tableName} (chunk_id, embedding)
|
|
688
|
+
SELECT cv.mirror_hash || ':' || cv.seq, cv.embedding
|
|
689
|
+
FROM content_vectors cv
|
|
690
|
+
WHERE cv.model = ?
|
|
691
|
+
AND (cv.mirror_hash || ':' || cv.seq) NOT IN (
|
|
692
|
+
SELECT chunk_id FROM ${tableName}
|
|
693
|
+
)`
|
|
694
|
+
).run(model);
|
|
695
|
+
const added =
|
|
696
|
+
db.query<{ c: number }, []>("SELECT changes() AS c").get()?.c ?? 0;
|
|
589
697
|
|
|
590
698
|
return { added, removed };
|
|
591
699
|
})();
|