@polpo-ai/core 0.1.0
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/LICENSE +21 -0
- package/dist/adapter.d.ts +61 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +9 -0
- package/dist/adapter.js.map +1 -0
- package/dist/agent-manager.d.ts +36 -0
- package/dist/agent-manager.d.ts.map +1 -0
- package/dist/agent-manager.js +176 -0
- package/dist/agent-manager.js.map +1 -0
- package/dist/approval-manager.d.ts +49 -0
- package/dist/approval-manager.d.ts.map +1 -0
- package/dist/approval-manager.js +325 -0
- package/dist/approval-manager.js.map +1 -0
- package/dist/approval-store.d.ts +19 -0
- package/dist/approval-store.d.ts.map +1 -0
- package/dist/approval-store.js +2 -0
- package/dist/approval-store.js.map +1 -0
- package/dist/checkpoint-store.d.ts +20 -0
- package/dist/checkpoint-store.d.ts.map +1 -0
- package/dist/checkpoint-store.js +2 -0
- package/dist/checkpoint-store.js.map +1 -0
- package/dist/config-store.d.ts +13 -0
- package/dist/config-store.d.ts.map +1 -0
- package/dist/config-store.js +2 -0
- package/dist/config-store.js.map +1 -0
- package/dist/cron.d.ts +29 -0
- package/dist/cron.d.ts.map +1 -0
- package/dist/cron.js +105 -0
- package/dist/cron.js.map +1 -0
- package/dist/delay-store.d.ts +21 -0
- package/dist/delay-store.d.ts.map +1 -0
- package/dist/delay-store.js +2 -0
- package/dist/delay-store.js.map +1 -0
- package/dist/escalation-manager.d.ts +31 -0
- package/dist/escalation-manager.d.ts.map +1 -0
- package/dist/escalation-manager.js +281 -0
- package/dist/escalation-manager.js.map +1 -0
- package/dist/event-bus.d.ts +18 -0
- package/dist/event-bus.d.ts.map +1 -0
- package/dist/event-bus.js +2 -0
- package/dist/event-bus.js.map +1 -0
- package/dist/events.d.ts +377 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +9 -0
- package/dist/events.js.map +1 -0
- package/dist/hooks.d.ts +185 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +152 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/log-store.d.ts +31 -0
- package/dist/log-store.d.ts.map +1 -0
- package/dist/log-store.js +6 -0
- package/dist/log-store.js.map +1 -0
- package/dist/memory-store.d.ts +17 -0
- package/dist/memory-store.d.ts.map +1 -0
- package/dist/memory-store.js +2 -0
- package/dist/memory-store.js.map +1 -0
- package/dist/notification-router-port.d.ts +11 -0
- package/dist/notification-router-port.d.ts.map +1 -0
- package/dist/notification-router-port.js +2 -0
- package/dist/notification-router-port.js.map +1 -0
- package/dist/notification-store.d.ts +58 -0
- package/dist/notification-store.d.ts.map +1 -0
- package/dist/notification-store.js +9 -0
- package/dist/notification-store.js.map +1 -0
- package/dist/orchestrator-context.d.ts +87 -0
- package/dist/orchestrator-context.d.ts.map +1 -0
- package/dist/orchestrator-context.js +2 -0
- package/dist/orchestrator-context.js.map +1 -0
- package/dist/peer-store.d.ts +29 -0
- package/dist/peer-store.d.ts.map +1 -0
- package/dist/peer-store.js +6 -0
- package/dist/peer-store.js.map +1 -0
- package/dist/quality-controller.d.ts +46 -0
- package/dist/quality-controller.d.ts.map +1 -0
- package/dist/quality-controller.js +373 -0
- package/dist/quality-controller.js.map +1 -0
- package/dist/run-store.d.ts +31 -0
- package/dist/run-store.d.ts.map +1 -0
- package/dist/run-store.js +2 -0
- package/dist/run-store.js.map +1 -0
- package/dist/scheduler.d.ts +35 -0
- package/dist/scheduler.d.ts.map +1 -0
- package/dist/scheduler.js +195 -0
- package/dist/scheduler.js.map +1 -0
- package/dist/schemas.d.ts +104 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/schemas.js +200 -0
- package/dist/schemas.js.map +1 -0
- package/dist/session-store.d.ts +50 -0
- package/dist/session-store.d.ts.map +1 -0
- package/dist/session-store.js +6 -0
- package/dist/session-store.js.map +1 -0
- package/dist/sla-monitor.d.ts +30 -0
- package/dist/sla-monitor.d.ts.map +1 -0
- package/dist/sla-monitor.js +156 -0
- package/dist/sla-monitor.js.map +1 -0
- package/dist/state-machine.d.ts +8 -0
- package/dist/state-machine.d.ts.map +1 -0
- package/dist/state-machine.js +23 -0
- package/dist/state-machine.js.map +1 -0
- package/dist/task-manager.d.ts +38 -0
- package/dist/task-manager.d.ts.map +1 -0
- package/dist/task-manager.js +308 -0
- package/dist/task-manager.js.map +1 -0
- package/dist/task-store.d.ts +33 -0
- package/dist/task-store.d.ts.map +1 -0
- package/dist/task-store.js +2 -0
- package/dist/task-store.js.map +1 -0
- package/dist/task-watcher.d.ts +38 -0
- package/dist/task-watcher.d.ts.map +1 -0
- package/dist/task-watcher.js +103 -0
- package/dist/task-watcher.js.map +1 -0
- package/dist/types.d.ts +1073 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +35 -0
- package/dist/types.js.map +1 -0
- package/package.json +170 -0
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QualityController — manages quality gates within missions and aggregates
|
|
3
|
+
* quality metrics across tasks, agents, and missions.
|
|
4
|
+
*/
|
|
5
|
+
export class QualityController {
|
|
6
|
+
ctx;
|
|
7
|
+
metrics = new Map();
|
|
8
|
+
evaluatedGates = new Set();
|
|
9
|
+
registeredGateRules = new Set();
|
|
10
|
+
notificationRouter;
|
|
11
|
+
constructor(ctx) {
|
|
12
|
+
this.ctx = ctx;
|
|
13
|
+
}
|
|
14
|
+
setNotificationRouter(router) {
|
|
15
|
+
this.notificationRouter = router;
|
|
16
|
+
}
|
|
17
|
+
init() {
|
|
18
|
+
this.ctx.hooks.register({
|
|
19
|
+
hook: "assessment:complete",
|
|
20
|
+
phase: "after",
|
|
21
|
+
priority: 200,
|
|
22
|
+
name: "quality-controller:collect-metrics",
|
|
23
|
+
handler: (hookCtx) => {
|
|
24
|
+
const { taskId, task, assessment, passed } = hookCtx.data;
|
|
25
|
+
this.recordAssessment(taskId, "task", assessment, passed);
|
|
26
|
+
if (task.assignTo) {
|
|
27
|
+
this.recordAssessment(task.assignTo, "agent", assessment, passed);
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
this.ctx.hooks.register({
|
|
32
|
+
hook: "task:complete",
|
|
33
|
+
phase: "after",
|
|
34
|
+
priority: 210,
|
|
35
|
+
name: "quality-controller:sla-outcome",
|
|
36
|
+
handler: (hookCtx) => {
|
|
37
|
+
const { taskId, task } = hookCtx.data;
|
|
38
|
+
if (task?.deadline) {
|
|
39
|
+
const deadline = new Date(task.deadline).getTime();
|
|
40
|
+
const now = Date.now();
|
|
41
|
+
const key = this.metricsKey("task", taskId);
|
|
42
|
+
const m = this.getOrCreate(key, taskId, "task");
|
|
43
|
+
if (now <= deadline) {
|
|
44
|
+
m.deadlinesMet++;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
m.deadlinesMissed++;
|
|
48
|
+
}
|
|
49
|
+
m.updatedAt = new Date().toISOString();
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
this.ctx.hooks.register({
|
|
54
|
+
hook: "task:retry",
|
|
55
|
+
phase: "after",
|
|
56
|
+
priority: 200,
|
|
57
|
+
name: "quality-controller:record-retry",
|
|
58
|
+
handler: (hookCtx) => {
|
|
59
|
+
const { taskId, task } = hookCtx.data;
|
|
60
|
+
const key = this.metricsKey("task", taskId);
|
|
61
|
+
const m = this.getOrCreate(key, taskId, "task");
|
|
62
|
+
m.totalRetries++;
|
|
63
|
+
m.updatedAt = new Date().toISOString();
|
|
64
|
+
if (task.assignTo) {
|
|
65
|
+
const agentKey = this.metricsKey("agent", task.assignTo);
|
|
66
|
+
const am = this.getOrCreate(agentKey, task.assignTo, "agent");
|
|
67
|
+
am.totalRetries++;
|
|
68
|
+
am.updatedAt = new Date().toISOString();
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
evaluateGate(missionId, gate, tasks) {
|
|
74
|
+
const gateKey = `${missionId}:${gate.name}`;
|
|
75
|
+
this.ensureGateNotificationRules(gateKey, gate);
|
|
76
|
+
if (this.evaluatedGates.has(gateKey)) {
|
|
77
|
+
return { passed: true };
|
|
78
|
+
}
|
|
79
|
+
const afterTasks = tasks.filter(t => gate.afterTasks.includes(t.title) || gate.afterTasks.includes(t.id));
|
|
80
|
+
if (afterTasks.length < gate.afterTasks.length) {
|
|
81
|
+
const foundIds = new Set([...afterTasks.map(t => t.title), ...afterTasks.map(t => t.id)]);
|
|
82
|
+
const missing = gate.afterTasks.filter(ref => !foundIds.has(ref));
|
|
83
|
+
return {
|
|
84
|
+
passed: false,
|
|
85
|
+
reason: `Waiting for tasks to complete: ${missing.join(", ")}`,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
const nonTerminal = afterTasks.filter(t => t.status !== "done" && t.status !== "failed");
|
|
89
|
+
if (nonTerminal.length > 0) {
|
|
90
|
+
return {
|
|
91
|
+
passed: false,
|
|
92
|
+
reason: `Waiting for tasks to complete: ${nonTerminal.map(t => t.title).join(", ")}`,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
if (gate.requireAllPassed) {
|
|
96
|
+
const failedTasks = afterTasks.filter(t => t.status === "failed");
|
|
97
|
+
if (failedTasks.length > 0) {
|
|
98
|
+
const reason = `Required tasks failed: ${failedTasks.map(t => t.title).join(", ")}`;
|
|
99
|
+
this.ctx.emitter.emit("quality:gate:failed", {
|
|
100
|
+
missionId,
|
|
101
|
+
gateName: gate.name,
|
|
102
|
+
reason,
|
|
103
|
+
});
|
|
104
|
+
this.ctx.hooks.runAfter("quality:gate", {
|
|
105
|
+
missionId,
|
|
106
|
+
gateName: gate.name,
|
|
107
|
+
allPassed: false,
|
|
108
|
+
tasks: afterTasks.map(t => ({
|
|
109
|
+
taskId: t.id,
|
|
110
|
+
title: t.title,
|
|
111
|
+
status: t.status,
|
|
112
|
+
score: t.result?.assessment?.globalScore,
|
|
113
|
+
})),
|
|
114
|
+
}).catch(() => { });
|
|
115
|
+
return { passed: false, reason };
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (gate.minScore !== undefined) {
|
|
119
|
+
const scores = afterTasks
|
|
120
|
+
.map(t => t.result?.assessment?.globalScore)
|
|
121
|
+
.filter((s) => s !== undefined);
|
|
122
|
+
const avgScore = scores.length > 0
|
|
123
|
+
? scores.reduce((a, b) => a + b, 0) / scores.length
|
|
124
|
+
: undefined;
|
|
125
|
+
if (avgScore === undefined || avgScore < gate.minScore) {
|
|
126
|
+
const reason = `Average score ${avgScore?.toFixed(2) ?? "N/A"} below threshold ${gate.minScore}`;
|
|
127
|
+
this.ctx.emitter.emit("quality:gate:failed", {
|
|
128
|
+
missionId,
|
|
129
|
+
gateName: gate.name,
|
|
130
|
+
avgScore,
|
|
131
|
+
reason,
|
|
132
|
+
});
|
|
133
|
+
this.ctx.hooks.runAfter("quality:gate", {
|
|
134
|
+
missionId,
|
|
135
|
+
gateName: gate.name,
|
|
136
|
+
avgScore,
|
|
137
|
+
allPassed: false,
|
|
138
|
+
tasks: afterTasks.map(t => ({
|
|
139
|
+
taskId: t.id,
|
|
140
|
+
title: t.title,
|
|
141
|
+
status: t.status,
|
|
142
|
+
score: t.result?.assessment?.globalScore,
|
|
143
|
+
})),
|
|
144
|
+
}).catch(() => { });
|
|
145
|
+
return { passed: false, reason, avgScore };
|
|
146
|
+
}
|
|
147
|
+
this.evaluatedGates.add(gateKey);
|
|
148
|
+
this.ctx.emitter.emit("quality:gate:passed", {
|
|
149
|
+
missionId,
|
|
150
|
+
gateName: gate.name,
|
|
151
|
+
avgScore,
|
|
152
|
+
});
|
|
153
|
+
this.ctx.hooks.runAfter("quality:gate", {
|
|
154
|
+
missionId,
|
|
155
|
+
gateName: gate.name,
|
|
156
|
+
avgScore,
|
|
157
|
+
allPassed: true,
|
|
158
|
+
tasks: afterTasks.map(t => ({
|
|
159
|
+
taskId: t.id,
|
|
160
|
+
title: t.title,
|
|
161
|
+
status: t.status,
|
|
162
|
+
score: t.result?.assessment?.globalScore,
|
|
163
|
+
})),
|
|
164
|
+
}).catch(() => { });
|
|
165
|
+
return { passed: true, avgScore };
|
|
166
|
+
}
|
|
167
|
+
this.evaluatedGates.add(gateKey);
|
|
168
|
+
this.ctx.emitter.emit("quality:gate:passed", {
|
|
169
|
+
missionId,
|
|
170
|
+
gateName: gate.name,
|
|
171
|
+
});
|
|
172
|
+
this.ctx.hooks.runAfter("quality:gate", {
|
|
173
|
+
missionId,
|
|
174
|
+
gateName: gate.name,
|
|
175
|
+
allPassed: true,
|
|
176
|
+
tasks: afterTasks.map(t => ({
|
|
177
|
+
taskId: t.id,
|
|
178
|
+
title: t.title,
|
|
179
|
+
status: t.status,
|
|
180
|
+
score: t.result?.assessment?.globalScore,
|
|
181
|
+
})),
|
|
182
|
+
}).catch(() => { });
|
|
183
|
+
return { passed: true };
|
|
184
|
+
}
|
|
185
|
+
getBlockingGate(missionId, taskTitle, taskId, gates, tasks) {
|
|
186
|
+
for (const gate of gates) {
|
|
187
|
+
if (!gate.blocksTasks.includes(taskTitle) && !gate.blocksTasks.includes(taskId)) {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
const result = this.evaluateGate(missionId, gate, tasks);
|
|
191
|
+
if (!result.passed) {
|
|
192
|
+
return { gate, result };
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
checkMissionThreshold(mission, tasks, defaultThreshold) {
|
|
198
|
+
const threshold = mission.qualityThreshold ?? defaultThreshold;
|
|
199
|
+
if (threshold === undefined)
|
|
200
|
+
return { passed: true };
|
|
201
|
+
const scores = tasks
|
|
202
|
+
.filter(t => t.status === "done")
|
|
203
|
+
.map(t => {
|
|
204
|
+
const score = t.result?.assessment?.globalScore;
|
|
205
|
+
const weight = t.priority ?? 1.0;
|
|
206
|
+
return score !== undefined ? { score, weight } : undefined;
|
|
207
|
+
})
|
|
208
|
+
.filter((s) => s !== undefined);
|
|
209
|
+
if (scores.length === 0) {
|
|
210
|
+
return { passed: true, threshold };
|
|
211
|
+
}
|
|
212
|
+
const totalWeight = scores.reduce((sum, s) => sum + s.weight, 0);
|
|
213
|
+
const avgScore = totalWeight > 0
|
|
214
|
+
? scores.reduce((sum, s) => sum + s.score * s.weight, 0) / totalWeight
|
|
215
|
+
: scores.reduce((sum, s) => sum + s.score, 0) / scores.length;
|
|
216
|
+
const passed = avgScore >= threshold;
|
|
217
|
+
if (!passed) {
|
|
218
|
+
this.ctx.emitter.emit("quality:threshold:failed", {
|
|
219
|
+
missionId: mission.id,
|
|
220
|
+
avgScore,
|
|
221
|
+
threshold,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
return { avgScore, threshold, passed };
|
|
225
|
+
}
|
|
226
|
+
recordAssessment(entityId, entityType, assessment, passed) {
|
|
227
|
+
const key = this.metricsKey(entityType, entityId);
|
|
228
|
+
const m = this.getOrCreate(key, entityId, entityType);
|
|
229
|
+
m.totalAssessments++;
|
|
230
|
+
if (passed)
|
|
231
|
+
m.passedAssessments++;
|
|
232
|
+
if (assessment.globalScore !== undefined) {
|
|
233
|
+
const scores = this.getScoresArray(m);
|
|
234
|
+
scores.push(assessment.globalScore);
|
|
235
|
+
m.avgScore = scores.reduce((a, b) => a + b, 0) / scores.length;
|
|
236
|
+
m.minScore = Math.min(...scores);
|
|
237
|
+
m.maxScore = Math.max(...scores);
|
|
238
|
+
}
|
|
239
|
+
if (assessment.scores) {
|
|
240
|
+
for (const ds of assessment.scores) {
|
|
241
|
+
if (!m.dimensionScores[ds.dimension]) {
|
|
242
|
+
m.dimensionScores[ds.dimension] = ds.score;
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
m.dimensionScores[ds.dimension] =
|
|
246
|
+
(m.dimensionScores[ds.dimension] * (m.totalAssessments - 1) + ds.score) / m.totalAssessments;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (entityType === "task" && assessment.trigger === "fix") {
|
|
251
|
+
m.totalFixes++;
|
|
252
|
+
}
|
|
253
|
+
m.updatedAt = new Date().toISOString();
|
|
254
|
+
}
|
|
255
|
+
getMetrics(entityType, entityId) {
|
|
256
|
+
return this.metrics.get(this.metricsKey(entityType, entityId));
|
|
257
|
+
}
|
|
258
|
+
getAllMetrics(entityType) {
|
|
259
|
+
const all = [...this.metrics.values()];
|
|
260
|
+
if (entityType)
|
|
261
|
+
return all.filter(m => m.entityType === entityType);
|
|
262
|
+
return all;
|
|
263
|
+
}
|
|
264
|
+
aggregateMissionMetrics(missionId, tasks) {
|
|
265
|
+
const key = this.metricsKey("mission", missionId);
|
|
266
|
+
const m = this.getOrCreate(key, missionId, "mission");
|
|
267
|
+
const scores = [];
|
|
268
|
+
let totalAssessments = 0;
|
|
269
|
+
let passedAssessments = 0;
|
|
270
|
+
let totalRetries = 0;
|
|
271
|
+
let totalFixes = 0;
|
|
272
|
+
let deadlinesMet = 0;
|
|
273
|
+
let deadlinesMissed = 0;
|
|
274
|
+
for (const task of tasks) {
|
|
275
|
+
const taskMetrics = this.getMetrics("task", task.id);
|
|
276
|
+
if (taskMetrics) {
|
|
277
|
+
totalAssessments += taskMetrics.totalAssessments;
|
|
278
|
+
passedAssessments += taskMetrics.passedAssessments;
|
|
279
|
+
totalRetries += taskMetrics.totalRetries;
|
|
280
|
+
totalFixes += taskMetrics.totalFixes;
|
|
281
|
+
deadlinesMet += taskMetrics.deadlinesMet;
|
|
282
|
+
deadlinesMissed += taskMetrics.deadlinesMissed;
|
|
283
|
+
if (taskMetrics.avgScore !== undefined) {
|
|
284
|
+
scores.push(taskMetrics.avgScore);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
m.totalAssessments = totalAssessments;
|
|
289
|
+
m.passedAssessments = passedAssessments;
|
|
290
|
+
m.totalRetries = totalRetries;
|
|
291
|
+
m.totalFixes = totalFixes;
|
|
292
|
+
m.deadlinesMet = deadlinesMet;
|
|
293
|
+
m.deadlinesMissed = deadlinesMissed;
|
|
294
|
+
if (scores.length > 0) {
|
|
295
|
+
m.avgScore = scores.reduce((a, b) => a + b, 0) / scores.length;
|
|
296
|
+
m.minScore = Math.min(...scores);
|
|
297
|
+
m.maxScore = Math.max(...scores);
|
|
298
|
+
}
|
|
299
|
+
m.updatedAt = new Date().toISOString();
|
|
300
|
+
return m;
|
|
301
|
+
}
|
|
302
|
+
metricsKey(entityType, entityId) {
|
|
303
|
+
return `${entityType}:${entityId}`;
|
|
304
|
+
}
|
|
305
|
+
getOrCreate(key, entityId, entityType) {
|
|
306
|
+
let m = this.metrics.get(key);
|
|
307
|
+
if (!m) {
|
|
308
|
+
m = {
|
|
309
|
+
entityId,
|
|
310
|
+
entityType,
|
|
311
|
+
totalAssessments: 0,
|
|
312
|
+
passedAssessments: 0,
|
|
313
|
+
dimensionScores: {},
|
|
314
|
+
totalRetries: 0,
|
|
315
|
+
totalFixes: 0,
|
|
316
|
+
deadlinesMet: 0,
|
|
317
|
+
deadlinesMissed: 0,
|
|
318
|
+
updatedAt: new Date().toISOString(),
|
|
319
|
+
};
|
|
320
|
+
this.metrics.set(key, m);
|
|
321
|
+
}
|
|
322
|
+
return m;
|
|
323
|
+
}
|
|
324
|
+
getScoresArray(m) {
|
|
325
|
+
if (m.avgScore !== undefined && m.totalAssessments > 0) {
|
|
326
|
+
return Array(m.totalAssessments - 1).fill(m.avgScore);
|
|
327
|
+
}
|
|
328
|
+
return [];
|
|
329
|
+
}
|
|
330
|
+
ensureGateNotificationRules(gateKey, gate) {
|
|
331
|
+
if (!this.notificationRouter)
|
|
332
|
+
return;
|
|
333
|
+
if (!gate.notifyChannels || gate.notifyChannels.length === 0)
|
|
334
|
+
return;
|
|
335
|
+
if (this.registeredGateRules.has(gateKey))
|
|
336
|
+
return;
|
|
337
|
+
this.registeredGateRules.add(gateKey);
|
|
338
|
+
this.notificationRouter.addRule({
|
|
339
|
+
id: `qgate-pass-${gateKey}`,
|
|
340
|
+
name: `Quality Gate "${gate.name}" Passed (auto-registered)`,
|
|
341
|
+
events: ["quality:gate:passed"],
|
|
342
|
+
condition: { field: "gateName", op: "==", value: gate.name },
|
|
343
|
+
channels: gate.notifyChannels,
|
|
344
|
+
severity: "info",
|
|
345
|
+
});
|
|
346
|
+
this.notificationRouter.addRule({
|
|
347
|
+
id: `qgate-fail-${gateKey}`,
|
|
348
|
+
name: `Quality Gate "${gate.name}" Failed (auto-registered)`,
|
|
349
|
+
events: ["quality:gate:failed"],
|
|
350
|
+
condition: { field: "gateName", op: "==", value: gate.name },
|
|
351
|
+
channels: gate.notifyChannels,
|
|
352
|
+
severity: "critical",
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
clearGateCache(missionId) {
|
|
356
|
+
if (missionId) {
|
|
357
|
+
for (const key of this.evaluatedGates) {
|
|
358
|
+
if (key.startsWith(`${missionId}:`)) {
|
|
359
|
+
this.evaluatedGates.delete(key);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
this.evaluatedGates.clear();
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
dispose() {
|
|
368
|
+
this.metrics.clear();
|
|
369
|
+
this.evaluatedGates.clear();
|
|
370
|
+
this.registeredGateRules.clear();
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
//# sourceMappingURL=quality-controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quality-controller.js","sourceRoot":"","sources":["../src/quality-controller.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,MAAM,OAAO,iBAAiB;IAOlB;IANF,OAAO,GAAG,IAAI,GAAG,EAA0B,CAAC;IAC5C,cAAc,GAAG,IAAI,GAAG,EAAU,CAAC;IACnC,mBAAmB,GAAG,IAAI,GAAG,EAAU,CAAC;IACxC,kBAAkB,CAA0B;IAEpD,Y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,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE;YAC5D,QAAQ,EAAE,IAAI,CAAC,cAAc;YAC7B,QAAQ,EAAE,UAAU;SACrB,CAAC,CAAC;IACL,CAAC;IAED,cAAc,CAAC,SAAkB;QAC/B,IAAI,SAAS,EAAE,CAAC;YACd,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;gBACtC,IAAI,GAAG,CAAC,UAAU,CAAC,GAAG,SAAS,GAAG,CAAC,EAAE,CAAC;oBACpC,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBAClC,CAAC;YACH,CAAC;QACH,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,CAAC;QAC9B,CAAC;IACH,CAAC;IAED,OAAO;QACL,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACrB,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,CAAC;QAC5B,IAAI,CAAC,mBAAmB,CAAC,KAAK,EAAE,CAAC;IACnC,CAAC;CACF"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { AgentActivity, TaskResult, TaskOutcome } from "./types.js";
|
|
2
|
+
export type RunStatus = "running" | "completed" | "failed" | "killed";
|
|
3
|
+
export interface RunRecord {
|
|
4
|
+
id: string;
|
|
5
|
+
taskId: string;
|
|
6
|
+
pid: number;
|
|
7
|
+
agentName: string;
|
|
8
|
+
sessionId?: string;
|
|
9
|
+
status: RunStatus;
|
|
10
|
+
startedAt: string;
|
|
11
|
+
updatedAt: string;
|
|
12
|
+
activity: AgentActivity;
|
|
13
|
+
result?: TaskResult;
|
|
14
|
+
/** Outcomes auto-collected during execution (files, media, text artifacts). */
|
|
15
|
+
outcomes?: TaskOutcome[];
|
|
16
|
+
configPath: string;
|
|
17
|
+
}
|
|
18
|
+
export interface RunStore {
|
|
19
|
+
upsertRun(run: RunRecord): Promise<void>;
|
|
20
|
+
updateActivity(runId: string, activity: AgentActivity): Promise<void>;
|
|
21
|
+
/** Store auto-collected outcomes on the run record (called before completeRun). */
|
|
22
|
+
updateOutcomes(runId: string, outcomes: TaskOutcome[]): Promise<void>;
|
|
23
|
+
completeRun(runId: string, status: RunStatus, result: TaskResult): Promise<void>;
|
|
24
|
+
getRun(runId: string): Promise<RunRecord | undefined>;
|
|
25
|
+
getRunByTaskId(taskId: string): Promise<RunRecord | undefined>;
|
|
26
|
+
getActiveRuns(): Promise<RunRecord[]>;
|
|
27
|
+
getTerminalRuns(): Promise<RunRecord[]>;
|
|
28
|
+
deleteRun(runId: string): Promise<void>;
|
|
29
|
+
close(): Promise<void> | void;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=run-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-store.d.ts","sourceRoot":"","sources":["../src/run-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzE,MAAM,MAAM,SAAS,GAAG,SAAS,GAAG,WAAW,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAEtE,MAAM,WAAW,SAAS;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,SAAS,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,aAAa,CAAC;IACxB,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,+EAA+E;IAC/E,QAAQ,CAAC,EAAE,WAAW,EAAE,CAAC;IACzB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,QAAQ;IACvB,SAAS,CAAC,GAAG,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACzC,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACtE,mFAAmF;IACnF,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACtE,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACjF,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC,CAAC;IACtD,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC,CAAC;IAC/D,aAAa,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC;IACtC,eAAe,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC;IACxC,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACxC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;CAC/B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-store.js","sourceRoot":"","sources":["../src/run-store.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { OrchestratorContext } from "./orchestrator-context.js";
|
|
2
|
+
import type { ScheduleEntry, Mission } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Scheduler — tick-driven scheduling engine for missions.
|
|
5
|
+
*
|
|
6
|
+
* Missions use dedicated statuses for scheduling:
|
|
7
|
+
* - `scheduled`: one-shot — waiting for the trigger time, then transitions to
|
|
8
|
+
* active. After completion the mission stays completed (schedule disabled).
|
|
9
|
+
* On failure it returns to `scheduled` for automatic retry.
|
|
10
|
+
* - `recurring`: recurring — fires on every cron tick, transitions to active,
|
|
11
|
+
* then returns to `recurring` after completion or failure.
|
|
12
|
+
*/
|
|
13
|
+
export declare class Scheduler {
|
|
14
|
+
private ctx;
|
|
15
|
+
private schedules;
|
|
16
|
+
private lastCheckMs;
|
|
17
|
+
private checkIntervalMs;
|
|
18
|
+
private executeMissionFn?;
|
|
19
|
+
private missionCompletedHandler?;
|
|
20
|
+
constructor(ctx: OrchestratorContext, opts?: {
|
|
21
|
+
checkIntervalMs?: number;
|
|
22
|
+
});
|
|
23
|
+
init(): Promise<void>;
|
|
24
|
+
setExecutor(fn: (missionId: string) => void): void;
|
|
25
|
+
registerMission(mission: Mission): ScheduleEntry | null;
|
|
26
|
+
unregisterMission(missionId: string): boolean;
|
|
27
|
+
check(): Promise<void>;
|
|
28
|
+
private triggerSchedule;
|
|
29
|
+
getSchedule(scheduleId: string): ScheduleEntry | undefined;
|
|
30
|
+
getScheduleByMissionId(missionId: string): ScheduleEntry | undefined;
|
|
31
|
+
getAllSchedules(): ScheduleEntry[];
|
|
32
|
+
getActiveSchedules(): ScheduleEntry[];
|
|
33
|
+
dispose(): void;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=scheduler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scheduler.d.ts","sourceRoot":"","sources":["../src/scheduler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AACrE,OAAO,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAGzD;;;;;;;;;GASG;AACH,qBAAa,SAAS;IAQlB,OAAO,CAAC,GAAG;IAPb,OAAO,CAAC,SAAS,CAAoC;IACrD,OAAO,CAAC,WAAW,CAAK;IACxB,OAAO,CAAC,eAAe,CAAS;IAChC,OAAO,CAAC,gBAAgB,CAAC,CAA8B;IACvD,OAAO,CAAC,uBAAuB,CAAC,CAAuC;gBAG7D,GAAG,EAAE,mBAAmB,EAChC,IAAI,CAAC,EAAE;QAAE,eAAe,CAAC,EAAE,MAAM,CAAA;KAAE;IAK/B,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAmB3B,WAAW,CAAC,EAAE,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI;IAIlD,eAAe,CAAC,OAAO,EAAE,OAAO,GAAG,aAAa,GAAG,IAAI;IAyCvD,iBAAiB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;IAKvC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;YAiBd,eAAe;IA+E7B,WAAW,CAAC,UAAU,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS;IAI1D,sBAAsB,CAAC,SAAS,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS;IAIpE,eAAe,IAAI,aAAa,EAAE;IAIlC,kBAAkB,IAAI,aAAa,EAAE;IAIrC,OAAO,IAAI,IAAI;CAQhB"}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { isCronExpression, nextCronOccurrence } from "./cron.js";
|
|
2
|
+
/**
|
|
3
|
+
* Scheduler — tick-driven scheduling engine for missions.
|
|
4
|
+
*
|
|
5
|
+
* Missions use dedicated statuses for scheduling:
|
|
6
|
+
* - `scheduled`: one-shot — waiting for the trigger time, then transitions to
|
|
7
|
+
* active. After completion the mission stays completed (schedule disabled).
|
|
8
|
+
* On failure it returns to `scheduled` for automatic retry.
|
|
9
|
+
* - `recurring`: recurring — fires on every cron tick, transitions to active,
|
|
10
|
+
* then returns to `recurring` after completion or failure.
|
|
11
|
+
*/
|
|
12
|
+
export class Scheduler {
|
|
13
|
+
ctx;
|
|
14
|
+
schedules = new Map();
|
|
15
|
+
lastCheckMs = 0;
|
|
16
|
+
checkIntervalMs;
|
|
17
|
+
executeMissionFn;
|
|
18
|
+
missionCompletedHandler;
|
|
19
|
+
constructor(ctx, opts) {
|
|
20
|
+
this.ctx = ctx;
|
|
21
|
+
this.checkIntervalMs = opts?.checkIntervalMs ?? 30_000;
|
|
22
|
+
}
|
|
23
|
+
async init() {
|
|
24
|
+
const missions = await this.ctx.registry.getAllMissions?.() ?? [];
|
|
25
|
+
const terminalStates = new Set(["completed", "cancelled", "draft"]);
|
|
26
|
+
for (const mission of missions) {
|
|
27
|
+
if (!mission.schedule)
|
|
28
|
+
continue;
|
|
29
|
+
if (terminalStates.has(mission.status))
|
|
30
|
+
continue;
|
|
31
|
+
this.registerMission(mission);
|
|
32
|
+
}
|
|
33
|
+
this.missionCompletedHandler = async ({ missionId }) => {
|
|
34
|
+
const mission = await this.ctx.registry.getMission?.(missionId);
|
|
35
|
+
if (!mission?.schedule)
|
|
36
|
+
return;
|
|
37
|
+
if (mission.status === "recurring" || mission.status === "scheduled") {
|
|
38
|
+
this.registerMission(mission);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
this.ctx.emitter.on("mission:completed", this.missionCompletedHandler);
|
|
42
|
+
}
|
|
43
|
+
setExecutor(fn) {
|
|
44
|
+
this.executeMissionFn = fn;
|
|
45
|
+
}
|
|
46
|
+
registerMission(mission) {
|
|
47
|
+
if (!mission.schedule)
|
|
48
|
+
return null;
|
|
49
|
+
const isRecurring = mission.status === "recurring";
|
|
50
|
+
const isCron = isCronExpression(mission.schedule);
|
|
51
|
+
const now = new Date();
|
|
52
|
+
let nextRunAt;
|
|
53
|
+
if (isCron) {
|
|
54
|
+
const next = nextCronOccurrence(mission.schedule, now);
|
|
55
|
+
nextRunAt = next?.toISOString();
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
const scheduled = new Date(mission.schedule);
|
|
59
|
+
if (scheduled.getTime() > now.getTime()) {
|
|
60
|
+
nextRunAt = scheduled.toISOString();
|
|
61
|
+
}
|
|
62
|
+
else if (!isRecurring) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const entry = {
|
|
67
|
+
id: `sched-${mission.id}`,
|
|
68
|
+
missionId: mission.id,
|
|
69
|
+
expression: mission.schedule,
|
|
70
|
+
recurring: isRecurring,
|
|
71
|
+
enabled: true,
|
|
72
|
+
nextRunAt,
|
|
73
|
+
createdAt: new Date().toISOString(),
|
|
74
|
+
};
|
|
75
|
+
this.schedules.set(entry.id, entry);
|
|
76
|
+
this.ctx.emitter.emit("schedule:created", {
|
|
77
|
+
scheduleId: entry.id,
|
|
78
|
+
missionId: mission.id,
|
|
79
|
+
nextRunAt,
|
|
80
|
+
});
|
|
81
|
+
return entry;
|
|
82
|
+
}
|
|
83
|
+
unregisterMission(missionId) {
|
|
84
|
+
const schedId = `sched-${missionId}`;
|
|
85
|
+
return this.schedules.delete(schedId);
|
|
86
|
+
}
|
|
87
|
+
async check() {
|
|
88
|
+
const now = Date.now();
|
|
89
|
+
if (now - this.lastCheckMs < this.checkIntervalMs)
|
|
90
|
+
return;
|
|
91
|
+
this.lastCheckMs = now;
|
|
92
|
+
for (const [schedId, entry] of this.schedules) {
|
|
93
|
+
if (!entry.enabled)
|
|
94
|
+
continue;
|
|
95
|
+
if (!entry.nextRunAt)
|
|
96
|
+
continue;
|
|
97
|
+
const nextRun = new Date(entry.nextRunAt).getTime();
|
|
98
|
+
if (now < nextRun)
|
|
99
|
+
continue;
|
|
100
|
+
await this.triggerSchedule(schedId, entry);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
async triggerSchedule(schedId, entry) {
|
|
104
|
+
const mission = await this.ctx.registry.getMission?.(entry.missionId);
|
|
105
|
+
if (!mission) {
|
|
106
|
+
entry.enabled = false;
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (mission.status !== "scheduled" && mission.status !== "recurring") {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (mission.endDate) {
|
|
113
|
+
const endTime = new Date(mission.endDate).getTime();
|
|
114
|
+
if (Date.now() >= endTime) {
|
|
115
|
+
entry.enabled = false;
|
|
116
|
+
entry.nextRunAt = undefined;
|
|
117
|
+
await this.ctx.registry.updateMission?.(entry.missionId, { status: "completed" });
|
|
118
|
+
this.ctx.emitter.emit("schedule:expired", {
|
|
119
|
+
scheduleId: schedId,
|
|
120
|
+
missionId: entry.missionId,
|
|
121
|
+
endDate: mission.endDate,
|
|
122
|
+
});
|
|
123
|
+
this.ctx.emitter.emit("log", {
|
|
124
|
+
level: "info",
|
|
125
|
+
message: `[Scheduler] Mission ${entry.missionId} schedule expired (endDate: ${mission.endDate}). Transitioned to completed.`,
|
|
126
|
+
});
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const hookResult = this.ctx.hooks.runBeforeSync("schedule:trigger", {
|
|
131
|
+
scheduleId: schedId,
|
|
132
|
+
missionId: entry.missionId,
|
|
133
|
+
expression: entry.expression,
|
|
134
|
+
});
|
|
135
|
+
if (hookResult.cancelled) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
this.ctx.emitter.emit("schedule:triggered", {
|
|
139
|
+
scheduleId: schedId,
|
|
140
|
+
missionId: entry.missionId,
|
|
141
|
+
expression: entry.expression,
|
|
142
|
+
});
|
|
143
|
+
let executionFailed = false;
|
|
144
|
+
try {
|
|
145
|
+
if (this.executeMissionFn) {
|
|
146
|
+
this.executeMissionFn(entry.missionId);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
catch (err) {
|
|
150
|
+
executionFailed = true;
|
|
151
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
152
|
+
this.ctx.emitter.emit("log", {
|
|
153
|
+
level: "error",
|
|
154
|
+
message: `[Scheduler] Failed to execute mission ${entry.missionId}: ${msg}`,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
if (executionFailed && !entry.recurring) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
entry.lastRunAt = new Date().toISOString();
|
|
161
|
+
if (entry.recurring && isCronExpression(entry.expression)) {
|
|
162
|
+
const next = nextCronOccurrence(entry.expression, new Date());
|
|
163
|
+
entry.nextRunAt = next?.toISOString();
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
entry.enabled = false;
|
|
167
|
+
entry.nextRunAt = undefined;
|
|
168
|
+
}
|
|
169
|
+
this.ctx.emitter.emit("schedule:completed", {
|
|
170
|
+
scheduleId: schedId,
|
|
171
|
+
missionId: entry.missionId,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
getSchedule(scheduleId) {
|
|
175
|
+
return this.schedules.get(scheduleId);
|
|
176
|
+
}
|
|
177
|
+
getScheduleByMissionId(missionId) {
|
|
178
|
+
return this.schedules.get(`sched-${missionId}`);
|
|
179
|
+
}
|
|
180
|
+
getAllSchedules() {
|
|
181
|
+
return [...this.schedules.values()];
|
|
182
|
+
}
|
|
183
|
+
getActiveSchedules() {
|
|
184
|
+
return [...this.schedules.values()].filter(s => s.enabled);
|
|
185
|
+
}
|
|
186
|
+
dispose() {
|
|
187
|
+
if (this.missionCompletedHandler) {
|
|
188
|
+
this.ctx.emitter.off("mission:completed", this.missionCompletedHandler);
|
|
189
|
+
this.missionCompletedHandler = undefined;
|
|
190
|
+
}
|
|
191
|
+
this.schedules.clear();
|
|
192
|
+
this.executeMissionFn = undefined;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
//# sourceMappingURL=scheduler.js.map
|