noormme 1.2.3 → 1.2.4

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.
Files changed (126) hide show
  1. package/dist/cjs/agentic/ActionJournal.d.ts +6 -6
  2. package/dist/cjs/agentic/ActionJournal.js +10 -10
  3. package/dist/cjs/agentic/CapabilityManager.d.ts +5 -5
  4. package/dist/cjs/agentic/CapabilityManager.js +34 -19
  5. package/dist/cjs/agentic/Cortex.js +12 -13
  6. package/dist/cjs/agentic/PersonaManager.d.ts +4 -4
  7. package/dist/cjs/agentic/PersonaManager.js +18 -10
  8. package/dist/cjs/agentic/PolicyEnforcer.d.ts +4 -8
  9. package/dist/cjs/agentic/PolicyEnforcer.js +25 -22
  10. package/dist/cjs/agentic/improvement/AblationEngine.d.ts +7 -3
  11. package/dist/cjs/agentic/improvement/AblationEngine.js +51 -24
  12. package/dist/cjs/agentic/improvement/ActionRefiner.d.ts +1 -1
  13. package/dist/cjs/agentic/improvement/ActionRefiner.js +25 -15
  14. package/dist/cjs/agentic/improvement/EvolutionRitual.js +2 -1
  15. package/dist/cjs/agentic/improvement/EvolutionaryPilot.d.ts +1 -1
  16. package/dist/cjs/agentic/improvement/EvolutionaryPilot.js +17 -19
  17. package/dist/cjs/agentic/improvement/GovernanceManager.d.ts +5 -5
  18. package/dist/cjs/agentic/improvement/GovernanceManager.js +12 -22
  19. package/dist/cjs/agentic/improvement/HiveLink.d.ts +3 -3
  20. package/dist/cjs/agentic/improvement/HiveLink.js +15 -157
  21. package/dist/cjs/agentic/improvement/KnowledgeDistiller.d.ts +15 -29
  22. package/dist/cjs/agentic/improvement/KnowledgeDistiller.js +66 -349
  23. package/dist/cjs/agentic/improvement/QuotaManager.d.ts +1 -1
  24. package/dist/cjs/agentic/improvement/QuotaManager.js +11 -11
  25. package/dist/cjs/agentic/improvement/RecursiveReasoner.d.ts +4 -4
  26. package/dist/cjs/agentic/improvement/RecursiveReasoner.js +10 -10
  27. package/dist/cjs/agentic/improvement/ReflectionEngine.d.ts +2 -2
  28. package/dist/cjs/agentic/improvement/ReflectionEngine.js +2 -2
  29. package/dist/cjs/agentic/improvement/RitualOrchestrator.d.ts +3 -3
  30. package/dist/cjs/agentic/improvement/RitualOrchestrator.js +12 -9
  31. package/dist/cjs/agentic/improvement/SelfEvolution.d.ts +1 -1
  32. package/dist/cjs/agentic/improvement/SelfEvolution.js +21 -18
  33. package/dist/cjs/agentic/improvement/SelfTestRegistry.d.ts +1 -1
  34. package/dist/cjs/agentic/improvement/SelfTestRegistry.js +31 -19
  35. package/dist/cjs/agentic/improvement/SovereignMetrics.d.ts +6 -6
  36. package/dist/cjs/agentic/improvement/SovereignMetrics.js +12 -12
  37. package/dist/cjs/agentic/improvement/StrategicPlanner.d.ts +10 -54
  38. package/dist/cjs/agentic/improvement/StrategicPlanner.js +43 -396
  39. package/dist/cjs/agentic/improvement/distillation/ConflictChallenger.d.ts +3 -0
  40. package/dist/cjs/agentic/improvement/distillation/ConflictChallenger.js +54 -0
  41. package/dist/cjs/agentic/improvement/distillation/FactDistiller.d.ts +10 -0
  42. package/dist/cjs/agentic/improvement/distillation/FactDistiller.js +115 -0
  43. package/dist/cjs/agentic/improvement/distillation/KnowledgeConsolidator.d.ts +6 -0
  44. package/dist/cjs/agentic/improvement/distillation/KnowledgeConsolidator.js +63 -0
  45. package/dist/cjs/agentic/improvement/distillation/RelationshipArchitect.d.ts +6 -0
  46. package/dist/cjs/agentic/improvement/distillation/RelationshipArchitect.js +73 -0
  47. package/dist/cjs/agentic/improvement/governance/MaintenanceOracle.js +5 -5
  48. package/dist/cjs/agentic/improvement/governance/PersonaAuditor.js +4 -4
  49. package/dist/cjs/agentic/improvement/governance/RemediationEngine.js +8 -3
  50. package/dist/cjs/agentic/improvement/hive/DomainMaster.d.ts +4 -0
  51. package/dist/cjs/agentic/improvement/hive/DomainMaster.js +19 -0
  52. package/dist/cjs/agentic/improvement/hive/KnowledgePromoter.d.ts +6 -0
  53. package/dist/cjs/agentic/improvement/hive/KnowledgePromoter.js +56 -0
  54. package/dist/cjs/agentic/improvement/hive/SkillPropagator.d.ts +6 -0
  55. package/dist/cjs/agentic/improvement/hive/SkillPropagator.js +91 -0
  56. package/dist/cjs/agentic/improvement/strategy/EvolutionVerificator.d.ts +6 -0
  57. package/dist/cjs/agentic/improvement/strategy/EvolutionVerificator.js +48 -0
  58. package/dist/cjs/agentic/improvement/strategy/MutationEngine.d.ts +10 -0
  59. package/dist/cjs/agentic/improvement/strategy/MutationEngine.js +119 -0
  60. package/dist/cjs/agentic/improvement/strategy/PerformanceAnalyst.d.ts +6 -0
  61. package/dist/cjs/agentic/improvement/strategy/PerformanceAnalyst.js +84 -0
  62. package/dist/cjs/cli/index.js +0 -0
  63. package/dist/cjs/types/index.d.ts +8 -0
  64. package/dist/esm/agentic/ActionJournal.d.ts +6 -6
  65. package/dist/esm/agentic/ActionJournal.js +10 -10
  66. package/dist/esm/agentic/CapabilityManager.d.ts +5 -5
  67. package/dist/esm/agentic/CapabilityManager.js +34 -19
  68. package/dist/esm/agentic/Cortex.js +12 -13
  69. package/dist/esm/agentic/PersonaManager.d.ts +4 -4
  70. package/dist/esm/agentic/PersonaManager.js +18 -10
  71. package/dist/esm/agentic/PolicyEnforcer.d.ts +4 -8
  72. package/dist/esm/agentic/PolicyEnforcer.js +25 -22
  73. package/dist/esm/agentic/improvement/AblationEngine.d.ts +7 -3
  74. package/dist/esm/agentic/improvement/AblationEngine.js +51 -24
  75. package/dist/esm/agentic/improvement/ActionRefiner.d.ts +1 -1
  76. package/dist/esm/agentic/improvement/ActionRefiner.js +25 -15
  77. package/dist/esm/agentic/improvement/EvolutionRitual.js +2 -1
  78. package/dist/esm/agentic/improvement/EvolutionaryPilot.d.ts +1 -1
  79. package/dist/esm/agentic/improvement/EvolutionaryPilot.js +17 -19
  80. package/dist/esm/agentic/improvement/GovernanceManager.d.ts +5 -5
  81. package/dist/esm/agentic/improvement/GovernanceManager.js +12 -22
  82. package/dist/esm/agentic/improvement/HiveLink.d.ts +3 -3
  83. package/dist/esm/agentic/improvement/HiveLink.js +15 -157
  84. package/dist/esm/agentic/improvement/KnowledgeDistiller.d.ts +15 -29
  85. package/dist/esm/agentic/improvement/KnowledgeDistiller.js +66 -349
  86. package/dist/esm/agentic/improvement/QuotaManager.d.ts +1 -1
  87. package/dist/esm/agentic/improvement/QuotaManager.js +11 -11
  88. package/dist/esm/agentic/improvement/RecursiveReasoner.d.ts +4 -4
  89. package/dist/esm/agentic/improvement/RecursiveReasoner.js +10 -10
  90. package/dist/esm/agentic/improvement/ReflectionEngine.d.ts +2 -2
  91. package/dist/esm/agentic/improvement/ReflectionEngine.js +2 -2
  92. package/dist/esm/agentic/improvement/RitualOrchestrator.d.ts +3 -3
  93. package/dist/esm/agentic/improvement/RitualOrchestrator.js +12 -9
  94. package/dist/esm/agentic/improvement/SelfEvolution.d.ts +1 -1
  95. package/dist/esm/agentic/improvement/SelfEvolution.js +21 -18
  96. package/dist/esm/agentic/improvement/SelfTestRegistry.d.ts +1 -1
  97. package/dist/esm/agentic/improvement/SelfTestRegistry.js +31 -19
  98. package/dist/esm/agentic/improvement/SovereignMetrics.d.ts +6 -6
  99. package/dist/esm/agentic/improvement/SovereignMetrics.js +12 -12
  100. package/dist/esm/agentic/improvement/StrategicPlanner.d.ts +10 -54
  101. package/dist/esm/agentic/improvement/StrategicPlanner.js +43 -396
  102. package/dist/esm/agentic/improvement/distillation/ConflictChallenger.d.ts +3 -0
  103. package/dist/esm/agentic/improvement/distillation/ConflictChallenger.js +51 -0
  104. package/dist/esm/agentic/improvement/distillation/FactDistiller.d.ts +10 -0
  105. package/dist/esm/agentic/improvement/distillation/FactDistiller.js +112 -0
  106. package/dist/esm/agentic/improvement/distillation/KnowledgeConsolidator.d.ts +6 -0
  107. package/dist/esm/agentic/improvement/distillation/KnowledgeConsolidator.js +60 -0
  108. package/dist/esm/agentic/improvement/distillation/RelationshipArchitect.d.ts +6 -0
  109. package/dist/esm/agentic/improvement/distillation/RelationshipArchitect.js +70 -0
  110. package/dist/esm/agentic/improvement/governance/MaintenanceOracle.js +5 -5
  111. package/dist/esm/agentic/improvement/governance/PersonaAuditor.js +4 -4
  112. package/dist/esm/agentic/improvement/governance/RemediationEngine.js +8 -3
  113. package/dist/esm/agentic/improvement/hive/DomainMaster.d.ts +4 -0
  114. package/dist/esm/agentic/improvement/hive/DomainMaster.js +16 -0
  115. package/dist/esm/agentic/improvement/hive/KnowledgePromoter.d.ts +6 -0
  116. package/dist/esm/agentic/improvement/hive/KnowledgePromoter.js +53 -0
  117. package/dist/esm/agentic/improvement/hive/SkillPropagator.d.ts +6 -0
  118. package/dist/esm/agentic/improvement/hive/SkillPropagator.js +88 -0
  119. package/dist/esm/agentic/improvement/strategy/EvolutionVerificator.d.ts +6 -0
  120. package/dist/esm/agentic/improvement/strategy/EvolutionVerificator.js +45 -0
  121. package/dist/esm/agentic/improvement/strategy/MutationEngine.d.ts +10 -0
  122. package/dist/esm/agentic/improvement/strategy/MutationEngine.js +116 -0
  123. package/dist/esm/agentic/improvement/strategy/PerformanceAnalyst.d.ts +6 -0
  124. package/dist/esm/agentic/improvement/strategy/PerformanceAnalyst.js +81 -0
  125. package/dist/esm/types/index.d.ts +8 -0
  126. package/package.json +40 -44
@@ -1,5 +1,8 @@
1
1
  /// <reference types="./KnowledgeDistiller.d.ts" />
2
- import { calculateSimilarity } from '../../util/similarity.js';
2
+ import { FactDistiller } from './distillation/FactDistiller.js';
3
+ import { ConflictChallenger } from './distillation/ConflictChallenger.js';
4
+ import { RelationshipArchitect } from './distillation/RelationshipArchitect.js';
5
+ import { KnowledgeConsolidator } from './distillation/KnowledgeConsolidator.js';
3
6
  /**
4
7
  * KnowledgeDistiller extracts structured "KnowledgeItems" from longtail history,
5
8
  * allowing agents to build a permanent, queryable knowledge base.
@@ -10,198 +13,102 @@ export class KnowledgeDistiller {
10
13
  knowledgeTable;
11
14
  linksTable;
12
15
  bloomFilter = new Set();
13
- constructor(db, // accepting any Kysely but casting internally for our specific tables
14
- config = {}) {
16
+ distiller;
17
+ challenger;
18
+ architect;
19
+ consolidator;
20
+ constructor(db, config = {}) {
15
21
  this.db = db;
16
22
  this.config = config;
17
23
  this.knowledgeTable = config.knowledgeTable || 'agent_knowledge_base';
18
24
  this.linksTable = 'agent_knowledge_links';
19
- }
20
- /**
21
- * typedDb helper to cast the generic DB to our specific schema
22
- */
23
- get typedDb() {
24
- return this.db;
25
+ this.distiller = new FactDistiller();
26
+ this.challenger = new ConflictChallenger();
27
+ this.architect = new RelationshipArchitect();
28
+ this.consolidator = new KnowledgeConsolidator();
25
29
  }
26
30
  /**
27
31
  * Add or update a knowledge item with robust merging and transaction support.
28
32
  */
29
33
  async distill(entity, fact, confidence, sourceSessionId, tags = [], metadata = {}, source = 'assistant') {
30
- // Pass 6: Bloom filter Heuristic
31
- // Lightweight de-duplication for ultra-scale throughput.
32
- // If the fact matches a very recent pattern, skip expensive DB checks.
34
+ // Production Hardening: 32-bit Rolling Bloom Filter
33
35
  const factHash = this.bloomHash(`${entity}:${fact}`);
34
36
  if (this.bloomFilter.has(factHash)) {
35
- // Likely a duplicate, but we'll do a quick check if it's "verified" to avoid re-distilling
36
- console.log(`[KnowledgeDistiller] Bloom filter hit for ${entity}. Skipping redundant distillation.`);
37
37
  const quickMatch = await this.db
38
38
  .selectFrom(this.knowledgeTable)
39
39
  .select(['id', 'status'])
40
40
  .where('entity', '=', entity)
41
41
  .where('fact', '=', fact)
42
42
  .executeTakeFirst();
43
- if (quickMatch &&
44
- quickMatch.status === 'verified' &&
45
- source !== 'user') {
46
- return (await this.getKnowledgeByEntity(entity)).find((k) => k.fact === fact);
43
+ if (quickMatch && quickMatch.status === 'verified' && source !== 'user') {
44
+ const results = await this.getKnowledgeByEntity(entity);
45
+ const match = results.find(k => k.fact === fact);
46
+ if (match)
47
+ return match;
47
48
  }
48
49
  }
49
50
  this.bloomFilter.add(factHash);
50
- if (this.bloomFilter.size > 1000)
51
- this.bloomFilter.clear(); // Simple rolling window
51
+ if (this.bloomFilter.size > 2000)
52
+ this.bloomFilter.clear();
52
53
  return await this.db.transaction().execute(async (trx) => {
53
- // Check for exact match first
54
- const existing = await trx
55
- .selectFrom(this.knowledgeTable)
56
- .selectAll()
57
- .where('entity', '=', entity)
58
- .where('fact', '=', fact)
59
- .executeTakeFirst();
60
- if (existing) {
61
- // Merge tags
62
- const existingTags = existing.tags
63
- ? JSON.parse(existing.tags)
64
- : [];
65
- const mergedTags = Array.from(new Set([...existingTags, ...tags]));
66
- // Merge metadata
67
- const existingMeta = existing.metadata
68
- ? JSON.parse(existing.metadata)
69
- : {};
70
- // Hallucination Prevention: Tracking contributing sessions
71
- const sessions = new Set(existingMeta.sessions || []);
72
- if (sourceSessionId)
73
- sessions.add(sourceSessionId);
74
- const mergedMeta = {
75
- ...existingMeta,
76
- ...metadata,
77
- sessions: Array.from(sessions),
78
- session_count: sessions.size,
79
- };
80
- // Source weighting: User verification boosts confidence faster
81
- const boost = source === 'user' ? 0.2 : 0.05;
82
- const finalConfidence = Math.min(1.0, existing.confidence + boost);
83
- // Status Lifecycle: Auto-verify if source is user or if session count >= 3
84
- let finalStatus = existing.status || 'proposed';
85
- if (source === 'user' || mergedMeta.session_count >= 3) {
86
- finalStatus = 'verified';
87
- }
88
- const updated = await trx
89
- .updateTable(this.knowledgeTable)
90
- .set({
91
- confidence: finalConfidence,
92
- status: finalStatus,
93
- tags: JSON.stringify(mergedTags),
94
- metadata: JSON.stringify(mergedMeta),
95
- updated_at: new Date(),
96
- source_session_id: sourceSessionId ?? existing.source_session_id,
97
- })
98
- .where('id', '=', existing.id)
99
- .returningAll()
100
- .executeTakeFirstOrThrow();
101
- return this.parseKnowledge(updated);
102
- }
103
- // Conflict Detection: Check if a similar entity has a conflicting fact
104
- // Audit Phase 11: Explicit transaction propagation
54
+ // 1. Check for Exact Match & Merge
55
+ let result = await this.distiller.distillExact(trx, this.knowledgeTable, entity, fact, confidence, sourceSessionId, tags, metadata, source);
56
+ if (result)
57
+ return this.parseKnowledge(result);
58
+ // 2. Conflict Detection (Contradiction Challenge)
105
59
  await this.challengeKnowledge(entity, fact, confidence, trx);
106
- // Create new
107
- const initialMeta = {
108
- ...metadata,
109
- source,
110
- sessions: sourceSessionId ? [sourceSessionId] : [],
111
- session_count: sourceSessionId ? 1 : 0,
112
- };
113
- const created = await trx
114
- .insertInto(this.knowledgeTable)
115
- .values({
116
- entity,
117
- fact,
118
- confidence: source === 'user' ? Math.max(confidence, 0.8) : confidence,
119
- status: source === 'user' ? 'verified' : 'proposed',
120
- source_session_id: sourceSessionId ?? null,
121
- tags: JSON.stringify(tags),
122
- metadata: JSON.stringify(initialMeta),
123
- created_at: new Date(),
124
- updated_at: new Date(),
125
- })
126
- .returningAll()
127
- .executeTakeFirstOrThrow();
60
+ // 3. Create New Item
61
+ const created = await this.distiller.createInitial(trx, this.knowledgeTable, entity, fact, confidence, sourceSessionId, tags, metadata, source);
128
62
  const parsed = this.parseKnowledge(created);
129
- // Semantic Auto-Linking: Try to link to related entities
130
- await this.autoLinkKnowledge(parsed, trx);
63
+ // 4. Semantic Auto-Linking
64
+ await this.architect.autoLink(parsed, trx, this.knowledgeTable, this.linksTable);
131
65
  return parsed;
132
66
  });
133
67
  }
68
+ /**
69
+ * Manually challenge an existing fact with competing information.
70
+ */
71
+ async challengeKnowledge(entity, fact, confidence, trxOrDb = this.db) {
72
+ await this.challenger.challenge(trxOrDb, this.knowledgeTable, entity, fact, confidence, (i) => this.parseKnowledge(i));
73
+ }
134
74
  /**
135
75
  * Verify and reinforce existing knowledge.
136
- * Increases confidence if the fact matches.
137
76
  */
138
77
  async verifyKnowledge(id, reinforcement = 0.1) {
139
- const existing = await this.typedDb
140
- .selectFrom(this.knowledgeTable)
141
- .selectAll()
142
- .where('id', '=', id)
143
- .executeTakeFirst();
144
- if (!existing)
145
- return null;
146
- const metadata = typeof existing.metadata === 'string'
147
- ? JSON.parse(existing.metadata)
148
- : existing.metadata || {};
149
- // Hallucination Guard: Cap confidence for non-user sources until cross-session verification
150
- let maxConfidence = 1.0;
151
- if (metadata.source !== 'user' && (metadata.session_count || 0) < 3) {
152
- maxConfidence = 0.85;
153
- console.log(`[KnowledgeDistiller] Confidence capped at 0.85 for item ${id} (Need 3+ sessions for total trust)`);
154
- }
155
- const newConfidence = Math.min(maxConfidence, existing.confidence + reinforcement);
156
- // Promotion Lifecycle
157
- let newStatus = existing.status || 'proposed';
158
- if (newConfidence >= 0.9 || metadata.session_count >= 3) {
159
- newStatus = 'verified';
160
- }
161
- const updated = await this.db
162
- .updateTable(this.knowledgeTable)
163
- .set({
164
- confidence: newConfidence,
165
- status: newStatus,
166
- updated_at: new Date(),
167
- })
168
- .where('id', '=', id)
169
- .returningAll()
170
- .executeTakeFirstOrThrow();
171
- return this.parseKnowledge(updated);
78
+ const updated = await this.distiller.verify(this.db, this.knowledgeTable, id, reinforcement);
79
+ return updated ? this.parseKnowledge(updated) : null;
172
80
  }
173
81
  /**
174
82
  * Search knowledge by entity with optional tag filtering.
175
- * Records a "hit" for each retrieved item to track utility.
176
83
  */
177
84
  async getKnowledgeByEntity(entity, filterTags) {
178
- let query = this.db
85
+ const items = await this.db
179
86
  .selectFrom(this.knowledgeTable)
180
87
  .selectAll()
181
88
  .where('entity', '=', entity)
182
- .orderBy('confidence', 'desc');
183
- const items = (await query.execute());
89
+ .orderBy('confidence', 'desc')
90
+ .execute();
184
91
  const parsed = items.map((i) => this.parseKnowledge(i));
185
92
  let filtered = parsed;
186
93
  if (filterTags && filterTags.length > 0) {
187
94
  filtered = parsed.filter((item) => item.tags?.some((tag) => filterTags.includes(tag)));
188
95
  }
189
- // Record hits asynchronously
96
+ // Record hits asynchronously to hotspot optimization layer
190
97
  for (const item of filtered) {
191
- this.recordHit(item.id).catch((err) => console.error(`[KnowledgeDistiller] Failed to record hit for ${item.id}:`, err));
98
+ this.recordHit(item.id).catch(() => { });
192
99
  }
193
100
  return filtered;
194
101
  }
195
102
  /**
196
- * Record a retrieval hit for a knowledge item.
103
+ * Record a retrieval hit for a knowledge item and emit hotspot telemetry.
197
104
  */
198
105
  async recordHit(id) {
199
106
  await this.db.transaction().execute(async (trx) => {
200
107
  const existing = await trx
201
108
  .selectFrom(this.knowledgeTable)
202
- .selectAll()
109
+ .select(['id', 'entity', 'metadata'])
203
110
  .where('id', '=', id)
204
- .forUpdate() // Audit Phase 11: Atomic hit record
111
+ .forUpdate()
205
112
  .executeTakeFirst();
206
113
  if (!existing)
207
114
  return;
@@ -218,7 +125,7 @@ export class KnowledgeDistiller {
218
125
  })
219
126
  .where('id', '=', id)
220
127
  .execute();
221
- // Production Hardening: Emit a metric event for this hit to enable efficient hotspot detection
128
+ // Production Hardening: Real-time hotspots in metrics layer
222
129
  await trx
223
130
  .insertInto(this.config.metricsTable || 'agent_metrics')
224
131
  .values({
@@ -229,156 +136,11 @@ export class KnowledgeDistiller {
229
136
  .execute();
230
137
  });
231
138
  }
232
- /**
233
- * Calculate the "Fitness" of a memory item.
234
- * Score = (Confidence * 0.4) + (SignalToNoise * 0.4) + (SourceMultiplier * 0.2)
235
- */
236
- calculateFitness(item) {
237
- const metadata = item.metadata || {};
238
- const hitCount = metadata.hit_count || 0;
239
- const createdAt = new Date(item.createdAt).getTime();
240
- const ageInDays = Math.max(1, (Date.now() - createdAt) / (1000 * 60 * 60 * 24));
241
- // Signal-to-Noise: Hits per day
242
- const stn = Math.min(1.0, hitCount / ageInDays);
243
- // Source Multiplier: User verified facts get 1.0, Assistant-generated 0.7
244
- const sourceMult = metadata.source === 'user' ? 1.0 : 0.7;
245
- return item.confidence * 0.4 + stn * 0.4 + sourceMult * 0.2;
246
- }
247
- /**
248
- * Challenge existing knowledge with new evidence.
249
- * If confidence of new fact is high and contradicts existing ones (same entity, different fact),
250
- * we degrade the confidence of the old facts.
251
- */
252
- async challengeKnowledge(entity, competingFact, confidence, trxOrDb = this.db) {
253
- // Audit Phase 11: Semantic sanitization of competing fact
254
- const safeFact = competingFact.slice(0, 500).replace(/[\u0000-\u001F\u007F-\u009F]/g, '').replace(/<\|.*?\|>/g, '');
255
- const query = trxOrDb
256
- .selectFrom(this.knowledgeTable)
257
- .selectAll()
258
- .where('entity', '=', entity)
259
- .orderBy('confidence', 'desc');
260
- const existingItems = await query.execute();
261
- for (const item of existingItems) {
262
- // Parse for logic
263
- const parsedItem = this.parseKnowledge(item);
264
- if (parsedItem.fact === competingFact)
265
- continue;
266
- // Deep Hardening: Conflict detection using semantic similarity check
267
- // If the entity is the same but the fact is different, we assess conflict
268
- if (confidence > 0.8) {
269
- let newMeta = { ...parsedItem.metadata };
270
- let penalty = 0.2;
271
- let newStatus = parsedItem.status;
272
- if (parsedItem.confidence > 0.7) {
273
- newStatus = 'disputed';
274
- newMeta = {
275
- ...newMeta,
276
- status_reason: `Contradicted by: ${safeFact}`,
277
- };
278
- penalty = 0.1;
279
- }
280
- else {
281
- newStatus = 'deprecated';
282
- newMeta = {
283
- ...newMeta,
284
- status_reason: `Superseded by: ${safeFact}`,
285
- };
286
- penalty = 0.4;
287
- }
288
- const newConfidence = Math.max(0, parsedItem.confidence - penalty);
289
- await trxOrDb
290
- .updateTable(this.knowledgeTable)
291
- .set({
292
- confidence: newConfidence,
293
- status: newStatus,
294
- metadata: JSON.stringify(newMeta),
295
- updated_at: new Date(),
296
- })
297
- .where('id', '=', parsedItem.id)
298
- .execute();
299
- }
300
- }
301
- }
302
- /**
303
- * Automatic Semantic Linking
304
- * Scans for entity mentions in facts and creates links.
305
- * Production Hardening: Uses batched cross-similarity and NER-style tokenization.
306
- */
307
- async autoLinkKnowledge(item, trx) {
308
- // 1. Structural/Syntactic Extraction (NER-style tokenization)
309
- // Extract capitalized phrases (potential entities), quoted strings, and CamelCase identifiers
310
- const tokens = item.fact.match(/([A-Z][a-z]+(?:\s[A-Z][a-z]+)*)|("[^"]+")|([a-z]+[A-Z][a-z]+)/g) || [];
311
- const potentialEntities = Array.from(new Set(tokens.map((t) => t.replace(/"/g, '').trim()))).filter((t) => t.length > 2 && t !== item.entity);
312
- if (potentialEntities.length > 0) {
313
- const matches = await trx
314
- .selectFrom(this.knowledgeTable)
315
- .select(['id', 'entity'])
316
- .where('entity', 'in', potentialEntities)
317
- .execute();
318
- for (const match of matches) {
319
- await this.linkKnowledge(item.id, match.id, 'mentions', { auto: true, source: 'structural_extraction' }, trx);
320
- }
321
- }
322
- // 2. Semantic Similarity Pass (Vector/Cosine Approximation)
323
- // We limit the search to items with high intrinsic fitness to avoid linking to "noise"
324
- const candidates = await trx
325
- .selectFrom(this.knowledgeTable)
326
- .selectAll()
327
- .where('id', '!=', item.id)
328
- .where('confidence', '>', 0.4) // Only link to semi-reliable knowledge
329
- .orderBy('updated_at', 'desc')
330
- .limit(50) // Increased limit for better coverage
331
- .execute();
332
- // Batch similarity processing to reduce link churn
333
- const linksToCreate = [];
334
- for (const other of candidates) {
335
- const parsedOther = this.parseKnowledge(other);
336
- const sim = calculateSimilarity(item.fact, parsedOther.fact);
337
- // High similarity threshold for auto-linking to prevent a "everything linked to everything" graph
338
- if (sim > 0.75) {
339
- linksToCreate.push({ targetId: parsedOther.id, sim });
340
- }
341
- }
342
- // Apply links in batch
343
- for (const link of linksToCreate) {
344
- await this.linkKnowledge(item.id, link.targetId, 'semantically_related', { similarity: link.sim, version: '2.0' }, trx);
345
- }
346
- }
347
139
  /**
348
140
  * Link two knowledge items together with a relationship.
349
- * Transaction-aware version.
350
141
  */
351
142
  async linkKnowledge(sourceId, targetId, relationship, metadata, trxOrDb = this.db) {
352
- if (sourceId === targetId)
353
- return;
354
- const existing = await trxOrDb
355
- .selectFrom(this.linksTable)
356
- .select('id')
357
- .where('source_id', '=', sourceId)
358
- .where('target_id', '=', targetId)
359
- .where('relationship', '=', relationship)
360
- .executeTakeFirst();
361
- if (existing) {
362
- await trxOrDb
363
- .updateTable(this.linksTable)
364
- .set({
365
- metadata: metadata ? JSON.stringify(metadata) : null,
366
- })
367
- .where('id', '=', existing.id)
368
- .execute();
369
- }
370
- else {
371
- await trxOrDb
372
- .insertInto(this.linksTable)
373
- .values({
374
- source_id: sourceId,
375
- target_id: targetId,
376
- relationship,
377
- metadata: metadata ? JSON.stringify(metadata) : null,
378
- created_at: new Date(),
379
- })
380
- .execute();
381
- }
143
+ return this.architect.link(sourceId, targetId, relationship, metadata, trxOrDb, this.linksTable);
382
144
  }
383
145
  /**
384
146
  * Get the "Knowledge Graph" for a specific entity (1-hop neighborhood).
@@ -414,77 +176,34 @@ export class KnowledgeDistiller {
414
176
  .deleteFrom(this.knowledgeTable)
415
177
  .where('confidence', '<', threshold)
416
178
  .executeTakeFirst();
417
- return Number(result.numDeletedRows);
179
+ return Number(result.numDeletedRows || 0);
418
180
  }
419
181
  /**
420
182
  * Consolidate knowledge by merging similar facts for the same entity.
421
- * Production Hardening: Iterative bucketed approach to avoid recursive stack issues.
422
183
  */
423
184
  async consolidateKnowledge() {
424
- let totalMerged = 0;
425
- // Find entities with multiple facts (Paginated to handle scale)
426
- const candidates = await this.db
427
- .selectFrom(this.knowledgeTable)
428
- .select('entity')
429
- .groupBy('entity')
430
- .having((eb) => eb.fn.count('id'), '>', 1)
431
- .limit(500) // Audit Phase 11: Chunked search
432
- .execute();
433
- for (const cand of candidates) {
434
- const entity = cand.entity;
435
- const items = await this.getKnowledgeByEntity(entity);
436
- // Iterative merging inside the entity bucket (Paginated check)
437
- const mergedIds = new Set();
438
- const iterLimit = Math.min(items.length, 100);
439
- for (let i = 0; i < iterLimit; i++) {
440
- if (mergedIds.has(items[i].id))
441
- continue;
442
- for (let j = i + 1; j < iterLimit; j++) {
443
- if (mergedIds.has(items[j].id))
444
- continue;
445
- const sim = calculateSimilarity(items[i].fact, items[j].fact);
446
- if (sim > 0.85) {
447
- await this.mergeItems(items[i], items[j]);
448
- mergedIds.add(items[j].id);
449
- totalMerged++;
450
- }
451
- }
452
- }
453
- }
454
- return totalMerged;
185
+ return this.consolidator.consolidate(this.db, this.knowledgeTable, (e) => this.getKnowledgeByEntity(e));
455
186
  }
456
- async mergeItems(primary, secondary) {
457
- const mergedMeta = {
458
- ...secondary.metadata,
459
- ...primary.metadata,
460
- consolidated_from: secondary.id,
461
- consolidated_at: new Date().toISOString(),
462
- };
463
- const mergedTags = Array.from(new Set([...(primary.tags || []), ...(secondary.tags || [])]));
464
- await this.db.transaction().execute(async (trx) => {
465
- // Update primary
466
- await trx
467
- .updateTable(this.knowledgeTable)
468
- .set({
469
- confidence: Math.max(primary.confidence, secondary.confidence),
470
- metadata: JSON.stringify(mergedMeta),
471
- tags: JSON.stringify(mergedTags),
472
- updated_at: new Date(),
473
- })
474
- .where('id', '=', primary.id)
475
- .execute();
476
- // Delete secondary
477
- await trx
478
- .deleteFrom(this.knowledgeTable)
479
- .where('id', '=', secondary.id)
480
- .execute();
481
- });
187
+ /**
188
+ * Calculate the "Fitness" of a knowledge item.
189
+ * Score = (Confidence * 0.4) + (SignalToNoise * 0.4) + (SourceMultiplier * 0.2)
190
+ */
191
+ calculateFitness(item) {
192
+ const metadata = item.metadata || {};
193
+ const hitCount = metadata.hit_count || 0;
194
+ const createdAt = new Date(item.createdAt).getTime();
195
+ const ageInDays = Math.max(1, (Date.now() - createdAt) / (1000 * 60 * 60 * 24));
196
+ // Signal-to-Noise: Hits per day
197
+ const stn = Math.min(1.0, hitCount / ageInDays);
198
+ // Source Multiplier: User verified facts get 1.0, Assistant-generated 0.7
199
+ const sourceMult = metadata.source === 'user' ? 1.0 : 0.7;
200
+ return item.confidence * 0.4 + stn * 0.4 + sourceMult * 0.2;
482
201
  }
483
202
  bloomHash(str) {
484
203
  let hash = 0;
485
204
  for (let i = 0; i < str.length; i++) {
486
205
  hash = (hash << 5) - hash + str.charCodeAt(i);
487
- hash |= 0; // Format to 32bit int
206
+ hash |= 0;
488
207
  }
489
208
  return hash;
490
209
  }
@@ -497,9 +216,7 @@ export class KnowledgeDistiller {
497
216
  status: item.status || 'proposed',
498
217
  sourceSessionId: item.source_session_id,
499
218
  tags: typeof item.tags === 'string' ? JSON.parse(item.tags) : item.tags || [],
500
- metadata: typeof item.metadata === 'string'
501
- ? JSON.parse(item.metadata)
502
- : item.metadata || {},
219
+ metadata: typeof item.metadata === 'string' ? JSON.parse(item.metadata) : item.metadata || {},
503
220
  createdAt: new Date(item.created_at),
504
221
  updatedAt: new Date(item.updated_at),
505
222
  };
@@ -16,7 +16,7 @@ export declare class QuotaManager {
16
16
  /**
17
17
  * Check if a quota is violated for a given target.
18
18
  */
19
- checkQuota(targetType: 'persona' | 'swarm' | 'global', targetId?: string | null): Promise<{
19
+ checkQuota(targetType: 'persona' | 'swarm' | 'global', targetId?: string | null, trxOrDb?: any): Promise<{
20
20
  allowed: boolean;
21
21
  reason?: string;
22
22
  }>;
@@ -21,14 +21,14 @@ export class QuotaManager {
21
21
  /**
22
22
  * Check if a quota is violated for a given target.
23
23
  */
24
- async checkQuota(targetType, targetId = null) {
25
- const quotas = await this.getActiveQuotas(targetType, targetId);
24
+ async checkQuota(targetType, targetId = null, trxOrDb = this.db) {
25
+ const quotas = await this.getActiveQuotas(targetType, targetId, trxOrDb);
26
26
  for (const quota of quotas) {
27
- const usage = await this.getCurrentUsage(quota);
27
+ const usage = await this.getCurrentUsage(quota, trxOrDb);
28
28
  // Currency Conversion Logic: Convert usage to target currency if specified
29
29
  let normalizedUsage = usage;
30
30
  if (quota.metric === 'cost' && quota.metadata?.currency && quota.metadata.currency !== 'USD') {
31
- const rate = await this.getExchangeRate(quota.metadata.currency);
31
+ const rate = await this.getExchangeRate(quota.metadata.currency, trxOrDb);
32
32
  normalizedUsage = usage * rate;
33
33
  }
34
34
  if (normalizedUsage >= quota.limit) {
@@ -46,10 +46,10 @@ export class QuotaManager {
46
46
  /**
47
47
  * Fetches active quotas for a given target.
48
48
  */
49
- async getActiveQuotas(targetType, targetId) {
49
+ async getActiveQuotas(targetType, targetId, trxOrDb = this.db) {
50
50
  const quotas = [];
51
51
  // 1. Fetch from PolicyEnforcer (Global & Type-specific)
52
- const policies = await this.cortex.policies.getActivePolicies();
52
+ const policies = await this.cortex.policies.getActivePolicies(trxOrDb);
53
53
  for (const policy of policies) {
54
54
  if (policy.type === 'budget' &&
55
55
  (policy.definition.targetType === targetType ||
@@ -70,7 +70,7 @@ export class QuotaManager {
70
70
  }
71
71
  // 2. Fetch from Persona metadata (Specific override)
72
72
  if (targetType === 'persona' && targetId) {
73
- const persona = await this.db
73
+ const persona = await trxOrDb
74
74
  .selectFrom(this.config.personasTable || 'agent_personas')
75
75
  .select('metadata')
76
76
  .where('id', '=', targetId)
@@ -89,10 +89,10 @@ export class QuotaManager {
89
89
  /**
90
90
  * Calculates current usage for a specific quota window.
91
91
  */
92
- async getCurrentUsage(quota) {
92
+ async getCurrentUsage(quota, trxOrDb = this.db) {
93
93
  const windowMs = this.getPeriodInMs(quota.period);
94
94
  const startTime = new Date(Date.now() - windowMs);
95
- let query = this.db
95
+ let query = trxOrDb
96
96
  .selectFrom(this.resourcesTable)
97
97
  .where('created_at', '>', startTime);
98
98
  if (quota.targetType === 'persona' && quota.targetId) {
@@ -121,14 +121,14 @@ export class QuotaManager {
121
121
  * Resolves exchange rate via persistent metric store or live update.
122
122
  * Implements a true Oracle pattern for resource normalization.
123
123
  */
124
- async getExchangeRate(currency) {
124
+ async getExchangeRate(currency, trxOrDb = this.db) {
125
125
  // 1. Check in-memory cache
126
126
  const cached = this.rateCache.get(currency);
127
127
  if (cached && Date.now() - cached.timestamp < this.CACHE_TTL) {
128
128
  return cached.rate;
129
129
  }
130
130
  // 2. Check persistent metric store
131
- const record = await this.db
131
+ const record = await trxOrDb
132
132
  .selectFrom(this.metricsTable)
133
133
  .select(['metric_value', 'created_at'])
134
134
  .where('metric_name', '=', `exchange_rate_${currency}`)
@@ -15,21 +15,21 @@ export declare class RecursiveReasoner {
15
15
  /**
16
16
  * Find goals matching a pattern across all sessions
17
17
  */
18
- analyzeGlobalProgress(pattern: string): Promise<AgentGoal[]>;
18
+ analyzeGlobalProgress(pattern: string, trxOrDb?: any): Promise<AgentGoal[]>;
19
19
  /**
20
20
  * Synthesize high-level lessons and cluster similar ones using Token-Weighted significance.
21
21
  * Pass 5: High-Throughput Semantic Analysis.
22
22
  */
23
- synthesizeLessons(): Promise<Record<string, string[]>>;
23
+ synthesizeLessons(trxOrDb?: any): Promise<Record<string, string[]>>;
24
24
  /**
25
25
  * Pass 6: Goal Cross-Pollination
26
26
  * Distill successful persona mutations into global systemic goals.
27
27
  */
28
- crossPollinateGoals(): Promise<number>;
28
+ crossPollinateGoals(trxOrDb?: any): Promise<number>;
29
29
  /**
30
30
  * Detect contradictions in goals (e.g., opposing objectives).
31
31
  */
32
- detectContradictions(): Promise<string[]>;
32
+ detectContradictions(trxOrDb?: any): Promise<string[]>;
33
33
  /**
34
34
  * Register a new conflict pair (e.g. ['buy', 'sell'])
35
35
  */