refactor(server): extract shared task graph utilities (#839)

* refactor(server): extract checkout status projection

* refactor(server): extract shared task graph utilities
This commit is contained in:
Mohamed Boudra
2026-05-09 21:28:40 +08:00
committed by GitHub
parent e5deb90986
commit 73f35537f9
3 changed files with 89 additions and 121 deletions
+14 -55
View File
@@ -1,13 +1,5 @@
import type { Task, TaskStore } from "./types.js";
function sortByPriorityThenCreated(a: Task, b: Task): number {
if (a.priority !== undefined && b.priority === undefined) return -1;
if (a.priority === undefined && b.priority !== undefined) return 1;
if (a.priority !== undefined && b.priority !== undefined) {
if (a.priority !== b.priority) return a.priority - b.priority;
}
return a.created.localeCompare(b.created);
}
import { buildChildrenMap, loadScopedTaskGraph, sortByPriorityThenCreated } from "./task-graph.js";
export interface ExecutionOrderResult {
/** Tasks in execution order (done first, then pending) */
@@ -30,45 +22,22 @@ export async function computeExecutionOrder(
store: TaskStore,
scopeId?: string,
): Promise<ExecutionOrderResult> {
const allTasks = await store.list();
const taskMap = new Map(allTasks.map((t) => [t.id, t]));
const graph = await loadScopedTaskGraph(store, scopeId);
// Get scoped tasks
let candidates: Task[];
if (scopeId) {
const scopeTask = await store.get(scopeId);
const descendants = await store.getDescendants(scopeId);
candidates = scopeTask ? [scopeTask, ...descendants] : descendants;
} else {
candidates = allTasks;
}
// Build children map
const childrenMap = new Map<string, Task[]>();
for (const t of allTasks) {
if (t.parentId) {
const siblings = childrenMap.get(t.parentId) ?? [];
siblings.push(t);
childrenMap.set(t.parentId, siblings);
}
}
const candidateIds = new Set(candidates.map((t) => t.id));
// Simulate execution: track done status
// Include all done tasks (not just in scope) for dep resolution
const simDone = new Set(allTasks.filter((t) => t.status === "done").map((t) => t.id));
const simDone = new Set(graph.allTasks.filter((t) => t.status === "done").map((t) => t.id));
const remaining = new Set(
candidates.filter((t) => t.status === "open" || t.status === "in_progress").map((t) => t.id),
graph.candidates
.filter((t) => t.status === "open" || t.status === "in_progress")
.map((t) => t.id),
);
const isReady = (taskId: string): boolean => {
const task = taskMap.get(taskId);
const task = graph.taskMap.get(taskId);
if (!task) return false;
// All deps done (deps can be outside scope)
const depsOk = task.deps.every((depId) => simDone.has(depId));
// All children done (only consider children in scope)
const children = (childrenMap.get(taskId) ?? []).filter((c) => candidateIds.has(c.id));
const children = (graph.childrenMap.get(taskId) ?? []).filter((c) =>
graph.candidateIds.has(c.id),
);
const childrenOk = children.every((c) => simDone.has(c.id));
return depsOk && childrenOk;
};
@@ -77,8 +46,7 @@ export async function computeExecutionOrder(
const orderMap = new Map<string, number>();
let orderIdx = 0;
// Done tasks first (by created date = historical execution order)
const done = candidates
const done = graph.candidates
.filter((t) => t.status === "done")
.sort((a, b) => a.created.localeCompare(b.created));
for (const t of done) {
@@ -90,10 +58,10 @@ export async function computeExecutionOrder(
while (remaining.size > 0) {
const readyNow = [...remaining]
.filter(isReady)
.map((tid) => taskMap.get(tid)!)
.map((tid) => graph.taskMap.get(tid)!)
.sort(sortByPriorityThenCreated);
if (readyNow.length === 0) break; // No more can be done (cycle or blocked)
if (readyNow.length === 0) break;
const next = readyNow[0];
timeline.push(next);
@@ -115,17 +83,8 @@ export function buildSortedChildrenMap(
tasks: Task[],
orderMap: Map<string, number>,
): Map<string, Task[]> {
const childrenMap = new Map<string, Task[]>();
const childrenMap = buildChildrenMap(tasks);
for (const t of tasks) {
if (t.parentId) {
const siblings = childrenMap.get(t.parentId) ?? [];
siblings.push(t);
childrenMap.set(t.parentId, siblings);
}
}
// Sort each group by execution order
for (const [parentId, children] of childrenMap) {
children.sort((a, b) => {
const orderA = orderMap.get(a.id) ?? Infinity;