|
| 1 | +/** |
| 2 | + * The orchestrator task queue. |
| 3 | + * |
| 4 | + * In memory, synchronous, single-owner: one Node process drives the run, so |
| 5 | + * there is no locking. The queue imposes no execution policy — `nextRunnable` |
| 6 | + * returns every pending task whose dependencies are satisfied, and how many of |
| 7 | + * those run at once is decided by the task graph, not the queue. |
| 8 | + * |
| 9 | + * Every transition rewrites `<installDir>/.posthog-wizard/queue.json`, a small |
| 10 | + * file holding the whole queue, handoffs included. Today it is the run's |
| 11 | + * log and the report's source; later it is the resume point. |
| 12 | + */ |
| 13 | +import * as fs from 'fs'; |
| 14 | +import * as path from 'path'; |
| 15 | +import { randomUUID } from 'crypto'; |
| 16 | +import { writeJsonAtomic } from '../../../utils/atomic-ledger'; |
| 17 | + |
| 18 | +export const TaskStatus = { |
| 19 | + Pending: 'pending', |
| 20 | + Running: 'running', |
| 21 | + Done: 'done', |
| 22 | + Skipped: 'skipped', |
| 23 | + Failed: 'failed', |
| 24 | +} as const; |
| 25 | + |
| 26 | +export type TaskStatus = (typeof TaskStatus)[keyof typeof TaskStatus]; |
| 27 | + |
| 28 | +export interface QueuedTask { |
| 29 | + id: string; |
| 30 | + type: string; |
| 31 | + status: TaskStatus; |
| 32 | + dependsOn: string[]; |
| 33 | + inputs: Record<string, unknown>; |
| 34 | + model?: string; |
| 35 | + attempts: number; |
| 36 | + maxAttempts: number; |
| 37 | + /** The structured handoff the task reported on completion. */ |
| 38 | + handoff?: TaskHandoff; |
| 39 | + /** 'orchestrator' for seeded tasks, or the id of the task that enqueued this one. */ |
| 40 | + enqueuedBy: string; |
| 41 | + createdAt: string; |
| 42 | + startedAt?: string; |
| 43 | + finishedAt?: string; |
| 44 | + error?: { type: string; message: string }; |
| 45 | +} |
| 46 | + |
| 47 | +export interface QueueFile { |
| 48 | + version: 1; |
| 49 | + runId: string; |
| 50 | + tasks: QueuedTask[]; |
| 51 | +} |
| 52 | + |
| 53 | +/** The structured handoff a task leaves for the next agent. */ |
| 54 | +export interface TaskHandoff { |
| 55 | + goals: string; |
| 56 | + did: string; |
| 57 | + forNextAgent: string; |
| 58 | + filesTouched?: string[]; |
| 59 | +} |
| 60 | + |
| 61 | +export interface EnqueueInput { |
| 62 | + type: string; |
| 63 | + inputs?: Record<string, unknown>; |
| 64 | + dependsOn?: string[]; |
| 65 | + model?: string; |
| 66 | + maxAttempts?: number; |
| 67 | + enqueuedBy?: string; |
| 68 | +} |
| 69 | + |
| 70 | +export const QUEUE_DIR_NAME = '.posthog-wizard'; |
| 71 | +const DEFAULT_MAX_ATTEMPTS = 2; |
| 72 | + |
| 73 | +function nowIso(): string { |
| 74 | + return new Date().toISOString(); |
| 75 | +} |
| 76 | + |
| 77 | +export class QueueStore { |
| 78 | + private tasks: QueuedTask[] = []; |
| 79 | + |
| 80 | + readonly runId: string; |
| 81 | + readonly queuePath: string; |
| 82 | + |
| 83 | + constructor(installDir: string, runId: string) { |
| 84 | + this.runId = runId; |
| 85 | + const dir = path.join(installDir, QUEUE_DIR_NAME); |
| 86 | + this.queuePath = path.join(dir, 'queue.json'); |
| 87 | + fs.mkdirSync(dir, { recursive: true }); |
| 88 | + } |
| 89 | + |
| 90 | + // ── Reads ─────────────────────────────────────────────────────────── |
| 91 | + |
| 92 | + list(): readonly QueuedTask[] { |
| 93 | + return this.tasks; |
| 94 | + } |
| 95 | + |
| 96 | + get(id: string): QueuedTask | undefined { |
| 97 | + return this.tasks.find((t) => t.id === id); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Every pending task whose dependencies are all satisfied (`done` or |
| 102 | + * `skipped`). A skipped dependency does not block downstream work. |
| 103 | + */ |
| 104 | + nextRunnable(): QueuedTask[] { |
| 105 | + const doneIds = new Set( |
| 106 | + this.tasks |
| 107 | + .filter( |
| 108 | + (t) => |
| 109 | + t.status === TaskStatus.Done || t.status === TaskStatus.Skipped, |
| 110 | + ) |
| 111 | + .map((t) => t.id), |
| 112 | + ); |
| 113 | + return this.tasks.filter( |
| 114 | + (t) => |
| 115 | + t.status === TaskStatus.Pending && |
| 116 | + t.dependsOn.every((d) => doneIds.has(d)), |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * True when no task is in progress and none can be started. Either everything |
| 122 | + * is terminal, or the only pending tasks are blocked by a failed dependency. |
| 123 | + */ |
| 124 | + isDrained(): boolean { |
| 125 | + if (this.tasks.some((t) => t.status === TaskStatus.Running)) return false; |
| 126 | + return this.nextRunnable().length === 0; |
| 127 | + } |
| 128 | + |
| 129 | + summary(): Record<TaskStatus, number> & { total: number } { |
| 130 | + const counts: Record<TaskStatus, number> = { |
| 131 | + [TaskStatus.Pending]: 0, |
| 132 | + [TaskStatus.Running]: 0, |
| 133 | + [TaskStatus.Done]: 0, |
| 134 | + [TaskStatus.Skipped]: 0, |
| 135 | + [TaskStatus.Failed]: 0, |
| 136 | + }; |
| 137 | + for (const t of this.tasks) counts[t.status] += 1; |
| 138 | + return { ...counts, total: this.tasks.length }; |
| 139 | + } |
| 140 | + |
| 141 | + readHandoff(id: string): TaskHandoff | null { |
| 142 | + return this.get(id)?.handoff ?? null; |
| 143 | + } |
| 144 | + |
| 145 | + /** Handoffs of completed tasks of a given type, oldest first. */ |
| 146 | + readHandoffsByType(type: string): TaskHandoff[] { |
| 147 | + return this.tasks |
| 148 | + .filter((t) => t.type === type && t.handoff) |
| 149 | + .map((t) => t.handoff as TaskHandoff); |
| 150 | + } |
| 151 | + |
| 152 | + // ── Transitions (each one reflected to queue.json) ────────────────── |
| 153 | + |
| 154 | + enqueue(input: EnqueueInput): QueuedTask { |
| 155 | + const task: QueuedTask = { |
| 156 | + id: randomUUID(), |
| 157 | + type: input.type, |
| 158 | + status: TaskStatus.Pending, |
| 159 | + dependsOn: input.dependsOn ?? [], |
| 160 | + inputs: input.inputs ?? {}, |
| 161 | + model: input.model, |
| 162 | + attempts: 0, |
| 163 | + maxAttempts: input.maxAttempts ?? DEFAULT_MAX_ATTEMPTS, |
| 164 | + enqueuedBy: input.enqueuedBy ?? 'orchestrator', |
| 165 | + createdAt: nowIso(), |
| 166 | + }; |
| 167 | + this.tasks.push(task); |
| 168 | + this.reflect(); |
| 169 | + return task; |
| 170 | + } |
| 171 | + |
| 172 | + start(id: string): QueuedTask { |
| 173 | + const t = this.require(id); |
| 174 | + t.status = TaskStatus.Running; |
| 175 | + t.startedAt = nowIso(); |
| 176 | + t.attempts += 1; |
| 177 | + this.reflect(); |
| 178 | + return t; |
| 179 | + } |
| 180 | + |
| 181 | + complete(id: string, handoff?: TaskHandoff): QueuedTask { |
| 182 | + return this.finish(id, TaskStatus.Done, handoff); |
| 183 | + } |
| 184 | + |
| 185 | + /** Terminal: the agent could not do the task. Not done, not failed. */ |
| 186 | + skip(id: string, handoff?: TaskHandoff): QueuedTask { |
| 187 | + return this.finish(id, TaskStatus.Skipped, handoff); |
| 188 | + } |
| 189 | + |
| 190 | + fail( |
| 191 | + id: string, |
| 192 | + error: { type: string; message: string }, |
| 193 | + handoff?: TaskHandoff, |
| 194 | + ): QueuedTask { |
| 195 | + const t = this.require(id); |
| 196 | + t.error = error; |
| 197 | + return this.finish(id, TaskStatus.Failed, handoff); |
| 198 | + } |
| 199 | + |
| 200 | + /** Put a failed/in-progress task back to pending for a retry within the run. */ |
| 201 | + requeue(id: string): QueuedTask { |
| 202 | + const t = this.require(id); |
| 203 | + t.status = TaskStatus.Pending; |
| 204 | + t.startedAt = undefined; |
| 205 | + t.finishedAt = undefined; |
| 206 | + this.reflect(); |
| 207 | + return t; |
| 208 | + } |
| 209 | + |
| 210 | + // ── Internals ─────────────────────────────────────────────────────── |
| 211 | + |
| 212 | + private finish( |
| 213 | + id: string, |
| 214 | + status: 'done' | 'skipped' | 'failed', |
| 215 | + handoff?: TaskHandoff, |
| 216 | + ): QueuedTask { |
| 217 | + const t = this.require(id); |
| 218 | + if (handoff) t.handoff = handoff; |
| 219 | + t.status = status; |
| 220 | + t.finishedAt = nowIso(); |
| 221 | + this.reflect(); |
| 222 | + return t; |
| 223 | + } |
| 224 | + |
| 225 | + private reflect(): void { |
| 226 | + const file: QueueFile = { |
| 227 | + version: 1, |
| 228 | + runId: this.runId, |
| 229 | + tasks: this.tasks, |
| 230 | + }; |
| 231 | + writeJsonAtomic(this.queuePath, file); |
| 232 | + } |
| 233 | + |
| 234 | + private require(id: string): QueuedTask { |
| 235 | + const t = this.get(id); |
| 236 | + if (!t) throw new Error(`No task ${id} in the queue`); |
| 237 | + return t; |
| 238 | + } |
| 239 | +} |
0 commit comments