mark-improving-agent 2.2.8 → 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.8
1
+ 2.2.9
@@ -3,3 +3,4 @@ export * from './agentic-loop.js';
3
3
  export * from './multi-agent-system.js';
4
4
  export { createMCPProtocol } from './mcp-protocol.js';
5
5
  export { createPeerReviewSystem } from './peer-review.js';
6
+ export { createTrustScorer, TrustScorer } from './trust-scorer.js';
@@ -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.8';
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.8",
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",