noormme 1.2.2 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/agentic/ActionJournal.d.ts +6 -6
- package/dist/cjs/agentic/ActionJournal.js +10 -10
- package/dist/cjs/agentic/CapabilityManager.d.ts +5 -5
- package/dist/cjs/agentic/CapabilityManager.js +34 -19
- package/dist/cjs/agentic/Cortex.js +12 -13
- package/dist/cjs/agentic/PersonaManager.d.ts +4 -4
- package/dist/cjs/agentic/PersonaManager.js +18 -10
- package/dist/cjs/agentic/PolicyEnforcer.d.ts +4 -8
- package/dist/cjs/agentic/PolicyEnforcer.js +25 -22
- package/dist/cjs/agentic/improvement/AblationEngine.d.ts +7 -3
- package/dist/cjs/agentic/improvement/AblationEngine.js +51 -24
- package/dist/cjs/agentic/improvement/ActionRefiner.d.ts +1 -1
- package/dist/cjs/agentic/improvement/ActionRefiner.js +25 -15
- package/dist/cjs/agentic/improvement/EvolutionRitual.js +2 -1
- package/dist/cjs/agentic/improvement/EvolutionaryPilot.d.ts +1 -1
- package/dist/cjs/agentic/improvement/EvolutionaryPilot.js +17 -19
- package/dist/cjs/agentic/improvement/GovernanceManager.d.ts +5 -5
- package/dist/cjs/agentic/improvement/GovernanceManager.js +12 -22
- package/dist/cjs/agentic/improvement/HiveLink.d.ts +3 -3
- package/dist/cjs/agentic/improvement/HiveLink.js +15 -157
- package/dist/cjs/agentic/improvement/KnowledgeDistiller.d.ts +15 -29
- package/dist/cjs/agentic/improvement/KnowledgeDistiller.js +66 -349
- package/dist/cjs/agentic/improvement/QuotaManager.d.ts +1 -1
- package/dist/cjs/agentic/improvement/QuotaManager.js +11 -11
- package/dist/cjs/agentic/improvement/RecursiveReasoner.d.ts +4 -4
- package/dist/cjs/agentic/improvement/RecursiveReasoner.js +10 -10
- package/dist/cjs/agentic/improvement/ReflectionEngine.d.ts +2 -2
- package/dist/cjs/agentic/improvement/ReflectionEngine.js +2 -2
- package/dist/cjs/agentic/improvement/RitualOrchestrator.d.ts +3 -3
- package/dist/cjs/agentic/improvement/RitualOrchestrator.js +12 -9
- package/dist/cjs/agentic/improvement/SelfEvolution.d.ts +1 -1
- package/dist/cjs/agentic/improvement/SelfEvolution.js +21 -18
- package/dist/cjs/agentic/improvement/SelfTestRegistry.d.ts +1 -1
- package/dist/cjs/agentic/improvement/SelfTestRegistry.js +31 -19
- package/dist/cjs/agentic/improvement/SovereignMetrics.d.ts +6 -6
- package/dist/cjs/agentic/improvement/SovereignMetrics.js +12 -12
- package/dist/cjs/agentic/improvement/StrategicPlanner.d.ts +10 -54
- package/dist/cjs/agentic/improvement/StrategicPlanner.js +43 -396
- package/dist/cjs/agentic/improvement/distillation/ConflictChallenger.d.ts +3 -0
- package/dist/cjs/agentic/improvement/distillation/ConflictChallenger.js +54 -0
- package/dist/cjs/agentic/improvement/distillation/FactDistiller.d.ts +10 -0
- package/dist/cjs/agentic/improvement/distillation/FactDistiller.js +115 -0
- package/dist/cjs/agentic/improvement/distillation/KnowledgeConsolidator.d.ts +6 -0
- package/dist/cjs/agentic/improvement/distillation/KnowledgeConsolidator.js +63 -0
- package/dist/cjs/agentic/improvement/distillation/RelationshipArchitect.d.ts +6 -0
- package/dist/cjs/agentic/improvement/distillation/RelationshipArchitect.js +73 -0
- package/dist/cjs/agentic/improvement/governance/MaintenanceOracle.js +5 -5
- package/dist/cjs/agentic/improvement/governance/PersonaAuditor.js +41 -29
- package/dist/cjs/agentic/improvement/governance/RemediationEngine.js +8 -3
- package/dist/cjs/agentic/improvement/governance/SkillAuditor.js +17 -9
- package/dist/cjs/agentic/improvement/hive/DomainMaster.d.ts +4 -0
- package/dist/cjs/agentic/improvement/hive/DomainMaster.js +19 -0
- package/dist/cjs/agentic/improvement/hive/KnowledgePromoter.d.ts +6 -0
- package/dist/cjs/agentic/improvement/hive/KnowledgePromoter.js +56 -0
- package/dist/cjs/agentic/improvement/hive/SkillPropagator.d.ts +6 -0
- package/dist/cjs/agentic/improvement/hive/SkillPropagator.js +91 -0
- package/dist/cjs/agentic/improvement/strategy/EvolutionVerificator.d.ts +6 -0
- package/dist/cjs/agentic/improvement/strategy/EvolutionVerificator.js +48 -0
- package/dist/cjs/agentic/improvement/strategy/MutationEngine.d.ts +10 -0
- package/dist/cjs/agentic/improvement/strategy/MutationEngine.js +119 -0
- package/dist/cjs/agentic/improvement/strategy/PerformanceAnalyst.d.ts +6 -0
- package/dist/cjs/agentic/improvement/strategy/PerformanceAnalyst.js +84 -0
- package/dist/cjs/cli/index.js +0 -0
- package/dist/cjs/types/index.d.ts +8 -0
- package/dist/esm/agentic/ActionJournal.d.ts +6 -6
- package/dist/esm/agentic/ActionJournal.js +10 -10
- package/dist/esm/agentic/CapabilityManager.d.ts +5 -5
- package/dist/esm/agentic/CapabilityManager.js +34 -19
- package/dist/esm/agentic/Cortex.js +12 -13
- package/dist/esm/agentic/PersonaManager.d.ts +4 -4
- package/dist/esm/agentic/PersonaManager.js +18 -10
- package/dist/esm/agentic/PolicyEnforcer.d.ts +4 -8
- package/dist/esm/agentic/PolicyEnforcer.js +25 -22
- package/dist/esm/agentic/improvement/AblationEngine.d.ts +7 -3
- package/dist/esm/agentic/improvement/AblationEngine.js +51 -24
- package/dist/esm/agentic/improvement/ActionRefiner.d.ts +1 -1
- package/dist/esm/agentic/improvement/ActionRefiner.js +25 -15
- package/dist/esm/agentic/improvement/EvolutionRitual.js +2 -1
- package/dist/esm/agentic/improvement/EvolutionaryPilot.d.ts +1 -1
- package/dist/esm/agentic/improvement/EvolutionaryPilot.js +17 -19
- package/dist/esm/agentic/improvement/GovernanceManager.d.ts +5 -5
- package/dist/esm/agentic/improvement/GovernanceManager.js +12 -22
- package/dist/esm/agentic/improvement/HiveLink.d.ts +3 -3
- package/dist/esm/agentic/improvement/HiveLink.js +15 -157
- package/dist/esm/agentic/improvement/KnowledgeDistiller.d.ts +15 -29
- package/dist/esm/agentic/improvement/KnowledgeDistiller.js +66 -349
- package/dist/esm/agentic/improvement/QuotaManager.d.ts +1 -1
- package/dist/esm/agentic/improvement/QuotaManager.js +11 -11
- package/dist/esm/agentic/improvement/RecursiveReasoner.d.ts +4 -4
- package/dist/esm/agentic/improvement/RecursiveReasoner.js +10 -10
- package/dist/esm/agentic/improvement/ReflectionEngine.d.ts +2 -2
- package/dist/esm/agentic/improvement/ReflectionEngine.js +2 -2
- package/dist/esm/agentic/improvement/RitualOrchestrator.d.ts +3 -3
- package/dist/esm/agentic/improvement/RitualOrchestrator.js +12 -9
- package/dist/esm/agentic/improvement/SelfEvolution.d.ts +1 -1
- package/dist/esm/agentic/improvement/SelfEvolution.js +21 -18
- package/dist/esm/agentic/improvement/SelfTestRegistry.d.ts +1 -1
- package/dist/esm/agentic/improvement/SelfTestRegistry.js +31 -19
- package/dist/esm/agentic/improvement/SovereignMetrics.d.ts +6 -6
- package/dist/esm/agentic/improvement/SovereignMetrics.js +12 -12
- package/dist/esm/agentic/improvement/StrategicPlanner.d.ts +10 -54
- package/dist/esm/agentic/improvement/StrategicPlanner.js +43 -396
- package/dist/esm/agentic/improvement/distillation/ConflictChallenger.d.ts +3 -0
- package/dist/esm/agentic/improvement/distillation/ConflictChallenger.js +51 -0
- package/dist/esm/agentic/improvement/distillation/FactDistiller.d.ts +10 -0
- package/dist/esm/agentic/improvement/distillation/FactDistiller.js +112 -0
- package/dist/esm/agentic/improvement/distillation/KnowledgeConsolidator.d.ts +6 -0
- package/dist/esm/agentic/improvement/distillation/KnowledgeConsolidator.js +60 -0
- package/dist/esm/agentic/improvement/distillation/RelationshipArchitect.d.ts +6 -0
- package/dist/esm/agentic/improvement/distillation/RelationshipArchitect.js +70 -0
- package/dist/esm/agentic/improvement/governance/MaintenanceOracle.js +5 -5
- package/dist/esm/agentic/improvement/governance/PersonaAuditor.js +41 -29
- package/dist/esm/agentic/improvement/governance/RemediationEngine.js +8 -3
- package/dist/esm/agentic/improvement/governance/SkillAuditor.js +17 -9
- package/dist/esm/agentic/improvement/hive/DomainMaster.d.ts +4 -0
- package/dist/esm/agentic/improvement/hive/DomainMaster.js +16 -0
- package/dist/esm/agentic/improvement/hive/KnowledgePromoter.d.ts +6 -0
- package/dist/esm/agentic/improvement/hive/KnowledgePromoter.js +53 -0
- package/dist/esm/agentic/improvement/hive/SkillPropagator.d.ts +6 -0
- package/dist/esm/agentic/improvement/hive/SkillPropagator.js +88 -0
- package/dist/esm/agentic/improvement/strategy/EvolutionVerificator.d.ts +6 -0
- package/dist/esm/agentic/improvement/strategy/EvolutionVerificator.js +45 -0
- package/dist/esm/agentic/improvement/strategy/MutationEngine.d.ts +10 -0
- package/dist/esm/agentic/improvement/strategy/MutationEngine.js +116 -0
- package/dist/esm/agentic/improvement/strategy/PerformanceAnalyst.d.ts +6 -0
- package/dist/esm/agentic/improvement/strategy/PerformanceAnalyst.js +81 -0
- package/dist/esm/types/index.d.ts +8 -0
- package/package.json +40 -44
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Kysely } from '../kysely.js';
|
|
2
|
-
import type {
|
|
2
|
+
import type { AgentAction, AgenticConfig } from '../types/index.js';
|
|
3
3
|
import type { TelemetryOrchestrator } from './telemetry/TelemetryOrchestrator.js';
|
|
4
4
|
export interface ActionTable {
|
|
5
5
|
id: number | string;
|
|
@@ -30,26 +30,26 @@ export declare class ActionJournal {
|
|
|
30
30
|
/**
|
|
31
31
|
* Log an action (tool call)
|
|
32
32
|
*/
|
|
33
|
-
logAction(sessionId: string | number, toolName: string, args: Record<string, any>, messageId?: string | number): Promise<AgentAction>;
|
|
33
|
+
logAction(sessionId: string | number, toolName: string, args: Record<string, any>, messageId?: string | number, trxOrDb?: any): Promise<AgentAction>;
|
|
34
34
|
/**
|
|
35
35
|
* Update action with outcome
|
|
36
36
|
*/
|
|
37
|
-
recordOutcome(actionId: string | number, status: AgentAction['status'], outcome: string, durationMs?: number, metadata?: Record<string, any
|
|
37
|
+
recordOutcome(actionId: string | number, status: AgentAction['status'], outcome: string, durationMs?: number, metadata?: Record<string, any>, trxOrDb?: any): Promise<AgentAction>;
|
|
38
38
|
/**
|
|
39
39
|
* Get actions for a session with pagination
|
|
40
40
|
*/
|
|
41
41
|
getSessionActions(sessionId: string | number, options?: {
|
|
42
42
|
limit?: number;
|
|
43
43
|
cursor?: string | number;
|
|
44
|
-
}): Promise<AgentAction[]>;
|
|
44
|
+
}, trxOrDb?: any): Promise<AgentAction[]>;
|
|
45
45
|
/**
|
|
46
46
|
* Get actions by tool name across all sessions.
|
|
47
47
|
*/
|
|
48
|
-
getActionsByTool(toolName: string, limit?: number): Promise<AgentAction[]>;
|
|
48
|
+
getActionsByTool(toolName: string, limit?: number, trxOrDb?: any): Promise<AgentAction[]>;
|
|
49
49
|
/**
|
|
50
50
|
* Generate a report of tool failures.
|
|
51
51
|
*/
|
|
52
|
-
getFailureReport(): Promise<{
|
|
52
|
+
getFailureReport(trxOrDb?: any): Promise<{
|
|
53
53
|
toolName: string;
|
|
54
54
|
failureCount: number;
|
|
55
55
|
lastFailure: string;
|
|
@@ -22,8 +22,8 @@ class ActionJournal {
|
|
|
22
22
|
/**
|
|
23
23
|
* Log an action (tool call)
|
|
24
24
|
*/
|
|
25
|
-
async logAction(sessionId, toolName, args, messageId) {
|
|
26
|
-
const action = await
|
|
25
|
+
async logAction(sessionId, toolName, args, messageId, trxOrDb = this.db) {
|
|
26
|
+
const action = await trxOrDb
|
|
27
27
|
.insertInto(this.actionsTable)
|
|
28
28
|
.values({
|
|
29
29
|
session_id: sessionId,
|
|
@@ -45,8 +45,8 @@ class ActionJournal {
|
|
|
45
45
|
/**
|
|
46
46
|
* Update action with outcome
|
|
47
47
|
*/
|
|
48
|
-
async recordOutcome(actionId, status, outcome, durationMs, metadata) {
|
|
49
|
-
const action = await
|
|
48
|
+
async recordOutcome(actionId, status, outcome, durationMs, metadata, trxOrDb = this.db) {
|
|
49
|
+
const action = await trxOrDb
|
|
50
50
|
.updateTable(this.actionsTable)
|
|
51
51
|
.set({
|
|
52
52
|
status,
|
|
@@ -69,9 +69,9 @@ class ActionJournal {
|
|
|
69
69
|
/**
|
|
70
70
|
* Get actions for a session with pagination
|
|
71
71
|
*/
|
|
72
|
-
async getSessionActions(sessionId, options = {}) {
|
|
72
|
+
async getSessionActions(sessionId, options = {}, trxOrDb = this.db) {
|
|
73
73
|
const { limit = 100, cursor } = options;
|
|
74
|
-
let query =
|
|
74
|
+
let query = trxOrDb
|
|
75
75
|
.selectFrom(this.actionsTable)
|
|
76
76
|
.selectAll()
|
|
77
77
|
.where('session_id', '=', sessionId)
|
|
@@ -86,8 +86,8 @@ class ActionJournal {
|
|
|
86
86
|
/**
|
|
87
87
|
* Get actions by tool name across all sessions.
|
|
88
88
|
*/
|
|
89
|
-
async getActionsByTool(toolName, limit = 50) {
|
|
90
|
-
const actions = await
|
|
89
|
+
async getActionsByTool(toolName, limit = 50, trxOrDb = this.db) {
|
|
90
|
+
const actions = await trxOrDb
|
|
91
91
|
.selectFrom(this.actionsTable)
|
|
92
92
|
.selectAll()
|
|
93
93
|
.where('tool_name', '=', toolName)
|
|
@@ -99,10 +99,10 @@ class ActionJournal {
|
|
|
99
99
|
/**
|
|
100
100
|
* Generate a report of tool failures.
|
|
101
101
|
*/
|
|
102
|
-
async getFailureReport() {
|
|
102
|
+
async getFailureReport(trxOrDb = this.db) {
|
|
103
103
|
// Audit Phase 19: Sliding window (default 7 days) to prevent OOM/slow scans
|
|
104
104
|
const windowStart = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
|
|
105
|
-
const results = await
|
|
105
|
+
const results = await trxOrDb
|
|
106
106
|
.selectFrom(this.actionsTable)
|
|
107
107
|
.select([
|
|
108
108
|
'tool_name',
|
|
@@ -30,24 +30,24 @@ export declare class CapabilityManager {
|
|
|
30
30
|
/**
|
|
31
31
|
* Register or update a capability (skill)
|
|
32
32
|
*/
|
|
33
|
-
registerCapability(name: string, version: string, description?: string, metadata?: Record<string, any
|
|
33
|
+
registerCapability(name: string, version: string, description?: string, metadata?: Record<string, any>, trxOrDb?: any): Promise<AgentCapability>;
|
|
34
34
|
/**
|
|
35
35
|
* Update reliability based on action outcome using a damped moving average.
|
|
36
36
|
* Manages the lifecycle of emergent skills (sandbox -> verified / blacklisted).
|
|
37
37
|
*/
|
|
38
|
-
reportOutcome(name: string, success: boolean): Promise<void>;
|
|
38
|
+
reportOutcome(name: string, success: boolean, trxOrDb?: any): Promise<void>;
|
|
39
39
|
/**
|
|
40
40
|
* Get reliability score for a capability.
|
|
41
41
|
*/
|
|
42
|
-
getReliability(name: string): Promise<number>;
|
|
42
|
+
getReliability(name: string, trxOrDb?: any): Promise<number>;
|
|
43
43
|
/**
|
|
44
44
|
* Get all registered capabilities, optionally filtered by status
|
|
45
45
|
*/
|
|
46
|
-
getCapabilities(status?: AgentCapability['status']): Promise<AgentCapability[]>;
|
|
46
|
+
getCapabilities(status?: AgentCapability['status'], trxOrDb?: any): Promise<AgentCapability[]>;
|
|
47
47
|
/**
|
|
48
48
|
* Validate if a persona has access to a specific capability (Sandbox Enforcement).
|
|
49
49
|
*/
|
|
50
|
-
validateCapabilityAccess(personaId: string | number, capabilityName: string): Promise<{
|
|
50
|
+
validateCapabilityAccess(personaId: string | number, capabilityName: string, trxOrDb?: any): Promise<{
|
|
51
51
|
allowed: boolean;
|
|
52
52
|
reason?: string;
|
|
53
53
|
}>;
|
|
@@ -30,8 +30,8 @@ class CapabilityManager {
|
|
|
30
30
|
/**
|
|
31
31
|
* Register or update a capability (skill)
|
|
32
32
|
*/
|
|
33
|
-
async registerCapability(name, version, description, metadata = {}) {
|
|
34
|
-
|
|
33
|
+
async registerCapability(name, version, description, metadata = {}, trxOrDb = this.db) {
|
|
34
|
+
const runner = async (trx) => {
|
|
35
35
|
const existing = await trx
|
|
36
36
|
.selectFrom(this.capabilitiesTable)
|
|
37
37
|
.selectAll()
|
|
@@ -74,21 +74,29 @@ class CapabilityManager {
|
|
|
74
74
|
.returningAll()
|
|
75
75
|
.executeTakeFirstOrThrow();
|
|
76
76
|
return this.parseCapability(created);
|
|
77
|
-
}
|
|
77
|
+
};
|
|
78
|
+
if (trxOrDb && trxOrDb !== this.db) {
|
|
79
|
+
return await runner(trxOrDb);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
return await this.db.transaction().execute(runner);
|
|
83
|
+
}
|
|
78
84
|
}
|
|
79
85
|
/**
|
|
80
86
|
* Update reliability based on action outcome using a damped moving average.
|
|
81
87
|
* Manages the lifecycle of emergent skills (sandbox -> verified / blacklisted).
|
|
82
88
|
*/
|
|
83
|
-
async reportOutcome(name, success) {
|
|
84
|
-
|
|
89
|
+
async reportOutcome(name, success, trxOrDb = this.db) {
|
|
90
|
+
const runner = async (trx) => {
|
|
85
91
|
let query = trx
|
|
86
92
|
.selectFrom(this.capabilitiesTable)
|
|
87
93
|
.selectAll()
|
|
88
94
|
.where('name', '=', name)
|
|
89
95
|
.orderBy('updated_at', 'desc');
|
|
90
96
|
// PRODUCTION HARDENING: Lock row to prevent RMW race (Skip for SQLite)
|
|
91
|
-
|
|
97
|
+
const executor = trx.getExecutor();
|
|
98
|
+
const adapterName = executor?.adapter?.constructor?.name || executor?.dialect?.constructor?.name || '';
|
|
99
|
+
if (!adapterName.toLowerCase().includes('sqlite')) {
|
|
92
100
|
query = query.forUpdate();
|
|
93
101
|
}
|
|
94
102
|
const capability = await query.executeTakeFirst();
|
|
@@ -189,13 +197,19 @@ class CapabilityManager {
|
|
|
189
197
|
.where('id', '=', cap.id)
|
|
190
198
|
.execute();
|
|
191
199
|
}
|
|
192
|
-
}
|
|
200
|
+
};
|
|
201
|
+
if (trxOrDb && trxOrDb !== this.db) {
|
|
202
|
+
await runner(trxOrDb);
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
await this.db.transaction().execute(runner);
|
|
206
|
+
}
|
|
193
207
|
}
|
|
194
208
|
/**
|
|
195
209
|
* Get reliability score for a capability.
|
|
196
210
|
*/
|
|
197
|
-
async getReliability(name) {
|
|
198
|
-
const cap = await
|
|
211
|
+
async getReliability(name, trxOrDb = this.db) {
|
|
212
|
+
const cap = await trxOrDb
|
|
199
213
|
.selectFrom(this.capabilitiesTable)
|
|
200
214
|
.select('reliability')
|
|
201
215
|
.where('name', '=', name)
|
|
@@ -206,8 +220,8 @@ class CapabilityManager {
|
|
|
206
220
|
/**
|
|
207
221
|
* Get all registered capabilities, optionally filtered by status
|
|
208
222
|
*/
|
|
209
|
-
async getCapabilities(status) {
|
|
210
|
-
let query =
|
|
223
|
+
async getCapabilities(status, trxOrDb = this.db) {
|
|
224
|
+
let query = trxOrDb
|
|
211
225
|
.selectFrom(this.capabilitiesTable)
|
|
212
226
|
.selectAll();
|
|
213
227
|
if (status) {
|
|
@@ -223,9 +237,10 @@ class CapabilityManager {
|
|
|
223
237
|
// Filter to latest/best variants if many versions exist
|
|
224
238
|
const unique = new Map();
|
|
225
239
|
for (const c of list) {
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
240
|
+
const cap = c;
|
|
241
|
+
const meta = typeof cap.metadata === 'string' ? JSON.parse(cap.metadata) : (cap.metadata || {});
|
|
242
|
+
if (!unique.has(cap.name) || meta.is_alpha) {
|
|
243
|
+
unique.set(cap.name, cap);
|
|
229
244
|
}
|
|
230
245
|
}
|
|
231
246
|
return Array.from(unique.values()).map((c) => this.parseCapability(c));
|
|
@@ -233,13 +248,13 @@ class CapabilityManager {
|
|
|
233
248
|
/**
|
|
234
249
|
* Validate if a persona has access to a specific capability (Sandbox Enforcement).
|
|
235
250
|
*/
|
|
236
|
-
async validateCapabilityAccess(personaId, capabilityName) {
|
|
237
|
-
const persona = await this.cortex.personas.getPersona(String(personaId)) ||
|
|
238
|
-
await
|
|
251
|
+
async validateCapabilityAccess(personaId, capabilityName, trxOrDb = this.db) {
|
|
252
|
+
const persona = await this.cortex.personas.getPersona(String(personaId), trxOrDb) ||
|
|
253
|
+
await trxOrDb.selectFrom(this.config.personasTable || 'agent_personas')
|
|
239
254
|
.selectAll()
|
|
240
255
|
.where('id', '=', personaId)
|
|
241
256
|
.executeTakeFirst()
|
|
242
|
-
.then(p => p ? this.cortex.personas.parsePersona(p) : null);
|
|
257
|
+
.then((p) => p ? this.cortex.personas.parsePersona(p) : null);
|
|
243
258
|
if (!persona) {
|
|
244
259
|
return { allowed: false, reason: `Persona ${personaId} not found.` };
|
|
245
260
|
}
|
|
@@ -251,7 +266,7 @@ class CapabilityManager {
|
|
|
251
266
|
};
|
|
252
267
|
}
|
|
253
268
|
// Check if capability is blacklisted globally
|
|
254
|
-
const cap = await
|
|
269
|
+
const cap = await trxOrDb
|
|
255
270
|
.selectFrom(this.capabilitiesTable)
|
|
256
271
|
.select(['status', 'reliability'])
|
|
257
272
|
.where('name', '=', capabilityName)
|
|
@@ -133,23 +133,22 @@ class Cortex {
|
|
|
133
133
|
this.executionLock = true;
|
|
134
134
|
console.log('[Cortex] Initiating Autonomous Soul-Searching Loop v2 (Deep Hardening Pass)...');
|
|
135
135
|
try {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
136
|
+
await this.db.transaction().execute(async (trx) => {
|
|
137
|
+
// 1. Audit health & Run self-tests
|
|
138
|
+
await this.#runIsolated('Audit', () => this.governor.performAudit(trx));
|
|
139
|
+
await this.#runIsolated('Self-Tests', () => this.tests.runAllProbes(trx));
|
|
140
|
+
// 2. Run background rituals (optimization, compression)
|
|
141
|
+
await this.#runIsolated('Rituals', () => this.rituals.runPendingRituals(trx));
|
|
142
|
+
// 3. Learn from actions & Prune dead data
|
|
143
|
+
await this.#runIsolated('Action Refinement', () => this.refiner.refineActions(trx));
|
|
144
|
+
await this.#runIsolated('Zombie Pruning', () => this.ablation.pruneZombies(30, trx));
|
|
145
|
+
await this.#runIsolated('Ablation Monitoring', () => this.ablation.monitorAblationPerformance(trx));
|
|
146
|
+
});
|
|
147
|
+
// These are often long-running or have their own internal transaction boundaries
|
|
146
148
|
await this.#runIsolated('Strategy Mutation', () => this.strategy.mutateStrategy());
|
|
147
|
-
// 5. High-Throughput Evolution Pulse
|
|
148
149
|
await this.#runIsolated('Evolution Pulse', () => this.evolutionRitual.execute());
|
|
149
|
-
// 6. Broadcast knowledge & skills
|
|
150
150
|
await this.#runIsolated('Knowledge Broadcast', () => this.hive.broadcastKnowledge());
|
|
151
151
|
await this.#runIsolated('Skill Synthesis', () => this.skillSynthesizer.discoverAndSynthesize());
|
|
152
|
-
// 7. Evolutionary pulse
|
|
153
152
|
await this.#runIsolated('Improvement Cycle', () => this.pilot.runSelfImprovementCycle());
|
|
154
153
|
console.log('[Cortex] Soul-Searching loop completed.');
|
|
155
154
|
}
|
|
@@ -30,18 +30,18 @@ export declare class PersonaManager {
|
|
|
30
30
|
capabilities?: string[];
|
|
31
31
|
policies?: string[];
|
|
32
32
|
metadata?: Record<string, any>;
|
|
33
|
-
}): Promise<AgentPersona>;
|
|
33
|
+
}, trxOrDb?: any): Promise<AgentPersona>;
|
|
34
34
|
/**
|
|
35
35
|
* Get a persona by name
|
|
36
36
|
*/
|
|
37
|
-
getPersona(name: string): Promise<AgentPersona | null>;
|
|
37
|
+
getPersona(name: string, trxOrDb?: any): Promise<AgentPersona | null>;
|
|
38
38
|
/**
|
|
39
39
|
* Delete a persona by name
|
|
40
40
|
*/
|
|
41
|
-
deletePersona(name: string): Promise<boolean>;
|
|
41
|
+
deletePersona(name: string, trxOrDb?: any): Promise<boolean>;
|
|
42
42
|
/**
|
|
43
43
|
* List all personas
|
|
44
44
|
*/
|
|
45
|
-
listPersonas(): Promise<AgentPersona[]>;
|
|
45
|
+
listPersonas(trxOrDb?: any): Promise<AgentPersona[]>;
|
|
46
46
|
private parsePersona;
|
|
47
47
|
}
|
|
@@ -19,14 +19,16 @@ class PersonaManager {
|
|
|
19
19
|
/**
|
|
20
20
|
* Create or update a persona
|
|
21
21
|
*/
|
|
22
|
-
async upsertPersona(name, options = {}) {
|
|
23
|
-
|
|
22
|
+
async upsertPersona(name, options = {}, trxOrDb = this.db) {
|
|
23
|
+
const runner = async (trx) => {
|
|
24
24
|
let query = trx
|
|
25
25
|
.selectFrom(this.personasTable)
|
|
26
26
|
.selectAll()
|
|
27
27
|
.where('name', '=', name);
|
|
28
28
|
// Audit Phase 13: Atomic identity lock (Skip for SQLite)
|
|
29
|
-
|
|
29
|
+
const executor = trx.getExecutor();
|
|
30
|
+
const adapterName = executor?.adapter?.constructor?.name || executor?.dialect?.constructor?.name || '';
|
|
31
|
+
if (!adapterName.toLowerCase().includes('sqlite')) {
|
|
30
32
|
query = query.forUpdate();
|
|
31
33
|
}
|
|
32
34
|
const existing = await query.executeTakeFirst();
|
|
@@ -58,13 +60,19 @@ class PersonaManager {
|
|
|
58
60
|
.returningAll()
|
|
59
61
|
.executeTakeFirstOrThrow();
|
|
60
62
|
return this.parsePersona(created);
|
|
61
|
-
}
|
|
63
|
+
};
|
|
64
|
+
if (trxOrDb && trxOrDb !== this.db) {
|
|
65
|
+
return await runner(trxOrDb);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
return await this.db.transaction().execute(runner);
|
|
69
|
+
}
|
|
62
70
|
}
|
|
63
71
|
/**
|
|
64
72
|
* Get a persona by name
|
|
65
73
|
*/
|
|
66
|
-
async getPersona(name) {
|
|
67
|
-
const persona = await
|
|
74
|
+
async getPersona(name, trxOrDb = this.db) {
|
|
75
|
+
const persona = await trxOrDb
|
|
68
76
|
.selectFrom(this.personasTable)
|
|
69
77
|
.selectAll()
|
|
70
78
|
.where('name', '=', name)
|
|
@@ -74,8 +82,8 @@ class PersonaManager {
|
|
|
74
82
|
/**
|
|
75
83
|
* Delete a persona by name
|
|
76
84
|
*/
|
|
77
|
-
async deletePersona(name) {
|
|
78
|
-
const result = await
|
|
85
|
+
async deletePersona(name, trxOrDb = this.db) {
|
|
86
|
+
const result = await trxOrDb
|
|
79
87
|
.deleteFrom(this.personasTable)
|
|
80
88
|
.where('name', '=', name)
|
|
81
89
|
.executeTakeFirst();
|
|
@@ -84,8 +92,8 @@ class PersonaManager {
|
|
|
84
92
|
/**
|
|
85
93
|
* List all personas
|
|
86
94
|
*/
|
|
87
|
-
async listPersonas() {
|
|
88
|
-
const list = await
|
|
95
|
+
async listPersonas(trxOrDb = this.db) {
|
|
96
|
+
const list = await trxOrDb
|
|
89
97
|
.selectFrom(this.personasTable)
|
|
90
98
|
.selectAll()
|
|
91
99
|
.orderBy('name', 'asc')
|
|
@@ -33,30 +33,26 @@ export declare class PolicyEnforcer {
|
|
|
33
33
|
/**
|
|
34
34
|
* Define or update a policy with robust validation.
|
|
35
35
|
*/
|
|
36
|
-
definePolicy(name: string, type: AgentPolicy['type'], definition: Record<string, any>, isEnabled?: boolean): Promise<AgentPolicy>;
|
|
36
|
+
definePolicy(name: string, type: AgentPolicy['type'], definition: Record<string, any>, isEnabled?: boolean, trxOrDb?: any): Promise<AgentPolicy>;
|
|
37
37
|
/**
|
|
38
38
|
* Comprehensive policy evaluation against a context value.
|
|
39
39
|
* Supports thresholds, regex patterns, and cumulative budgets.
|
|
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
|
+
checkPolicy(name: string, value: any, visited?: Set<string>, trxOrDb?: any): Promise<{
|
|
46
42
|
allowed: boolean;
|
|
47
43
|
reason?: string;
|
|
48
44
|
}>;
|
|
49
45
|
/**
|
|
50
46
|
* Evaluate a full context (object) against all applicable policies.
|
|
51
47
|
*/
|
|
52
|
-
evaluateContext(context: Record<string, any
|
|
48
|
+
evaluateContext(context: Record<string, any>, trxOrDb?: any): Promise<{
|
|
53
49
|
allowed: boolean;
|
|
54
50
|
violations: string[];
|
|
55
51
|
}>;
|
|
56
52
|
/**
|
|
57
53
|
* Get all active policies.
|
|
58
54
|
*/
|
|
59
|
-
getActivePolicies(): Promise<AgentPolicy[]>;
|
|
55
|
+
getActivePolicies(trxOrDb?: any): Promise<AgentPolicy[]>;
|
|
60
56
|
private getCumulativeMetric;
|
|
61
57
|
private parsePolicy;
|
|
62
58
|
}
|
|
@@ -23,14 +23,15 @@ class PolicyEnforcer {
|
|
|
23
23
|
/**
|
|
24
24
|
* Define or update a policy with robust validation.
|
|
25
25
|
*/
|
|
26
|
-
async definePolicy(name, type, definition, isEnabled = true) {
|
|
27
|
-
|
|
26
|
+
async definePolicy(name, type, definition, isEnabled = true, trxOrDb = this.db) {
|
|
27
|
+
const runner = async (trx) => {
|
|
28
28
|
let query = trx
|
|
29
29
|
.selectFrom(this.policiesTable)
|
|
30
30
|
.select('id')
|
|
31
31
|
.where('name', '=', name);
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
const executor = trx.getExecutor();
|
|
33
|
+
const adapterName = executor?.adapter?.constructor?.name || executor?.dialect?.constructor?.name || '';
|
|
34
|
+
if (!adapterName.toLowerCase().includes('sqlite')) {
|
|
34
35
|
query = query.forUpdate();
|
|
35
36
|
}
|
|
36
37
|
const existing = await query.executeTakeFirst();
|
|
@@ -61,17 +62,19 @@ class PolicyEnforcer {
|
|
|
61
62
|
.returningAll()
|
|
62
63
|
.executeTakeFirstOrThrow();
|
|
63
64
|
return this.parsePolicy(created);
|
|
64
|
-
}
|
|
65
|
+
};
|
|
66
|
+
if (trxOrDb && trxOrDb !== this.db) {
|
|
67
|
+
return await runner(trxOrDb);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
return await this.db.transaction().execute(runner);
|
|
71
|
+
}
|
|
65
72
|
}
|
|
66
73
|
/**
|
|
67
74
|
* Comprehensive policy evaluation against a context value.
|
|
68
75
|
* Supports thresholds, regex patterns, and cumulative budgets.
|
|
69
76
|
*/
|
|
70
|
-
|
|
71
|
-
* Comprehensive policy evaluation against a context value.
|
|
72
|
-
* Supports thresholds, regex patterns, and cumulative budgets.
|
|
73
|
-
*/
|
|
74
|
-
async checkPolicy(name, value, visited = new Set()) {
|
|
77
|
+
async checkPolicy(name, value, visited = new Set(), trxOrDb = this.db) {
|
|
75
78
|
// Audit Pass 6: Re-entrancy / Circular Dependency Detection
|
|
76
79
|
if (visited.has(name)) {
|
|
77
80
|
return {
|
|
@@ -80,7 +83,7 @@ class PolicyEnforcer {
|
|
|
80
83
|
};
|
|
81
84
|
}
|
|
82
85
|
visited.add(name);
|
|
83
|
-
const policy = await
|
|
86
|
+
const policy = await trxOrDb
|
|
84
87
|
.selectFrom(this.policiesTable)
|
|
85
88
|
.selectAll()
|
|
86
89
|
.where('name', '=', name)
|
|
@@ -111,7 +114,7 @@ class PolicyEnforcer {
|
|
|
111
114
|
if (def.pattern.length > 500) {
|
|
112
115
|
return { allowed: false, reason: `Policy '${name}' regex pattern too long (potential ReDoS risk)` };
|
|
113
116
|
}
|
|
114
|
-
const dangerousPatterns = /(\*|\+)\1|\(\.\*\)
|
|
117
|
+
const dangerousPatterns = /(\*|\+)\1|\(\.\*\)\*|\(\[.*\](\*|\+)\)(\*|\+)|\(.*\+\)\+/;
|
|
115
118
|
if (dangerousPatterns.test(def.pattern)) {
|
|
116
119
|
return { allowed: false, reason: `Policy '${name}' contains potentially dangerous ReDoS pattern` };
|
|
117
120
|
}
|
|
@@ -139,7 +142,7 @@ class PolicyEnforcer {
|
|
|
139
142
|
if (policy.type === 'budget' && def.metricName) {
|
|
140
143
|
const period = def.period || 'daily';
|
|
141
144
|
const limit = def.limit || 0;
|
|
142
|
-
const total = await this.getCumulativeMetric(def.metricName, period);
|
|
145
|
+
const total = await this.getCumulativeMetric(def.metricName, period, trxOrDb);
|
|
143
146
|
if (total + (typeof value === 'number' ? value : 0) > limit) {
|
|
144
147
|
return {
|
|
145
148
|
allowed: false,
|
|
@@ -151,7 +154,7 @@ class PolicyEnforcer {
|
|
|
151
154
|
// Audit Pass 6: Moved from evaluateContext to checkPolicy for deeper nesting support
|
|
152
155
|
if (def.dependsOn && Array.isArray(def.dependsOn)) {
|
|
153
156
|
for (const depName of def.dependsOn) {
|
|
154
|
-
const result = await this.checkPolicy(depName, value, new Set(visited));
|
|
157
|
+
const result = await this.checkPolicy(depName, value, new Set(visited), trxOrDb);
|
|
155
158
|
if (!result.allowed) {
|
|
156
159
|
return { allowed: false, reason: `Composite block: ${name} -> ${result.reason}` };
|
|
157
160
|
}
|
|
@@ -162,19 +165,19 @@ class PolicyEnforcer {
|
|
|
162
165
|
/**
|
|
163
166
|
* Evaluate a full context (object) against all applicable policies.
|
|
164
167
|
*/
|
|
165
|
-
async evaluateContext(context) {
|
|
166
|
-
const policies = await this.getActivePolicies();
|
|
168
|
+
async evaluateContext(context, trxOrDb = this.db) {
|
|
169
|
+
const policies = await this.getActivePolicies(trxOrDb);
|
|
167
170
|
const violations = [];
|
|
168
171
|
for (const policy of policies) {
|
|
169
172
|
// If the context has a key matching the policy name, check it
|
|
170
173
|
if (context[policy.name] !== undefined) {
|
|
171
|
-
const result = await this.checkPolicy(policy.name, context[policy.name]);
|
|
174
|
+
const result = await this.checkPolicy(policy.name, context[policy.name], new Set(), trxOrDb);
|
|
172
175
|
if (!result.allowed)
|
|
173
176
|
violations.push(result.reason);
|
|
174
177
|
}
|
|
175
178
|
// Check for type-specific global policies (e.g. all privacy policies)
|
|
176
179
|
if (policy.type === 'privacy' && context.content) {
|
|
177
|
-
const result = await this.checkPolicy(policy.name, context.content);
|
|
180
|
+
const result = await this.checkPolicy(policy.name, context.content, new Set(), trxOrDb);
|
|
178
181
|
if (!result.allowed)
|
|
179
182
|
violations.push(result.reason);
|
|
180
183
|
}
|
|
@@ -187,15 +190,15 @@ class PolicyEnforcer {
|
|
|
187
190
|
/**
|
|
188
191
|
* Get all active policies.
|
|
189
192
|
*/
|
|
190
|
-
async getActivePolicies() {
|
|
191
|
-
const list = await
|
|
193
|
+
async getActivePolicies(trxOrDb = this.db) {
|
|
194
|
+
const list = await trxOrDb
|
|
192
195
|
.selectFrom(this.policiesTable)
|
|
193
196
|
.selectAll()
|
|
194
197
|
.where('is_enabled', '=', true)
|
|
195
198
|
.execute();
|
|
196
199
|
return list.map((p) => this.parsePolicy(p));
|
|
197
200
|
}
|
|
198
|
-
async getCumulativeMetric(metricName, period) {
|
|
201
|
+
async getCumulativeMetric(metricName, period, trxOrDb = this.db) {
|
|
199
202
|
const cacheKey = `${metricName}:${period}`;
|
|
200
203
|
const cached = this.metricCache.get(cacheKey);
|
|
201
204
|
const now = new Date();
|
|
@@ -217,7 +220,7 @@ class PolicyEnforcer {
|
|
|
217
220
|
else if (period === 'hourly') {
|
|
218
221
|
cutoff = new Date(now.getTime() - 3600000);
|
|
219
222
|
}
|
|
220
|
-
const result = await
|
|
223
|
+
const result = await trxOrDb
|
|
221
224
|
.selectFrom(this.metricsTable)
|
|
222
225
|
.select((eb) => eb.fn.sum('metric_value').as('total'))
|
|
223
226
|
.where('metric_name', '=', metricName)
|
|
@@ -14,22 +14,26 @@ export declare class AblationEngine {
|
|
|
14
14
|
private linksTable;
|
|
15
15
|
constructor(db: Kysely<any>, cortex: Cortex, config?: AgenticConfig);
|
|
16
16
|
private get typedDb();
|
|
17
|
+
/**
|
|
18
|
+
* Helper to apply forUpdate only where supported (Skip for SQLite)
|
|
19
|
+
*/
|
|
20
|
+
private withLock;
|
|
17
21
|
/**
|
|
18
22
|
* Identify "Zombies": Items that have never been retrieved/hit and are old.
|
|
19
23
|
*/
|
|
20
|
-
pruneZombies(thresholdDays?: number): Promise<number>;
|
|
24
|
+
pruneZombies(thresholdDays?: number, trxOrDb?: any): Promise<number>;
|
|
21
25
|
/**
|
|
22
26
|
* Monitor Performance and perform Intelligent Rollbacks.
|
|
23
27
|
* Prioritizes recovery of items with highest historical hit counts.
|
|
24
28
|
*/
|
|
25
|
-
monitorAblationPerformance(): Promise<{
|
|
29
|
+
monitorAblationPerformance(trxOrDb?: any): Promise<{
|
|
26
30
|
status: 'stable' | 'degraded';
|
|
27
31
|
recoveredCount: number;
|
|
28
32
|
}>;
|
|
29
33
|
/**
|
|
30
34
|
* Conduct an "Ablation Test": Temporarily disable a knowledge item.
|
|
31
35
|
*/
|
|
32
|
-
testAblation(id: string | number): Promise<boolean>;
|
|
36
|
+
testAblation(id: string | number, trxOrDb?: any): Promise<boolean>;
|
|
33
37
|
/**
|
|
34
38
|
* Restore an ablated knowledge item to its original state.
|
|
35
39
|
*/
|