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