@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.
Files changed (46) hide show
  1. package/README.md +1 -1
  2. package/assets/skill/SKILL.md +27 -0
  3. package/assets/skill/cli-reference.md +20 -13
  4. package/browser-extension/artifacts/{gno-browser-clipper-v2.16.0.zip → gno-browser-clipper-v2.17.0.zip} +0 -0
  5. package/browser-extension/artifacts/gno-browser-clipper-v2.17.0.zip.sha256 +1 -0
  6. package/browser-extension/dist/manifest.json +1 -1
  7. package/package.json +1 -1
  8. package/spec/cli.md +42 -2
  9. package/spec/db/schema.sql +53 -1
  10. package/spec/mcp.md +9 -3
  11. package/spec/output-schemas/doctor.schema.json +27 -0
  12. package/spec/output-schemas/retrieval-trace-payloads.schema.json +14 -0
  13. package/spec/output-schemas/search-results.schema.json +14 -0
  14. package/spec/output-schemas/status.schema.json +22 -0
  15. package/src/cli/commands/doctor.ts +101 -29
  16. package/src/cli/commands/links.ts +29 -5
  17. package/src/cli/commands/vec.ts +180 -8
  18. package/src/cli/program.ts +43 -0
  19. package/src/config/types.ts +6 -0
  20. package/src/core/retrieval-trace-session.ts +3 -0
  21. package/src/core/retrieval-trace.ts +9 -0
  22. package/src/core/vector-partition-status.ts +13 -0
  23. package/src/embed/variant-backlog.ts +36 -2
  24. package/src/mcp/tools/links.ts +7 -1
  25. package/src/pipeline/explain.ts +17 -2
  26. package/src/pipeline/hybrid.ts +28 -1
  27. package/src/pipeline/types.ts +7 -0
  28. package/src/pipeline/vsearch.ts +19 -2
  29. package/src/serve/public/globals.built.css +1 -1
  30. package/src/serve/routes/links.ts +7 -1
  31. package/src/store/migrations/038-vector-mirror-null-cosine.ts +47 -0
  32. package/src/store/migrations/039-vector-mirror-v2.ts +94 -0
  33. package/src/store/migrations/index.ts +4 -0
  34. package/src/store/sqlite/adapter.ts +117 -74
  35. package/src/store/vector/coarse-router.ts +312 -0
  36. package/src/store/vector/partition-index.ts +309 -18
  37. package/src/store/vector/proof-cache.ts +132 -26
  38. package/src/store/vector/runtime-compat.ts +6 -0
  39. package/src/store/vector/sqlite-vec.ts +43 -26
  40. package/src/store/vector/stats.ts +8 -0
  41. package/src/store/vector/status.ts +18 -1
  42. package/src/store/vector/types.ts +9 -0
  43. package/src/store/vector/v2-mirror.ts +669 -0
  44. package/src/store/vector/variant-search.ts +434 -20
  45. package/src/store/vector/variants.ts +480 -32
  46. package/browser-extension/artifacts/gno-browser-clipper-v2.16.0.zip.sha256 +0 -1
@@ -412,7 +412,13 @@ export async function handleDocSimilar(
412
412
  const searchResult = await ctx.vectorIndex.searchNearest(
413
413
  embedding,
414
414
  candidateLimit,
415
- storedVectorSearchOptions(source)
415
+ {
416
+ ...storedVectorSearchOptions(source),
417
+ coarseVectorSearch: ctx.config?.coarseVectorSearch,
418
+ ...(!crossCollection
419
+ ? { eligibility: { collection: doc.collection } }
420
+ : {}),
421
+ }
416
422
  );
417
423
 
418
424
  if (!searchResult.ok) {
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Migration: persist per-partition null-cosine state on `vector_mirror_state`.
3
+ *
4
+ * `hasNullCosineVariant` is consulted only after `isMirrorClean` passes, so
5
+ * storing `null_cosine` (0 = clean of null-cosine vectors, 1 = contains at
6
+ * least one null-cosine vector, NULL = unknown) directly on the clean mirror
7
+ * row reuses migration 036's delete-only `vector_variants` triggers with no
8
+ * extra table or triggers. Older binaries that overwrite the row with
9
+ * `(partition_id, dirty)` reset `null_cosine` to NULL (safe unknown fallback).
10
+ *
11
+ * @module src/store/migrations/038-vector-mirror-null-cosine
12
+ */
13
+
14
+ import type { Database } from "bun:sqlite";
15
+
16
+ import type { Migration } from "./runner";
17
+
18
+ export const migration: Migration = {
19
+ version: 38,
20
+ name: "vector_mirror_null_cosine",
21
+
22
+ up(db: Database): void {
23
+ const hasColumn = db
24
+ .query<{ name: string }, []>(
25
+ "SELECT name FROM pragma_table_info('vector_mirror_state') WHERE name = 'null_cosine'"
26
+ )
27
+ .get();
28
+ if (!hasColumn) {
29
+ db.exec(`
30
+ ALTER TABLE vector_mirror_state
31
+ ADD COLUMN null_cosine INTEGER
32
+ CHECK (null_cosine IS NULL OR null_cosine IN (0, 1));
33
+ `);
34
+ }
35
+ },
36
+
37
+ down(db: Database): void {
38
+ const hasColumn = db
39
+ .query<{ name: string }, []>(
40
+ "SELECT name FROM pragma_table_info('vector_mirror_state') WHERE name = 'null_cosine'"
41
+ )
42
+ .get();
43
+ if (hasColumn) {
44
+ db.exec("ALTER TABLE vector_mirror_state DROP COLUMN null_cosine;");
45
+ }
46
+ },
47
+ };
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Migration 039: bit coarse vec0 mirror mapping, state table, and delete-only
3
+ * invalidation triggers (fn-235).
4
+ *
5
+ * Adds:
6
+ * - `vector_partition_rows`: stable integer rowids mapping `(partition_id, collection, variant_id)`
7
+ * to `vec_v2_<partitionId>` rowids.
8
+ * - `vector_mirror_v2_state`: per-partition clean mark plus build-time recall
9
+ * self-check results (`check_recall`, `check_queries`, `check_coarse_k`).
10
+ * - Delete-only triggers on `vector_variants` (I/U/D) and `vector_owners` (I/U)
11
+ * that write ONLY to the plain `vector_mirror_v2_state` table (never referencing
12
+ * any `vec0` table and never adding triggers to `documents`).
13
+ */
14
+ import type { Database } from "bun:sqlite";
15
+
16
+ import type { Migration } from "./index";
17
+
18
+ export const migration: Migration = {
19
+ version: 39,
20
+ name: "vector_mirror_v2",
21
+
22
+ up(db: Database): void {
23
+ db.exec(`
24
+ CREATE TABLE IF NOT EXISTS vector_partition_rows (
25
+ id INTEGER PRIMARY KEY,
26
+ partition_id TEXT NOT NULL,
27
+ collection TEXT NOT NULL,
28
+ variant_id INTEGER NOT NULL,
29
+ UNIQUE(partition_id, collection, variant_id)
30
+ )
31
+ `);
32
+ db.exec(`
33
+ CREATE INDEX IF NOT EXISTS idx_vector_partition_rows_part_var
34
+ ON vector_partition_rows(partition_id, variant_id)
35
+ `);
36
+ db.exec(`
37
+ CREATE TABLE IF NOT EXISTS vector_mirror_v2_state (
38
+ partition_id TEXT PRIMARY KEY,
39
+ dirty INTEGER NOT NULL CHECK (dirty IN (0, 1)),
40
+ check_recall REAL,
41
+ check_queries INTEGER,
42
+ check_coarse_k INTEGER
43
+ )
44
+ `);
45
+ db.exec(`
46
+ CREATE TRIGGER IF NOT EXISTS trg_vector_variants_v2_insert
47
+ AFTER INSERT ON vector_variants
48
+ BEGIN
49
+ DELETE FROM vector_mirror_v2_state WHERE partition_id = NEW.partition_id;
50
+ END
51
+ `);
52
+ db.exec(`
53
+ CREATE TRIGGER IF NOT EXISTS trg_vector_variants_v2_update
54
+ AFTER UPDATE ON vector_variants
55
+ BEGIN
56
+ DELETE FROM vector_mirror_v2_state WHERE partition_id = OLD.partition_id;
57
+ DELETE FROM vector_mirror_v2_state WHERE partition_id = NEW.partition_id;
58
+ END
59
+ `);
60
+ db.exec(`
61
+ CREATE TRIGGER IF NOT EXISTS trg_vector_variants_v2_delete
62
+ AFTER DELETE ON vector_variants
63
+ BEGIN
64
+ DELETE FROM vector_mirror_v2_state WHERE partition_id = OLD.partition_id;
65
+ END
66
+ `);
67
+ db.exec(`
68
+ CREATE TRIGGER IF NOT EXISTS trg_vector_owners_v2_insert
69
+ AFTER INSERT ON vector_owners
70
+ BEGIN
71
+ DELETE FROM vector_mirror_v2_state WHERE partition_id = NEW.partition_id;
72
+ END
73
+ `);
74
+ db.exec(`
75
+ CREATE TRIGGER IF NOT EXISTS trg_vector_owners_v2_update
76
+ AFTER UPDATE ON vector_owners
77
+ BEGIN
78
+ DELETE FROM vector_mirror_v2_state WHERE partition_id = OLD.partition_id;
79
+ DELETE FROM vector_mirror_v2_state WHERE partition_id = NEW.partition_id;
80
+ END
81
+ `);
82
+ },
83
+
84
+ down(db: Database): void {
85
+ db.exec("DROP TRIGGER IF EXISTS trg_vector_owners_v2_update");
86
+ db.exec("DROP TRIGGER IF EXISTS trg_vector_owners_v2_insert");
87
+ db.exec("DROP TRIGGER IF EXISTS trg_vector_variants_v2_delete");
88
+ db.exec("DROP TRIGGER IF EXISTS trg_vector_variants_v2_update");
89
+ db.exec("DROP TRIGGER IF EXISTS trg_vector_variants_v2_insert");
90
+ db.exec("DROP TABLE IF EXISTS vector_mirror_v2_state");
91
+ db.exec("DROP INDEX IF EXISTS idx_vector_partition_rows_part_var");
92
+ db.exec("DROP TABLE IF EXISTS vector_partition_rows");
93
+ },
94
+ };
@@ -51,6 +51,8 @@ import { migration as m034 } from "./034-collection-link-workspace";
51
51
  import { migration as m035 } from "./035-vector-length-index";
52
52
  import { migration as m036 } from "./036-vector-mirror-state";
53
53
  import { migration as m037 } from "./037-performance-indexes";
54
+ import { migration as m038 } from "./038-vector-mirror-null-cosine";
55
+ import { migration as m039 } from "./039-vector-mirror-v2";
54
56
 
55
57
  /** All migrations in order */
56
58
  export const migrations = [
@@ -91,4 +93,6 @@ export const migrations = [
91
93
  m035,
92
94
  m036,
93
95
  m037,
96
+ m038,
97
+ m039,
94
98
  ];
@@ -12,6 +12,8 @@ import "./setup";
12
12
  import { Database } from "bun:sqlite";
13
13
  // node:async_hooks binds nested transactions to one async request; Bun has no separate native equivalent.
14
14
  import { AsyncLocalStorage } from "node:async_hooks";
15
+ // node:os totalmem: no Bun equivalent for total system memory.
16
+ import { totalmem } from "node:os";
15
17
  // node:path basename: no Bun path utilities.
16
18
  import { basename } from "node:path";
17
19
 
@@ -577,6 +579,67 @@ function withoutBusyWait(db: Database, write: () => void): void {
577
579
  }
578
580
 
579
581
  /** Resolve a caller-supplied busy_timeout, defaulting rather than using 0. */
582
+ export const SQLITE_PRIMARY_CACHE_SIZE = -131072;
583
+ export const SQLITE_READ_CACHE_SIZE = -65536;
584
+
585
+ const SQLITE_MAX_REQUESTED_MMAP_BYTES = 8 * 1024 * 1024 * 1024;
586
+ const SQLITE_32BIT_MMAP_BYTES = 268435456;
587
+
588
+ const IS_64_BIT_ARCH =
589
+ process.arch === "x64" ||
590
+ process.arch === "arm64" ||
591
+ process.arch === "ppc64" ||
592
+ process.arch === "s390x" ||
593
+ process.arch === "riscv64" ||
594
+ process.arch === "loong64";
595
+
596
+ /**
597
+ * Compute the requested SQLite mmap_size in bytes: on 64-bit systems, up to
598
+ * 8 GiB bounded by half of total system memory (SQLite clamps to its compile-time
599
+ * MAX_MMAP_SIZE); on 32-bit systems, 256 MiB.
600
+ */
601
+ export function computeRequestedMmapSizeBytes(
602
+ totalMemoryBytes: number = totalmem(),
603
+ is64Bit: boolean = IS_64_BIT_ARCH
604
+ ): number {
605
+ if (!is64Bit) {
606
+ return SQLITE_32BIT_MMAP_BYTES;
607
+ }
608
+ const halfRam = Math.floor(Math.max(0, totalMemoryBytes) * 0.5);
609
+ return Math.min(SQLITE_MAX_REQUESTED_MMAP_BYTES, halfRam);
610
+ }
611
+
612
+ /**
613
+ * Apply shared SQLite memory/I/O PRAGMAs across all database open paths
614
+ * (`open`, `openReadOnly`, `openReadDb`) and return the effective `mmap_size`
615
+ * read back from SQLite. Never throws when mmap_size is clamped or unsupported.
616
+ */
617
+ export function applySqlitePerformancePragmas(
618
+ db: Database,
619
+ role: "primary" | "read"
620
+ ): number {
621
+ db.exec("PRAGMA temp_store = MEMORY");
622
+ db.exec(
623
+ `PRAGMA cache_size = ${
624
+ role === "primary" ? SQLITE_PRIMARY_CACHE_SIZE : SQLITE_READ_CACHE_SIZE
625
+ }`
626
+ );
627
+ const requestedMmap = computeRequestedMmapSizeBytes();
628
+ try {
629
+ db.exec(`PRAGMA mmap_size = ${requestedMmap}`);
630
+ } catch {
631
+ // Platforms/filesystems that reject mmap_size keep default file I/O.
632
+ }
633
+ try {
634
+ return (
635
+ db.query<{ mmap_size: number }, []>("PRAGMA mmap_size").get()
636
+ ?.mmap_size ?? 0
637
+ );
638
+ } catch {
639
+ return 0;
640
+ }
641
+ }
642
+
580
643
  function resolveBusyTimeoutMs(busyTimeoutMs?: number): number {
581
644
  if (
582
645
  busyTimeoutMs === undefined ||
@@ -693,13 +756,7 @@ export class SqliteAdapter implements StorePort, SqliteDbProvider {
693
756
  // rely on the existing DB journal mode instead of failing open().
694
757
  }
695
758
 
696
- this.db.exec("PRAGMA temp_store = MEMORY");
697
- this.db.exec("PRAGMA cache_size = -64000");
698
- try {
699
- this.db.exec("PRAGMA mmap_size = 268435456");
700
- } catch {
701
- // Platforms/filesystems that reject mmap_size keep default file I/O.
702
- }
759
+ applySqlitePerformancePragmas(this.db, "primary");
703
760
 
704
761
  if (process.env.CI) {
705
762
  this.db.exec("PRAGMA synchronous = OFF");
@@ -754,13 +811,7 @@ export class SqliteAdapter implements StorePort, SqliteDbProvider {
754
811
  this.db.exec(
755
812
  `PRAGMA busy_timeout = ${resolveBusyTimeoutMs(busyTimeoutMs)}`
756
813
  );
757
- this.db.exec("PRAGMA temp_store = MEMORY");
758
- this.db.exec("PRAGMA cache_size = -32000");
759
- try {
760
- this.db.exec("PRAGMA mmap_size = 268435456");
761
- } catch {
762
- // Platforms/filesystems that reject mmap_size keep default file I/O.
763
- }
814
+ applySqlitePerformancePragmas(this.db, "read");
764
815
  this.contextGeneration += 1;
765
816
  return ok(undefined);
766
817
  } catch (cause) {
@@ -865,13 +916,7 @@ export class SqliteAdapter implements StorePort, SqliteDbProvider {
865
916
  main.query<{ timeout: number }, []>("PRAGMA busy_timeout").get()
866
917
  ?.timeout ?? DEFAULT_BUSY_TIMEOUT_MS;
867
918
  db.exec(`PRAGMA busy_timeout = ${busyTimeout}`);
868
- db.exec("PRAGMA temp_store = MEMORY");
869
- db.exec("PRAGMA cache_size = -32000");
870
- try {
871
- db.exec("PRAGMA mmap_size = 268435456");
872
- } catch {
873
- // Platforms/filesystems that reject mmap_size keep default file I/O.
874
- }
919
+ applySqlitePerformancePragmas(db, "read");
875
920
  if (this.ftsTokenizer.startsWith("snowball")) {
876
921
  const snowball = loadFts5Snowball(db);
877
922
  if (!snowball.loaded)
@@ -2851,63 +2896,61 @@ export class SqliteAdapter implements StorePort, SqliteDbProvider {
2851
2896
  }>
2852
2897
  >
2853
2898
  > {
2854
- if (options.documentIds.length === 0 && options.mirrorHashes.length === 0) {
2899
+ const uniqueIds = [...new Set(options.documentIds)];
2900
+ const uniqueHashes = [...new Set(options.mirrorHashes)];
2901
+ if (uniqueIds.length === 0 && uniqueHashes.length === 0) {
2855
2902
  return ok([]);
2856
2903
  }
2857
2904
  try {
2858
2905
  const db = this.ensureOpen();
2859
- const idsJson = JSON.stringify([...new Set(options.documentIds)]);
2860
- const hashesJson = JSON.stringify([...new Set(options.mirrorHashes)]);
2861
- const rows = options.collection
2862
- ? db
2863
- .query<
2864
- {
2865
- id: number;
2866
- docid: string;
2867
- uri: string;
2868
- title: string | null;
2869
- collection: string;
2870
- rel_path: string;
2871
- mirror_hash: string;
2872
- },
2873
- [string, string, string]
2874
- >(
2875
- `SELECT id, docid, uri, title, collection, rel_path, mirror_hash
2876
- FROM documents
2877
- WHERE active = 1
2878
- AND mirror_hash IS NOT NULL
2879
- AND collection = ?
2880
- AND (
2881
- id IN (SELECT value FROM json_each(?))
2882
- OR mirror_hash IN (SELECT value FROM json_each(?))
2883
- )
2884
- ORDER BY id`
2885
- )
2886
- .all(options.collection, idsJson, hashesJson)
2887
- : db
2888
- .query<
2889
- {
2890
- id: number;
2891
- docid: string;
2892
- uri: string;
2893
- title: string | null;
2894
- collection: string;
2895
- rel_path: string;
2896
- mirror_hash: string;
2897
- },
2898
- [string, string]
2899
- >(
2900
- `SELECT id, docid, uri, title, collection, rel_path, mirror_hash
2901
- FROM documents
2902
- WHERE active = 1
2903
- AND mirror_hash IS NOT NULL
2904
- AND (
2905
- id IN (SELECT value FROM json_each(?))
2906
- OR mirror_hash IN (SELECT value FROM json_each(?))
2907
- )
2908
- ORDER BY id`
2909
- )
2910
- .all(idsJson, hashesJson);
2906
+ const collectionClause = options.collection
2907
+ ? " AND d.collection = ?"
2908
+ : "";
2909
+ const legs: string[] = [];
2910
+ const params: string[] = [];
2911
+
2912
+ // Drive each leg from json_each(?) via CROSS JOIN so SQLite seeks by PK
2913
+ // or idx_documents_mirror_hash instead of scanning idx_documents_collection
2914
+ // with a Bloom filter on scoped lookups.
2915
+ if (uniqueIds.length > 0) {
2916
+ legs.push(
2917
+ `SELECT d.id AS id, d.docid, d.uri, d.title, d.collection, d.rel_path, d.mirror_hash
2918
+ FROM json_each(?) AS j
2919
+ CROSS JOIN documents d ON d.id = j.value
2920
+ WHERE d.active = 1
2921
+ AND d.mirror_hash IS NOT NULL${collectionClause}`
2922
+ );
2923
+ params.push(JSON.stringify(uniqueIds));
2924
+ if (options.collection) params.push(options.collection);
2925
+ }
2926
+
2927
+ if (uniqueHashes.length > 0) {
2928
+ legs.push(
2929
+ `SELECT d.id AS id, d.docid, d.uri, d.title, d.collection, d.rel_path, d.mirror_hash
2930
+ FROM json_each(?) AS j
2931
+ CROSS JOIN documents d INDEXED BY idx_documents_mirror_hash ON d.mirror_hash = j.value
2932
+ WHERE d.active = 1
2933
+ AND d.mirror_hash IS NOT NULL${collectionClause}`
2934
+ );
2935
+ params.push(JSON.stringify(uniqueHashes));
2936
+ if (options.collection) params.push(options.collection);
2937
+ }
2938
+
2939
+ const sql = `${legs.join("\nUNION\n")}\nORDER BY 1`;
2940
+ const rows = db
2941
+ .query<
2942
+ {
2943
+ id: number;
2944
+ docid: string;
2945
+ uri: string;
2946
+ title: string | null;
2947
+ collection: string;
2948
+ rel_path: string;
2949
+ mirror_hash: string;
2950
+ },
2951
+ string[]
2952
+ >(sql)
2953
+ .all(...params);
2911
2954
 
2912
2955
  return ok(
2913
2956
  rows.map((row) => ({