@h-rig/supervisor-plugin 0.0.6-alpha.157 → 0.0.6-alpha.158

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.
@@ -0,0 +1,374 @@
1
+ // @bun
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
+
223
+ // packages/supervisor-plugin/src/analysis/taskScore.ts
224
+ var ROLE_WEIGHT = {
225
+ architect: 25,
226
+ extractor: 10
227
+ };
228
+ var CRITICALITY_WEIGHT = {
229
+ core: 20,
230
+ high: 10,
231
+ normal: 0
232
+ };
233
+ function finiteNumber(value, fallback) {
234
+ return typeof value === "number" && Number.isFinite(value) ? value : fallback;
235
+ }
236
+ function scoreTask(input) {
237
+ const priority = finiteNumber(input.priority, 3);
238
+ const unblockCount = Math.max(0, finiteNumber(input.unblockCount, 0));
239
+ const roleWeight = input.role ? ROLE_WEIGHT[input.role] ?? 0 : 0;
240
+ const criticalityWeight = input.criticality ? CRITICALITY_WEIGHT[input.criticality] ?? 0 : 0;
241
+ const validationWeight = (input.validation ?? []).some((entry) => entry.includes("test-contract") || entry.includes("test-boundary")) ? 8 : 0;
242
+ const queueWeight = finiteNumber(input.queueWeight, 0);
243
+ return unblockCount * 100 + (10 - priority) + roleWeight + criticalityWeight + validationWeight + queueWeight;
244
+ }
245
+ function rankTasks(tasks, scoreInput, idOf) {
246
+ return tasks.map((task) => {
247
+ const input = scoreInput(task);
248
+ return {
249
+ task,
250
+ score: scoreTask(input),
251
+ priority: finiteNumber(input.priority, 3),
252
+ unblockCount: Math.max(0, finiteNumber(input.unblockCount, 0))
253
+ };
254
+ }).toSorted((left, right) => {
255
+ const scoreDelta = right.score - left.score;
256
+ if (scoreDelta !== 0)
257
+ return scoreDelta;
258
+ const unblockDelta = right.unblockCount - left.unblockCount;
259
+ if (unblockDelta !== 0)
260
+ return unblockDelta;
261
+ const priorityDelta = left.priority - right.priority;
262
+ if (priorityDelta !== 0)
263
+ return priorityDelta;
264
+ return idOf(left.task).localeCompare(idOf(right.task));
265
+ });
266
+ }
267
+
268
+ // packages/supervisor-plugin/src/analysis/taskRanking.ts
269
+ function isObjectRecord2(value) {
270
+ return typeof value === "object" && value !== null && !Array.isArray(value);
271
+ }
272
+ function readStringList2(value) {
273
+ return Array.isArray(value) ? value.filter((entry) => typeof entry === "string" && entry.length > 0) : [];
274
+ }
275
+ function priorityValue(task) {
276
+ return typeof task.priority === "number" && Number.isFinite(task.priority) ? task.priority : Number.MAX_SAFE_INTEGER;
277
+ }
278
+ function readTaskRole(task) {
279
+ if (typeof task.role === "string" && task.role.trim())
280
+ return task.role.trim();
281
+ const metadata = isObjectRecord2(task.metadata) ? task.metadata : null;
282
+ return typeof metadata?.role === "string" && metadata.role.trim() ? metadata.role.trim() : null;
283
+ }
284
+ function readTaskCriticality(task) {
285
+ const metadata = isObjectRecord2(task.metadata) ? task.metadata : null;
286
+ return typeof metadata?.criticality === "string" && metadata.criticality.trim() ? metadata.criticality.trim() : null;
287
+ }
288
+ function readTaskValidationKeys(task) {
289
+ const taskRecord = task;
290
+ const topLevel = readStringList2(taskRecord.validationKeys);
291
+ if (topLevel.length > 0)
292
+ return topLevel;
293
+ const metadata = isObjectRecord2(task.metadata) ? task.metadata : null;
294
+ return readStringList2(metadata?.validation);
295
+ }
296
+ function readTaskQueueWeight(task) {
297
+ const metadata = isObjectRecord2(task.metadata) ? task.metadata : null;
298
+ const queueWeight = metadata?.queueWeight ?? metadata?.queue_weight;
299
+ return typeof queueWeight === "number" && Number.isFinite(queueWeight) ? queueWeight : 0;
300
+ }
301
+ function scoreInputForTask(task, unblockCount) {
302
+ return {
303
+ priority: task.priority,
304
+ unblockCount,
305
+ role: readTaskRole(task),
306
+ criticality: readTaskCriticality(task),
307
+ validation: readTaskValidationKeys(task),
308
+ queueWeight: readTaskQueueWeight(task)
309
+ };
310
+ }
311
+ function selectNextReadyTaskByPriority(tasks, options = {}) {
312
+ const excluded = new Set(options.excludeTaskIds ?? []);
313
+ const badges = computeTaskDependencyBadges(tasks);
314
+ const candidates = tasks.filter((task) => !excluded.has(task.id)).filter((task) => options.filter?.(task) ?? true).filter((task) => badges.get(task.id)?.ready === true).toSorted((left, right) => {
315
+ const priorityDelta = priorityValue(left) - priorityValue(right);
316
+ if (priorityDelta !== 0)
317
+ return priorityDelta;
318
+ const createdDelta = (left.createdAt ?? "").localeCompare(right.createdAt ?? "");
319
+ if (createdDelta !== 0)
320
+ return createdDelta;
321
+ return left.id.localeCompare(right.id);
322
+ });
323
+ return candidates[0] ?? null;
324
+ }
325
+ function rankReadyTasks(tasks, options = {}) {
326
+ const excluded = new Set(options.excludeTaskIds ?? []);
327
+ const activeTaskIds = new Set(options.activeTaskIds ?? []);
328
+ const badges = computeTaskDependencyBadges(tasks);
329
+ const tasksById = new Map(tasks.map((task) => [String(task.id), task]));
330
+ const openBlockCount = (task) => (badges.get(task.id)?.blocks ?? []).filter((blockedTaskId) => {
331
+ const blockedTask = tasksById.get(blockedTaskId);
332
+ return blockedTask ? !isTaskTerminalStatus(blockedTask.status) : false;
333
+ }).length;
334
+ 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);
335
+ const maxUnblockCount = Math.max(0, ...readyTasks.map(openBlockCount));
336
+ const selectedByMode = readyTasks.filter((task) => {
337
+ const unblockCount = openBlockCount(task);
338
+ if (options.selection === "blocking-only")
339
+ return unblockCount > 0;
340
+ if (options.selection === "max-unblock")
341
+ return maxUnblockCount > 0 && unblockCount === maxUnblockCount;
342
+ return true;
343
+ });
344
+ return rankTasks(selectedByMode, (task) => scoreInputForTask(task, openBlockCount(task)), (task) => task.id).map((entry) => ({
345
+ task: entry.task,
346
+ score: entry.score,
347
+ priority: entry.priority,
348
+ unblockCount: entry.unblockCount,
349
+ scope: readTaskScope(entry.task)
350
+ }));
351
+ }
352
+ function selectRankedReadyTasks(tasks, options = {}) {
353
+ const ranked = rankReadyTasks(tasks, options);
354
+ if (options.requireDisjointScopes !== true && !options.disjointWithScopes) {
355
+ return ranked.slice(0, options.limit).map((entry) => entry.task);
356
+ }
357
+ const occupiedScopes = new Set(options.disjointWithScopes ?? []);
358
+ const selected = [];
359
+ for (const entry of ranked) {
360
+ if (entry.scope.some((scope) => occupiedScopes.has(scope)))
361
+ continue;
362
+ selected.push(entry.task);
363
+ for (const scope of entry.scope)
364
+ occupiedScopes.add(scope);
365
+ if (options.limit !== undefined && selected.length >= options.limit)
366
+ break;
367
+ }
368
+ return selected;
369
+ }
370
+ export {
371
+ selectRankedReadyTasks,
372
+ selectNextReadyTaskByPriority,
373
+ rankReadyTasks
374
+ };
@@ -0,0 +1,17 @@
1
+ export type TaskCriticality = "core" | "high" | "normal" | string;
2
+ export interface TaskScoreInput {
3
+ readonly priority?: number | null;
4
+ readonly unblockCount?: number | null;
5
+ readonly role?: string | null;
6
+ readonly criticality?: TaskCriticality | null;
7
+ readonly validation?: readonly string[] | null;
8
+ readonly queueWeight?: number | null;
9
+ }
10
+ export interface RankedTask<T> {
11
+ readonly task: T;
12
+ readonly score: number;
13
+ readonly priority: number;
14
+ readonly unblockCount: number;
15
+ }
16
+ export declare function scoreTask(input: TaskScoreInput): number;
17
+ export declare function rankTasks<T>(tasks: readonly T[], scoreInput: (task: T) => TaskScoreInput, idOf: (task: T) => string): readonly RankedTask<T>[];
@@ -0,0 +1,49 @@
1
+ // @bun
2
+ // packages/supervisor-plugin/src/analysis/taskScore.ts
3
+ var ROLE_WEIGHT = {
4
+ architect: 25,
5
+ extractor: 10
6
+ };
7
+ var CRITICALITY_WEIGHT = {
8
+ core: 20,
9
+ high: 10,
10
+ normal: 0
11
+ };
12
+ function finiteNumber(value, fallback) {
13
+ return typeof value === "number" && Number.isFinite(value) ? value : fallback;
14
+ }
15
+ function scoreTask(input) {
16
+ const priority = finiteNumber(input.priority, 3);
17
+ const unblockCount = Math.max(0, finiteNumber(input.unblockCount, 0));
18
+ const roleWeight = input.role ? ROLE_WEIGHT[input.role] ?? 0 : 0;
19
+ const criticalityWeight = input.criticality ? CRITICALITY_WEIGHT[input.criticality] ?? 0 : 0;
20
+ const validationWeight = (input.validation ?? []).some((entry) => entry.includes("test-contract") || entry.includes("test-boundary")) ? 8 : 0;
21
+ const queueWeight = finiteNumber(input.queueWeight, 0);
22
+ return unblockCount * 100 + (10 - priority) + roleWeight + criticalityWeight + validationWeight + queueWeight;
23
+ }
24
+ function rankTasks(tasks, scoreInput, idOf) {
25
+ return tasks.map((task) => {
26
+ const input = scoreInput(task);
27
+ return {
28
+ task,
29
+ score: scoreTask(input),
30
+ priority: finiteNumber(input.priority, 3),
31
+ unblockCount: Math.max(0, finiteNumber(input.unblockCount, 0))
32
+ };
33
+ }).toSorted((left, right) => {
34
+ const scoreDelta = right.score - left.score;
35
+ if (scoreDelta !== 0)
36
+ return scoreDelta;
37
+ const unblockDelta = right.unblockCount - left.unblockCount;
38
+ if (unblockDelta !== 0)
39
+ return unblockDelta;
40
+ const priorityDelta = left.priority - right.priority;
41
+ if (priorityDelta !== 0)
42
+ return priorityDelta;
43
+ return idOf(left.task).localeCompare(idOf(right.task));
44
+ });
45
+ }
46
+ export {
47
+ scoreTask,
48
+ rankTasks
49
+ };
package/dist/src/cli.d.ts CHANGED
@@ -1,12 +1,7 @@
1
+ import { type CommandOutcome } from "@rig/contracts";
1
2
  import type { RuntimeCliContext } from "@rig/core";
2
3
  export declare const SUPERVISOR_LOOP_CLI_ID = "supervisor.loop";
3
4
  export declare const SUPERVISOR_UNBLOCK_CLI_ID = "supervisor.unblock";
4
- type CommandOutcome = {
5
- readonly ok: boolean;
6
- readonly group: string;
7
- readonly command: string;
8
- readonly details?: Record<string, unknown>;
9
- };
10
5
  export declare function executeLoop(context: RuntimeCliContext, args: readonly string[]): Promise<CommandOutcome>;
11
6
  export declare function executeUnblock(context: RuntimeCliContext, args: readonly string[]): Promise<CommandOutcome>;
12
7
  export declare const supervisorCliCommands: readonly [{
@@ -26,4 +21,3 @@ export declare const supervisorCliCommands: readonly [{
26
21
  readonly projectRequired: true;
27
22
  readonly run: typeof executeUnblock;
28
23
  }];
29
- export {};