-
-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathVectorService.mjs
More file actions
1093 lines (960 loc) · 48.1 KB
/
Copy pathVectorService.mjs
File metadata and controls
1093 lines (960 loc) · 48.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import aiConfig from '../../mcp/server/knowledge-base/config.mjs';
import TextEmbeddingService from '../memory-core/TextEmbeddingService.mjs';
import mcConfig from '../../mcp/server/memory-core/config.mjs';
import Base from '../../../src/core/Base.mjs';
import {
bytesToTokens,
emitConsumerFriction
} from '../memory-core/helpers/consumerFrictionHelper.mjs';
import ChromaManager from './ChromaManager.mjs';
import crypto from 'crypto';
import fs from 'fs-extra';
import logger from '../../mcp/server/knowledge-base/logger.mjs';
import path from 'path';
import readline from 'readline';
import DestructiveOperationGuard from '../../mcp/server/shared/services/DestructiveOperationGuard.mjs';
import {computeCorpusFingerprint, decideResume, selectResumableChunks} from './helpers/resumableEmbedding.mjs';
import {clearResumeState, readResumeState, writeResumeState} from './helpers/kbEmbeddingResumeStore.mjs';
const TENANT_GUARDED_FIELDS = ['tenantId', 'repoSlug', 'visibility', 'originAgentIdentity', 'tenantConfigVersion', 'ingestedAt'];
const STALE_STRATEGIES = Object.freeze(new Set(['delete-upfront', 'shadow-swap']));
const STALE_STRATEGY_SKIP = 'skip';
/**
* @summary Manages vector database operations including embedding generation and storage.
*
* This service encapsulates all interactions with the vector database (ChromaDB) and the
* embedding provider (Google Generative AI). It is responsible for the "Load" phase of the
* ETL pipeline, taking structured knowledge chunks and ensuring they are correctly
* vectorized and stored.
*
* **Helper Role:**
* This service is primarily a helper for `DatabaseService` and is not intended to be
* directly exposed as MCP tools. Its methods are invoked by `DatabaseService` as part
* of higher-level orchestration workflows.
*
* ### Key Responsibilities:
* 1. **Embedding Generation:** Interacts with the Google Generative AI API to generate
* text embeddings for knowledge chunks.
* 2. **Vector Storage:** Manages the ChromaDB collection, including upserting new/changed
* documents and removing stale ones.
* 3. **Data Enrichment:** Pre-calculates class inheritance chains to improve query context.
* 4. **Diffing:** Optimizes API usage by only processing chunks that have changed since
* the last run.
*
* @class Neo.ai.services.knowledge-base.VectorService
* @extends Neo.core.Base
* @singleton
*/
class VectorService extends Base {
static config = {
/**
* @member {String} className='Neo.ai.services.knowledge-base.VectorService'
* @protected
*/
className: 'Neo.ai.services.knowledge-base.VectorService',
/**
* @member {Boolean} singleton=true
* @protected
*/
singleton: true
}
/**
* Permanently deletes the knowledge base collection.
* @param {Object} [options]
* @param {String|Object} [options.confirmation] Explicit production confirmation token.
* @returns {Promise<object>} A promise that resolves to a success message.
*/
async deleteCollection({confirmation} = {}) {
const collectionName = aiConfig.collectionName;
try {
await DestructiveOperationGuard.assertDestructiveTargetAllowed({
operation: 'knowledge-base.chroma.delete',
subsystem: 'knowledge-base',
mode : 'delete',
target : {
collectionName,
chroma: {
host: aiConfig.host,
port: aiConfig.port,
path: aiConfig.path
},
path : aiConfig.path,
repoRoot: aiConfig.neoRootDir
},
confirmation
});
// Route through ChromaManager.deleteCollection so the canonical-name guard applies.
// Forward the operator confirmation so the canonical-name guard accepts it.
await ChromaManager.deleteCollection({name: collectionName, confirmation});
ChromaManager.invalidateKnowledgeBaseCollectionCache();
const message = `Knowledge base collection '${collectionName}' deleted successfully.`;
logger.log(message);
return {message};
} catch (error) {
if (error.message.includes(`Collection ${collectionName} does not exist.`)) {
const message = `Knowledge base collection '${collectionName}' did not exist. No action taken.`;
logger.log(message);
return {message};
}
throw error;
}
}
/**
* Returns the tenant-isolation config surface.
*
* The Knowledge Base server config can expose tenant fields directly or through the
* portable `aiConfig.knowledgeBase.*` shape used by shared AI config surfaces.
* Supporting both keeps this write boundary stable across ingestion API variants.
*
* @returns {Object} Tenant-isolation configuration object.
*/
getTenantIsolationConfig() {
const nested = aiConfig.knowledgeBase;
// The portable nested tenant-shape carries tenant fields. Tier-1's
// `knowledgeBase` leaf (KB OPS config: alerting / reconciliation / GC) is inherited here via
// the realm chain and is NOT a tenant-shape — so a naked `?? aiConfig` would wrongly select
// it (security regression: tenant fields read as undefined). Prefer the nested object only
// when it actually exposes a tenant field; otherwise fall through to the flat top-level config.
const hasTenantShape = nested != null && (
nested.defaultTenantId !== undefined ||
nested.defaultRepoSlug !== undefined ||
nested.defaultVisibility !== undefined ||
nested.spoofRejectionMode !== undefined
);
return hasTenantShape ? nested : aiConfig;
}
/**
* Resolves the authoritative tenant tuple used for KB write-side stamping.
*
* The tuple is server-derived: external ingestion callers pass already-authenticated
* context here, while Neo's default local sync falls back to the shared curated corpus.
*
* @param {Object} [tenantContext] Server-derived tenant context.
* @param {String} [tenantContext.tenantId] Tenant identifier.
* @param {String} [tenantContext.repoSlug] Repository slug within the tenant.
* @param {String} [tenantContext.visibility] Visibility scope for read-side filtering.
* @param {String} [tenantContext.originAgentIdentity] Authenticated agent identity.
* @param {Number} [tenantContext.configVersion] Active `KnowledgeBaseTenantConfig` version;
* stamped onto chunk metadata as `tenantConfigVersion`.
* `ingestedAt` (epoch ms, server-stamped via `Date.now()`) is added unconditionally —
* the retention / GC / reconciliation timestamp. It marks when the chunk row is
* **actually embedded / upserted**: `embed()`'s zero-change fast path skips an unchanged
* same-content re-push, so that chunk keeps its prior `ingestedAt` (a content change
* yields a *new* chunk row — new content-hash ID — with its own fresh `ingestedAt`).
* It is purely server-derived (never client-authored), so it is also a
* `TENANT_GUARDED_FIELDS` member. Consumers MUST treat a missing `ingestedAt` as
* unknown-age and fail-safe — never expire or action a chunk with no timestamp.
* @returns {{tenantId: String, repoSlug: String, visibility: String, tenantConfigVersion: Number, ingestedAt: Number, originAgentIdentity: String|undefined}}
*/
resolveTenantStamp(tenantContext = {}) {
const config = this.getTenantIsolationConfig();
const stamp = {
tenantId : tenantContext.tenantId ?? config.defaultTenantId,
repoSlug : tenantContext.repoSlug ?? config.defaultRepoSlug,
visibility : tenantContext.visibility ?? config.defaultVisibility,
tenantConfigVersion: tenantContext.configVersion ?? 0,
ingestedAt : Date.now()
};
if (tenantContext.originAgentIdentity) {
stamp.originAgentIdentity = tenantContext.originAgentIdentity;
}
return stamp;
}
/**
* Creates the tenant-aware storage ID for a parsed chunk.
*
* `chunk.hash` remains the content fingerprint, while the Chroma ID binds that
* fingerprint to the authoritative `{tenantId, repoSlug}` tuple so two tenants can
* ingest byte-identical files without colliding.
*
* @param {Object} chunk Parsed knowledge chunk.
* @param {Object} stamp Authoritative tenant stamp.
* @returns {String} SHA-256 tenant-aware chunk ID.
*/
createTenantAwareChunkId(chunk, stamp) {
const identityString = JSON.stringify({
tenantId: stamp.tenantId,
repoSlug: stamp.repoSlug,
hash : chunk.hash,
type : chunk.type,
name : chunk.name,
source : chunk.source
});
return crypto.createHash('sha256').update(identityString).digest('hex');
}
/**
* Rejects or logs client-supplied identity fields that conflict with server context.
*
* @param {Object} chunk Parsed knowledge chunk.
* @param {Object} stamp Authoritative tenant stamp.
* @param {String} chunkId Tenant-aware chunk ID for diagnostics.
* @returns {void}
*/
validateTenantStamp(chunk, stamp, chunkId) {
const config = this.getTenantIsolationConfig();
const mode = config.spoofRejectionMode ?? 'overwrite';
for (const field of TENANT_GUARDED_FIELDS) {
const hasClientValue = Object.prototype.hasOwnProperty.call(chunk, field);
const serverValue = stamp[field];
const clientValue = chunk[field];
if (!hasClientValue || clientValue === serverValue) {
continue;
}
const warning = {
field,
chunkId,
clientValue,
serverValue : serverValue ?? null,
tenantId : stamp.tenantId,
repoSlug : stamp.repoSlug,
originAgentIdentity: stamp.originAgentIdentity ?? null
};
if (mode === 'reject') {
const error = new Error(`KB_TENANT_SPOOF_REJECTED: client-supplied ${field} does not match the server-derived value.`);
error.code = 'KB_TENANT_SPOOF_REJECTED';
error.details = warning;
throw error;
}
logger.warn('[VectorService] Overwriting client-supplied tenant metadata field.', warning);
}
}
/**
* Applies authoritative tenant metadata before diffing or upserting.
*
* @param {Object} chunk Parsed knowledge chunk.
* @param {Object} stamp Authoritative tenant stamp.
* @returns {Object} Stamped chunk with tenant-aware `id` and `hash`.
*/
applyTenantStamp(chunk, stamp) {
const chunkId = this.createTenantAwareChunkId(chunk, stamp);
this.validateTenantStamp(chunk, stamp, chunkId);
const stampedChunk = {
...chunk,
...stamp,
hash: chunkId,
id : chunkId
};
if (!stamp.originAgentIdentity) {
delete stampedChunk.originAgentIdentity;
}
return stampedChunk;
}
/**
* Resolves the stale-data handling strategy while preserving the legacy
* `deleteStale: false` incremental-ingestion contract.
*
* @param {Object} options
* @param {String} [options.staleStrategy] Explicit stale strategy.
* @param {Boolean} [options.deleteStale] Legacy boolean stale-deletion flag.
* @returns {String} Resolved strategy: `delete-upfront`, `shadow-swap`, or internal `skip`.
* @throws {Error} When the explicit stale strategy is unsupported.
*/
resolveStaleStrategy({staleStrategy, deleteStale = true} = {}) {
if (staleStrategy) {
if (!STALE_STRATEGIES.has(staleStrategy)) {
throw new Error(`Unsupported staleStrategy '${staleStrategy}'. Expected one of: ${Array.from(STALE_STRATEGIES).join(', ')}.`);
}
return staleStrategy;
}
return deleteStale ? 'delete-upfront' : STALE_STRATEGY_SKIP;
}
/**
* Resolves the local embedding-model guardrail used before provider invocation.
*
* @returns {{enabled: Boolean, contextLimitTokens: Number, safeProcessingLimitTokens: Number, model: String}}
*/
resolveEmbeddingGuardrail() {
const localEmbeddingProviders = new Set(['openAiCompatible', 'ollama']);
const embeddingProvider = mcConfig.embeddingProvider;
const contextLimitTokens = Number(aiConfig.localModels.embedding.contextLimitTokens);
const safeProcessingLimitTokens = Number(aiConfig.localModels.embedding.safeProcessingLimitTokens);
const model = embeddingProvider === 'ollama'
? aiConfig.ollama.embeddingModel
: embeddingProvider === 'openAiCompatible'
? aiConfig.openAiCompatible.embeddingModel
: embeddingProvider;
return {
enabled : localEmbeddingProviders.has(embeddingProvider),
contextLimitTokens,
safeProcessingLimitTokens,
model
};
}
/**
* Builds the provider input string used by the embedding guardrail and provider call.
*
* @param {Object} chunk Parsed knowledge chunk.
* @returns {String} Provider input text.
*/
buildEmbeddingInputText(chunk) {
return `${chunk.type}: ${chunk.name} in ${chunk.className || ''}\n${chunk.description || chunk.content || ''}`;
}
/**
* Expands recoverable over-budget text chunks before tenant stamping and diffing.
*
* The final `embedChunks()` guardrail still refuses any chunk that remains too large;
* this pass only prevents the full-corpus sync route from losing split-safe source
* files before they ever become Chroma rows.
*
* @param {Object[]} chunks Raw parsed knowledge chunks read from JSONL.
* @returns {Object[]} Original chunks plus deterministic split children.
*/
expandOversizedEmbeddingChunks(chunks) {
const guardrail = this.resolveEmbeddingGuardrail();
if (!guardrail.enabled) {
return chunks;
}
const expanded = [];
for (const chunk of chunks) {
const inputText = this.buildEmbeddingInputText(chunk),
evaluation = this.measureEmbeddingInput({text: inputText, guardrail});
if (!evaluation.skip) {
expanded.push(chunk);
continue;
}
const splitChunks = this.splitOversizedEmbeddingChunk({chunk, guardrail});
expanded.push(...splitChunks);
}
return expanded;
}
/**
* Creates a deterministic pre-tenant split hash for a full-sync child chunk.
*
* @param {Object} options
* @param {Object} options.chunk Source chunk before splitting.
* @param {String} options.content Split content slice.
* @param {Number} options.index Zero-based split index.
* @param {Number} options.total Total split count.
* @param {Number} options.charStart Character offset where the split starts.
* @param {Number} options.charEnd Character offset where the split ends.
* @returns {String} Stable SHA-256 hash.
*/
createSplitChunkHash({chunk, content, index, total, charStart, charEnd}) {
const identityString = JSON.stringify({
parentHash : chunk.hash,
type : chunk.type,
kind : chunk.kind,
name : chunk.name,
source : chunk.source,
content,
oversizedSplitIndex : index,
oversizedSplitTotal : total,
oversizedSplitCharStart: charStart,
oversizedSplitCharEnd : charEnd
});
return crypto.createHash('sha256').update(identityString).digest('hex');
}
/**
* Splits a recoverable over-budget text chunk into deterministic sub-chunks.
*
* @param {Object} options
* @param {Object} options.chunk Source chunk before tenant stamping.
* @param {Object} options.guardrail Resolved embedding guardrail.
* @param {Function} [options.createHash] Optional caller-specific split hash function.
* @returns {Object[]} Either split children or the original chunk.
*/
splitOversizedEmbeddingChunk({chunk, guardrail, createHash}) {
const content = chunk.content || chunk.description;
if (typeof content !== 'string' || content.length === 0) {
return [chunk];
}
const maxInputBytes = Math.max(1, guardrail.safeProcessingLimitTokens * 3),
prefixBytes = Buffer.byteLength(`${chunk.type}: ${chunk.name} in ${chunk.className || ''}\n`, 'utf8'),
maxContentBytes = Math.max(1, maxInputBytes - prefixBytes - 128),
parts = this.splitTextByByteBudget(content, maxContentBytes);
if (parts.length <= 1) {
return [chunk];
}
let charStart = 0;
return parts.map((part, index) => {
const charEnd = charStart + part.length,
child = {
...chunk,
content : part,
description: part,
name : `${chunk.name} [part ${index + 1}/${parts.length}]`,
hashInputs : Array.from(new Set([
...(chunk.hashInputs || []),
'oversizedSplitIndex',
'oversizedSplitTotal',
'oversizedSplitCharStart',
'oversizedSplitCharEnd'
])),
oversizedSplit : true,
oversizedSplitIndex : index,
oversizedSplitTotal : parts.length,
oversizedSplitCharStart: charStart,
oversizedSplitCharEnd : charEnd
};
const hash = createHash
? createHash(child)
: this.createSplitChunkHash({
chunk,
content: part,
index,
total : parts.length,
charStart,
charEnd
});
child.hash = hash;
child.id = hash;
charStart = charEnd;
return child;
});
}
/**
* Splits text on stable line boundaries, falling back to character slices for huge lines.
*
* @param {String} text Source text.
* @param {Number} maxBytes Maximum byte size per returned part.
* @returns {String[]} Split text parts.
*/
splitTextByByteBudget(text, maxBytes) {
if (Buffer.byteLength(text, 'utf8') <= maxBytes) {
return [text];
}
const parts = [];
let current = '';
for (const line of text.match(/[^\n]*\n?|[^\n]+$/g).filter(Boolean)) {
if (Buffer.byteLength(line, 'utf8') > maxBytes) {
if (current) {
parts.push(current);
current = '';
}
parts.push(...this.splitLongStringByByteBudget(line, maxBytes));
continue;
}
if (current && Buffer.byteLength(current + line, 'utf8') > maxBytes) {
parts.push(current);
current = line;
} else {
current += line;
}
}
if (current) {
parts.push(current);
}
return parts.filter(part => part.length > 0);
}
/**
* Splits one oversized line without breaking JavaScript surrogate pairs.
*
* @param {String} value Source string.
* @param {Number} maxBytes Maximum byte size per part.
* @returns {String[]} Split text parts.
*/
splitLongStringByByteBudget(value, maxBytes) {
const parts = [];
let current = '';
for (const char of value) {
if (current && Buffer.byteLength(current + char, 'utf8') > maxBytes) {
parts.push(current);
current = char;
} else {
current += char;
}
}
if (current) {
parts.push(current);
}
return parts;
}
/**
* Measures provider input size without recording a final refusal diagnostic.
*
* @param {Object} options
* @param {String} options.text Provider input text.
* @param {Object} options.guardrail Resolved embedding guardrail.
* @returns {{skip: Boolean, inputBytes: Number, inputTokensEstimate: Number}}
*/
measureEmbeddingInput({text, guardrail}) {
if (!guardrail.enabled) {
return {skip: false, inputBytes: 0, inputTokensEstimate: 0};
}
const inputBytes = Buffer.byteLength(text || '', 'utf8');
const inputTokensEstimate = bytesToTokens(inputBytes);
return {
skip: inputTokensEstimate > guardrail.safeProcessingLimitTokens,
inputBytes,
inputTokensEstimate
};
}
/**
* Emits a bounded, non-secret friction record for an over-budget embedding input.
*
* @param {Object} options
* @param {Object} options.chunk Tenant-stamped chunk.
* @param {String} options.text Provider input text.
* @param {Object} options.guardrail Resolved embedding guardrail.
* @returns {{skip: Boolean, inputBytes: Number, inputTokensEstimate: Number}}
*/
evaluateEmbeddingInput({chunk, text, guardrail}) {
const {skip, inputBytes, inputTokensEstimate} = this.measureEmbeddingInput({text, guardrail});
if (!skip) {
return {skip: false, inputBytes, inputTokensEstimate};
}
emitConsumerFriction({
assetRef : chunk.id || chunk.source || chunk.name || 'kb-chunk',
consumer : 'VectorService.embedChunks',
model : guardrail.model,
symptom : 'size-precheck-skip',
emissionPoint : 'pre-invocation',
suggestionKind : 'split-document',
inputBytes,
inputTokensEstimate,
contextLimitTokens : guardrail.contextLimitTokens,
safeProcessingLimitTokens: guardrail.safeProcessingLimitTokens,
serviceDomain : 'other',
note : 'KB embedding input exceeds safe processing band; split or reduce the source chunk before embedding.'
});
logger.warn('[VectorService] Skipping over-budget embedding chunk before provider invocation.', {
chunkId : chunk.id,
tenantId : chunk.tenantId,
repoSlug : chunk.repoSlug,
source : chunk.source,
inputBytes,
inputTokensEstimate,
safeProcessingLimitTokens: guardrail.safeProcessingLimitTokens,
contextLimitTokens : guardrail.contextLimitTokens
});
return {skip: true, inputBytes, inputTokensEstimate};
}
/**
* Embeds a set of chunks into the provided Chroma collection.
*
* @param {Object} options
* @param {Object} options.collection Chroma collection target.
* @param {Object[]} options.chunksToProcess Tenant-stamped chunks to embed.
* @returns {Promise<{embedded: Number, skipped: Number}>}
*/
async embedChunks({collection, chunksToProcess}) {
if (chunksToProcess.length === 0) {
return {embedded: 0, skipped: 0};
}
logger.log(`Using TextEmbeddingService with provider: ${mcConfig.embeddingProvider}.`);
logger.log('Embedding chunks...');
const {batchSize, batchDelay, maxRetries} = aiConfig;
const guardrail = this.resolveEmbeddingGuardrail();
let embeddedCount = 0;
let skippedCount = 0;
for (let i = 0; i < chunksToProcess.length; i += batchSize) {
if (i > 0 && batchDelay) {
await this.timeout(batchDelay);
}
const batch = chunksToProcess.slice(i, i + batchSize);
const batchInputs = batch.map(chunk => ({
chunk,
text: this.buildEmbeddingInputText(chunk)
}));
const embeddable = [];
for (const input of batchInputs) {
const result = this.evaluateEmbeddingInput({
chunk: input.chunk,
text : input.text,
guardrail
});
if (result.skip) {
skippedCount++;
} else {
embeddable.push(input);
}
}
if (embeddable.length === 0) {
logger.warn(`[VectorService] Skipped embedding batch ${i / batchSize + 1}; all ${batch.length} chunk(s) exceeded the embedding safe-processing band.`);
continue;
}
const batchToEmbed = embeddable.map(input => input.chunk);
const textsToEmbed = embeddable.map(input => input.text);
let retries = 0;
let success = false;
while (retries < maxRetries && !success) {
try {
const embeddings = await TextEmbeddingService.embedTexts(textsToEmbed, mcConfig.embeddingProvider);
const metadatas = batchToEmbed.map(chunk => {
const metadata = {};
for (const [key, value] of Object.entries(chunk)) {
metadata[key] = (value === null) ? 'null' : (typeof value === 'object') ? JSON.stringify(value) : value;
}
return metadata;
});
await collection.upsert({
ids: batchToEmbed.map(chunk => chunk.id),
embeddings,
metadatas
});
embeddedCount += batchToEmbed.length;
logger.log(`Processed and embedded batch ${i / batchSize + 1} of ${Math.ceil(chunksToProcess.length / batchSize)} (${batchToEmbed.length} embedded, ${batch.length - batchToEmbed.length} skipped).`);
success = true;
} catch (err) {
retries++;
console.error(`An error occurred during embedding batch ${i / batchSize + 1}. Retrying (${retries}/${maxRetries})...`, err.message);
if (retries < maxRetries) {
await new Promise(res => setTimeout(res, 2 ** retries * 1000)); // Exponential backoff
} else {
throw new Error(`Failed to process batch ${i / batchSize + 1} after ${maxRetries} retries. Aborting.`);
}
}
}
}
return {embedded: embeddedCount, skipped: skippedCount};
}
/**
* Creates a process-unique temporary collection name for a shadow-swap phase.
*
* @param {String} phase Name suffix identifying the phase.
* @returns {String} Temporary Chroma collection name.
*/
createSwapCollectionName(phase) {
return `${aiConfig.collectionName}-${phase}-${Date.now()}-${crypto.randomUUID()}`;
}
/**
* Rebuilds the full corpus into a shadow collection, then promotes it to the
* canonical name without ever gutting the live collection in-place.
*
* ChromaDB has no single atomic exchange primitive, so the promote step is a
* bounded two-rename transaction: live -> parking, shadow -> canonical. The old
* collection remains parked as a rollback artifact because destructive collection
* deletion is operator-gated outside unit tests.
*
* @param {Object} options
* @param {Object} options.liveCollection Existing canonical collection handle.
* @param {Object[]} options.knowledgeBase Full tenant-stamped corpus.
* @param {Number} options.idsToDeleteCount Logical stale-id count removed from the canonical view.
* @returns {Promise<Object>} Embedding result.
*/
async embedViaShadowSwap({liveCollection, knowledgeBase, idsToDeleteCount}) {
const stateDir = this.getResumeStateDir();
const fingerprint = computeCorpusFingerprint(knowledgeBase);
const resumeState = await readResumeState({dir: stateDir});
const decision = decideResume({resumeState, currentFingerprint: fingerprint});
let shadowCollection = null;
let shadowName = null;
let chunksToEmbed = knowledgeBase;
let attempts = 1;
let alreadyEmbedded = 0;
// Resume into the preserved shadow (it holds the completed batches), skipping already-embedded chunks.
if (decision.resume) {
try {
shadowName = resumeState.shadowName;
attempts = decision.attempts;
shadowCollection = await ChromaManager.client.getCollection({name: shadowName, embeddingFunction: aiConfig.dummyEmbeddingFunction});
const selection = selectResumableChunks({chunks: knowledgeBase, existingIds: await this.readCollectionIds(shadowCollection)});
chunksToEmbed = selection.remaining;
alreadyEmbedded = selection.alreadyEmbedded;
logger.log(`Resuming KB embedding into '${shadowName}' — ${alreadyEmbedded} already embedded, ${chunksToEmbed.length} remaining (attempt ${attempts}).`);
} catch (resumeError) {
logger.warn(`[VectorService] Could not resume into preserved shadow '${shadowName}' (${resumeError.message}); rebuilding fresh.`);
shadowCollection = null;
}
}
// Fresh build: discard any stale preserved shadow (corpus-drift / attempt-cap / vanished), then rebuild.
if (!shadowCollection) {
if (resumeState?.shadowName) {
await this.discardResumeShadow(resumeState.shadowName);
}
await clearResumeState({dir: stateDir});
shadowName = this.createSwapCollectionName('shadow');
attempts = 1;
chunksToEmbed = knowledgeBase;
alreadyEmbedded = 0;
logger.log(`Building shadow knowledge-base collection '${shadowName}' (${decision.reason}).`);
// Write-ahead resume marker: record the shadow BEFORE creating + embedding it, so a non-promoted
// shadow is ALWAYS indexed by a marker. Otherwise a transient embed failure whose catch-path
// marker-write ALSO fails strands the shadow with no marker — the next fresh-build's
// discardResumeShadow(resumeState?.shadowName) then no-ops and the shadow orphans permanently. A
// failure here throws before createCollection, so no shadow is ever created without its marker.
await writeResumeState({dir: stateDir, fingerprint, shadowName, attempts});
shadowCollection = await ChromaManager.client.createCollection({name: shadowName, embeddingFunction: aiConfig.dummyEmbeddingFunction});
}
const parkingName = this.createSwapCollectionName('parking');
let liveParked = false;
let shadowPromoted = false;
try {
const embedResult = await this.embedChunks({collection: shadowCollection, chunksToProcess: chunksToEmbed});
if (embedResult.skipped > 0) {
throw new Error(`KB_EMBEDDING_INPUT_SIZE_EXCEEDED: shadow-swap refused to promote an incomplete corpus after skipping ${embedResult.skipped} over-budget embedding chunk(s).`);
}
logger.log(`Promoting shadow collection '${shadowName}' to '${aiConfig.collectionName}'.`);
await liveCollection.modify({name: parkingName});
liveParked = true;
await shadowCollection.modify({name: aiConfig.collectionName});
shadowPromoted = true;
ChromaManager.invalidateKnowledgeBaseCollectionCache();
await clearResumeState({dir: stateDir}); // promoted → nothing to resume
const collection = await ChromaManager.getKnowledgeBaseCollection();
const count = await collection.count();
const message = `Embedding complete via shadow-swap. Collection now contains ${count} items. Previous collection parked as '${parkingName}'.`;
logger.log(message);
return {
message,
embedded : embedResult.embedded + alreadyEmbedded,
deleted : idsToDeleteCount,
staleStrategy : 'shadow-swap',
shadowCollection : shadowName,
parkedCollection : parkingName,
canonicalCollection: aiConfig.collectionName
};
} catch (error) {
ChromaManager.invalidateKnowledgeBaseCollectionCache();
if (liveParked && !shadowPromoted) {
try {
await liveCollection.modify({name: aiConfig.collectionName});
ChromaManager.invalidateKnowledgeBaseCollectionCache();
} catch (rollbackError) {
logger.error('[VectorService] Failed to roll back parked live collection after shadow-swap failure:', rollbackError.message);
}
}
if (!shadowPromoted) {
// A too-big chunk (KB_EMBEDDING_INPUT_SIZE_EXCEEDED) will NEVER embed — resuming it is futile,
// so park the shadow as a dead artifact (the prior behavior) and clear its marker. A TRANSIENT
// embed failure (a provider blip) instead PRESERVES the shadow so the next run resumes from
// here. This re-write advances the attempt counter; fresh builds already wrote the marker
// ahead of embedding and resumes carry a prior marker, so the shadow is already indexed.
if (error?.message?.includes('KB_EMBEDDING_INPUT_SIZE_EXCEEDED')) {
await this.parkFailedShadowCollection({shadowCollection, shadowName});
await clearResumeState({dir: stateDir});
} else {
try {
await writeResumeState({dir: stateDir, fingerprint, shadowName, attempts});
logger.warn(`[VectorService] Preserved shadow '${shadowName}' for resume (attempt ${attempts}) after a transient embedding failure: ${error.message}`);
} catch (preserveError) {
// The write-ahead (fresh build) or prior (resume) marker still indexes the shadow, so
// it is not orphaned — only the attempt-counter refresh was lost.
logger.error(`[VectorService] Could not refresh resume-state for '${shadowName}' (${preserveError.message}); the write-ahead marker still protects the shadow.`);
}
}
}
throw error;
}
}
/**
* @summary Resolves the gitignored directory holding the KB embedding resume-state marker.
* @returns {String}
*/
getResumeStateDir() {
return this.resumeStateDir ?? path.resolve(aiConfig.neoRootDir, '.neo-ai-data', 'kb-sync');
}
/**
* @summary Reads every document id present in a Chroma collection (paginated id-only fetch).
* Used to compute which chunks a preserved resume-shadow already holds.
* @param {Object} collection Chroma collection handle.
* @returns {Promise<Set<String>>}
*/
async readCollectionIds(collection) {
const ids = [];
const limit = 1000;
let offset = 0;
while (true) {
const batch = await collection.get({include: [], limit, offset});
const batchIds = batch?.ids ?? [];
ids.push(...batchIds);
if (batchIds.length < limit) break;
offset += limit;
}
return new Set(ids);
}
/**
* @summary Parks a stale preserved resume-shadow (corpus drifted / attempt cap reached) so the fresh
* rebuild starts clean. Best-effort: a vanished/unreadable shadow is simply nothing to discard.
* @param {String} shadowName The preserved resume-shadow collection name.
* @returns {Promise<void>}
*/
async discardResumeShadow(shadowName) {
try {
const shadowCollection = await ChromaManager.client.getCollection({name: shadowName, embeddingFunction: aiConfig.dummyEmbeddingFunction});
await this.parkFailedShadowCollection({shadowCollection, shadowName});
} catch (error) {
logger.warn(`[VectorService] Could not discard stale resume-shadow '${shadowName}': ${error.message}`);
}
}
/**
* @summary Renames an incomplete shadow collection so future canonical resolves do not treat it as an active promote.
*
* The collection-name delete guard intentionally blocks unconfirmed production
* deletes, including non-canonical names. A failed pre-promote shadow therefore
* gets moved to a non-active recovery name instead of being dropped silently.
*
* @param {Object} options
* @param {Object} options.shadowCollection Shadow collection handle to park.
* @param {String} options.shadowName Original active shadow collection name.
* @returns {Promise<String|null>} Parked name, or `null` if parking failed.
*/
async parkFailedShadowCollection({shadowCollection, shadowName}) {
const failedShadowName = this.createSwapCollectionName('failed-shadow');
try {
await shadowCollection.modify({name: failedShadowName});
logger.warn(`[VectorService] Parked failed shadow collection '${shadowName}' as '${failedShadowName}'.`);
return failedShadowName;
} catch (error) {
logger.error(`[VectorService] Failed to park shadow collection '${shadowName}' after shadow-swap failure:`, error.message);
return null;
}
}
/**
* Reads a JSONL file, enriches data, generates embeddings, and updates ChromaDB.
*
* **Work-volume gate:** when invoked via MCP (`viaMcp: true`), refuses
* synchronous execution if `chunksToProcess.length` exceeds `aiConfig.mcpSyncMaxChunks`
* (default 50, aligned with `batchSize`). Returns a structured `{error, code, ...}`
* payload that the MCP server's `Server.mjs` converts to `isError: true` per its
* existing `'error' in result` contract. CLI invocations (via `npm run ai:sync-kb`)
* pass `viaMcp: false` and bypass the gate — explicit opt-in to long-running work.
*
* @param {String} knowledgeBasePath The path to the JSONL source file.
* @param {Object} [opts] Optional invocation context.
* @param {Boolean} [opts.viaMcp=false] True when called via MCP tool dispatch;
* enables the work-volume gate.
* @param {Object} [opts.tenantContext] Server-derived tenant stamp context.
* @param {Boolean} [opts.deleteStale=true] True applies full-corpus stale-id deletion.
* Incremental ingestion pushes pass `false` and
* use explicit deletion signaling instead.
* @param {String} [opts.staleStrategy] Stale handling strategy. `delete-upfront`
* preserves the historical behavior;
* `shadow-swap` rebuilds into a fresh collection
* before promoting it to the canonical name.
* @returns {Promise<object>} A promise that resolves to a success message, OR a
* `{error, code: 'KB_SYNC_VOLUME_EXCEEDED', ...}` shape when the MCP gate fires.
*/
async embed(knowledgeBasePath, {viaMcp = false, tenantContext = {}, deleteStale = true, staleStrategy} = {}) {
logger.log('Starting knowledge base embedding...');
const resolvedStaleStrategy = this.resolveStaleStrategy({staleStrategy, deleteStale});
if (!await fs.pathExists(knowledgeBasePath)) {
throw new Error(`Knowledge base file not found at ${knowledgeBasePath}.`);
}
const knowledgeBase = [];
const fileStream = fs.createReadStream(knowledgeBasePath);
const rl = readline.createInterface({input: fileStream, crlfDelay: Infinity});
for await (const line of rl) {
knowledgeBase.push(JSON.parse(line));
}
logger.log(`Loaded ${knowledgeBase.length} knowledge chunks from file.`);
const tenantStamp = this.resolveTenantStamp(tenantContext);
const expandedKnowledgeBase = this.expandOversizedEmbeddingChunks(knowledgeBase);
for (let i = 0; i < expandedKnowledgeBase.length; i++) {
expandedKnowledgeBase[i] = this.applyTenantStamp(expandedKnowledgeBase[i], tenantStamp);
}
// Enrich with inheritance chains
const classNameToDataMap = {};
expandedKnowledgeBase.forEach(chunk => {
if (chunk.kind === 'module-context' && chunk.className) {
classNameToDataMap[chunk.className] = {
source: chunk.source,
parent: chunk.extends || null
};
}
});
expandedKnowledgeBase.forEach(chunk => {
let currentClass = chunk.className; // Metadata is now on every chunk
const inheritanceChain = [];
const visited = new Set();
// If no className metadata (e.g. non-class files), skip
if (!currentClass) return;
while (currentClass && classNameToDataMap[currentClass]?.parent && !visited.has(currentClass)) {
visited.add(currentClass);
const parentClassName = classNameToDataMap[currentClass].parent;
const parentData = classNameToDataMap[parentClassName];
if (parentData) {
inheritanceChain.push({ className: parentClassName, source: parentData.source });
}
currentClass = parentClassName;
}
chunk.inheritanceChain = inheritanceChain;
});
const collection = await ChromaManager.getKnowledgeBaseCollection();
logger.log(`Using collection: ${collection.name}`);
logger.log('Fetching existing documents from ChromaDB...');
const existingIds = new Set();
let offset = 0;
const limit = 2000;
let batch;
// ChromaDB has a default limit (usually 10) if not specified.
// Even with a larger limit, it's safer to paginate for large collections.
do {
batch = await collection.get({
include: [],
limit,
offset : offset
});
batch.ids.forEach(id => existingIds.add(id));
offset += limit;
logger.log(`Fetched ${existingIds.size} IDs so far...`);
} while (batch.ids.length === limit);
logger.log(`Found ${existingIds.size} existing documents.`);