@gmickel/gno 2.16.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 +20 -13
- package/browser-extension/artifacts/{gno-browser-clipper-v2.16.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 +42 -2
- 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/doctor.ts +101 -29
- package/src/cli/commands/links.ts +29 -5
- package/src/cli/commands/vec.ts +180 -8
- package/src/cli/program.ts +43 -0
- package/src/config/types.ts +6 -0
- package/src/core/retrieval-trace-session.ts +3 -0
- package/src/core/retrieval-trace.ts +9 -0
- package/src/core/vector-partition-status.ts +13 -0
- package/src/embed/variant-backlog.ts +36 -2
- package/src/mcp/tools/links.ts +7 -1
- package/src/pipeline/explain.ts +17 -2
- package/src/pipeline/hybrid.ts +28 -1
- package/src/pipeline/types.ts +7 -0
- package/src/pipeline/vsearch.ts +19 -2
- package/src/serve/public/globals.built.css +1 -1
- package/src/serve/routes/links.ts +7 -1
- 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 +4 -0
- package/src/store/sqlite/adapter.ts +117 -74
- package/src/store/vector/coarse-router.ts +312 -0
- package/src/store/vector/partition-index.ts +309 -18
- package/src/store/vector/proof-cache.ts +132 -26
- package/src/store/vector/runtime-compat.ts +6 -0
- package/src/store/vector/sqlite-vec.ts +43 -26
- package/src/store/vector/stats.ts +8 -0
- package/src/store/vector/status.ts +18 -1
- package/src/store/vector/types.ts +9 -0
- package/src/store/vector/v2-mirror.ts +669 -0
- package/src/store/vector/variant-search.ts +434 -20
- package/src/store/vector/variants.ts +480 -32
- package/browser-extension/artifacts/gno-browser-clipper-v2.16.0.zip.sha256 +0 -1
|
@@ -11,7 +11,20 @@ import type {
|
|
|
11
11
|
import { formatDocForEmbedding } from "../../pipeline/contextual";
|
|
12
12
|
import { buildEligibleDocumentQuery } from "../sqlite/eligibility";
|
|
13
13
|
import {
|
|
14
|
-
|
|
14
|
+
COARSE_ROUTER_DEFAULTS,
|
|
15
|
+
computeCoarseK,
|
|
16
|
+
countScopeMirrorRows,
|
|
17
|
+
extractRequestedCollections,
|
|
18
|
+
hasNonCollectionFilter,
|
|
19
|
+
isV2CompatibleDimensions,
|
|
20
|
+
resolveVectorRoute,
|
|
21
|
+
v2TableName,
|
|
22
|
+
} from "./coarse-router";
|
|
23
|
+
import { hasNullCosineColumn } from "./partition-index";
|
|
24
|
+
import {
|
|
25
|
+
checkNullCosineVariant,
|
|
26
|
+
type NullCosineCheckResult,
|
|
27
|
+
persistNullCosineAfterSnapshot,
|
|
15
28
|
provenOwners,
|
|
16
29
|
vectorProofKey,
|
|
17
30
|
} from "./proof-cache";
|
|
@@ -28,6 +41,7 @@ import {
|
|
|
28
41
|
writerConnection,
|
|
29
42
|
writerIdle,
|
|
30
43
|
} from "./sqlite-vec";
|
|
44
|
+
import { hasV2MirrorTables, hasV2Table, readV2MirrorState } from "./v2-mirror";
|
|
31
45
|
import {
|
|
32
46
|
chunkKeyHint,
|
|
33
47
|
embeddingInputHash,
|
|
@@ -594,7 +608,10 @@ function buildEligibleVariantQuerySpec(
|
|
|
594
608
|
: `FROM vector_owners o${ownerPartitionVariantHint(db)}
|
|
595
609
|
CROSS JOIN (${eligible.sql}) e
|
|
596
610
|
ON e.id = o.document_id AND e.mirror_hash = o.mirror_hash`;
|
|
597
|
-
|
|
611
|
+
const chunkJoin = options.eligibility?.language
|
|
612
|
+
? `JOIN content_chunks c${chunkKeyHint(db)} ON c.mirror_hash = o.mirror_hash AND c.seq = o.seq`
|
|
613
|
+
: "";
|
|
614
|
+
return { eligible, conditions, params, innerFrom, chunkJoin };
|
|
598
615
|
}
|
|
599
616
|
|
|
600
617
|
function searchVectorVariantsFromVariants(
|
|
@@ -615,7 +632,7 @@ function searchVectorVariantsFromVariants(
|
|
|
615
632
|
SELECT o.variant_id AS variantId, o.mirror_hash AS mirrorHash, o.seq,
|
|
616
633
|
json_group_array(o.document_id) AS documentIds
|
|
617
634
|
${spec.innerFrom}
|
|
618
|
-
|
|
635
|
+
${spec.chunkJoin}
|
|
619
636
|
WHERE ${spec.conditions.join(" AND ")}
|
|
620
637
|
GROUP BY o.variant_id, o.mirror_hash, o.seq
|
|
621
638
|
) g
|
|
@@ -675,6 +692,176 @@ function searchVectorVariantsFromVariants(
|
|
|
675
692
|
return results;
|
|
676
693
|
}
|
|
677
694
|
|
|
695
|
+
function readPersistedNullCosineFlag(
|
|
696
|
+
db: Database,
|
|
697
|
+
partitionId: string
|
|
698
|
+
): 0 | 1 | null {
|
|
699
|
+
if (!hasNullCosineColumn(db)) return null;
|
|
700
|
+
const row = db
|
|
701
|
+
.query<{ dirty: number; null_cosine: number | null }, [string]>(
|
|
702
|
+
"SELECT dirty, null_cosine FROM vector_mirror_state WHERE partition_id = ?"
|
|
703
|
+
)
|
|
704
|
+
.get(partitionId);
|
|
705
|
+
if (row?.dirty !== 0) return null;
|
|
706
|
+
return row.null_cosine === 0 ? 0 : row.null_cosine === 1 ? 1 : null;
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
/**
|
|
710
|
+
* Two-stage coarse bit search inside the active read snapshot (fn-235 §4):
|
|
711
|
+
* Stage 1: Hamming KNN on `vec_v2_<partitionId>` (per collection or unscoped).
|
|
712
|
+
* Stage 2: map rowids -> distinct `variant_id`s through `vector_partition_rows`,
|
|
713
|
+
* rescore exact float32 cosine against `vector_variants`, hydrate only
|
|
714
|
+
* eligible owners in scope (access-control boundary), run `proveUnknownOwners`,
|
|
715
|
+
* apply exact `minScore`, sort by `(distance, mirrorHash, seq, variantId)`.
|
|
716
|
+
*
|
|
717
|
+
* Returns `null` on underfill (fewer than `k` proven hits while Stage 1 filled
|
|
718
|
+
* `coarseK` and no candidate fell below `minScore`) so the caller reruns exact.
|
|
719
|
+
*/
|
|
720
|
+
function searchVectorVariantsViaCoarseBit(
|
|
721
|
+
db: Database,
|
|
722
|
+
model: string,
|
|
723
|
+
partitionId: string,
|
|
724
|
+
embedding: Float32Array,
|
|
725
|
+
k: number,
|
|
726
|
+
coarseK: number,
|
|
727
|
+
collections: readonly string[],
|
|
728
|
+
minScore: number | undefined,
|
|
729
|
+
options: VectorSearchOptions,
|
|
730
|
+
proofs: Map<string, boolean>,
|
|
731
|
+
batchOwners: ReturnType<typeof createBatchOwnersStmt>
|
|
732
|
+
): { results: VectorSearchResult[]; rescored: number } | null {
|
|
733
|
+
const queryVector = encodeEmbedding(embedding);
|
|
734
|
+
const v2Table = v2TableName(partitionId);
|
|
735
|
+
const rowidSet = new Set<number>();
|
|
736
|
+
let stage1ReturnedFull = false;
|
|
737
|
+
|
|
738
|
+
if (collections.length === 0) {
|
|
739
|
+
const rows = db
|
|
740
|
+
.query<{ rowid: number }, [Uint8Array, number]>(
|
|
741
|
+
`SELECT rowid FROM "${v2Table}"
|
|
742
|
+
WHERE coarse MATCH vec_quantize_binary(?) AND k = ?`
|
|
743
|
+
)
|
|
744
|
+
.all(queryVector, coarseK);
|
|
745
|
+
if (rows.length >= coarseK) stage1ReturnedFull = true;
|
|
746
|
+
for (const r of rows) rowidSet.add(r.rowid);
|
|
747
|
+
} else {
|
|
748
|
+
const legStmt = db.prepare<{ rowid: number }, [Uint8Array, number, string]>(
|
|
749
|
+
`SELECT rowid FROM "${v2Table}"
|
|
750
|
+
WHERE coarse MATCH vec_quantize_binary(?) AND k = ? AND collection = ?`
|
|
751
|
+
);
|
|
752
|
+
try {
|
|
753
|
+
for (const collection of collections) {
|
|
754
|
+
const rows = legStmt.all(queryVector, coarseK, collection);
|
|
755
|
+
if (rows.length >= coarseK) stage1ReturnedFull = true;
|
|
756
|
+
for (const r of rows) rowidSet.add(r.rowid);
|
|
757
|
+
}
|
|
758
|
+
} finally {
|
|
759
|
+
legStmt.finalize();
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
if (rowidSet.size === 0) {
|
|
764
|
+
return { results: [], rescored: 0 };
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
// Map rowids -> distinct variant_ids through plain table `vector_partition_rows`
|
|
768
|
+
// driven by json_each CROSS JOIN PK seeks (never passing rowids to vec0 `rowid IN`).
|
|
769
|
+
const variantIds = db
|
|
770
|
+
.query<{ variant_id: number }, [string, string]>(
|
|
771
|
+
`SELECT DISTINCT r.variant_id
|
|
772
|
+
FROM json_each(?) AS m
|
|
773
|
+
CROSS JOIN vector_partition_rows r ON r.id = m.value
|
|
774
|
+
WHERE r.partition_id = ?`
|
|
775
|
+
)
|
|
776
|
+
.all(JSON.stringify([...rowidSet]), partitionId)
|
|
777
|
+
.map((r) => r.variant_id);
|
|
778
|
+
|
|
779
|
+
if (variantIds.length === 0) {
|
|
780
|
+
return stage1ReturnedFull ? null : { results: [], rescored: 0 };
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
// Stage 2: exact float32 rescore + eligible-documents owner hydration enforcing
|
|
784
|
+
// the exact collection access-control boundary. CROSS JOIN pins json_each(cand)
|
|
785
|
+
// as the outer loop so SQLite seeks only the <= coarseK candidate variants
|
|
786
|
+
// instead of scanning all vector_owners in the partition.
|
|
787
|
+
const eligible = buildEligibleDocumentQuery(
|
|
788
|
+
{ ...options.eligibility, semanticMetadata: true },
|
|
789
|
+
db
|
|
790
|
+
);
|
|
791
|
+
const hydrated = db
|
|
792
|
+
.query<RankedVariantRow, (Uint8Array | string | number)[]>(
|
|
793
|
+
`SELECT o.variant_id AS variantId, o.mirror_hash AS mirrorHash, o.seq,
|
|
794
|
+
v.input_hash AS inputHash,
|
|
795
|
+
vec_distance_cosine(v.embedding, ?) AS distance,
|
|
796
|
+
json_group_array(o.document_id) AS documentIds
|
|
797
|
+
FROM json_each(?) AS cand
|
|
798
|
+
CROSS JOIN vector_variants v${variantPartitionIdHint(db)}
|
|
799
|
+
ON v.partition_id = ? AND v.variant_id = cand.value
|
|
800
|
+
CROSS JOIN vector_owners o${ownerPartitionVariantHint(db)}
|
|
801
|
+
ON o.partition_id = v.partition_id AND o.variant_id = v.variant_id
|
|
802
|
+
CROSS JOIN (${eligible.sql}) e
|
|
803
|
+
ON e.id = o.document_id AND e.mirror_hash = o.mirror_hash
|
|
804
|
+
GROUP BY o.variant_id, o.mirror_hash, o.seq
|
|
805
|
+
ORDER BY distance, o.mirror_hash, o.seq, o.variant_id`
|
|
806
|
+
)
|
|
807
|
+
.all(
|
|
808
|
+
queryVector,
|
|
809
|
+
JSON.stringify(variantIds),
|
|
810
|
+
partitionId,
|
|
811
|
+
...eligible.params
|
|
812
|
+
);
|
|
813
|
+
|
|
814
|
+
let hitMinScoreCutoff = false;
|
|
815
|
+
const candidateRows: CandidateRow[] = [];
|
|
816
|
+
for (const row of hydrated) {
|
|
817
|
+
if (
|
|
818
|
+
minScore !== undefined &&
|
|
819
|
+
row.distance !== null &&
|
|
820
|
+
1 - row.distance < minScore
|
|
821
|
+
) {
|
|
822
|
+
hitMinScoreCutoff = true;
|
|
823
|
+
break;
|
|
824
|
+
}
|
|
825
|
+
candidateRows.push({
|
|
826
|
+
variantId: row.variantId ?? 0,
|
|
827
|
+
mirrorHash: row.mirrorHash,
|
|
828
|
+
seq: row.seq,
|
|
829
|
+
inputHash: row.inputHash,
|
|
830
|
+
distance: row.distance,
|
|
831
|
+
documentIds: JSON.parse(row.documentIds) as number[],
|
|
832
|
+
});
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
proveUnknownOwners(model, proofs, [candidateRows], batchOwners);
|
|
836
|
+
|
|
837
|
+
const results: VectorSearchResult[] = [];
|
|
838
|
+
for (const row of candidateRows) {
|
|
839
|
+
const proven = row.documentIds
|
|
840
|
+
.filter((id) =>
|
|
841
|
+
proofs.get(
|
|
842
|
+
vectorProofKey(model, id, row.mirrorHash, row.seq, row.inputHash)
|
|
843
|
+
)
|
|
844
|
+
)
|
|
845
|
+
.sort((a, b) => a - b);
|
|
846
|
+
if (proven.length === 0) continue;
|
|
847
|
+
results.push({
|
|
848
|
+
mirrorHash: row.mirrorHash,
|
|
849
|
+
seq: row.seq,
|
|
850
|
+
distance: row.distance,
|
|
851
|
+
documentIds: proven,
|
|
852
|
+
});
|
|
853
|
+
if (results.length === k) break;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
// Underfill: if fewer than k proven results remain, Stage 1 returned a full
|
|
857
|
+
// coarseK, and no candidate fell below minScore, rerun exact (never raise coarseK > 500).
|
|
858
|
+
if (results.length < k && stage1ReturnedFull && !hitMinScoreCutoff) {
|
|
859
|
+
return null;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
return { results, rescored: variantIds.length };
|
|
863
|
+
}
|
|
864
|
+
|
|
678
865
|
/** Null means legacy authority. Once promoted, unavailable provenance fails closed. */
|
|
679
866
|
function searchVectorVariantsInSnapshot(
|
|
680
867
|
db: Database,
|
|
@@ -683,7 +870,8 @@ function searchVectorVariantsInSnapshot(
|
|
|
683
870
|
embedding: Float32Array,
|
|
684
871
|
k: number,
|
|
685
872
|
options: VectorSearchOptions,
|
|
686
|
-
inOuterTransaction: boolean
|
|
873
|
+
inOuterTransaction: boolean,
|
|
874
|
+
pendingPersist: { value?: NullCosineCheckResult["persistAfterSnapshot"] }
|
|
687
875
|
): VectorSearchResult[] | null {
|
|
688
876
|
const resolved = resolveActivePartition(
|
|
689
877
|
db,
|
|
@@ -702,13 +890,92 @@ function searchVectorVariantsInSnapshot(
|
|
|
702
890
|
const batchOwners = createBatchOwnersStmt(db);
|
|
703
891
|
const proofs = provenOwners(db, inOuterTransaction);
|
|
704
892
|
|
|
705
|
-
|
|
893
|
+
const collections = extractRequestedCollections(options);
|
|
894
|
+
const hasV2Tables = hasV2MirrorTables(db);
|
|
895
|
+
const mirrorRows = countScopeMirrorRows(
|
|
896
|
+
db,
|
|
897
|
+
partitionId,
|
|
898
|
+
collections,
|
|
899
|
+
hasV2Tables,
|
|
900
|
+
inOuterTransaction
|
|
901
|
+
);
|
|
902
|
+
|
|
903
|
+
// Check v1 clean unfiltered mirror eligibility (for exact-vec0 fast path).
|
|
904
|
+
const v1UnfilteredClean =
|
|
706
905
|
hasMirror &&
|
|
707
906
|
!hasSelectiveEligibility(options) &&
|
|
708
907
|
!options.eligibility?.language &&
|
|
709
|
-
isMirrorClean(db, partitionId)
|
|
710
|
-
|
|
908
|
+
isMirrorClean(db, partitionId);
|
|
909
|
+
|
|
910
|
+
let nullCosineFlag: 0 | 1 | null = null;
|
|
911
|
+
let canUseV1ExactVec0 = false;
|
|
912
|
+
if (v1UnfilteredClean) {
|
|
913
|
+
const nullCheck = checkNullCosineVariant(
|
|
914
|
+
db,
|
|
915
|
+
partitionId,
|
|
916
|
+
inOuterTransaction
|
|
917
|
+
);
|
|
918
|
+
if (nullCheck.persistAfterSnapshot) {
|
|
919
|
+
pendingPersist.value = nullCheck.persistAfterSnapshot;
|
|
920
|
+
}
|
|
921
|
+
nullCosineFlag = nullCheck.found ? 1 : 0;
|
|
922
|
+
canUseV1ExactVec0 = !nullCheck.found;
|
|
923
|
+
} else if (
|
|
924
|
+
!hasNonCollectionFilter(options) &&
|
|
925
|
+
(options.coarseVectorSearch ?? COARSE_ROUTER_DEFAULTS.enabledByDefault) &&
|
|
926
|
+
options.exact !== true
|
|
711
927
|
) {
|
|
928
|
+
// Scoped coarse check: O(1) persisted PK lookup only, never triggering a scan.
|
|
929
|
+
nullCosineFlag = readPersistedNullCosineFlag(db, partitionId);
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
const v2State =
|
|
933
|
+
hasV2Tables &&
|
|
934
|
+
isV2CompatibleDimensions(dimensions) &&
|
|
935
|
+
hasV2Table(db, partitionId)
|
|
936
|
+
? readV2MirrorState(db, partitionId)
|
|
937
|
+
: null;
|
|
938
|
+
|
|
939
|
+
const decision = resolveVectorRoute({
|
|
940
|
+
options,
|
|
941
|
+
dimensions,
|
|
942
|
+
queryCount: 1,
|
|
943
|
+
maxK: k,
|
|
944
|
+
collections,
|
|
945
|
+
mirrorRows,
|
|
946
|
+
v2MirrorClean: v2State?.dirty === 0,
|
|
947
|
+
v2CheckRecall: v2State?.checkRecall ?? null,
|
|
948
|
+
nullCosineFlag,
|
|
949
|
+
canUseV1ExactVec0,
|
|
950
|
+
});
|
|
951
|
+
|
|
952
|
+
if (decision.route === "coarse-bit" && decision.coarseK !== undefined) {
|
|
953
|
+
const coarse = searchVectorVariantsViaCoarseBit(
|
|
954
|
+
db,
|
|
955
|
+
model,
|
|
956
|
+
partitionId,
|
|
957
|
+
embedding,
|
|
958
|
+
k,
|
|
959
|
+
decision.coarseK,
|
|
960
|
+
collections,
|
|
961
|
+
minScore,
|
|
962
|
+
options,
|
|
963
|
+
proofs,
|
|
964
|
+
batchOwners
|
|
965
|
+
);
|
|
966
|
+
if (coarse !== null) {
|
|
967
|
+
options.onRoute?.({
|
|
968
|
+
route: "coarse-bit",
|
|
969
|
+
mirrorRows,
|
|
970
|
+
coarseK: decision.coarseK,
|
|
971
|
+
rescored: coarse.rescored,
|
|
972
|
+
});
|
|
973
|
+
return coarse.results;
|
|
974
|
+
}
|
|
975
|
+
// Underfill fallback to exact path.
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
if (canUseV1ExactVec0) {
|
|
712
979
|
const fast = searchVectorVariantsViaVec0(
|
|
713
980
|
db,
|
|
714
981
|
model,
|
|
@@ -720,9 +987,13 @@ function searchVectorVariantsInSnapshot(
|
|
|
720
987
|
proofs,
|
|
721
988
|
batchOwners
|
|
722
989
|
);
|
|
723
|
-
if (fast !== null)
|
|
990
|
+
if (fast !== null) {
|
|
991
|
+
options.onRoute?.({ route: "exact-vec0", mirrorRows });
|
|
992
|
+
return fast;
|
|
993
|
+
}
|
|
724
994
|
}
|
|
725
995
|
|
|
996
|
+
options.onRoute?.({ route: "exact-sql", mirrorRows });
|
|
726
997
|
return searchVectorVariantsFromVariants(
|
|
727
998
|
db,
|
|
728
999
|
model,
|
|
@@ -747,7 +1018,8 @@ function searchVectorVariantsBatchInSnapshot(
|
|
|
747
1018
|
embeddings: readonly Float32Array[],
|
|
748
1019
|
limits: readonly number[],
|
|
749
1020
|
options: VectorSearchOptions,
|
|
750
|
-
inOuterTransaction: boolean
|
|
1021
|
+
inOuterTransaction: boolean,
|
|
1022
|
+
pendingPersist: { value?: NullCosineCheckResult["persistAfterSnapshot"] }
|
|
751
1023
|
): VectorSearchResult[][] | null {
|
|
752
1024
|
const resolved = resolveActivePartition(
|
|
753
1025
|
db,
|
|
@@ -766,15 +1038,133 @@ function searchVectorVariantsBatchInSnapshot(
|
|
|
766
1038
|
const batchOwners = createBatchOwnersStmt(db);
|
|
767
1039
|
const proofs = provenOwners(db, inOuterTransaction);
|
|
768
1040
|
|
|
769
|
-
|
|
770
|
-
const
|
|
1041
|
+
const collections = extractRequestedCollections(options);
|
|
1042
|
+
const hasV2Tables = hasV2MirrorTables(db);
|
|
1043
|
+
const mirrorRows = countScopeMirrorRows(
|
|
1044
|
+
db,
|
|
1045
|
+
partitionId,
|
|
1046
|
+
collections,
|
|
1047
|
+
hasV2Tables,
|
|
1048
|
+
inOuterTransaction
|
|
1049
|
+
);
|
|
1050
|
+
|
|
1051
|
+
const v1UnfilteredClean =
|
|
771
1052
|
hasMirror &&
|
|
772
1053
|
!hasSelectiveEligibility(options) &&
|
|
773
1054
|
!options.eligibility?.language &&
|
|
774
|
-
isMirrorClean(db, partitionId)
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
1055
|
+
isMirrorClean(db, partitionId);
|
|
1056
|
+
|
|
1057
|
+
let nullCosineFlag: 0 | 1 | null = null;
|
|
1058
|
+
let canUseV1ExactVec0 = false;
|
|
1059
|
+
if (v1UnfilteredClean) {
|
|
1060
|
+
const nullCheck = checkNullCosineVariant(
|
|
1061
|
+
db,
|
|
1062
|
+
partitionId,
|
|
1063
|
+
inOuterTransaction
|
|
1064
|
+
);
|
|
1065
|
+
if (nullCheck.persistAfterSnapshot) {
|
|
1066
|
+
pendingPersist.value = nullCheck.persistAfterSnapshot;
|
|
1067
|
+
}
|
|
1068
|
+
nullCosineFlag = nullCheck.found ? 1 : 0;
|
|
1069
|
+
canUseV1ExactVec0 = !nullCheck.found;
|
|
1070
|
+
} else if (
|
|
1071
|
+
!hasNonCollectionFilter(options) &&
|
|
1072
|
+
(options.coarseVectorSearch ?? COARSE_ROUTER_DEFAULTS.enabledByDefault) &&
|
|
1073
|
+
options.exact !== true
|
|
1074
|
+
) {
|
|
1075
|
+
nullCosineFlag = readPersistedNullCosineFlag(db, partitionId);
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
const v2State =
|
|
1079
|
+
hasV2Tables &&
|
|
1080
|
+
isV2CompatibleDimensions(dimensions) &&
|
|
1081
|
+
hasV2Table(db, partitionId)
|
|
1082
|
+
? readV2MirrorState(db, partitionId)
|
|
1083
|
+
: null;
|
|
1084
|
+
|
|
1085
|
+
const maxK = limits.reduce((acc, val) => (val > acc ? val : acc), 0);
|
|
1086
|
+
const decision = resolveVectorRoute({
|
|
1087
|
+
options,
|
|
1088
|
+
dimensions,
|
|
1089
|
+
queryCount: embeddings.length,
|
|
1090
|
+
maxK,
|
|
1091
|
+
collections,
|
|
1092
|
+
mirrorRows,
|
|
1093
|
+
v2MirrorClean: v2State?.dirty === 0,
|
|
1094
|
+
v2CheckRecall: v2State?.checkRecall ?? null,
|
|
1095
|
+
nullCosineFlag,
|
|
1096
|
+
canUseV1ExactVec0,
|
|
1097
|
+
});
|
|
1098
|
+
|
|
1099
|
+
if (decision.route === "coarse-bit" && decision.coarseK !== undefined) {
|
|
1100
|
+
let totalRescored = 0;
|
|
1101
|
+
let allCoarseSucceeded = true;
|
|
1102
|
+
const out = embeddings.map((embedding, i) => {
|
|
1103
|
+
const k = limits[i]!;
|
|
1104
|
+
const queryCoarseK = computeCoarseK(k);
|
|
1105
|
+
const coarse = searchVectorVariantsViaCoarseBit(
|
|
1106
|
+
db,
|
|
1107
|
+
model,
|
|
1108
|
+
partitionId,
|
|
1109
|
+
embedding,
|
|
1110
|
+
k,
|
|
1111
|
+
queryCoarseK,
|
|
1112
|
+
collections,
|
|
1113
|
+
minScore,
|
|
1114
|
+
options,
|
|
1115
|
+
proofs,
|
|
1116
|
+
batchOwners
|
|
1117
|
+
);
|
|
1118
|
+
if (coarse !== null) {
|
|
1119
|
+
totalRescored += coarse.rescored;
|
|
1120
|
+
return coarse.results;
|
|
1121
|
+
}
|
|
1122
|
+
allCoarseSucceeded = false;
|
|
1123
|
+
if (canUseV1ExactVec0) {
|
|
1124
|
+
const fast = searchVectorVariantsViaVec0(
|
|
1125
|
+
db,
|
|
1126
|
+
model,
|
|
1127
|
+
partitionId,
|
|
1128
|
+
tableName,
|
|
1129
|
+
embedding,
|
|
1130
|
+
k,
|
|
1131
|
+
minScore,
|
|
1132
|
+
proofs,
|
|
1133
|
+
batchOwners
|
|
1134
|
+
);
|
|
1135
|
+
if (fast !== null) return fast;
|
|
1136
|
+
}
|
|
1137
|
+
return searchVectorVariantsFromVariants(
|
|
1138
|
+
db,
|
|
1139
|
+
model,
|
|
1140
|
+
partitionId,
|
|
1141
|
+
embedding,
|
|
1142
|
+
k,
|
|
1143
|
+
minScore,
|
|
1144
|
+
spec,
|
|
1145
|
+
proofs,
|
|
1146
|
+
batchOwners
|
|
1147
|
+
);
|
|
1148
|
+
});
|
|
1149
|
+
options.onRoute?.(
|
|
1150
|
+
allCoarseSucceeded
|
|
1151
|
+
? {
|
|
1152
|
+
route: "coarse-bit",
|
|
1153
|
+
mirrorRows,
|
|
1154
|
+
coarseK: decision.coarseK,
|
|
1155
|
+
rescored: totalRescored,
|
|
1156
|
+
}
|
|
1157
|
+
: {
|
|
1158
|
+
route: canUseV1ExactVec0 ? "exact-vec0" : "exact-sql",
|
|
1159
|
+
mirrorRows,
|
|
1160
|
+
}
|
|
1161
|
+
);
|
|
1162
|
+
return out;
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
if (canUseV1ExactVec0) {
|
|
1166
|
+
let anyFallback = false;
|
|
1167
|
+
const out = embeddings.map((embedding, i) => {
|
|
778
1168
|
const k = limits[i]!;
|
|
779
1169
|
const fast = searchVectorVariantsViaVec0(
|
|
780
1170
|
db,
|
|
@@ -788,6 +1178,7 @@ function searchVectorVariantsBatchInSnapshot(
|
|
|
788
1178
|
batchOwners
|
|
789
1179
|
);
|
|
790
1180
|
if (fast !== null) return fast;
|
|
1181
|
+
anyFallback = true;
|
|
791
1182
|
return searchVectorVariantsFromVariants(
|
|
792
1183
|
db,
|
|
793
1184
|
model,
|
|
@@ -800,8 +1191,15 @@ function searchVectorVariantsBatchInSnapshot(
|
|
|
800
1191
|
batchOwners
|
|
801
1192
|
);
|
|
802
1193
|
});
|
|
1194
|
+
options.onRoute?.({
|
|
1195
|
+
route: anyFallback ? "exact-sql" : "exact-vec0",
|
|
1196
|
+
mirrorRows,
|
|
1197
|
+
});
|
|
1198
|
+
return out;
|
|
803
1199
|
}
|
|
804
1200
|
|
|
1201
|
+
options.onRoute?.({ route: "exact-sql", mirrorRows });
|
|
1202
|
+
|
|
805
1203
|
// Filtered or dirty mirror: scan eligible vector_variants ONCE per chunk of up to 8 queries.
|
|
806
1204
|
const allResults: VectorSearchResult[][] = Array.from<VectorSearchResult[]>({
|
|
807
1205
|
length: embeddings.length,
|
|
@@ -827,7 +1225,7 @@ function searchVectorVariantsBatchInSnapshot(
|
|
|
827
1225
|
SELECT o.variant_id AS variantId, o.mirror_hash AS mirrorHash, o.seq,
|
|
828
1226
|
json_group_array(o.document_id) AS documentIds
|
|
829
1227
|
${spec.innerFrom}
|
|
830
|
-
|
|
1228
|
+
${spec.chunkJoin}
|
|
831
1229
|
WHERE ${spec.conditions.join(" AND ")}
|
|
832
1230
|
GROUP BY o.variant_id, o.mirror_hash, o.seq
|
|
833
1231
|
) g
|
|
@@ -956,7 +1354,10 @@ export function searchVectorVariants(
|
|
|
956
1354
|
options: VectorSearchOptions = {}
|
|
957
1355
|
): VectorSearchResult[] | null {
|
|
958
1356
|
const inOuterTransaction = db.inTransaction;
|
|
959
|
-
|
|
1357
|
+
const pendingPersist: {
|
|
1358
|
+
value?: NullCosineCheckResult["persistAfterSnapshot"];
|
|
1359
|
+
} = {};
|
|
1360
|
+
const result = db.transaction(() =>
|
|
960
1361
|
searchVectorVariantsInSnapshot(
|
|
961
1362
|
db,
|
|
962
1363
|
model,
|
|
@@ -964,9 +1365,14 @@ export function searchVectorVariants(
|
|
|
964
1365
|
embedding,
|
|
965
1366
|
k,
|
|
966
1367
|
options,
|
|
967
|
-
inOuterTransaction
|
|
1368
|
+
inOuterTransaction,
|
|
1369
|
+
pendingPersist
|
|
968
1370
|
)
|
|
969
1371
|
)();
|
|
1372
|
+
if (!inOuterTransaction && pendingPersist.value) {
|
|
1373
|
+
persistNullCosineAfterSnapshot(writerConnection(db), pendingPersist.value);
|
|
1374
|
+
}
|
|
1375
|
+
return result;
|
|
970
1376
|
}
|
|
971
1377
|
|
|
972
1378
|
/** Keep batched eligibility, provenance validation and ranking on one SQLite snapshot. */
|
|
@@ -979,7 +1385,10 @@ export function searchVectorVariantsBatch(
|
|
|
979
1385
|
options: VectorSearchOptions = {}
|
|
980
1386
|
): VectorSearchResult[][] | null {
|
|
981
1387
|
const inOuterTransaction = db.inTransaction;
|
|
982
|
-
|
|
1388
|
+
const pendingPersist: {
|
|
1389
|
+
value?: NullCosineCheckResult["persistAfterSnapshot"];
|
|
1390
|
+
} = {};
|
|
1391
|
+
const result = db.transaction(() =>
|
|
983
1392
|
searchVectorVariantsBatchInSnapshot(
|
|
984
1393
|
db,
|
|
985
1394
|
model,
|
|
@@ -987,7 +1396,12 @@ export function searchVectorVariantsBatch(
|
|
|
987
1396
|
embeddings,
|
|
988
1397
|
limits,
|
|
989
1398
|
options,
|
|
990
|
-
inOuterTransaction
|
|
1399
|
+
inOuterTransaction,
|
|
1400
|
+
pendingPersist
|
|
991
1401
|
)
|
|
992
1402
|
)();
|
|
1403
|
+
if (!inOuterTransaction && pendingPersist.value) {
|
|
1404
|
+
persistNullCosineAfterSnapshot(writerConnection(db), pendingPersist.value);
|
|
1405
|
+
}
|
|
1406
|
+
return result;
|
|
993
1407
|
}
|