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
@@ -16,7 +16,7 @@ export class EvolutionaryPilot {
16
16
  /**
17
17
  * Run a self-improvement cycle based on dynamic baselining
18
18
  */
19
- async runSelfImprovementCycle() {
19
+ async runSelfImprovementCycle(trxOrDb = this.db) {
20
20
  console.log('[EvolutionaryPilot] Initiating self-improvement cycle with full-spectrum baselining...');
21
21
  const changes = [];
22
22
  let evolved = false;
@@ -28,7 +28,7 @@ export class EvolutionaryPilot {
28
28
  'trust_signal',
29
29
  ];
30
30
  const samplingCount = this.config.evolution?.samplingCount || 100;
31
- const recentMetrics = await this.cortex.metrics.getRecentMetrics(samplingCount);
31
+ const recentMetrics = await this.cortex.metrics.getRecentMetrics(samplingCount, trxOrDb);
32
32
  for (const metricName of metrics) {
33
33
  // Audit Phase 17: Bounded metric slice for scale-safe baselining
34
34
  const values = recentMetrics
@@ -41,14 +41,14 @@ export class EvolutionaryPilot {
41
41
  const minSamples = this.config.evolution?.minSamples || 5;
42
42
  if (values.length < minSamples)
43
43
  continue;
44
- const policies = await this.cortex.policies.getActivePolicies();
44
+ const policies = await this.cortex.policies.getActivePolicies(trxOrDb);
45
45
  const latencyZ = policies.find(p => p.name === 'latency_drift_z')?.definition?.threshold || 2.0;
46
46
  const latencyMean = policies.find(p => p.name === 'latency_mean_ceiling')?.definition?.threshold || 1000;
47
47
  console.log(`[EvolutionaryPilot] Baselining ${metricName}: Mean=${stats.mean.toFixed(2)}, StdDev=${stats.stdDev.toFixed(2)}, Current=${stats.current.toFixed(2)}, Z-Score=${stats.zScore.toFixed(2)}`);
48
48
  // 2. Trigger Evolution based on metric-specific thresholds
49
49
  if (metricName === 'query_latency' &&
50
50
  (stats.zScore > latencyZ || stats.mean > latencyMean)) {
51
- const result = await this.optimizeLatency();
51
+ const result = await this.optimizeLatency(trxOrDb);
52
52
  if (result) {
53
53
  changes.push(...result);
54
54
  evolved = true;
@@ -59,7 +59,7 @@ export class EvolutionaryPilot {
59
59
  if (metricName === 'success_rate' &&
60
60
  (stats.zScore < successZ || stats.mean < successMean)) {
61
61
  console.warn(`[EvolutionaryPilot] Success rate collapse detected (${stats.mean.toFixed(2)}). Triggering strategic mutation.`);
62
- const strategies = await this.cortex.strategy.mutateStrategy();
62
+ const strategies = await this.cortex.strategy.mutateStrategy(trxOrDb);
63
63
  changes.push(...strategies);
64
64
  evolved = true;
65
65
  }
@@ -72,13 +72,13 @@ export class EvolutionaryPilot {
72
72
  }
73
73
  }
74
74
  // 3. Meta-Meta Evolution Tuning
75
- const tuned = await this.tuneEmergentSkillHyperparameters(recentMetrics);
75
+ const tuned = await this.tuneEmergentSkillHyperparameters(recentMetrics, trxOrDb);
76
76
  if (tuned) {
77
77
  changes.push('Self-tuned emergent skill hyperparameters (Meta-Meta Evolution)');
78
78
  evolved = true;
79
79
  }
80
80
  // 4. Verify: Perform an audit
81
- const audit = await this.cortex.governor.performAudit();
81
+ const audit = await this.cortex.governor.performAudit(trxOrDb);
82
82
  if (!audit.healthy) {
83
83
  console.warn('[EvolutionaryPilot] Evolution resulted in unhealthy state. Reverting may be required.');
84
84
  changes.push('WARNING: Unhealthy state detected after evolution');
@@ -89,11 +89,11 @@ export class EvolutionaryPilot {
89
89
  * Meta-Meta Evolution: Adjusts the hyperparameters of the SkillSynthesizer
90
90
  * based on the systemic success or failure of recently verified skills.
91
91
  */
92
- async tuneEmergentSkillHyperparameters(recentMetrics) {
92
+ async tuneEmergentSkillHyperparameters(recentMetrics, trxOrDb = this.db) {
93
93
  console.log(`[EvolutionaryPilot] Running Meta-Meta Evolution tuning...`);
94
94
  // Count how many skills are currently blacklisted vs verified
95
- const blacklisted = await this.cortex.capabilities.getCapabilities('blacklisted');
96
- const verified = await this.cortex.capabilities.getCapabilities('verified');
95
+ const blacklisted = await this.cortex.capabilities.getCapabilities('blacklisted', trxOrDb);
96
+ const verified = await this.cortex.capabilities.getCapabilities('verified', trxOrDb);
97
97
  let tuned = false;
98
98
  const config = this.config.evolution;
99
99
  if (!config)
@@ -150,21 +150,19 @@ export class EvolutionaryPilot {
150
150
  const zScore = stdDev === 0 ? 0 : (current - mean) / stdDev;
151
151
  return { mean, stdDev, current, zScore };
152
152
  }
153
- async optimizeLatency() {
153
+ async optimizeLatency(trxOrDb = this.db) {
154
154
  const changes = [];
155
155
  console.log(`[EvolutionaryPilot] Triggering latency optimization...`);
156
- const dialect = this.db.getExecutor().dialect;
157
- const isSqlite = this.db
158
- .getExecutor()
159
- .adapter.constructor.name.toLowerCase()
160
- .includes('sqlite') ||
161
- (dialect && dialect.constructor.name.toLowerCase().includes('sqlite'));
156
+ const executor = trxOrDb.getExecutor();
157
+ const adapterName = executor?.adapter?.constructor?.name || executor?.dialect?.constructor?.name || '';
158
+ const isSqlite = adapterName.toLowerCase().includes('sqlite');
162
159
  if (isSqlite) {
163
- await sql `PRAGMA optimize`.execute(this.db);
160
+ await sql `PRAGMA optimize`.execute(trxOrDb);
164
161
  changes.push('Applied PRAGMA optimize');
165
162
  }
166
163
  const messagesTable = this.config.messagesTable || 'agent_messages';
167
- await this.cortex.evolution.evolve(`CREATE INDEX IF NOT EXISTS idx_agent_msg_session_time ON ${messagesTable}(session_id, created_at)`);
164
+ // NOTE: cortex.evolution.evolve should be transaction-aware
165
+ await this.cortex.evolution.evolve(`CREATE INDEX IF NOT EXISTS idx_agent_msg_session_time ON ${messagesTable}(session_id, created_at)`, {}, trxOrDb);
168
166
  changes.push(`Applied composite index to ${messagesTable}`);
169
167
  return changes;
170
168
  }
@@ -1,5 +1,6 @@
1
1
  /// <reference types="./GoalArchitect.d.ts" />
2
2
  import { calculateSimilarity } from '../../util/similarity.js';
3
+ import { withLock } from '../util/db-utils.js';
3
4
  /**
4
5
  * GoalArchitect enables agents to autonomously deconstruct complex
5
6
  * objectives into manageable sub-goals.
@@ -24,11 +25,11 @@ export class GoalArchitect {
24
25
  return await this.db.transaction().execute(async (trx) => {
25
26
  // 1. Audit Phase 9: Circular Dependency Protection inside transaction
26
27
  await this.detectCircularDependency(goalId, new Set(), trx);
27
- const goal = (await trx
28
+ const query = trx
28
29
  .selectFrom(this.goalsTable)
29
30
  .selectAll()
30
- .where('id', '=', goalId)
31
- .forUpdate() // Audit Phase 9: Atomic acquisition
31
+ .where('id', '=', goalId);
32
+ const goal = (await withLock(query, trx) // Audit Phase 9: Atomic acquisition
32
33
  .executeTakeFirst());
33
34
  if (!goal)
34
35
  throw new Error(`Goal ${goalId} not found`);
@@ -99,11 +100,11 @@ export class GoalArchitect {
99
100
  const sortedIds = [...goalIds].sort((a, b) => String(a).localeCompare(String(b)));
100
101
  await this.db.transaction().execute(async (trx) => {
101
102
  // Pre-lock all rows in deterministic order
102
- await trx
103
+ const query = trx
103
104
  .selectFrom(this.goalsTable)
104
105
  .select('id')
105
- .where('id', 'in', sortedIds)
106
- .forUpdate()
106
+ .where('id', 'in', sortedIds);
107
+ await withLock(query, trx)
107
108
  .execute();
108
109
  for (let i = 0; i < goalIds.length; i++) {
109
110
  await trx
@@ -119,11 +120,11 @@ export class GoalArchitect {
119
120
  */
120
121
  async markGoalAs(goalId, status, outcome) {
121
122
  return await this.db.transaction().execute(async (trx) => {
122
- const goal = (await trx
123
+ const query = trx
123
124
  .selectFrom(this.goalsTable)
124
125
  .selectAll()
125
- .where('id', '=', goalId)
126
- .forUpdate() // Audit Phase 9: Atomic status/meta update
126
+ .where('id', '=', goalId);
127
+ const goal = (await withLock(query, trx) // Audit Phase 9: Atomic status/meta update
127
128
  .executeTakeFirst());
128
129
  if (!goal)
129
130
  throw new Error(`Goal ${goalId} not found`);
@@ -26,24 +26,24 @@ export declare class GovernanceManager {
26
26
  /**
27
27
  * Perform a "Panic Check" - looking for critical failures or cost overruns
28
28
  */
29
- performAudit(): Promise<{
29
+ performAudit(trxOrDb?: any): Promise<{
30
30
  healthy: boolean;
31
31
  issues: string[];
32
32
  }>;
33
33
  /**
34
34
  * Suggest architectural repairs if performance is degrading
35
35
  */
36
- suggestRepairs(): Promise<string[]>;
36
+ suggestRepairs(trxOrDb?: any): Promise<string[]>;
37
37
  /**
38
38
  * Quarantine a persona that is behaving outside safety parameters.
39
39
  */
40
- quarantinePersona(id: string | number, reason: string): Promise<void>;
40
+ quarantinePersona(id: string | number, reason: string, trxOrDb?: any): Promise<void>;
41
41
  /**
42
42
  * Blacklist a skill that is causing systemic issues.
43
43
  */
44
- quarantineSkill(name: string, reason: string): Promise<void>;
44
+ quarantineSkill(name: string, reason: string, trxOrDb?: any): Promise<void>;
45
45
  /**
46
46
  * Monitor cross-node behaviors and flag sudden spikes or malicious patterns.
47
47
  */
48
- validateEmergentBehavior(trx?: any): Promise<string[]>;
48
+ validateEmergentBehavior(trxOrDb?: any): Promise<string[]>;
49
49
  }
@@ -46,13 +46,13 @@ export class GovernanceManager {
46
46
  /**
47
47
  * Perform a "Panic Check" - looking for critical failures or cost overruns
48
48
  */
49
- async performAudit() {
49
+ async performAudit(trxOrDb = this.db) {
50
50
  const issuesList = [];
51
51
  let auditMetadata = {};
52
52
  // Execute core audit gathering phase
53
53
  const ctx = {
54
54
  db: this.db,
55
- trx: this.db,
55
+ trx: trxOrDb,
56
56
  cortex: this.cortex,
57
57
  config: this.config,
58
58
  metricsTable: this.metricsTable,
@@ -90,21 +90,11 @@ export class GovernanceManager {
90
90
  // Phase 1: Emergency Rollbacks
91
91
  if (activePersona && (success < 0.4 || hCost > hourlyLimit * 1.5)) {
92
92
  console.error(`[GovernanceManager] CRITICAL THRESHOLD BREACH. Initiating emergency containment for persona ${activePersona.id}`);
93
- await this.personaAuditor.quarantinePersona({ db: this.db, cortex: this.cortex }, activePersona.id, 'Critical threshold breach');
93
+ await this.personaAuditor.quarantinePersona(ctx, activePersona.id, 'Critical threshold breach');
94
94
  issuesList.push(`Containment: Emergency rollback triggered for persona ${activePersona.id}`);
95
95
  }
96
- await this.cortex.reflections.reflect(null, 'failure', 'Governance Compliance Audit', issuesList);
96
+ await this.cortex.reflections.reflect('system', 'failure', 'Governance Compliance Audit', issuesList, undefined, trxOrDb);
97
97
  // Phase 3: Remediation Rituals
98
- const ctx = {
99
- db: this.db,
100
- trx: this.db, // Standalone remediation
101
- cortex: this.cortex,
102
- config: this.config,
103
- metricsTable: this.metricsTable,
104
- policiesTable: this.policiesTable,
105
- personasTable: this.personasTable,
106
- skillsTable: this.skillsTable
107
- };
108
98
  await this.remediationEngine.triggerRemediation(ctx, issuesList);
109
99
  }
110
100
  return {
@@ -115,10 +105,10 @@ export class GovernanceManager {
115
105
  /**
116
106
  * Suggest architectural repairs if performance is degrading
117
107
  */
118
- async suggestRepairs() {
108
+ async suggestRepairs(trxOrDb = this.db) {
119
109
  const ctx = {
120
110
  db: this.db,
121
- trx: this.db,
111
+ trx: trxOrDb,
122
112
  cortex: this.cortex,
123
113
  config: this.config,
124
114
  metricsTable: this.metricsTable,
@@ -131,10 +121,10 @@ export class GovernanceManager {
131
121
  /**
132
122
  * Quarantine a persona that is behaving outside safety parameters.
133
123
  */
134
- async quarantinePersona(id, reason) {
124
+ async quarantinePersona(id, reason, trxOrDb = this.db) {
135
125
  const ctx = {
136
126
  db: this.db,
137
- trx: this.db,
127
+ trx: trxOrDb,
138
128
  cortex: this.cortex,
139
129
  config: this.config,
140
130
  metricsTable: this.metricsTable,
@@ -147,10 +137,10 @@ export class GovernanceManager {
147
137
  /**
148
138
  * Blacklist a skill that is causing systemic issues.
149
139
  */
150
- async quarantineSkill(name, reason) {
140
+ async quarantineSkill(name, reason, trxOrDb = this.db) {
151
141
  const ctx = {
152
142
  db: this.db,
153
- trx: this.db,
143
+ trx: trxOrDb,
154
144
  cortex: this.cortex,
155
145
  config: this.config,
156
146
  metricsTable: this.metricsTable,
@@ -163,10 +153,10 @@ export class GovernanceManager {
163
153
  /**
164
154
  * Monitor cross-node behaviors and flag sudden spikes or malicious patterns.
165
155
  */
166
- async validateEmergentBehavior(trx) {
156
+ async validateEmergentBehavior(trxOrDb = this.db) {
167
157
  const ctx = {
168
158
  db: this.db,
169
- trx: trx || this.db,
159
+ trx: trxOrDb,
170
160
  cortex: this.cortex,
171
161
  config: this.config,
172
162
  metricsTable: this.metricsTable,
@@ -10,11 +10,13 @@ export declare class HiveLink {
10
10
  private cortex;
11
11
  private config;
12
12
  private knowledgeTable;
13
+ private promoter;
14
+ private propagator;
15
+ private domainMaster;
13
16
  constructor(db: Kysely<any>, cortex: Cortex, config?: AgenticConfig);
14
17
  private get typedDb();
15
18
  /**
16
19
  * Promote high-confidence local knowledge to global "Hive" knowledge.
17
- * This creates a new, session-agnostic entry or updates an existing global one.
18
20
  */
19
21
  broadcastKnowledge(options?: {
20
22
  minConfidence?: number;
@@ -23,12 +25,10 @@ export declare class HiveLink {
23
25
  }): Promise<number>;
24
26
  /**
25
27
  * Strengthen knowledge related to a specific domain (tag).
26
- * Increases confidence of all items with this tag, representing "domain mastery".
27
28
  */
28
29
  syncDomain(domainTag: string, boostFactor?: number): Promise<number>;
29
30
  /**
30
31
  * Propagate high-performing capabilities globally using the "Sovereign Draft" protocol.
31
- * Uses Bayesian Convergence and Shadow Promotion to avoid deadlocks.
32
32
  */
33
33
  broadcastSkills(): Promise<number>;
34
34
  /**
@@ -1,5 +1,7 @@
1
1
  /// <reference types="./HiveLink.d.ts" />
2
- import { sql } from '../../raw-builder/sql.js';
2
+ import { KnowledgePromoter } from './hive/KnowledgePromoter.js';
3
+ import { SkillPropagator } from './hive/SkillPropagator.js';
4
+ import { DomainMaster } from './hive/DomainMaster.js';
3
5
  /**
4
6
  * HiveLink facilitates "Collective Intelligence" by synchronizing
5
7
  * knowledge and lessons learned across different agent personas.
@@ -9,108 +11,53 @@ export class HiveLink {
9
11
  cortex;
10
12
  config;
11
13
  knowledgeTable;
14
+ promoter;
15
+ propagator;
16
+ domainMaster;
12
17
  constructor(db, cortex, config = {}) {
13
18
  this.db = db;
14
19
  this.cortex = cortex;
15
20
  this.config = config;
16
21
  this.knowledgeTable = config.knowledgeTable || 'agent_knowledge_base';
22
+ this.promoter = new KnowledgePromoter();
23
+ this.propagator = new SkillPropagator();
24
+ this.domainMaster = new DomainMaster();
17
25
  }
18
26
  get typedDb() {
19
27
  return this.db;
20
28
  }
21
29
  /**
22
30
  * Promote high-confidence local knowledge to global "Hive" knowledge.
23
- * This creates a new, session-agnostic entry or updates an existing global one.
24
31
  */
25
32
  async broadcastKnowledge(options = {}) {
26
33
  const minConfidence = options.minConfidence ?? 0.9;
27
34
  const limit = options.limit ?? 100;
28
35
  const offset = options.offset ?? 0;
29
36
  console.log(`[HiveLink] Broadcasting knowledge (limit=${limit}, offset=${offset}) with confidence >= ${minConfidence}`);
30
- // Find high-confidence items that are "local" (have a source_session_id)
31
37
  const items = (await this.typedDb
32
38
  .selectFrom(this.knowledgeTable)
33
39
  .selectAll()
34
40
  .where('confidence', '>=', minConfidence)
35
- .where('source_session_id', 'is not', null) // Only local items
41
+ .where('source_session_id', 'is not', null)
36
42
  .limit(limit)
37
43
  .offset(offset)
38
44
  .execute());
39
45
  let promotedCount = 0;
40
46
  for (const item of items) {
41
- await this.db.transaction().execute(async (trx) => {
42
- // Check if a global version already exists
43
- const existingGlobal = await trx
44
- .selectFrom(this.knowledgeTable)
45
- .selectAll()
46
- .where('entity', '=', item.entity)
47
- .where('fact', '=', item.fact)
48
- .where('source_session_id', 'is', null)
49
- .forUpdate() // Audit Phase 15: prevent concurrent promotion duplication
50
- .executeTakeFirst();
51
- if (existingGlobal) {
52
- // Reinforce existing global knowledge
53
- const newConfidence = Math.min(0.99, Math.max(existingGlobal.confidence, item.confidence) + 0.01);
54
- await trx
55
- .updateTable(this.knowledgeTable)
56
- .set({
57
- confidence: newConfidence,
58
- updated_at: new Date(),
59
- })
60
- .where('id', '=', existingGlobal.id)
61
- .execute();
62
- }
63
- else {
64
- // Create new global knowledge
65
- const tags = item.tags ? [...item.tags] : [];
66
- if (!tags.includes('hive_mind'))
67
- tags.push('hive_mind');
68
- await trx
69
- .insertInto(this.knowledgeTable)
70
- .values({
71
- entity: item.entity,
72
- fact: item.fact,
73
- confidence: item.confidence,
74
- source_session_id: null,
75
- tags: JSON.stringify(tags),
76
- metadata: JSON.stringify({
77
- ...item.metadata,
78
- promoted_from: item.id,
79
- promoted_at: new Date(),
80
- }),
81
- created_at: new Date(),
82
- updated_at: new Date(),
83
- })
84
- .execute();
85
- promotedCount++;
86
- }
87
- });
47
+ const promoted = await this.promoter.promote(this.db, this.cortex, this.config, this.knowledgeTable, item);
48
+ if (promoted)
49
+ promotedCount++;
88
50
  }
89
51
  return promotedCount;
90
52
  }
91
53
  /**
92
54
  * Strengthen knowledge related to a specific domain (tag).
93
- * Increases confidence of all items with this tag, representing "domain mastery".
94
55
  */
95
56
  async syncDomain(domainTag, boostFactor = 0.05) {
96
- console.log(`[HiveLink] Syncing/Boosting domain '${domainTag}' (Set-Based)`);
97
- // Use a single SQL update for high throughput
98
- const result = await this.db
99
- .updateTable(this.knowledgeTable)
100
- .set({
101
- confidence: sql `MIN(1.0, confidence + ${boostFactor})`,
102
- updated_at: new Date(),
103
- })
104
- .where('tags', 'like', `%"${domainTag}"%`)
105
- .where('confidence', '<', 1.0)
106
- .execute();
107
- // Kysely update .execute() returns an array of results or similar depending on adapter
108
- // For simple update, we might just return the count if supported or 1
109
- return Number(result[0]?.numUpdatedRows ?? 1);
57
+ return this.domainMaster.boostDomain(this.db, this.knowledgeTable, domainTag, boostFactor);
110
58
  }
111
59
  /**
112
60
  * Propagate high-performing capabilities globally using the "Sovereign Draft" protocol.
113
- * Uses Bayesian Convergence and Shadow Promotion to avoid deadlocks.
114
61
  */
115
62
  async broadcastSkills() {
116
63
  if (!this.config.evolution?.enableHiveLink &&
@@ -119,96 +66,7 @@ export class HiveLink {
119
66
  return 0;
120
67
  }
121
68
  console.log(`[HiveLink] Executing Sovereign Draft for emergent skills...`);
122
- let broadcastCount = 0;
123
- const capTable = this.config.capabilitiesTable || 'agent_capabilities';
124
- // 1. Resolve Verified Skills with Bayesian "Alpha" selection
125
- const verifiedSkills = await this.cortex.capabilities.getCapabilities('verified');
126
- // Group verified skills by lineage (base tool name)
127
- const lineageGroups = new Map();
128
- for (const skill of verifiedSkills) {
129
- const meta = typeof skill.metadata === 'string' ? JSON.parse(skill.metadata) : (skill.metadata || {});
130
- const lineage = meta.lineage || skill.name;
131
- if (!lineageGroups.has(lineage))
132
- lineageGroups.set(lineage, []);
133
- lineageGroups.get(lineage).push({ skill, meta });
134
- }
135
- for (const [lineage, variants] of lineageGroups.entries()) {
136
- // Bayesian Winner Selection
137
- // Score = (anchored_reliability * totalCount + K * prior) / (totalCount + K)
138
- // For simplicity, we use anchored_reliability which already incorporates this weighting
139
- const alphaCandidate = variants.reduce((prev, curr) => {
140
- const pAnchored = prev.meta.anchored_reliability ?? 0;
141
- const cAnchored = curr.meta.anchored_reliability ?? 0;
142
- return cAnchored > pAnchored ? curr : prev;
143
- });
144
- // Non-Blocking Set Update: Flag Alpha and Shadow versions
145
- await this.db.transaction().execute(async (trx) => {
146
- const currentMeta = alphaCandidate.meta || {};
147
- const updatedMeta = {
148
- ...currentMeta,
149
- is_alpha: true,
150
- broadcasted: true,
151
- broadcasted_at: new Date()
152
- };
153
- // Flag the winner as Alpha
154
- // Audit Phase 15: Lock candidate before promotion
155
- const winner = await trx
156
- .selectFrom(capTable)
157
- .select('id')
158
- .where('id', '=', alphaCandidate.skill.id)
159
- .forUpdate()
160
- .executeTakeFirst();
161
- if (winner) {
162
- await trx
163
- .updateTable(capTable)
164
- .set({
165
- metadata: JSON.stringify(updatedMeta),
166
- })
167
- .where('id', '=', winner.id)
168
- .execute();
169
- }
170
- // Flag others in the same lineage as Shadow (Dialect-safe approach)
171
- const shadowIds = variants
172
- .filter(v => v.skill.id !== alphaCandidate.skill.id)
173
- .map(v => v.skill.id);
174
- if (shadowIds.length > 0) {
175
- // We fetch and update individually or use a bulk update if the dialect supports complex JSON manipulation.
176
- // For maximum compatibility (Postgres/SQLite), we do it in a loop for the shadows if the count is small.
177
- for (const sid of shadowIds) {
178
- const sMatch = variants.find(v => v.skill.id === sid);
179
- const sMeta = sMatch?.meta || {};
180
- await trx.updateTable(capTable)
181
- .set({
182
- metadata: JSON.stringify({ ...sMeta, is_alpha: false, is_shadow: true }),
183
- status: 'experimental'
184
- })
185
- .where('id', '=', sid)
186
- .execute();
187
- }
188
- }
189
- });
190
- broadcastCount += variants.length;
191
- }
192
- // 2. Broadcast Blacklisted Skills (Immune Prophet)
193
- const blacklisted = await this.cortex.capabilities.getCapabilities('blacklisted');
194
- const blackIDs = blacklisted
195
- .filter(s => {
196
- const meta = typeof s.metadata === 'string' ? JSON.parse(s.metadata) : (s.metadata || {});
197
- return !meta.broadcasted;
198
- })
199
- .map(s => s.id);
200
- if (blackIDs.length > 0) {
201
- await this.db
202
- .updateTable(capTable)
203
- .set({
204
- metadata: sql `json_set(metadata, '$.broadcasted', true, '$.hive_blacklisted', true)`,
205
- updated_at: new Date()
206
- })
207
- .where('id', 'in', blackIDs)
208
- .execute();
209
- broadcastCount += blackIDs.length;
210
- }
211
- return broadcastCount;
69
+ return this.propagator.propagate(this.db, this.cortex, this.config);
212
70
  }
213
71
  /**
214
72
  * Simple semver-style version comparison.
@@ -35,50 +35,33 @@ export declare class KnowledgeDistiller {
35
35
  private knowledgeTable;
36
36
  private linksTable;
37
37
  private bloomFilter;
38
- constructor(db: Kysely<any>, // accepting any Kysely but casting internally for our specific tables
39
- config?: AgenticConfig);
40
- /**
41
- * typedDb helper to cast the generic DB to our specific schema
42
- */
43
- private get typedDb();
38
+ private distiller;
39
+ private challenger;
40
+ private architect;
41
+ private consolidator;
42
+ constructor(db: Kysely<any>, config?: AgenticConfig);
44
43
  /**
45
44
  * Add or update a knowledge item with robust merging and transaction support.
46
45
  */
47
46
  distill(entity: string, fact: string, confidence: number, sourceSessionId?: string | number, tags?: string[], metadata?: Record<string, any>, source?: 'user' | 'assistant' | 'system'): Promise<KnowledgeItem>;
47
+ /**
48
+ * Manually challenge an existing fact with competing information.
49
+ */
50
+ challengeKnowledge(entity: string, fact: string, confidence: number, trxOrDb?: any): Promise<void>;
48
51
  /**
49
52
  * Verify and reinforce existing knowledge.
50
- * Increases confidence if the fact matches.
51
53
  */
52
54
  verifyKnowledge(id: number | string, reinforcement?: number): Promise<KnowledgeItem | null>;
53
55
  /**
54
56
  * Search knowledge by entity with optional tag filtering.
55
- * Records a "hit" for each retrieved item to track utility.
56
57
  */
57
58
  getKnowledgeByEntity(entity: string, filterTags?: string[]): Promise<KnowledgeItem[]>;
58
59
  /**
59
- * Record a retrieval hit for a knowledge item.
60
+ * Record a retrieval hit for a knowledge item and emit hotspot telemetry.
60
61
  */
61
62
  recordHit(id: number | string): Promise<void>;
62
- /**
63
- * Calculate the "Fitness" of a memory item.
64
- * Score = (Confidence * 0.4) + (SignalToNoise * 0.4) + (SourceMultiplier * 0.2)
65
- */
66
- calculateFitness(item: KnowledgeItem): number;
67
- /**
68
- * Challenge existing knowledge with new evidence.
69
- * If confidence of new fact is high and contradicts existing ones (same entity, different fact),
70
- * we degrade the confidence of the old facts.
71
- */
72
- challengeKnowledge(entity: string, competingFact: string, confidence: number, trxOrDb?: any): Promise<void>;
73
- /**
74
- * Automatic Semantic Linking
75
- * Scans for entity mentions in facts and creates links.
76
- * Production Hardening: Uses batched cross-similarity and NER-style tokenization.
77
- */
78
- private autoLinkKnowledge;
79
63
  /**
80
64
  * Link two knowledge items together with a relationship.
81
- * Transaction-aware version.
82
65
  */
83
66
  linkKnowledge(sourceId: number | string, targetId: number | string, relationship: string, metadata?: Record<string, any>, trxOrDb?: any): Promise<void>;
84
67
  /**
@@ -97,10 +80,13 @@ export declare class KnowledgeDistiller {
97
80
  pruneLowConfidence(threshold?: number): Promise<number>;
98
81
  /**
99
82
  * Consolidate knowledge by merging similar facts for the same entity.
100
- * Production Hardening: Iterative bucketed approach to avoid recursive stack issues.
101
83
  */
102
84
  consolidateKnowledge(): Promise<number>;
103
- private mergeItems;
85
+ /**
86
+ * Calculate the "Fitness" of a knowledge item.
87
+ * Score = (Confidence * 0.4) + (SignalToNoise * 0.4) + (SourceMultiplier * 0.2)
88
+ */
89
+ calculateFitness(item: KnowledgeItem): number;
104
90
  private bloomHash;
105
91
  private parseKnowledge;
106
92
  }