mark-improving-agent 2.2.7 → 2.2.9
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/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.2.
|
|
1
|
+
2.2.9
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cognitive Budget Manager
|
|
3
|
+
*
|
|
4
|
+
* Intelligently manages token usage and context window to prevent overflow.
|
|
5
|
+
* Tracks token consumption, predicts when context will exceed limits,
|
|
6
|
+
* and proactively compresses or archives low-priority content.
|
|
7
|
+
*
|
|
8
|
+
* @module cognition/cognitive-budget
|
|
9
|
+
* @fileoverview Cognitive budget management for context window optimization
|
|
10
|
+
*/
|
|
11
|
+
import { atomicWriteJSON, readJSON } from '../../storage/archive.js';
|
|
12
|
+
import { createLogger } from '../../utils/logger.js';
|
|
13
|
+
const logger = createLogger('[CognitiveBudget]');
|
|
14
|
+
const DEFAULT_CONFIG = {
|
|
15
|
+
maxTokens: 100000,
|
|
16
|
+
warningThreshold: 0.7,
|
|
17
|
+
criticalThreshold: 0.9,
|
|
18
|
+
autoCompress: true,
|
|
19
|
+
compressionRatio: 0.5,
|
|
20
|
+
};
|
|
21
|
+
const STATE_FILE = 'cognitive-budget.json';
|
|
22
|
+
/**
|
|
23
|
+
* Estimate tokens from text (rough approximation)
|
|
24
|
+
* 1 token ≈ 4 characters for English, 2 for Chinese
|
|
25
|
+
*/
|
|
26
|
+
function estimateTokens(text) {
|
|
27
|
+
if (!text)
|
|
28
|
+
return 0;
|
|
29
|
+
let count = 0;
|
|
30
|
+
for (const char of text) {
|
|
31
|
+
if (char.charCodeAt(0) > 127) {
|
|
32
|
+
count += 2; // Chinese/Unicode
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
count += 1;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return Math.ceil(count / 4);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Categorize content by priority
|
|
42
|
+
*/
|
|
43
|
+
function getContentPriority(category) {
|
|
44
|
+
const priorityMap = {
|
|
45
|
+
'system': 1.0, // Highest - system prompts
|
|
46
|
+
'identity': 0.95, // Identity core
|
|
47
|
+
'memory': 0.8, // Long-term memory
|
|
48
|
+
'goals': 0.75, // Active goals
|
|
49
|
+
'context': 0.6, // Recent context
|
|
50
|
+
'reflection': 0.5, // Reflection data
|
|
51
|
+
'history': 0.3, // Chat history
|
|
52
|
+
'archive': 0.1, // Archived content - lowest
|
|
53
|
+
};
|
|
54
|
+
return priorityMap[category] ?? 0.5;
|
|
55
|
+
}
|
|
56
|
+
export function createCognitiveBudgetManager(dataDir, config = {}) {
|
|
57
|
+
const cfg = { ...DEFAULT_CONFIG, ...config };
|
|
58
|
+
const usage = new Map();
|
|
59
|
+
const alerts = [];
|
|
60
|
+
let totalTokens = 0;
|
|
61
|
+
let sessionStart = Date.now();
|
|
62
|
+
const statePath = `${dataDir}/cognition/${STATE_FILE}`;
|
|
63
|
+
function track(category, tokens) {
|
|
64
|
+
if (tokens <= 0)
|
|
65
|
+
return;
|
|
66
|
+
const current = usage.get(category) || 0;
|
|
67
|
+
usage.set(category, current + tokens);
|
|
68
|
+
totalTokens += tokens;
|
|
69
|
+
logger.debug(`Tracked ${tokens} tokens for '${category}'`, {
|
|
70
|
+
categoryTotal: usage.get(category),
|
|
71
|
+
overallTotal: totalTokens,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
function getUsage() {
|
|
75
|
+
const byCategory = {};
|
|
76
|
+
let sum = 0;
|
|
77
|
+
for (const [cat, tokens] of usage) {
|
|
78
|
+
byCategory[cat] = tokens;
|
|
79
|
+
sum += tokens;
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
current: sum,
|
|
83
|
+
max: cfg.maxTokens,
|
|
84
|
+
ratio: sum / cfg.maxTokens,
|
|
85
|
+
byCategory,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function getLoadLevel() {
|
|
89
|
+
const ratio = getUsage().ratio;
|
|
90
|
+
if (ratio >= cfg.criticalThreshold)
|
|
91
|
+
return 'critical';
|
|
92
|
+
if (ratio >= cfg.warningThreshold)
|
|
93
|
+
return 'warning';
|
|
94
|
+
if (ratio >= 0.5)
|
|
95
|
+
return 'moderate';
|
|
96
|
+
return 'healthy';
|
|
97
|
+
}
|
|
98
|
+
function checkThresholds() {
|
|
99
|
+
const level = getLoadLevel();
|
|
100
|
+
if (level === 'healthy')
|
|
101
|
+
return null;
|
|
102
|
+
const usage = getUsage();
|
|
103
|
+
const recommendations = [];
|
|
104
|
+
switch (level) {
|
|
105
|
+
case 'moderate':
|
|
106
|
+
recommendations.push('Consider summarizing old context soon');
|
|
107
|
+
break;
|
|
108
|
+
case 'warning':
|
|
109
|
+
recommendations.push('Archive low-priority content');
|
|
110
|
+
recommendations.push('Enable compression if not already');
|
|
111
|
+
break;
|
|
112
|
+
case 'critical':
|
|
113
|
+
recommendations.push('Immediately archive or compress content');
|
|
114
|
+
recommendations.push('Consider fragmenting long context');
|
|
115
|
+
recommendations.push('Prune oldest history entries');
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
const messages = {
|
|
119
|
+
healthy: 'Context budget healthy',
|
|
120
|
+
moderate: 'Context usage increasing',
|
|
121
|
+
warning: 'Context approaching limit - action recommended',
|
|
122
|
+
critical: 'Context critical - immediate action required',
|
|
123
|
+
};
|
|
124
|
+
return {
|
|
125
|
+
level,
|
|
126
|
+
message: messages[level],
|
|
127
|
+
timestamp: Date.now(),
|
|
128
|
+
recommendations,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function getCompressionRecommendations(limit = 5) {
|
|
132
|
+
const recommendations = [];
|
|
133
|
+
const usage = getUsage();
|
|
134
|
+
if (usage.current < cfg.maxTokens * 0.5) {
|
|
135
|
+
return recommendations;
|
|
136
|
+
}
|
|
137
|
+
// Analyze each category for compression potential
|
|
138
|
+
for (const [category, tokens] of Object.entries(usage.byCategory)) {
|
|
139
|
+
const priority = getContentPriority(category);
|
|
140
|
+
const capacity = cfg.maxTokens * (1 - priority);
|
|
141
|
+
if (tokens > capacity * 0.3) {
|
|
142
|
+
const excess = tokens - capacity * 0.3;
|
|
143
|
+
const priorityScore = 1 - priority;
|
|
144
|
+
recommendations.push({
|
|
145
|
+
priority: priorityScore,
|
|
146
|
+
reason: `Category '${category}' has ${tokens} tokens at priority ${priority.toFixed(2)}`,
|
|
147
|
+
estimatedSavings: Math.floor(excess * 0.5),
|
|
148
|
+
target: category,
|
|
149
|
+
strategy: priority < 0.3 ? 'prune' : priority < 0.6 ? 'archive' : 'summarize',
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// Sort by priority (highest first)
|
|
154
|
+
recommendations.sort((a, b) => b.priority - a.priority);
|
|
155
|
+
return recommendations.slice(0, limit);
|
|
156
|
+
}
|
|
157
|
+
async function compress(recommendation) {
|
|
158
|
+
logger.info(`Compressing category: ${recommendation.target}`, recommendation);
|
|
159
|
+
const current = usage.get(recommendation.target) || 0;
|
|
160
|
+
const savings = Math.floor(current * (1 - cfg.compressionRatio));
|
|
161
|
+
// Apply compression
|
|
162
|
+
usage.set(recommendation.target, current - savings);
|
|
163
|
+
totalTokens -= savings;
|
|
164
|
+
// Record alert
|
|
165
|
+
alerts.push({
|
|
166
|
+
level: getLoadLevel(),
|
|
167
|
+
message: `Compressed ${recommendation.target}: freed ~${savings} tokens`,
|
|
168
|
+
timestamp: Date.now(),
|
|
169
|
+
recommendations: [recommendation.strategy],
|
|
170
|
+
});
|
|
171
|
+
// Keep only last 50 alerts
|
|
172
|
+
if (alerts.length > 50) {
|
|
173
|
+
alerts.shift();
|
|
174
|
+
}
|
|
175
|
+
await persist();
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
function reset() {
|
|
179
|
+
usage.clear();
|
|
180
|
+
totalTokens = 0;
|
|
181
|
+
sessionStart = Date.now();
|
|
182
|
+
alerts.length = 0;
|
|
183
|
+
}
|
|
184
|
+
async function persist() {
|
|
185
|
+
const state = {
|
|
186
|
+
usage: Object.fromEntries(usage),
|
|
187
|
+
totalTokens,
|
|
188
|
+
sessionStart,
|
|
189
|
+
config: cfg,
|
|
190
|
+
alerts: alerts.slice(-20),
|
|
191
|
+
};
|
|
192
|
+
await atomicWriteJSON(statePath, state);
|
|
193
|
+
}
|
|
194
|
+
async function boot() {
|
|
195
|
+
const loaded = await readJSON(statePath, {});
|
|
196
|
+
if (loaded) {
|
|
197
|
+
if (loaded.usage) {
|
|
198
|
+
for (const [k, v] of Object.entries(loaded.usage)) {
|
|
199
|
+
usage.set(k, v);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (typeof loaded.totalTokens === 'number') {
|
|
203
|
+
totalTokens = loaded.totalTokens;
|
|
204
|
+
}
|
|
205
|
+
if (loaded.alerts) {
|
|
206
|
+
alerts.push(...loaded.alerts.slice(-20));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
logger.info('CognitiveBudgetManager initialized', {
|
|
210
|
+
maxTokens: cfg.maxTokens,
|
|
211
|
+
currentTokens: getUsage().current,
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
track,
|
|
216
|
+
getUsage,
|
|
217
|
+
getLoadLevel,
|
|
218
|
+
checkThresholds,
|
|
219
|
+
getCompressionRecommendations,
|
|
220
|
+
compress,
|
|
221
|
+
reset,
|
|
222
|
+
persist,
|
|
223
|
+
boot,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multi-Agent Trust Scoring System
|
|
3
|
+
*
|
|
4
|
+
* Provides trust and quality scoring for AI agents based on their
|
|
5
|
+
* task performance, peer reviews, and capability assessments.
|
|
6
|
+
* Includes EU AI Act compliance metrics.
|
|
7
|
+
*
|
|
8
|
+
* @module collaboration/trust-scorer
|
|
9
|
+
* @fileoverview Trust scoring system for multi-agent collaboration
|
|
10
|
+
*/
|
|
11
|
+
import { createLogger } from '../../utils/logger.js';
|
|
12
|
+
import { createPeerReviewSystem } from './peer-review.js';
|
|
13
|
+
const logger = createLogger('[TrustScorer]');
|
|
14
|
+
/**
|
|
15
|
+
* Trust score level based on numeric score
|
|
16
|
+
*/
|
|
17
|
+
export var TrustLevel;
|
|
18
|
+
(function (TrustLevel) {
|
|
19
|
+
TrustLevel["UNTRUSTED"] = "untrusted";
|
|
20
|
+
TrustLevel["LOW"] = "low";
|
|
21
|
+
TrustLevel["MEDIUM"] = "medium";
|
|
22
|
+
TrustLevel["HIGH"] = "high";
|
|
23
|
+
TrustLevel["TRUSTED"] = "trusted";
|
|
24
|
+
})(TrustLevel || (TrustLevel = {}));
|
|
25
|
+
/**
|
|
26
|
+
* EU AI Act risk categories
|
|
27
|
+
*/
|
|
28
|
+
export var AIActRiskCategory;
|
|
29
|
+
(function (AIActRiskCategory) {
|
|
30
|
+
AIActRiskCategory["UNacceptable_Risk"] = "unacceptable-risk";
|
|
31
|
+
AIActRiskCategory["HIGH_RISK"] = "high-risk";
|
|
32
|
+
AIActRiskCategory["LIMITED_RISK"] = "limited-risk";
|
|
33
|
+
AIActRiskCategory["MINIMAL_RISK"] = "minimal-risk";
|
|
34
|
+
})(AIActRiskCategory || (AIActRiskCategory = {}));
|
|
35
|
+
/**
|
|
36
|
+
* Trust Scorer class
|
|
37
|
+
*/
|
|
38
|
+
export class TrustScorer {
|
|
39
|
+
config;
|
|
40
|
+
agentProfiles = new Map();
|
|
41
|
+
scoringEvents = [];
|
|
42
|
+
peerReviewSystem;
|
|
43
|
+
constructor(config = {}) {
|
|
44
|
+
this.config = {
|
|
45
|
+
minTasksForTrustScore: config.minTasksForTrustScore ?? 5,
|
|
46
|
+
taskSuccessWeight: config.taskSuccessWeight ?? 0.4,
|
|
47
|
+
peerReviewWeight: config.peerReviewWeight ?? 0.35,
|
|
48
|
+
capabilityWeight: config.capabilityWeight ?? 0.25,
|
|
49
|
+
decayRatePerDay: config.decayRatePerDay ?? 0.01,
|
|
50
|
+
trustLevelThresholds: config.trustLevelThresholds ?? {
|
|
51
|
+
untrusted: 0,
|
|
52
|
+
low: 20,
|
|
53
|
+
medium: 40,
|
|
54
|
+
high: 70,
|
|
55
|
+
trusted: 85
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
this.peerReviewSystem = createPeerReviewSystem({
|
|
59
|
+
consensusThreshold: 0.6,
|
|
60
|
+
requiredReviewers: 3,
|
|
61
|
+
enableArbitration: false
|
|
62
|
+
});
|
|
63
|
+
logger.info('TrustScorer initialized', this.config);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Register a new agent in the trust system
|
|
67
|
+
*/
|
|
68
|
+
registerAgent(agentId, agentName) {
|
|
69
|
+
const profile = {
|
|
70
|
+
agentId,
|
|
71
|
+
agentName,
|
|
72
|
+
trustScore: 50, // Start with neutral score
|
|
73
|
+
trustLevel: TrustLevel.MEDIUM,
|
|
74
|
+
successfulTasks: 0,
|
|
75
|
+
failedTasks: 0,
|
|
76
|
+
successRate: 0,
|
|
77
|
+
avgPeerScore: 0.5,
|
|
78
|
+
reviewCount: 0,
|
|
79
|
+
capabilities: new Map(),
|
|
80
|
+
aiActCategory: AIActRiskCategory.MINIMAL_RISK,
|
|
81
|
+
registeredAt: Date.now(),
|
|
82
|
+
lastActivityAt: Date.now(),
|
|
83
|
+
isActive: true
|
|
84
|
+
};
|
|
85
|
+
this.agentProfiles.set(agentId, profile);
|
|
86
|
+
logger.info(`Agent registered: ${agentId} (${agentName})`);
|
|
87
|
+
return profile;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Record a task outcome for an agent
|
|
91
|
+
*/
|
|
92
|
+
recordTaskOutcome(agentId, taskId, success, metadata) {
|
|
93
|
+
const profile = this.getOrCreateProfile(agentId);
|
|
94
|
+
if (success) {
|
|
95
|
+
profile.successfulTasks++;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
profile.failedTasks++;
|
|
99
|
+
}
|
|
100
|
+
profile.lastActivityAt = Date.now();
|
|
101
|
+
profile.successRate = profile.successfulTasks / (profile.successfulTasks + profile.failedTasks);
|
|
102
|
+
const event = {
|
|
103
|
+
eventId: `evt_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
|
104
|
+
agentId,
|
|
105
|
+
eventType: success ? 'task_success' : 'task_failure',
|
|
106
|
+
timestamp: Date.now(),
|
|
107
|
+
referenceId: taskId,
|
|
108
|
+
scoreDelta: success ? 1 : -1,
|
|
109
|
+
metadata
|
|
110
|
+
};
|
|
111
|
+
this.scoringEvents.push(event);
|
|
112
|
+
this.recalculateTrustScore(agentId);
|
|
113
|
+
logger.info(`Task outcome recorded: ${agentId} - ${taskId} - ${success ? 'SUCCESS' : 'FAILURE'}`);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Record a peer review for an agent
|
|
117
|
+
*/
|
|
118
|
+
recordPeerReview(agentId, reviewId, score, // 0-1
|
|
119
|
+
reviewerId) {
|
|
120
|
+
const profile = this.getOrCreateProfile(agentId);
|
|
121
|
+
const totalScore = profile.avgPeerScore * profile.reviewCount + score;
|
|
122
|
+
profile.reviewCount++;
|
|
123
|
+
profile.avgPeerScore = totalScore / profile.reviewCount;
|
|
124
|
+
profile.lastActivityAt = Date.now();
|
|
125
|
+
const event = {
|
|
126
|
+
eventId: `evt_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
|
127
|
+
agentId,
|
|
128
|
+
eventType: 'peer_review',
|
|
129
|
+
timestamp: Date.now(),
|
|
130
|
+
referenceId: reviewId,
|
|
131
|
+
scoreDelta: score - 0.5,
|
|
132
|
+
metadata: { reviewerId }
|
|
133
|
+
};
|
|
134
|
+
this.scoringEvents.push(event);
|
|
135
|
+
this.recalculateTrustScore(agentId);
|
|
136
|
+
logger.info(`Peer review recorded: ${agentId} - Review: ${reviewerId} - Score: ${score.toFixed(2)}`);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Update capability quality score for an agent
|
|
140
|
+
*/
|
|
141
|
+
updateCapabilityScore(agentId, capability, qualityScore, assessmentCount = 1) {
|
|
142
|
+
const profile = this.getOrCreateProfile(agentId);
|
|
143
|
+
const existing = profile.capabilities.get(capability);
|
|
144
|
+
const newScore = existing
|
|
145
|
+
? {
|
|
146
|
+
capability,
|
|
147
|
+
qualityScore: (existing.qualityScore * existing.assessmentCount + qualityScore) / (existing.assessmentCount + assessmentCount),
|
|
148
|
+
assessmentCount: existing.assessmentCount + assessmentCount,
|
|
149
|
+
lastUpdated: Date.now(),
|
|
150
|
+
trend: qualityScore > existing.qualityScore ? 'positive' : qualityScore < existing.qualityScore ? 'negative' : 'stable'
|
|
151
|
+
}
|
|
152
|
+
: {
|
|
153
|
+
capability,
|
|
154
|
+
qualityScore,
|
|
155
|
+
assessmentCount,
|
|
156
|
+
lastUpdated: Date.now(),
|
|
157
|
+
trend: 'stable'
|
|
158
|
+
};
|
|
159
|
+
profile.capabilities.set(capability, newScore);
|
|
160
|
+
profile.lastActivityAt = Date.now();
|
|
161
|
+
const event = {
|
|
162
|
+
eventId: `evt_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
|
163
|
+
agentId,
|
|
164
|
+
eventType: 'capability_assessment',
|
|
165
|
+
timestamp: Date.now(),
|
|
166
|
+
metadata: { capability, qualityScore }
|
|
167
|
+
};
|
|
168
|
+
this.scoringEvents.push(event);
|
|
169
|
+
this.recalculateTrustScore(agentId);
|
|
170
|
+
logger.info(`Capability updated: ${agentId} - ${capability}: ${qualityScore}`);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Set EU AI Act risk category for an agent
|
|
174
|
+
*/
|
|
175
|
+
setAIActCategory(agentId, category) {
|
|
176
|
+
const profile = this.getOrCreateProfile(agentId);
|
|
177
|
+
profile.aiActCategory = category;
|
|
178
|
+
logger.info(`AI Act category set: ${agentId} - ${category}`);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Get trust scoring result for an agent
|
|
182
|
+
*/
|
|
183
|
+
getTrustScore(agentId) {
|
|
184
|
+
const profile = this.agentProfiles.get(agentId);
|
|
185
|
+
if (!profile) {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
const recentEvents = this.scoringEvents
|
|
189
|
+
.filter(e => e.agentId === agentId)
|
|
190
|
+
.slice(-10);
|
|
191
|
+
const breakdown = this.calculateScoreBreakdown(profile);
|
|
192
|
+
return {
|
|
193
|
+
profile: { ...profile, capabilities: new Map(profile.capabilities) },
|
|
194
|
+
recentEvents,
|
|
195
|
+
breakdown
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Get trust level from numeric score
|
|
200
|
+
*/
|
|
201
|
+
getTrustLevel(score) {
|
|
202
|
+
const { trustLevelThresholds } = this.config;
|
|
203
|
+
if (score >= trustLevelThresholds.trusted)
|
|
204
|
+
return TrustLevel.TRUSTED;
|
|
205
|
+
if (score >= trustLevelThresholds.high)
|
|
206
|
+
return TrustLevel.HIGH;
|
|
207
|
+
if (score >= trustLevelThresholds.medium)
|
|
208
|
+
return TrustLevel.MEDIUM;
|
|
209
|
+
if (score >= trustLevelThresholds.low)
|
|
210
|
+
return TrustLevel.LOW;
|
|
211
|
+
return TrustLevel.UNTRUSTED;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Get or create agent profile
|
|
215
|
+
*/
|
|
216
|
+
getOrCreateProfile(agentId) {
|
|
217
|
+
let profile = this.agentProfiles.get(agentId);
|
|
218
|
+
if (!profile) {
|
|
219
|
+
profile = this.registerAgent(agentId, agentId);
|
|
220
|
+
}
|
|
221
|
+
return profile;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Recalculate trust score for an agent
|
|
225
|
+
*/
|
|
226
|
+
recalculateTrustScore(agentId) {
|
|
227
|
+
const profile = this.agentProfiles.get(agentId);
|
|
228
|
+
if (!profile)
|
|
229
|
+
return;
|
|
230
|
+
const breakdown = this.calculateScoreBreakdown(profile);
|
|
231
|
+
profile.trustScore = Math.max(0, Math.min(100, Math.round(breakdown.finalScore)));
|
|
232
|
+
profile.trustLevel = this.getTrustLevel(profile.trustScore);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Calculate score breakdown for an agent
|
|
236
|
+
*/
|
|
237
|
+
calculateScoreBreakdown(profile) {
|
|
238
|
+
const totalTasks = profile.successfulTasks + profile.failedTasks;
|
|
239
|
+
// Task success score (requires minimum tasks)
|
|
240
|
+
let taskScore = 50;
|
|
241
|
+
if (totalTasks >= this.config.minTasksForTrustScore) {
|
|
242
|
+
taskScore = profile.successRate * 100;
|
|
243
|
+
}
|
|
244
|
+
// Peer review score (already 0-1, scale to 0-100)
|
|
245
|
+
const peerReviewScore = profile.avgPeerScore * 100;
|
|
246
|
+
// Capability score
|
|
247
|
+
let capabilityScore = 50;
|
|
248
|
+
if (profile.capabilities.size > 0) {
|
|
249
|
+
const totalCapabilityScore = Array.from(profile.capabilities.values())
|
|
250
|
+
.reduce((sum, cap) => sum + cap.qualityScore, 0);
|
|
251
|
+
capabilityScore = totalCapabilityScore / profile.capabilities.size;
|
|
252
|
+
}
|
|
253
|
+
// Calculate weighted final score
|
|
254
|
+
const weightedScore = (taskScore * this.config.taskSuccessWeight +
|
|
255
|
+
peerReviewScore * this.config.peerReviewWeight +
|
|
256
|
+
capabilityScore * this.config.capabilityWeight);
|
|
257
|
+
// Apply decay for inactivity
|
|
258
|
+
const daysInactive = (Date.now() - profile.lastActivityAt) / (1000 * 60 * 60 * 24);
|
|
259
|
+
const decayPenalty = Math.min(30, daysInactive * this.config.decayRatePerDay * 100);
|
|
260
|
+
const finalScore = Math.max(0, weightedScore - decayPenalty);
|
|
261
|
+
return {
|
|
262
|
+
taskScore: Math.round(taskScore * 100) / 100,
|
|
263
|
+
peerReviewScore: Math.round(peerReviewScore * 100) / 100,
|
|
264
|
+
capabilityScore: Math.round(capabilityScore * 100) / 100,
|
|
265
|
+
decayPenalty: Math.round(decayPenalty * 100) / 100,
|
|
266
|
+
finalScore: Math.round(finalScore * 100) / 100
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Get all registered agents sorted by trust score
|
|
271
|
+
*/
|
|
272
|
+
getAllAgents() {
|
|
273
|
+
return Array.from(this.agentProfiles.values())
|
|
274
|
+
.sort((a, b) => b.trustScore - a.trustScore);
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Get agents by trust level
|
|
278
|
+
*/
|
|
279
|
+
getAgentsByLevel(level) {
|
|
280
|
+
return Array.from(this.agentProfiles.values())
|
|
281
|
+
.filter(p => p.trustLevel === level);
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Get high-risk agents (EU AI Act)
|
|
285
|
+
*/
|
|
286
|
+
getHighRiskAgents() {
|
|
287
|
+
return Array.from(this.agentProfiles.values())
|
|
288
|
+
.filter(p => p.aiActCategory === AIActRiskCategory.HIGH_RISK ||
|
|
289
|
+
p.aiActCategory === AIActRiskCategory.UNacceptable_Risk);
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Check if agent meets trust threshold for a task
|
|
293
|
+
*/
|
|
294
|
+
meetsTrustThreshold(agentId, minTrustLevel) {
|
|
295
|
+
const profile = this.agentProfiles.get(agentId);
|
|
296
|
+
if (!profile)
|
|
297
|
+
return false;
|
|
298
|
+
const levels = [
|
|
299
|
+
TrustLevel.UNTRUSTED,
|
|
300
|
+
TrustLevel.LOW,
|
|
301
|
+
TrustLevel.MEDIUM,
|
|
302
|
+
TrustLevel.HIGH,
|
|
303
|
+
TrustLevel.TRUSTED
|
|
304
|
+
];
|
|
305
|
+
return levels.indexOf(profile.trustLevel) >= levels.indexOf(minTrustLevel);
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Get scoring events for an agent
|
|
309
|
+
*/
|
|
310
|
+
getEvents(agentId, limit = 50) {
|
|
311
|
+
return this.scoringEvents
|
|
312
|
+
.filter(e => e.agentId === agentId)
|
|
313
|
+
.slice(-limit);
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Get system statistics
|
|
317
|
+
*/
|
|
318
|
+
getSystemStats() {
|
|
319
|
+
const profiles = Array.from(this.agentProfiles.values());
|
|
320
|
+
const avgTrustScore = profiles.length > 0
|
|
321
|
+
? profiles.reduce((sum, p) => sum + p.trustScore, 0) / profiles.length
|
|
322
|
+
: 0;
|
|
323
|
+
const agentsByLevel = {
|
|
324
|
+
[TrustLevel.UNTRUSTED]: 0,
|
|
325
|
+
[TrustLevel.LOW]: 0,
|
|
326
|
+
[TrustLevel.MEDIUM]: 0,
|
|
327
|
+
[TrustLevel.HIGH]: 0,
|
|
328
|
+
[TrustLevel.TRUSTED]: 0
|
|
329
|
+
};
|
|
330
|
+
profiles.forEach(p => agentsByLevel[p.trustLevel]++);
|
|
331
|
+
return {
|
|
332
|
+
totalAgents: profiles.length,
|
|
333
|
+
avgTrustScore: Math.round(avgTrustScore * 100) / 100,
|
|
334
|
+
agentsByLevel,
|
|
335
|
+
highRiskCount: this.getHighRiskAgents().length,
|
|
336
|
+
totalEvents: this.scoringEvents.length
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Export profile data for compliance
|
|
341
|
+
*/
|
|
342
|
+
exportForCompliance(agentId) {
|
|
343
|
+
const profile = this.agentProfiles.get(agentId);
|
|
344
|
+
if (!profile)
|
|
345
|
+
return null;
|
|
346
|
+
return {
|
|
347
|
+
agentId: profile.agentId,
|
|
348
|
+
agentName: profile.agentName,
|
|
349
|
+
trustScore: profile.trustScore,
|
|
350
|
+
trustLevel: profile.trustLevel,
|
|
351
|
+
aiActCategory: profile.aiActCategory,
|
|
352
|
+
capabilities: Array.from(profile.capabilities.entries()).map(([cap, score]) => ({
|
|
353
|
+
capability: cap,
|
|
354
|
+
qualityScore: score.qualityScore,
|
|
355
|
+
assessmentCount: score.assessmentCount,
|
|
356
|
+
trend: score.trend
|
|
357
|
+
})),
|
|
358
|
+
registeredAt: new Date(profile.registeredAt).toISOString(),
|
|
359
|
+
lastActivityAt: new Date(profile.lastActivityAt).toISOString(),
|
|
360
|
+
complianceCheck: {
|
|
361
|
+
hasRequiredAssessments: profile.reviewCount >= 3,
|
|
362
|
+
meetsMinimumTrust: profile.trustScore >= 40,
|
|
363
|
+
highRiskClassification: profile.aiActCategory === AIActRiskCategory.HIGH_RISK
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Create a trust scoring system instance
|
|
370
|
+
*/
|
|
371
|
+
export function createTrustScorer(config) {
|
|
372
|
+
logger.info('Creating TrustScorer instance');
|
|
373
|
+
return new TrustScorer(config);
|
|
374
|
+
}
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '2.2.
|
|
1
|
+
export const VERSION = '2.2.9';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mark-improving-agent",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.9",
|
|
4
4
|
"description": "Self-evolving AI agent with permanent memory, identity continuity, and self-evolution — for AI agents that need to remember, learn, and evolve across sessions",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|