@gmickel/gno 2.14.0 → 2.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/assets/skill/cli-reference.md +4 -2
- package/assets/spa-production.json.gz +0 -0
- package/browser-extension/artifacts/{gno-browser-clipper-v2.14.0.zip → gno-browser-clipper-v2.16.0.zip} +0 -0
- package/browser-extension/artifacts/gno-browser-clipper-v2.16.0.zip.sha256 +1 -0
- package/browser-extension/dist/manifest.json +1 -1
- package/package.json +1 -1
- package/spec/cli.md +88 -45
- package/spec/db/schema.sql +45 -0
- package/spec/output-schemas/embed-receipt.schema.json +89 -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/embed.ts +341 -20
- package/src/cli/commands/links.ts +1 -1
- package/src/cli/commands/mcp/install.ts +2 -2
- package/src/cli/commands/mcp/status.ts +1 -1
- package/src/cli/commands/mcp/uninstall.ts +1 -1
- package/src/cli/commands/query.ts +1 -1
- package/src/cli/commands/shared.ts +4 -4
- package/src/cli/commands/skill/install.ts +1 -1
- package/src/cli/commands/skill/paths-cmd.ts +1 -1
- package/src/cli/commands/skill/uninstall.ts +1 -1
- package/src/cli/commands/status.ts +75 -58
- package/src/cli/config-cache.ts +86 -0
- package/src/cli/globals.ts +64 -0
- package/src/cli/options.ts +23 -0
- package/src/cli/program.ts +44 -37
- package/src/cli/run.ts +121 -67
- package/src/cli/runtime-health.ts +132 -122
- package/src/cli/session-binding.ts +11 -2
- package/src/config/saver.ts +8 -0
- package/src/converters/adapters/browser-export/formats.ts +25 -15
- package/src/converters/adapters/email/adapter.ts +37 -53
- package/src/converters/adapters/email/html.ts +37 -20
- package/src/converters/adapters/email/mime.ts +71 -39
- package/src/converters/adapters/ical/adapter.ts +20 -12
- package/src/converters/adapters/jsonl/adapter.ts +8 -6
- package/src/converters/adapters/shared/html-text.ts +55 -37
- package/src/converters/adapters/shared/ooxml-protection.ts +5 -13
- package/src/converters/adapters/shared/utf8-lines.ts +69 -31
- package/src/converters/adapters/transcript/adapter.ts +12 -7
- package/src/converters/adapters/xlsx/adapter.ts +10 -7
- package/src/converters/adapters/xlsx/sheet-markdown.ts +93 -50
- package/src/converters/canonicalize.ts +17 -3
- package/src/converters/native/markdown.ts +24 -17
- package/src/converters/native/plaintext.ts +5 -6
- package/src/converters/registry.ts +130 -8
- package/src/core/activation-status.ts +4 -8
- package/src/core/change-diff.ts +38 -11
- package/src/core/context-compiler.ts +17 -18
- package/src/core/context-evidence.ts +116 -25
- package/src/core/context-verifier.ts +70 -31
- package/src/core/file-lock.ts +10 -2
- package/src/core/graph-analysis.ts +3 -3
- package/src/core/link-inventory-markdown.ts +2 -2
- package/src/core/links.ts +20 -87
- package/src/core/memory-fence.ts +16 -9
- package/src/core/memory-recall.ts +168 -34
- package/src/core/path-rules.ts +143 -0
- package/src/core/section-parse.ts +16 -6
- package/src/core/typed-metadata.ts +68 -0
- package/src/core/wiki-link-syntax.ts +88 -0
- package/src/embed/backlog.ts +173 -60
- package/src/embed/batch.ts +83 -12
- package/src/embed/resume-cursor.ts +103 -0
- package/src/embed/retry.ts +41 -32
- package/src/embed/variant-backlog.ts +697 -63
- package/src/embed/variant-plan.ts +41 -45
- package/src/embed/variant-retry.ts +157 -44
- package/src/index.ts +3 -3
- package/src/ingestion/budget-error-codes.ts +13 -0
- package/src/ingestion/chunker.ts +60 -49
- package/src/ingestion/compiled-context.ts +4 -5
- package/src/ingestion/convert-file.ts +1 -1
- package/src/ingestion/document-metadata.ts +16 -9
- package/src/ingestion/excluded-ranges.ts +97 -0
- package/src/ingestion/file-processor.ts +122 -37
- package/src/ingestion/frontmatter.ts +35 -7
- package/src/ingestion/graph-reconciliation.ts +187 -51
- package/src/ingestion/language.ts +66 -61
- package/src/ingestion/position.ts +5 -5
- package/src/ingestion/prepare-file.ts +50 -29
- package/src/ingestion/record-adapter-canonical.ts +37 -26
- package/src/ingestion/record-adapter.ts +46 -26
- package/src/ingestion/record-container.ts +121 -53
- package/src/ingestion/record-path.ts +5 -2
- package/src/ingestion/source-availability/darwin-io.ts +10 -7
- package/src/ingestion/source-availability/darwin-path.ts +21 -3
- package/src/ingestion/source-availability/readers.ts +51 -0
- package/src/ingestion/strip.ts +39 -105
- package/src/ingestion/sync.ts +692 -386
- package/src/ingestion/typed-metadata.ts +10 -4
- package/src/ingestion/walker.ts +485 -158
- package/src/llm/cache.ts +128 -84
- package/src/llm/http-inference.ts +112 -35
- package/src/llm/http-policy.ts +101 -0
- package/src/llm/httpEmbedding.ts +8 -1
- package/src/llm/httpGeneration.ts +5 -1
- package/src/llm/httpRerank.ts +5 -1
- package/src/llm/inference-scope.ts +15 -2
- package/src/llm/lazy-ports.ts +33 -3
- package/src/llm/lockfile.ts +34 -7
- package/src/llm/model-cleanup.ts +24 -0
- package/src/llm/native-worker/client.ts +23 -12
- package/src/llm/native-worker/dispatcher.ts +19 -9
- package/src/llm/native-worker/embedding-identity.ts +42 -16
- package/src/llm/native-worker/entry.ts +2 -1
- package/src/llm/native-worker/owner.ts +1 -0
- package/src/llm/native-worker/ports.ts +86 -23
- package/src/llm/native-worker/protocol.ts +386 -55
- package/src/llm/nodeLlamaCpp/adapter.ts +46 -29
- package/src/llm/nodeLlamaCpp/embedding.ts +93 -40
- package/src/llm/nodeLlamaCpp/generation.ts +194 -30
- package/src/llm/nodeLlamaCpp/lifecycle.ts +2 -0
- package/src/llm/nodeLlamaCpp/rerank-capacity.ts +101 -23
- package/src/llm/nodeLlamaCpp/rerank.ts +253 -44
- package/src/llm/types.ts +14 -0
- package/src/mcp/context.ts +342 -23
- package/src/mcp/http-transport.ts +21 -3
- package/src/mcp/tool-profile-types.ts +62 -0
- package/src/mcp/tool-profile.ts +32 -56
- package/src/mcp/tools/context.ts +5 -1
- package/src/mcp/tools/index.ts +269 -191
- package/src/mcp/tools/links.ts +7 -5
- package/src/mcp/tools/query.ts +60 -37
- package/src/mcp/tools/vsearch.ts +25 -14
- package/src/pipeline/answer.ts +133 -40
- package/src/pipeline/claim-verification.ts +293 -189
- package/src/pipeline/claim-verifier.ts +6 -7
- package/src/pipeline/egress-lineage.ts +37 -26
- package/src/pipeline/exclude.ts +34 -11
- package/src/pipeline/expansion.ts +233 -19
- package/src/pipeline/filters.ts +169 -33
- package/src/pipeline/graph-retrieval.ts +117 -61
- package/src/pipeline/hybrid.ts +598 -329
- package/src/pipeline/hydration.ts +86 -24
- package/src/pipeline/intent.ts +15 -7
- package/src/pipeline/owner-fusion.ts +61 -19
- package/src/pipeline/query-language.ts +15 -2
- package/src/pipeline/rerank.ts +15 -22
- package/src/pipeline/search.ts +193 -72
- package/src/pipeline/snippet.ts +15 -2
- package/src/pipeline/temporal.ts +51 -10
- package/src/pipeline/vsearch.ts +250 -152
- package/src/publish/attachment-raster.ts +2 -16
- package/src/publish/encrypted-export.ts +15 -5
- package/src/serve/browse-tree.ts +50 -42
- package/src/serve/connectors.ts +2 -1
- package/src/serve/context.ts +13 -4
- package/src/serve/doc-events.ts +3 -0
- package/src/serve/public/app.tsx +49 -21
- package/src/serve/public/components/BrowseTreeSidebar.tsx +163 -23
- package/src/serve/public/components/CaptureModal.tsx +14 -10
- package/src/serve/public/components/ai-elements/code-block.tsx +87 -50
- package/src/serve/public/components/editor/MarkdownPreview.tsx +99 -67
- package/src/serve/public/globals.built.css +1 -1
- package/src/serve/public/globals.css +19 -0
- package/src/serve/public/hooks/useCaptureModal.tsx +23 -10
- package/src/serve/public/lib/metadata-filter.ts +234 -9
- package/src/serve/public/pages/Browse.tsx +61 -27
- package/src/serve/public/pages/DocView.tsx +44 -25
- package/src/serve/public/pages/GraphView.tsx +8 -3
- package/src/serve/resident-runtime.ts +199 -48
- package/src/serve/routes/api.ts +98 -67
- package/src/serve/routes/links.ts +18 -4
- package/src/serve/server.ts +1370 -1180
- package/src/serve/spa-bundle-source.ts +52 -27
- package/src/serve/status.ts +101 -52
- package/src/serve/watch-reconciliation-fallback.ts +11 -7
- package/src/serve/watch-reconciliation-shared.ts +42 -4
- package/src/serve/watch-reconciliation.ts +21 -8
- package/src/serve/watch-service-events.ts +7 -1
- package/src/serve/watch-service-hosts.ts +3 -1
- package/src/serve/watch-service.ts +46 -21
- package/src/serve/watch-snapshot-scan.ts +6 -15
- package/src/sessions/archive.ts +20 -8
- package/src/sessions/sanitize.ts +38 -18
- package/src/sessions/service.ts +180 -102
- package/src/store/migrations/036-vector-mirror-state.ts +83 -0
- package/src/store/migrations/037-performance-indexes.ts +56 -0
- package/src/store/migrations/index.ts +4 -0
- package/src/store/sqlite/adapter.ts +1217 -651
- package/src/store/sqlite/chunking-policy.ts +15 -1
- package/src/store/sqlite/eligibility.ts +110 -70
- package/src/store/sqlite/graph-neighbors.ts +71 -46
- package/src/store/sqlite/graph-reference-state.ts +27 -3
- package/src/store/sqlite/graph-similarity.ts +59 -12
- package/src/store/sqlite/metadata-predicate.ts +123 -28
- package/src/store/sqlite/retrieval-trace-management-store.ts +126 -33
- package/src/store/sqlite/retrieval-trace-retention.ts +88 -76
- package/src/store/sqlite/retrieval-trace-rows.ts +7 -3
- package/src/store/types.ts +32 -0
- package/src/store/vector/eligibility.ts +39 -0
- package/src/store/vector/lazy.ts +17 -0
- package/src/store/vector/partition-index.ts +49 -47
- package/src/store/vector/proof-cache.ts +103 -0
- package/src/store/vector/sqlite-vec.ts +230 -139
- package/src/store/vector/stats.ts +29 -73
- package/src/store/vector/status.ts +151 -52
- package/src/store/vector/stored-vectors.ts +131 -45
- package/src/store/vector/types.ts +10 -0
- package/src/store/vector/variant-search.ts +771 -109
- package/src/store/vector/variants.ts +785 -144
- package/browser-extension/artifacts/gno-browser-clipper-v2.14.0.zip.sha256 +0 -1
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,
|
|
@@ -63,6 +64,29 @@ export interface EmbedBacklogDeps {
|
|
|
63
64
|
acquireWriteTurn?: AcquireWriteTurn;
|
|
64
65
|
/** Explicit confirmation to build a separate vector partition (never implied by --yes). */
|
|
65
66
|
allowNewPartition?: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Bounded run (verified variant passes): stop at the first batch boundary
|
|
69
|
+
* at or past either limit. Chunks count every owner attempted, embedded or
|
|
70
|
+
* failed; seconds run from the start of the pass.
|
|
71
|
+
*/
|
|
72
|
+
limits?: EmbedRunLimits;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface EmbedRunLimits {
|
|
76
|
+
maxChunks?: number;
|
|
77
|
+
maxSeconds?: number;
|
|
78
|
+
/**
|
|
79
|
+
* Pending owners in scope when the pass started, for callers that hold the
|
|
80
|
+
* write lease for the whole pass (the CLI): the stop classification then
|
|
81
|
+
* uses this count minus committed owners instead of probing the backlog.
|
|
82
|
+
*/
|
|
83
|
+
pendingAtStart?: number;
|
|
84
|
+
/**
|
|
85
|
+
* Chunks already sent to the provider for this run before the embedding
|
|
86
|
+
* loop (runtime verification). They count toward `maxChunks`; the run
|
|
87
|
+
* still embeds its first batch, so it always progresses.
|
|
88
|
+
*/
|
|
89
|
+
spent?: number;
|
|
66
90
|
}
|
|
67
91
|
|
|
68
92
|
export interface EmbedBacklogResult {
|
|
@@ -77,6 +101,14 @@ export interface EmbedBacklogResult {
|
|
|
77
101
|
syncError?: string;
|
|
78
102
|
/** The pass stopped early because another writer held the write gate. */
|
|
79
103
|
deferred?: boolean;
|
|
104
|
+
/**
|
|
105
|
+
* The pass stopped at a batch boundary because a `limits` bound was reached
|
|
106
|
+
* while owners were still pending. Activation is skipped, as for any
|
|
107
|
+
* incomplete pass; committed batches stay.
|
|
108
|
+
*/
|
|
109
|
+
stopped?: "limit";
|
|
110
|
+
/** The pass ended early because the runtime identity changed; work may remain. */
|
|
111
|
+
incomplete?: boolean;
|
|
80
112
|
}
|
|
81
113
|
|
|
82
114
|
interface Cursor {
|
|
@@ -115,7 +147,7 @@ export async function embedBacklog(
|
|
|
115
147
|
const background = isBackgroundInference();
|
|
116
148
|
const batchSize = background
|
|
117
149
|
? Math.min(deps.batchSize ?? 32, 32)
|
|
118
|
-
: (deps.batchSize ??
|
|
150
|
+
: (deps.batchSize ?? 64);
|
|
119
151
|
const embedFingerprint = getEmbeddingFingerprint({
|
|
120
152
|
modelUri,
|
|
121
153
|
dimensions: vectorIndex.dimensions,
|
|
@@ -193,73 +225,154 @@ export async function embedBacklog(
|
|
|
193
225
|
};
|
|
194
226
|
|
|
195
227
|
try {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
// Get next batch using seek pagination
|
|
199
|
-
const batchResult = await statsPort.getBacklog(
|
|
200
|
-
modelUri,
|
|
201
|
-
embedFingerprint,
|
|
202
|
-
{
|
|
203
|
-
limit: batchSize,
|
|
204
|
-
after: cursor,
|
|
205
|
-
collection,
|
|
206
|
-
}
|
|
207
|
-
);
|
|
228
|
+
const httpConcurrency =
|
|
229
|
+
!background && !deps.acquireWriteTurn && isHttpModelUri(modelUri) ? 3 : 1;
|
|
208
230
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
231
|
+
if (httpConcurrency > 1) {
|
|
232
|
+
type InFlightBatch = Promise<
|
|
233
|
+
Awaited<ReturnType<typeof embedAndStoreBatch>>
|
|
234
|
+
>;
|
|
235
|
+
const inFlight: InFlightBatch[] = [];
|
|
236
|
+
let exhausted = false;
|
|
212
237
|
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
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
|
+
};
|
|
217
278
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
279
|
+
const fillErr = await fillHttpWindow();
|
|
280
|
+
if (fillErr) {
|
|
281
|
+
await Promise.allSettled(inFlight);
|
|
282
|
+
return fillErr;
|
|
222
283
|
}
|
|
223
284
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
const batchStoreResult = background
|
|
237
|
-
? await withInferencePage(storePage)
|
|
238
|
-
: await storePage();
|
|
239
|
-
if (!batchStoreResult) {
|
|
240
|
-
// Past its deadline: this page stays pending for the next pass.
|
|
241
|
-
errors += batch.length;
|
|
242
|
-
deps.onProgress?.(embedded, errors);
|
|
243
|
-
await Bun.sleep(0);
|
|
244
|
-
continue;
|
|
245
|
-
}
|
|
246
|
-
if (batchStoreResult.deferred)
|
|
247
|
-
return ok({ embedded, errors, contentionErrors, deferred: true });
|
|
248
|
-
embedded += batchStoreResult.embedded;
|
|
249
|
-
errors += batchStoreResult.errors;
|
|
250
|
-
contentionErrors += batchStoreResult.contentionErrors;
|
|
251
|
-
if (background) {
|
|
252
|
-
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);
|
|
253
297
|
deps.onProgress?.(embedded, errors);
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
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
|
+
}
|
|
258
306
|
}
|
|
259
|
-
|
|
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
|
+
}
|
|
260
329
|
|
|
261
|
-
|
|
262
|
-
|
|
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
|
+
}
|
|
263
376
|
}
|
|
264
377
|
}
|
|
265
378
|
|
package/src/embed/batch.ts
CHANGED
|
@@ -57,9 +57,72 @@ async function resetEmbeddingPort(
|
|
|
57
57
|
return embedPort.init();
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
export interface EmbedRecoveryOptions {
|
|
61
|
+
/**
|
|
62
|
+
* Called before each provider request with the number of texts it submits,
|
|
63
|
+
* including resubmissions after a port reset and adaptive recovery. Bounded
|
|
64
|
+
* runs charge these against `--max-chunks`.
|
|
65
|
+
*/
|
|
66
|
+
onSubmit?: (texts: number) => void;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Report each provider request to `onSubmit` before it is sent. */
|
|
70
|
+
function meteredPort(
|
|
71
|
+
embedPort: EmbeddingPort,
|
|
72
|
+
onSubmit: ((texts: number) => void) | undefined
|
|
73
|
+
): Pick<EmbeddingPort, "embed" | "embedBatch"> {
|
|
74
|
+
return {
|
|
75
|
+
embed: (text) => {
|
|
76
|
+
onSubmit?.(1);
|
|
77
|
+
return embedPort.embed(text);
|
|
78
|
+
},
|
|
79
|
+
embedBatch: (texts) => {
|
|
80
|
+
onSubmit?.(texts.length);
|
|
81
|
+
return embedPort.embedBatch(texts);
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A port that reports each `embed`/`embedBatch` request to `onSubmit` while
|
|
88
|
+
* `meter.active` is true; every other member is the original port's. Used to
|
|
89
|
+
* charge requests made outside the embedding loop (runtime verification)
|
|
90
|
+
* against a bounded run.
|
|
91
|
+
*/
|
|
92
|
+
export function meterEmbeddingPort(
|
|
93
|
+
embedPort: EmbeddingPort,
|
|
94
|
+
onSubmit: (texts: number) => void
|
|
95
|
+
): { port: EmbeddingPort; meter: { active: boolean } } {
|
|
96
|
+
const meter = { active: true };
|
|
97
|
+
const port = new Proxy(embedPort, {
|
|
98
|
+
get(target, prop) {
|
|
99
|
+
if (meter.active && prop === "embed")
|
|
100
|
+
return (
|
|
101
|
+
text: string,
|
|
102
|
+
options?: Parameters<EmbeddingPort["embed"]>[1]
|
|
103
|
+
) => {
|
|
104
|
+
onSubmit(1);
|
|
105
|
+
return target.embed(text, options);
|
|
106
|
+
};
|
|
107
|
+
if (meter.active && prop === "embedBatch")
|
|
108
|
+
return (
|
|
109
|
+
texts: string[],
|
|
110
|
+
options?: Parameters<EmbeddingPort["embedBatch"]>[1]
|
|
111
|
+
) => {
|
|
112
|
+
onSubmit(texts.length);
|
|
113
|
+
return target.embedBatch(texts, options);
|
|
114
|
+
};
|
|
115
|
+
const value: unknown = Reflect.get(target, prop, target);
|
|
116
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
return { port, meter };
|
|
120
|
+
}
|
|
121
|
+
|
|
60
122
|
export async function embedTextsWithRecovery(
|
|
61
123
|
embedPort: EmbeddingPort,
|
|
62
|
-
texts: string[]
|
|
124
|
+
texts: string[],
|
|
125
|
+
options: EmbedRecoveryOptions = {}
|
|
63
126
|
): Promise<LlmResult<EmbedBatchRecoveryResult>> {
|
|
64
127
|
if (texts.length === 0) {
|
|
65
128
|
return {
|
|
@@ -73,11 +136,12 @@ export async function embedTextsWithRecovery(
|
|
|
73
136
|
};
|
|
74
137
|
}
|
|
75
138
|
|
|
139
|
+
const provider = meteredPort(embedPort, options.onSubmit);
|
|
76
140
|
const profile = getEmbeddingCompatibilityProfile(embedPort.modelUri);
|
|
77
141
|
// A background page gets one provider attempt. Recovery remains durable for
|
|
78
142
|
// the next pass instead of expanding the current native scheduling turn.
|
|
79
143
|
if (isBackgroundInference() && profile.batchEmbeddingTrusted) {
|
|
80
|
-
const result = await
|
|
144
|
+
const result = await provider.embedBatch(texts);
|
|
81
145
|
assertInferenceResult(result);
|
|
82
146
|
if (!result.ok) return result;
|
|
83
147
|
const complete = result.value.length === texts.length;
|
|
@@ -92,7 +156,7 @@ export async function embedTextsWithRecovery(
|
|
|
92
156
|
};
|
|
93
157
|
}
|
|
94
158
|
if (profile.batchEmbeddingTrusted) {
|
|
95
|
-
let batchResult = await
|
|
159
|
+
let batchResult = await provider.embedBatch(texts);
|
|
96
160
|
assertInferenceResult(batchResult);
|
|
97
161
|
if (!batchResult.ok) {
|
|
98
162
|
const formattedBatchError = formatFailureMessage(batchResult.error);
|
|
@@ -101,7 +165,7 @@ export async function embedTextsWithRecovery(
|
|
|
101
165
|
if (!reset.ok) {
|
|
102
166
|
return reset;
|
|
103
167
|
}
|
|
104
|
-
batchResult = await
|
|
168
|
+
batchResult = await provider.embedBatch(texts);
|
|
105
169
|
assertInferenceResult(batchResult);
|
|
106
170
|
}
|
|
107
171
|
}
|
|
@@ -117,9 +181,14 @@ export async function embedTextsWithRecovery(
|
|
|
117
181
|
};
|
|
118
182
|
}
|
|
119
183
|
|
|
120
|
-
const recovered = await recoverWithAdaptiveBatches(
|
|
121
|
-
|
|
122
|
-
|
|
184
|
+
const recovered = await recoverWithAdaptiveBatches(
|
|
185
|
+
embedPort,
|
|
186
|
+
provider,
|
|
187
|
+
texts,
|
|
188
|
+
{
|
|
189
|
+
rootBatchAlreadyFailed: true,
|
|
190
|
+
}
|
|
191
|
+
);
|
|
123
192
|
if (!recovered.ok) {
|
|
124
193
|
return recovered;
|
|
125
194
|
}
|
|
@@ -139,7 +208,7 @@ export async function embedTextsWithRecovery(
|
|
|
139
208
|
};
|
|
140
209
|
}
|
|
141
210
|
|
|
142
|
-
const recovered = await recoverIndividually(embedPort, texts);
|
|
211
|
+
const recovered = await recoverIndividually(embedPort, provider, texts);
|
|
143
212
|
if (!recovered.ok) {
|
|
144
213
|
return recovered;
|
|
145
214
|
}
|
|
@@ -159,6 +228,7 @@ export async function embedTextsWithRecovery(
|
|
|
159
228
|
|
|
160
229
|
async function recoverWithAdaptiveBatches(
|
|
161
230
|
embedPort: EmbeddingPort,
|
|
231
|
+
provider: Pick<EmbeddingPort, "embed" | "embedBatch">,
|
|
162
232
|
texts: string[],
|
|
163
233
|
options: { rootBatchAlreadyFailed?: boolean } = {}
|
|
164
234
|
): Promise<
|
|
@@ -188,7 +258,7 @@ async function recoverWithAdaptiveBatches(
|
|
|
188
258
|
}
|
|
189
259
|
|
|
190
260
|
if (rangeTexts.length === 1) {
|
|
191
|
-
const result = await
|
|
261
|
+
const result = await provider.embed(rangeTexts[0] ?? "");
|
|
192
262
|
assertInferenceResult(result);
|
|
193
263
|
if (result.ok) {
|
|
194
264
|
vectors[offset] = result.value;
|
|
@@ -202,7 +272,7 @@ async function recoverWithAdaptiveBatches(
|
|
|
202
272
|
let batchResult: Awaited<ReturnType<typeof embedPort.embedBatch>> | null =
|
|
203
273
|
null;
|
|
204
274
|
if (!batchAlreadyFailed) {
|
|
205
|
-
batchResult = await
|
|
275
|
+
batchResult = await provider.embedBatch(rangeTexts);
|
|
206
276
|
assertInferenceResult(batchResult);
|
|
207
277
|
}
|
|
208
278
|
if (
|
|
@@ -230,7 +300,7 @@ async function recoverWithAdaptiveBatches(
|
|
|
230
300
|
return reinit;
|
|
231
301
|
}
|
|
232
302
|
|
|
233
|
-
const retry = await recoverIndividually(embedPort, texts);
|
|
303
|
+
const retry = await recoverIndividually(embedPort, provider, texts);
|
|
234
304
|
if (!retry.ok) {
|
|
235
305
|
return retry;
|
|
236
306
|
}
|
|
@@ -267,6 +337,7 @@ async function recoverWithAdaptiveBatches(
|
|
|
267
337
|
|
|
268
338
|
async function recoverIndividually(
|
|
269
339
|
embedPort: EmbeddingPort,
|
|
340
|
+
provider: Pick<EmbeddingPort, "embed" | "embedBatch">,
|
|
270
341
|
texts: string[]
|
|
271
342
|
): Promise<
|
|
272
343
|
LlmResult<Omit<EmbedBatchRecoveryResult, "batchFailed" | "batchError">>
|
|
@@ -278,7 +349,7 @@ async function recoverIndividually(
|
|
|
278
349
|
|
|
279
350
|
for (const text of texts) {
|
|
280
351
|
assertInferenceActive();
|
|
281
|
-
const result = await
|
|
352
|
+
const result = await provider.embed(text);
|
|
282
353
|
assertInferenceResult(result);
|
|
283
354
|
if (result.ok) {
|
|
284
355
|
vectors.push(result.value);
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resume position for bounded embed runs (`--max-chunks`, `--max-seconds`).
|
|
3
|
+
*
|
|
4
|
+
* A bounded run that stops saves the position of its last attempted batch in
|
|
5
|
+
* `schema_meta`. The next bounded run for the same scope starts after it,
|
|
6
|
+
* reaches the end of the backlog and then wraps to the start, so chunks that
|
|
7
|
+
* keep failing stay pending without blocking every later chunk. A pass that
|
|
8
|
+
* completes clears the position. Advisory only: a missing or malformed value
|
|
9
|
+
* reads as "start from the beginning".
|
|
10
|
+
*
|
|
11
|
+
* @module src/embed/resume-cursor
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { Database } from "bun:sqlite";
|
|
15
|
+
|
|
16
|
+
const PREFIX = "embed_resume_cursor:";
|
|
17
|
+
|
|
18
|
+
export function resumeCursorKey(
|
|
19
|
+
scope: string,
|
|
20
|
+
collection: string | undefined
|
|
21
|
+
): string {
|
|
22
|
+
return `${PREFIX}${scope}:${collection ?? ""}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function readResumeCursor<T extends object>(
|
|
26
|
+
db: Database,
|
|
27
|
+
key: string,
|
|
28
|
+
isCursor: (value: unknown) => value is T
|
|
29
|
+
): T | undefined {
|
|
30
|
+
try {
|
|
31
|
+
const row = db
|
|
32
|
+
.query<{ value: string }, [string]>(
|
|
33
|
+
"SELECT value FROM schema_meta WHERE key = ?"
|
|
34
|
+
)
|
|
35
|
+
.get(key);
|
|
36
|
+
if (!row) return undefined;
|
|
37
|
+
const value: unknown = JSON.parse(row.value);
|
|
38
|
+
return isCursor(value) ? value : undefined;
|
|
39
|
+
} catch {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Saves the cursor, or removes it when `value` is undefined. */
|
|
45
|
+
export function writeResumeCursor(
|
|
46
|
+
db: Database,
|
|
47
|
+
key: string,
|
|
48
|
+
value: object | undefined
|
|
49
|
+
): void {
|
|
50
|
+
if (value === undefined) {
|
|
51
|
+
db.run("DELETE FROM schema_meta WHERE key = ?", [key]);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
db.run(
|
|
55
|
+
`INSERT INTO schema_meta (key, value, updated_at) VALUES (?, ?, datetime('now'))
|
|
56
|
+
ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at`,
|
|
57
|
+
[key, JSON.stringify(value)]
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface OwnerCursor {
|
|
62
|
+
documentId: number;
|
|
63
|
+
seq: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function isOwnerCursor(value: unknown): value is OwnerCursor {
|
|
67
|
+
const v = value as Partial<OwnerCursor> | null;
|
|
68
|
+
return (
|
|
69
|
+
!!v &&
|
|
70
|
+
typeof v === "object" &&
|
|
71
|
+
Number.isSafeInteger(v.documentId) &&
|
|
72
|
+
Number.isSafeInteger(v.seq)
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Orders owners as pages do: by document id, then chunk seq. */
|
|
77
|
+
export function compareOwnerCursor(a: OwnerCursor, b: OwnerCursor): number {
|
|
78
|
+
return a.documentId === b.documentId
|
|
79
|
+
? a.seq - b.seq
|
|
80
|
+
: a.documentId - b.documentId;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface ChunkCursor {
|
|
84
|
+
mirrorHash: string;
|
|
85
|
+
seq: number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function isChunkCursor(value: unknown): value is ChunkCursor {
|
|
89
|
+
const v = value as Partial<ChunkCursor> | null;
|
|
90
|
+
return (
|
|
91
|
+
!!v &&
|
|
92
|
+
typeof v === "object" &&
|
|
93
|
+
typeof v.mirrorHash === "string" &&
|
|
94
|
+
Number.isSafeInteger(v.seq)
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Orders chunks as the legacy backlog does: by mirror hash, then seq. */
|
|
99
|
+
export function compareChunkCursor(a: ChunkCursor, b: ChunkCursor): number {
|
|
100
|
+
if (a.mirrorHash !== b.mirrorHash)
|
|
101
|
+
return a.mirrorHash < b.mirrorHash ? -1 : 1;
|
|
102
|
+
return a.seq - b.seq;
|
|
103
|
+
}
|
package/src/embed/retry.ts
CHANGED
|
@@ -199,6 +199,8 @@ export async function embedAndStoreBatch(params: {
|
|
|
199
199
|
delays?: number[];
|
|
200
200
|
/** Gate held only around persistence, never around inference. */
|
|
201
201
|
acquireWriteTurn?: AcquireWriteTurn;
|
|
202
|
+
/** Called with the text count of each provider request (see `embedTextsWithRecovery`). */
|
|
203
|
+
onSubmit?: (texts: number) => void;
|
|
202
204
|
}): Promise<EmbedStoreBatchResult> {
|
|
203
205
|
const { embedPort, vectorIndex, items, modelUri, embedFingerprint } = params;
|
|
204
206
|
const db = params.statsPort && getVectorStatsDatabase(params.statsPort);
|
|
@@ -211,12 +213,19 @@ export async function embedAndStoreBatch(params: {
|
|
|
211
213
|
errorSamples: ["Atomic vector checkpoint unavailable"],
|
|
212
214
|
batchFailed: false,
|
|
213
215
|
};
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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
|
+
});
|
|
220
229
|
|
|
221
230
|
if (!embedResult.ok) {
|
|
222
231
|
const formattedError = formatLlmFailure(embedResult.error);
|
|
@@ -267,44 +276,44 @@ export async function embedAndStoreBatch(params: {
|
|
|
267
276
|
|
|
268
277
|
let written = 0;
|
|
269
278
|
let committedRows: VectorRow[] = [];
|
|
270
|
-
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;
|
|
271
294
|
const checkpoint = (rows: VectorRow[]): VectorRow[] => {
|
|
272
295
|
assertInferenceActive();
|
|
273
296
|
if (params.identityStillCurrent && !params.identityStillCurrent())
|
|
274
297
|
return [];
|
|
275
|
-
if (!db) return rows;
|
|
298
|
+
if (!db || !selectCurrentChunkStmt) return rows;
|
|
276
299
|
// A concurrently activated verified partition forbids a late legacy write.
|
|
277
|
-
if (
|
|
278
|
-
db
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
db
|
|
284
|
-
.query(
|
|
285
|
-
"SELECT 1 FROM vector_partitions WHERE model = ? AND state = 'active' AND activated_epoch IS NOT NULL LIMIT 1"
|
|
286
|
-
)
|
|
287
|
-
.get(modelUri)
|
|
288
|
-
)
|
|
289
|
-
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
|
+
}
|
|
290
306
|
return rows.filter((row) => {
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
SELECT c.text, d.title FROM content_chunks c
|
|
295
|
-
JOIN documents d ON d.mirror_hash = c.mirror_hash AND d.active = 1
|
|
296
|
-
WHERE c.mirror_hash = ? AND c.seq = ? ORDER BY d.id LIMIT 1
|
|
297
|
-
`)
|
|
298
|
-
.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);
|
|
299
310
|
return (
|
|
300
|
-
!!item &&
|
|
301
311
|
!!current &&
|
|
302
312
|
formatDocForEmbedding(
|
|
303
313
|
current.text,
|
|
304
314
|
current.title ?? undefined,
|
|
305
315
|
modelUri
|
|
306
|
-
) ===
|
|
307
|
-
formatDocForEmbedding(item.text, item.title ?? undefined, modelUri)
|
|
316
|
+
) === expectedFormatted
|
|
308
317
|
);
|
|
309
318
|
});
|
|
310
319
|
};
|