@tagma/sdk 0.1.2

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.
package/src/engine.ts ADDED
@@ -0,0 +1,598 @@
1
+ import { resolve, dirname } from 'path';
2
+ import { mkdir } from 'fs/promises';
3
+ import type {
4
+ PipelineConfig, TaskConfig, TrackConfig, TaskState, TaskStatus,
5
+ TaskResult, DriverPlugin, TriggerPlugin, CompletionPlugin,
6
+ MiddlewarePlugin, MiddlewareContext, DriverContext,
7
+ OnFailure,
8
+ } from './types';
9
+ import { buildDag, type Dag, type DagNode } from './dag';
10
+ import { getHandler, hasHandler } from './registry';
11
+ import { runSpawn, runCommand } from './runner';
12
+ import { parseDuration, nowISO, generateRunId } from './utils';
13
+ import {
14
+ executeHook,
15
+ buildPipelineStartContext, buildTaskContext,
16
+ buildPipelineCompleteContext, buildPipelineErrorContext,
17
+ type PipelineInfo, type TrackInfo, type TaskInfo,
18
+ } from './hooks';
19
+ import { Logger, tailLines, clip } from './logger';
20
+ import { InMemoryApprovalGateway, type ApprovalGateway } from './approval';
21
+
22
+ // ═══ Preflight Validation ═══
23
+
24
+ function preflight(config: PipelineConfig, dag: Dag): void {
25
+ const errors: string[] = [];
26
+
27
+ for (const [, node] of dag.nodes) {
28
+ const task = node.task;
29
+ const track = node.track;
30
+ const driverName = task.driver ?? track.driver ?? config.driver ?? 'claude-code';
31
+
32
+ if (!hasHandler('drivers', driverName)) {
33
+ errors.push(`Task "${node.taskId}": driver "${driverName}" not registered`);
34
+ }
35
+
36
+ if (task.trigger && !hasHandler('triggers', task.trigger.type)) {
37
+ errors.push(`Task "${node.taskId}": trigger type "${task.trigger.type}" not registered`);
38
+ }
39
+
40
+ if (task.completion && !hasHandler('completions', task.completion.type)) {
41
+ errors.push(`Task "${node.taskId}": completion type "${task.completion.type}" not registered`);
42
+ }
43
+
44
+ const mws = task.middlewares ?? track.middlewares ?? [];
45
+ for (const mw of mws) {
46
+ if (!hasHandler('middlewares', mw.type)) {
47
+ errors.push(`Task "${node.taskId}": middleware type "${mw.type}" not registered`);
48
+ }
49
+ }
50
+
51
+ if (task.continue_from && hasHandler('drivers', driverName)) {
52
+ const driver = getHandler<DriverPlugin>('drivers', driverName);
53
+ if (!driver.capabilities.sessionResume) {
54
+ const upstreamId = resolveRefInDag(dag, task.continue_from, track.id);
55
+ if (upstreamId) {
56
+ const upstream = dag.nodes.get(upstreamId);
57
+ if (upstream && !upstream.task.output) {
58
+ errors.push(
59
+ `Task "${node.taskId}" uses continue_from: "${task.continue_from}", ` +
60
+ `but upstream task "${upstreamId}" has no "output" field. ` +
61
+ `Add output to the upstream task, or remove continue_from.`
62
+ );
63
+ }
64
+ }
65
+ }
66
+ }
67
+ }
68
+
69
+ if (errors.length > 0) {
70
+ throw new Error(`Preflight validation failed:\n - ${errors.join('\n - ')}`);
71
+ }
72
+ }
73
+
74
+ function resolveRefInDag(dag: Dag, ref: string, fromTrackId: string): string | null {
75
+ if (dag.nodes.has(ref)) return ref;
76
+ const sameTrack = `${fromTrackId}.${ref}`;
77
+ if (dag.nodes.has(sameTrack)) return sameTrack;
78
+ for (const [id] of dag.nodes) {
79
+ if (id.endsWith(`.${ref}`)) return id;
80
+ }
81
+ return null;
82
+ }
83
+
84
+ // ═══ Engine ═══
85
+
86
+ export interface EngineResult {
87
+ readonly success: boolean;
88
+ readonly summary: {
89
+ total: number; success: number; failed: number;
90
+ skipped: number; timeout: number; blocked: number;
91
+ };
92
+ readonly states: ReadonlyMap<string, TaskState>;
93
+ }
94
+
95
+ export interface RunPipelineOptions {
96
+ readonly approvalGateway?: ApprovalGateway;
97
+ }
98
+
99
+ export async function runPipeline(
100
+ config: PipelineConfig,
101
+ workDir: string,
102
+ options: RunPipelineOptions = {},
103
+ ): Promise<EngineResult> {
104
+ const approvalGateway = options.approvalGateway ?? new InMemoryApprovalGateway();
105
+ const dag = buildDag(config);
106
+ preflight(config, dag);
107
+
108
+ const runId = generateRunId();
109
+ const startedAt = nowISO();
110
+ const pipelineInfo: PipelineInfo = { name: config.name, run_id: runId, started_at: startedAt };
111
+ const log = new Logger(workDir, runId);
112
+ log.info('[pipeline]', `start "${config.name}" run_id=${runId}`);
113
+
114
+ // File-only: dump the resolved pipeline shape + DAG topology for post-mortem.
115
+ log.section('Pipeline configuration');
116
+ log.quiet(`name: ${config.name}`);
117
+ log.quiet(`driver: ${config.driver ?? '(default: claude-code)'}`);
118
+ log.quiet(`timeout: ${config.timeout ?? '(none)'}`);
119
+ log.quiet(`tracks: ${config.tracks.length}`);
120
+ log.quiet(`tasks (total): ${dag.nodes.size}`);
121
+ log.quiet(`plugins: ${(config.plugins ?? []).join(', ') || '(none)'}`);
122
+ log.quiet(`hooks: ${config.hooks ? Object.keys(config.hooks).join(', ') || '(none)' : '(none)'}`);
123
+
124
+ log.section('DAG topology');
125
+ for (const [id, node] of dag.nodes) {
126
+ const deps = node.dependsOn.length ? node.dependsOn.join(', ') : '(root)';
127
+ const kind = node.task.prompt ? 'ai' : 'cmd';
128
+ log.quiet(` • ${id} [${kind}] track=${node.track.id} deps=[${deps}]`);
129
+ }
130
+ log.quiet('');
131
+
132
+ // Initialize states (before hook, so we can return them even if blocked)
133
+ const states = new Map<string, TaskState>();
134
+ for (const [id, node] of dag.nodes) {
135
+ states.set(id, {
136
+ config: node.task,
137
+ trackConfig: node.track,
138
+ status: 'idle',
139
+ result: null,
140
+ startedAt: null,
141
+ finishedAt: null,
142
+ });
143
+ }
144
+
145
+ // Pipeline start hook (gate)
146
+ const startHook = await executeHook(
147
+ config.hooks, 'pipeline_start', buildPipelineStartContext(pipelineInfo), workDir,
148
+ );
149
+ if (!startHook.allowed) {
150
+ console.error(`Pipeline blocked by pipeline_start hook (exit code ${startHook.exitCode})`);
151
+ await executeHook(config.hooks, 'pipeline_error',
152
+ buildPipelineErrorContext(pipelineInfo, 'pipeline_blocked', 'pipeline_blocked'), workDir);
153
+ // All tasks stay idle — pipeline never started
154
+ return {
155
+ success: false,
156
+ summary: { total: dag.nodes.size, success: 0, failed: 0, skipped: 0, timeout: 0, blocked: 0 },
157
+ states,
158
+ };
159
+ }
160
+
161
+ // Pipeline approved — transition all tasks to waiting
162
+ for (const [, state] of states) {
163
+ state.status = 'waiting';
164
+ }
165
+
166
+ const sessionMap = new Map<string, string>();
167
+ const outputMap = new Map<string, string>();
168
+ const normalizedMap = new Map<string, string>();
169
+
170
+ // Pipeline timeout
171
+ const pipelineTimeoutMs = config.timeout ? parseDuration(config.timeout) : 0;
172
+ let pipelineAborted = false;
173
+ const abortController = new AbortController();
174
+ let pipelineTimer: ReturnType<typeof setTimeout> | null = null;
175
+
176
+ if (pipelineTimeoutMs > 0) {
177
+ pipelineTimer = setTimeout(() => {
178
+ pipelineAborted = true;
179
+ abortController.abort();
180
+ }, pipelineTimeoutMs);
181
+ }
182
+
183
+ // When the pipeline is aborted (timeout, external shutdown), drain all
184
+ // pending approvals so waiting triggers unblock immediately.
185
+ abortController.signal.addEventListener('abort', () => {
186
+ approvalGateway.abortAll('pipeline aborted');
187
+ });
188
+
189
+ // ── Helpers ──
190
+
191
+ function getOnFailure(taskId: string): OnFailure {
192
+ return dag.nodes.get(taskId)?.track.on_failure ?? 'skip_downstream';
193
+ }
194
+
195
+ function isDependencySatisfied(depId: string): 'satisfied' | 'unsatisfied' | 'skip' {
196
+ const depState = states.get(depId);
197
+ if (!depState) return 'skip';
198
+ switch (depState.status) {
199
+ case 'success': return 'satisfied';
200
+ case 'skipped': return 'skip';
201
+ case 'failed': case 'timeout': case 'blocked':
202
+ return getOnFailure(depId) === 'ignore' ? 'satisfied' : 'skip';
203
+ default: return 'unsatisfied';
204
+ }
205
+ }
206
+
207
+ function applyStopAll(trackId: string): void {
208
+ for (const [, state] of states) {
209
+ const node = dag.nodes.get(state.config.id);
210
+ if (state.trackConfig.id === trackId && !isTerminal(state.status)) {
211
+ state.status = 'skipped';
212
+ state.finishedAt = nowISO();
213
+ }
214
+ }
215
+ }
216
+
217
+ function buildTaskInfoObj(taskId: string): TaskInfo {
218
+ const state = states.get(taskId)!;
219
+ return {
220
+ id: taskId,
221
+ name: state.config.name,
222
+ type: state.config.prompt ? 'ai' : 'command',
223
+ status: state.status,
224
+ exit_code: state.result?.exitCode ?? null,
225
+ duration_ms: state.result?.durationMs ?? null,
226
+ output_path: state.result?.outputPath ?? null,
227
+ stderr_path: state.result?.stderrPath ?? null,
228
+ session_id: state.result?.sessionId ?? null,
229
+ started_at: state.startedAt,
230
+ finished_at: state.finishedAt,
231
+ };
232
+ }
233
+
234
+ function trackInfoOf(taskId: string): TrackInfo {
235
+ const node = dag.nodes.get(taskId)!;
236
+ return { id: node.track.id, name: node.track.name };
237
+ }
238
+
239
+ async function fireHook(taskId: string, event: 'task_success' | 'task_failure'): Promise<void> {
240
+ await executeHook(config.hooks, event,
241
+ buildTaskContext(event, pipelineInfo, trackInfoOf(taskId), buildTaskInfoObj(taskId)), workDir);
242
+ }
243
+
244
+ // ── Process a single task ──
245
+
246
+ async function processTask(taskId: string): Promise<void> {
247
+ const state = states.get(taskId)!;
248
+ const node = dag.nodes.get(taskId)!;
249
+ const task = node.task;
250
+ const track = node.track;
251
+
252
+ log.section(`Task ${taskId}`);
253
+ log.debug(`[task:${taskId}]`,
254
+ `type=${task.prompt ? 'ai' : 'cmd'} track=${track.id} deps=[${node.dependsOn.join(', ') || '(root)'}]`);
255
+
256
+ // 1. Check dependencies
257
+ for (const depId of node.dependsOn) {
258
+ const result = isDependencySatisfied(depId);
259
+ if (result === 'skip') {
260
+ const depStatus = states.get(depId)?.status ?? 'unknown';
261
+ log.debug(`[task:${taskId}]`, `skipped (upstream "${depId}" status=${depStatus})`);
262
+ state.status = 'skipped';
263
+ state.finishedAt = nowISO();
264
+ return;
265
+ }
266
+ if (result === 'unsatisfied') return; // still waiting
267
+ }
268
+
269
+ // 2. Check trigger
270
+ if (task.trigger) {
271
+ log.debug(`[task:${taskId}]`, `trigger wait: type=${task.trigger.type} ${JSON.stringify(task.trigger)}`);
272
+ try {
273
+ const triggerPlugin = getHandler<TriggerPlugin>('triggers', task.trigger.type);
274
+ await triggerPlugin.watch(task.trigger as Record<string, unknown>, {
275
+ taskId: node.taskId,
276
+ trackId: track.id,
277
+ workDir: task.cwd ?? workDir,
278
+ signal: abortController.signal,
279
+ approvalGateway,
280
+ });
281
+ log.debug(`[task:${taskId}]`, `trigger fired`);
282
+ } catch (err: unknown) {
283
+ const msg = err instanceof Error ? err.message : String(err);
284
+ // If pipeline was aborted while we were still waiting for the trigger,
285
+ // this task never entered running state → skipped, not timeout.
286
+ if (pipelineAborted) {
287
+ state.status = 'skipped';
288
+ } else if (msg.includes('rejected') || msg.includes('denied')) {
289
+ state.status = 'blocked'; // user/policy rejection
290
+ } else if (msg.includes('timeout')) {
291
+ state.status = 'timeout'; // genuine trigger wait timeout
292
+ } else {
293
+ state.status = 'failed'; // plugin error, watcher crash, etc.
294
+ }
295
+ state.finishedAt = nowISO();
296
+ await fireHook(taskId, 'task_failure');
297
+ return;
298
+ }
299
+ }
300
+
301
+ // 3. task_start hook (gate)
302
+ const hookResult = await executeHook(config.hooks, 'task_start',
303
+ buildTaskContext('task_start', pipelineInfo, trackInfoOf(taskId), buildTaskInfoObj(taskId)), workDir);
304
+ if (hookResult.exitCode !== 0 || config.hooks?.task_start) {
305
+ log.debug(`[task:${taskId}]`,
306
+ `task_start hook exit=${hookResult.exitCode} allowed=${hookResult.allowed}`);
307
+ }
308
+ if (!hookResult.allowed) {
309
+ state.status = 'blocked';
310
+ state.finishedAt = nowISO();
311
+ await fireHook(taskId, 'task_failure');
312
+ return;
313
+ }
314
+
315
+ // 4. Mark running
316
+ state.status = 'running';
317
+ state.startedAt = nowISO();
318
+ log.info(`[task:${taskId}]`, task.command ? `running: ${task.command}` : `running (driver task)`);
319
+
320
+ // File-only: resolved config for this task
321
+ const resolvedDriver = task.driver ?? track.driver ?? config.driver ?? 'claude-code';
322
+ const resolvedTier = task.model_tier ?? track.model_tier ?? '(default)';
323
+ const resolvedPerms = task.permissions ?? track.permissions ?? '(default)';
324
+ const resolvedCwd = task.cwd ?? track.cwd ?? workDir;
325
+ log.debug(`[task:${taskId}]`,
326
+ `resolved: driver=${resolvedDriver} tier=${resolvedTier} cwd=${resolvedCwd}`);
327
+ log.debug(`[task:${taskId}]`, `permissions: ${JSON.stringify(resolvedPerms)}`);
328
+ if (task.continue_from) {
329
+ log.debug(`[task:${taskId}]`, `continue_from: "${task.continue_from}"`);
330
+ }
331
+ if (task.timeout) {
332
+ log.debug(`[task:${taskId}]`, `timeout: ${task.timeout}`);
333
+ }
334
+
335
+ try {
336
+ let result: TaskResult;
337
+ const timeoutMs = task.timeout ? parseDuration(task.timeout) : undefined;
338
+
339
+ const runOpts = { timeoutMs, signal: abortController.signal };
340
+
341
+ if (task.command) {
342
+ log.debug(`[task:${taskId}]`, `command: ${task.command}`);
343
+ result = await runCommand(task.command, task.cwd ?? workDir, runOpts);
344
+ } else {
345
+ // AI task: apply middleware chain
346
+ const driverName = task.driver ?? track.driver ?? config.driver ?? 'claude-code';
347
+ const driver = getHandler<DriverPlugin>('drivers', driverName);
348
+
349
+ let prompt = task.prompt!;
350
+ const originalLen = prompt.length;
351
+ const mws = task.middlewares !== undefined ? task.middlewares : track.middlewares;
352
+ if (mws && mws.length > 0) {
353
+ log.debug(`[task:${taskId}]`,
354
+ `middleware chain: ${mws.map(m => m.type).join(' → ')}`);
355
+ const mwCtx: MiddlewareContext = {
356
+ task, track, outputMap, workDir: task.cwd ?? workDir,
357
+ };
358
+ for (const mwConfig of mws) {
359
+ const before = prompt.length;
360
+ const mwPlugin = getHandler<MiddlewarePlugin>('middlewares', mwConfig.type);
361
+ prompt = await mwPlugin.enhance(prompt, mwConfig as Record<string, unknown>, mwCtx);
362
+ log.debug(`[task:${taskId}]`,
363
+ ` ${mwConfig.type}: ${before} → ${prompt.length} chars`);
364
+ }
365
+ }
366
+ log.debug(`[task:${taskId}]`,
367
+ `prompt: ${originalLen} chars (final: ${prompt.length} chars)`);
368
+ log.quiet(`--- prompt (final) ---\n${clip(prompt)}\n--- end prompt ---`);
369
+
370
+ const enrichedTask: TaskConfig = { ...task, prompt };
371
+ const driverCtx: DriverContext = {
372
+ sessionMap, outputMap, normalizedMap, workDir: task.cwd ?? workDir,
373
+ };
374
+ const spec = await driver.buildCommand(enrichedTask, track, driverCtx);
375
+ log.debug(`[task:${taskId}]`, `driver=${driverName}`);
376
+ log.debug(`[task:${taskId}]`,
377
+ `spawn args: ${JSON.stringify(spec.args)}`);
378
+ if (spec.cwd) log.debug(`[task:${taskId}]`, `spawn cwd: ${spec.cwd}`);
379
+ if (spec.env) log.debug(`[task:${taskId}]`,
380
+ `spawn env overrides: ${Object.keys(spec.env).join(', ')}`);
381
+ if (spec.stdin) log.debug(`[task:${taskId}]`,
382
+ `spawn stdin: ${spec.stdin.length} chars`);
383
+ result = await runSpawn(spec, driver, runOpts);
384
+ }
385
+
386
+ // 5. Determine status
387
+ if (result.exitCode === -1) {
388
+ state.status = 'timeout';
389
+ } else if (result.exitCode !== 0) {
390
+ state.status = 'failed';
391
+ } else if (task.completion) {
392
+ const plugin = getHandler<CompletionPlugin>('completions', task.completion.type);
393
+ const completionCtx = { workDir: task.cwd ?? workDir };
394
+ const passed = await plugin.check(task.completion as Record<string, unknown>, result, completionCtx);
395
+ state.status = passed ? 'success' : 'failed';
396
+ } else {
397
+ state.status = 'success';
398
+ }
399
+
400
+ // 6. Write output file with RAW stdout (preserves driver output format).
401
+ // The separate normalizedMap holds canonical text for continue_from.
402
+ if (task.output) {
403
+ const outPath = resolve(workDir, task.output);
404
+ await mkdir(dirname(outPath), { recursive: true });
405
+ await Bun.write(outPath, result.stdout);
406
+ result = { ...result, outputPath: outPath };
407
+ outputMap.set(taskId, outPath);
408
+ const bareId = taskId.includes('.') ? taskId.split('.').pop()! : taskId;
409
+ if (!outputMap.has(bareId)) outputMap.set(bareId, outPath);
410
+ }
411
+
412
+ // Store normalized text separately (in-memory) for continue_from handoff
413
+ if (result.normalizedOutput !== null) {
414
+ normalizedMap.set(taskId, result.normalizedOutput);
415
+ const bareId = taskId.includes('.') ? taskId.split('.').pop()! : taskId;
416
+ if (!normalizedMap.has(bareId)) normalizedMap.set(bareId, result.normalizedOutput);
417
+ }
418
+
419
+ if (result.stderr) {
420
+ const stderrDir = resolve(workDir, './tmp', runId);
421
+ await mkdir(stderrDir, { recursive: true });
422
+ const stderrPath = resolve(stderrDir, `${taskId.replace(/\./g, '_')}.stderr`);
423
+ await Bun.write(stderrPath, result.stderr);
424
+ result = { ...result, stderrPath };
425
+ }
426
+
427
+ if (result.sessionId) {
428
+ sessionMap.set(taskId, result.sessionId);
429
+ const bareId = taskId.includes('.') ? taskId.split('.').pop()! : taskId;
430
+ if (!sessionMap.has(bareId)) sessionMap.set(bareId, result.sessionId);
431
+ }
432
+
433
+ state.result = result;
434
+ state.finishedAt = nowISO();
435
+
436
+ // Log task outcome with relevant details
437
+ const durSec = (result.durationMs / 1000).toFixed(1);
438
+ if (state.status === 'success') {
439
+ log.info(`[task:${taskId}]`, `success (${durSec}s)`);
440
+ } else {
441
+ log.error(`[task:${taskId}]`,
442
+ `${state.status} exit=${result.exitCode} duration=${durSec}s`);
443
+ if (result.stderr) {
444
+ const tail = tailLines(result.stderr, 10);
445
+ log.error(`[task:${taskId}]`, `stderr tail:\n${tail}`);
446
+ }
447
+ }
448
+
449
+ // File-only: full stdout/stderr dump (clipped) + extracted metadata
450
+ log.debug(`[task:${taskId}]`,
451
+ `stdout: ${result.stdout.length} chars, stderr: ${result.stderr.length} chars`);
452
+ if (result.sessionId) {
453
+ log.debug(`[task:${taskId}]`, `sessionId: ${result.sessionId}`);
454
+ }
455
+ if (result.outputPath) {
456
+ log.debug(`[task:${taskId}]`, `wrote output: ${result.outputPath}`);
457
+ }
458
+ if (result.stderrPath) {
459
+ log.debug(`[task:${taskId}]`, `wrote stderr: ${result.stderrPath}`);
460
+ }
461
+ if (result.stdout) {
462
+ log.quiet(`--- stdout (${taskId}) ---\n${clip(result.stdout)}\n--- end stdout ---`);
463
+ }
464
+ if (result.stderr) {
465
+ log.quiet(`--- stderr (${taskId}) ---\n${clip(result.stderr)}\n--- end stderr ---`);
466
+ }
467
+ if (task.completion) {
468
+ log.debug(`[task:${taskId}]`,
469
+ `completion check: type=${task.completion.type} result=${state.status}`);
470
+ }
471
+
472
+ } catch (err: unknown) {
473
+ state.status = 'failed';
474
+ state.finishedAt = nowISO();
475
+ const errMsg = err instanceof Error ? (err.stack ?? err.message) : String(err);
476
+ log.error(`[task:${taskId}]`, `failed before execution: ${errMsg}`);
477
+ state.result = {
478
+ exitCode: -1,
479
+ stdout: '',
480
+ stderr: errMsg,
481
+ outputPath: null, stderrPath: null, durationMs: 0,
482
+ sessionId: null, normalizedOutput: null,
483
+ };
484
+ }
485
+
486
+ // 7. Fire hooks
487
+ const finalStatus: TaskStatus = state.status;
488
+ await fireHook(taskId, finalStatus === 'success' ? 'task_success' : 'task_failure');
489
+
490
+ // 8. Handle stop_all for failure states
491
+ if (finalStatus !== 'success' && getOnFailure(taskId) === 'stop_all') {
492
+ applyStopAll(node.track.id);
493
+ }
494
+ }
495
+
496
+ // ── Event loop ──
497
+ try {
498
+ let progress = true;
499
+ while (progress && !pipelineAborted) {
500
+ progress = false;
501
+
502
+ // Collect tasks whose deps are all terminal and that are still waiting
503
+ const launchable: string[] = [];
504
+ for (const [id, state] of states) {
505
+ if (state.status !== 'waiting') continue;
506
+ const node = dag.nodes.get(id)!;
507
+ const allDepsTerminal = node.dependsOn.length === 0 ||
508
+ node.dependsOn.every(d => isTerminal(states.get(d)!.status));
509
+ if (allDepsTerminal) launchable.push(id);
510
+ }
511
+
512
+ if (launchable.length === 0) {
513
+ // Check if anything is still running (trigger waits etc.)
514
+ const anyNonTerminal = [...states.values()].some(s => !isTerminal(s.status));
515
+ if (!anyNonTerminal) break;
516
+ await new Promise(r => setTimeout(r, 50));
517
+ progress = true;
518
+ continue;
519
+ }
520
+
521
+ // Launch all launchable tasks concurrently
522
+ await Promise.all(launchable.map(id => processTask(id)));
523
+ progress = true;
524
+ }
525
+
526
+ if (pipelineAborted) {
527
+ for (const [, state] of states) {
528
+ if (!isTerminal(state.status)) {
529
+ // Running tasks get timeout (they were killed); waiting tasks get skipped
530
+ state.status = state.status === 'running' ? 'timeout' : 'skipped';
531
+ state.finishedAt = nowISO();
532
+ }
533
+ }
534
+ }
535
+ } finally {
536
+ if (pipelineTimer) clearTimeout(pipelineTimer);
537
+ // Safety net: drain any approvals still pending at shutdown (e.g. crash path).
538
+ if (approvalGateway.pending().length > 0) {
539
+ approvalGateway.abortAll('pipeline finished');
540
+ }
541
+ }
542
+
543
+ // ── Summary ──
544
+ const summary = { total: 0, success: 0, failed: 0, skipped: 0, timeout: 0, blocked: 0 };
545
+ for (const [, state] of states) {
546
+ summary.total++;
547
+ switch (state.status) {
548
+ case 'success': summary.success++; break;
549
+ case 'failed': summary.failed++; break;
550
+ case 'skipped': summary.skipped++; break;
551
+ case 'timeout': summary.timeout++; break;
552
+ case 'blocked': summary.blocked++; break;
553
+ }
554
+ }
555
+
556
+ const finishedAt = nowISO();
557
+ const durationMs = new Date(finishedAt).getTime() - new Date(startedAt).getTime();
558
+
559
+ if (pipelineAborted) {
560
+ await executeHook(config.hooks, 'pipeline_error',
561
+ buildPipelineErrorContext(pipelineInfo, 'Pipeline timeout exceeded'), workDir);
562
+ } else {
563
+ await executeHook(config.hooks, 'pipeline_complete',
564
+ buildPipelineCompleteContext(
565
+ { ...pipelineInfo, finished_at: finishedAt, duration_ms: durationMs }, summary), workDir);
566
+ }
567
+
568
+ const allSuccess = !pipelineAborted
569
+ && summary.failed === 0 && summary.timeout === 0 && summary.blocked === 0;
570
+
571
+ log.section('Pipeline summary');
572
+ log.quiet(`status: ${pipelineAborted ? 'aborted (timeout)' : 'completed'}`);
573
+ log.quiet(`duration: ${(durationMs / 1000).toFixed(1)}s`);
574
+ log.quiet(
575
+ `counts: total=${summary.total} success=${summary.success} ` +
576
+ `failed=${summary.failed} skipped=${summary.skipped} ` +
577
+ `timeout=${summary.timeout} blocked=${summary.blocked}`);
578
+ log.quiet('');
579
+ log.quiet('per-task:');
580
+ for (const [id, state] of states) {
581
+ const dur = state.result?.durationMs != null
582
+ ? `${(state.result.durationMs / 1000).toFixed(1)}s` : '-';
583
+ const exit = state.result?.exitCode ?? '-';
584
+ log.quiet(` ${state.status.padEnd(8)} ${id} (exit=${exit}, ${dur})`);
585
+ }
586
+
587
+ console.log(`\n[Pipeline "${config.name}"] completed`);
588
+ console.log(` Total: ${summary.total} | Success: ${summary.success} | Failed: ${summary.failed} | Skipped: ${summary.skipped} | Timeout: ${summary.timeout} | Blocked: ${summary.blocked}`);
589
+ console.log(` Duration: ${(durationMs / 1000).toFixed(1)}s`);
590
+ console.log(` Log: ${log.path}`);
591
+
592
+ return { success: allSuccess, summary, states };
593
+ }
594
+
595
+ function isTerminal(status: TaskStatus): boolean {
596
+ return status === 'success' || status === 'failed' || status === 'timeout'
597
+ || status === 'skipped' || status === 'blocked';
598
+ }