noormme 1.2.3 → 1.2.5

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