noormme 1.2.3 → 1.2.5
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 +35 -24
- package/dist/cjs/agentic/Cortex.js +12 -13
- package/dist/cjs/agentic/EpisodicMemory.js +4 -3
- package/dist/cjs/agentic/PersonaManager.d.ts +4 -4
- package/dist/cjs/agentic/PersonaManager.js +19 -15
- package/dist/cjs/agentic/PolicyEnforcer.d.ts +4 -8
- package/dist/cjs/agentic/PolicyEnforcer.js +26 -26
- package/dist/cjs/agentic/ResourceMonitor.js +2 -1
- package/dist/cjs/agentic/SessionManager.js +7 -6
- package/dist/cjs/agentic/improvement/AblationEngine.d.ts +3 -3
- package/dist/cjs/agentic/improvement/AblationEngine.js +41 -24
- package/dist/cjs/agentic/improvement/ActionRefiner.d.ts +1 -1
- package/dist/cjs/agentic/improvement/ActionRefiner.js +21 -15
- package/dist/cjs/agentic/improvement/ConflictResolver.js +4 -3
- 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/GoalArchitect.js +10 -9
- 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 +69 -351
- 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 +27 -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 +7 -11
- 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 +57 -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/agentic/util/db-utils.d.ts +4 -0
- package/dist/cjs/agentic/util/db-utils.js +27 -0
- package/dist/cjs/cli/index.js +0 -0
- package/dist/cjs/helpers/agent-schema.js +1 -0
- package/dist/cjs/testing/test-utils.js +2 -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 +35 -24
- package/dist/esm/agentic/Cortex.js +12 -13
- package/dist/esm/agentic/EpisodicMemory.js +4 -3
- package/dist/esm/agentic/PersonaManager.d.ts +4 -4
- package/dist/esm/agentic/PersonaManager.js +19 -15
- package/dist/esm/agentic/PolicyEnforcer.d.ts +4 -8
- package/dist/esm/agentic/PolicyEnforcer.js +26 -26
- package/dist/esm/agentic/ResourceMonitor.js +2 -1
- package/dist/esm/agentic/SessionManager.js +7 -6
- package/dist/esm/agentic/improvement/AblationEngine.d.ts +3 -3
- package/dist/esm/agentic/improvement/AblationEngine.js +41 -24
- package/dist/esm/agentic/improvement/ActionRefiner.d.ts +1 -1
- package/dist/esm/agentic/improvement/ActionRefiner.js +21 -15
- package/dist/esm/agentic/improvement/ConflictResolver.js +4 -3
- 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/GoalArchitect.js +10 -9
- 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 +69 -351
- 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 +27 -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 +7 -11
- 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 +54 -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/agentic/util/db-utils.d.ts +4 -0
- package/dist/esm/agentic/util/db-utils.js +25 -0
- package/dist/esm/helpers/agent-schema.js +1 -0
- package/dist/esm/testing/test-utils.js +2 -0
- package/dist/esm/types/index.d.ts +8 -0
- package/package.json +40 -44
|
@@ -1,5 +1,9 @@
|
|
|
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';
|
|
6
|
+
import { withLock } from '../util/db-utils.js';
|
|
3
7
|
/**
|
|
4
8
|
* KnowledgeDistiller extracts structured "KnowledgeItems" from longtail history,
|
|
5
9
|
* allowing agents to build a permanent, queryable knowledge base.
|
|
@@ -10,198 +14,102 @@ export class KnowledgeDistiller {
|
|
|
10
14
|
knowledgeTable;
|
|
11
15
|
linksTable;
|
|
12
16
|
bloomFilter = new Set();
|
|
13
|
-
|
|
14
|
-
|
|
17
|
+
distiller;
|
|
18
|
+
challenger;
|
|
19
|
+
architect;
|
|
20
|
+
consolidator;
|
|
21
|
+
constructor(db, config = {}) {
|
|
15
22
|
this.db = db;
|
|
16
23
|
this.config = config;
|
|
17
24
|
this.knowledgeTable = config.knowledgeTable || 'agent_knowledge_base';
|
|
18
25
|
this.linksTable = 'agent_knowledge_links';
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
get typedDb() {
|
|
24
|
-
return this.db;
|
|
26
|
+
this.distiller = new FactDistiller();
|
|
27
|
+
this.challenger = new ConflictChallenger();
|
|
28
|
+
this.architect = new RelationshipArchitect();
|
|
29
|
+
this.consolidator = new KnowledgeConsolidator();
|
|
25
30
|
}
|
|
26
31
|
/**
|
|
27
32
|
* Add or update a knowledge item with robust merging and transaction support.
|
|
28
33
|
*/
|
|
29
34
|
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.
|
|
35
|
+
// Production Hardening: 32-bit Rolling Bloom Filter
|
|
33
36
|
const factHash = this.bloomHash(`${entity}:${fact}`);
|
|
34
37
|
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
38
|
const quickMatch = await this.db
|
|
38
39
|
.selectFrom(this.knowledgeTable)
|
|
39
40
|
.select(['id', 'status'])
|
|
40
41
|
.where('entity', '=', entity)
|
|
41
42
|
.where('fact', '=', fact)
|
|
42
43
|
.executeTakeFirst();
|
|
43
|
-
if (quickMatch &&
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
if (quickMatch && quickMatch.status === 'verified' && source !== 'user') {
|
|
45
|
+
const results = await this.getKnowledgeByEntity(entity);
|
|
46
|
+
const match = results.find(k => k.fact === fact);
|
|
47
|
+
if (match)
|
|
48
|
+
return match;
|
|
47
49
|
}
|
|
48
50
|
}
|
|
49
51
|
this.bloomFilter.add(factHash);
|
|
50
|
-
if (this.bloomFilter.size >
|
|
51
|
-
this.bloomFilter.clear();
|
|
52
|
+
if (this.bloomFilter.size > 2000)
|
|
53
|
+
this.bloomFilter.clear();
|
|
52
54
|
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
|
|
55
|
+
// 1. Check for Exact Match & Merge
|
|
56
|
+
let result = await this.distiller.distillExact(trx, this.knowledgeTable, entity, fact, confidence, sourceSessionId, tags, metadata, source);
|
|
57
|
+
if (result)
|
|
58
|
+
return this.parseKnowledge(result);
|
|
59
|
+
// 2. Conflict Detection (Contradiction Challenge)
|
|
105
60
|
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();
|
|
61
|
+
// 3. Create New Item
|
|
62
|
+
const created = await this.distiller.createInitial(trx, this.knowledgeTable, entity, fact, confidence, sourceSessionId, tags, metadata, source);
|
|
128
63
|
const parsed = this.parseKnowledge(created);
|
|
129
|
-
// Semantic Auto-Linking
|
|
130
|
-
await this.
|
|
64
|
+
// 4. Semantic Auto-Linking
|
|
65
|
+
await this.architect.autoLink(parsed, trx, this.knowledgeTable, this.linksTable);
|
|
131
66
|
return parsed;
|
|
132
67
|
});
|
|
133
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Manually challenge an existing fact with competing information.
|
|
71
|
+
*/
|
|
72
|
+
async challengeKnowledge(entity, fact, confidence, trxOrDb = this.db) {
|
|
73
|
+
await this.challenger.challenge(trxOrDb, this.knowledgeTable, entity, fact, confidence, (i) => this.parseKnowledge(i));
|
|
74
|
+
}
|
|
134
75
|
/**
|
|
135
76
|
* Verify and reinforce existing knowledge.
|
|
136
|
-
* Increases confidence if the fact matches.
|
|
137
77
|
*/
|
|
138
78
|
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);
|
|
79
|
+
const updated = await this.distiller.verify(this.db, this.knowledgeTable, id, reinforcement);
|
|
80
|
+
return updated ? this.parseKnowledge(updated) : null;
|
|
172
81
|
}
|
|
173
82
|
/**
|
|
174
83
|
* Search knowledge by entity with optional tag filtering.
|
|
175
|
-
* Records a "hit" for each retrieved item to track utility.
|
|
176
84
|
*/
|
|
177
85
|
async getKnowledgeByEntity(entity, filterTags) {
|
|
178
|
-
|
|
86
|
+
const items = await this.db
|
|
179
87
|
.selectFrom(this.knowledgeTable)
|
|
180
88
|
.selectAll()
|
|
181
89
|
.where('entity', '=', entity)
|
|
182
|
-
.orderBy('confidence', 'desc')
|
|
183
|
-
|
|
90
|
+
.orderBy('confidence', 'desc')
|
|
91
|
+
.execute();
|
|
184
92
|
const parsed = items.map((i) => this.parseKnowledge(i));
|
|
185
93
|
let filtered = parsed;
|
|
186
94
|
if (filterTags && filterTags.length > 0) {
|
|
187
95
|
filtered = parsed.filter((item) => item.tags?.some((tag) => filterTags.includes(tag)));
|
|
188
96
|
}
|
|
189
|
-
// Record hits asynchronously
|
|
97
|
+
// Record hits asynchronously to hotspot optimization layer
|
|
190
98
|
for (const item of filtered) {
|
|
191
|
-
this.recordHit(item.id).catch((
|
|
99
|
+
this.recordHit(item.id).catch(() => { });
|
|
192
100
|
}
|
|
193
101
|
return filtered;
|
|
194
102
|
}
|
|
195
103
|
/**
|
|
196
|
-
* Record a retrieval hit for a knowledge item.
|
|
104
|
+
* Record a retrieval hit for a knowledge item and emit hotspot telemetry.
|
|
197
105
|
*/
|
|
198
106
|
async recordHit(id) {
|
|
199
107
|
await this.db.transaction().execute(async (trx) => {
|
|
200
|
-
const
|
|
108
|
+
const query = trx
|
|
201
109
|
.selectFrom(this.knowledgeTable)
|
|
202
|
-
.
|
|
203
|
-
.where('id', '=', id)
|
|
204
|
-
|
|
110
|
+
.select(['id', 'entity', 'metadata'])
|
|
111
|
+
.where('id', '=', id);
|
|
112
|
+
const existing = await withLock(query, trx)
|
|
205
113
|
.executeTakeFirst();
|
|
206
114
|
if (!existing)
|
|
207
115
|
return;
|
|
@@ -218,7 +126,7 @@ export class KnowledgeDistiller {
|
|
|
218
126
|
})
|
|
219
127
|
.where('id', '=', id)
|
|
220
128
|
.execute();
|
|
221
|
-
// Production Hardening:
|
|
129
|
+
// Production Hardening: Real-time hotspots in metrics layer
|
|
222
130
|
await trx
|
|
223
131
|
.insertInto(this.config.metricsTable || 'agent_metrics')
|
|
224
132
|
.values({
|
|
@@ -229,156 +137,11 @@ export class KnowledgeDistiller {
|
|
|
229
137
|
.execute();
|
|
230
138
|
});
|
|
231
139
|
}
|
|
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
140
|
/**
|
|
348
141
|
* Link two knowledge items together with a relationship.
|
|
349
|
-
* Transaction-aware version.
|
|
350
142
|
*/
|
|
351
143
|
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
|
-
}
|
|
144
|
+
return this.architect.link(sourceId, targetId, relationship, metadata, trxOrDb, this.linksTable);
|
|
382
145
|
}
|
|
383
146
|
/**
|
|
384
147
|
* Get the "Knowledge Graph" for a specific entity (1-hop neighborhood).
|
|
@@ -414,77 +177,34 @@ export class KnowledgeDistiller {
|
|
|
414
177
|
.deleteFrom(this.knowledgeTable)
|
|
415
178
|
.where('confidence', '<', threshold)
|
|
416
179
|
.executeTakeFirst();
|
|
417
|
-
return Number(result.numDeletedRows);
|
|
180
|
+
return Number(result.numDeletedRows || 0);
|
|
418
181
|
}
|
|
419
182
|
/**
|
|
420
183
|
* Consolidate knowledge by merging similar facts for the same entity.
|
|
421
|
-
* Production Hardening: Iterative bucketed approach to avoid recursive stack issues.
|
|
422
184
|
*/
|
|
423
185
|
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;
|
|
186
|
+
return this.consolidator.consolidate(this.db, this.knowledgeTable, (e) => this.getKnowledgeByEntity(e));
|
|
455
187
|
}
|
|
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
|
-
});
|
|
188
|
+
/**
|
|
189
|
+
* Calculate the "Fitness" of a knowledge item.
|
|
190
|
+
* Score = (Confidence * 0.4) + (SignalToNoise * 0.4) + (SourceMultiplier * 0.2)
|
|
191
|
+
*/
|
|
192
|
+
calculateFitness(item) {
|
|
193
|
+
const metadata = item.metadata || {};
|
|
194
|
+
const hitCount = metadata.hit_count || 0;
|
|
195
|
+
const createdAt = new Date(item.createdAt).getTime();
|
|
196
|
+
const ageInDays = Math.max(1, (Date.now() - createdAt) / (1000 * 60 * 60 * 24));
|
|
197
|
+
// Signal-to-Noise: Hits per day
|
|
198
|
+
const stn = Math.min(1.0, hitCount / ageInDays);
|
|
199
|
+
// Source Multiplier: User verified facts get 1.0, Assistant-generated 0.7
|
|
200
|
+
const sourceMult = metadata.source === 'user' ? 1.0 : 0.7;
|
|
201
|
+
return item.confidence * 0.4 + stn * 0.4 + sourceMult * 0.2;
|
|
482
202
|
}
|
|
483
203
|
bloomHash(str) {
|
|
484
204
|
let hash = 0;
|
|
485
205
|
for (let i = 0; i < str.length; i++) {
|
|
486
206
|
hash = (hash << 5) - hash + str.charCodeAt(i);
|
|
487
|
-
hash |= 0;
|
|
207
|
+
hash |= 0;
|
|
488
208
|
}
|
|
489
209
|
return hash;
|
|
490
210
|
}
|
|
@@ -497,9 +217,7 @@ export class KnowledgeDistiller {
|
|
|
497
217
|
status: item.status || 'proposed',
|
|
498
218
|
sourceSessionId: item.source_session_id,
|
|
499
219
|
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 || {},
|
|
220
|
+
metadata: typeof item.metadata === 'string' ? JSON.parse(item.metadata) : item.metadata || {},
|
|
503
221
|
createdAt: new Date(item.created_at),
|
|
504
222
|
updatedAt: new Date(item.updated_at),
|
|
505
223
|
};
|
|
@@ -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
|
*/
|