@h-rig/core 0.0.6-alpha.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.
@@ -0,0 +1,374 @@
1
+ // @bun
2
+ // packages/core/src/rigSelectors.ts
3
+ function selectRunsByTask(snapshot, taskId) {
4
+ if (!taskId)
5
+ return [];
6
+ return snapshot?.runs.filter((run) => run.taskId === taskId) ?? [];
7
+ }
8
+ function selectPendingApprovals(snapshot) {
9
+ return snapshot?.approvals.filter((approval) => approval.status === "pending") ?? [];
10
+ }
11
+ function selectUserInputsForRun(snapshot, runId) {
12
+ if (!runId)
13
+ return [];
14
+ return (snapshot?.userInputs ?? []).filter((request) => request.runId === runId);
15
+ }
16
+
17
+ // packages/core/src/taskGraphCodes.ts
18
+ var TASK_CODE_RE = /^\[([A-Z0-9]+(?:-[A-Z0-9]+)*)\]\s*/;
19
+ function extractTaskCode(title) {
20
+ const match = title.match(TASK_CODE_RE);
21
+ return match?.[1] ?? null;
22
+ }
23
+ function extractTaskGroupKey(title) {
24
+ const code = extractTaskCode(title);
25
+ if (!code)
26
+ return null;
27
+ const parts = code.split("-");
28
+ const suffix = parts.at(-1) ?? "";
29
+ if (/^\d+$/.test(suffix)) {
30
+ return parts.slice(0, -1).join("-");
31
+ }
32
+ return parts[0] ?? code;
33
+ }
34
+ function stripTaskCode(label) {
35
+ return label.replace(TASK_CODE_RE, "");
36
+ }
37
+
38
+ // packages/core/src/taskGraph.ts
39
+ function isObjectRecord(value) {
40
+ return typeof value === "object" && value !== null && !Array.isArray(value);
41
+ }
42
+ function readTaskMetadataStringList(task, key) {
43
+ const metadata = isObjectRecord(task.metadata) ? task.metadata : null;
44
+ const value = metadata?.[key];
45
+ return Array.isArray(value) ? value.filter((entry) => typeof entry === "string" && entry.length > 0) : [];
46
+ }
47
+ function readTaskSourceIssueId(task) {
48
+ const metadata = isObjectRecord(task.metadata) ? task.metadata : null;
49
+ return typeof metadata?.sourceIssueId === "string" && metadata.sourceIssueId.length > 0 ? metadata.sourceIssueId : null;
50
+ }
51
+ function resolveTaskReference(ref, tasksById, taskIdByExternalRef, taskIdBySourceIssueId) {
52
+ if (tasksById.has(ref))
53
+ return ref;
54
+ return taskIdBySourceIssueId.get(ref) ?? taskIdByExternalRef.get(ref) ?? null;
55
+ }
56
+ function buildTaskReferenceIndex(tasks) {
57
+ return {
58
+ tasksById: new Map(tasks.map((task) => [task.id, task])),
59
+ taskIdByExternalRef: new Map(tasks.flatMap((task) => task.externalId ? [[task.externalId, task.id]] : [])),
60
+ taskIdBySourceIssueId: new Map(tasks.flatMap((task) => {
61
+ const sourceIssueId = readTaskSourceIssueId(task);
62
+ return sourceIssueId ? [[sourceIssueId, task.id]] : [];
63
+ }))
64
+ };
65
+ }
66
+
67
+ // packages/core/src/taskGraphLayout.ts
68
+ var CARD_WIDTH = 200;
69
+ var CARD_HEIGHT = 110;
70
+ var CELL_V_PAD = 12;
71
+ var CELL_H_PAD = 12;
72
+ var ROW_GAP = 28;
73
+ var COL_GAP = 40;
74
+ var LANE_LABEL_W = 120;
75
+ var STAGE_HDR_H = 32;
76
+ var PALETTE = [
77
+ { bg: "#3a2d12", border: "#8d6b19", edge: "#d6a11d" },
78
+ { bg: "#102642", border: "#245fbf", edge: "#66a2ff" },
79
+ { bg: "#2c173f", border: "#7b39d4", edge: "#a76df5" },
80
+ { bg: "#112d1c", border: "#2d8f4e", edge: "#62d882" },
81
+ { bg: "#3a2314", border: "#c86d1c", edge: "#e69654" },
82
+ { bg: "#31152b", border: "#bf3d88", edge: "#f07ebb" },
83
+ { bg: "#132c35", border: "#1783a6", edge: "#53c4e5" },
84
+ { bg: "#26310f", border: "#6d9a19", edge: "#a7da42" }
85
+ ];
86
+ function isObjectRecord2(value) {
87
+ return typeof value === "object" && value !== null && !Array.isArray(value);
88
+ }
89
+ function readIssueType(task) {
90
+ const metadata = isObjectRecord2(task.metadata) ? task.metadata : null;
91
+ return typeof metadata?.issueType === "string" ? metadata.issueType : null;
92
+ }
93
+ function isGraphTask(task) {
94
+ return readIssueType(task) !== "epic";
95
+ }
96
+ function findEpicAncestor(task, resolve, tasksById) {
97
+ const visited = new Set;
98
+ let current = task;
99
+ let epic = null;
100
+ while (!visited.has(current.id)) {
101
+ visited.add(current.id);
102
+ const parentRef = readTaskMetadataStringList(current, "parentChildDeps")[0];
103
+ if (!parentRef)
104
+ break;
105
+ const parentId = resolve(parentRef);
106
+ if (!parentId)
107
+ break;
108
+ const parent = tasksById.get(parentId);
109
+ if (!parent)
110
+ break;
111
+ if (readIssueType(parent) === "epic") {
112
+ epic = parent;
113
+ }
114
+ current = parent;
115
+ }
116
+ return epic;
117
+ }
118
+ function getRowKey(task, resolve, tasksById) {
119
+ const epic = findEpicAncestor(task, resolve, tasksById);
120
+ if (epic) {
121
+ return `group:${epic.id}`;
122
+ }
123
+ const codeGroup = extractTaskGroupKey(task.title);
124
+ if (codeGroup) {
125
+ return `code:${codeGroup}`;
126
+ }
127
+ return `type:${readIssueType(task) ?? "task"}`;
128
+ }
129
+ function getRowLabel(task, rowKey, resolve, tasksById) {
130
+ if (rowKey.startsWith("group:")) {
131
+ const groupId = rowKey.slice("group:".length);
132
+ return tasksById.get(groupId)?.title ?? groupId;
133
+ }
134
+ if (rowKey.startsWith("code:")) {
135
+ return rowKey.slice("code:".length);
136
+ }
137
+ const code = extractTaskCode(task.title);
138
+ if (code)
139
+ return code;
140
+ const issueType = readIssueType(task);
141
+ if (issueType === "task")
142
+ return "Tasks";
143
+ if (issueType)
144
+ return `${issueType[0]?.toUpperCase() ?? ""}${issueType.slice(1)}`;
145
+ const parentRef = readTaskMetadataStringList(task, "parentChildDeps")[0];
146
+ if (parentRef) {
147
+ const parentId = resolve(parentRef);
148
+ if (parentId) {
149
+ return tasksById.get(parentId)?.title ?? "Tasks";
150
+ }
151
+ }
152
+ return "Tasks";
153
+ }
154
+ function computeDepths(ids, edges) {
155
+ const blockers = new Map;
156
+ for (const edge of edges) {
157
+ if (!ids.has(edge.source) || !ids.has(edge.target))
158
+ continue;
159
+ const current = blockers.get(edge.target) ?? [];
160
+ current.push(edge.source);
161
+ blockers.set(edge.target, current);
162
+ }
163
+ const memo = new Map;
164
+ const visit = (id, stack) => {
165
+ const cached = memo.get(id);
166
+ if (cached !== undefined)
167
+ return cached;
168
+ if (stack.has(id))
169
+ return 0;
170
+ stack.add(id);
171
+ const deps = blockers.get(id);
172
+ if (!deps || deps.length === 0) {
173
+ memo.set(id, 0);
174
+ return 0;
175
+ }
176
+ let maxDepth = 0;
177
+ for (const dep of deps) {
178
+ maxDepth = Math.max(maxDepth, visit(dep, stack) + 1);
179
+ }
180
+ stack.delete(id);
181
+ memo.set(id, maxDepth);
182
+ return maxDepth;
183
+ };
184
+ for (const id of ids) {
185
+ visit(id, new Set);
186
+ }
187
+ return memo;
188
+ }
189
+ function buildTaskGraphLayout(snapshot, tasks, options) {
190
+ const showParentChild = options?.showParentChild ?? false;
191
+ const graphTasks = tasks.filter(isGraphTask);
192
+ if (graphTasks.length === 0) {
193
+ return {
194
+ lanes: [],
195
+ stages: [],
196
+ nodes: [],
197
+ edges: [],
198
+ totalWidth: 0,
199
+ totalHeight: 0,
200
+ taskCount: 0
201
+ };
202
+ }
203
+ const { tasksById, taskIdByExternalRef, taskIdBySourceIssueId } = buildTaskReferenceIndex(tasks);
204
+ const resolve = (ref) => resolveTaskReference(ref, tasksById, taskIdByExternalRef, taskIdBySourceIssueId);
205
+ const rows = new Map;
206
+ const rowLabelByKey = new Map;
207
+ for (const task of graphTasks) {
208
+ const rowKey = getRowKey(task, resolve, tasksById);
209
+ const current = rows.get(rowKey) ?? [];
210
+ current.push(task);
211
+ rows.set(rowKey, current);
212
+ if (!rowLabelByKey.has(rowKey)) {
213
+ rowLabelByKey.set(rowKey, getRowLabel(task, rowKey, resolve, tasksById));
214
+ }
215
+ }
216
+ const orderedRows = [...rows.entries()].sort((left, right) => {
217
+ if (left[1].length !== right[1].length)
218
+ return right[1].length - left[1].length;
219
+ return (rowLabelByKey.get(left[0]) ?? left[0]).localeCompare(rowLabelByKey.get(right[0]) ?? right[0]);
220
+ });
221
+ const rowIndexByKey = new Map(orderedRows.map(([key], index) => [key, index]));
222
+ const rowIndexByTaskId = new Map;
223
+ for (const task of graphTasks) {
224
+ rowIndexByTaskId.set(task.id, rowIndexByKey.get(getRowKey(task, resolve, tasksById)) ?? 0);
225
+ }
226
+ const blockingEdgesRaw = [];
227
+ const depsIn = new Map;
228
+ const depsOut = new Map;
229
+ const edges = [];
230
+ for (const task of graphTasks) {
231
+ for (const ref of readTaskMetadataStringList(task, "dependencies")) {
232
+ const sourceId = resolve(ref);
233
+ if (!sourceId)
234
+ continue;
235
+ blockingEdgesRaw.push({ source: sourceId, target: task.id });
236
+ depsOut.set(sourceId, (depsOut.get(sourceId) ?? 0) + 1);
237
+ depsIn.set(task.id, (depsIn.get(task.id) ?? 0) + 1);
238
+ const color = PALETTE[(rowIndexByTaskId.get(sourceId) ?? 0) % PALETTE.length].edge;
239
+ edges.push({
240
+ id: `blocking:${sourceId}:${task.id}`,
241
+ sourceId,
242
+ targetId: task.id,
243
+ color,
244
+ kind: "blocking"
245
+ });
246
+ }
247
+ if (!showParentChild)
248
+ continue;
249
+ for (const ref of readTaskMetadataStringList(task, "parentChildDeps")) {
250
+ const sourceId = resolve(ref);
251
+ if (!sourceId)
252
+ continue;
253
+ edges.push({
254
+ id: `parent:${sourceId}:${task.id}`,
255
+ sourceId,
256
+ targetId: task.id,
257
+ color: "#5b6472",
258
+ kind: "parent-child"
259
+ });
260
+ }
261
+ }
262
+ const graphTaskIds = new Set(graphTasks.map((task) => task.id));
263
+ const depths = computeDepths(graphTaskIds, blockingEdgesRaw);
264
+ const maxStage = Math.max(0, ...depths.values());
265
+ const rowMaxStack = new Array(orderedRows.length).fill(0);
266
+ const cells = new Map;
267
+ for (const task of graphTasks) {
268
+ const rowIndex = rowIndexByTaskId.get(task.id) ?? 0;
269
+ const stage = depths.get(task.id) ?? 0;
270
+ const key = `${rowIndex}:${stage}`;
271
+ const current = cells.get(key) ?? [];
272
+ current.push(task);
273
+ cells.set(key, current);
274
+ }
275
+ for (const [cellKey, cellTasks] of cells) {
276
+ const [rowIndexText] = cellKey.split(":");
277
+ const rowIndex = Number.parseInt(rowIndexText ?? "0", 10) || 0;
278
+ cellTasks.sort((left, right) => {
279
+ const leftFanout = depsOut.get(left.id) ?? 0;
280
+ const rightFanout = depsOut.get(right.id) ?? 0;
281
+ if (leftFanout !== rightFanout)
282
+ return rightFanout - leftFanout;
283
+ return left.title.localeCompare(right.title);
284
+ });
285
+ rowMaxStack[rowIndex] = Math.max(rowMaxStack[rowIndex] ?? 0, cellTasks.length);
286
+ }
287
+ const rowHeights = rowMaxStack.map((count) => Math.max(count, 1) * (CARD_HEIGHT + CELL_V_PAD) - CELL_V_PAD + CELL_V_PAD * 2);
288
+ const colWidths = new Array(maxStage + 1).fill(CARD_WIDTH + CELL_H_PAD * 2);
289
+ const colX = [];
290
+ let currentX = LANE_LABEL_W;
291
+ for (let index = 0;index <= maxStage; index += 1) {
292
+ colX.push(currentX);
293
+ currentX += (colWidths[index] ?? 0) + COL_GAP;
294
+ }
295
+ const totalWidth = currentX - COL_GAP;
296
+ const rowY = [];
297
+ let currentY = STAGE_HDR_H;
298
+ for (let rowIndex = 0;rowIndex < orderedRows.length; rowIndex += 1) {
299
+ rowY.push(currentY);
300
+ currentY += (rowHeights[rowIndex] ?? 0) + ROW_GAP;
301
+ }
302
+ const totalHeight = currentY - ROW_GAP;
303
+ const lanes = orderedRows.map(([rowKey, rowTasks], rowIndex) => ({
304
+ key: rowKey,
305
+ label: rowLabelByKey.get(rowKey) ?? rowKey,
306
+ rowIndex,
307
+ x: LANE_LABEL_W - 6,
308
+ y: (rowY[rowIndex] ?? 0) - 6,
309
+ width: totalWidth - LANE_LABEL_W + 12,
310
+ height: (rowHeights[rowIndex] ?? 0) + 12,
311
+ color: PALETTE[rowIndex % PALETTE.length].border,
312
+ taskCount: rowTasks.length
313
+ }));
314
+ const stages = Array.from({ length: maxStage + 1 }, (_, index) => ({
315
+ index,
316
+ label: index === 0 ? "Roots" : `Stage ${index}`,
317
+ x: (colX[index] ?? 0) + CELL_H_PAD,
318
+ width: CARD_WIDTH
319
+ }));
320
+ const pendingApprovalRunIds = new Set(selectPendingApprovals(snapshot).map((approval) => approval.runId));
321
+ const nodes = [];
322
+ for (const [rowIndex, [rowKey]] of orderedRows.entries()) {
323
+ for (let stage = 0;stage <= maxStage; stage += 1) {
324
+ const cellTasks = cells.get(`${rowIndex}:${stage}`) ?? [];
325
+ const baseX = (colX[stage] ?? 0) + CELL_H_PAD;
326
+ const baseY = (rowY[rowIndex] ?? 0) + CELL_V_PAD;
327
+ const palette = PALETTE[rowIndex % PALETTE.length];
328
+ for (const [stackIndex, task] of cellTasks.entries()) {
329
+ const runs = selectRunsByTask(snapshot, task.id);
330
+ const runIds = new Set(runs.map((run) => run.id));
331
+ const hasApprovals = runs.some((run) => pendingApprovalRunIds.has(run.id));
332
+ const hasPendingUserInput = runs.some((run) => selectUserInputsForRun(snapshot, run.id).some((request) => request.status === "pending"));
333
+ const hasRejectedReview = (snapshot?.reviews ?? []).some((review) => runIds.has(review.runId) && review.status === "rejected");
334
+ const hasFailedValidations = (snapshot?.validations ?? []).some((validation) => runIds.has(validation.runId) && validation.status === "failed");
335
+ const artifactCount = (snapshot?.artifacts ?? []).filter((artifact) => runIds.has(artifact.runId)).length;
336
+ nodes.push({
337
+ id: task.id,
338
+ task,
339
+ rowKey,
340
+ rowLabel: rowLabelByKey.get(rowKey) ?? rowKey,
341
+ rowIndex,
342
+ stage,
343
+ x: baseX,
344
+ y: baseY + stackIndex * (CARD_HEIGHT + CELL_V_PAD),
345
+ width: CARD_WIDTH,
346
+ height: CARD_HEIGHT,
347
+ color: palette.border,
348
+ taskCode: extractTaskCode(task.title),
349
+ strippedTitle: stripTaskCode(task.title),
350
+ depsIn: depsIn.get(task.id) ?? 0,
351
+ depsOut: depsOut.get(task.id) ?? 0,
352
+ runCount: runs.length,
353
+ hasApprovals,
354
+ hasPendingUserInput,
355
+ hasRejectedReview,
356
+ hasFailedValidations,
357
+ artifactCount
358
+ });
359
+ }
360
+ }
361
+ }
362
+ return {
363
+ lanes,
364
+ stages,
365
+ nodes,
366
+ edges,
367
+ totalWidth,
368
+ totalHeight,
369
+ taskCount: graphTasks.length
370
+ };
371
+ }
372
+ export {
373
+ buildTaskGraphLayout
374
+ };
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@h-rig/core",
3
+ "version": "0.0.6-alpha.0",
4
+ "type": "module",
5
+ "description": "Rig package",
6
+ "license": "UNLICENSED",
7
+ "files": [
8
+ "dist",
9
+ "README.md"
10
+ ],
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/src/index.js"
14
+ },
15
+ "./load-config": {
16
+ "import": "./dist/src/load-config.js"
17
+ },
18
+ "./engineReadModelReducer": {
19
+ "import": "./dist/src/engineReadModelReducer.js"
20
+ },
21
+ "./rigSelectors": {
22
+ "import": "./dist/src/rigSelectors.js"
23
+ },
24
+ "./taskGraph": {
25
+ "import": "./dist/src/taskGraph.js"
26
+ }
27
+ },
28
+ "engines": {
29
+ "bun": ">=1.3.11"
30
+ },
31
+ "main": "./dist/src/index.js",
32
+ "module": "./dist/src/index.js",
33
+ "dependencies": {
34
+ "@rig/contracts": "npm:@h-rig/contracts@0.0.6-alpha.0",
35
+ "effect": "4.0.0-beta.78"
36
+ }
37
+ }