@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
|
@@ -66,19 +66,24 @@ export function slugifySectionTitle(title: string): string {
|
|
|
66
66
|
|
|
67
67
|
export const lineStartOffsets = (content: string): number[] => {
|
|
68
68
|
const offsets = [0];
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
let pos = content.indexOf("\n");
|
|
70
|
+
while (pos !== -1) {
|
|
71
|
+
offsets.push(pos + 1);
|
|
72
|
+
pos = content.indexOf("\n", pos + 1);
|
|
71
73
|
}
|
|
72
74
|
return offsets;
|
|
73
75
|
};
|
|
74
76
|
|
|
75
77
|
/** Extract structural section records (no quote evidence). */
|
|
76
|
-
export function extractSectionRecords(
|
|
78
|
+
export function extractSectionRecords(
|
|
79
|
+
content: string,
|
|
80
|
+
precomputedStarts?: readonly number[]
|
|
81
|
+
): SectionRecord[] {
|
|
77
82
|
const records: SectionRecord[] = [];
|
|
78
83
|
const counts = new Map<string, number>();
|
|
79
84
|
const pathCounts = new Map<string, number>();
|
|
80
85
|
const lines = content.split("\n");
|
|
81
|
-
const starts = lineStartOffsets(content);
|
|
86
|
+
const starts = precomputedStarts ?? lineStartOffsets(content);
|
|
82
87
|
let openFence: OpenFence | null = null;
|
|
83
88
|
const stack: { level: number; title: string }[] = [];
|
|
84
89
|
|
|
@@ -149,8 +154,13 @@ export function extractSectionRecords(content: string): SectionRecord[] {
|
|
|
149
154
|
return records;
|
|
150
155
|
}
|
|
151
156
|
|
|
152
|
-
export function extractSections(
|
|
153
|
-
|
|
157
|
+
export function extractSections(
|
|
158
|
+
content: string,
|
|
159
|
+
precomputedStarts?: readonly number[]
|
|
160
|
+
): DocumentSection[] {
|
|
161
|
+
return extractSectionRecords(content, precomputedStarts).map(
|
|
162
|
+
(record) => record.section
|
|
163
|
+
);
|
|
154
164
|
}
|
|
155
165
|
|
|
156
166
|
/** Extract one inclusive, 1-based line range without normalizing source bytes. */
|
|
@@ -70,6 +70,74 @@ export const typedMetadataSchema = z
|
|
|
70
70
|
)
|
|
71
71
|
);
|
|
72
72
|
|
|
73
|
+
function isValidScalar(value: unknown): value is MetadataScalar {
|
|
74
|
+
if (typeof value === "string") {
|
|
75
|
+
return value.length <= METADATA_LIMITS.stringLength;
|
|
76
|
+
}
|
|
77
|
+
if (typeof value === "number") {
|
|
78
|
+
return Number.isFinite(value);
|
|
79
|
+
}
|
|
80
|
+
return typeof value === "boolean";
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Direct validator for stored `documents.typed_metadata` JSON strings.
|
|
85
|
+
* Avoids `JSON.stringify` + `TextEncoder` when raw UTF-8 length plus the
|
|
86
|
+
* worst-case numeric re-serialization bound (24 bytes per finite JS number)
|
|
87
|
+
* proves the canonical JSON fits within 64 KiB; otherwise checks exact
|
|
88
|
+
* canonical `JSON.stringify(parsed)` byte length.
|
|
89
|
+
*/
|
|
90
|
+
const MAX_JSON_NUMBER_BYTES = 24;
|
|
91
|
+
|
|
92
|
+
export function parseStoredTypedMetadata(raw: string): TypedMetadata {
|
|
93
|
+
const parsed: unknown = JSON.parse(raw);
|
|
94
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
95
|
+
throw new Error("Invalid typed metadata: expected object");
|
|
96
|
+
}
|
|
97
|
+
const entries = Object.entries(parsed);
|
|
98
|
+
if (entries.length > METADATA_LIMITS.keys) {
|
|
99
|
+
throw new Error("Too many metadata keys");
|
|
100
|
+
}
|
|
101
|
+
let numberCount = 0;
|
|
102
|
+
for (const [key, value] of entries) {
|
|
103
|
+
if (key.length < 1 || key.length > METADATA_LIMITS.keyLength) {
|
|
104
|
+
throw new Error("Invalid metadata key length");
|
|
105
|
+
}
|
|
106
|
+
if (reserved.has(key)) {
|
|
107
|
+
throw new Error("Reserved metadata key");
|
|
108
|
+
}
|
|
109
|
+
if (Array.isArray(value)) {
|
|
110
|
+
if (value.length > METADATA_LIMITS.members) {
|
|
111
|
+
throw new Error("Too many metadata array members");
|
|
112
|
+
}
|
|
113
|
+
const firstType = value.length > 0 ? typeof value[0] : null;
|
|
114
|
+
for (const member of value) {
|
|
115
|
+
if (!isValidScalar(member)) {
|
|
116
|
+
throw new Error("Invalid metadata array member");
|
|
117
|
+
}
|
|
118
|
+
if (typeof member !== firstType) {
|
|
119
|
+
throw new Error("Array members must have one scalar type");
|
|
120
|
+
}
|
|
121
|
+
if (typeof member === "number") {
|
|
122
|
+
numberCount++;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
} else if (!isValidScalar(value)) {
|
|
126
|
+
throw new Error("Invalid metadata value");
|
|
127
|
+
} else if (typeof value === "number") {
|
|
128
|
+
numberCount++;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const rawBytes = Buffer.byteLength(raw, "utf8");
|
|
132
|
+
if (
|
|
133
|
+
rawBytes + numberCount * MAX_JSON_NUMBER_BYTES > METADATA_LIMITS.bytes &&
|
|
134
|
+
Buffer.byteLength(JSON.stringify(parsed), "utf8") > METADATA_LIMITS.bytes
|
|
135
|
+
) {
|
|
136
|
+
throw new Error("Metadata exceeds 64 KiB");
|
|
137
|
+
}
|
|
138
|
+
return parsed as TypedMetadata;
|
|
139
|
+
}
|
|
140
|
+
|
|
73
141
|
const leafSchemas = [
|
|
74
142
|
z
|
|
75
143
|
.object({ op: z.enum(["eq", "ne"]), key: keySchema, value: scalarSchema })
|
|
@@ -47,6 +47,19 @@ export function formatVectorPartitionLines(
|
|
|
47
47
|
lines.push(` read by: ${p.compatibleRuntimes.join("; ")}`);
|
|
48
48
|
if (p.incompatibleRuntimes.length)
|
|
49
49
|
lines.push(` incompatible: ${p.incompatibleRuntimes.join("; ")}`);
|
|
50
|
+
if (p.coarseMirror && p.coarseMirror.state !== "unsupported") {
|
|
51
|
+
const recallPart =
|
|
52
|
+
p.coarseMirror.checkRecall !== null
|
|
53
|
+
? `, recall@10=${(p.coarseMirror.checkRecall * 100).toFixed(1)}% (${p.coarseMirror.checkQueries ?? 0}q, coarseK=${p.coarseMirror.checkCoarseK ?? 200})${p.coarseMirror.belowFloor ? " [below 95% floor]" : ""}`
|
|
54
|
+
: ", self-check=not run";
|
|
55
|
+
const repairHint =
|
|
56
|
+
p.coarseMirror.state === "dirty" || p.coarseMirror.state === "missing"
|
|
57
|
+
? " (repair with: gno vec sync)"
|
|
58
|
+
: "";
|
|
59
|
+
lines.push(
|
|
60
|
+
` coarse mirror: ${p.coarseMirror.state}${recallPart}${repairHint}`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
50
63
|
}
|
|
51
64
|
return lines;
|
|
52
65
|
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-dependency pure wiki-link string helpers.
|
|
3
|
+
* Extracted from src/core/links so browser bundles (MarkdownPreview) and
|
|
4
|
+
* lightweight callers do not pull in node:path or markdown parsing modules.
|
|
5
|
+
*
|
|
6
|
+
* @module src/core/wiki-link-syntax
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export interface TargetParts {
|
|
10
|
+
/** Reference (name or path) without anchor */
|
|
11
|
+
ref: string;
|
|
12
|
+
/** Anchor/fragment without # */
|
|
13
|
+
anchor?: string;
|
|
14
|
+
/** Collection prefix */
|
|
15
|
+
collection?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Collection prefix pattern: collection:rest */
|
|
19
|
+
const COLLECTION_PREFIX_REGEX = /^([a-z0-9_-]+):(.+)$/i;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Normalize wiki name: NFC + lowercase + trim.
|
|
23
|
+
* Used for matching wiki links to document titles.
|
|
24
|
+
*/
|
|
25
|
+
export function normalizeWikiName(name: string): string {
|
|
26
|
+
return name.normalize("NFC").toLowerCase().trim();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Strip a trailing .md extension (case-insensitive) without lowercasing.
|
|
31
|
+
*/
|
|
32
|
+
export function stripWikiMdExt(ref: string): string {
|
|
33
|
+
const lower = ref.toLowerCase();
|
|
34
|
+
return lower.endsWith(".md") ? ref.slice(0, -3) : ref;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Split the content of a wiki link into target and alias. Obsidian also reads
|
|
39
|
+
* `\|` (the pipe escaped inside a Markdown table) as the alias separator,
|
|
40
|
+
* so `[[Note\|Alias]]` targets `Note`, not `Note\`.
|
|
41
|
+
*/
|
|
42
|
+
export function splitWikiLinkContent(content: string): {
|
|
43
|
+
target: string;
|
|
44
|
+
alias?: string;
|
|
45
|
+
} {
|
|
46
|
+
const pipeIndex = content.indexOf("|");
|
|
47
|
+
if (pipeIndex < 0) {
|
|
48
|
+
return { target: content };
|
|
49
|
+
}
|
|
50
|
+
const escaped = pipeIndex > 0 && content[pipeIndex - 1] === "\\";
|
|
51
|
+
return {
|
|
52
|
+
target: content.slice(0, escaped ? pipeIndex - 1 : pipeIndex),
|
|
53
|
+
alias: content.slice(pipeIndex + 1),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Parse target into ref, anchor, and optional collection prefix.
|
|
59
|
+
* Handles: "Note", "Note#Section", "collection:Note#Section"
|
|
60
|
+
*/
|
|
61
|
+
export function parseTargetParts(target: string): TargetParts {
|
|
62
|
+
let remaining = target.trim();
|
|
63
|
+
let collection: string | undefined;
|
|
64
|
+
|
|
65
|
+
// Check for collection prefix (before any #)
|
|
66
|
+
const hashIndex = remaining.indexOf("#");
|
|
67
|
+
const textBeforeHash =
|
|
68
|
+
hashIndex >= 0 ? remaining.slice(0, hashIndex) : remaining;
|
|
69
|
+
|
|
70
|
+
const prefixMatch = COLLECTION_PREFIX_REGEX.exec(textBeforeHash);
|
|
71
|
+
if (prefixMatch?.[1] && prefixMatch[2]) {
|
|
72
|
+
collection = prefixMatch[1].toLowerCase(); // Normalize to lowercase for consistency
|
|
73
|
+
// Reconstruct remaining without the collection prefix
|
|
74
|
+
remaining =
|
|
75
|
+
prefixMatch[2] + (hashIndex >= 0 ? remaining.slice(hashIndex) : "");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Split on # for anchor
|
|
79
|
+
const parts = remaining.split("#");
|
|
80
|
+
const ref = (parts[0] ?? "").trim();
|
|
81
|
+
const anchor = parts[1]?.trim();
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
ref,
|
|
85
|
+
anchor: anchor && anchor.length > 0 ? anchor : undefined,
|
|
86
|
+
collection,
|
|
87
|
+
};
|
|
88
|
+
}
|
package/src/embed/backlog.ts
CHANGED
|
@@ -16,6 +16,7 @@ import type {
|
|
|
16
16
|
import type { VectorVariantStore } from "../store/vector/variants";
|
|
17
17
|
import type { AcquireWriteTurn } from "./retry";
|
|
18
18
|
|
|
19
|
+
import { isHttpModelUri } from "../llm/httpEmbedding";
|
|
19
20
|
import {
|
|
20
21
|
assertInferenceActive,
|
|
21
22
|
isBackgroundInference,
|
|
@@ -146,7 +147,7 @@ export async function embedBacklog(
|
|
|
146
147
|
const background = isBackgroundInference();
|
|
147
148
|
const batchSize = background
|
|
148
149
|
? Math.min(deps.batchSize ?? 32, 32)
|
|
149
|
-
: (deps.batchSize ??
|
|
150
|
+
: (deps.batchSize ?? 64);
|
|
150
151
|
const embedFingerprint = getEmbeddingFingerprint({
|
|
151
152
|
modelUri,
|
|
152
153
|
dimensions: vectorIndex.dimensions,
|
|
@@ -224,73 +225,154 @@ export async function embedBacklog(
|
|
|
224
225
|
};
|
|
225
226
|
|
|
226
227
|
try {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
// Get next batch using seek pagination
|
|
230
|
-
const batchResult = await statsPort.getBacklog(
|
|
231
|
-
modelUri,
|
|
232
|
-
embedFingerprint,
|
|
233
|
-
{
|
|
234
|
-
limit: batchSize,
|
|
235
|
-
after: cursor,
|
|
236
|
-
collection,
|
|
237
|
-
}
|
|
238
|
-
);
|
|
228
|
+
const httpConcurrency =
|
|
229
|
+
!background && !deps.acquireWriteTurn && isHttpModelUri(modelUri) ? 3 : 1;
|
|
239
230
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
231
|
+
if (httpConcurrency > 1) {
|
|
232
|
+
type InFlightBatch = Promise<
|
|
233
|
+
Awaited<ReturnType<typeof embedAndStoreBatch>>
|
|
234
|
+
>;
|
|
235
|
+
const inFlight: InFlightBatch[] = [];
|
|
236
|
+
let exhausted = false;
|
|
243
237
|
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
238
|
+
const fillHttpWindow = async (): Promise<
|
|
239
|
+
StoreResult<EmbedBacklogResult> | undefined
|
|
240
|
+
> => {
|
|
241
|
+
while (!exhausted && inFlight.length < httpConcurrency) {
|
|
242
|
+
assertInferenceActive();
|
|
243
|
+
const batchResult = await statsPort.getBacklog(
|
|
244
|
+
modelUri,
|
|
245
|
+
embedFingerprint,
|
|
246
|
+
{
|
|
247
|
+
limit: batchSize,
|
|
248
|
+
after: cursor,
|
|
249
|
+
collection,
|
|
250
|
+
}
|
|
251
|
+
);
|
|
252
|
+
if (!batchResult.ok) {
|
|
253
|
+
return err("QUERY_FAILED", batchResult.error.message);
|
|
254
|
+
}
|
|
255
|
+
const batch = batchResult.value;
|
|
256
|
+
if (batch.length === 0) {
|
|
257
|
+
exhausted = true;
|
|
258
|
+
return undefined;
|
|
259
|
+
}
|
|
260
|
+
const lastItem = batch.at(-1);
|
|
261
|
+
if (lastItem) {
|
|
262
|
+
cursor = { mirrorHash: lastItem.mirrorHash, seq: lastItem.seq };
|
|
263
|
+
}
|
|
264
|
+
inFlight.push(
|
|
265
|
+
embedAndStoreBatch({
|
|
266
|
+
embedPort,
|
|
267
|
+
vectorIndex,
|
|
268
|
+
items: batch,
|
|
269
|
+
modelUri,
|
|
270
|
+
embedFingerprint,
|
|
271
|
+
identityStillCurrent: deps.identityStillCurrent,
|
|
272
|
+
statsPort,
|
|
273
|
+
})
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
return undefined;
|
|
277
|
+
};
|
|
248
278
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
279
|
+
const fillErr = await fillHttpWindow();
|
|
280
|
+
if (fillErr) {
|
|
281
|
+
await Promise.allSettled(inFlight);
|
|
282
|
+
return fillErr;
|
|
253
283
|
}
|
|
254
284
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
const batchStoreResult = background
|
|
268
|
-
? await withInferencePage(storePage)
|
|
269
|
-
: await storePage();
|
|
270
|
-
if (!batchStoreResult) {
|
|
271
|
-
// Past its deadline: this page stays pending for the next pass.
|
|
272
|
-
errors += batch.length;
|
|
273
|
-
deps.onProgress?.(embedded, errors);
|
|
274
|
-
await Bun.sleep(0);
|
|
275
|
-
continue;
|
|
276
|
-
}
|
|
277
|
-
if (batchStoreResult.deferred)
|
|
278
|
-
return ok({ embedded, errors, contentionErrors, deferred: true });
|
|
279
|
-
embedded += batchStoreResult.embedded;
|
|
280
|
-
errors += batchStoreResult.errors;
|
|
281
|
-
contentionErrors += batchStoreResult.contentionErrors;
|
|
282
|
-
if (background) {
|
|
283
|
-
errors += batchStoreResult.retryItems.length;
|
|
285
|
+
while (inFlight.length > 0) {
|
|
286
|
+
const nextPromise = inFlight.shift()!;
|
|
287
|
+
const batchStoreResult = await nextPromise;
|
|
288
|
+
if (batchStoreResult.deferred) {
|
|
289
|
+
await Promise.allSettled(inFlight);
|
|
290
|
+
return ok({ embedded, errors, contentionErrors, deferred: true });
|
|
291
|
+
}
|
|
292
|
+
const beforeEmbedded = embedded;
|
|
293
|
+
embedded += batchStoreResult.embedded;
|
|
294
|
+
errors += batchStoreResult.errors;
|
|
295
|
+
contentionErrors += batchStoreResult.contentionErrors;
|
|
296
|
+
enqueueRetryItems(batchStoreResult.retryItems, 1);
|
|
284
297
|
deps.onProgress?.(embedded, errors);
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
298
|
+
if (embedded > beforeEmbedded && retryQueue.size > 0) {
|
|
299
|
+
await drainRetryQueue();
|
|
300
|
+
}
|
|
301
|
+
const nextErr = await fillHttpWindow();
|
|
302
|
+
if (nextErr) {
|
|
303
|
+
await Promise.allSettled(inFlight);
|
|
304
|
+
return nextErr;
|
|
305
|
+
}
|
|
289
306
|
}
|
|
290
|
-
|
|
307
|
+
} else {
|
|
308
|
+
while (true) {
|
|
309
|
+
assertInferenceActive();
|
|
310
|
+
// Get next batch using seek pagination
|
|
311
|
+
const batchResult = await statsPort.getBacklog(
|
|
312
|
+
modelUri,
|
|
313
|
+
embedFingerprint,
|
|
314
|
+
{
|
|
315
|
+
limit: batchSize,
|
|
316
|
+
after: cursor,
|
|
317
|
+
collection,
|
|
318
|
+
}
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
if (!batchResult.ok) {
|
|
322
|
+
return err("QUERY_FAILED", batchResult.error.message);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const batch = batchResult.value;
|
|
326
|
+
if (batch.length === 0) {
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
291
329
|
|
|
292
|
-
|
|
293
|
-
|
|
330
|
+
// Advance cursor (even on failure, to avoid infinite loops)
|
|
331
|
+
const lastItem = batch.at(-1);
|
|
332
|
+
if (lastItem) {
|
|
333
|
+
cursor = { mirrorHash: lastItem.mirrorHash, seq: lastItem.seq };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const beforeEmbedded = embedded;
|
|
337
|
+
const storePage = () =>
|
|
338
|
+
embedAndStoreBatch({
|
|
339
|
+
embedPort,
|
|
340
|
+
vectorIndex,
|
|
341
|
+
items: batch,
|
|
342
|
+
modelUri,
|
|
343
|
+
embedFingerprint,
|
|
344
|
+
identityStillCurrent: deps.identityStillCurrent,
|
|
345
|
+
statsPort,
|
|
346
|
+
acquireWriteTurn: deps.acquireWriteTurn,
|
|
347
|
+
});
|
|
348
|
+
const batchStoreResult = background
|
|
349
|
+
? await withInferencePage(storePage)
|
|
350
|
+
: await storePage();
|
|
351
|
+
if (!batchStoreResult) {
|
|
352
|
+
// Past its deadline: this page stays pending for the next pass.
|
|
353
|
+
errors += batch.length;
|
|
354
|
+
deps.onProgress?.(embedded, errors);
|
|
355
|
+
await Bun.sleep(0);
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
if (batchStoreResult.deferred)
|
|
359
|
+
return ok({ embedded, errors, contentionErrors, deferred: true });
|
|
360
|
+
embedded += batchStoreResult.embedded;
|
|
361
|
+
errors += batchStoreResult.errors;
|
|
362
|
+
contentionErrors += batchStoreResult.contentionErrors;
|
|
363
|
+
if (background) {
|
|
364
|
+
errors += batchStoreResult.retryItems.length;
|
|
365
|
+
deps.onProgress?.(embedded, errors);
|
|
366
|
+
// Each cursor page is a turn. Failed early pages cannot starve later work.
|
|
367
|
+
await Bun.sleep(0);
|
|
368
|
+
if (deps.identityStillCurrent && !deps.identityStillCurrent()) break;
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
enqueueRetryItems(batchStoreResult.retryItems, 1);
|
|
372
|
+
|
|
373
|
+
if (embedded > beforeEmbedded) {
|
|
374
|
+
await drainRetryQueue();
|
|
375
|
+
}
|
|
294
376
|
}
|
|
295
377
|
}
|
|
296
378
|
|
package/src/embed/retry.ts
CHANGED
|
@@ -213,13 +213,19 @@ export async function embedAndStoreBatch(params: {
|
|
|
213
213
|
errorSamples: ["Atomic vector checkpoint unavailable"],
|
|
214
214
|
batchFailed: false,
|
|
215
215
|
};
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
216
|
+
const formattedByKey = new Map<string, string>();
|
|
217
|
+
const formattedInputs = items.map((item) => {
|
|
218
|
+
const formatted = formatDocForEmbedding(
|
|
219
|
+
item.text,
|
|
220
|
+
item.title ?? undefined,
|
|
221
|
+
modelUri
|
|
222
|
+
);
|
|
223
|
+
formattedByKey.set(chunkRetryKey(item), formatted);
|
|
224
|
+
return formatted;
|
|
225
|
+
});
|
|
226
|
+
const embedResult = await embedTextsWithRecovery(embedPort, formattedInputs, {
|
|
227
|
+
onSubmit: params.onSubmit,
|
|
228
|
+
});
|
|
223
229
|
|
|
224
230
|
if (!embedResult.ok) {
|
|
225
231
|
const formattedError = formatLlmFailure(embedResult.error);
|
|
@@ -270,44 +276,44 @@ export async function embedAndStoreBatch(params: {
|
|
|
270
276
|
|
|
271
277
|
let written = 0;
|
|
272
278
|
let committedRows: VectorRow[] = [];
|
|
273
|
-
const
|
|
279
|
+
const hasPartitionsTableStmt = db
|
|
280
|
+
? db.query(
|
|
281
|
+
"SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'vector_partitions'"
|
|
282
|
+
)
|
|
283
|
+
: null;
|
|
284
|
+
let hasActivePartitionStmt: ReturnType<
|
|
285
|
+
NonNullable<typeof db>["query"]
|
|
286
|
+
> | null = null;
|
|
287
|
+
const selectCurrentChunkStmt = db
|
|
288
|
+
? db.query<{ text: string; title: string | null }, [string, number]>(`
|
|
289
|
+
SELECT c.text, d.title FROM content_chunks c
|
|
290
|
+
JOIN documents d ON d.mirror_hash = c.mirror_hash AND d.active = 1
|
|
291
|
+
WHERE c.mirror_hash = ? AND c.seq = ? ORDER BY d.id LIMIT 1
|
|
292
|
+
`)
|
|
293
|
+
: null;
|
|
274
294
|
const checkpoint = (rows: VectorRow[]): VectorRow[] => {
|
|
275
295
|
assertInferenceActive();
|
|
276
296
|
if (params.identityStillCurrent && !params.identityStillCurrent())
|
|
277
297
|
return [];
|
|
278
|
-
if (!db) return rows;
|
|
298
|
+
if (!db || !selectCurrentChunkStmt) return rows;
|
|
279
299
|
// A concurrently activated verified partition forbids a late legacy write.
|
|
280
|
-
if (
|
|
281
|
-
db
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
db
|
|
287
|
-
.query(
|
|
288
|
-
"SELECT 1 FROM vector_partitions WHERE model = ? AND state = 'active' AND activated_epoch IS NOT NULL LIMIT 1"
|
|
289
|
-
)
|
|
290
|
-
.get(modelUri)
|
|
291
|
-
)
|
|
292
|
-
return [];
|
|
300
|
+
if (hasPartitionsTableStmt?.get()) {
|
|
301
|
+
hasActivePartitionStmt ??= db.query<unknown, [string]>(
|
|
302
|
+
"SELECT 1 FROM vector_partitions WHERE model = ? AND state = 'active' AND activated_epoch IS NOT NULL LIMIT 1"
|
|
303
|
+
);
|
|
304
|
+
if (hasActivePartitionStmt.get(modelUri)) return [];
|
|
305
|
+
}
|
|
293
306
|
return rows.filter((row) => {
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
SELECT c.text, d.title FROM content_chunks c
|
|
298
|
-
JOIN documents d ON d.mirror_hash = c.mirror_hash AND d.active = 1
|
|
299
|
-
WHERE c.mirror_hash = ? AND c.seq = ? ORDER BY d.id LIMIT 1
|
|
300
|
-
`)
|
|
301
|
-
.get(row.mirrorHash, row.seq);
|
|
307
|
+
const expectedFormatted = formattedByKey.get(chunkRetryKey(row));
|
|
308
|
+
if (expectedFormatted === undefined) return false;
|
|
309
|
+
const current = selectCurrentChunkStmt.get(row.mirrorHash, row.seq);
|
|
302
310
|
return (
|
|
303
|
-
!!item &&
|
|
304
311
|
!!current &&
|
|
305
312
|
formatDocForEmbedding(
|
|
306
313
|
current.text,
|
|
307
314
|
current.title ?? undefined,
|
|
308
315
|
modelUri
|
|
309
|
-
) ===
|
|
310
|
-
formatDocForEmbedding(item.text, item.title ?? undefined, modelUri)
|
|
316
|
+
) === expectedFormatted
|
|
311
317
|
);
|
|
312
318
|
});
|
|
313
319
|
};
|