@gmickel/gno 2.15.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.15.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 +1 -1
- 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 +2 -2
- 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/program.ts +20 -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/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 +142 -60
- package/src/embed/retry.ts +39 -33
- package/src/embed/variant-backlog.ts +546 -114
- 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 +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/037-performance-indexes.ts +56 -0
- package/src/store/migrations/index.ts +2 -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 +28 -45
- 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 +495 -139
- package/browser-extension/artifacts/gno-browser-clipper-v2.15.0.zip.sha256 +0 -1
|
@@ -3,6 +3,7 @@ import type { Database } from "bun:sqlite";
|
|
|
3
3
|
|
|
4
4
|
import { getEmbeddingFingerprint } from "../../embed/fingerprint";
|
|
5
5
|
import { formatDocForEmbedding } from "../../pipeline/contextual";
|
|
6
|
+
import { provenOwners, vectorProofKey } from "./proof-cache";
|
|
6
7
|
import {
|
|
7
8
|
currentOwnerCount,
|
|
8
9
|
identityPartitionId,
|
|
@@ -257,7 +258,6 @@ interface OwnerCoverage {
|
|
|
257
258
|
collection: string;
|
|
258
259
|
mirror_hash: string;
|
|
259
260
|
seq: number;
|
|
260
|
-
text: string;
|
|
261
261
|
title: string | null;
|
|
262
262
|
input_hash: string | null;
|
|
263
263
|
embedding_bytes: number | null;
|
|
@@ -269,6 +269,7 @@ export function getVariantStatus(
|
|
|
269
269
|
db: Database,
|
|
270
270
|
options?: { embedModel?: string; embedFingerprint?: string }
|
|
271
271
|
): { backlog: number; embeddedByCollection: Map<string, number> } | null {
|
|
272
|
+
const inOuterTx = db.inTransaction;
|
|
272
273
|
return db.transaction(() => {
|
|
273
274
|
if (!hasPartitionTable(db)) return null;
|
|
274
275
|
const partitions = readPartitions(db, options?.embedModel ?? null);
|
|
@@ -308,71 +309,169 @@ export function getVariantStatus(
|
|
|
308
309
|
usablePartitions.set(partition.partition_id, partition);
|
|
309
310
|
}
|
|
310
311
|
if (authoritativeModels.size === 0) return null;
|
|
312
|
+
const proofs = provenOwners(db, inOuterTx);
|
|
313
|
+
const usableJson = JSON.stringify([...usablePartitions.keys()]);
|
|
314
|
+
const scopedToModel = Boolean(options?.embedModel);
|
|
311
315
|
// Unscoped legacy coverage remains valid only for models that have never
|
|
312
|
-
// selected or activated verified authority;
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
316
|
+
// selected or activated verified authority; omit the legacy CTE entirely
|
|
317
|
+
// when embedModel is set, and defer c.text reads to uncached bound rows.
|
|
318
|
+
const sql = scopedToModel
|
|
319
|
+
? `
|
|
320
|
+
SELECT d.id AS document_id, o.partition_id, d.collection, d.mirror_hash, c.seq, d.title,
|
|
321
|
+
v.input_hash, length(v.embedding) AS embedding_bytes,
|
|
322
|
+
0 AS legacy_embedded
|
|
323
|
+
FROM documents d
|
|
324
|
+
JOIN content_chunks c ON c.mirror_hash = d.mirror_hash
|
|
325
|
+
LEFT JOIN vector_owners o ON o.document_id = d.id AND o.seq = c.seq
|
|
326
|
+
AND o.mirror_hash = d.mirror_hash
|
|
327
|
+
AND o.partition_id IN (SELECT value FROM json_each(?))
|
|
328
|
+
LEFT JOIN vector_variants v ON v.variant_id = o.variant_id
|
|
329
|
+
AND v.partition_id = o.partition_id
|
|
330
|
+
WHERE d.active = 1
|
|
331
|
+
`
|
|
332
|
+
: `
|
|
333
|
+
WITH legacy_vectors AS (
|
|
334
|
+
SELECT mirror_hash, seq, MAX(embedded_at) AS embedded_at
|
|
335
|
+
FROM content_vectors
|
|
336
|
+
WHERE model NOT IN (SELECT value FROM json_each(?))
|
|
337
|
+
GROUP BY mirror_hash, seq
|
|
338
|
+
)
|
|
339
|
+
SELECT d.id AS document_id, o.partition_id, d.collection, d.mirror_hash, c.seq, d.title,
|
|
340
|
+
v.input_hash, length(v.embedding) AS embedding_bytes,
|
|
341
|
+
CASE WHEN lv.embedded_at >= c.created_at THEN 1 ELSE 0 END AS legacy_embedded
|
|
342
|
+
FROM documents d
|
|
343
|
+
JOIN content_chunks c ON c.mirror_hash = d.mirror_hash
|
|
344
|
+
LEFT JOIN vector_owners o ON o.document_id = d.id AND o.seq = c.seq
|
|
345
|
+
AND o.mirror_hash = d.mirror_hash
|
|
346
|
+
AND o.partition_id IN (SELECT value FROM json_each(?))
|
|
347
|
+
LEFT JOIN vector_variants v ON v.variant_id = o.variant_id
|
|
348
|
+
AND v.partition_id = o.partition_id
|
|
349
|
+
LEFT JOIN legacy_vectors lv ON lv.mirror_hash = c.mirror_hash AND lv.seq = c.seq
|
|
350
|
+
WHERE d.active = 1
|
|
351
|
+
`;
|
|
352
|
+
const statement = db.prepare<OwnerCoverage, string[]>(sql);
|
|
353
|
+
const statementParams = scopedToModel
|
|
354
|
+
? [usableJson]
|
|
355
|
+
: [JSON.stringify([...authoritativeModels]), usableJson];
|
|
336
356
|
const owners = new Map<
|
|
337
357
|
string,
|
|
338
358
|
{ collection: string; chunk: string; embedded: boolean }
|
|
339
359
|
>();
|
|
360
|
+
const pendingProofs: Array<{
|
|
361
|
+
ownerKey: string;
|
|
362
|
+
proofKey: string;
|
|
363
|
+
mirrorHash: string;
|
|
364
|
+
seq: number;
|
|
365
|
+
title: string | null;
|
|
366
|
+
model: string;
|
|
367
|
+
inputHash: string;
|
|
368
|
+
}> = [];
|
|
369
|
+
const neededChunks = new Map<string, [string, number]>();
|
|
370
|
+
const PROOF_TEXT_BATCH_SIZE = 512;
|
|
371
|
+
const fetchChunkTexts = db.query<
|
|
372
|
+
{ mirror_hash: string; seq: number; text: string },
|
|
373
|
+
[string]
|
|
374
|
+
>(`
|
|
375
|
+
WITH req AS (
|
|
376
|
+
SELECT
|
|
377
|
+
json_extract(value, '$[0]') AS mirror_hash,
|
|
378
|
+
json_extract(value, '$[1]') AS seq
|
|
379
|
+
FROM json_each(?)
|
|
380
|
+
)
|
|
381
|
+
SELECT c.mirror_hash, c.seq, c.text
|
|
382
|
+
FROM req r
|
|
383
|
+
JOIN content_chunks c ON c.mirror_hash = r.mirror_hash AND c.seq = r.seq
|
|
384
|
+
`);
|
|
385
|
+
const flushPendingProofs = (): void => {
|
|
386
|
+
if (pendingProofs.length === 0) return;
|
|
387
|
+
const textByChunk = new Map<string, string>();
|
|
388
|
+
const chunkEntries = [...neededChunks.values()];
|
|
389
|
+
for (const row of fetchChunkTexts.all(JSON.stringify(chunkEntries))) {
|
|
390
|
+
textByChunk.set(`${row.mirror_hash}\0${row.seq}`, row.text);
|
|
391
|
+
}
|
|
392
|
+
for (const item of pendingProofs) {
|
|
393
|
+
const owner = owners.get(item.ownerKey);
|
|
394
|
+
if (!owner) continue;
|
|
395
|
+
let valid = proofs.get(item.proofKey);
|
|
396
|
+
if (valid === undefined) {
|
|
397
|
+
const text = textByChunk.get(`${item.mirrorHash}\0${item.seq}`);
|
|
398
|
+
valid =
|
|
399
|
+
text !== undefined &&
|
|
400
|
+
item.inputHash ===
|
|
401
|
+
embeddingInputHash(
|
|
402
|
+
formatDocForEmbedding(text, item.title ?? undefined, item.model)
|
|
403
|
+
);
|
|
404
|
+
proofs.set(item.proofKey, valid);
|
|
405
|
+
}
|
|
406
|
+
if (valid) owner.embedded = true;
|
|
407
|
+
}
|
|
408
|
+
pendingProofs.length = 0;
|
|
409
|
+
neededChunks.clear();
|
|
410
|
+
};
|
|
340
411
|
try {
|
|
341
|
-
for (const row of statement.iterate(
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
412
|
+
for (const row of statement.iterate(...statementParams)) {
|
|
413
|
+
const ownerKey = `${row.document_id}:${row.seq}`;
|
|
414
|
+
let owner = owners.get(ownerKey);
|
|
415
|
+
if (!owner) {
|
|
416
|
+
owner = {
|
|
417
|
+
collection: row.collection,
|
|
418
|
+
chunk: `${row.mirror_hash}\0${row.seq}`,
|
|
419
|
+
embedded: false,
|
|
420
|
+
};
|
|
421
|
+
owners.set(ownerKey, owner);
|
|
422
|
+
}
|
|
423
|
+
if (owner.embedded) continue;
|
|
424
|
+
if (row.legacy_embedded) {
|
|
425
|
+
owner.embedded = true;
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
346
428
|
const partition = row.partition_id
|
|
347
429
|
? usablePartitions.get(row.partition_id)
|
|
348
430
|
: undefined;
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
431
|
+
if (
|
|
432
|
+
!partition ||
|
|
433
|
+
row.input_hash === null ||
|
|
434
|
+
row.embedding_bytes !==
|
|
435
|
+
partition.dimensions * Float32Array.BYTES_PER_ELEMENT
|
|
436
|
+
) {
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
const pKey = vectorProofKey(
|
|
440
|
+
partition.model,
|
|
441
|
+
row.document_id,
|
|
442
|
+
row.mirror_hash,
|
|
443
|
+
row.seq,
|
|
444
|
+
row.input_hash
|
|
362
445
|
);
|
|
363
|
-
const
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
446
|
+
const cached = proofs.get(pKey);
|
|
447
|
+
if (cached !== undefined) {
|
|
448
|
+
if (cached) owner.embedded = true;
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
pendingProofs.push({
|
|
452
|
+
ownerKey,
|
|
453
|
+
proofKey: pKey,
|
|
454
|
+
mirrorHash: row.mirror_hash,
|
|
455
|
+
seq: row.seq,
|
|
456
|
+
title: row.title,
|
|
457
|
+
model: partition.model,
|
|
458
|
+
inputHash: row.input_hash,
|
|
459
|
+
});
|
|
460
|
+
const chunkKey = `${row.mirror_hash}\0${row.seq}`;
|
|
461
|
+
if (!neededChunks.has(chunkKey)) {
|
|
462
|
+
neededChunks.set(chunkKey, [row.mirror_hash, row.seq]);
|
|
463
|
+
}
|
|
464
|
+
if (
|
|
465
|
+
neededChunks.size >= PROOF_TEXT_BATCH_SIZE ||
|
|
466
|
+
pendingProofs.length >= PROOF_TEXT_BATCH_SIZE
|
|
467
|
+
) {
|
|
468
|
+
flushPendingProofs();
|
|
469
|
+
}
|
|
372
470
|
}
|
|
373
471
|
} finally {
|
|
374
472
|
statement.finalize();
|
|
375
473
|
}
|
|
474
|
+
flushPendingProofs();
|
|
376
475
|
const collections = new Map<string, Map<string, boolean>>();
|
|
377
476
|
let backlog = 0;
|
|
378
477
|
for (const owner of owners.values()) {
|
|
@@ -10,6 +10,7 @@ import type { Database } from "bun:sqlite";
|
|
|
10
10
|
import type { VectorSearchResult } from "./types";
|
|
11
11
|
|
|
12
12
|
import { formatDocForEmbedding } from "../../pipeline/contextual";
|
|
13
|
+
import { provenOwners, vectorProofKey } from "./proof-cache";
|
|
13
14
|
import { decodeEmbedding } from "./sqlite-vec";
|
|
14
15
|
import { storedVectorPartition } from "./status";
|
|
15
16
|
import { embeddingInputHash } from "./variants";
|
|
@@ -47,17 +48,27 @@ export interface StoredVectorDocument {
|
|
|
47
48
|
|
|
48
49
|
interface PartitionVectorRow {
|
|
49
50
|
documentId: number;
|
|
51
|
+
mirrorHash: string;
|
|
52
|
+
seq: number;
|
|
50
53
|
text: string;
|
|
51
54
|
title: string | null;
|
|
52
55
|
inputHash: string;
|
|
53
56
|
embedding: Uint8Array;
|
|
54
57
|
}
|
|
55
58
|
|
|
56
|
-
const
|
|
59
|
+
const unitVectorInPlace = (vector: Float32Array): Float32Array => {
|
|
57
60
|
let norm = 0;
|
|
58
|
-
for (
|
|
61
|
+
for (let i = 0; i < vector.length; i++) {
|
|
62
|
+
const value = vector[i]!;
|
|
63
|
+
norm += value * value;
|
|
64
|
+
}
|
|
59
65
|
norm = Math.sqrt(norm);
|
|
60
|
-
|
|
66
|
+
if (norm > 0) {
|
|
67
|
+
for (let i = 0; i < vector.length; i++) {
|
|
68
|
+
vector[i] = vector[i]! / norm;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return vector;
|
|
61
72
|
};
|
|
62
73
|
|
|
63
74
|
/**
|
|
@@ -76,40 +87,81 @@ export function readSimilaritySourceVectors(
|
|
|
76
87
|
const vectors = new Map<number, Float32Array>();
|
|
77
88
|
const add = (documentId: number, blob: Uint8Array): void => {
|
|
78
89
|
if (!vectors.has(documentId))
|
|
79
|
-
vectors.set(documentId,
|
|
90
|
+
vectors.set(documentId, unitVectorInPlace(decodeEmbedding(blob)));
|
|
80
91
|
};
|
|
81
92
|
if (documents.length === 0) return vectors;
|
|
82
93
|
|
|
83
94
|
if (source.kind === "partition") {
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
95
|
+
const inOuterTransaction = db.inTransaction;
|
|
96
|
+
return db
|
|
97
|
+
.transaction(() => {
|
|
98
|
+
const proofs = provenOwners(db, inOuterTransaction);
|
|
99
|
+
const processRows = (rows: PartitionVectorRow[]): void => {
|
|
100
|
+
for (const row of rows) {
|
|
101
|
+
if (vectors.has(row.documentId)) continue;
|
|
102
|
+
const pKey = vectorProofKey(
|
|
103
|
+
source.model,
|
|
104
|
+
row.documentId,
|
|
105
|
+
row.mirrorHash,
|
|
106
|
+
row.seq,
|
|
107
|
+
row.inputHash
|
|
108
|
+
);
|
|
109
|
+
let valid = proofs.get(pKey);
|
|
110
|
+
if (valid === undefined) {
|
|
111
|
+
const input = formatDocForEmbedding(
|
|
112
|
+
row.text,
|
|
113
|
+
row.title ?? undefined,
|
|
114
|
+
source.model
|
|
115
|
+
);
|
|
116
|
+
valid = row.inputHash === embeddingInputHash(input);
|
|
117
|
+
proofs.set(pKey, valid);
|
|
118
|
+
}
|
|
119
|
+
if (valid) add(row.documentId, row.embedding);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const allIds = documents.map((document) => document.id);
|
|
124
|
+
const seqZeroRows = db
|
|
125
|
+
.query<PartitionVectorRow, [string, string]>(`
|
|
126
|
+
SELECT o.document_id AS documentId, o.mirror_hash AS mirrorHash, o.seq,
|
|
127
|
+
c.text, d.title, v.input_hash AS inputHash, v.embedding
|
|
128
|
+
FROM vector_owners o
|
|
129
|
+
JOIN documents d ON d.id = o.document_id AND d.active = 1
|
|
130
|
+
AND d.mirror_hash = o.mirror_hash
|
|
131
|
+
JOIN content_chunks c ON c.mirror_hash = o.mirror_hash AND c.seq = o.seq
|
|
132
|
+
JOIN vector_variants v ON v.variant_id = o.variant_id
|
|
133
|
+
AND v.partition_id = o.partition_id
|
|
134
|
+
WHERE o.partition_id = ?
|
|
135
|
+
AND o.seq = 0
|
|
136
|
+
AND o.document_id IN (SELECT value FROM json_each(?))
|
|
137
|
+
ORDER BY o.document_id
|
|
138
|
+
`)
|
|
139
|
+
.all(source.partitionId, JSON.stringify(allIds));
|
|
140
|
+
processRows(seqZeroRows);
|
|
141
|
+
|
|
142
|
+
const missingIds = allIds.filter((id) => !vectors.has(id));
|
|
143
|
+
if (missingIds.length > 0) {
|
|
144
|
+
const fallbackRows = db
|
|
145
|
+
.query<PartitionVectorRow, [string, string]>(`
|
|
146
|
+
SELECT o.document_id AS documentId, o.mirror_hash AS mirrorHash, o.seq,
|
|
147
|
+
c.text, d.title, v.input_hash AS inputHash, v.embedding
|
|
148
|
+
FROM vector_owners o
|
|
149
|
+
JOIN documents d ON d.id = o.document_id AND d.active = 1
|
|
150
|
+
AND d.mirror_hash = o.mirror_hash
|
|
151
|
+
JOIN content_chunks c ON c.mirror_hash = o.mirror_hash AND c.seq = o.seq
|
|
152
|
+
JOIN vector_variants v ON v.variant_id = o.variant_id
|
|
153
|
+
AND v.partition_id = o.partition_id
|
|
154
|
+
WHERE o.partition_id = ?
|
|
155
|
+
AND o.seq > 0
|
|
156
|
+
AND o.document_id IN (SELECT value FROM json_each(?))
|
|
157
|
+
ORDER BY o.document_id, o.seq
|
|
158
|
+
`)
|
|
159
|
+
.all(source.partitionId, JSON.stringify(missingIds));
|
|
160
|
+
processRows(fallbackRows);
|
|
161
|
+
}
|
|
162
|
+
return vectors;
|
|
163
|
+
})
|
|
164
|
+
.deferred();
|
|
113
165
|
}
|
|
114
166
|
|
|
115
167
|
const idsByMirror = new Map<string, number[]>();
|
|
@@ -118,18 +170,52 @@ export function readSimilaritySourceVectors(
|
|
|
118
170
|
if (ids) ids.push(document.id);
|
|
119
171
|
else idsByMirror.set(document.mirrorHash, [document.id]);
|
|
120
172
|
}
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
173
|
+
const allMirrors = [...idsByMirror.keys()];
|
|
174
|
+
return db
|
|
175
|
+
.transaction(() => {
|
|
176
|
+
const seqZeroRows = db
|
|
177
|
+
.query<
|
|
178
|
+
{ mirrorHash: string; embedding: Uint8Array },
|
|
179
|
+
[string, string]
|
|
180
|
+
>(`
|
|
181
|
+
SELECT mirror_hash AS mirrorHash, embedding
|
|
182
|
+
FROM content_vectors
|
|
183
|
+
WHERE model = ? AND seq = 0
|
|
184
|
+
AND mirror_hash IN (SELECT value FROM json_each(?))
|
|
185
|
+
`)
|
|
186
|
+
.all(source.model, JSON.stringify(allMirrors));
|
|
187
|
+
const foundMirrors = new Set<string>();
|
|
188
|
+
for (const row of seqZeroRows) {
|
|
189
|
+
foundMirrors.add(row.mirrorHash);
|
|
190
|
+
for (const id of idsByMirror.get(row.mirrorHash) ?? [])
|
|
191
|
+
add(id, row.embedding);
|
|
192
|
+
}
|
|
193
|
+
const missingMirrors = allMirrors.filter(
|
|
194
|
+
(mirror) => !foundMirrors.has(mirror)
|
|
195
|
+
);
|
|
196
|
+
if (missingMirrors.length > 0) {
|
|
197
|
+
const fallbackRows = db
|
|
198
|
+
.query<
|
|
199
|
+
{ mirrorHash: string; embedding: Uint8Array },
|
|
200
|
+
[string, string]
|
|
201
|
+
>(`
|
|
202
|
+
SELECT mirrorHash, embedding FROM (
|
|
203
|
+
SELECT mirror_hash AS mirrorHash, embedding,
|
|
204
|
+
ROW_NUMBER() OVER (PARTITION BY mirror_hash ORDER BY seq) AS rn
|
|
205
|
+
FROM content_vectors
|
|
206
|
+
WHERE model = ? AND seq > 0
|
|
207
|
+
AND mirror_hash IN (SELECT value FROM json_each(?))
|
|
208
|
+
) WHERE rn = 1
|
|
209
|
+
`)
|
|
210
|
+
.all(source.model, JSON.stringify(missingMirrors));
|
|
211
|
+
for (const row of fallbackRows) {
|
|
212
|
+
for (const id of idsByMirror.get(row.mirrorHash) ?? [])
|
|
213
|
+
add(id, row.embedding);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return vectors;
|
|
217
|
+
})
|
|
218
|
+
.deferred();
|
|
133
219
|
}
|
|
134
220
|
|
|
135
221
|
/**
|
|
@@ -128,6 +128,16 @@ export interface VectorIndexPort {
|
|
|
128
128
|
options?: VectorSearchOptions
|
|
129
129
|
): Promise<StoreResult<VectorSearchResult[]>>;
|
|
130
130
|
|
|
131
|
+
/**
|
|
132
|
+
* Optional batched nearest-neighbor search sharing one eligible scan /
|
|
133
|
+
* snapshot across multiple query embeddings (e.g. expanded hybrid queries).
|
|
134
|
+
*/
|
|
135
|
+
searchNearestBatch?(
|
|
136
|
+
embeddings: Float32Array[],
|
|
137
|
+
limits: number[],
|
|
138
|
+
options?: VectorSearchOptions
|
|
139
|
+
): Promise<StoreResult<VectorSearchResult[][]>>;
|
|
140
|
+
|
|
131
141
|
// ─────────────────────────────────────────────────────────────────────────
|
|
132
142
|
// Index maintenance
|
|
133
143
|
// ─────────────────────────────────────────────────────────────────────────
|