noormme 1.2.0 → 1.2.1

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 (132) hide show
  1. package/README.md +60 -6
  2. package/dist/cjs/agentic/ActionJournal.d.ts +5 -2
  3. package/dist/cjs/agentic/ActionJournal.js +13 -5
  4. package/dist/cjs/agentic/CapabilityManager.d.ts +7 -0
  5. package/dist/cjs/agentic/CapabilityManager.js +84 -7
  6. package/dist/cjs/agentic/CognitiveRepository.js +3 -6
  7. package/dist/cjs/agentic/Cortex.d.ts +4 -0
  8. package/dist/cjs/agentic/Cortex.js +38 -17
  9. package/dist/cjs/agentic/EpisodicMemory.d.ts +5 -1
  10. package/dist/cjs/agentic/EpisodicMemory.js +11 -4
  11. package/dist/cjs/agentic/PersonaManager.js +37 -31
  12. package/dist/cjs/agentic/PolicyEnforcer.d.ts +6 -1
  13. package/dist/cjs/agentic/PolicyEnforcer.js +74 -17
  14. package/dist/cjs/agentic/ResourceMonitor.d.ts +9 -0
  15. package/dist/cjs/agentic/ResourceMonitor.js +36 -2
  16. package/dist/cjs/agentic/SessionManager.js +24 -17
  17. package/dist/cjs/agentic/VectorIndexer.d.ts +1 -0
  18. package/dist/cjs/agentic/VectorIndexer.js +26 -17
  19. package/dist/cjs/agentic/improvement/AblationEngine.d.ts +4 -6
  20. package/dist/cjs/agentic/improvement/AblationEngine.js +57 -37
  21. package/dist/cjs/agentic/improvement/ActionRefiner.js +30 -14
  22. package/dist/cjs/agentic/improvement/ConflictResolver.d.ts +3 -1
  23. package/dist/cjs/agentic/improvement/ConflictResolver.js +59 -47
  24. package/dist/cjs/agentic/improvement/CortexJanitor.js +11 -0
  25. package/dist/cjs/agentic/improvement/CuriosityEngine.d.ts +1 -1
  26. package/dist/cjs/agentic/improvement/CuriosityEngine.js +48 -21
  27. package/dist/cjs/agentic/improvement/EvolutionRitual.js +26 -14
  28. package/dist/cjs/agentic/improvement/EvolutionaryPilot.js +16 -4
  29. package/dist/cjs/agentic/improvement/GoalArchitect.d.ts +6 -2
  30. package/dist/cjs/agentic/improvement/GoalArchitect.js +72 -34
  31. package/dist/cjs/agentic/improvement/GovernanceManager.d.ts +9 -3
  32. package/dist/cjs/agentic/improvement/GovernanceManager.js +232 -92
  33. package/dist/cjs/agentic/improvement/HiveLink.d.ts +7 -3
  34. package/dist/cjs/agentic/improvement/HiveLink.js +135 -113
  35. package/dist/cjs/agentic/improvement/KnowledgeDistiller.js +43 -35
  36. package/dist/cjs/agentic/improvement/QuotaManager.d.ts +41 -0
  37. package/dist/cjs/agentic/improvement/QuotaManager.js +185 -0
  38. package/dist/cjs/agentic/improvement/RecursiveReasoner.js +50 -25
  39. package/dist/cjs/agentic/improvement/ReflectionEngine.d.ts +4 -1
  40. package/dist/cjs/agentic/improvement/ReflectionEngine.js +3 -1
  41. package/dist/cjs/agentic/improvement/RitualOrchestrator.js +27 -16
  42. package/dist/cjs/agentic/improvement/RuleEngine.d.ts +1 -1
  43. package/dist/cjs/agentic/improvement/RuleEngine.js +10 -4
  44. package/dist/cjs/agentic/improvement/SelfEvolution.js +21 -17
  45. package/dist/cjs/agentic/improvement/SelfTestRegistry.d.ts +5 -0
  46. package/dist/cjs/agentic/improvement/SelfTestRegistry.js +129 -109
  47. package/dist/cjs/agentic/improvement/SkillSynthesizer.d.ts +1 -0
  48. package/dist/cjs/agentic/improvement/SkillSynthesizer.js +22 -13
  49. package/dist/cjs/agentic/improvement/StrategicPlanner.d.ts +1 -0
  50. package/dist/cjs/agentic/improvement/StrategicPlanner.js +26 -19
  51. package/dist/cjs/agentic/telemetry/CognitiveSynthesizer.d.ts +5 -0
  52. package/dist/cjs/agentic/telemetry/CognitiveSynthesizer.js +54 -12
  53. package/dist/cjs/agentic/telemetry/EventHarvester.d.ts +1 -1
  54. package/dist/cjs/agentic/telemetry/EventHarvester.js +10 -3
  55. package/dist/cjs/agentic/telemetry/ResearchAlchemist.d.ts +7 -2
  56. package/dist/cjs/agentic/telemetry/ResearchAlchemist.js +49 -8
  57. package/dist/cjs/agentic/telemetry/TelemetryOrchestrator.d.ts +4 -1
  58. package/dist/cjs/agentic/telemetry/TelemetryOrchestrator.js +38 -11
  59. package/dist/cjs/cli/commands/inspect.js +40 -1
  60. package/dist/cjs/cli/commands/watch.js +31 -25
  61. package/dist/cjs/dialect/sqlite/sqlite-introspector.js +15 -5
  62. package/dist/cjs/helpers/agent-schema.js +1 -0
  63. package/dist/cjs/migration/data_migrator.js +4 -4
  64. package/dist/cjs/migration/schema_differ.js +37 -15
  65. package/dist/cjs/types/index.d.ts +12 -0
  66. package/dist/cjs/util/safe-sql-helpers.js +7 -10
  67. package/dist/esm/agentic/ActionJournal.d.ts +5 -2
  68. package/dist/esm/agentic/ActionJournal.js +13 -5
  69. package/dist/esm/agentic/CapabilityManager.d.ts +7 -0
  70. package/dist/esm/agentic/CapabilityManager.js +84 -7
  71. package/dist/esm/agentic/CognitiveRepository.js +3 -6
  72. package/dist/esm/agentic/Cortex.d.ts +4 -0
  73. package/dist/esm/agentic/Cortex.js +38 -17
  74. package/dist/esm/agentic/EpisodicMemory.d.ts +5 -1
  75. package/dist/esm/agentic/EpisodicMemory.js +11 -4
  76. package/dist/esm/agentic/PersonaManager.js +37 -31
  77. package/dist/esm/agentic/PolicyEnforcer.d.ts +6 -1
  78. package/dist/esm/agentic/PolicyEnforcer.js +74 -17
  79. package/dist/esm/agentic/ResourceMonitor.d.ts +9 -0
  80. package/dist/esm/agentic/ResourceMonitor.js +36 -2
  81. package/dist/esm/agentic/SessionManager.js +24 -17
  82. package/dist/esm/agentic/VectorIndexer.d.ts +1 -0
  83. package/dist/esm/agentic/VectorIndexer.js +26 -17
  84. package/dist/esm/agentic/improvement/AblationEngine.d.ts +4 -6
  85. package/dist/esm/agentic/improvement/AblationEngine.js +57 -37
  86. package/dist/esm/agentic/improvement/ActionRefiner.js +30 -14
  87. package/dist/esm/agentic/improvement/ConflictResolver.d.ts +3 -1
  88. package/dist/esm/agentic/improvement/ConflictResolver.js +59 -47
  89. package/dist/esm/agentic/improvement/CortexJanitor.js +11 -0
  90. package/dist/esm/agentic/improvement/CuriosityEngine.d.ts +1 -1
  91. package/dist/esm/agentic/improvement/CuriosityEngine.js +48 -21
  92. package/dist/esm/agentic/improvement/EvolutionRitual.js +26 -14
  93. package/dist/esm/agentic/improvement/EvolutionaryPilot.js +16 -4
  94. package/dist/esm/agentic/improvement/GoalArchitect.d.ts +6 -2
  95. package/dist/esm/agentic/improvement/GoalArchitect.js +72 -34
  96. package/dist/esm/agentic/improvement/GovernanceManager.d.ts +9 -3
  97. package/dist/esm/agentic/improvement/GovernanceManager.js +232 -92
  98. package/dist/esm/agentic/improvement/HiveLink.d.ts +7 -3
  99. package/dist/esm/agentic/improvement/HiveLink.js +135 -113
  100. package/dist/esm/agentic/improvement/KnowledgeDistiller.js +43 -35
  101. package/dist/esm/agentic/improvement/QuotaManager.d.ts +41 -0
  102. package/dist/esm/agentic/improvement/QuotaManager.js +182 -0
  103. package/dist/esm/agentic/improvement/RecursiveReasoner.js +50 -25
  104. package/dist/esm/agentic/improvement/ReflectionEngine.d.ts +4 -1
  105. package/dist/esm/agentic/improvement/ReflectionEngine.js +3 -1
  106. package/dist/esm/agentic/improvement/RitualOrchestrator.js +27 -16
  107. package/dist/esm/agentic/improvement/RuleEngine.d.ts +1 -1
  108. package/dist/esm/agentic/improvement/RuleEngine.js +10 -4
  109. package/dist/esm/agentic/improvement/SelfEvolution.js +21 -17
  110. package/dist/esm/agentic/improvement/SelfTestRegistry.d.ts +5 -0
  111. package/dist/esm/agentic/improvement/SelfTestRegistry.js +129 -109
  112. package/dist/esm/agentic/improvement/SkillSynthesizer.d.ts +1 -0
  113. package/dist/esm/agentic/improvement/SkillSynthesizer.js +22 -13
  114. package/dist/esm/agentic/improvement/StrategicPlanner.d.ts +1 -0
  115. package/dist/esm/agentic/improvement/StrategicPlanner.js +26 -19
  116. package/dist/esm/agentic/telemetry/CognitiveSynthesizer.d.ts +5 -0
  117. package/dist/esm/agentic/telemetry/CognitiveSynthesizer.js +54 -12
  118. package/dist/esm/agentic/telemetry/EventHarvester.d.ts +1 -1
  119. package/dist/esm/agentic/telemetry/EventHarvester.js +10 -3
  120. package/dist/esm/agentic/telemetry/ResearchAlchemist.d.ts +7 -2
  121. package/dist/esm/agentic/telemetry/ResearchAlchemist.js +49 -8
  122. package/dist/esm/agentic/telemetry/TelemetryOrchestrator.d.ts +4 -1
  123. package/dist/esm/agentic/telemetry/TelemetryOrchestrator.js +38 -11
  124. package/dist/esm/cli/commands/inspect.js +40 -1
  125. package/dist/esm/cli/commands/watch.js +31 -25
  126. package/dist/esm/dialect/sqlite/sqlite-introspector.js +15 -5
  127. package/dist/esm/helpers/agent-schema.js +1 -0
  128. package/dist/esm/migration/data_migrator.js +4 -4
  129. package/dist/esm/migration/schema_differ.js +37 -15
  130. package/dist/esm/types/index.d.ts +12 -0
  131. package/dist/esm/util/safe-sql-helpers.js +7 -10
  132. package/package.json +1 -1
@@ -69,12 +69,14 @@ export class RecursiveReasoner {
69
69
  const clusters = {};
70
70
  // Global Token Frequency Pass
71
71
  const globalTokenFreq = new Map();
72
- for (const lesson of rawLessons) {
72
+ const maxItems = this.config.maxSynthesisItems || 500;
73
+ const limitedResults = rawLessons.slice(0, maxItems);
74
+ for (const lesson of limitedResults) {
73
75
  for (const token of this.tokenize(lesson)) {
74
76
  globalTokenFreq.set(token, (globalTokenFreq.get(token) || 0) + 1);
75
77
  }
76
78
  }
77
- for (const lesson of rawLessons) {
79
+ for (const lesson of limitedResults) {
78
80
  const tokens = this.tokenize(lesson);
79
81
  if (tokens.length === 0)
80
82
  continue;
@@ -106,30 +108,34 @@ export class RecursiveReasoner {
106
108
  .selectAll()
107
109
  .where('metadata', 'like', '%"evolution_status":"stable"%')
108
110
  .execute();
111
+ // Audit Phase 14: Linear Batch Check
112
+ const descriptions = breakthroughs
113
+ .map((p) => {
114
+ const meta = JSON.parse(p.metadata || '{}');
115
+ const reasoning = meta.mutation_reasoning || meta.reasoning;
116
+ return reasoning ? `Systemic Best-Practice: ${reasoning}` : null;
117
+ })
118
+ .filter(Boolean);
119
+ if (descriptions.length === 0)
120
+ return 0;
121
+ const existingGoals = await this.typedDb
122
+ .selectFrom(goalsTable)
123
+ .select('description')
124
+ .where('description', 'in', descriptions)
125
+ .execute();
126
+ const existingSet = new Set(existingGoals.map((g) => g.description));
109
127
  let goalsCreated = 0;
110
- for (const p of breakthroughs) {
111
- const persona = p;
112
- const metadata = JSON.parse(persona.metadata || '{}');
113
- const reasoning = metadata.mutation_reasoning || metadata.reasoning;
114
- if (!reasoning)
115
- continue;
116
- // Check if this goal already exists to avoid duplication
117
- const existing = await this.typedDb
118
- .selectFrom(goalsTable)
119
- .select('id')
120
- .where('description', 'like', `%${reasoning.slice(0, 50)}%`)
121
- .executeTakeFirst();
122
- if (!existing) {
123
- console.log(`[RecursiveReasoner] Distilling breakthrough from Persona ${persona.id} into Global Goal...`);
128
+ for (const desc of descriptions) {
129
+ if (!existingSet.has(desc)) {
130
+ console.log(`[RecursiveReasoner] Distilling breakthrough into Global Goal: ${desc.slice(0, 50)}...`);
124
131
  await this.typedDb
125
132
  .insertInto(goalsTable)
126
133
  .values({
127
134
  session_id: 0, // System-level goal
128
- description: `Systemic Best-Practice: ${reasoning}`,
135
+ description: desc,
129
136
  status: 'pending',
130
137
  priority: 5,
131
138
  metadata: JSON.stringify({
132
- source_persona: persona.id,
133
139
  cross_pollinated: true,
134
140
  }),
135
141
  created_at: new Date(),
@@ -152,15 +158,34 @@ export class RecursiveReasoner {
152
158
  .where('status', '=', 'active')
153
159
  .execute());
154
160
  const contradictions = [];
155
- for (let i = 0; i < activeGoals.length; i++) {
156
- for (let j = i + 1; j < activeGoals.length; j++) {
157
- const g1 = activeGoals[i];
158
- const g2 = activeGoals[j];
159
- const conflict = this.checkConflict(g1.description, g2.description);
160
- if (conflict) {
161
- contradictions.push(`Goal Conflict: "${g1.description}" opposes "${g2.description}" regarding '${conflict}'`);
161
+ const subjectMap = new Map();
162
+ // Audit Phase 7: O(N) Linear Contradiction Detection
163
+ for (const goal of activeGoals) {
164
+ const tokens = this.tokenize(goal.description);
165
+ for (const token of tokens) {
166
+ if (!subjectMap.has(token))
167
+ subjectMap.set(token, []);
168
+ subjectMap.get(token).push({ desc: goal.description, keywords: new Set(tokens) });
169
+ }
170
+ }
171
+ for (const [subject, entries] of subjectMap.entries()) {
172
+ if (entries.length < 2)
173
+ continue;
174
+ for (let i = 0; i < entries.length; i++) {
175
+ for (let j = i + 1; j < entries.length; j++) {
176
+ const conflict = this.checkConflict(entries[i].desc, entries[j].desc);
177
+ if (conflict) {
178
+ contradictions.push(`Goal Conflict: "${entries[i].desc}" opposes "${entries[j].desc}" regarding '${conflict}'`);
179
+ // Limit duplicates for the same pair
180
+ if (contradictions.length > 50)
181
+ break;
182
+ }
162
183
  }
184
+ if (contradictions.length > 50)
185
+ break;
163
186
  }
187
+ if (contradictions.length > 50)
188
+ break;
164
189
  }
165
190
  return contradictions;
166
191
  }
@@ -16,7 +16,10 @@ export declare class ReflectionEngine {
16
16
  /**
17
17
  * Get reflections for a specific session
18
18
  */
19
- getSessionReflections(sessionId: string | number): Promise<AgentReflection[]>;
19
+ getSessionReflections(sessionId: string | number, options?: {
20
+ limit?: number;
21
+ offset?: number;
22
+ }): Promise<AgentReflection[]>;
20
23
  /**
21
24
  * Get recent lessons learned across all sessions
22
25
  */
@@ -35,12 +35,14 @@ export class ReflectionEngine {
35
35
  /**
36
36
  * Get reflections for a specific session
37
37
  */
38
- async getSessionReflections(sessionId) {
38
+ async getSessionReflections(sessionId, options = {}) {
39
39
  const reflections = (await this.db
40
40
  .selectFrom(this.reflectionsTable)
41
41
  .selectAll()
42
42
  .where('session_id', '=', sessionId)
43
43
  .orderBy('created_at', 'desc')
44
+ .limit(options.limit ?? 500) // Audit Phase 18: Hard memory safety limit
45
+ .offset(options.offset ?? 0)
44
46
  .execute());
45
47
  return reflections.map((r) => this.parseReflection(r));
46
48
  }
@@ -86,25 +86,36 @@ export class RitualOrchestrator {
86
86
  switch (ritual.type) {
87
87
  case 'compression':
88
88
  const sessionsTable = this.config.sessionsTable || 'agent_sessions';
89
- const activeSessions = await this.db
90
- .selectFrom(sessionsTable)
91
- .select('id')
92
- .where('status', '=', 'active')
93
- .execute();
89
+ const messagesTable = this.config.messagesTable || 'agent_messages';
94
90
  const compressionThreshold = this.config.contextWindowSize || 20;
95
91
  let compressedCount = 0;
96
- for (const session of activeSessions) {
97
- const messagesTable = this.config.messagesTable || 'agent_messages';
98
- const countResult = (await this.db
99
- .selectFrom(messagesTable)
100
- .select((eb) => eb.fn.countAll().as('count'))
101
- .where('session_id', '=', session.id)
102
- .executeTakeFirst());
103
- const count = Number(countResult?.count || 0);
104
- if (count > compressionThreshold) {
105
- await this.cortex.compressor.semanticPruning(session.id);
106
- compressedCount++;
92
+ let offset = 0;
93
+ const batchSize = 100; // Audit Phase 17: Paginated session processing
94
+ while (true) {
95
+ const activeSessions = await this.db
96
+ .selectFrom(sessionsTable)
97
+ .select('id')
98
+ .where('status', '=', 'active')
99
+ .limit(batchSize)
100
+ .offset(offset)
101
+ .execute();
102
+ if (activeSessions.length === 0)
103
+ break;
104
+ for (const session of activeSessions) {
105
+ const countResult = (await this.db
106
+ .selectFrom(messagesTable)
107
+ .select((eb) => eb.fn.countAll().as('count'))
108
+ .where('session_id', '=', session.id)
109
+ .executeTakeFirst());
110
+ const count = Number(countResult?.count || 0);
111
+ if (count > compressionThreshold) {
112
+ await this.cortex.compressor.semanticPruning(session.id);
113
+ compressedCount++;
114
+ }
107
115
  }
116
+ if (activeSessions.length < batchSize)
117
+ break;
118
+ offset += batchSize;
108
119
  }
109
120
  ritualMetadata.sessionsCompressed = compressedCount;
110
121
  break;
@@ -33,7 +33,7 @@ export declare class RuleEngine {
33
33
  priority?: number;
34
34
  script?: string;
35
35
  metadata?: Record<string, any>;
36
- }): Promise<CognitiveRule>;
36
+ }, trx?: any): Promise<CognitiveRule>;
37
37
  /**
38
38
  * Evaluate rules against a specific data object.
39
39
  * Returns the highest priority rule action that matches.
@@ -18,9 +18,9 @@ export class RuleEngine {
18
18
  /**
19
19
  * Define a new cognitive rule transactionally.
20
20
  */
21
- async defineRule(tableName, operation, action, options = {}) {
22
- return await this.db.transaction().execute(async (trx) => {
23
- const rule = (await trx
21
+ async defineRule(tableName, operation, action, options = {}, trx) {
22
+ const defineStep = async (t) => {
23
+ const rule = (await t
24
24
  .insertInto(this.rulesTable)
25
25
  .values({
26
26
  table_name: tableName,
@@ -36,7 +36,13 @@ export class RuleEngine {
36
36
  .returningAll()
37
37
  .executeTakeFirstOrThrow());
38
38
  return this.parseRule(rule);
39
- });
39
+ };
40
+ if (trx) {
41
+ return await defineStep(trx);
42
+ }
43
+ else {
44
+ return await this.db.transaction().execute(defineStep);
45
+ }
40
46
  }
41
47
  /**
42
48
  * Evaluate rules against a specific data object.
@@ -99,23 +99,27 @@ export class SelfEvolution {
99
99
  */
100
100
  async evolve(ddl, options = {}) {
101
101
  console.log(`[SelfEvolution] Applying structural change: ${ddl}`);
102
- // 1. Apply the DDL change
103
- await this.evolution.applySuggestion(ddl);
104
- // 1b. Log for potential rollback
105
- await this.db
106
- .insertInto(this.snapshotsTable)
107
- .values({
108
- name: options.name || `auto_evolution_${Date.now()}`,
109
- dna: await this.getDNA(),
110
- metadata: JSON.stringify({
111
- ...options.metadata,
112
- ddl,
113
- is_auto: true,
114
- timestamp: Date.now(),
115
- }),
116
- created_at: new Date(),
117
- })
118
- .execute();
102
+ await this.db.transaction().execute(async (trx) => {
103
+ // 1. Apply the DDL change
104
+ // Pass transaction to evolution helper if it supports it,
105
+ // otherwise use trx.execute() directly for the DDL
106
+ await trx.execute(sql.raw(ddl));
107
+ // 1b. Log for potential rollback
108
+ await trx
109
+ .insertInto(this.snapshotsTable)
110
+ .values({
111
+ name: options.name || `auto_evolution_${Date.now()}`,
112
+ dna: await this.getDNA(),
113
+ metadata: JSON.stringify({
114
+ ...options.metadata,
115
+ ddl,
116
+ is_auto: true,
117
+ timestamp: Date.now(),
118
+ }),
119
+ created_at: new Date(),
120
+ })
121
+ .execute();
122
+ });
119
123
  // 2. Regenerate types
120
124
  await this.regenerateTypes();
121
125
  console.log(`[SelfEvolution] Schema evolved successfully.`);
@@ -22,4 +22,9 @@ export declare class SelfTestRegistry {
22
22
  success: boolean;
23
23
  error?: string;
24
24
  }[]>;
25
+ private runAuditAction;
26
+ /**
27
+ * Interpret custom probe logic by providing the LLM with relevant database snapshots.
28
+ */
29
+ private dynamicEvaluation;
25
30
  }
@@ -43,121 +43,33 @@ export class SelfTestRegistry {
43
43
  for (const probe of probes) {
44
44
  console.log(`[SelfTestRegistry] Running probe: ${probe.name}`);
45
45
  try {
46
- // Real implementation: Execute the probe script
47
- // For safety and integration, we support a set of predefined audit functions
48
- // or the ability to run a dynamic check if enabled.
49
46
  let success = false;
50
47
  if (probe.script.startsWith('audit:')) {
51
- const action = probe.script.split(':')[1];
52
- switch (action) {
53
- case 'check_schema_consistency':
54
- // Real check: compare introspection with required core tables
55
- const tables = await this.db.introspection.getTables();
56
- const requiredSubsets = [
57
- this.config.messagesTable || 'agent_messages',
58
- this.config.sessionsTable || 'agent_sessions',
59
- this.config.memoriesTable || 'agent_memories',
60
- this.config.knowledgeTable || 'agent_knowledge_base',
61
- this.config.actionsTable || 'agent_actions',
62
- this.config.metricsTable || 'agent_metrics',
63
- ];
64
- const missing = requiredSubsets.filter((req) => !tables.some((t) => t.name === req));
65
- success = missing.length === 0;
66
- break;
67
- case 'check_memory_integrity':
68
- // Real check: verify embeddings are not null where expected
69
- const memoriesTable = this.config.memoriesTable || 'agent_memories';
70
- const invalidMemories = await this.db
71
- .selectFrom(memoriesTable)
72
- .select('id')
73
- .where('embedding', 'is', null)
74
- .execute();
75
- success = invalidMemories.length === 0;
76
- break;
77
- case 'check_session_coherence':
78
- // Check if sessions have at least one message
79
- const sessionsTable = this.config.sessionsTable || 'agent_sessions';
80
- const messagesTable = this.config.messagesTable || 'agent_messages';
81
- const emptySessions = await this.db
82
- .selectFrom(sessionsTable)
83
- .leftJoin(messagesTable, `${sessionsTable}.id`, `${messagesTable}.session_id`)
84
- .select(`${sessionsTable}.id as sid`)
85
- .where(`${messagesTable}.id`, 'is', null)
86
- .execute();
87
- success = emptySessions.length < 5; // Allow small buffer of new sessions
88
- break;
89
- case 'check_data_integrity':
90
- // Real check: Detect orphaned records pointing to non-existent sessions
91
- const knowledgeTable = this.config.knowledgeTable || 'agent_knowledge_base';
92
- const actionsTable = this.config.actionsTable || 'agent_actions';
93
- const sessionsTbl = this.config.sessionsTable || 'agent_sessions';
94
- const orphanedKnowledge = await this.db
95
- .selectFrom(knowledgeTable)
96
- .where('source_session_id', 'is not', null)
97
- .where((eb) => eb.not(eb.exists(eb
98
- .selectFrom(sessionsTbl)
99
- .select('id')
100
- .whereRef(`${knowledgeTable}.source_session_id`, '=', `${sessionsTbl}.id`))))
101
- .execute();
102
- const orphanedActions = await this.db
103
- .selectFrom(actionsTable)
104
- .where((eb) => eb.not(eb.exists(eb
105
- .selectFrom(sessionsTbl)
106
- .select('id')
107
- .whereRef(`${actionsTable}.session_id`, '=', `${sessionsTbl}.id`))))
108
- .execute();
109
- success =
110
- orphanedKnowledge.length === 0 && orphanedActions.length === 0;
111
- break;
112
- case 'check_performance_drift':
113
- // Real check: Compare last 10 queries average with historical baseline
114
- const metricsTbl = this.config.metricsTable || 'agent_metrics';
115
- const recentMetrics = (await this.db
116
- .selectFrom(metricsTbl)
117
- .select('execution_time')
118
- .orderBy('created_at', 'desc')
119
- .limit(20)
120
- .execute());
121
- if (recentMetrics.length < 5) {
122
- success = true; // Not enough data to determine drift
123
- }
124
- else {
125
- const avgRecent = recentMetrics.reduce((sum, m) => sum + (m.execution_time || 0), 0) / recentMetrics.length;
126
- // Baseline: Avg of previous 100 metrics excluding the most recent 20
127
- const baselineMetrics = (await this.db
128
- .selectFrom(metricsTbl)
129
- .select('execution_time')
130
- .orderBy('created_at', 'desc')
131
- .offset(20)
132
- .limit(100)
133
- .execute());
134
- if (baselineMetrics.length === 0) {
135
- success = avgRecent < 500; // Fallback threshold (500ms)
136
- }
137
- else {
138
- const avgBaseline = baselineMetrics.reduce((sum, m) => sum + (m.execution_time || 0), 0) / baselineMetrics.length;
139
- // Fail if recent performance is > 50% worse than baseline
140
- success = avgRecent < avgBaseline * 1.5;
141
- }
142
- }
143
- break;
144
- default:
145
- success = true;
146
- }
48
+ success = await this.runAuditAction(probe.script.split(':')[1]);
147
49
  }
148
50
  else {
149
- // Fallback to simple truthy check or simulation if not a known audit
150
- success = true;
51
+ // Master Sentinel Pass: Dynamic Probe Evaluation
52
+ // If not a hardcoded audit, use LLM to interpret the script against DB state
53
+ success = await this.dynamicEvaluation(probe);
151
54
  }
152
55
  results.push({ name: probe.name, success });
153
- await this.db
154
- .updateTable('agent_logic_probes')
155
- .set({
156
- last_run: new Date(),
157
- last_status: success ? 'pass' : 'fail',
158
- })
159
- .where('id', '=', probe.id)
160
- .execute();
56
+ await this.db.transaction().execute(async (trx) => {
57
+ // Audit Phase 13: Lock row before updating last_status
58
+ await trx
59
+ .selectFrom('agent_logic_probes')
60
+ .select('id')
61
+ .where('id', '=', probe.id)
62
+ .forUpdate()
63
+ .executeTakeFirst();
64
+ await trx
65
+ .updateTable('agent_logic_probes')
66
+ .set({
67
+ last_run: new Date(),
68
+ last_status: success ? 'pass' : 'fail',
69
+ })
70
+ .where('id', '=', probe.id)
71
+ .execute();
72
+ });
161
73
  }
162
74
  catch (e) {
163
75
  results.push({ name: probe.name, success: false, error: String(e) });
@@ -165,4 +77,112 @@ export class SelfTestRegistry {
165
77
  }
166
78
  return results;
167
79
  }
80
+ async runAuditAction(action) {
81
+ switch (action) {
82
+ case 'check_schema_consistency':
83
+ const tables = await this.db.introspection.getTables();
84
+ const requiredSubsets = [
85
+ this.config.messagesTable || 'agent_messages',
86
+ this.config.sessionsTable || 'agent_sessions',
87
+ this.config.memoriesTable || 'agent_memories',
88
+ this.config.knowledgeTable || 'agent_knowledge_base',
89
+ this.config.actionsTable || 'agent_actions',
90
+ this.config.metricsTable || 'agent_metrics',
91
+ ];
92
+ const missing = requiredSubsets.filter((req) => !tables.some((t) => t.name === req));
93
+ return missing.length === 0;
94
+ case 'check_memory_integrity':
95
+ const memoriesTable = this.config.memoriesTable || 'agent_memories';
96
+ const invalidMemories = await this.db
97
+ .selectFrom(memoriesTable)
98
+ .select('id')
99
+ .where('embedding', 'is', null)
100
+ .execute();
101
+ return invalidMemories.length === 0;
102
+ case 'check_session_coherence':
103
+ const sessionsTable = this.config.sessionsTable || 'agent_sessions';
104
+ const messagesTable = this.config.messagesTable || 'agent_messages';
105
+ const emptySessions = await this.db
106
+ .selectFrom(sessionsTable)
107
+ .leftJoin(messagesTable, `${sessionsTable}.id`, `${messagesTable}.session_id`)
108
+ .select(`${sessionsTable}.id as sid`)
109
+ .where(`${messagesTable}.id`, 'is', null)
110
+ .execute();
111
+ return emptySessions.length < 5;
112
+ case 'check_data_integrity':
113
+ const knowledgeTable = this.config.knowledgeTable || 'agent_knowledge_base';
114
+ const actionsTable = this.config.actionsTable || 'agent_actions';
115
+ const sessionsTbl = this.config.sessionsTable || 'agent_sessions';
116
+ const orphanedKnowledge = await this.db
117
+ .selectFrom(knowledgeTable)
118
+ .where('source_session_id', 'is not', null)
119
+ .where((eb) => eb.not(eb.exists(eb
120
+ .selectFrom(sessionsTbl)
121
+ .select('id')
122
+ .whereRef(`${knowledgeTable}.source_session_id`, '=', `${sessionsTbl}.id`))))
123
+ .execute();
124
+ const orphanedActions = await this.db
125
+ .selectFrom(actionsTable)
126
+ .where((eb) => eb.not(eb.exists(eb
127
+ .selectFrom(sessionsTbl)
128
+ .select('id')
129
+ .whereRef(`${actionsTable}.session_id`, '=', `${sessionsTbl}.id`))))
130
+ .execute();
131
+ return orphanedKnowledge.length === 0 && orphanedActions.length === 0;
132
+ case 'check_performance_drift':
133
+ const metricsTbl = this.config.metricsTable || 'agent_metrics';
134
+ const recentMetrics = (await this.db
135
+ .selectFrom(metricsTbl)
136
+ .select('execution_time')
137
+ .orderBy('created_at', 'desc')
138
+ .limit(20)
139
+ .execute());
140
+ if (recentMetrics.length < 5)
141
+ return true;
142
+ const avgRecent = recentMetrics.reduce((sum, m) => sum + (m.execution_time || 0), 0) / recentMetrics.length;
143
+ const baselineMetrics = (await this.db
144
+ .selectFrom(metricsTbl)
145
+ .select('execution_time')
146
+ .orderBy('created_at', 'desc')
147
+ .offset(20)
148
+ .limit(100)
149
+ .execute());
150
+ if (baselineMetrics.length === 0)
151
+ return avgRecent < 500;
152
+ const avgBaseline = baselineMetrics.reduce((sum, m) => sum + (m.execution_time || 0), 0) / baselineMetrics.length;
153
+ return avgRecent < avgBaseline * 1.5;
154
+ default:
155
+ return false;
156
+ }
157
+ }
158
+ /**
159
+ * Interpret custom probe logic by providing the LLM with relevant database snapshots.
160
+ */
161
+ async dynamicEvaluation(probe) {
162
+ const model = this.cortex.llmFast || this.cortex.llm;
163
+ if (!model)
164
+ return true; // Safety fallback if no AI is available
165
+ // Provide a small sample of metrics and actions to allow for semantic reasoning
166
+ const sampleMetrics = await this.db.selectFrom(this.config.metricsTable || 'agent_metrics').limit(10).execute();
167
+ const prompt = `
168
+ You are a Logic Verification Engine for NOORMME.
169
+ TESTRUN: "${probe.name}"
170
+ SCRIPT: "${probe.script}"
171
+ EXPECTED: "${probe.expected_outcome || 'Unspecified'}"
172
+
173
+ DB CONTEXT (Sample Metrics):
174
+ ${JSON.stringify(sampleMetrics, null, 2)}
175
+
176
+ TASK:
177
+ Evaluate if the script passes based on the context.
178
+ If the script is a natural language requirement, infer the result.
179
+ RETURN ONLY "PASS" OR "FAIL".
180
+ `;
181
+ const response = await model.complete({
182
+ prompt,
183
+ temperature: 0.1,
184
+ maxTokens: 5
185
+ });
186
+ return response.content.toUpperCase().includes('PASS');
187
+ }
168
188
  }
@@ -23,6 +23,7 @@ declare class AISynthesisStrategy implements SkillSynthesisStrategy {
23
23
  mutation: any;
24
24
  }[]>;
25
25
  private buildMutationPrompt;
26
+ private sanitizeInput;
26
27
  }
27
28
  /**
28
29
  * SkillSynthesizer implements Pass 6 Ultra-Scale Orchestration.
@@ -77,10 +77,10 @@ class AISynthesisStrategy {
77
77
  TOOLS TO MUTATE:
78
78
  ${contexts
79
79
  .map((ctx, i) => `
80
- [Tool ${i + 1}: ${ctx.targetTool}]
81
- Current Description: ${ctx.existingDescription || 'None'}
80
+ [Tool ${i + 1}: ${this.sanitizeInput(ctx.targetTool)}]
81
+ Current Description: ${this.sanitizeInput(ctx.existingDescription || 'None')}
82
82
  Failure Patterns:
83
- ${ctx.failures.map((f) => `- Args: ${JSON.stringify(f.arguments)}, Error: ${f.error}`).join('\n')}
83
+ ${ctx.failures.map((f) => `- Args: ${this.sanitizeInput(JSON.stringify(f.arguments))}, Error: ${this.sanitizeInput(f.error || 'None')}`).join('\n')}
84
84
  `)
85
85
  .join('\n')}
86
86
 
@@ -120,19 +120,23 @@ class AISynthesisStrategy {
120
120
  }
121
121
  buildMutationPrompt(context) {
122
122
  const failureList = context.failures
123
- .map((f, i) => `${i + 1}. Args: ${JSON.stringify(f.arguments)}, Error: ${f.error || 'None'}`)
123
+ .map((f, i) => `${i + 1}. Args: ${this.sanitizeInput(JSON.stringify(f.arguments))}, Error: ${this.sanitizeInput(f.error || 'None')}`)
124
124
  .join('\n');
125
125
  return `
126
- You are a Meta-Evolutionary AI Engine optimizing a tool: "${context.targetTool}".
126
+ You are a Meta-Evolutionary AI Engine optimizing a tool: "${this.sanitizeInput(context.targetTool)}".
127
127
  Analyze FAILURES and rewrite the description to prevent them.
128
128
 
129
- EXISTING: "${context.existingDescription || 'None'}"
129
+ EXISTING: "${this.sanitizeInput(context.existingDescription || 'None')}"
130
130
  FAILURES:
131
131
  ${failureList}
132
132
 
133
133
  RETURN JSON: { "description": "...", "metadata": { "fixed": [...], "reason": "..." } }
134
134
  `;
135
135
  }
136
+ sanitizeInput(input) {
137
+ // Audit Phase 9: Neutralize prompt injection markers and control chars
138
+ return input.slice(0, 1000).replace(/[\u0000-\u001F\u007F-\u009F]/g, '').replace(/<\|.*?\|>/g, '');
139
+ }
136
140
  }
137
141
  /**
138
142
  * SkillSynthesizer implements Pass 6 Ultra-Scale Orchestration.
@@ -261,6 +265,8 @@ export class SkillSynthesizer {
261
265
  return await this.cortex.capabilities.registerCapability(tool, mutation.version, mutation.mutatedDescription, {
262
266
  initialStatus: 'experimental',
263
267
  mutatedFrom: tool,
268
+ lineage: mutation.mutatedMetadata?.lineage || tool,
269
+ variant_tag: `v${Date.now()}`,
264
270
  synthesis_engine: 'Ultra-Scale-Tiered',
265
271
  ...mutation.mutatedMetadata,
266
272
  synthesized_at: new Date(),
@@ -273,13 +279,16 @@ export class SkillSynthesizer {
273
279
  const toPrune = experimental
274
280
  .sort((a, b) => a.reliability - b.reliability)
275
281
  .slice(0, Math.ceil(experimental.length * 0.2));
276
- for (const skill of toPrune) {
277
- await this.db
278
- .deleteFrom(this.cortex.agenticConfig.capabilitiesTable ||
279
- 'agent_capabilities')
280
- .where('id', '=', skill.id)
281
- .execute();
282
- }
282
+ // Audit Phase 19: Transactional pruning
283
+ await this.db.transaction().execute(async (trx) => {
284
+ for (const skill of toPrune) {
285
+ await trx
286
+ .deleteFrom(this.cortex.agenticConfig.capabilitiesTable ||
287
+ 'agent_capabilities')
288
+ .where('id', '=', skill.id)
289
+ .execute();
290
+ }
291
+ });
283
292
  }
284
293
  }
285
294
  }
@@ -81,6 +81,7 @@ export declare class StrategicPlanner {
81
81
  * Revert the last mutation for a persona.
82
82
  */
83
83
  rollbackPersona(id: string | number): Promise<string>;
84
+ private sanitizeRole;
84
85
  private parsePersona;
85
86
  }
86
87
  export {};