@gmickel/gno 2.15.0 → 2.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/assets/skill/SKILL.md +27 -0
- package/assets/skill/cli-reference.md +24 -15
- package/assets/spa-production.json.gz +0 -0
- package/browser-extension/artifacts/{gno-browser-clipper-v2.15.0.zip → gno-browser-clipper-v2.17.0.zip} +0 -0
- package/browser-extension/artifacts/gno-browser-clipper-v2.17.0.zip.sha256 +1 -0
- package/browser-extension/dist/manifest.json +1 -1
- package/package.json +1 -1
- package/spec/cli.md +43 -3
- package/spec/db/schema.sql +53 -1
- package/spec/mcp.md +9 -3
- package/spec/output-schemas/doctor.schema.json +27 -0
- package/spec/output-schemas/retrieval-trace-payloads.schema.json +14 -0
- package/spec/output-schemas/search-results.schema.json +14 -0
- package/spec/output-schemas/status.schema.json +22 -0
- package/src/cli/commands/agents/commands.ts +1 -1
- package/src/cli/commands/ask.ts +1 -1
- package/src/cli/commands/context-build.ts +1 -1
- package/src/cli/commands/doctor.ts +101 -29
- package/src/cli/commands/embed.ts +2 -2
- package/src/cli/commands/links.ts +30 -6
- package/src/cli/commands/mcp/install.ts +2 -2
- package/src/cli/commands/mcp/status.ts +1 -1
- package/src/cli/commands/mcp/uninstall.ts +1 -1
- package/src/cli/commands/query.ts +1 -1
- package/src/cli/commands/shared.ts +4 -4
- package/src/cli/commands/skill/install.ts +1 -1
- package/src/cli/commands/skill/paths-cmd.ts +1 -1
- package/src/cli/commands/skill/uninstall.ts +1 -1
- package/src/cli/commands/status.ts +75 -58
- package/src/cli/commands/vec.ts +180 -8
- package/src/cli/config-cache.ts +86 -0
- package/src/cli/globals.ts +64 -0
- package/src/cli/program.ts +63 -36
- package/src/cli/run.ts +121 -67
- package/src/cli/runtime-health.ts +132 -122
- package/src/cli/session-binding.ts +11 -2
- package/src/config/saver.ts +8 -0
- package/src/config/types.ts +6 -0
- package/src/converters/adapters/browser-export/formats.ts +25 -15
- package/src/converters/adapters/email/adapter.ts +37 -53
- package/src/converters/adapters/email/html.ts +37 -20
- package/src/converters/adapters/email/mime.ts +71 -39
- package/src/converters/adapters/ical/adapter.ts +20 -12
- package/src/converters/adapters/jsonl/adapter.ts +8 -6
- package/src/converters/adapters/shared/html-text.ts +55 -37
- package/src/converters/adapters/shared/ooxml-protection.ts +5 -13
- package/src/converters/adapters/shared/utf8-lines.ts +69 -31
- package/src/converters/adapters/transcript/adapter.ts +12 -7
- package/src/converters/adapters/xlsx/adapter.ts +10 -7
- package/src/converters/adapters/xlsx/sheet-markdown.ts +93 -50
- package/src/converters/canonicalize.ts +17 -3
- package/src/converters/native/markdown.ts +24 -17
- package/src/converters/native/plaintext.ts +5 -6
- package/src/converters/registry.ts +130 -8
- package/src/core/activation-status.ts +4 -8
- package/src/core/change-diff.ts +38 -11
- package/src/core/context-compiler.ts +17 -18
- package/src/core/context-evidence.ts +116 -25
- package/src/core/context-verifier.ts +70 -31
- package/src/core/file-lock.ts +10 -2
- package/src/core/graph-analysis.ts +3 -3
- package/src/core/link-inventory-markdown.ts +2 -2
- package/src/core/links.ts +20 -87
- package/src/core/memory-fence.ts +16 -9
- package/src/core/memory-recall.ts +168 -34
- package/src/core/path-rules.ts +143 -0
- package/src/core/retrieval-trace-session.ts +3 -0
- package/src/core/retrieval-trace.ts +9 -0
- package/src/core/section-parse.ts +16 -6
- package/src/core/typed-metadata.ts +68 -0
- package/src/core/vector-partition-status.ts +13 -0
- package/src/core/wiki-link-syntax.ts +88 -0
- package/src/embed/backlog.ts +142 -60
- package/src/embed/retry.ts +39 -33
- package/src/embed/variant-backlog.ts +582 -116
- package/src/embed/variant-retry.ts +157 -48
- package/src/index.ts +3 -3
- package/src/ingestion/budget-error-codes.ts +13 -0
- package/src/ingestion/chunker.ts +60 -49
- package/src/ingestion/compiled-context.ts +4 -5
- package/src/ingestion/convert-file.ts +1 -1
- package/src/ingestion/document-metadata.ts +16 -9
- package/src/ingestion/excluded-ranges.ts +97 -0
- package/src/ingestion/file-processor.ts +122 -37
- package/src/ingestion/frontmatter.ts +35 -7
- package/src/ingestion/graph-reconciliation.ts +187 -51
- package/src/ingestion/language.ts +66 -61
- package/src/ingestion/position.ts +5 -5
- package/src/ingestion/prepare-file.ts +50 -29
- package/src/ingestion/record-adapter-canonical.ts +37 -26
- package/src/ingestion/record-adapter.ts +46 -26
- package/src/ingestion/record-container.ts +121 -53
- package/src/ingestion/record-path.ts +5 -2
- package/src/ingestion/source-availability/darwin-io.ts +10 -7
- package/src/ingestion/source-availability/darwin-path.ts +21 -3
- package/src/ingestion/source-availability/readers.ts +51 -0
- package/src/ingestion/strip.ts +39 -105
- package/src/ingestion/sync.ts +692 -386
- package/src/ingestion/typed-metadata.ts +10 -4
- package/src/ingestion/walker.ts +485 -158
- package/src/llm/cache.ts +128 -84
- package/src/llm/http-inference.ts +112 -35
- package/src/llm/http-policy.ts +101 -0
- package/src/llm/httpEmbedding.ts +8 -1
- package/src/llm/httpGeneration.ts +5 -1
- package/src/llm/httpRerank.ts +5 -1
- package/src/llm/inference-scope.ts +15 -2
- package/src/llm/lazy-ports.ts +33 -3
- package/src/llm/lockfile.ts +34 -7
- package/src/llm/model-cleanup.ts +24 -0
- package/src/llm/native-worker/client.ts +23 -12
- package/src/llm/native-worker/dispatcher.ts +19 -9
- package/src/llm/native-worker/embedding-identity.ts +42 -16
- package/src/llm/native-worker/entry.ts +2 -1
- package/src/llm/native-worker/owner.ts +1 -0
- package/src/llm/native-worker/ports.ts +86 -23
- package/src/llm/native-worker/protocol.ts +386 -55
- package/src/llm/nodeLlamaCpp/adapter.ts +46 -29
- package/src/llm/nodeLlamaCpp/embedding.ts +93 -40
- package/src/llm/nodeLlamaCpp/generation.ts +194 -30
- package/src/llm/nodeLlamaCpp/lifecycle.ts +2 -0
- package/src/llm/nodeLlamaCpp/rerank-capacity.ts +101 -23
- package/src/llm/nodeLlamaCpp/rerank.ts +253 -44
- package/src/llm/types.ts +14 -0
- package/src/mcp/context.ts +342 -23
- package/src/mcp/http-transport.ts +21 -3
- package/src/mcp/tool-profile-types.ts +62 -0
- package/src/mcp/tool-profile.ts +32 -56
- package/src/mcp/tools/context.ts +5 -1
- package/src/mcp/tools/index.ts +269 -191
- package/src/mcp/tools/links.ts +14 -6
- package/src/mcp/tools/query.ts +60 -37
- package/src/mcp/tools/vsearch.ts +25 -14
- package/src/pipeline/answer.ts +133 -40
- package/src/pipeline/claim-verification.ts +293 -189
- package/src/pipeline/claim-verifier.ts +6 -7
- package/src/pipeline/egress-lineage.ts +37 -26
- package/src/pipeline/exclude.ts +34 -11
- package/src/pipeline/expansion.ts +233 -19
- package/src/pipeline/explain.ts +17 -2
- package/src/pipeline/filters.ts +169 -33
- package/src/pipeline/graph-retrieval.ts +117 -61
- package/src/pipeline/hybrid.ts +626 -330
- package/src/pipeline/hydration.ts +86 -24
- package/src/pipeline/intent.ts +15 -7
- package/src/pipeline/owner-fusion.ts +61 -19
- package/src/pipeline/query-language.ts +15 -2
- package/src/pipeline/rerank.ts +15 -22
- package/src/pipeline/search.ts +193 -72
- package/src/pipeline/snippet.ts +15 -2
- package/src/pipeline/temporal.ts +51 -10
- package/src/pipeline/types.ts +7 -0
- package/src/pipeline/vsearch.ts +269 -154
- package/src/publish/attachment-raster.ts +2 -16
- package/src/publish/encrypted-export.ts +15 -5
- package/src/serve/browse-tree.ts +50 -42
- package/src/serve/connectors.ts +2 -1
- package/src/serve/context.ts +13 -4
- package/src/serve/doc-events.ts +3 -0
- package/src/serve/public/app.tsx +49 -21
- package/src/serve/public/components/BrowseTreeSidebar.tsx +163 -23
- package/src/serve/public/components/CaptureModal.tsx +14 -10
- package/src/serve/public/components/ai-elements/code-block.tsx +87 -50
- package/src/serve/public/components/editor/MarkdownPreview.tsx +99 -67
- package/src/serve/public/globals.built.css +1 -1
- package/src/serve/public/globals.css +19 -0
- package/src/serve/public/hooks/useCaptureModal.tsx +23 -10
- package/src/serve/public/lib/metadata-filter.ts +234 -9
- package/src/serve/public/pages/Browse.tsx +61 -27
- package/src/serve/public/pages/DocView.tsx +44 -25
- package/src/serve/public/pages/GraphView.tsx +8 -3
- package/src/serve/resident-runtime.ts +199 -48
- package/src/serve/routes/api.ts +98 -67
- package/src/serve/routes/links.ts +25 -5
- package/src/serve/server.ts +1370 -1180
- package/src/serve/spa-bundle-source.ts +52 -27
- package/src/serve/status.ts +101 -52
- package/src/serve/watch-reconciliation-fallback.ts +11 -7
- package/src/serve/watch-reconciliation-shared.ts +42 -4
- package/src/serve/watch-reconciliation.ts +21 -8
- package/src/serve/watch-service-events.ts +7 -1
- package/src/serve/watch-service-hosts.ts +3 -1
- package/src/serve/watch-service.ts +46 -21
- package/src/serve/watch-snapshot-scan.ts +6 -15
- package/src/sessions/archive.ts +20 -8
- package/src/sessions/sanitize.ts +38 -18
- package/src/sessions/service.ts +180 -102
- package/src/store/migrations/037-performance-indexes.ts +56 -0
- package/src/store/migrations/038-vector-mirror-null-cosine.ts +47 -0
- package/src/store/migrations/039-vector-mirror-v2.ts +94 -0
- package/src/store/migrations/index.ts +6 -0
- package/src/store/sqlite/adapter.ts +1260 -651
- package/src/store/sqlite/chunking-policy.ts +15 -1
- package/src/store/sqlite/eligibility.ts +110 -70
- package/src/store/sqlite/graph-neighbors.ts +71 -46
- package/src/store/sqlite/graph-reference-state.ts +27 -3
- package/src/store/sqlite/graph-similarity.ts +59 -12
- package/src/store/sqlite/metadata-predicate.ts +123 -28
- package/src/store/sqlite/retrieval-trace-management-store.ts +126 -33
- package/src/store/sqlite/retrieval-trace-retention.ts +88 -76
- package/src/store/sqlite/retrieval-trace-rows.ts +7 -3
- package/src/store/types.ts +32 -0
- package/src/store/vector/coarse-router.ts +312 -0
- package/src/store/vector/eligibility.ts +39 -0
- package/src/store/vector/lazy.ts +17 -0
- package/src/store/vector/partition-index.ts +333 -59
- package/src/store/vector/proof-cache.ts +209 -0
- package/src/store/vector/runtime-compat.ts +6 -0
- package/src/store/vector/sqlite-vec.ts +270 -162
- package/src/store/vector/stats.ts +37 -73
- package/src/store/vector/status.ts +169 -53
- package/src/store/vector/stored-vectors.ts +131 -45
- package/src/store/vector/types.ts +19 -0
- package/src/store/vector/v2-mirror.ts +669 -0
- package/src/store/vector/variant-search.ts +1187 -111
- package/src/store/vector/variants.ts +964 -160
- package/browser-extension/artifacts/gno-browser-clipper-v2.15.0.zip.sha256 +0 -1
|
@@ -1,34 +1,101 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Maintenance of an activated vector partition's vec0 index
|
|
3
3
|
* (`vec_v1_<partition>`), which mirrors the partition's `vector_variants`.
|
|
4
|
-
* `gno vec sync` and `gno vec
|
|
5
|
-
*
|
|
4
|
+
* `gno vec sync`, `gno vec rebuild`, and `gno vec prune` use it on indexes
|
|
5
|
+
* embedded since 2.7. Sync/rebuild never change authority, owners, or variants,
|
|
6
|
+
* so the variant epoch stays.
|
|
6
7
|
*
|
|
7
8
|
* @module src/store/vector/partition-index
|
|
8
9
|
*/
|
|
9
10
|
import type { Database } from "bun:sqlite";
|
|
10
11
|
|
|
12
|
+
import { modelTableName } from "./sqlite-vec";
|
|
13
|
+
import {
|
|
14
|
+
rebuildV2PartitionMirrorInTx,
|
|
15
|
+
runV2RecallSelfCheck,
|
|
16
|
+
syncV2PartitionMirrorInTx,
|
|
17
|
+
} from "./v2-mirror";
|
|
18
|
+
|
|
11
19
|
export interface StoredPartition {
|
|
12
20
|
partitionId: string;
|
|
13
21
|
dimensions: number;
|
|
14
22
|
}
|
|
15
23
|
|
|
24
|
+
export type NullCosineMark = 0 | 1 | null;
|
|
25
|
+
|
|
26
|
+
/** True when migration 038's `null_cosine` column is present on `vector_mirror_state`. */
|
|
27
|
+
export function hasNullCosineColumn(db: Database): boolean {
|
|
28
|
+
return Boolean(
|
|
29
|
+
db
|
|
30
|
+
.query<{ name: string }, []>(
|
|
31
|
+
"SELECT name FROM pragma_table_info('vector_mirror_state') WHERE name = 'null_cosine'"
|
|
32
|
+
)
|
|
33
|
+
.get()
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Exact SQLite null-cosine check for one embedding BLOB. Uses sqlite-vec's own
|
|
39
|
+
* float32 cosine distance so underflow/overflow vectors (e.g. components of
|
|
40
|
+
* 1e-30 or 1e30) match SQLite's `vec_distance_cosine` behavior identically.
|
|
41
|
+
*/
|
|
42
|
+
export function isNullCosineEmbedding(db: Database, blob: Uint8Array): boolean {
|
|
43
|
+
const row = db
|
|
44
|
+
.query<{ is_null: number }, [Uint8Array]>(
|
|
45
|
+
"SELECT vec_distance_cosine(?1, ?1) IS NULL AS is_null"
|
|
46
|
+
)
|
|
47
|
+
.get(blob);
|
|
48
|
+
return row?.is_null === 1;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Probe whether `partitionId` contains any variant whose cosine distance can
|
|
53
|
+
* be NULL (zero or float32-norm underflow/overflow). Requires sqlite-vec.
|
|
54
|
+
*/
|
|
55
|
+
export function probePartitionNullCosine(
|
|
56
|
+
db: Database,
|
|
57
|
+
partitionId: string
|
|
58
|
+
): boolean {
|
|
59
|
+
return Boolean(
|
|
60
|
+
db
|
|
61
|
+
.query<unknown, [string]>(
|
|
62
|
+
`SELECT 1 FROM vector_variants
|
|
63
|
+
WHERE partition_id = ?
|
|
64
|
+
AND vec_distance_cosine(embedding, embedding) IS NULL
|
|
65
|
+
LIMIT 1`
|
|
66
|
+
)
|
|
67
|
+
.get(partitionId)
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
16
71
|
/**
|
|
17
72
|
* Records that the partition's vec0 mirror matches its stored variants
|
|
18
|
-
* (migration 036)
|
|
19
|
-
*
|
|
73
|
+
* (migration 036) and optionally persists its `null_cosine` state (migration
|
|
74
|
+
* 038). A no-op on databases without mirror tracking. Callers run it inside
|
|
75
|
+
* the transaction that made the mirror current.
|
|
20
76
|
*/
|
|
21
|
-
export function markMirrorClean(
|
|
77
|
+
export function markMirrorClean(
|
|
78
|
+
db: Database,
|
|
79
|
+
partitionId: string,
|
|
80
|
+
nullCosine: NullCosineMark = null
|
|
81
|
+
): void {
|
|
22
82
|
if (
|
|
23
83
|
!db
|
|
24
84
|
.query("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?")
|
|
25
85
|
.get("vector_mirror_state")
|
|
26
86
|
)
|
|
27
87
|
return;
|
|
28
|
-
db
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
88
|
+
if (hasNullCosineColumn(db)) {
|
|
89
|
+
db.run(
|
|
90
|
+
"INSERT OR REPLACE INTO vector_mirror_state (partition_id, dirty, null_cosine) VALUES (?, 0, ?)",
|
|
91
|
+
[partitionId, nullCosine]
|
|
92
|
+
);
|
|
93
|
+
} else {
|
|
94
|
+
db.run(
|
|
95
|
+
"INSERT OR REPLACE INTO vector_mirror_state (partition_id, dirty) VALUES (?, 0)",
|
|
96
|
+
[partitionId]
|
|
97
|
+
);
|
|
98
|
+
}
|
|
32
99
|
}
|
|
33
100
|
|
|
34
101
|
const tableName = (partition: StoredPartition): string =>
|
|
@@ -41,79 +108,286 @@ const createTable = (db: Database, partition: StoredPartition): void => {
|
|
|
41
108
|
)`);
|
|
42
109
|
};
|
|
43
110
|
|
|
44
|
-
const
|
|
45
|
-
db:
|
|
46
|
-
|
|
47
|
-
rows: Iterable<{ variantId: number; embedding: Uint8Array }>
|
|
48
|
-
): number => {
|
|
49
|
-
let inserted = 0;
|
|
50
|
-
for (const row of rows) {
|
|
51
|
-
db.run(
|
|
52
|
-
`INSERT INTO ${tableName(partition)}(variant_id, embedding) VALUES (?, ?)`,
|
|
53
|
-
[row.variantId, row.embedding]
|
|
54
|
-
);
|
|
55
|
-
inserted += 1;
|
|
56
|
-
}
|
|
57
|
-
return inserted;
|
|
58
|
-
};
|
|
111
|
+
const logicalChanges = (db: Database): number =>
|
|
112
|
+
db.query<{ changes: number }, []>("SELECT changes() AS changes").get()
|
|
113
|
+
?.changes ?? 0;
|
|
59
114
|
|
|
60
115
|
/**
|
|
61
116
|
* Remove index rows without a matching variant (orphans and stale
|
|
62
117
|
* embeddings), then add the variants the index lacks. Requires sqlite-vec.
|
|
63
118
|
*/
|
|
64
|
-
export function
|
|
119
|
+
export function syncPartitionIndexWithDetails(
|
|
65
120
|
db: Database,
|
|
66
121
|
partition: StoredPartition
|
|
67
|
-
): {
|
|
122
|
+
): {
|
|
123
|
+
added: number;
|
|
124
|
+
removed: number;
|
|
125
|
+
v2Added?: number;
|
|
126
|
+
v2Removed?: number;
|
|
127
|
+
checkRecall?: number | null;
|
|
128
|
+
} {
|
|
68
129
|
const table = tableName(partition);
|
|
69
130
|
return db
|
|
70
131
|
.transaction(() => {
|
|
71
132
|
createTable(db, partition);
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
133
|
+
db.query(
|
|
134
|
+
`DELETE FROM ${table}
|
|
135
|
+
WHERE variant_id IN (
|
|
136
|
+
SELECT x.variant_id FROM ${table} x
|
|
137
|
+
LEFT JOIN vector_variants v ON v.variant_id = x.variant_id
|
|
138
|
+
AND v.partition_id = ?
|
|
139
|
+
WHERE v.variant_id IS NULL OR x.embedding != v.embedding
|
|
140
|
+
)`
|
|
141
|
+
).run(partition.partitionId);
|
|
142
|
+
const removed = logicalChanges(db);
|
|
143
|
+
db.query(
|
|
144
|
+
`INSERT INTO ${table}(variant_id, embedding)
|
|
145
|
+
SELECT v.variant_id, v.embedding FROM vector_variants v
|
|
146
|
+
LEFT JOIN ${table} x ON x.variant_id = v.variant_id
|
|
147
|
+
WHERE v.partition_id = ? AND x.variant_id IS NULL`
|
|
148
|
+
).run(partition.partitionId);
|
|
149
|
+
const added = logicalChanges(db);
|
|
150
|
+
const nullCosine: 0 | 1 = probePartitionNullCosine(
|
|
151
|
+
db,
|
|
152
|
+
partition.partitionId
|
|
153
|
+
)
|
|
154
|
+
? 1
|
|
155
|
+
: 0;
|
|
156
|
+
markMirrorClean(db, partition.partitionId, nullCosine);
|
|
157
|
+
const v2 = syncV2PartitionMirrorInTx(
|
|
158
|
+
db,
|
|
159
|
+
partition.partitionId,
|
|
160
|
+
partition.dimensions
|
|
161
|
+
);
|
|
162
|
+
if (v2.skipped) {
|
|
163
|
+
return { added, removed };
|
|
82
164
|
}
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
.
|
|
90
|
-
|
|
91
|
-
markMirrorClean(db, partition.partitionId);
|
|
92
|
-
return { added, removed: stale.length };
|
|
165
|
+
const check = runV2RecallSelfCheck(db, partition);
|
|
166
|
+
return {
|
|
167
|
+
added,
|
|
168
|
+
removed,
|
|
169
|
+
v2Added: v2.added,
|
|
170
|
+
v2Removed: v2.removed,
|
|
171
|
+
checkRecall: check.recall,
|
|
172
|
+
};
|
|
93
173
|
})
|
|
94
174
|
.immediate();
|
|
95
175
|
}
|
|
96
176
|
|
|
97
|
-
|
|
98
|
-
export function rebuildPartitionIndex(
|
|
177
|
+
export function syncPartitionIndex(
|
|
99
178
|
db: Database,
|
|
100
179
|
partition: StoredPartition
|
|
101
|
-
): number {
|
|
180
|
+
): { added: number; removed: number } {
|
|
181
|
+
const { added, removed } = syncPartitionIndexWithDetails(db, partition);
|
|
182
|
+
return { added, removed };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Drop and repopulate the v1 and v2 mirrors from the partition's variants. */
|
|
186
|
+
export function rebuildPartitionIndexWithDetails(
|
|
187
|
+
db: Database,
|
|
188
|
+
partition: StoredPartition
|
|
189
|
+
): { count: number; v2Count?: number; checkRecall?: number | null } {
|
|
190
|
+
const table = tableName(partition);
|
|
102
191
|
return db
|
|
103
192
|
.transaction(() => {
|
|
104
|
-
db.exec(`DROP TABLE IF EXISTS ${
|
|
193
|
+
db.exec(`DROP TABLE IF EXISTS ${table}`);
|
|
105
194
|
createTable(db, partition);
|
|
106
|
-
|
|
195
|
+
db.query(
|
|
196
|
+
`INSERT INTO ${table}(variant_id, embedding)
|
|
197
|
+
SELECT variant_id, embedding FROM vector_variants WHERE partition_id = ?`
|
|
198
|
+
).run(partition.partitionId);
|
|
199
|
+
const inserted = logicalChanges(db);
|
|
200
|
+
const nullCosine: 0 | 1 = probePartitionNullCosine(
|
|
107
201
|
db,
|
|
108
|
-
partition
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
202
|
+
partition.partitionId
|
|
203
|
+
)
|
|
204
|
+
? 1
|
|
205
|
+
: 0;
|
|
206
|
+
markMirrorClean(db, partition.partitionId, nullCosine);
|
|
207
|
+
if (partition.dimensions % 8 !== 0) {
|
|
208
|
+
return { count: inserted };
|
|
209
|
+
}
|
|
210
|
+
const v2Count = rebuildV2PartitionMirrorInTx(
|
|
211
|
+
db,
|
|
212
|
+
partition.partitionId,
|
|
213
|
+
partition.dimensions
|
|
114
214
|
);
|
|
115
|
-
|
|
116
|
-
return inserted;
|
|
215
|
+
const check = runV2RecallSelfCheck(db, partition);
|
|
216
|
+
return { count: inserted, v2Count, checkRecall: check.recall };
|
|
117
217
|
})
|
|
118
218
|
.immediate();
|
|
119
219
|
}
|
|
220
|
+
|
|
221
|
+
/** Drop and repopulate the index from the partition's variants. */
|
|
222
|
+
export function rebuildPartitionIndex(
|
|
223
|
+
db: Database,
|
|
224
|
+
partition: StoredPartition
|
|
225
|
+
): number {
|
|
226
|
+
return rebuildPartitionIndexWithDetails(db, partition).count;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export type PruneLegacyVectorsResult =
|
|
230
|
+
| {
|
|
231
|
+
ok: true;
|
|
232
|
+
droppedTables: string[];
|
|
233
|
+
removedRows: number;
|
|
234
|
+
activatedModels: string[];
|
|
235
|
+
}
|
|
236
|
+
| { ok: false; error: string };
|
|
237
|
+
|
|
238
|
+
const VEC_V1_TABLE_PATTERN = /^vec_v1_([0-9a-f]{64})$/;
|
|
239
|
+
const VEC_V2_TABLE_PATTERN = /^vec_v2_([0-9a-f]{64})$/;
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Safely prune superseded legacy vector tables (`vec_<hash>`), legacy
|
|
243
|
+
* `content_vectors` rows for models with an activated v1 partition, and
|
|
244
|
+
* orphaned `vec_v1_<partition>` tables absent from `vector_partitions`.
|
|
245
|
+
*
|
|
246
|
+
* Requires sqlite-vec (DROP TABLE on a vec0 virtual table fails with
|
|
247
|
+
* `no such module: vec0` without the extension) and a writable connection.
|
|
248
|
+
*/
|
|
249
|
+
export async function pruneLegacyVectors(
|
|
250
|
+
db: Database,
|
|
251
|
+
options: { loadSqliteVec?: (db: Database) => Promise<boolean> } = {}
|
|
252
|
+
): Promise<PruneLegacyVectorsResult> {
|
|
253
|
+
const queryOnly =
|
|
254
|
+
db.query<{ query_only: number }, []>("PRAGMA query_only").get()
|
|
255
|
+
?.query_only === 1;
|
|
256
|
+
if (queryOnly) {
|
|
257
|
+
return {
|
|
258
|
+
ok: false,
|
|
259
|
+
error: "Cannot prune legacy vectors on a read-only database.",
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
let sqliteVecLoaded = false;
|
|
264
|
+
if (options.loadSqliteVec) {
|
|
265
|
+
sqliteVecLoaded = await options.loadSqliteVec(db);
|
|
266
|
+
} else {
|
|
267
|
+
try {
|
|
268
|
+
const sqliteVec = await import("sqlite-vec");
|
|
269
|
+
sqliteVec.load(db);
|
|
270
|
+
sqliteVecLoaded = true;
|
|
271
|
+
} catch {
|
|
272
|
+
sqliteVecLoaded = false;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (!sqliteVecLoaded) {
|
|
276
|
+
return {
|
|
277
|
+
ok: false,
|
|
278
|
+
error:
|
|
279
|
+
"sqlite-vec is unavailable; cannot drop legacy or orphaned vector tables.",
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
try {
|
|
284
|
+
return db
|
|
285
|
+
.transaction((): PruneLegacyVectorsResult => {
|
|
286
|
+
const hasPartitionsTable = Boolean(
|
|
287
|
+
db
|
|
288
|
+
.query(
|
|
289
|
+
"SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'vector_partitions'"
|
|
290
|
+
)
|
|
291
|
+
.get()
|
|
292
|
+
);
|
|
293
|
+
if (!hasPartitionsTable) {
|
|
294
|
+
return {
|
|
295
|
+
ok: true,
|
|
296
|
+
droppedTables: [],
|
|
297
|
+
removedRows: 0,
|
|
298
|
+
activatedModels: [],
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const activatedModels = db
|
|
303
|
+
.query<{ model: string }, []>(
|
|
304
|
+
`SELECT DISTINCT model
|
|
305
|
+
FROM vector_partitions
|
|
306
|
+
WHERE state = 'active' AND activated_epoch IS NOT NULL
|
|
307
|
+
ORDER BY model`
|
|
308
|
+
)
|
|
309
|
+
.all()
|
|
310
|
+
.map((row) => row.model);
|
|
311
|
+
|
|
312
|
+
const knownPartitionIds = new Set(
|
|
313
|
+
db
|
|
314
|
+
.query<{ partition_id: string }, []>(
|
|
315
|
+
"SELECT partition_id FROM vector_partitions"
|
|
316
|
+
)
|
|
317
|
+
.all()
|
|
318
|
+
.map((row) => row.partition_id)
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
const allTables = db
|
|
322
|
+
.query<{ name: string }, []>(
|
|
323
|
+
"SELECT name FROM sqlite_master WHERE type = 'table'"
|
|
324
|
+
)
|
|
325
|
+
.all()
|
|
326
|
+
.map((row) => row.name);
|
|
327
|
+
const tableSet = new Set(allTables);
|
|
328
|
+
|
|
329
|
+
const droppedTables: string[] = [];
|
|
330
|
+
let removedRows = 0;
|
|
331
|
+
|
|
332
|
+
// 1. Drop legacy per-model vec_<hash> tables and delete legacy
|
|
333
|
+
// content_vectors rows ONLY for models with an activated partition.
|
|
334
|
+
for (const model of activatedModels) {
|
|
335
|
+
const legacyTable = modelTableName(model);
|
|
336
|
+
if (tableSet.has(legacyTable)) {
|
|
337
|
+
db.exec(`DROP TABLE IF EXISTS "${legacyTable}"`);
|
|
338
|
+
droppedTables.push(legacyTable);
|
|
339
|
+
}
|
|
340
|
+
if (tableSet.has("content_vectors")) {
|
|
341
|
+
db.query("DELETE FROM content_vectors WHERE model = ?").run(model);
|
|
342
|
+
removedRows += logicalChanges(db);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// 2. Drop orphaned vec_v1_<partition> and vec_v2_<partition> tables absent from vector_partitions.
|
|
347
|
+
for (const name of allTables) {
|
|
348
|
+
const match =
|
|
349
|
+
VEC_V1_TABLE_PATTERN.exec(name) ?? VEC_V2_TABLE_PATTERN.exec(name);
|
|
350
|
+
if (!match) continue;
|
|
351
|
+
const partitionId = match[1]!;
|
|
352
|
+
if (!knownPartitionIds.has(partitionId)) {
|
|
353
|
+
db.exec(`DROP TABLE IF EXISTS "${name}"`);
|
|
354
|
+
droppedTables.push(name);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// 3. Remove orphaned rows from vector_mirror_state, vector_mirror_v2_state,
|
|
359
|
+
// and vector_partition_rows if present.
|
|
360
|
+
if (tableSet.has("vector_mirror_state")) {
|
|
361
|
+
db.run(
|
|
362
|
+
`DELETE FROM vector_mirror_state
|
|
363
|
+
WHERE partition_id NOT IN (SELECT partition_id FROM vector_partitions)`
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
if (tableSet.has("vector_mirror_v2_state")) {
|
|
367
|
+
db.run(
|
|
368
|
+
`DELETE FROM vector_mirror_v2_state
|
|
369
|
+
WHERE partition_id NOT IN (SELECT partition_id FROM vector_partitions)`
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
if (tableSet.has("vector_partition_rows")) {
|
|
373
|
+
db.run(
|
|
374
|
+
`DELETE FROM vector_partition_rows
|
|
375
|
+
WHERE partition_id NOT IN (SELECT partition_id FROM vector_partitions)`
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return {
|
|
380
|
+
ok: true,
|
|
381
|
+
droppedTables,
|
|
382
|
+
removedRows,
|
|
383
|
+
activatedModels,
|
|
384
|
+
};
|
|
385
|
+
})
|
|
386
|
+
.immediate();
|
|
387
|
+
} catch (cause) {
|
|
388
|
+
return {
|
|
389
|
+
ok: false,
|
|
390
|
+
error: `Vector prune failed: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-connection input-hash proof cache keyed on `vector_variant_epoch.epoch`,
|
|
3
|
+
* plus O(1) persisted null-cosine state lookup backed by `vector_mirror_state`.
|
|
4
|
+
*
|
|
5
|
+
* Every mutation to `documents`, `content_chunks`, `vector_owners`, or
|
|
6
|
+
* `vector_variants` increments `vector_variant_epoch.epoch` via migration 028
|
|
7
|
+
* triggers, so proofs survive unrelated writes (`retrieval_traces`,
|
|
8
|
+
* `llm_cache`, `egress_audit_receipts`, `vector_runtime_callers`) while still
|
|
9
|
+
* invalidating across connections whenever document titles or chunk text change.
|
|
10
|
+
*
|
|
11
|
+
* When a caller is already inside an uncommitted outer transaction
|
|
12
|
+
* (`inOuterTransaction === true`), a call-local Map is returned so a later
|
|
13
|
+
* `ROLLBACK` can never poison the connection-shared cache.
|
|
14
|
+
*
|
|
15
|
+
* @module src/store/vector/proof-cache
|
|
16
|
+
*/
|
|
17
|
+
import type { Database } from "bun:sqlite";
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
hasNullCosineColumn,
|
|
21
|
+
probePartitionNullCosine,
|
|
22
|
+
} from "./partition-index";
|
|
23
|
+
|
|
24
|
+
const MAX_CACHED_PROOFS = 100_000;
|
|
25
|
+
|
|
26
|
+
const proofCaches = new WeakMap<
|
|
27
|
+
Database,
|
|
28
|
+
{
|
|
29
|
+
epoch: number;
|
|
30
|
+
proofs: Map<string, boolean>;
|
|
31
|
+
nullCosinePartitions: Map<string, boolean>;
|
|
32
|
+
}
|
|
33
|
+
>();
|
|
34
|
+
|
|
35
|
+
export function vectorProofKey(
|
|
36
|
+
model: string,
|
|
37
|
+
documentId: number,
|
|
38
|
+
mirrorHash: string,
|
|
39
|
+
seq: number,
|
|
40
|
+
inputHash: string
|
|
41
|
+
): string {
|
|
42
|
+
return `${model}\0${documentId}\0${mirrorHash}\0${seq}\0${inputHash}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function readVariantEpoch(db: Database): number | null {
|
|
46
|
+
const row = db
|
|
47
|
+
.query<{ epoch: number }, []>(
|
|
48
|
+
"SELECT epoch FROM vector_variant_epoch WHERE id = 1"
|
|
49
|
+
)
|
|
50
|
+
.get();
|
|
51
|
+
return row ? row.epoch : null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function getEpochCache(
|
|
55
|
+
db: Database,
|
|
56
|
+
inOuterTransaction: boolean
|
|
57
|
+
): {
|
|
58
|
+
epoch: number;
|
|
59
|
+
proofs: Map<string, boolean>;
|
|
60
|
+
nullCosinePartitions: Map<string, boolean>;
|
|
61
|
+
} | null {
|
|
62
|
+
if (inOuterTransaction) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
const epoch = readVariantEpoch(db);
|
|
66
|
+
if (epoch === null) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
let cache = proofCaches.get(db);
|
|
70
|
+
if (cache?.epoch !== epoch || cache.proofs.size > MAX_CACHED_PROOFS) {
|
|
71
|
+
cache = {
|
|
72
|
+
epoch,
|
|
73
|
+
proofs: new Map(),
|
|
74
|
+
nullCosinePartitions: new Map(),
|
|
75
|
+
};
|
|
76
|
+
proofCaches.set(db, cache);
|
|
77
|
+
}
|
|
78
|
+
return cache;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function provenOwners(
|
|
82
|
+
db: Database,
|
|
83
|
+
inOuterTransaction: boolean
|
|
84
|
+
): Map<string, boolean> {
|
|
85
|
+
return getEpochCache(db, inOuterTransaction)?.proofs ?? new Map();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface NullCosineCheckResult {
|
|
89
|
+
found: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Non-null when this call had to scan `vector_variants` because
|
|
92
|
+
* `vector_mirror_state.null_cosine` was NULL, carrying the variant epoch
|
|
93
|
+
* observed inside the read snapshot for a guarded post-snapshot persist.
|
|
94
|
+
*/
|
|
95
|
+
persistAfterSnapshot?: {
|
|
96
|
+
partitionId: string;
|
|
97
|
+
value: 0 | 1;
|
|
98
|
+
epoch: number;
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Inspect whether `partitionId` contains any null-cosine variant.
|
|
104
|
+
*
|
|
105
|
+
* Checks persisted `vector_mirror_state.null_cosine` first (O(1) PK lookup,
|
|
106
|
+
* surviving cold CLI starts and unrelated document/chunk edits), then the
|
|
107
|
+
* connection-local epoch cache, and only falls back to the `LIMIT 1` probe
|
|
108
|
+
* when unknown (`NULL`). Never writes inside the read snapshot.
|
|
109
|
+
*/
|
|
110
|
+
export function checkNullCosineVariant(
|
|
111
|
+
db: Database,
|
|
112
|
+
partitionId: string,
|
|
113
|
+
inOuterTransaction: boolean
|
|
114
|
+
): NullCosineCheckResult {
|
|
115
|
+
let mirrorCleanWithUnknown = false;
|
|
116
|
+
if (hasNullCosineColumn(db)) {
|
|
117
|
+
const persisted = db
|
|
118
|
+
.query<{ dirty: number; null_cosine: number | null }, [string]>(
|
|
119
|
+
"SELECT dirty, null_cosine FROM vector_mirror_state WHERE partition_id = ?"
|
|
120
|
+
)
|
|
121
|
+
.get(partitionId);
|
|
122
|
+
if (persisted?.dirty === 0) {
|
|
123
|
+
if (persisted.null_cosine === 0 || persisted.null_cosine === 1) {
|
|
124
|
+
return { found: persisted.null_cosine === 1 };
|
|
125
|
+
}
|
|
126
|
+
mirrorCleanWithUnknown = true;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const cache = getEpochCache(db, inOuterTransaction);
|
|
131
|
+
const cached = cache?.nullCosinePartitions.get(partitionId);
|
|
132
|
+
if (cached !== undefined) {
|
|
133
|
+
return {
|
|
134
|
+
found: cached,
|
|
135
|
+
...(mirrorCleanWithUnknown && cache
|
|
136
|
+
? {
|
|
137
|
+
persistAfterSnapshot: {
|
|
138
|
+
partitionId,
|
|
139
|
+
value: cached ? 1 : 0,
|
|
140
|
+
epoch: cache.epoch,
|
|
141
|
+
},
|
|
142
|
+
}
|
|
143
|
+
: {}),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const found = probePartitionNullCosine(db, partitionId);
|
|
148
|
+
cache?.nullCosinePartitions.set(partitionId, found);
|
|
149
|
+
const epoch = cache?.epoch ?? readVariantEpoch(db);
|
|
150
|
+
return {
|
|
151
|
+
found,
|
|
152
|
+
...(mirrorCleanWithUnknown && !inOuterTransaction && epoch !== null
|
|
153
|
+
? {
|
|
154
|
+
persistAfterSnapshot: {
|
|
155
|
+
partitionId,
|
|
156
|
+
value: found ? 1 : 0,
|
|
157
|
+
epoch,
|
|
158
|
+
},
|
|
159
|
+
}
|
|
160
|
+
: {}),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* True if the partition contains any variant whose cosine distance can be NULL
|
|
166
|
+
* (e.g. an all-zero vector or float32 norm underflow/overflow).
|
|
167
|
+
*/
|
|
168
|
+
export function hasNullCosineVariant(
|
|
169
|
+
db: Database,
|
|
170
|
+
partitionId: string,
|
|
171
|
+
inOuterTransaction: boolean
|
|
172
|
+
): boolean {
|
|
173
|
+
return checkNullCosineVariant(db, partitionId, inOuterTransaction).found;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Best-effort guarded persist of a probed `null_cosine` flag after the search's
|
|
178
|
+
* read snapshot closes. Skips on query-only connections, active transactions,
|
|
179
|
+
* changed variant epoch, dirty mirrors, or lock contention.
|
|
180
|
+
*/
|
|
181
|
+
export function persistNullCosineAfterSnapshot(
|
|
182
|
+
writerDb: Database,
|
|
183
|
+
pending: NonNullable<NullCosineCheckResult["persistAfterSnapshot"]>
|
|
184
|
+
): boolean {
|
|
185
|
+
if (writerDb.inTransaction) return false;
|
|
186
|
+
try {
|
|
187
|
+
const queryOnly =
|
|
188
|
+
writerDb.query<{ query_only: number }, []>("PRAGMA query_only").get()
|
|
189
|
+
?.query_only === 1;
|
|
190
|
+
if (queryOnly) return false;
|
|
191
|
+
if (!hasNullCosineColumn(writerDb)) return false;
|
|
192
|
+
writerDb.run(
|
|
193
|
+
`UPDATE vector_mirror_state
|
|
194
|
+
SET null_cosine = ?
|
|
195
|
+
WHERE partition_id = ?
|
|
196
|
+
AND dirty = 0
|
|
197
|
+
AND null_cosine IS NULL
|
|
198
|
+
AND (SELECT epoch FROM vector_variant_epoch WHERE id = 1) = ?`,
|
|
199
|
+
[pending.value, pending.partitionId, pending.epoch]
|
|
200
|
+
);
|
|
201
|
+
return (
|
|
202
|
+
(writerDb.query<{ c: number }, []>("SELECT changes() AS c").get()?.c ??
|
|
203
|
+
0) > 0
|
|
204
|
+
);
|
|
205
|
+
} catch {
|
|
206
|
+
// Busy/locked writer or read-only file: skip silently, probe cache stays valid.
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
@@ -18,6 +18,10 @@ import { getVariantModelFingerprint } from "../../embed/fingerprint";
|
|
|
18
18
|
import { runtimeCallerKey } from "../../llm/native-worker/embedding-identity";
|
|
19
19
|
import { formatDocForEmbedding } from "../../pipeline/contextual";
|
|
20
20
|
import { decodeEmbedding } from "./sqlite-vec";
|
|
21
|
+
import {
|
|
22
|
+
dropV2PartitionMirrorInTx,
|
|
23
|
+
rebuildV2PartitionMirrorInTx,
|
|
24
|
+
} from "./v2-mirror";
|
|
21
25
|
import {
|
|
22
26
|
embeddingInputHash,
|
|
23
27
|
loadSqliteVec,
|
|
@@ -373,6 +377,8 @@ async function rekeyLegacy(
|
|
|
373
377
|
[newId]
|
|
374
378
|
);
|
|
375
379
|
db.exec(`DROP TABLE IF EXISTS vec_v1_${oldId}`);
|
|
380
|
+
dropV2PartitionMirrorInTx(db, oldId);
|
|
381
|
+
rebuildV2PartitionMirrorInTx(db, newId, primary.dimensions);
|
|
376
382
|
}).immediate();
|
|
377
383
|
return { kind: "rekeyed", msPerChunk };
|
|
378
384
|
}
|