| 69 | } |
| 70 | |
| 71 | function validateTask(raw: unknown, index: number): RawTask { |
| 72 | if (!raw || typeof raw !== "object") { |
| 73 | throw new Error(`tasks[${index}] must be an object.`); |
| 74 | } |
| 75 | const t = raw as Record<string, unknown>; |
| 76 | const id = t.id; |
| 77 | if (typeof id !== "string" || id.trim() === "") { |
| 78 | throw new Error(`tasks[${index}].id must be a non-empty string.`); |
| 79 | } |
| 80 | const depends_on = t.depends_on ?? []; |
| 81 | if (!Array.isArray(depends_on) || depends_on.some((d) => typeof d !== "string")) { |
| 82 | throw new Error(`tasks[${index}].depends_on must be an array of strings.`); |
| 83 | } |
| 84 | const complexity = t.complexity; |
| 85 | if (typeof complexity !== "string" || !COMPLEXITY_VALUES.has(complexity as Complexity)) { |
| 86 | throw new Error(`tasks[${index}].complexity must be one of HIGH | MED | LOW.`); |
| 87 | } |
| 88 | const subtask_prompt = t.subtask_prompt; |
| 89 | if (typeof subtask_prompt !== "string" || subtask_prompt.trim() === "") { |
| 90 | throw new Error(`tasks[${index}].subtask_prompt must be a non-empty string.`); |
| 91 | } |
| 92 | return { |
| 93 | id, |
| 94 | depends_on: [...new Set(depends_on as string[])], |
| 95 | complexity: complexity as Complexity, |
| 96 | subtask_prompt, |
| 97 | }; |
| 98 | } |
| 99 | |
| 100 | /** Throws on the first cycle found. Uses iterative DFS with a recursion stack. */ |
| 101 | function detectCycle(tasks: RawTask[]): void { |