@gmickel/gno 2.14.0 → 2.16.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/cli-reference.md +4 -2
- package/assets/spa-production.json.gz +0 -0
- package/browser-extension/artifacts/{gno-browser-clipper-v2.14.0.zip → gno-browser-clipper-v2.16.0.zip} +0 -0
- package/browser-extension/artifacts/gno-browser-clipper-v2.16.0.zip.sha256 +1 -0
- package/browser-extension/dist/manifest.json +1 -1
- package/package.json +1 -1
- package/spec/cli.md +88 -45
- package/spec/db/schema.sql +45 -0
- package/spec/output-schemas/embed-receipt.schema.json +89 -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/embed.ts +341 -20
- package/src/cli/commands/links.ts +1 -1
- 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/config-cache.ts +86 -0
- package/src/cli/globals.ts +64 -0
- package/src/cli/options.ts +23 -0
- package/src/cli/program.ts +44 -37
- 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/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/section-parse.ts +16 -6
- package/src/core/typed-metadata.ts +68 -0
- package/src/core/wiki-link-syntax.ts +88 -0
- package/src/embed/backlog.ts +173 -60
- package/src/embed/batch.ts +83 -12
- package/src/embed/resume-cursor.ts +103 -0
- package/src/embed/retry.ts +41 -32
- package/src/embed/variant-backlog.ts +697 -63
- package/src/embed/variant-plan.ts +41 -45
- package/src/embed/variant-retry.ts +157 -44
- 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 +7 -5
- 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/filters.ts +169 -33
- package/src/pipeline/graph-retrieval.ts +117 -61
- package/src/pipeline/hybrid.ts +598 -329
- 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/vsearch.ts +250 -152
- 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 +18 -4
- 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/036-vector-mirror-state.ts +83 -0
- package/src/store/migrations/037-performance-indexes.ts +56 -0
- package/src/store/migrations/index.ts +4 -0
- package/src/store/sqlite/adapter.ts +1217 -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/eligibility.ts +39 -0
- package/src/store/vector/lazy.ts +17 -0
- package/src/store/vector/partition-index.ts +49 -47
- package/src/store/vector/proof-cache.ts +103 -0
- package/src/store/vector/sqlite-vec.ts +230 -139
- package/src/store/vector/stats.ts +29 -73
- package/src/store/vector/status.ts +151 -52
- package/src/store/vector/stored-vectors.ts +131 -45
- package/src/store/vector/types.ts +10 -0
- package/src/store/vector/variant-search.ts +771 -109
- package/src/store/vector/variants.ts +785 -144
- package/browser-extension/artifacts/gno-browser-clipper-v2.14.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)
|
|
@@ -240,20 +243,59 @@ export async function createVectorIndexPort(
|
|
|
240
243
|
rows: VectorRow[],
|
|
241
244
|
checkpoint?: (rows: VectorRow[]) => VectorRow[]
|
|
242
245
|
): Promise<StoreResult<number>> {
|
|
243
|
-
//
|
|
246
|
+
// Combine content_vectors write and best-effort vec0 update into one
|
|
247
|
+
// outer transaction; a SAVEPOINT isolates vec0 failures so committed
|
|
248
|
+
// content_vectors rows are preserved identically.
|
|
244
249
|
try {
|
|
245
|
-
db.transaction(() => {
|
|
250
|
+
const count = db.transaction(() => {
|
|
246
251
|
rows = checkpoint?.(rows) ?? rows;
|
|
247
|
-
|
|
252
|
+
const encoded = rows.map(
|
|
253
|
+
(row) =>
|
|
254
|
+
new Uint8Array(
|
|
255
|
+
row.embedding.buffer,
|
|
256
|
+
row.embedding.byteOffset,
|
|
257
|
+
row.embedding.byteLength
|
|
258
|
+
)
|
|
259
|
+
);
|
|
260
|
+
for (let i = 0; i < rows.length; i++) {
|
|
261
|
+
const row = rows[i]!;
|
|
248
262
|
upsertVectorStmt.run(
|
|
249
263
|
row.mirrorHash,
|
|
250
264
|
row.seq,
|
|
251
265
|
row.model,
|
|
252
266
|
row.embedFingerprint,
|
|
253
|
-
|
|
267
|
+
encoded[i]!
|
|
254
268
|
);
|
|
255
269
|
}
|
|
270
|
+
|
|
271
|
+
if (deleteVecChunkStmt && insertVecStmt && rows.length > 0) {
|
|
272
|
+
db.exec("SAVEPOINT vec0_upsert");
|
|
273
|
+
try {
|
|
274
|
+
for (let i = 0; i < rows.length; i++) {
|
|
275
|
+
const row = rows[i]!;
|
|
276
|
+
const chunkId = `${row.mirrorHash}:${row.seq}`;
|
|
277
|
+
// sqlite-vec vec0 tables do not reliably support OR REPLACE semantics.
|
|
278
|
+
// Delete first, then insert the fresh vector row.
|
|
279
|
+
deleteVecChunkStmt.run(chunkId);
|
|
280
|
+
insertVecStmt.run(chunkId, encoded[i]!);
|
|
281
|
+
}
|
|
282
|
+
db.exec("RELEASE SAVEPOINT vec0_upsert");
|
|
283
|
+
} catch (e) {
|
|
284
|
+
db.exec("ROLLBACK TO SAVEPOINT vec0_upsert");
|
|
285
|
+
db.exec("RELEASE SAVEPOINT vec0_upsert");
|
|
286
|
+
// Vec0 write failed - storage succeeded, search needs sync
|
|
287
|
+
vecDirty = true;
|
|
288
|
+
if (!vecErrorLogged) {
|
|
289
|
+
vecErrorLogged = true;
|
|
290
|
+
console.warn(
|
|
291
|
+
`[vec] Index write failed, will sync after embed: ${e instanceof Error ? e.message : String(e)}`
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return rows.length;
|
|
256
297
|
})();
|
|
298
|
+
return Promise.resolve(ok(count));
|
|
257
299
|
} catch (e) {
|
|
258
300
|
return Promise.resolve(
|
|
259
301
|
err(
|
|
@@ -263,32 +305,6 @@ export async function createVectorIndexPort(
|
|
|
263
305
|
)
|
|
264
306
|
);
|
|
265
307
|
}
|
|
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
308
|
}
|
|
293
309
|
|
|
294
310
|
const port: VectorIndexPort = {
|
|
@@ -363,7 +379,7 @@ export async function createVectorIndexPort(
|
|
|
363
379
|
);
|
|
364
380
|
if (variants !== null) return Promise.resolve(ok(variants));
|
|
365
381
|
if (searchOptions?.eligibility) {
|
|
366
|
-
const eligible =
|
|
382
|
+
const eligible = buildEligibleVectorChunksQuery(db, searchOptions);
|
|
367
383
|
const distanceFunction =
|
|
368
384
|
distanceMetric === "cosine"
|
|
369
385
|
? "vec_distance_cosine"
|
|
@@ -375,11 +391,16 @@ export async function createVectorIndexPort(
|
|
|
375
391
|
>(`
|
|
376
392
|
SELECT v.mirror_hash AS mirrorHash, v.seq,
|
|
377
393
|
${distanceFunction}(v.embedding, ?) AS distance
|
|
378
|
-
FROM
|
|
379
|
-
|
|
394
|
+
FROM (
|
|
395
|
+
${eligible.sql}
|
|
396
|
+
) e
|
|
397
|
+
JOIN content_vectors v
|
|
398
|
+
ON v.mirror_hash = e.mirror_hash
|
|
399
|
+
AND v.seq = e.seq
|
|
400
|
+
AND v.model = ?
|
|
380
401
|
ORDER BY distance, v.mirror_hash, v.seq LIMIT ?
|
|
381
402
|
`)
|
|
382
|
-
.all(encodeEmbedding(embedding),
|
|
403
|
+
.all(encodeEmbedding(embedding), ...eligible.params, model, k);
|
|
383
404
|
return Promise.resolve(
|
|
384
405
|
ok(
|
|
385
406
|
rows.filter(
|
|
@@ -400,50 +421,87 @@ export async function createVectorIndexPort(
|
|
|
400
421
|
distanceMetric === "cosine"
|
|
401
422
|
? "vec_distance_cosine"
|
|
402
423
|
: "vec_distance_L2";
|
|
403
|
-
const
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
424
|
+
const queryVector = encodeEmbedding(embedding);
|
|
425
|
+
const scopedStmt = db.query<
|
|
426
|
+
{
|
|
427
|
+
mirror_hash: string;
|
|
428
|
+
seq: number;
|
|
429
|
+
distance: number;
|
|
430
|
+
},
|
|
431
|
+
[Uint8Array, string, string, number]
|
|
432
|
+
>(
|
|
433
|
+
`SELECT mirror_hash, seq,
|
|
434
|
+
${distanceFunction}(embedding, ?) AS distance
|
|
435
|
+
FROM content_vectors
|
|
436
|
+
WHERE model = ?
|
|
437
|
+
AND mirror_hash IN (SELECT value FROM json_each(?))
|
|
438
|
+
ORDER BY distance, mirror_hash, seq
|
|
439
|
+
LIMIT ?`
|
|
440
|
+
);
|
|
441
|
+
const rows = scopedStmt.all(
|
|
442
|
+
queryVector,
|
|
443
|
+
model,
|
|
444
|
+
JSON.stringify(hashes),
|
|
445
|
+
k
|
|
446
|
+
);
|
|
447
|
+
const minScore = searchOptions.minScore;
|
|
448
|
+
// SQLite sorts NULL distances (e.g. zero vectors under cosine) first.
|
|
449
|
+
// When hashes span multiple legacy batches and a NULL distance appears
|
|
450
|
+
// in the top-k, preserve exact per-batch SQL selection + JS merge order.
|
|
451
|
+
if (
|
|
452
|
+
hashes.length > ALLOWED_HASH_BATCH_SIZE &&
|
|
453
|
+
rows.length > 0 &&
|
|
454
|
+
rows[0]!.distance === null
|
|
408
455
|
) {
|
|
409
|
-
const
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
.
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
456
|
+
const scoped: VectorSearchResult[] = [];
|
|
457
|
+
for (
|
|
458
|
+
let index = 0;
|
|
459
|
+
index < hashes.length;
|
|
460
|
+
index += ALLOWED_HASH_BATCH_SIZE
|
|
461
|
+
) {
|
|
462
|
+
const batch = hashes.slice(
|
|
463
|
+
index,
|
|
464
|
+
index + ALLOWED_HASH_BATCH_SIZE
|
|
465
|
+
);
|
|
466
|
+
const batchRows = scopedStmt.all(
|
|
467
|
+
queryVector,
|
|
468
|
+
model,
|
|
469
|
+
JSON.stringify(batch),
|
|
470
|
+
k
|
|
471
|
+
);
|
|
472
|
+
scoped.push(
|
|
473
|
+
...batchRows.map((row) => ({
|
|
474
|
+
mirrorHash: row.mirror_hash,
|
|
475
|
+
seq: row.seq,
|
|
476
|
+
distance: row.distance,
|
|
477
|
+
}))
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
return Promise.resolve(
|
|
481
|
+
ok(
|
|
482
|
+
scoped
|
|
483
|
+
.sort(compareVectorResults)
|
|
484
|
+
.filter(
|
|
485
|
+
(row) =>
|
|
486
|
+
minScore === undefined || 1 - row.distance >= minScore
|
|
487
|
+
)
|
|
488
|
+
.slice(0, k)
|
|
427
489
|
)
|
|
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
490
|
);
|
|
436
491
|
}
|
|
437
|
-
const minScore = searchOptions.minScore;
|
|
438
492
|
return Promise.resolve(
|
|
439
493
|
ok(
|
|
440
|
-
|
|
494
|
+
rows
|
|
495
|
+
.map((row) => ({
|
|
496
|
+
mirrorHash: row.mirror_hash,
|
|
497
|
+
seq: row.seq,
|
|
498
|
+
distance: row.distance,
|
|
499
|
+
}))
|
|
441
500
|
.sort(compareVectorResults)
|
|
442
501
|
.filter(
|
|
443
502
|
(row) =>
|
|
444
503
|
minScore === undefined || 1 - row.distance >= minScore
|
|
445
504
|
)
|
|
446
|
-
.slice(0, k)
|
|
447
505
|
)
|
|
448
506
|
);
|
|
449
507
|
}
|
|
@@ -486,41 +544,94 @@ export async function createVectorIndexPort(
|
|
|
486
544
|
}
|
|
487
545
|
},
|
|
488
546
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
547
|
+
async searchNearestBatch(
|
|
548
|
+
embeddings: Float32Array[],
|
|
549
|
+
limits: number[],
|
|
550
|
+
searchOptions?: VectorSearchOptions
|
|
551
|
+
): Promise<StoreResult<VectorSearchResult[][]>> {
|
|
552
|
+
if (!searchAvailable || !(searchStmt || noVectorsYet)) {
|
|
553
|
+
return err(
|
|
554
|
+
"VEC_SEARCH_UNAVAILABLE",
|
|
555
|
+
formatUnavailableMessage(loadError)
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
if (embeddings.length !== limits.length) {
|
|
559
|
+
return err(
|
|
560
|
+
"INVALID_INPUT",
|
|
561
|
+
"Vector batch search embeddings and limits must have equal length"
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
for (const k of limits) {
|
|
565
|
+
if (!Number.isSafeInteger(k) || k < 1) {
|
|
566
|
+
return err(
|
|
567
|
+
"INVALID_INPUT",
|
|
568
|
+
"Vector search limit must be a positive integer"
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
if (embeddings.length === 0) {
|
|
573
|
+
return ok([]);
|
|
574
|
+
}
|
|
575
|
+
if (embeddings.length === 1) {
|
|
576
|
+
const single = await port.searchNearest(
|
|
577
|
+
embeddings[0]!,
|
|
578
|
+
limits[0]!,
|
|
579
|
+
searchOptions
|
|
580
|
+
);
|
|
581
|
+
return single.ok ? ok([single.value]) : single;
|
|
492
582
|
}
|
|
493
583
|
|
|
494
584
|
try {
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
585
|
+
const batch = searchVectorVariantsBatch(
|
|
586
|
+
db,
|
|
587
|
+
model,
|
|
588
|
+
dimensions,
|
|
589
|
+
embeddings,
|
|
590
|
+
limits,
|
|
591
|
+
searchOptions
|
|
592
|
+
);
|
|
593
|
+
if (batch !== null) return ok(batch);
|
|
594
|
+
} catch (e) {
|
|
595
|
+
return err(
|
|
596
|
+
"VEC_SEARCH_FAILED",
|
|
597
|
+
`Vector search failed: ${e instanceof Error ? e.message : String(e)}`
|
|
598
|
+
);
|
|
599
|
+
}
|
|
503
600
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
601
|
+
const legacyResults: VectorSearchResult[][] = [];
|
|
602
|
+
for (let i = 0; i < embeddings.length; i++) {
|
|
603
|
+
const single = await port.searchNearest(
|
|
604
|
+
embeddings[i]!,
|
|
605
|
+
limits[i]!,
|
|
606
|
+
searchOptions
|
|
607
|
+
);
|
|
608
|
+
if (!single.ok) return single;
|
|
609
|
+
legacyResults.push(single.value);
|
|
610
|
+
}
|
|
611
|
+
return ok(legacyResults);
|
|
612
|
+
},
|
|
514
613
|
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
614
|
+
rebuildVecIndex(): Promise<StoreResult<void>> {
|
|
615
|
+
if (!searchAvailable) {
|
|
616
|
+
return Promise.resolve(ok(undefined)); // No-op if no vec support
|
|
617
|
+
}
|
|
518
618
|
|
|
619
|
+
try {
|
|
620
|
+
// Drop, recreate, and repopulate vec table in pure SQL inside one transaction
|
|
519
621
|
db.transaction(() => {
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
622
|
+
db.exec(`DROP TABLE IF EXISTS ${tableName}`);
|
|
623
|
+
db.exec(`
|
|
624
|
+
CREATE VIRTUAL TABLE ${tableName} USING vec0(
|
|
625
|
+
chunk_id TEXT PRIMARY KEY,
|
|
626
|
+
embedding FLOAT[${dimensions}] distance_metric=${distanceMetric}
|
|
627
|
+
);
|
|
628
|
+
`);
|
|
629
|
+
db.query(
|
|
630
|
+
`INSERT INTO ${tableName} (chunk_id, embedding)
|
|
631
|
+
SELECT mirror_hash || ':' || seq, embedding
|
|
632
|
+
FROM content_vectors
|
|
633
|
+
WHERE model = ?`
|
|
634
|
+
).run(model);
|
|
524
635
|
})();
|
|
525
636
|
|
|
526
637
|
return Promise.resolve(ok(undefined));
|
|
@@ -540,52 +651,32 @@ export async function createVectorIndexPort(
|
|
|
540
651
|
}
|
|
541
652
|
|
|
542
653
|
try {
|
|
543
|
-
// Wrap entire sync in transaction for atomicity
|
|
654
|
+
// Wrap entire sync in transaction for atomicity, pure SQL without pulling BLOBs into JS
|
|
544
655
|
const result = db.transaction(() => {
|
|
545
656
|
// 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;
|
|
657
|
+
db.query(
|
|
658
|
+
`DELETE FROM ${tableName}
|
|
659
|
+
WHERE chunk_id NOT IN (
|
|
660
|
+
SELECT mirror_hash || ':' || seq
|
|
661
|
+
FROM content_vectors
|
|
662
|
+
WHERE model = ?
|
|
663
|
+
)`
|
|
664
|
+
).run(model);
|
|
665
|
+
const removed =
|
|
666
|
+
db.query<{ c: number }, []>("SELECT changes() AS c").get()?.c ?? 0;
|
|
559
667
|
|
|
560
668
|
// 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
|
-
}
|
|
669
|
+
db.query(
|
|
670
|
+
`INSERT INTO ${tableName} (chunk_id, embedding)
|
|
671
|
+
SELECT cv.mirror_hash || ':' || cv.seq, cv.embedding
|
|
672
|
+
FROM content_vectors cv
|
|
673
|
+
WHERE cv.model = ?
|
|
674
|
+
AND (cv.mirror_hash || ':' || cv.seq) NOT IN (
|
|
675
|
+
SELECT chunk_id FROM ${tableName}
|
|
676
|
+
)`
|
|
677
|
+
).run(model);
|
|
678
|
+
const added =
|
|
679
|
+
db.query<{ c: number }, []>("SELECT changes() AS c").get()?.c ?? 0;
|
|
589
680
|
|
|
590
681
|
return { added, removed };
|
|
591
682
|
})();
|
|
@@ -57,10 +57,10 @@ export function createVectorStatsPort(db: Database): VectorStatsPort {
|
|
|
57
57
|
countVectors(model: string): Promise<StoreResult<number>> {
|
|
58
58
|
try {
|
|
59
59
|
const result = db
|
|
60
|
-
.
|
|
60
|
+
.query<{ count: number }, [string]>(
|
|
61
61
|
"SELECT COUNT(*) as count FROM content_vectors WHERE model = ?"
|
|
62
62
|
)
|
|
63
|
-
.get(model)
|
|
63
|
+
.get(model)!;
|
|
64
64
|
return Promise.resolve(ok(result.count));
|
|
65
65
|
} catch (e) {
|
|
66
66
|
return Promise.resolve(
|
|
@@ -84,21 +84,17 @@ export function createVectorStatsPort(db: Database): VectorStatsPort {
|
|
|
84
84
|
const sql = `
|
|
85
85
|
SELECT COUNT(*) as count
|
|
86
86
|
FROM content_chunks c
|
|
87
|
+
LEFT JOIN content_vectors v
|
|
88
|
+
ON v.mirror_hash = c.mirror_hash
|
|
89
|
+
AND v.seq = c.seq
|
|
90
|
+
AND v.model = ?
|
|
91
|
+
AND v.embed_fingerprint = ?
|
|
87
92
|
WHERE ${activeDoc.sql}
|
|
88
|
-
|
|
89
|
-
SELECT 1 FROM content_vectors v
|
|
90
|
-
WHERE v.mirror_hash = c.mirror_hash
|
|
91
|
-
AND v.seq = c.seq
|
|
92
|
-
AND v.model = ?
|
|
93
|
-
AND v.embed_fingerprint = ?
|
|
94
|
-
AND v.embedded_at >= c.created_at
|
|
95
|
-
)
|
|
93
|
+
AND (v.mirror_hash IS NULL OR v.embedded_at IS NULL OR v.embedded_at < c.created_at)
|
|
96
94
|
`;
|
|
97
95
|
const result = db
|
|
98
|
-
.
|
|
99
|
-
.get(...activeDoc.params
|
|
100
|
-
count: number;
|
|
101
|
-
};
|
|
96
|
+
.query<{ count: number }, string[]>(sql)
|
|
97
|
+
.get(model, embedFingerprint, ...activeDoc.params)!;
|
|
102
98
|
return Promise.resolve(ok(result.count));
|
|
103
99
|
} catch (e) {
|
|
104
100
|
return Promise.resolve(
|
|
@@ -124,86 +120,46 @@ export function createVectorStatsPort(db: Database): VectorStatsPort {
|
|
|
124
120
|
const after = options?.after;
|
|
125
121
|
const activeDoc = buildActiveDocumentExistsClause(options?.collection);
|
|
126
122
|
|
|
127
|
-
// Seek pagination:
|
|
128
|
-
//
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
(SELECT d.title FROM documents d WHERE d.mirror_hash = c.mirror_hash AND d.active = 1 ORDER BY d.id LIMIT 1) as title,
|
|
134
|
-
CASE
|
|
135
|
-
WHEN NOT EXISTS (
|
|
136
|
-
SELECT 1 FROM content_vectors v
|
|
137
|
-
WHERE v.mirror_hash = c.mirror_hash
|
|
138
|
-
AND v.seq = c.seq
|
|
139
|
-
AND v.model = ?
|
|
140
|
-
AND v.embed_fingerprint = ?
|
|
141
|
-
) THEN 'new'
|
|
142
|
-
ELSE 'changed'
|
|
143
|
-
END as reason
|
|
144
|
-
FROM content_chunks c
|
|
145
|
-
WHERE ${activeDoc.sql}
|
|
146
|
-
AND NOT EXISTS (
|
|
147
|
-
SELECT 1 FROM content_vectors v
|
|
148
|
-
WHERE v.mirror_hash = c.mirror_hash
|
|
149
|
-
AND v.seq = c.seq
|
|
150
|
-
AND v.model = ?
|
|
151
|
-
AND v.embed_fingerprint = ?
|
|
152
|
-
AND v.embedded_at >= c.created_at
|
|
153
|
-
)
|
|
154
|
-
AND (c.mirror_hash > ? OR (c.mirror_hash = ? AND c.seq > ?))
|
|
155
|
-
ORDER BY c.mirror_hash, c.seq
|
|
156
|
-
LIMIT ?
|
|
157
|
-
`
|
|
158
|
-
: `
|
|
123
|
+
// Seek pagination: single LEFT JOIN computes both pending eligibility
|
|
124
|
+
// and 'new' vs 'changed' reason without double NOT EXISTS lookups.
|
|
125
|
+
const cursorClause = after
|
|
126
|
+
? "AND (c.mirror_hash > ? OR (c.mirror_hash = ? AND c.seq > ?))"
|
|
127
|
+
: "";
|
|
128
|
+
const sql = `
|
|
159
129
|
SELECT c.mirror_hash as mirrorHash, c.seq, c.text,
|
|
160
130
|
(SELECT d.title FROM documents d WHERE d.mirror_hash = c.mirror_hash AND d.active = 1 ORDER BY d.id LIMIT 1) as title,
|
|
161
131
|
CASE
|
|
162
|
-
WHEN
|
|
163
|
-
SELECT 1 FROM content_vectors v
|
|
164
|
-
WHERE v.mirror_hash = c.mirror_hash
|
|
165
|
-
AND v.seq = c.seq
|
|
166
|
-
AND v.model = ?
|
|
167
|
-
AND v.embed_fingerprint = ?
|
|
168
|
-
) THEN 'new'
|
|
132
|
+
WHEN v.mirror_hash IS NULL THEN 'new'
|
|
169
133
|
ELSE 'changed'
|
|
170
134
|
END as reason
|
|
171
135
|
FROM content_chunks c
|
|
136
|
+
LEFT JOIN content_vectors v
|
|
137
|
+
ON v.mirror_hash = c.mirror_hash
|
|
138
|
+
AND v.seq = c.seq
|
|
139
|
+
AND v.model = ?
|
|
140
|
+
AND v.embed_fingerprint = ?
|
|
172
141
|
WHERE ${activeDoc.sql}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
WHERE v.mirror_hash = c.mirror_hash
|
|
176
|
-
AND v.seq = c.seq
|
|
177
|
-
AND v.model = ?
|
|
178
|
-
AND v.embed_fingerprint = ?
|
|
179
|
-
AND v.embedded_at >= c.created_at
|
|
180
|
-
)
|
|
142
|
+
AND (v.mirror_hash IS NULL OR v.embedded_at IS NULL OR v.embedded_at < c.created_at)
|
|
143
|
+
${cursorClause}
|
|
181
144
|
ORDER BY c.mirror_hash, c.seq
|
|
182
145
|
LIMIT ?
|
|
183
146
|
`;
|
|
184
147
|
|
|
185
|
-
const params = after
|
|
148
|
+
const params: (string | number)[] = after
|
|
186
149
|
? [
|
|
187
150
|
model,
|
|
188
151
|
embedFingerprint,
|
|
189
152
|
...activeDoc.params,
|
|
190
|
-
model,
|
|
191
|
-
embedFingerprint,
|
|
192
153
|
after.mirrorHash,
|
|
193
154
|
after.mirrorHash,
|
|
194
155
|
after.seq,
|
|
195
156
|
limit,
|
|
196
157
|
]
|
|
197
|
-
: [
|
|
198
|
-
model,
|
|
199
|
-
embedFingerprint,
|
|
200
|
-
...activeDoc.params,
|
|
201
|
-
model,
|
|
202
|
-
embedFingerprint,
|
|
203
|
-
limit,
|
|
204
|
-
];
|
|
158
|
+
: [model, embedFingerprint, ...activeDoc.params, limit];
|
|
205
159
|
|
|
206
|
-
const results = db
|
|
160
|
+
const results = db
|
|
161
|
+
.query<BacklogItem, (string | number)[]>(sql)
|
|
162
|
+
.all(...params);
|
|
207
163
|
return Promise.resolve(ok(results));
|
|
208
164
|
} catch (e) {
|
|
209
165
|
return Promise.resolve(
|