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