forked from personamanagmentlayer/pcl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweight-adapter.ts
More file actions
223 lines (191 loc) · 5.9 KB
/
Copy pathweight-adapter.ts
File metadata and controls
223 lines (191 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* Dynamic weight adaptation for team members
* Part of Q2 2025 Adaptive Intelligence - Phase 3
*/
import { OutcomeTracker } from './outcome-tracker.js';
import type {
AdaptiveWeightConfig,
MemberPerformance,
WeightAdjustmentEvent,
} from './types.js';
/**
* Adapts team member weights based on performance
*/
export class WeightAdapter {
private config: AdaptiveWeightConfig;
private outcomeTracker: OutcomeTracker;
private adjustmentCount: Map<string, number>;
private adjustmentHistory: WeightAdjustmentEvent[];
constructor(config: AdaptiveWeightConfig, tracker: OutcomeTracker) {
this.config = config;
this.outcomeTracker = tracker;
this.adjustmentCount = new Map();
this.adjustmentHistory = [];
}
/**
* Check if weights should be adjusted
*/
shouldAdjust(teamId: string): boolean {
if (!this.config.enabled) return false;
const count = this.adjustmentCount.get(teamId) || 0;
return count >= this.config.adaptationInterval;
}
/**
* Adjust weights based on performance
*/
adjustWeights(
teamId: string,
currentWeights: Map<string, number>
): Map<string, number> {
const newWeights = new Map(currentWeights);
// Get performance for all members
const performances = this.outcomeTracker.getAllMemberPerformances(teamId);
// Compute target weights
for (const [personaId, currentWeight] of currentWeights) {
const performance = performances.get(personaId);
if (!performance || performance.totalResponses === 0) {
// No performance data, keep current weight
continue;
}
const targetWeight = this.computeTargetWeight(performance);
// Gradual adjustment using learning rate
const adjustment =
(targetWeight - currentWeight) * this.config.learningRate;
const newWeight = this.clamp(
currentWeight + adjustment,
this.config.minWeight,
this.config.maxWeight
);
newWeights.set(personaId, newWeight);
}
// Normalize weights to sum to member count
const sum = Array.from(newWeights.values()).reduce((a, b) => a + b, 0);
const memberCount = newWeights.size;
const scale = memberCount / sum;
const normalizedWeights = new Map<string, number>();
for (const [personaId, weight] of newWeights) {
normalizedWeights.set(personaId, weight * scale);
}
// Record adjustment event
this.recordAdjustment(teamId, currentWeights, normalizedWeights);
// Reset adjustment counter
this.adjustmentCount.set(teamId, 0);
return normalizedWeights;
}
/**
* Record a merge for tracking
*/
recordMerge(teamId: string): void {
const count = this.adjustmentCount.get(teamId) || 0;
this.adjustmentCount.set(teamId, count + 1);
}
/**
* Get adjustment history for a team
*/
getAdjustmentHistory(
teamId: string,
limit: number = 10
): WeightAdjustmentEvent[] {
return this.adjustmentHistory
.filter((event) => event.teamId === teamId)
.slice(-limit);
}
/**
* Compute target weight based on performance signals
*/
private computeTargetWeight(performance: MemberPerformance): number {
const { signals } = this.config;
// Weighted combination of performance signals
const score =
performance.avgConfidence * signals.confidence +
performance.selectionRate * signals.selection +
performance.avgQuality * signals.quality;
// Normalize to [0, 1]
const totalSignalWeight =
signals.confidence + signals.selection + signals.quality;
const normalizedScore = score / totalSignalWeight;
// Convert to weight range [minWeight, maxWeight]
const weightRange = this.config.maxWeight - this.config.minWeight;
const targetWeight = this.config.minWeight + normalizedScore * weightRange;
return this.clamp(
targetWeight,
this.config.minWeight,
this.config.maxWeight
);
}
/**
* Clamp value to range
*/
private clamp(value: number, min: number, max: number): number {
return Math.max(min, Math.min(max, value));
}
/**
* Record weight adjustment event
*/
private recordAdjustment(
teamId: string,
oldWeights: Map<string, number>,
newWeights: Map<string, number>
): void {
// Compute differences
const changes: string[] = [];
for (const [personaId, newWeight] of newWeights) {
const oldWeight = oldWeights.get(personaId) || 1.0;
const diff = newWeight - oldWeight;
if (Math.abs(diff) > 0.01) {
changes.push(
`${personaId}: ${oldWeight.toFixed(2)} → ${newWeight.toFixed(2)}`
);
}
}
const event: WeightAdjustmentEvent = {
teamId,
timestamp: Date.now(),
oldWeights: new Map(oldWeights),
newWeights: new Map(newWeights),
reason:
changes.length > 0 ? changes.join(', ') : 'No significant changes',
};
this.adjustmentHistory.push(event);
// Keep only recent history
if (this.adjustmentHistory.length > 100) {
this.adjustmentHistory = this.adjustmentHistory.slice(-100);
}
}
/**
* Clear adjustment tracking
*/
clear(teamId?: string): void {
if (teamId) {
this.adjustmentCount.delete(teamId);
this.adjustmentHistory = this.adjustmentHistory.filter(
(event) => event.teamId !== teamId
);
} else {
this.adjustmentCount.clear();
this.adjustmentHistory = [];
}
}
/**
* Export adapter state
*/
export(): {
adjustmentCount: Map<string, number>;
adjustmentHistory: WeightAdjustmentEvent[];
} {
return {
adjustmentCount: new Map(this.adjustmentCount),
adjustmentHistory: [...this.adjustmentHistory],
};
}
/**
* Import adapter state
*/
import(data: {
adjustmentCount: Map<string, number>;
adjustmentHistory: WeightAdjustmentEvent[];
}): void {
this.adjustmentCount = new Map(data.adjustmentCount);
this.adjustmentHistory = [...data.adjustmentHistory];
}
}