noormme 1.2.0 → 1.2.2
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.
- package/README.md +60 -6
- package/dist/cjs/agentic/ActionJournal.d.ts +5 -2
- package/dist/cjs/agentic/ActionJournal.js +13 -5
- package/dist/cjs/agentic/CapabilityManager.d.ts +7 -0
- package/dist/cjs/agentic/CapabilityManager.js +84 -7
- package/dist/cjs/agentic/CognitiveRepository.js +3 -6
- package/dist/cjs/agentic/Cortex.d.ts +4 -0
- package/dist/cjs/agentic/Cortex.js +38 -17
- package/dist/cjs/agentic/EpisodicMemory.d.ts +5 -1
- package/dist/cjs/agentic/EpisodicMemory.js +11 -4
- package/dist/cjs/agentic/PersonaManager.js +37 -31
- package/dist/cjs/agentic/PolicyEnforcer.d.ts +6 -1
- package/dist/cjs/agentic/PolicyEnforcer.js +74 -17
- package/dist/cjs/agentic/ResourceMonitor.d.ts +9 -0
- package/dist/cjs/agentic/ResourceMonitor.js +36 -2
- package/dist/cjs/agentic/SessionManager.js +24 -17
- package/dist/cjs/agentic/VectorIndexer.d.ts +1 -0
- package/dist/cjs/agentic/VectorIndexer.js +26 -17
- package/dist/cjs/agentic/improvement/AblationEngine.d.ts +4 -6
- package/dist/cjs/agentic/improvement/AblationEngine.js +57 -37
- package/dist/cjs/agentic/improvement/ActionRefiner.js +30 -14
- package/dist/cjs/agentic/improvement/ConflictResolver.d.ts +3 -1
- package/dist/cjs/agentic/improvement/ConflictResolver.js +59 -47
- package/dist/cjs/agentic/improvement/CortexJanitor.js +11 -0
- package/dist/cjs/agentic/improvement/CuriosityEngine.d.ts +1 -1
- package/dist/cjs/agentic/improvement/CuriosityEngine.js +48 -21
- package/dist/cjs/agentic/improvement/EvolutionRitual.js +26 -14
- package/dist/cjs/agentic/improvement/EvolutionaryPilot.js +16 -4
- package/dist/cjs/agentic/improvement/GoalArchitect.d.ts +6 -2
- package/dist/cjs/agentic/improvement/GoalArchitect.js +72 -34
- package/dist/cjs/agentic/improvement/GovernanceManager.d.ts +20 -6
- package/dist/cjs/agentic/improvement/GovernanceManager.js +134 -155
- package/dist/cjs/agentic/improvement/HiveLink.d.ts +7 -3
- package/dist/cjs/agentic/improvement/HiveLink.js +135 -113
- package/dist/cjs/agentic/improvement/KnowledgeDistiller.js +43 -35
- package/dist/cjs/agentic/improvement/QuotaManager.d.ts +41 -0
- package/dist/cjs/agentic/improvement/QuotaManager.js +185 -0
- package/dist/cjs/agentic/improvement/RecursiveReasoner.js +50 -25
- package/dist/cjs/agentic/improvement/ReflectionEngine.d.ts +4 -1
- package/dist/cjs/agentic/improvement/ReflectionEngine.js +3 -1
- package/dist/cjs/agentic/improvement/RitualOrchestrator.js +27 -16
- package/dist/cjs/agentic/improvement/RuleEngine.d.ts +1 -1
- package/dist/cjs/agentic/improvement/RuleEngine.js +10 -4
- package/dist/cjs/agentic/improvement/SelfEvolution.js +22 -17
- package/dist/cjs/agentic/improvement/SelfTestRegistry.d.ts +5 -0
- package/dist/cjs/agentic/improvement/SelfTestRegistry.js +129 -109
- package/dist/cjs/agentic/improvement/SkillSynthesizer.d.ts +1 -0
- package/dist/cjs/agentic/improvement/SkillSynthesizer.js +22 -13
- package/dist/cjs/agentic/improvement/StrategicPlanner.d.ts +1 -0
- package/dist/cjs/agentic/improvement/StrategicPlanner.js +26 -19
- package/dist/cjs/agentic/improvement/governance/AuditContext.d.ts +17 -0
- package/dist/cjs/agentic/improvement/governance/AuditContext.js +2 -0
- package/dist/cjs/agentic/improvement/governance/BudgetAuditor.d.ts +4 -0
- package/dist/cjs/agentic/improvement/governance/BudgetAuditor.js +50 -0
- package/dist/cjs/agentic/improvement/governance/EmergenceAuditor.d.ts +4 -0
- package/dist/cjs/agentic/improvement/governance/EmergenceAuditor.js +37 -0
- package/dist/cjs/agentic/improvement/governance/MaintenanceOracle.d.ts +4 -0
- package/dist/cjs/agentic/improvement/governance/MaintenanceOracle.js +67 -0
- package/dist/cjs/agentic/improvement/governance/PerformanceAuditor.d.ts +4 -0
- package/dist/cjs/agentic/improvement/governance/PerformanceAuditor.js +43 -0
- package/dist/cjs/agentic/improvement/governance/PersonaAuditor.d.ts +6 -0
- package/dist/cjs/agentic/improvement/governance/PersonaAuditor.js +74 -0
- package/dist/cjs/agentic/improvement/governance/RemediationEngine.d.ts +5 -0
- package/dist/cjs/agentic/improvement/governance/RemediationEngine.js +43 -0
- package/dist/cjs/agentic/improvement/governance/SkillAuditor.d.ts +5 -0
- package/dist/cjs/agentic/improvement/governance/SkillAuditor.js +52 -0
- package/dist/cjs/agentic/telemetry/CognitiveSynthesizer.d.ts +5 -0
- package/dist/cjs/agentic/telemetry/CognitiveSynthesizer.js +54 -12
- package/dist/cjs/agentic/telemetry/EventHarvester.d.ts +1 -1
- package/dist/cjs/agentic/telemetry/EventHarvester.js +10 -3
- package/dist/cjs/agentic/telemetry/ResearchAlchemist.d.ts +7 -2
- package/dist/cjs/agentic/telemetry/ResearchAlchemist.js +49 -8
- package/dist/cjs/agentic/telemetry/TelemetryOrchestrator.d.ts +4 -1
- package/dist/cjs/agentic/telemetry/TelemetryOrchestrator.js +38 -11
- package/dist/cjs/cli/commands/inspect.js +40 -1
- package/dist/cjs/cli/commands/watch.js +31 -25
- package/dist/cjs/cli/index.js +0 -0
- package/dist/cjs/dialect/sqlite/sqlite-introspector.js +15 -5
- package/dist/cjs/helpers/agent-schema.js +15 -14
- package/dist/cjs/migration/data_migrator.js +4 -4
- package/dist/cjs/migration/schema_differ.js +37 -15
- package/dist/cjs/types/index.d.ts +12 -0
- package/dist/cjs/util/safe-sql-helpers.js +7 -10
- package/dist/esm/agentic/ActionJournal.d.ts +5 -2
- package/dist/esm/agentic/ActionJournal.js +13 -5
- package/dist/esm/agentic/CapabilityManager.d.ts +7 -0
- package/dist/esm/agentic/CapabilityManager.js +84 -7
- package/dist/esm/agentic/CognitiveRepository.js +3 -6
- package/dist/esm/agentic/Cortex.d.ts +4 -0
- package/dist/esm/agentic/Cortex.js +38 -17
- package/dist/esm/agentic/EpisodicMemory.d.ts +5 -1
- package/dist/esm/agentic/EpisodicMemory.js +11 -4
- package/dist/esm/agentic/PersonaManager.js +37 -31
- package/dist/esm/agentic/PolicyEnforcer.d.ts +6 -1
- package/dist/esm/agentic/PolicyEnforcer.js +74 -17
- package/dist/esm/agentic/ResourceMonitor.d.ts +9 -0
- package/dist/esm/agentic/ResourceMonitor.js +36 -2
- package/dist/esm/agentic/SessionManager.js +24 -17
- package/dist/esm/agentic/VectorIndexer.d.ts +1 -0
- package/dist/esm/agentic/VectorIndexer.js +26 -17
- package/dist/esm/agentic/improvement/AblationEngine.d.ts +4 -6
- package/dist/esm/agentic/improvement/AblationEngine.js +57 -37
- package/dist/esm/agentic/improvement/ActionRefiner.js +30 -14
- package/dist/esm/agentic/improvement/ConflictResolver.d.ts +3 -1
- package/dist/esm/agentic/improvement/ConflictResolver.js +59 -47
- package/dist/esm/agentic/improvement/CortexJanitor.js +11 -0
- package/dist/esm/agentic/improvement/CuriosityEngine.d.ts +1 -1
- package/dist/esm/agentic/improvement/CuriosityEngine.js +48 -21
- package/dist/esm/agentic/improvement/EvolutionRitual.js +26 -14
- package/dist/esm/agentic/improvement/EvolutionaryPilot.js +16 -4
- package/dist/esm/agentic/improvement/GoalArchitect.d.ts +6 -2
- package/dist/esm/agentic/improvement/GoalArchitect.js +72 -34
- package/dist/esm/agentic/improvement/GovernanceManager.d.ts +20 -6
- package/dist/esm/agentic/improvement/GovernanceManager.js +134 -155
- package/dist/esm/agentic/improvement/HiveLink.d.ts +7 -3
- package/dist/esm/agentic/improvement/HiveLink.js +135 -113
- package/dist/esm/agentic/improvement/KnowledgeDistiller.js +43 -35
- package/dist/esm/agentic/improvement/QuotaManager.d.ts +41 -0
- package/dist/esm/agentic/improvement/QuotaManager.js +182 -0
- package/dist/esm/agentic/improvement/RecursiveReasoner.js +50 -25
- package/dist/esm/agentic/improvement/ReflectionEngine.d.ts +4 -1
- package/dist/esm/agentic/improvement/ReflectionEngine.js +3 -1
- package/dist/esm/agentic/improvement/RitualOrchestrator.js +27 -16
- package/dist/esm/agentic/improvement/RuleEngine.d.ts +1 -1
- package/dist/esm/agentic/improvement/RuleEngine.js +10 -4
- package/dist/esm/agentic/improvement/SelfEvolution.js +22 -17
- package/dist/esm/agentic/improvement/SelfTestRegistry.d.ts +5 -0
- package/dist/esm/agentic/improvement/SelfTestRegistry.js +129 -109
- package/dist/esm/agentic/improvement/SkillSynthesizer.d.ts +1 -0
- package/dist/esm/agentic/improvement/SkillSynthesizer.js +22 -13
- package/dist/esm/agentic/improvement/StrategicPlanner.d.ts +1 -0
- package/dist/esm/agentic/improvement/StrategicPlanner.js +26 -19
- package/dist/esm/agentic/improvement/governance/AuditContext.d.ts +17 -0
- package/dist/esm/agentic/improvement/governance/AuditContext.js +2 -0
- package/dist/esm/agentic/improvement/governance/BudgetAuditor.d.ts +4 -0
- package/dist/esm/agentic/improvement/governance/BudgetAuditor.js +47 -0
- package/dist/esm/agentic/improvement/governance/EmergenceAuditor.d.ts +4 -0
- package/dist/esm/agentic/improvement/governance/EmergenceAuditor.js +34 -0
- package/dist/esm/agentic/improvement/governance/MaintenanceOracle.d.ts +4 -0
- package/dist/esm/agentic/improvement/governance/MaintenanceOracle.js +64 -0
- package/dist/esm/agentic/improvement/governance/PerformanceAuditor.d.ts +4 -0
- package/dist/esm/agentic/improvement/governance/PerformanceAuditor.js +40 -0
- package/dist/esm/agentic/improvement/governance/PersonaAuditor.d.ts +6 -0
- package/dist/esm/agentic/improvement/governance/PersonaAuditor.js +71 -0
- package/dist/esm/agentic/improvement/governance/RemediationEngine.d.ts +5 -0
- package/dist/esm/agentic/improvement/governance/RemediationEngine.js +40 -0
- package/dist/esm/agentic/improvement/governance/SkillAuditor.d.ts +5 -0
- package/dist/esm/agentic/improvement/governance/SkillAuditor.js +49 -0
- package/dist/esm/agentic/telemetry/CognitiveSynthesizer.d.ts +5 -0
- package/dist/esm/agentic/telemetry/CognitiveSynthesizer.js +54 -12
- package/dist/esm/agentic/telemetry/EventHarvester.d.ts +1 -1
- package/dist/esm/agentic/telemetry/EventHarvester.js +10 -3
- package/dist/esm/agentic/telemetry/ResearchAlchemist.d.ts +7 -2
- package/dist/esm/agentic/telemetry/ResearchAlchemist.js +49 -8
- package/dist/esm/agentic/telemetry/TelemetryOrchestrator.d.ts +4 -1
- package/dist/esm/agentic/telemetry/TelemetryOrchestrator.js +38 -11
- package/dist/esm/cli/commands/inspect.js +40 -1
- package/dist/esm/cli/commands/watch.js +31 -25
- package/dist/esm/dialect/sqlite/sqlite-introspector.js +15 -5
- package/dist/esm/helpers/agent-schema.js +15 -14
- package/dist/esm/migration/data_migrator.js +4 -4
- package/dist/esm/migration/schema_differ.js +37 -15
- package/dist/esm/types/index.d.ts +12 -0
- package/dist/esm/util/safe-sql-helpers.js +7 -10
- package/package.json +44 -40
|
@@ -26,10 +26,10 @@ async function migrateTableData(sourceDb, targetDb, sourceTable, targetTable, so
|
|
|
26
26
|
errors: [],
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
|
-
// Keyset pagination optimization: find a
|
|
30
|
-
const pkColumn = sourceTable.columns.find((c) => c.primaryKey
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
// Keyset pagination optimization: find a primary key or a unique single-column index
|
|
30
|
+
const pkColumn = sourceTable.columns.find((c) => c.primaryKey)?.name ||
|
|
31
|
+
sourceTable.indexes.find((i) => i.unique && i.columns.length === 1)
|
|
32
|
+
?.columns[0];
|
|
33
33
|
let lastId = null;
|
|
34
34
|
// Calculate number of batches (approximate)
|
|
35
35
|
const batchCount = Math.ceil(totalRows / options.batchSize);
|
|
@@ -179,24 +179,46 @@ function compareIndexes(sourceTable, targetTable) {
|
|
|
179
179
|
}
|
|
180
180
|
function compareConstraints(sourceTable, targetTable) {
|
|
181
181
|
const differences = [];
|
|
182
|
-
//
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
182
|
+
// PRODUCTION HARDENING: Deep Structural Audit
|
|
183
|
+
const sourceConstraints = sourceTable.constraints || [];
|
|
184
|
+
const targetConstraints = targetTable.constraints || [];
|
|
185
|
+
for (const source of sourceConstraints) {
|
|
186
|
+
const matching = targetConstraints.find((t) => {
|
|
187
|
+
const typeMatch = t.type === source.type;
|
|
188
|
+
const exprMatch = t.expression === source.expression;
|
|
189
|
+
const colsMatch = JSON.stringify(t.columns?.sort()) ===
|
|
190
|
+
JSON.stringify(source.columns?.sort());
|
|
191
|
+
return typeMatch && (source.type === 'CHECK' ? exprMatch : colsMatch);
|
|
190
192
|
});
|
|
193
|
+
if (!matching) {
|
|
194
|
+
differences.push({
|
|
195
|
+
type: 'constraint_added',
|
|
196
|
+
table: sourceTable.name,
|
|
197
|
+
details: {
|
|
198
|
+
source,
|
|
199
|
+
message: `Constraint of type ${source.type} ${source.name ? `'${source.name}' ` : ''}needs to be added`,
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
}
|
|
191
203
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
type
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
204
|
+
for (const target of targetConstraints) {
|
|
205
|
+
const matching = sourceConstraints.find((s) => {
|
|
206
|
+
const typeMatch = s.type === target.type;
|
|
207
|
+
const exprMatch = s.expression === target.expression;
|
|
208
|
+
const colsMatch = JSON.stringify(s.columns?.sort()) ===
|
|
209
|
+
JSON.stringify(target.columns?.sort());
|
|
210
|
+
return typeMatch && (target.type === 'CHECK' ? exprMatch : colsMatch);
|
|
199
211
|
});
|
|
212
|
+
if (!matching) {
|
|
213
|
+
differences.push({
|
|
214
|
+
type: 'constraint_removed',
|
|
215
|
+
table: sourceTable.name,
|
|
216
|
+
details: {
|
|
217
|
+
target,
|
|
218
|
+
message: `Constraint of type ${target.type} ${target.name ? `'${target.name}' ` : ''}exists in target but not in source`,
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
}
|
|
200
222
|
}
|
|
201
223
|
return differences;
|
|
202
224
|
}
|
|
@@ -385,6 +385,18 @@ export interface AgentPersona {
|
|
|
385
385
|
createdAt: Date;
|
|
386
386
|
updatedAt: Date;
|
|
387
387
|
}
|
|
388
|
+
export interface AgentQuota {
|
|
389
|
+
id: string | number;
|
|
390
|
+
targetType: 'persona' | 'swarm' | 'global';
|
|
391
|
+
targetId: string | null;
|
|
392
|
+
metric: 'cost' | 'tokens_input' | 'tokens_output' | 'tokens_total';
|
|
393
|
+
limit: number;
|
|
394
|
+
period: 'hourly' | 'daily' | 'monthly' | 'infinite';
|
|
395
|
+
currentUsage: number;
|
|
396
|
+
metadata?: Record<string, any>;
|
|
397
|
+
createdAt: Date;
|
|
398
|
+
updatedAt: Date;
|
|
399
|
+
}
|
|
388
400
|
export interface AgentEpoch {
|
|
389
401
|
id: string | number;
|
|
390
402
|
sessionId: string | number;
|
|
@@ -150,16 +150,13 @@ function safeCaseStatement(cases, elseResult) {
|
|
|
150
150
|
if (cases.length === 0) {
|
|
151
151
|
throw new Error('At least one WHEN clause is required');
|
|
152
152
|
}
|
|
153
|
-
//
|
|
154
|
-
//
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
})
|
|
161
|
-
.join(' ');
|
|
162
|
-
return sql_js_1.sql.raw(`CASE ${whenClauses} ELSE ${elseResult} END`);
|
|
153
|
+
// PRODUCTION HARDENING: Balanced Case Construction
|
|
154
|
+
// We avoid sql.raw() by building the CASE statement recursively or via template fragments
|
|
155
|
+
let caseBuilder = (0, sql_js_1.sql) `CASE`;
|
|
156
|
+
for (const c of cases) {
|
|
157
|
+
caseBuilder = (0, sql_js_1.sql) `${caseBuilder} WHEN ${c.condition} THEN ${sql_js_1.sql.val(c.result)}`;
|
|
158
|
+
}
|
|
159
|
+
return (0, sql_js_1.sql) `${caseBuilder} ELSE ${sql_js_1.sql.val(elseResult)} END`;
|
|
163
160
|
}
|
|
164
161
|
/**
|
|
165
162
|
* Example usage and best practices
|
|
@@ -36,9 +36,12 @@ export declare class ActionJournal {
|
|
|
36
36
|
*/
|
|
37
37
|
recordOutcome(actionId: string | number, status: AgentAction['status'], outcome: string, durationMs?: number, metadata?: Record<string, any>): Promise<AgentAction>;
|
|
38
38
|
/**
|
|
39
|
-
* Get actions for a session
|
|
39
|
+
* Get actions for a session with pagination
|
|
40
40
|
*/
|
|
41
|
-
getSessionActions(sessionId: string | number
|
|
41
|
+
getSessionActions(sessionId: string | number, options?: {
|
|
42
|
+
limit?: number;
|
|
43
|
+
cursor?: string | number;
|
|
44
|
+
}): Promise<AgentAction[]>;
|
|
42
45
|
/**
|
|
43
46
|
* Get actions by tool name across all sessions.
|
|
44
47
|
*/
|
|
@@ -65,15 +65,20 @@ export class ActionJournal {
|
|
|
65
65
|
return parsed;
|
|
66
66
|
}
|
|
67
67
|
/**
|
|
68
|
-
* Get actions for a session
|
|
68
|
+
* Get actions for a session with pagination
|
|
69
69
|
*/
|
|
70
|
-
async getSessionActions(sessionId) {
|
|
71
|
-
const
|
|
70
|
+
async getSessionActions(sessionId, options = {}) {
|
|
71
|
+
const { limit = 100, cursor } = options;
|
|
72
|
+
let query = this.typedDb
|
|
72
73
|
.selectFrom(this.actionsTable)
|
|
73
74
|
.selectAll()
|
|
74
75
|
.where('session_id', '=', sessionId)
|
|
75
|
-
.orderBy('
|
|
76
|
-
.
|
|
76
|
+
.orderBy('id', 'asc') // Audit Phase 9: Stable ordering for cursors
|
|
77
|
+
.limit(limit);
|
|
78
|
+
if (cursor) {
|
|
79
|
+
query = query.where('id', '>', cursor);
|
|
80
|
+
}
|
|
81
|
+
const actions = await query.execute();
|
|
77
82
|
return actions.map((a) => this.parseAction(a));
|
|
78
83
|
}
|
|
79
84
|
/**
|
|
@@ -93,6 +98,8 @@ export class ActionJournal {
|
|
|
93
98
|
* Generate a report of tool failures.
|
|
94
99
|
*/
|
|
95
100
|
async getFailureReport() {
|
|
101
|
+
// Audit Phase 19: Sliding window (default 7 days) to prevent OOM/slow scans
|
|
102
|
+
const windowStart = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
|
|
96
103
|
const results = await this.typedDb
|
|
97
104
|
.selectFrom(this.actionsTable)
|
|
98
105
|
.select([
|
|
@@ -101,6 +108,7 @@ export class ActionJournal {
|
|
|
101
108
|
(eb) => eb.fn.max('created_at').as('lastFailure'),
|
|
102
109
|
])
|
|
103
110
|
.where('status', '=', 'failure')
|
|
111
|
+
.where('created_at', '>', windowStart)
|
|
104
112
|
.groupBy('tool_name')
|
|
105
113
|
.orderBy((eb) => eb.fn.count('id'), 'desc')
|
|
106
114
|
.execute();
|
|
@@ -44,5 +44,12 @@ export declare class CapabilityManager {
|
|
|
44
44
|
* Get all registered capabilities, optionally filtered by status
|
|
45
45
|
*/
|
|
46
46
|
getCapabilities(status?: AgentCapability['status']): Promise<AgentCapability[]>;
|
|
47
|
+
/**
|
|
48
|
+
* Validate if a persona has access to a specific capability (Sandbox Enforcement).
|
|
49
|
+
*/
|
|
50
|
+
validateCapabilityAccess(personaId: string | number, capabilityName: string): Promise<{
|
|
51
|
+
allowed: boolean;
|
|
52
|
+
reason?: string;
|
|
53
|
+
}>;
|
|
47
54
|
private parseCapability;
|
|
48
55
|
}
|
|
@@ -80,12 +80,16 @@ export class CapabilityManager {
|
|
|
80
80
|
*/
|
|
81
81
|
async reportOutcome(name, success) {
|
|
82
82
|
await this.db.transaction().execute(async (trx) => {
|
|
83
|
-
|
|
83
|
+
let query = trx
|
|
84
84
|
.selectFrom(this.capabilitiesTable)
|
|
85
85
|
.selectAll()
|
|
86
86
|
.where('name', '=', name)
|
|
87
|
-
.orderBy('updated_at', 'desc')
|
|
88
|
-
|
|
87
|
+
.orderBy('updated_at', 'desc');
|
|
88
|
+
// PRODUCTION HARDENING: Lock row to prevent RMW race (Skip for SQLite)
|
|
89
|
+
if (this.db.getExecutor().adapter?.constructor.name !== 'SqliteAdapter') {
|
|
90
|
+
query = query.forUpdate();
|
|
91
|
+
}
|
|
92
|
+
const capability = await query.executeTakeFirst();
|
|
89
93
|
if (capability) {
|
|
90
94
|
const cap = capability;
|
|
91
95
|
const metadata = typeof cap.metadata === 'string'
|
|
@@ -95,11 +99,14 @@ export class CapabilityManager {
|
|
|
95
99
|
const successCount = (metadata.successCount || 0) + (success ? 1 : 0);
|
|
96
100
|
// Damped moving average: weight recent outcomes more but keep history
|
|
97
101
|
// formula: new = old * (1 - alpha) + current * alpha
|
|
98
|
-
const alpha = 0.2;
|
|
99
102
|
const currentReliability = cap.reliability;
|
|
103
|
+
const alpha = 0.2;
|
|
100
104
|
const newReliability = success
|
|
101
105
|
? Math.min(1.0, currentReliability * (1 - alpha) + alpha)
|
|
102
106
|
: Math.max(0.0, currentReliability * (1 - alpha));
|
|
107
|
+
// Sovereign Draft: Anchored Reliability (weighted by total runs)
|
|
108
|
+
const anchoredReliability = ((metadata.anchored_reliability || 1.0) * totalCount + (success ? 1 : 0)) /
|
|
109
|
+
(totalCount + 1);
|
|
103
110
|
let newStatus = cap.status || 'experimental';
|
|
104
111
|
// --- Emergent Skill Evolution Optimization ---
|
|
105
112
|
const successStreak = (metadata.successStreak || 0) + (success ? 1 : 0);
|
|
@@ -172,7 +179,8 @@ export class CapabilityManager {
|
|
|
172
179
|
failureStreak,
|
|
173
180
|
performanceBaseline: newBaseline,
|
|
174
181
|
performanceVariance: newVariance,
|
|
175
|
-
|
|
182
|
+
anchored_reliability: anchoredReliability,
|
|
183
|
+
lastOutcomeType: success ? 'success' : 'failure',
|
|
176
184
|
}),
|
|
177
185
|
updated_at: new Date(),
|
|
178
186
|
})
|
|
@@ -203,8 +211,77 @@ export class CapabilityManager {
|
|
|
203
211
|
if (status) {
|
|
204
212
|
query = query.where('status', '=', status);
|
|
205
213
|
}
|
|
206
|
-
|
|
207
|
-
|
|
214
|
+
// Sovereign Draft: Prioritize Alpha versions and higher reliability
|
|
215
|
+
// Audit Phase 19: Hard limit to prevent memory spikes in massive skillsets
|
|
216
|
+
const list = await query
|
|
217
|
+
.orderBy('name', 'asc')
|
|
218
|
+
.orderBy('reliability', 'desc')
|
|
219
|
+
.limit(1000)
|
|
220
|
+
.execute();
|
|
221
|
+
// Filter to latest/best variants if many versions exist
|
|
222
|
+
const unique = new Map();
|
|
223
|
+
for (const c of list) {
|
|
224
|
+
const meta = typeof c.metadata === 'string' ? JSON.parse(c.metadata) : (c.metadata || {});
|
|
225
|
+
if (!unique.has(c.name) || meta.is_alpha) {
|
|
226
|
+
unique.set(c.name, c);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return Array.from(unique.values()).map((c) => this.parseCapability(c));
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Validate if a persona has access to a specific capability (Sandbox Enforcement).
|
|
233
|
+
*/
|
|
234
|
+
async validateCapabilityAccess(personaId, capabilityName) {
|
|
235
|
+
const persona = await this.cortex.personas.getPersona(String(personaId)) ||
|
|
236
|
+
await this.typedDb.selectFrom(this.config.personasTable || 'agent_personas')
|
|
237
|
+
.selectAll()
|
|
238
|
+
.where('id', '=', personaId)
|
|
239
|
+
.executeTakeFirst()
|
|
240
|
+
.then(p => p ? this.cortex.personas.parsePersona(p) : null);
|
|
241
|
+
if (!persona) {
|
|
242
|
+
return { allowed: false, reason: `Persona ${personaId} not found.` };
|
|
243
|
+
}
|
|
244
|
+
// Check if persona is quarantined
|
|
245
|
+
if (persona.metadata?.status === 'quarantined') {
|
|
246
|
+
return {
|
|
247
|
+
allowed: false,
|
|
248
|
+
reason: `Persona ${personaId} is currently quarantined due to safety violations.`,
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
// Check if capability is blacklisted globally
|
|
252
|
+
const cap = await this.typedDb
|
|
253
|
+
.selectFrom(this.capabilitiesTable)
|
|
254
|
+
.select(['status', 'reliability'])
|
|
255
|
+
.where('name', '=', capabilityName)
|
|
256
|
+
.orderBy('reliability', 'desc')
|
|
257
|
+
.executeTakeFirst();
|
|
258
|
+
if (cap && cap.status === 'blacklisted') {
|
|
259
|
+
return {
|
|
260
|
+
allowed: false,
|
|
261
|
+
reason: `Capability '${capabilityName}' is globally blacklisted.`,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
// Enforce persona-specific capability list if defined
|
|
265
|
+
if (persona.capabilities && persona.capabilities.length > 0) {
|
|
266
|
+
const isAllowed = persona.capabilities.includes(capabilityName) || persona.capabilities.includes('*');
|
|
267
|
+
if (!isAllowed) {
|
|
268
|
+
return {
|
|
269
|
+
allowed: false,
|
|
270
|
+
reason: `Persona '${persona.name}' does not have permission to use capability '${capabilityName}'.`,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
// Enforce Sandbox limit for experimental skills
|
|
275
|
+
if (cap && cap.status === 'experimental') {
|
|
276
|
+
const experimentalCount = (persona.capabilities || []).filter((c) => c.startsWith('experimental_')).length;
|
|
277
|
+
if (experimentalCount >= (this.evolutionConfig.maxSandboxSkills || 5)) {
|
|
278
|
+
return {
|
|
279
|
+
allowed: false,
|
|
280
|
+
reason: `Persona '${persona.name}' has reached the maximum number of experimental sandbox skills.`,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return { allowed: true };
|
|
208
285
|
}
|
|
209
286
|
parseCapability(cap) {
|
|
210
287
|
return {
|
|
@@ -17,12 +17,9 @@ export class CognitiveRepository {
|
|
|
17
17
|
* Helper to evaluate rules and trigger actions
|
|
18
18
|
*/
|
|
19
19
|
async triggerRules(operation, data) {
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
'agent_rules';
|
|
24
|
-
const tables = await this.cortex.db.introspection.getTables();
|
|
25
|
-
if (!tables.some((t) => t.name === rulesTable)) {
|
|
20
|
+
// Audit Phase 18: Eliminate per-operation introspection.
|
|
21
|
+
// The Cortex is responsible for ensuring rules are only evaluated if the table exists.
|
|
22
|
+
if (!this.cortex.rules) {
|
|
26
23
|
return data;
|
|
27
24
|
}
|
|
28
25
|
const result = await this.cortex.rules.evaluateRules(this.table.name, operation, data);
|
|
@@ -31,11 +31,13 @@ import { SelfTestRegistry } from './improvement/SelfTestRegistry.js';
|
|
|
31
31
|
import { TelemetryOrchestrator } from './telemetry/TelemetryOrchestrator.js';
|
|
32
32
|
import { SkillSynthesizer } from './improvement/SkillSynthesizer.js';
|
|
33
33
|
import { EvolutionRitual } from './improvement/EvolutionRitual.js';
|
|
34
|
+
import { QuotaManager } from './improvement/QuotaManager.js';
|
|
34
35
|
/**
|
|
35
36
|
* Cortex is the unified facade for agentic operations.
|
|
36
37
|
* It coordinates sessions, memory, reflection, and knowledge.
|
|
37
38
|
*/
|
|
38
39
|
export declare class Cortex {
|
|
40
|
+
#private;
|
|
39
41
|
db: Kysely<any>;
|
|
40
42
|
config: NOORMConfig;
|
|
41
43
|
sessions: SessionManager;
|
|
@@ -69,11 +71,13 @@ export declare class Cortex {
|
|
|
69
71
|
telemetry: TelemetryOrchestrator;
|
|
70
72
|
skillSynthesizer: SkillSynthesizer;
|
|
71
73
|
evolutionRitual: EvolutionRitual;
|
|
74
|
+
quotas: QuotaManager;
|
|
72
75
|
llm: LLMProvider | null;
|
|
73
76
|
llmFast: LLMProvider | null;
|
|
74
77
|
llmPremium: LLMProvider | null;
|
|
75
78
|
agenticConfig: AgenticConfig;
|
|
76
79
|
constructor(db: Kysely<any>, config: NOORMConfig);
|
|
80
|
+
private executionLock;
|
|
77
81
|
/**
|
|
78
82
|
* The "Soul-Searching" Loop: A top-level orchestration of all self-improvement rituals.
|
|
79
83
|
*/
|
|
@@ -30,6 +30,7 @@ import { SelfTestRegistry } from './improvement/SelfTestRegistry.js';
|
|
|
30
30
|
import { TelemetryOrchestrator } from './telemetry/TelemetryOrchestrator.js';
|
|
31
31
|
import { SkillSynthesizer } from './improvement/SkillSynthesizer.js';
|
|
32
32
|
import { EvolutionRitual } from './improvement/EvolutionRitual.js';
|
|
33
|
+
import { QuotaManager } from './improvement/QuotaManager.js';
|
|
33
34
|
/**
|
|
34
35
|
* Cortex is the unified facade for agentic operations.
|
|
35
36
|
* It coordinates sessions, memory, reflection, and knowledge.
|
|
@@ -68,6 +69,7 @@ export class Cortex {
|
|
|
68
69
|
telemetry;
|
|
69
70
|
skillSynthesizer;
|
|
70
71
|
evolutionRitual;
|
|
72
|
+
quotas;
|
|
71
73
|
llm;
|
|
72
74
|
llmFast;
|
|
73
75
|
llmPremium;
|
|
@@ -115,45 +117,64 @@ export class Cortex {
|
|
|
115
117
|
this.tests = new SelfTestRegistry(db, this, agenticConfig);
|
|
116
118
|
this.skillSynthesizer = new SkillSynthesizer(db, this, agenticConfig);
|
|
117
119
|
this.evolutionRitual = new EvolutionRitual(db, this, agenticConfig);
|
|
120
|
+
this.quotas = new QuotaManager(db, this, agenticConfig);
|
|
118
121
|
}
|
|
122
|
+
executionLock = false;
|
|
119
123
|
/**
|
|
120
124
|
* The "Soul-Searching" Loop: A top-level orchestration of all self-improvement rituals.
|
|
121
125
|
*/
|
|
122
126
|
async selfIterate() {
|
|
127
|
+
if (this.executionLock) {
|
|
128
|
+
console.warn('[Cortex] Self-iteration already in progress. Skipping pulse.');
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
this.executionLock = true;
|
|
123
132
|
console.log('[Cortex] Initiating Autonomous Soul-Searching Loop v2 (Deep Hardening Pass)...');
|
|
124
133
|
try {
|
|
125
134
|
// 1. Audit health & Run self-tests
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
console.warn('[Cortex] Audit issues detected before iteration:', audit.issues);
|
|
129
|
-
}
|
|
130
|
-
await this.tests.runAllProbes();
|
|
135
|
+
await this.#runIsolated('Audit', () => this.governor.performAudit());
|
|
136
|
+
await this.#runIsolated('Self-Tests', () => this.tests.runAllProbes());
|
|
131
137
|
// 2. Run background rituals (optimization, compression)
|
|
132
|
-
await this.rituals.runPendingRituals();
|
|
138
|
+
await this.#runIsolated('Rituals', () => this.rituals.runPendingRituals());
|
|
133
139
|
// 3. Learn from actions & Prune dead data
|
|
134
|
-
await this.refiner.refineActions();
|
|
135
|
-
await this.ablation.pruneZombies();
|
|
136
|
-
|
|
137
|
-
await this.ablation.monitorAblationPerformance();
|
|
140
|
+
await this.#runIsolated('Action Refinement', () => this.refiner.refineActions());
|
|
141
|
+
await this.#runIsolated('Zombie Pruning', () => this.ablation.pruneZombies());
|
|
142
|
+
await this.#runIsolated('Ablation Monitoring', () => this.ablation.monitorAblationPerformance());
|
|
138
143
|
// 4. Mutation & Strategy
|
|
139
|
-
await this.strategy.mutateStrategy();
|
|
144
|
+
await this.#runIsolated('Strategy Mutation', () => this.strategy.mutateStrategy());
|
|
140
145
|
// 5. High-Throughput Evolution Pulse
|
|
141
|
-
await this.evolutionRitual.execute();
|
|
146
|
+
await this.#runIsolated('Evolution Pulse', () => this.evolutionRitual.execute());
|
|
142
147
|
// 6. Broadcast knowledge & skills
|
|
143
|
-
await this.hive.broadcastKnowledge();
|
|
144
|
-
|
|
145
|
-
await this.skillSynthesizer.discoverAndSynthesize();
|
|
148
|
+
await this.#runIsolated('Knowledge Broadcast', () => this.hive.broadcastKnowledge());
|
|
149
|
+
await this.#runIsolated('Skill Synthesis', () => this.skillSynthesizer.discoverAndSynthesize());
|
|
146
150
|
// 7. Evolutionary pulse
|
|
147
|
-
await this.pilot.runSelfImprovementCycle();
|
|
151
|
+
await this.#runIsolated('Improvement Cycle', () => this.pilot.runSelfImprovementCycle());
|
|
148
152
|
console.log('[Cortex] Soul-Searching loop completed.');
|
|
149
153
|
}
|
|
150
154
|
catch (err) {
|
|
151
155
|
console.error('[Cortex] Soul-Searching loop failed:', err);
|
|
152
|
-
// Telemetry: track failure
|
|
153
156
|
await this.telemetry.track('system', 'error', 'Self-iteration failed', {
|
|
154
157
|
error: String(err),
|
|
155
158
|
});
|
|
156
159
|
}
|
|
160
|
+
finally {
|
|
161
|
+
this.executionLock = false;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Execute a ritual step in total isolation to prevent global collapse
|
|
166
|
+
*/
|
|
167
|
+
async #runIsolated(name, ritual) {
|
|
168
|
+
try {
|
|
169
|
+
await ritual();
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
console.error(`[Cortex] Ritual '${name}' failed but pulse continuing:`, error);
|
|
173
|
+
await this.telemetry.track('system', 'error', `Ritual failure: ${name}`, {
|
|
174
|
+
ritual: name,
|
|
175
|
+
error: String(error)
|
|
176
|
+
});
|
|
177
|
+
}
|
|
157
178
|
}
|
|
158
179
|
/**
|
|
159
180
|
* Helper to quickly resume a session and fill the context buffer
|
|
@@ -33,8 +33,12 @@ export declare class EpisodicMemory {
|
|
|
33
33
|
completeEpisode(episodeId: string | number, summary: string, metadata?: Record<string, any>): Promise<AgentEpisode>;
|
|
34
34
|
/**
|
|
35
35
|
* Get all episodes for a session.
|
|
36
|
+
* Refactored Phase 12: Paginated retrieval for high-volume sessions.
|
|
36
37
|
*/
|
|
37
|
-
getSessionEpisodes(sessionId: string | number
|
|
38
|
+
getSessionEpisodes(sessionId: string | number, options?: {
|
|
39
|
+
limit?: number;
|
|
40
|
+
offset?: number;
|
|
41
|
+
}): Promise<AgentEpisode[]>;
|
|
38
42
|
/**
|
|
39
43
|
* Get recently completed episodes across all sessions.
|
|
40
44
|
*/
|
|
@@ -39,12 +39,15 @@ export class EpisodicMemory {
|
|
|
39
39
|
return await this.db.transaction().execute(async (trx) => {
|
|
40
40
|
const existing = await trx
|
|
41
41
|
.selectFrom(this.episodesTable)
|
|
42
|
-
.
|
|
42
|
+
.selectAll()
|
|
43
43
|
.where('id', '=', episodeId)
|
|
44
|
+
.forUpdate() // Audit Phase 12: Atomic completion lock
|
|
44
45
|
.executeTakeFirst();
|
|
45
|
-
|
|
46
|
+
if (!existing)
|
|
47
|
+
throw new Error(`Episode with ID ${episodeId} not found`);
|
|
48
|
+
const oldMeta = typeof existing.metadata === 'string'
|
|
46
49
|
? JSON.parse(existing.metadata)
|
|
47
|
-
: existing
|
|
50
|
+
: existing.metadata || {};
|
|
48
51
|
const newMeta = { ...oldMeta, ...metadata };
|
|
49
52
|
const episode = await trx
|
|
50
53
|
.updateTable(this.episodesTable)
|
|
@@ -62,13 +65,17 @@ export class EpisodicMemory {
|
|
|
62
65
|
}
|
|
63
66
|
/**
|
|
64
67
|
* Get all episodes for a session.
|
|
68
|
+
* Refactored Phase 12: Paginated retrieval for high-volume sessions.
|
|
65
69
|
*/
|
|
66
|
-
async getSessionEpisodes(sessionId) {
|
|
70
|
+
async getSessionEpisodes(sessionId, options = {}) {
|
|
71
|
+
const { limit = 100, offset = 0 } = options;
|
|
67
72
|
const list = await this.typedDb
|
|
68
73
|
.selectFrom(this.episodesTable)
|
|
69
74
|
.selectAll()
|
|
70
75
|
.where('session_id', '=', sessionId)
|
|
71
76
|
.orderBy('start_time', 'desc')
|
|
77
|
+
.limit(limit)
|
|
78
|
+
.offset(offset)
|
|
72
79
|
.execute();
|
|
73
80
|
return list.map((e) => this.parseEpisode(e));
|
|
74
81
|
}
|
|
@@ -18,39 +18,45 @@ export class PersonaManager {
|
|
|
18
18
|
* Create or update a persona
|
|
19
19
|
*/
|
|
20
20
|
async upsertPersona(name, options = {}) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
name
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
.
|
|
39
|
-
|
|
40
|
-
|
|
21
|
+
return await this.db.transaction().execute(async (trx) => {
|
|
22
|
+
let query = trx
|
|
23
|
+
.selectFrom(this.personasTable)
|
|
24
|
+
.selectAll()
|
|
25
|
+
.where('name', '=', name);
|
|
26
|
+
// Audit Phase 13: Atomic identity lock (Skip for SQLite)
|
|
27
|
+
if (this.db.getExecutor().adapter?.constructor.name !== 'SqliteAdapter') {
|
|
28
|
+
query = query.forUpdate();
|
|
29
|
+
}
|
|
30
|
+
const existing = await query.executeTakeFirst();
|
|
31
|
+
const values = {
|
|
32
|
+
name,
|
|
33
|
+
role: options.role || null,
|
|
34
|
+
capabilities: options.capabilities
|
|
35
|
+
? JSON.stringify(options.capabilities)
|
|
36
|
+
: null,
|
|
37
|
+
policies: options.policies ? JSON.stringify(options.policies) : null,
|
|
38
|
+
metadata: options.metadata ? JSON.stringify(options.metadata) : null,
|
|
39
|
+
updated_at: new Date(),
|
|
40
|
+
};
|
|
41
|
+
if (existing) {
|
|
42
|
+
const updated = await trx
|
|
43
|
+
.updateTable(this.personasTable)
|
|
44
|
+
.set(values)
|
|
45
|
+
.where('id', '=', existing.id)
|
|
46
|
+
.returningAll()
|
|
47
|
+
.executeTakeFirstOrThrow();
|
|
48
|
+
return this.parsePersona(updated);
|
|
49
|
+
}
|
|
50
|
+
const created = await trx
|
|
51
|
+
.insertInto(this.personasTable)
|
|
52
|
+
.values({
|
|
53
|
+
...values,
|
|
54
|
+
created_at: new Date(),
|
|
55
|
+
})
|
|
41
56
|
.returningAll()
|
|
42
57
|
.executeTakeFirstOrThrow();
|
|
43
|
-
return this.parsePersona(
|
|
44
|
-
}
|
|
45
|
-
const created = await this.typedDb
|
|
46
|
-
.insertInto(this.personasTable)
|
|
47
|
-
.values({
|
|
48
|
-
...values,
|
|
49
|
-
created_at: new Date(),
|
|
50
|
-
})
|
|
51
|
-
.returningAll()
|
|
52
|
-
.executeTakeFirstOrThrow();
|
|
53
|
-
return this.parsePersona(created);
|
|
58
|
+
return this.parsePersona(created);
|
|
59
|
+
});
|
|
54
60
|
}
|
|
55
61
|
/**
|
|
56
62
|
* Get a persona by name
|
|
@@ -27,6 +27,7 @@ export declare class PolicyEnforcer {
|
|
|
27
27
|
private config;
|
|
28
28
|
private policiesTable;
|
|
29
29
|
private metricsTable;
|
|
30
|
+
private metricCache;
|
|
30
31
|
constructor(db: Kysely<any>, config?: AgenticConfig);
|
|
31
32
|
private get typedDb();
|
|
32
33
|
/**
|
|
@@ -37,7 +38,11 @@ export declare class PolicyEnforcer {
|
|
|
37
38
|
* Comprehensive policy evaluation against a context value.
|
|
38
39
|
* Supports thresholds, regex patterns, and cumulative budgets.
|
|
39
40
|
*/
|
|
40
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Comprehensive policy evaluation against a context value.
|
|
43
|
+
* Supports thresholds, regex patterns, and cumulative budgets.
|
|
44
|
+
*/
|
|
45
|
+
checkPolicy(name: string, value: any, visited?: Set<string>): Promise<{
|
|
41
46
|
allowed: boolean;
|
|
42
47
|
reason?: string;
|
|
43
48
|
}>;
|