@soulcraft/brainy 3.49.0 → 3.50.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.
@@ -10,6 +10,7 @@ import { TypeUtils, NOUN_TYPE_COUNT, VERB_TYPE_COUNT } from '../types/graphTypes
10
10
  import { SparseIndex, ChunkManager, AdaptiveChunkingStrategy } from './metadataIndexChunking.js';
11
11
  import { EntityIdMapper } from './entityIdMapper.js';
12
12
  import { RoaringBitmap32 } from 'roaring-wasm';
13
+ import { FieldTypeInference } from './fieldTypeInference.js';
13
14
  export class MetadataIndexManager {
14
15
  constructor(storage, config = {}) {
15
16
  this.isRebuilding = false;
@@ -81,6 +82,8 @@ export class MetadataIndexManager {
81
82
  // Initialize chunking system (v3.42.0) with roaring bitmap support
82
83
  this.chunkManager = new ChunkManager(storage, this.idMapper);
83
84
  this.chunkingStrategy = new AdaptiveChunkingStrategy();
85
+ // Initialize Field Type Inference (v3.48.0)
86
+ this.fieldTypeInference = new FieldTypeInference(storage);
84
87
  // Lazy load counts from storage statistics on first access
85
88
  this.lazyLoadCounts();
86
89
  }
@@ -395,6 +398,8 @@ export class MetadataIndexManager {
395
398
  const data = await this.storage.getMetadata(indexPath);
396
399
  if (data) {
397
400
  const sparseIndex = SparseIndex.fromJSON(data);
401
+ // CRITICAL: Initialize chunk ID counter from existing chunks to prevent ID conflicts
402
+ this.chunkManager.initializeNextChunkId(field, sparseIndex);
398
403
  // Add to unified cache (sparse indices are expensive to rebuild)
399
404
  const size = JSON.stringify(data).length;
400
405
  this.unifiedCache.set(unifiedKey, sparseIndex, 'metadata', size, 200);
@@ -742,27 +747,54 @@ export class MetadataIndexManager {
742
747
  .toLowerCase();
743
748
  }
744
749
  /**
745
- * Normalize value for consistent indexing with smart optimization
750
+ * Normalize value for consistent indexing with VALUE-BASED temporal detection
751
+ *
752
+ * v3.48.0: Replaced unreliable field name pattern matching with production-ready
753
+ * value-based detection (DuckDB-inspired). Analyzes actual data values, not names.
754
+ *
755
+ * NO FALLBACKS - Pure value-based detection only.
746
756
  */
747
757
  normalizeValue(value, field) {
748
758
  if (value === null || value === undefined)
749
759
  return '__NULL__';
750
760
  if (typeof value === 'boolean')
751
761
  return value ? '__TRUE__' : '__FALSE__';
752
- // ALWAYS apply bucketing to temporal fields (prevents pollution from the start!)
753
- // This is the key fix: don't wait for cardinality stats, just bucket immediately
754
- if (field && typeof value === 'number') {
755
- const fieldLower = field.toLowerCase();
756
- const isTemporal = fieldLower.includes('time') || fieldLower.includes('date') ||
757
- fieldLower.includes('accessed') || fieldLower.includes('modified') ||
758
- fieldLower.includes('created') || fieldLower.includes('updated');
759
- if (isTemporal) {
760
- // Apply time bucketing immediately (no need to wait for stats)
761
- const bucketSize = this.TIMESTAMP_PRECISION_MS; // 1 minute buckets
762
+ // VALUE-BASED temporal detection (no pattern matching!)
763
+ // Analyze the VALUE itself to determine if it's a timestamp
764
+ if (typeof value === 'number') {
765
+ // Check if value looks like a Unix timestamp (2000-01-01 to 2100-01-01)
766
+ const MIN_TIMESTAMP_S = 946684800; // 2000-01-01 in seconds
767
+ const MAX_TIMESTAMP_S = 4102444800; // 2100-01-01 in seconds
768
+ const MIN_TIMESTAMP_MS = MIN_TIMESTAMP_S * 1000;
769
+ const MAX_TIMESTAMP_MS = MAX_TIMESTAMP_S * 1000;
770
+ const isTimestampSeconds = value >= MIN_TIMESTAMP_S && value <= MAX_TIMESTAMP_S;
771
+ const isTimestampMilliseconds = value >= MIN_TIMESTAMP_MS && value <= MAX_TIMESTAMP_MS;
772
+ if (isTimestampSeconds || isTimestampMilliseconds) {
773
+ // VALUE is a timestamp! Apply 1-minute bucketing
774
+ const bucketSize = this.TIMESTAMP_PRECISION_MS; // 60000ms = 1 minute
762
775
  const bucketed = Math.floor(value / bucketSize) * bucketSize;
763
776
  return bucketed.toString();
764
777
  }
765
778
  }
779
+ // Check if string value is ISO 8601 datetime
780
+ if (typeof value === 'string') {
781
+ // ISO 8601 pattern: YYYY-MM-DDTHH:MM:SS...
782
+ const iso8601Pattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/;
783
+ if (iso8601Pattern.test(value)) {
784
+ // VALUE is an ISO 8601 datetime! Convert to timestamp and bucket
785
+ try {
786
+ const timestamp = new Date(value).getTime();
787
+ if (!isNaN(timestamp)) {
788
+ const bucketSize = this.TIMESTAMP_PRECISION_MS;
789
+ const bucketed = Math.floor(timestamp / bucketSize) * bucketSize;
790
+ return bucketed.toString();
791
+ }
792
+ }
793
+ catch {
794
+ // Not a valid date, treat as string
795
+ }
796
+ }
797
+ }
766
798
  // Apply smart normalization based on field statistics (for non-temporal fields)
767
799
  if (field && this.fieldStats.has(field)) {
768
800
  const stats = this.fieldStats.get(field);
@@ -286,6 +286,13 @@ export declare class ChunkManager {
286
286
  * Get chunk storage path
287
287
  */
288
288
  private getChunkPath;
289
+ /**
290
+ * Initialize nextChunkId counter from existing sparse index
291
+ * CRITICAL: Must be called when loading sparse index to prevent ID conflicts
292
+ * @param field Field name
293
+ * @param sparseIndex Loaded sparse index containing existing chunk descriptors
294
+ */
295
+ initializeNextChunkId(field: string, sparseIndex: SparseIndex): void;
289
296
  /**
290
297
  * Get next available chunk ID for a field
291
298
  */
@@ -660,6 +660,20 @@ export class ChunkManager {
660
660
  getChunkPath(field, chunkId) {
661
661
  return `__chunk__${field}_${chunkId}`;
662
662
  }
663
+ /**
664
+ * Initialize nextChunkId counter from existing sparse index
665
+ * CRITICAL: Must be called when loading sparse index to prevent ID conflicts
666
+ * @param field Field name
667
+ * @param sparseIndex Loaded sparse index containing existing chunk descriptors
668
+ */
669
+ initializeNextChunkId(field, sparseIndex) {
670
+ const existingChunkIds = sparseIndex.getAllChunkIds();
671
+ if (existingChunkIds.length > 0) {
672
+ // Find maximum chunk ID and set next to max + 1
673
+ const maxChunkId = Math.max(...existingChunkIds);
674
+ this.nextChunkId.set(field, maxChunkId + 1);
675
+ }
676
+ }
663
677
  /**
664
678
  * Get next available chunk ID for a field
665
679
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soulcraft/brainy",
3
- "version": "3.49.0",
3
+ "version": "3.50.0",
4
4
  "description": "Universal Knowledge Protocol™ - World's first Triple Intelligence database unifying vector, graph, and document search in one API. 31 nouns × 40 verbs for infinite expressiveness.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -1,40 +0,0 @@
1
- /**
2
- * Knowledge Layer Augmentation for VFS
3
- *
4
- * Adds intelligent features to VFS without modifying core functionality:
5
- * - Event recording for all operations
6
- * - Semantic versioning based on content changes
7
- * - Entity and concept extraction
8
- * - Git bridge for import/export
9
- *
10
- * This is a TRUE augmentation - VFS works perfectly without it
11
- */
12
- import { Brainy } from '../brainy.js';
13
- import { BaseAugmentation } from './brainyAugmentation.js';
14
- export declare class KnowledgeAugmentation extends BaseAugmentation {
15
- name: string;
16
- timing: 'after';
17
- metadata: 'none';
18
- operations: any;
19
- priority: number;
20
- constructor(config?: any);
21
- execute<T = any>(operation: string, params: any, next: () => Promise<T>): Promise<T>;
22
- private eventRecorder?;
23
- private semanticVersioning?;
24
- private entitySystem?;
25
- private conceptSystem?;
26
- private gitBridge?;
27
- private originalMethods;
28
- initialize(context: any): Promise<void>;
29
- augment(brain: Brainy): Promise<void>;
30
- /**
31
- * Wrap a VFS method to add Knowledge Layer functionality
32
- */
33
- private wrapMethod;
34
- /**
35
- * Add Knowledge Layer methods to VFS
36
- */
37
- private addKnowledgeMethods;
38
- private isSemanticChange;
39
- cleanup(brain: Brainy): Promise<void>;
40
- }
@@ -1,251 +0,0 @@
1
- /**
2
- * Knowledge Layer Augmentation for VFS
3
- *
4
- * Adds intelligent features to VFS without modifying core functionality:
5
- * - Event recording for all operations
6
- * - Semantic versioning based on content changes
7
- * - Entity and concept extraction
8
- * - Git bridge for import/export
9
- *
10
- * This is a TRUE augmentation - VFS works perfectly without it
11
- */
12
- import { BaseAugmentation } from './brainyAugmentation.js';
13
- import { EventRecorder } from '../vfs/EventRecorder.js';
14
- import { SemanticVersioning } from '../vfs/SemanticVersioning.js';
15
- import { PersistentEntitySystem } from '../vfs/PersistentEntitySystem.js';
16
- import { ConceptSystem } from '../vfs/ConceptSystem.js';
17
- import { GitBridge } from '../vfs/GitBridge.js';
18
- export class KnowledgeAugmentation extends BaseAugmentation {
19
- constructor(config = {}) {
20
- super(config);
21
- this.name = 'knowledge';
22
- this.timing = 'after'; // Process after VFS operations
23
- this.metadata = 'none'; // No metadata access needed
24
- this.operations = []; // VFS-specific augmentation, no operation interception
25
- this.priority = 100; // Run last
26
- this.originalMethods = new Map();
27
- }
28
- async execute(operation, params, next) {
29
- // Pass through - this augmentation works at VFS level, not operation level
30
- return await next();
31
- }
32
- async initialize(context) {
33
- await this.augment(context.brain);
34
- }
35
- async augment(brain) {
36
- // Only augment if VFS exists
37
- const vfs = brain.vfs?.();
38
- if (!vfs) {
39
- console.warn('KnowledgeAugmentation: VFS not found, skipping');
40
- return;
41
- }
42
- // Initialize Knowledge Layer components
43
- this.eventRecorder = new EventRecorder(brain);
44
- this.semanticVersioning = new SemanticVersioning(brain);
45
- this.entitySystem = new PersistentEntitySystem(brain);
46
- this.conceptSystem = new ConceptSystem(brain);
47
- this.gitBridge = new GitBridge(vfs, brain);
48
- // Wrap VFS methods to add intelligence WITHOUT slowing them down
49
- this.wrapMethod(vfs, 'writeFile', async (original, path, data, options) => {
50
- // Call original first (stays fast)
51
- const result = await original.call(vfs, path, data, options);
52
- // Knowledge processing in background (non-blocking)
53
- setImmediate(async () => {
54
- try {
55
- // Record event
56
- if (this.eventRecorder) {
57
- await this.eventRecorder.recordEvent({
58
- type: 'write',
59
- path,
60
- content: data,
61
- size: data.length,
62
- author: options?.author || 'system'
63
- });
64
- }
65
- // Check for semantic versioning
66
- if (this.semanticVersioning) {
67
- const existingContent = await vfs.readFile(path).catch(() => null);
68
- const shouldVersion = existingContent && this.isSemanticChange(existingContent, data);
69
- if (shouldVersion) {
70
- await this.semanticVersioning.createVersion(path, data, {
71
- message: 'Automatic semantic version'
72
- });
73
- }
74
- }
75
- // Extract concepts
76
- if (this.conceptSystem && options?.extractConcepts !== false) {
77
- await this.conceptSystem.extractAndLinkConcepts(path, data);
78
- }
79
- // Extract entities
80
- if (this.entitySystem && options?.extractEntities !== false) {
81
- await this.entitySystem.extractEntities(data.toString('utf8'), data);
82
- }
83
- }
84
- catch (error) {
85
- // Knowledge Layer errors should not affect VFS operations
86
- console.debug('KnowledgeLayer background processing error:', error);
87
- }
88
- });
89
- return result;
90
- });
91
- this.wrapMethod(vfs, 'unlink', async (original, path) => {
92
- const result = await original.call(vfs, path);
93
- // Record deletion event
94
- setImmediate(async () => {
95
- if (this.eventRecorder) {
96
- await this.eventRecorder.recordEvent({
97
- type: 'delete',
98
- path,
99
- author: 'system'
100
- });
101
- }
102
- });
103
- return result;
104
- });
105
- this.wrapMethod(vfs, 'rename', async (original, oldPath, newPath) => {
106
- const result = await original.call(vfs, oldPath, newPath);
107
- // Record rename event
108
- setImmediate(async () => {
109
- if (this.eventRecorder) {
110
- await this.eventRecorder.recordEvent({
111
- type: 'rename',
112
- path: oldPath,
113
- metadata: { newPath },
114
- author: 'system'
115
- });
116
- }
117
- });
118
- return result;
119
- });
120
- // Add Knowledge Layer methods to VFS
121
- this.addKnowledgeMethods(vfs);
122
- console.log('✨ Knowledge Layer augmentation enabled');
123
- }
124
- /**
125
- * Wrap a VFS method to add Knowledge Layer functionality
126
- */
127
- wrapMethod(vfs, methodName, wrapper) {
128
- const original = vfs[methodName];
129
- if (!original)
130
- return;
131
- // Store original for cleanup
132
- this.originalMethods.set(methodName, original);
133
- // Replace with wrapped version
134
- vfs[methodName] = async (...args) => {
135
- return await wrapper(original, ...args);
136
- };
137
- }
138
- /**
139
- * Add Knowledge Layer methods to VFS
140
- */
141
- addKnowledgeMethods(vfs) {
142
- // Event history
143
- vfs.getHistory = async (path, options) => {
144
- if (!this.eventRecorder)
145
- throw new Error('Knowledge Layer not initialized');
146
- return await this.eventRecorder.getHistory(path, options);
147
- };
148
- vfs.reconstructAtTime = async (path, timestamp) => {
149
- if (!this.eventRecorder)
150
- throw new Error('Knowledge Layer not initialized');
151
- return await this.eventRecorder.reconstructFileAtTime(path, timestamp);
152
- };
153
- // Semantic versioning
154
- vfs.getVersions = async (path) => {
155
- if (!this.semanticVersioning)
156
- throw new Error('Knowledge Layer not initialized');
157
- return await this.semanticVersioning.getVersions(path);
158
- };
159
- vfs.restoreVersion = async (path, versionId) => {
160
- if (!this.semanticVersioning)
161
- throw new Error('Knowledge Layer not initialized');
162
- const version = await this.semanticVersioning.getVersion(path, versionId);
163
- if (version) {
164
- await vfs.writeFile(path, version);
165
- }
166
- };
167
- // Entities
168
- vfs.findEntity = async (query) => {
169
- if (!this.entitySystem)
170
- throw new Error('Knowledge Layer not initialized');
171
- return await this.entitySystem.findEntity(query);
172
- };
173
- vfs.getEntityAppearances = async (entityId) => {
174
- if (!this.entitySystem)
175
- throw new Error('Knowledge Layer not initialized');
176
- return await this.entitySystem.getEvolution(entityId);
177
- };
178
- // Concepts
179
- vfs.getConcepts = async (path) => {
180
- if (!this.conceptSystem)
181
- throw new Error('Knowledge Layer not initialized');
182
- const concepts = await this.conceptSystem.findConcepts({ manifestedIn: path });
183
- return concepts;
184
- };
185
- vfs.getConceptGraph = async (options) => {
186
- if (!this.conceptSystem)
187
- throw new Error('Knowledge Layer not initialized');
188
- return await this.conceptSystem.getConceptGraph(options);
189
- };
190
- // Git bridge
191
- vfs.exportToGit = async (vfsPath, gitPath) => {
192
- if (!this.gitBridge)
193
- throw new Error('Knowledge Layer not initialized');
194
- return await this.gitBridge.exportToGit(vfsPath, gitPath);
195
- };
196
- vfs.importFromGit = async (gitPath, vfsPath) => {
197
- if (!this.gitBridge)
198
- throw new Error('Knowledge Layer not initialized');
199
- return await this.gitBridge.importFromGit(gitPath, vfsPath);
200
- };
201
- // Temporal coupling
202
- vfs.findTemporalCoupling = async (path, windowMs) => {
203
- if (!this.eventRecorder)
204
- throw new Error('Knowledge Layer not initialized');
205
- return await this.eventRecorder.findTemporalCoupling(path, windowMs);
206
- };
207
- }
208
- isSemanticChange(oldContent, newContent) {
209
- // Simple heuristic - significant size change or different content
210
- const oldStr = oldContent.toString('utf8');
211
- const newStr = newContent.toString('utf8');
212
- // Check for significant size change (>10%)
213
- const sizeDiff = Math.abs(oldStr.length - newStr.length) / oldStr.length;
214
- if (sizeDiff > 0.1)
215
- return true;
216
- // Check for structural changes (simplified)
217
- const oldLines = oldStr.split('\n').filter(l => l.trim());
218
- const newLines = newStr.split('\n').filter(l => l.trim());
219
- // Different number of non-empty lines
220
- return Math.abs(oldLines.length - newLines.length) > 5;
221
- }
222
- async cleanup(brain) {
223
- const vfs = brain.vfs?.();
224
- if (!vfs)
225
- return;
226
- // Restore original methods
227
- for (const [methodName, original] of this.originalMethods) {
228
- vfs[methodName] = original;
229
- }
230
- // Remove added methods
231
- delete vfs.getHistory;
232
- delete vfs.reconstructAtTime;
233
- delete vfs.getVersions;
234
- delete vfs.restoreVersion;
235
- delete vfs.findEntity;
236
- delete vfs.getEntityAppearances;
237
- delete vfs.getConcepts;
238
- delete vfs.getConceptGraph;
239
- delete vfs.exportToGit;
240
- delete vfs.importFromGit;
241
- delete vfs.findTemporalCoupling;
242
- // Clean up components
243
- this.eventRecorder = undefined;
244
- this.semanticVersioning = undefined;
245
- this.entitySystem = undefined;
246
- this.conceptSystem = undefined;
247
- this.gitBridge = undefined;
248
- console.log('Knowledge Layer augmentation removed');
249
- }
250
- }
251
- //# sourceMappingURL=KnowledgeAugmentation.js.map
@@ -1,158 +0,0 @@
1
- /**
2
- * Type Inference System - Phase 3: Type-First Query Optimization
3
- *
4
- * Automatically infers NounTypes from natural language queries using keyword-based
5
- * heuristics for fast O(1) type detection.
6
- *
7
- * Performance Guarantee: < 1ms per query
8
- * Accuracy Target: > 80%
9
- *
10
- * Examples:
11
- * - "Find engineers in San Francisco" → [Person, Location]
12
- * - "Show documents about AI" → [Document, Concept]
13
- * - "List companies in tech sector" → [Organization, Topic]
14
- */
15
- import { NounType } from '../types/graphTypes.js';
16
- /**
17
- * Result of type inference with confidence score
18
- */
19
- export interface TypeInference {
20
- type: NounType;
21
- confidence: number;
22
- matchedKeywords: string[];
23
- }
24
- /**
25
- * Configuration for type inference behavior
26
- */
27
- export interface TypeInferenceConfig {
28
- /**
29
- * Minimum confidence threshold to include a type (default: 0.4)
30
- */
31
- minConfidence?: number;
32
- /**
33
- * Maximum number of types to return (default: 5)
34
- */
35
- maxTypes?: number;
36
- /**
37
- * Enable debug logging (default: false)
38
- */
39
- debug?: boolean;
40
- /**
41
- * Enable vector similarity fallback for unknown words (default: false)
42
- * When enabled, queries with low keyword confidence trigger vector-based type inference
43
- */
44
- enableVectorFallback?: boolean;
45
- /**
46
- * Minimum confidence threshold to trigger vector fallback (default: 0.7)
47
- * If keyword matching produces confidence below this, vector fallback is used
48
- */
49
- fallbackConfidenceThreshold?: number;
50
- /**
51
- * Minimum similarity score for vector-based type matches (default: 0.5)
52
- */
53
- vectorThreshold?: number;
54
- }
55
- /**
56
- * Type Inference System
57
- *
58
- * Uses keyword matching for fast type detection from natural language.
59
- * Designed for billion-scale performance with minimal latency.
60
- */
61
- export declare class TypeInferenceSystem {
62
- private keywordMap;
63
- private phraseMap;
64
- private config;
65
- private typeEmbeddings;
66
- private embedder;
67
- constructor(config?: TypeInferenceConfig);
68
- /**
69
- * Infer noun types from a natural language query (synchronous keyword matching only)
70
- * For hybrid mode with vector fallback, use inferTypesAsync()
71
- *
72
- * @param query - Natural language query string
73
- * @returns Array of type inferences sorted by confidence (highest first)
74
- */
75
- inferTypes(query: string): TypeInference[];
76
- /**
77
- * Infer noun types with hybrid approach: keyword matching + optional vector fallback
78
- * This is the async version that supports vector similarity fallback
79
- *
80
- * @param query - Natural language query string
81
- * @returns Promise resolving to array of type inferences
82
- */
83
- inferTypesAsync(query: string): Promise<TypeInference[]>;
84
- /**
85
- * Internal: Keyword-based type inference (synchronous, fast)
86
- */
87
- private inferTypesViaKeywords;
88
- /**
89
- * Internal: Hybrid inference with vector fallback (asynchronous)
90
- */
91
- private inferTypesWithFallback;
92
- /**
93
- * Match multi-word phrases in query
94
- */
95
- private matchPhrases;
96
- /**
97
- * Match individual keywords in query
98
- */
99
- private matchKeywords;
100
- /**
101
- * Find closest keyword using edit distance (for typo correction)
102
- * Allows edit distance 1-2 depending on word length
103
- */
104
- private findFuzzyKeywordMatch;
105
- /**
106
- * Calculate Levenshtein (edit) distance between two strings
107
- */
108
- private levenshteinDistance;
109
- /**
110
- * Update type score with new match
111
- */
112
- private updateTypeScore;
113
- /**
114
- * Load pre-compiled type embeddings from embeddedTypeEmbeddings.ts
115
- */
116
- private loadTypeEmbeddings;
117
- /**
118
- * Lazy-load TransformerEmbedding model (only when vector fallback is triggered)
119
- */
120
- private loadEmbedder;
121
- /**
122
- * Calculate cosine similarity between two vectors
123
- */
124
- private cosineSimilarity;
125
- /**
126
- * Infer types using vector similarity against pre-compiled type embeddings
127
- */
128
- private inferTypesViaVectorSimilarity;
129
- /**
130
- * Merge keyword-based and vector-based results
131
- * Prioritizes keyword results (explicit matches) over vector results (semantic matches)
132
- */
133
- private mergeResults;
134
- /**
135
- * Build keyword dictionary for single-word matching
136
- */
137
- private buildKeywordMap;
138
- /**
139
- * Build phrase dictionary for multi-word matching
140
- */
141
- private buildPhraseMap;
142
- /**
143
- * Get statistics about the inference system
144
- */
145
- getStats(): {
146
- keywordCount: number;
147
- phraseCount: number;
148
- config: Required<TypeInferenceConfig>;
149
- };
150
- }
151
- /**
152
- * Get or create the global TypeInferenceSystem instance
153
- */
154
- export declare function getTypeInferenceSystem(config?: TypeInferenceConfig): TypeInferenceSystem;
155
- /**
156
- * Convenience function to infer types from a query
157
- */
158
- export declare function inferTypes(query: string, config?: TypeInferenceConfig): TypeInference[];