@h-rig/supervisor-plugin 0.0.6-alpha.157 → 0.0.6-alpha.159
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/src/analysis/blockers.d.ts +35 -0
- package/dist/src/analysis/blockers.js +355 -0
- package/dist/src/analysis/taskGraphPrimitives.d.ts +58 -0
- package/dist/src/analysis/taskGraphPrimitives.js +528 -0
- package/dist/src/analysis/taskRanking.d.ts +24 -0
- package/dist/src/analysis/taskRanking.js +374 -0
- package/dist/src/analysis/taskScore.d.ts +17 -0
- package/dist/src/analysis/taskScore.js +49 -0
- package/dist/src/cli.d.ts +1 -7
- package/dist/src/cli.js +611 -91
- package/dist/src/index.js +682 -155
- package/dist/src/loop.d.ts +3 -5
- package/dist/src/loop.js +503 -23
- package/dist/src/plugin.js +655 -134
- package/dist/src/run-status.d.ts +2 -0
- package/dist/src/run-status.js +20 -0
- package/dist/src/supervisor.js +345 -6
- package/package.json +3 -7
package/dist/src/loop.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type { TaskLike } from "@rig/
|
|
3
|
-
|
|
4
|
-
import type { RunControl } from "@rig/run-worker/runs";
|
|
5
|
-
import type { RunRecord, SupervisorEvent, SupervisorProjection, SupervisorSelectionPolicy, SupervisorStopReason } from "@rig/contracts";
|
|
1
|
+
import { type RankedReadyTask } from "./analysis/taskRanking";
|
|
2
|
+
import type { ClientTaskProjection, RunReadModelControl, RunRecord, SupervisorEvent, SupervisorProjection, SupervisorSelectionPolicy, SupervisorStopReason, TaskLike } from "@rig/contracts";
|
|
3
|
+
type RunControl = RunReadModelControl;
|
|
6
4
|
export interface SupervisorLoopOptions {
|
|
7
5
|
readonly maxTasks?: number;
|
|
8
6
|
readonly concurrency?: number;
|
package/dist/src/loop.js
CHANGED
|
@@ -1,7 +1,409 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
// packages/supervisor-plugin/src/
|
|
3
|
-
import {
|
|
4
|
-
|
|
2
|
+
// packages/supervisor-plugin/src/analysis/taskGraphPrimitives.ts
|
|
3
|
+
import {
|
|
4
|
+
OPERATOR_INACTIVE_RUN_STATUSES
|
|
5
|
+
} from "@rig/contracts";
|
|
6
|
+
function isObjectRecord(value) {
|
|
7
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
8
|
+
}
|
|
9
|
+
function readStringList(value) {
|
|
10
|
+
return Array.isArray(value) ? value.filter((entry) => typeof entry === "string" && entry.length > 0) : [];
|
|
11
|
+
}
|
|
12
|
+
function unique(values) {
|
|
13
|
+
return Array.from(new Set(values));
|
|
14
|
+
}
|
|
15
|
+
function readTaskMetadataStringList(task, key) {
|
|
16
|
+
const taskRecord = task;
|
|
17
|
+
const topLevel = readStringList(taskRecord[key]);
|
|
18
|
+
if (topLevel.length > 0)
|
|
19
|
+
return topLevel;
|
|
20
|
+
const metadata = isObjectRecord(task.metadata) ? task.metadata : null;
|
|
21
|
+
const metadataList = readStringList(metadata?.[key]);
|
|
22
|
+
if (metadataList.length > 0)
|
|
23
|
+
return metadataList;
|
|
24
|
+
if (key === "dependencies") {
|
|
25
|
+
return readStringList(metadata?.deps);
|
|
26
|
+
}
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
function readTaskBlockingDependencyRefs(task) {
|
|
30
|
+
return readTaskMetadataStringList(task, "dependencies");
|
|
31
|
+
}
|
|
32
|
+
function readTaskSourceIssueId(task) {
|
|
33
|
+
if (typeof task.sourceIssueId === "string" && task.sourceIssueId.length > 0) {
|
|
34
|
+
return task.sourceIssueId;
|
|
35
|
+
}
|
|
36
|
+
const metadata = isObjectRecord(task.metadata) ? task.metadata : null;
|
|
37
|
+
if (typeof metadata?.sourceIssueId === "string" && metadata.sourceIssueId.length > 0) {
|
|
38
|
+
return metadata.sourceIssueId;
|
|
39
|
+
}
|
|
40
|
+
const rigMetadata = isObjectRecord(metadata?._rig) ? metadata._rig : null;
|
|
41
|
+
return typeof rigMetadata?.sourceIssueId === "string" && rigMetadata.sourceIssueId.length > 0 ? rigMetadata.sourceIssueId : null;
|
|
42
|
+
}
|
|
43
|
+
function readTaskScope(task) {
|
|
44
|
+
const taskRecord = task;
|
|
45
|
+
const topLevel = readStringList(taskRecord.scope);
|
|
46
|
+
if (topLevel.length > 0)
|
|
47
|
+
return unique(topLevel.map((entry) => entry.trim()).filter((entry) => entry.length > 0));
|
|
48
|
+
const metadata = isObjectRecord(task.metadata) ? task.metadata : null;
|
|
49
|
+
const metadataScope = readStringList(metadata?.scope);
|
|
50
|
+
if (metadataScope.length > 0)
|
|
51
|
+
return unique(metadataScope.map((entry) => entry.trim()).filter((entry) => entry.length > 0));
|
|
52
|
+
return unique([
|
|
53
|
+
...readStringList(metadata?.files),
|
|
54
|
+
...readStringList(metadata?.paths)
|
|
55
|
+
].map((entry) => entry.trim()).filter((entry) => entry.length > 0));
|
|
56
|
+
}
|
|
57
|
+
function resolveTaskReference(ref, tasksById, taskIdByExternalRef, taskIdBySourceIssueId) {
|
|
58
|
+
if (tasksById.has(ref))
|
|
59
|
+
return ref;
|
|
60
|
+
return taskIdBySourceIssueId.get(ref) ?? taskIdByExternalRef.get(ref) ?? null;
|
|
61
|
+
}
|
|
62
|
+
function buildTaskReferenceIndex(tasks) {
|
|
63
|
+
return {
|
|
64
|
+
tasksById: new Map(tasks.map((task) => [task.id, task])),
|
|
65
|
+
taskIdByExternalRef: new Map(tasks.flatMap((task) => task.externalId ? [[task.externalId, task.id]] : [])),
|
|
66
|
+
taskIdBySourceIssueId: new Map(tasks.flatMap((task) => {
|
|
67
|
+
const sourceIssueId = readTaskSourceIssueId(task);
|
|
68
|
+
return sourceIssueId ? [[sourceIssueId, task.id]] : [];
|
|
69
|
+
}))
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function computeTaskBlockingDepths(tasks) {
|
|
73
|
+
const { tasksById, taskIdByExternalRef, taskIdBySourceIssueId } = buildTaskReferenceIndex(tasks);
|
|
74
|
+
const memo = new Map;
|
|
75
|
+
const visit = (taskId, stack) => {
|
|
76
|
+
const cached = memo.get(taskId);
|
|
77
|
+
if (cached !== undefined)
|
|
78
|
+
return cached;
|
|
79
|
+
if (stack.has(taskId))
|
|
80
|
+
return 0;
|
|
81
|
+
const task = tasksById.get(taskId);
|
|
82
|
+
if (!task)
|
|
83
|
+
return 0;
|
|
84
|
+
stack.add(taskId);
|
|
85
|
+
const blockers = readTaskBlockingDependencyRefs(task).map((ref) => resolveTaskReference(ref, tasksById, taskIdByExternalRef, taskIdBySourceIssueId)).filter((ref) => ref !== null && ref !== taskId);
|
|
86
|
+
const depth = blockers.length === 0 ? 0 : Math.max(...blockers.map((blockerId) => visit(blockerId, stack) + 1));
|
|
87
|
+
stack.delete(taskId);
|
|
88
|
+
memo.set(taskId, depth);
|
|
89
|
+
return depth;
|
|
90
|
+
};
|
|
91
|
+
for (const task of tasks) {
|
|
92
|
+
visit(task.id, new Set);
|
|
93
|
+
}
|
|
94
|
+
return memo;
|
|
95
|
+
}
|
|
96
|
+
function isTaskTerminalStatus(status) {
|
|
97
|
+
switch (status) {
|
|
98
|
+
case "closed":
|
|
99
|
+
case "completed":
|
|
100
|
+
case "done":
|
|
101
|
+
case "cancelled":
|
|
102
|
+
case "canceled":
|
|
103
|
+
return true;
|
|
104
|
+
default:
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function isTaskBlockedStatus(status) {
|
|
109
|
+
return status === "blocked";
|
|
110
|
+
}
|
|
111
|
+
function isTaskRunnableStatus(status) {
|
|
112
|
+
if (status === null || status === undefined || status === "")
|
|
113
|
+
return true;
|
|
114
|
+
if (isTaskTerminalStatus(status) || isTaskBlockedStatus(status))
|
|
115
|
+
return false;
|
|
116
|
+
switch (status) {
|
|
117
|
+
case "ready":
|
|
118
|
+
case "open":
|
|
119
|
+
case "failed":
|
|
120
|
+
return true;
|
|
121
|
+
default:
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function computeTaskDependencyBadges(tasks) {
|
|
126
|
+
const index = buildTaskReferenceIndex(tasks);
|
|
127
|
+
const blockingDepths = computeTaskBlockingDepths(tasks);
|
|
128
|
+
const dependencyIdsByTask = new Map;
|
|
129
|
+
const unresolvedRefsByTask = new Map;
|
|
130
|
+
const blocksByTask = new Map;
|
|
131
|
+
for (const task of tasks) {
|
|
132
|
+
const dependencyIds = [];
|
|
133
|
+
const unresolvedRefs = [];
|
|
134
|
+
for (const ref of readTaskBlockingDependencyRefs(task)) {
|
|
135
|
+
const dependencyId = resolveTaskReference(ref, index.tasksById, index.taskIdByExternalRef, index.taskIdBySourceIssueId);
|
|
136
|
+
if (dependencyId && dependencyId !== task.id) {
|
|
137
|
+
dependencyIds.push(dependencyId);
|
|
138
|
+
const blocks = blocksByTask.get(dependencyId);
|
|
139
|
+
if (blocks) {
|
|
140
|
+
blocks.push(task.id);
|
|
141
|
+
} else {
|
|
142
|
+
blocksByTask.set(dependencyId, [task.id]);
|
|
143
|
+
}
|
|
144
|
+
} else {
|
|
145
|
+
unresolvedRefs.push(ref);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
dependencyIdsByTask.set(task.id, unique(dependencyIds));
|
|
149
|
+
unresolvedRefsByTask.set(task.id, unique(unresolvedRefs));
|
|
150
|
+
}
|
|
151
|
+
const summaries = new Map;
|
|
152
|
+
for (const task of tasks) {
|
|
153
|
+
const dependencyIds = dependencyIdsByTask.get(task.id) ?? [];
|
|
154
|
+
const unresolvedDependencyRefs = unresolvedRefsByTask.get(task.id) ?? [];
|
|
155
|
+
const blockedBy = dependencyIds.filter((dependencyId) => {
|
|
156
|
+
const dependency = index.tasksById.get(dependencyId);
|
|
157
|
+
return dependency ? !isTaskTerminalStatus(dependency.status) : false;
|
|
158
|
+
});
|
|
159
|
+
const blocks = unique(blocksByTask.get(task.id) ?? []);
|
|
160
|
+
const blocked = isTaskBlockedStatus(task.status) || blockedBy.length > 0;
|
|
161
|
+
const ready = isTaskRunnableStatus(task.status) && !blocked;
|
|
162
|
+
const badges = [];
|
|
163
|
+
if (blocked) {
|
|
164
|
+
badges.push({
|
|
165
|
+
kind: "blocked",
|
|
166
|
+
label: blockedBy.length > 0 ? `blocked \xD7${blockedBy.length}` : "blocked",
|
|
167
|
+
description: blockedBy.length > 0 ? `Waiting on ${blockedBy.join(", ")}.` : "Task source marks this task blocked.",
|
|
168
|
+
...blockedBy.length > 0 ? { count: blockedBy.length } : {},
|
|
169
|
+
taskIds: blockedBy
|
|
170
|
+
});
|
|
171
|
+
} else if (ready) {
|
|
172
|
+
badges.push({
|
|
173
|
+
kind: "ready",
|
|
174
|
+
label: "ready",
|
|
175
|
+
description: "No open dependencies block this task."
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
if (dependencyIds.length > 0 || blocks.length > 0 || unresolvedDependencyRefs.length > 0) {
|
|
179
|
+
badges.push({
|
|
180
|
+
kind: "dependency",
|
|
181
|
+
label: `deps ${dependencyIds.length}/${blocks.length}`,
|
|
182
|
+
description: [
|
|
183
|
+
dependencyIds.length > 0 ? `Depends on ${dependencyIds.join(", ")}.` : null,
|
|
184
|
+
blocks.length > 0 ? `Blocks ${blocks.join(", ")}.` : null,
|
|
185
|
+
unresolvedDependencyRefs.length > 0 ? `Unresolved refs: ${unresolvedDependencyRefs.join(", ")}.` : null
|
|
186
|
+
].filter((part) => part !== null).join(" "),
|
|
187
|
+
count: dependencyIds.length + blocks.length,
|
|
188
|
+
taskIds: unique([...dependencyIds, ...blocks])
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
summaries.set(task.id, {
|
|
192
|
+
taskId: task.id,
|
|
193
|
+
blockingDepth: blockingDepths.get(task.id) ?? 0,
|
|
194
|
+
dependencyIds,
|
|
195
|
+
unresolvedDependencyRefs,
|
|
196
|
+
blockedBy,
|
|
197
|
+
blocks,
|
|
198
|
+
blocked,
|
|
199
|
+
ready,
|
|
200
|
+
dependencyCount: dependencyIds.length,
|
|
201
|
+
dependentCount: blocks.length,
|
|
202
|
+
badges
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
return summaries;
|
|
206
|
+
}
|
|
207
|
+
var TASK_STATUSES = new Set([
|
|
208
|
+
"draft",
|
|
209
|
+
"open",
|
|
210
|
+
"ready",
|
|
211
|
+
"queued",
|
|
212
|
+
"running",
|
|
213
|
+
"in_progress",
|
|
214
|
+
"under_review",
|
|
215
|
+
"blocked",
|
|
216
|
+
"unknown",
|
|
217
|
+
"completed",
|
|
218
|
+
"failed",
|
|
219
|
+
"cancelled",
|
|
220
|
+
"closed"
|
|
221
|
+
]);
|
|
222
|
+
function stringValue(value) {
|
|
223
|
+
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
|
|
224
|
+
}
|
|
225
|
+
function stringArray(value) {
|
|
226
|
+
return Array.isArray(value) ? value.filter((entry) => typeof entry === "string" && entry.trim().length > 0).map((entry) => entry.trim()) : [];
|
|
227
|
+
}
|
|
228
|
+
function numberOrNull(value) {
|
|
229
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : null;
|
|
230
|
+
}
|
|
231
|
+
function metadataOf(task) {
|
|
232
|
+
return isObjectRecord(task.metadata) ? task.metadata : {};
|
|
233
|
+
}
|
|
234
|
+
function normalizeTaskStatus(status) {
|
|
235
|
+
const token = typeof status === "string" ? status.trim().toLowerCase() : "";
|
|
236
|
+
if (token === "done")
|
|
237
|
+
return "completed";
|
|
238
|
+
if (token === "canceled")
|
|
239
|
+
return "cancelled";
|
|
240
|
+
return TASK_STATUSES.has(token) ? token : "unknown";
|
|
241
|
+
}
|
|
242
|
+
function toTaskDependencyProjection(task) {
|
|
243
|
+
const metadata = metadataOf(task);
|
|
244
|
+
return {
|
|
245
|
+
id: String(task.id),
|
|
246
|
+
title: stringValue(task.title),
|
|
247
|
+
status: normalizeTaskStatus(task.status),
|
|
248
|
+
priority: numberOrNull(task.priority),
|
|
249
|
+
metadata,
|
|
250
|
+
externalId: stringValue(task.externalId),
|
|
251
|
+
sourceIssueId: stringValue(task.sourceIssueId),
|
|
252
|
+
dependencies: stringArray(task.dependencies),
|
|
253
|
+
parentChildDeps: stringArray(task.parentChildDeps),
|
|
254
|
+
createdAt: stringValue(task.createdAt) ?? "",
|
|
255
|
+
updatedAt: stringValue(task.updatedAt) ?? "",
|
|
256
|
+
role: stringValue(task.role),
|
|
257
|
+
scope: stringArray(task.scope),
|
|
258
|
+
validationKeys: stringArray(task.validationKeys),
|
|
259
|
+
labels: stringArray(task.labels),
|
|
260
|
+
assignees: task.assignees ?? null,
|
|
261
|
+
assignedTo: task.assignedTo ?? null
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
function latestRunByTaskId(runs) {
|
|
265
|
+
const byTask = new Map;
|
|
266
|
+
const stamp = (run) => Date.parse(run.updatedAt ?? run.startedAt ?? "") || 0;
|
|
267
|
+
for (const run of runs) {
|
|
268
|
+
if (!run.taskId)
|
|
269
|
+
continue;
|
|
270
|
+
const current = byTask.get(run.taskId);
|
|
271
|
+
if (!current || stamp(run) >= stamp(current))
|
|
272
|
+
byTask.set(run.taskId, run);
|
|
273
|
+
}
|
|
274
|
+
return byTask;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// packages/supervisor-plugin/src/analysis/taskScore.ts
|
|
278
|
+
var ROLE_WEIGHT = {
|
|
279
|
+
architect: 25,
|
|
280
|
+
extractor: 10
|
|
281
|
+
};
|
|
282
|
+
var CRITICALITY_WEIGHT = {
|
|
283
|
+
core: 20,
|
|
284
|
+
high: 10,
|
|
285
|
+
normal: 0
|
|
286
|
+
};
|
|
287
|
+
function finiteNumber(value, fallback) {
|
|
288
|
+
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
289
|
+
}
|
|
290
|
+
function scoreTask(input) {
|
|
291
|
+
const priority = finiteNumber(input.priority, 3);
|
|
292
|
+
const unblockCount = Math.max(0, finiteNumber(input.unblockCount, 0));
|
|
293
|
+
const roleWeight = input.role ? ROLE_WEIGHT[input.role] ?? 0 : 0;
|
|
294
|
+
const criticalityWeight = input.criticality ? CRITICALITY_WEIGHT[input.criticality] ?? 0 : 0;
|
|
295
|
+
const validationWeight = (input.validation ?? []).some((entry) => entry.includes("test-contract") || entry.includes("test-boundary")) ? 8 : 0;
|
|
296
|
+
const queueWeight = finiteNumber(input.queueWeight, 0);
|
|
297
|
+
return unblockCount * 100 + (10 - priority) + roleWeight + criticalityWeight + validationWeight + queueWeight;
|
|
298
|
+
}
|
|
299
|
+
function rankTasks(tasks, scoreInput, idOf) {
|
|
300
|
+
return tasks.map((task) => {
|
|
301
|
+
const input = scoreInput(task);
|
|
302
|
+
return {
|
|
303
|
+
task,
|
|
304
|
+
score: scoreTask(input),
|
|
305
|
+
priority: finiteNumber(input.priority, 3),
|
|
306
|
+
unblockCount: Math.max(0, finiteNumber(input.unblockCount, 0))
|
|
307
|
+
};
|
|
308
|
+
}).toSorted((left, right) => {
|
|
309
|
+
const scoreDelta = right.score - left.score;
|
|
310
|
+
if (scoreDelta !== 0)
|
|
311
|
+
return scoreDelta;
|
|
312
|
+
const unblockDelta = right.unblockCount - left.unblockCount;
|
|
313
|
+
if (unblockDelta !== 0)
|
|
314
|
+
return unblockDelta;
|
|
315
|
+
const priorityDelta = left.priority - right.priority;
|
|
316
|
+
if (priorityDelta !== 0)
|
|
317
|
+
return priorityDelta;
|
|
318
|
+
return idOf(left.task).localeCompare(idOf(right.task));
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// packages/supervisor-plugin/src/analysis/taskRanking.ts
|
|
323
|
+
function isObjectRecord2(value) {
|
|
324
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
325
|
+
}
|
|
326
|
+
function readStringList2(value) {
|
|
327
|
+
return Array.isArray(value) ? value.filter((entry) => typeof entry === "string" && entry.length > 0) : [];
|
|
328
|
+
}
|
|
329
|
+
function readTaskRole(task) {
|
|
330
|
+
if (typeof task.role === "string" && task.role.trim())
|
|
331
|
+
return task.role.trim();
|
|
332
|
+
const metadata = isObjectRecord2(task.metadata) ? task.metadata : null;
|
|
333
|
+
return typeof metadata?.role === "string" && metadata.role.trim() ? metadata.role.trim() : null;
|
|
334
|
+
}
|
|
335
|
+
function readTaskCriticality(task) {
|
|
336
|
+
const metadata = isObjectRecord2(task.metadata) ? task.metadata : null;
|
|
337
|
+
return typeof metadata?.criticality === "string" && metadata.criticality.trim() ? metadata.criticality.trim() : null;
|
|
338
|
+
}
|
|
339
|
+
function readTaskValidationKeys(task) {
|
|
340
|
+
const taskRecord = task;
|
|
341
|
+
const topLevel = readStringList2(taskRecord.validationKeys);
|
|
342
|
+
if (topLevel.length > 0)
|
|
343
|
+
return topLevel;
|
|
344
|
+
const metadata = isObjectRecord2(task.metadata) ? task.metadata : null;
|
|
345
|
+
return readStringList2(metadata?.validation);
|
|
346
|
+
}
|
|
347
|
+
function readTaskQueueWeight(task) {
|
|
348
|
+
const metadata = isObjectRecord2(task.metadata) ? task.metadata : null;
|
|
349
|
+
const queueWeight = metadata?.queueWeight ?? metadata?.queue_weight;
|
|
350
|
+
return typeof queueWeight === "number" && Number.isFinite(queueWeight) ? queueWeight : 0;
|
|
351
|
+
}
|
|
352
|
+
function scoreInputForTask(task, unblockCount) {
|
|
353
|
+
return {
|
|
354
|
+
priority: task.priority,
|
|
355
|
+
unblockCount,
|
|
356
|
+
role: readTaskRole(task),
|
|
357
|
+
criticality: readTaskCriticality(task),
|
|
358
|
+
validation: readTaskValidationKeys(task),
|
|
359
|
+
queueWeight: readTaskQueueWeight(task)
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
function rankReadyTasks(tasks, options = {}) {
|
|
363
|
+
const excluded = new Set(options.excludeTaskIds ?? []);
|
|
364
|
+
const activeTaskIds = new Set(options.activeTaskIds ?? []);
|
|
365
|
+
const badges = computeTaskDependencyBadges(tasks);
|
|
366
|
+
const tasksById = new Map(tasks.map((task) => [String(task.id), task]));
|
|
367
|
+
const openBlockCount = (task) => (badges.get(task.id)?.blocks ?? []).filter((blockedTaskId) => {
|
|
368
|
+
const blockedTask = tasksById.get(blockedTaskId);
|
|
369
|
+
return blockedTask ? !isTaskTerminalStatus(blockedTask.status) : false;
|
|
370
|
+
}).length;
|
|
371
|
+
const readyTasks = tasks.filter((task) => !excluded.has(task.id)).filter((task) => !activeTaskIds.has(task.id)).filter((task) => options.filter?.(task) ?? true).filter((task) => badges.get(task.id)?.ready === true);
|
|
372
|
+
const maxUnblockCount = Math.max(0, ...readyTasks.map(openBlockCount));
|
|
373
|
+
const selectedByMode = readyTasks.filter((task) => {
|
|
374
|
+
const unblockCount = openBlockCount(task);
|
|
375
|
+
if (options.selection === "blocking-only")
|
|
376
|
+
return unblockCount > 0;
|
|
377
|
+
if (options.selection === "max-unblock")
|
|
378
|
+
return maxUnblockCount > 0 && unblockCount === maxUnblockCount;
|
|
379
|
+
return true;
|
|
380
|
+
});
|
|
381
|
+
return rankTasks(selectedByMode, (task) => scoreInputForTask(task, openBlockCount(task)), (task) => task.id).map((entry) => ({
|
|
382
|
+
task: entry.task,
|
|
383
|
+
score: entry.score,
|
|
384
|
+
priority: entry.priority,
|
|
385
|
+
unblockCount: entry.unblockCount,
|
|
386
|
+
scope: readTaskScope(entry.task)
|
|
387
|
+
}));
|
|
388
|
+
}
|
|
389
|
+
function selectRankedReadyTasks(tasks, options = {}) {
|
|
390
|
+
const ranked = rankReadyTasks(tasks, options);
|
|
391
|
+
if (options.requireDisjointScopes !== true && !options.disjointWithScopes) {
|
|
392
|
+
return ranked.slice(0, options.limit).map((entry) => entry.task);
|
|
393
|
+
}
|
|
394
|
+
const occupiedScopes = new Set(options.disjointWithScopes ?? []);
|
|
395
|
+
const selected = [];
|
|
396
|
+
for (const entry of ranked) {
|
|
397
|
+
if (entry.scope.some((scope) => occupiedScopes.has(scope)))
|
|
398
|
+
continue;
|
|
399
|
+
selected.push(entry.task);
|
|
400
|
+
for (const scope of entry.scope)
|
|
401
|
+
occupiedScopes.add(scope);
|
|
402
|
+
if (options.limit !== undefined && selected.length >= options.limit)
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
return selected;
|
|
406
|
+
}
|
|
5
407
|
|
|
6
408
|
// packages/supervisor-plugin/src/journal.ts
|
|
7
409
|
import { Schema } from "effect";
|
|
@@ -82,8 +484,104 @@ function reduceSupervisorJournal(events) {
|
|
|
82
484
|
return { status, processed, succeeded, failed, skipped, current, plannedOrder, selectionPolicy, concurrency, idleReason, stopReason, closures, anomalies };
|
|
83
485
|
}
|
|
84
486
|
|
|
487
|
+
// packages/supervisor-plugin/src/run-status.ts
|
|
488
|
+
var KNOWN_RUN_STATUS = new Set([
|
|
489
|
+
"created",
|
|
490
|
+
"queued",
|
|
491
|
+
"preparing",
|
|
492
|
+
"running",
|
|
493
|
+
"validating",
|
|
494
|
+
"reviewing",
|
|
495
|
+
"closing-out",
|
|
496
|
+
"completed",
|
|
497
|
+
"failed",
|
|
498
|
+
"stopped"
|
|
499
|
+
]);
|
|
500
|
+
function coerceRunStatus(value, fallback) {
|
|
501
|
+
return KNOWN_RUN_STATUS.has(value) ? value : fallback;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// packages/supervisor-plugin/src/analysis/blockers.ts
|
|
505
|
+
var HUMAN_BLOCKERS = new Set(["human-decision", "human-approval", "external-input"]);
|
|
506
|
+
function labelsFor(task) {
|
|
507
|
+
return readTaskMetadataStringList(task, "labels").map((label) => label.toLowerCase());
|
|
508
|
+
}
|
|
509
|
+
function configuredTier(task, labels) {
|
|
510
|
+
const metadata = task.metadata && typeof task.metadata === "object" && !Array.isArray(task.metadata) ? task.metadata : {};
|
|
511
|
+
const tier = metadata.riskTier ?? metadata.actionRiskTier;
|
|
512
|
+
if (tier === "t1-read" || tier === "t2-reversible" || tier === "t3-external" || tier === "t4-irreversible")
|
|
513
|
+
return tier;
|
|
514
|
+
if (labels.some((label) => /prod|deploy|migration|billing|delete|irreversible/.test(label)))
|
|
515
|
+
return "t4-irreversible";
|
|
516
|
+
if (labels.some((label) => /customer|vendor|external|secret|credential|approval|decision/.test(label)))
|
|
517
|
+
return "t3-external";
|
|
518
|
+
const scopes = task.scope ?? [];
|
|
519
|
+
if (scopes.some((scope) => /docs|readme|test|spec/.test(scope.toLowerCase())))
|
|
520
|
+
return "t1-read";
|
|
521
|
+
return "t2-reversible";
|
|
522
|
+
}
|
|
523
|
+
function tierOf(task, labels = []) {
|
|
524
|
+
const projection = "metadata" in task && "id" in task && typeof task.id === "string" ? toTaskDependencyProjection(task) : task;
|
|
525
|
+
return configuredTier(projection, labels.length > 0 ? labels : labelsFor(projection)) ?? "t2-reversible";
|
|
526
|
+
}
|
|
527
|
+
function baseClassification(task, blockerClass, source, rationale, labels) {
|
|
528
|
+
return {
|
|
529
|
+
taskId: task.id,
|
|
530
|
+
blockerClass,
|
|
531
|
+
actionRiskTier: tierOf(task, labels),
|
|
532
|
+
rationale,
|
|
533
|
+
source,
|
|
534
|
+
autoApplied: source === "llm"
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
function normalizeRunStatus(status) {
|
|
538
|
+
if (status === "waiting-approval" || status === "waiting-user-input" || status === "needs-attention" || status === "completed" || status === "failed" || status === "stopped" || status === "running")
|
|
539
|
+
return status;
|
|
540
|
+
return null;
|
|
541
|
+
}
|
|
542
|
+
function isHumanBlockerClass(blockerClass) {
|
|
543
|
+
return HUMAN_BLOCKERS.has(blockerClass);
|
|
544
|
+
}
|
|
545
|
+
function classifyBlocker(input) {
|
|
546
|
+
const labels = input.labels ?? labelsFor(input.task);
|
|
547
|
+
const runStatus = normalizeRunStatus(input.run?.status);
|
|
548
|
+
if (runStatus === "waiting-approval")
|
|
549
|
+
return baseClassification(input.task, "human-approval", "status", "run awaiting approval", labels);
|
|
550
|
+
if (runStatus === "waiting-user-input")
|
|
551
|
+
return baseClassification(input.task, "external-input", "status", "run awaiting user input", labels);
|
|
552
|
+
if (input.task.status !== "blocked")
|
|
553
|
+
return baseClassification(input.task, "not-blocked", "elimination", "task is not blocked", labels);
|
|
554
|
+
const incomplete = (input.badges.get(input.task.id)?.blockedBy ?? []).filter((dependencyId) => {
|
|
555
|
+
const dependency = input.tasksById.get(dependencyId);
|
|
556
|
+
return dependency ? !isTaskTerminalStatus(dependency.status) : false;
|
|
557
|
+
});
|
|
558
|
+
if (incomplete.length > 0)
|
|
559
|
+
return baseClassification(input.task, "task-blocked", "elimination", `blocked by ${incomplete.length} incomplete task(s)`, labels);
|
|
560
|
+
if (labels.includes("needs-decision"))
|
|
561
|
+
return baseClassification(input.task, "human-decision", "label", "needs-decision label", labels);
|
|
562
|
+
if (labels.some((label) => /^waiting-on-/.test(label)))
|
|
563
|
+
return baseClassification(input.task, "external-input", "label", "waiting-on-* label", labels);
|
|
564
|
+
if (input.config?.llm)
|
|
565
|
+
return baseClassification(input.task, "human-decision", "llm", "LLM residue classifier marked this as human-gated", labels);
|
|
566
|
+
return baseClassification(input.task, "human-decision", "elimination", "blocked, all task dependencies terminal; non-task gate remains", labels);
|
|
567
|
+
}
|
|
568
|
+
function classifyTasks(tasks, runs = [], options = {}) {
|
|
569
|
+
const projected = tasks.map(toTaskDependencyProjection);
|
|
570
|
+
const badges = computeTaskDependencyBadges(projected);
|
|
571
|
+
const tasksById = new Map(projected.map((task) => [task.id, task]));
|
|
572
|
+
const runByTask = latestRunByTaskId(runs);
|
|
573
|
+
const classifier = options.classifier ?? classifyBlocker;
|
|
574
|
+
const classifications = projected.map((task) => classifier({ task, badges, tasksById, run: runByTask.get(task.id) ?? null, labels: labelsFor(task) })).filter((classification) => options.humanOnly !== true || isHumanBlockerClass(classification.blockerClass));
|
|
575
|
+
return {
|
|
576
|
+
classifications,
|
|
577
|
+
byTaskId: new Map(classifications.map((classification) => [classification.taskId, classification])),
|
|
578
|
+
human: classifications.filter((classification) => isHumanBlockerClass(classification.blockerClass)),
|
|
579
|
+
machine: classifications.filter((classification) => !isHumanBlockerClass(classification.blockerClass)),
|
|
580
|
+
generatedAt: options.generatedAt ?? new Date().toISOString()
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
|
|
85
584
|
// packages/supervisor-plugin/src/loop.ts
|
|
86
|
-
import { classifyTasks, isHumanBlockerClass } from "@rig/blocker-classifier-plugin";
|
|
87
585
|
function selectionMode(policy) {
|
|
88
586
|
return policy === "blocking-only" || policy === "max-unblock" ? policy : "all-ready";
|
|
89
587
|
}
|
|
@@ -142,25 +640,7 @@ function failedOutcome(outcome) {
|
|
|
142
640
|
return outcome.status === "failed" || outcome.status === "stopped" || outcome.status === "needs-attention" || outcome.status === "waiting-approval" || outcome.status === "waiting-user-input";
|
|
143
641
|
}
|
|
144
642
|
function runStatus(value) {
|
|
145
|
-
|
|
146
|
-
case "created":
|
|
147
|
-
case "queued":
|
|
148
|
-
case "preparing":
|
|
149
|
-
case "running":
|
|
150
|
-
case "waiting-approval":
|
|
151
|
-
case "waiting-user-input":
|
|
152
|
-
case "paused":
|
|
153
|
-
case "validating":
|
|
154
|
-
case "reviewing":
|
|
155
|
-
case "closing-out":
|
|
156
|
-
case "needs-attention":
|
|
157
|
-
case "completed":
|
|
158
|
-
case "failed":
|
|
159
|
-
case "stopped":
|
|
160
|
-
return value;
|
|
161
|
-
default:
|
|
162
|
-
return "failed";
|
|
163
|
-
}
|
|
643
|
+
return coerceRunStatus(value, "failed");
|
|
164
644
|
}
|
|
165
645
|
async function runSupervisorLoop(projectRoot, deps, options = {}) {
|
|
166
646
|
const events = [{ kind: "supervisor.started", at: at(deps), options }];
|