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