@tagma/sdk 0.6.12 → 0.7.1
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/README.md +56 -15
- package/dist/bootstrap.d.ts +6 -6
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +5 -6
- package/dist/bootstrap.js.map +1 -1
- package/dist/config.d.ts +8 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +5 -0
- package/dist/config.js.map +1 -0
- package/dist/core/dataflow.d.ts +23 -0
- package/dist/core/dataflow.d.ts.map +1 -0
- package/dist/core/dataflow.js +99 -0
- package/dist/core/dataflow.js.map +1 -0
- package/dist/core/log-prune.d.ts +16 -0
- package/dist/core/log-prune.d.ts.map +1 -0
- package/dist/core/log-prune.js +34 -0
- package/dist/core/log-prune.js.map +1 -0
- package/dist/core/preflight.d.ts +13 -0
- package/dist/core/preflight.d.ts.map +1 -0
- package/dist/core/preflight.js +61 -0
- package/dist/core/preflight.js.map +1 -0
- package/dist/core/run-context.d.ts +52 -0
- package/dist/core/run-context.d.ts.map +1 -0
- package/dist/core/run-context.js +156 -0
- package/dist/core/run-context.js.map +1 -0
- package/dist/core/run-state.d.ts +25 -0
- package/dist/core/run-state.d.ts.map +1 -0
- package/dist/core/run-state.js +93 -0
- package/dist/core/run-state.js.map +1 -0
- package/dist/core/scheduler.d.ts +13 -0
- package/dist/core/scheduler.d.ts.map +1 -0
- package/dist/core/scheduler.js +35 -0
- package/dist/core/scheduler.js.map +1 -0
- package/dist/core/task-executor.d.ts +13 -0
- package/dist/core/task-executor.d.ts.map +1 -0
- package/dist/core/task-executor.js +623 -0
- package/dist/core/task-executor.js.map +1 -0
- package/dist/core/trigger-errors.d.ts +9 -0
- package/dist/core/trigger-errors.d.ts.map +1 -0
- package/dist/core/trigger-errors.js +15 -0
- package/dist/core/trigger-errors.js.map +1 -0
- package/dist/engine.d.ts +6 -14
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +68 -1035
- package/dist/engine.js.map +1 -1
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/pipeline-definition.d.ts +3 -0
- package/dist/pipeline-definition.d.ts.map +1 -0
- package/dist/pipeline-definition.js +4 -0
- package/dist/pipeline-definition.js.map +1 -0
- package/dist/pipeline-runner.d.ts +2 -1
- package/dist/pipeline-runner.d.ts.map +1 -1
- package/dist/pipeline-runner.js +2 -2
- package/dist/pipeline-runner.js.map +1 -1
- package/dist/plugins.d.ts +5 -0
- package/dist/plugins.d.ts.map +1 -0
- package/dist/plugins.js +3 -0
- package/dist/plugins.js.map +1 -0
- package/dist/ports.d.ts +4 -0
- package/dist/ports.d.ts.map +1 -1
- package/dist/ports.js +27 -4
- package/dist/ports.js.map +1 -1
- package/dist/registry.d.ts +3 -19
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +7 -35
- package/dist/registry.js.map +1 -1
- package/dist/tagma.d.ts +24 -0
- package/dist/tagma.d.ts.map +1 -0
- package/dist/tagma.js +23 -0
- package/dist/tagma.js.map +1 -0
- package/dist/utils-api.d.ts +2 -0
- package/dist/utils-api.d.ts.map +1 -0
- package/dist/utils-api.js +2 -0
- package/dist/utils-api.js.map +1 -0
- package/dist/validate-raw.d.ts +4 -4
- package/dist/validate-raw.js +91 -132
- package/dist/validate-raw.js.map +1 -1
- package/dist/yaml.d.ts +4 -0
- package/dist/yaml.d.ts.map +1 -0
- package/dist/yaml.js +3 -0
- package/dist/yaml.js.map +1 -0
- package/package.json +53 -8
- package/src/bootstrap.ts +6 -6
- package/src/config.ts +26 -0
- package/src/core/dataflow.test.ts +166 -0
- package/src/core/dataflow.ts +161 -0
- package/src/core/log-prune.test.ts +58 -0
- package/src/core/log-prune.ts +43 -0
- package/src/core/preflight.test.ts +49 -0
- package/src/core/preflight.ts +89 -0
- package/src/core/run-context.test.ts +244 -0
- package/src/core/run-context.ts +207 -0
- package/src/core/run-state.test.ts +98 -0
- package/src/core/run-state.ts +122 -0
- package/src/core/scheduler.test.ts +83 -0
- package/src/core/scheduler.ts +42 -0
- package/src/core/task-executor.ts +769 -0
- package/src/core/trigger-errors.ts +15 -0
- package/src/engine-ports-mixed.test.ts +68 -411
- package/src/engine-ports.test.ts +37 -341
- package/src/engine.ts +80 -1248
- package/src/index.ts +28 -0
- package/src/pipeline-definition.ts +5 -0
- package/src/pipeline-runner.test.ts +5 -9
- package/src/pipeline-runner.ts +3 -2
- package/src/plugin-registry.test.ts +7 -10
- package/src/plugins.ts +18 -0
- package/src/ports.test.ts +80 -0
- package/src/ports.ts +36 -4
- package/src/registry.ts +7 -49
- package/src/schema-ports.test.ts +41 -214
- package/src/tagma.test.ts +84 -0
- package/src/tagma.ts +47 -0
- package/src/utils-api.ts +8 -0
- package/src/validate-raw-ports.test.ts +80 -393
- package/src/validate-raw.ts +93 -137
- package/src/yaml.ts +11 -0
- package/dist/sdk.d.ts +0 -32
- package/dist/sdk.d.ts.map +0 -1
- package/dist/sdk.js +0 -41
- package/dist/sdk.js.map +0 -1
- package/src/sdk.ts +0 -151
package/dist/engine.js
CHANGED
|
@@ -1,131 +1,19 @@
|
|
|
1
1
|
import { resolve } from 'path';
|
|
2
|
-
import { readdir, rm } from 'fs/promises';
|
|
3
2
|
import { buildDag } from './dag';
|
|
4
|
-
import { defaultRegistry } from './registry';
|
|
5
|
-
import { runSpawn, runCommand } from './runner';
|
|
6
3
|
import { parseDuration, nowISO, generateRunId } from './utils';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { executeHook, buildPipelineStartContext, buildTaskContext, buildPipelineCompleteContext, buildPipelineErrorContext, } from './hooks';
|
|
10
|
-
import { Logger, tailLines, clip } from './logger';
|
|
4
|
+
import { executeHook, buildPipelineStartContext, buildPipelineCompleteContext, buildPipelineErrorContext, } from './hooks';
|
|
5
|
+
import { Logger } from './logger';
|
|
11
6
|
import { InMemoryApprovalGateway } from './approval';
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
this.name = 'TriggerBlockedError';
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
export class TriggerTimeoutError extends Error {
|
|
23
|
-
code = 'TRIGGER_TIMEOUT';
|
|
24
|
-
constructor(message) {
|
|
25
|
-
super(message);
|
|
26
|
-
this.name = 'TriggerTimeoutError';
|
|
27
|
-
}
|
|
28
|
-
}
|
|
7
|
+
import { freezeStates, summarizeStates, toRunTaskState, } from './core/run-state';
|
|
8
|
+
import { preflight } from './core/preflight';
|
|
9
|
+
import { pruneLogDirs } from './core/log-prune';
|
|
10
|
+
import { RunContext } from './core/run-context';
|
|
11
|
+
import { allTasksTerminal, findLaunchableTasks, skipNonTerminalTasks, } from './core/scheduler';
|
|
12
|
+
import { executeTask } from './core/task-executor';
|
|
13
|
+
export { TriggerBlockedError, TriggerTimeoutError } from './core/trigger-errors';
|
|
29
14
|
function isPromptTaskConfig(task) {
|
|
30
15
|
return task.prompt !== undefined && task.command === undefined;
|
|
31
16
|
}
|
|
32
|
-
function isCommandTaskConfig(task) {
|
|
33
|
-
return task.command !== undefined && task.prompt === undefined;
|
|
34
|
-
}
|
|
35
|
-
// ═══ Preflight Validation ═══
|
|
36
|
-
function preflight(config, dag, registry) {
|
|
37
|
-
const errors = [];
|
|
38
|
-
for (const [, node] of dag.nodes) {
|
|
39
|
-
const task = node.task;
|
|
40
|
-
const track = node.track;
|
|
41
|
-
const driverName = task.driver ?? track.driver ?? config.driver ?? 'opencode';
|
|
42
|
-
// Pure command tasks don't use a driver — skip driver registration check.
|
|
43
|
-
const isCommandOnly = isCommandTaskConfig(task);
|
|
44
|
-
if (!isCommandOnly && !registry.hasHandler('drivers', driverName)) {
|
|
45
|
-
errors.push(`Task "${node.taskId}": driver "${driverName}" not registered`);
|
|
46
|
-
}
|
|
47
|
-
if (task.trigger && !registry.hasHandler('triggers', task.trigger.type)) {
|
|
48
|
-
errors.push(`Task "${node.taskId}": trigger type "${task.trigger.type}" not registered`);
|
|
49
|
-
}
|
|
50
|
-
if (task.completion && !registry.hasHandler('completions', task.completion.type)) {
|
|
51
|
-
errors.push(`Task "${node.taskId}": completion type "${task.completion.type}" not registered`);
|
|
52
|
-
}
|
|
53
|
-
const mws = task.middlewares ?? track.middlewares ?? [];
|
|
54
|
-
for (const mw of mws) {
|
|
55
|
-
if (!registry.hasHandler('middlewares', mw.type)) {
|
|
56
|
-
errors.push(`Task "${node.taskId}": middleware type "${mw.type}" not registered`);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
if (task.continue_from && registry.hasHandler('drivers', driverName)) {
|
|
60
|
-
const driver = registry.getHandler('drivers', driverName);
|
|
61
|
-
if (!driver.capabilities.sessionResume) {
|
|
62
|
-
// buildDag has already qualified `continue_from` and stored the result
|
|
63
|
-
// on the node; preflight runs after buildDag, so the upstream id is
|
|
64
|
-
// always available here without re-resolving.
|
|
65
|
-
const upstreamId = node.resolvedContinueFrom;
|
|
66
|
-
if (upstreamId) {
|
|
67
|
-
const upstream = dag.nodes.get(upstreamId);
|
|
68
|
-
if (upstream) {
|
|
69
|
-
// A handoff is possible via session resume (already ruled out above),
|
|
70
|
-
// OR in-memory text injection through normalizedMap
|
|
71
|
-
// (when the upstream driver implements parseResult and returns normalizedOutput).
|
|
72
|
-
const upstreamDriverName = upstream.task.driver ?? upstream.track.driver ?? config.driver ?? 'opencode';
|
|
73
|
-
const upstreamDriver = registry.hasHandler('drivers', upstreamDriverName)
|
|
74
|
-
? registry.getHandler('drivers', upstreamDriverName)
|
|
75
|
-
: null;
|
|
76
|
-
const canNormalize = typeof upstreamDriver?.parseResult === 'function';
|
|
77
|
-
if (!canNormalize) {
|
|
78
|
-
errors.push(`Task "${node.taskId}" uses continue_from: "${task.continue_from}", ` +
|
|
79
|
-
`but upstream task "${upstreamId}" its driver ` +
|
|
80
|
-
`does not implement parseResult for text-injection handoff. ` +
|
|
81
|
-
`Use a driver with parseResult, or remove continue_from.`);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
if (errors.length > 0) {
|
|
89
|
-
throw new Error(`Preflight validation failed:\n - ${errors.join('\n - ')}`);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
// ═══ Helpers ═══
|
|
93
|
-
/**
|
|
94
|
-
* Project the engine's internal TaskState onto the wire RunTaskState
|
|
95
|
-
* shape. `logs` / `totalLogCount` default to empty — they are populated
|
|
96
|
-
* on the server side from streamed `task_log` events, not from state.
|
|
97
|
-
*/
|
|
98
|
-
function toRunTaskState(taskId, trackId, taskName, state) {
|
|
99
|
-
const result = state.result;
|
|
100
|
-
const cfg = state.config;
|
|
101
|
-
return {
|
|
102
|
-
taskId,
|
|
103
|
-
trackId,
|
|
104
|
-
taskName,
|
|
105
|
-
status: state.status,
|
|
106
|
-
startedAt: state.startedAt,
|
|
107
|
-
finishedAt: state.finishedAt,
|
|
108
|
-
durationMs: result?.durationMs ?? null,
|
|
109
|
-
exitCode: result?.exitCode ?? null,
|
|
110
|
-
stdout: result?.stdout ?? '',
|
|
111
|
-
stderr: result?.stderr ?? '',
|
|
112
|
-
stdoutPath: result?.stdoutPath ?? null,
|
|
113
|
-
stderrPath: result?.stderrPath ?? null,
|
|
114
|
-
stdoutBytes: result?.stdoutBytes ?? null,
|
|
115
|
-
stderrBytes: result?.stderrBytes ?? null,
|
|
116
|
-
sessionId: result?.sessionId ?? null,
|
|
117
|
-
normalizedOutput: result?.normalizedOutput ?? null,
|
|
118
|
-
resolvedDriver: cfg.driver ?? null,
|
|
119
|
-
resolvedModel: cfg.model ?? null,
|
|
120
|
-
resolvedPermissions: cfg.permissions ?? null,
|
|
121
|
-
// Ports not yet wired through the engine's event surface. Null placeholder
|
|
122
|
-
// keeps the wire type honest until the ports extraction pass lands.
|
|
123
|
-
outputs: result?.outputs ?? null,
|
|
124
|
-
inputs: null,
|
|
125
|
-
logs: [],
|
|
126
|
-
totalLogCount: 0,
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
17
|
// Poll interval when no tasks are in-flight but non-terminal tasks remain
|
|
130
18
|
// (e.g. tasks waiting on a file or manual trigger).
|
|
131
19
|
const POLL_INTERVAL_MS = 50;
|
|
@@ -133,10 +21,13 @@ const POLL_INTERVAL_MS = 50;
|
|
|
133
21
|
// runaway parseResult can't accumulate hundreds of MB across tasks. 1 MB
|
|
134
22
|
// is generous for any text-context handoff between AI tasks.
|
|
135
23
|
const MAX_NORMALIZED_BYTES = 1_000_000;
|
|
136
|
-
export async function runPipeline(config, workDir, options
|
|
24
|
+
export async function runPipeline(config, workDir, options) {
|
|
137
25
|
const approvalGateway = options.approvalGateway ?? new InMemoryApprovalGateway();
|
|
138
26
|
const maxLogRuns = options.maxLogRuns ?? 20;
|
|
139
|
-
const registry = options.registry
|
|
27
|
+
const registry = options.registry;
|
|
28
|
+
if (!registry) {
|
|
29
|
+
throw new Error('runPipeline requires options.registry. Use createTagma().run(...) for the public SDK API.');
|
|
30
|
+
}
|
|
140
31
|
// Load any plugins declared in the pipeline config before preflight so that
|
|
141
32
|
// drivers, completions, and middlewares referenced in YAML are registered.
|
|
142
33
|
// Hosts that pre-load plugins from a custom path (e.g. the editor loading
|
|
@@ -181,18 +72,18 @@ export async function runPipeline(config, workDir, options = {}) {
|
|
|
181
72
|
log.quiet(` • ${id} [${kind}] track=${node.track.id} deps=[${deps}]`);
|
|
182
73
|
}
|
|
183
74
|
log.quiet('');
|
|
184
|
-
//
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
75
|
+
// Per-run state container. Constructed before the pipeline_start hook
|
|
76
|
+
// so the early-return path (blocked pipeline) can call freezeStates on
|
|
77
|
+
// the populated idle-state map. The constructor has no side effects —
|
|
78
|
+
// no listeners installed, no events emitted.
|
|
79
|
+
const ctx = new RunContext({
|
|
80
|
+
runId,
|
|
81
|
+
dag,
|
|
82
|
+
config,
|
|
83
|
+
workDir,
|
|
84
|
+
pipelineInfo,
|
|
85
|
+
onEvent: options.onEvent,
|
|
86
|
+
});
|
|
196
87
|
// Pipeline start hook (gate). Runs BEFORE the engine emits run_start so
|
|
197
88
|
// a blocked pipeline produces zero wire events (the server treats the
|
|
198
89
|
// thrown error as run_error). Hosts get a rich error message; nothing
|
|
@@ -213,11 +104,11 @@ export async function runPipeline(config, workDir, options = {}) {
|
|
|
213
104
|
timeout: 0,
|
|
214
105
|
blocked: 0,
|
|
215
106
|
},
|
|
216
|
-
states: freezeStates(states),
|
|
107
|
+
states: freezeStates(ctx.states),
|
|
217
108
|
};
|
|
218
109
|
}
|
|
219
110
|
// Pipeline approved — transition all tasks to waiting.
|
|
220
|
-
for (const [, state] of states) {
|
|
111
|
+
for (const [, state] of ctx.states) {
|
|
221
112
|
state.status = 'waiting';
|
|
222
113
|
}
|
|
223
114
|
// Emit run_start with a wire-shape snapshot so SSE subscribers can
|
|
@@ -226,65 +117,33 @@ export async function runPipeline(config, workDir, options = {}) {
|
|
|
226
117
|
// the engine owns the lifecycle boundary.
|
|
227
118
|
const runStartTasks = [];
|
|
228
119
|
for (const [id, node] of dag.nodes) {
|
|
229
|
-
const s = states.get(id);
|
|
120
|
+
const s = ctx.states.get(id);
|
|
230
121
|
runStartTasks.push(toRunTaskState(id, node.track.id, node.task.name ?? id, s));
|
|
231
122
|
}
|
|
232
|
-
emit({ type: 'run_start', runId, tasks: runStartTasks });
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
//
|
|
236
|
-
//
|
|
237
|
-
const outputValuesMap = new Map();
|
|
238
|
-
// Full upstream result data for lightweight input bindings such as
|
|
239
|
-
// `taskId.stdout` and `taskId.outputs.name`.
|
|
240
|
-
const bindingDataMap = new Map();
|
|
241
|
-
// Resolved port inputs keyed by fully-qualified task id. Written once,
|
|
242
|
-
// just before a task runs, so every subsequent task_update event can
|
|
243
|
-
// echo them to the UI without re-resolving.
|
|
244
|
-
const resolvedInputsMap = new Map();
|
|
245
|
-
// Reverse adjacency: for each task, list the direct-downstream task ids
|
|
246
|
-
// (tasks whose `depends_on` includes this one after DAG qualification).
|
|
247
|
-
// Computed once up front so Prompt-task port inference — which needs
|
|
248
|
-
// "what Commands directly consume me?" — is O(1) instead of O(tasks)
|
|
249
|
-
// per Prompt start. `dag.nodes` only exposes forward edges via
|
|
250
|
-
// `dependsOn`, so we build this locally.
|
|
251
|
-
const directDownstreams = new Map();
|
|
252
|
-
for (const [id] of dag.nodes)
|
|
253
|
-
directDownstreams.set(id, []);
|
|
254
|
-
for (const [id, node] of dag.nodes) {
|
|
255
|
-
for (const upstream of node.dependsOn) {
|
|
256
|
-
const list = directDownstreams.get(upstream);
|
|
257
|
-
if (list)
|
|
258
|
-
list.push(id);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
// Pipeline timeout + abort reason tracking.
|
|
262
|
-
//
|
|
263
|
-
// `abortReason` replaces the previous `pipelineAborted: boolean`: it
|
|
264
|
-
// carries the concrete cause (timeout / stop_all / external) through
|
|
265
|
-
// to run_end and the pipeline_error hook so downstream consumers can
|
|
266
|
-
// distinguish them without scraping message strings.
|
|
123
|
+
ctx.emit({ type: 'run_start', runId, tasks: runStartTasks });
|
|
124
|
+
// Pipeline timeout. `ctx.abortReason` carries the concrete cause
|
|
125
|
+
// (timeout / stop_all / external) through to run_end and the
|
|
126
|
+
// pipeline_error hook so downstream consumers can distinguish them
|
|
127
|
+
// without scraping message strings.
|
|
267
128
|
const pipelineTimeoutMs = config.timeout ? parseDuration(config.timeout) : 0;
|
|
268
|
-
let abortReason = null;
|
|
269
|
-
const abortController = new AbortController();
|
|
270
129
|
let pipelineTimer = null;
|
|
271
130
|
if (pipelineTimeoutMs > 0) {
|
|
272
131
|
pipelineTimer = setTimeout(() => {
|
|
273
|
-
if (abortReason === null)
|
|
274
|
-
abortReason = 'timeout';
|
|
275
|
-
abortController.abort();
|
|
132
|
+
if (ctx.abortReason === null)
|
|
133
|
+
ctx.abortReason = 'timeout';
|
|
134
|
+
ctx.abortController.abort();
|
|
276
135
|
}, pipelineTimeoutMs);
|
|
277
136
|
}
|
|
278
137
|
// When the pipeline is aborted (timeout, stop_all, external), drain
|
|
279
138
|
// all pending approvals so waiting triggers unblock immediately.
|
|
280
|
-
abortController.signal.addEventListener('abort', () => {
|
|
139
|
+
ctx.abortController.signal.addEventListener('abort', () => {
|
|
281
140
|
approvalGateway.abortAll('pipeline aborted');
|
|
282
141
|
});
|
|
283
142
|
// Wire external cancel signal into the internal abort controller.
|
|
284
143
|
const externalAbortHandler = () => {
|
|
285
|
-
if (abortReason === null)
|
|
286
|
-
abortReason = 'external';
|
|
287
|
-
abortController.abort();
|
|
144
|
+
if (ctx.abortReason === null)
|
|
145
|
+
ctx.abortReason = 'external';
|
|
146
|
+
ctx.abortController.abort();
|
|
288
147
|
};
|
|
289
148
|
if (options.signal) {
|
|
290
149
|
if (options.signal.aborted) {
|
|
@@ -299,7 +158,7 @@ export async function runPipeline(config, workDir, options = {}) {
|
|
|
299
158
|
// updates. The server no longer needs its own gateway subscription.
|
|
300
159
|
const unsubscribeApprovals = approvalGateway.subscribe((ev) => {
|
|
301
160
|
if (ev.type === 'requested') {
|
|
302
|
-
emit({
|
|
161
|
+
ctx.emit({
|
|
303
162
|
type: 'approval_request',
|
|
304
163
|
runId,
|
|
305
164
|
request: {
|
|
@@ -320,7 +179,7 @@ export async function runPipeline(config, workDir, options = {}) {
|
|
|
320
179
|
: ev.type === 'expired'
|
|
321
180
|
? 'timeout'
|
|
322
181
|
: 'aborted';
|
|
323
|
-
emit({
|
|
182
|
+
ctx.emit({
|
|
324
183
|
type: 'approval_resolved',
|
|
325
184
|
runId,
|
|
326
185
|
requestId: ev.request.id,
|
|
@@ -328,776 +187,27 @@ export async function runPipeline(config, workDir, options = {}) {
|
|
|
328
187
|
});
|
|
329
188
|
}
|
|
330
189
|
});
|
|
331
|
-
// ── Helpers ──
|
|
332
|
-
function emit(event) {
|
|
333
|
-
options.onEvent?.(event);
|
|
334
|
-
}
|
|
335
|
-
function setTaskStatus(taskId, newStatus) {
|
|
336
|
-
const state = states.get(taskId);
|
|
337
|
-
// Terminal lock: once a task reaches a terminal state it must not be
|
|
338
|
-
// re-transitioned. This prevents stop_all from marking running tasks as
|
|
339
|
-
// skipped and then having their in-flight processTask promise overwrite
|
|
340
|
-
// that with success/failed, producing an invalid double transition.
|
|
341
|
-
if (isTerminal(state.status))
|
|
342
|
-
return;
|
|
343
|
-
state.status = newStatus;
|
|
344
|
-
const result = state.result;
|
|
345
|
-
const cfg = state.config;
|
|
346
|
-
emit({
|
|
347
|
-
type: 'task_update',
|
|
348
|
-
runId,
|
|
349
|
-
taskId,
|
|
350
|
-
status: newStatus,
|
|
351
|
-
startedAt: state.startedAt ?? undefined,
|
|
352
|
-
finishedAt: state.finishedAt ?? undefined,
|
|
353
|
-
durationMs: result?.durationMs,
|
|
354
|
-
exitCode: result?.exitCode,
|
|
355
|
-
stdout: result?.stdout,
|
|
356
|
-
stderr: result?.stderr,
|
|
357
|
-
stdoutPath: result?.stdoutPath ?? null,
|
|
358
|
-
stderrPath: result?.stderrPath ?? null,
|
|
359
|
-
stdoutBytes: result?.stdoutBytes ?? null,
|
|
360
|
-
stderrBytes: result?.stderrBytes ?? null,
|
|
361
|
-
sessionId: result?.sessionId ?? null,
|
|
362
|
-
normalizedOutput: result?.normalizedOutput ?? null,
|
|
363
|
-
inputs: resolvedInputsMap.get(taskId) ?? null,
|
|
364
|
-
outputs: outputValuesMap.get(taskId) ?? null,
|
|
365
|
-
resolvedDriver: cfg.driver ?? null,
|
|
366
|
-
resolvedModel: cfg.model ?? null,
|
|
367
|
-
resolvedPermissions: cfg.permissions ?? null,
|
|
368
|
-
});
|
|
369
|
-
}
|
|
370
|
-
function getOnFailure(taskId) {
|
|
371
|
-
return dag.nodes.get(taskId)?.track.on_failure ?? 'skip_downstream';
|
|
372
|
-
}
|
|
373
|
-
function isDependencySatisfied(depId) {
|
|
374
|
-
const depState = states.get(depId);
|
|
375
|
-
if (!depState)
|
|
376
|
-
return 'skip';
|
|
377
|
-
switch (depState.status) {
|
|
378
|
-
case 'success':
|
|
379
|
-
return 'satisfied';
|
|
380
|
-
case 'skipped':
|
|
381
|
-
return 'skip';
|
|
382
|
-
case 'failed':
|
|
383
|
-
case 'timeout':
|
|
384
|
-
case 'blocked':
|
|
385
|
-
return getOnFailure(depId) === 'ignore' ? 'satisfied' : 'skip';
|
|
386
|
-
default:
|
|
387
|
-
return 'unsatisfied';
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* H3: "stop_all" historically only stopped tasks within the same track,
|
|
392
|
-
* which contradicted both its name and user expectations. It now stops
|
|
393
|
-
* the **entire pipeline**:
|
|
394
|
-
* - In-flight tasks are signalled via the shared abort controller so
|
|
395
|
-
* drivers / runner.ts can cancel cooperatively (returning
|
|
396
|
-
* `failureKind: 'timeout'`).
|
|
397
|
-
* - Still-waiting tasks across every track are immediately marked
|
|
398
|
-
* skipped so the run completes promptly.
|
|
399
|
-
* The terminal lock in setTaskStatus prevents any later re-transition
|
|
400
|
-
* should a completed running task try to overwrite the skipped state.
|
|
401
|
-
*/
|
|
402
|
-
function applyStopAll(_failedTrackId) {
|
|
403
|
-
if (abortReason === null)
|
|
404
|
-
abortReason = 'stop_all';
|
|
405
|
-
abortController.abort();
|
|
406
|
-
for (const [id, state] of states) {
|
|
407
|
-
if (state.status === 'waiting') {
|
|
408
|
-
state.finishedAt = nowISO();
|
|
409
|
-
setTaskStatus(id, 'skipped');
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
function buildTaskInfoObj(taskId) {
|
|
414
|
-
const state = states.get(taskId);
|
|
415
|
-
return {
|
|
416
|
-
id: taskId,
|
|
417
|
-
name: state.config.name,
|
|
418
|
-
type: isPromptTaskConfig(state.config) ? 'ai' : 'command',
|
|
419
|
-
status: state.status,
|
|
420
|
-
exit_code: state.result?.exitCode ?? null,
|
|
421
|
-
duration_ms: state.result?.durationMs ?? null,
|
|
422
|
-
stderr_path: state.result?.stderrPath ?? null,
|
|
423
|
-
session_id: state.result?.sessionId ?? null,
|
|
424
|
-
started_at: state.startedAt,
|
|
425
|
-
finished_at: state.finishedAt,
|
|
426
|
-
};
|
|
427
|
-
}
|
|
428
|
-
function trackInfoOf(taskId) {
|
|
429
|
-
const node = dag.nodes.get(taskId);
|
|
430
|
-
return { id: node.track.id, name: node.track.name };
|
|
431
|
-
}
|
|
432
|
-
async function fireHook(taskId, event) {
|
|
433
|
-
await executeHook(config.hooks, event, buildTaskContext(event, pipelineInfo, trackInfoOf(taskId), buildTaskInfoObj(taskId)), workDir, abortController.signal);
|
|
434
|
-
}
|
|
435
190
|
// ── Process a single task ──
|
|
436
|
-
async function processTask(taskId) {
|
|
437
|
-
const state = states.get(taskId);
|
|
438
|
-
const node = dag.nodes.get(taskId);
|
|
439
|
-
const task = node.task;
|
|
440
|
-
const track = node.track;
|
|
441
|
-
log.section(`Task ${taskId}`, taskId);
|
|
442
|
-
log.debug(`[task:${taskId}]`, `type=${isPromptTaskConfig(task) ? 'ai' : 'cmd'} track=${track.id} deps=[${node.dependsOn.join(', ') || '(root)'}]`);
|
|
443
|
-
// 1. Check dependencies
|
|
444
|
-
for (const depId of node.dependsOn) {
|
|
445
|
-
const result = isDependencySatisfied(depId);
|
|
446
|
-
if (result === 'skip') {
|
|
447
|
-
const depStatus = states.get(depId)?.status ?? 'unknown';
|
|
448
|
-
log.debug(`[task:${taskId}]`, `skipped (upstream "${depId}" status=${depStatus})`);
|
|
449
|
-
state.finishedAt = nowISO();
|
|
450
|
-
setTaskStatus(taskId, 'skipped');
|
|
451
|
-
return;
|
|
452
|
-
}
|
|
453
|
-
if (result === 'unsatisfied')
|
|
454
|
-
return; // still waiting
|
|
455
|
-
}
|
|
456
|
-
// 2. Check trigger
|
|
457
|
-
if (task.trigger) {
|
|
458
|
-
log.debug(`[task:${taskId}]`, `trigger wait: type=${task.trigger.type} ${JSON.stringify(task.trigger)}`);
|
|
459
|
-
try {
|
|
460
|
-
const triggerPlugin = registry.getHandler('triggers', task.trigger.type);
|
|
461
|
-
// R6: race the plugin's watch() against the pipeline's abort signal
|
|
462
|
-
// AND the task-level timeout. Third-party triggers may forget to
|
|
463
|
-
// wire up ctx.signal — without the abort race, an aborted pipeline
|
|
464
|
-
// would hang forever waiting for the plugin's watch promise to
|
|
465
|
-
// resolve. And without the timeout race, a buggy watch() that never
|
|
466
|
-
// settles would ignore the user's `task.timeout` (which the spawn
|
|
467
|
-
// path at step 4 already honours) — a task could wedge the whole
|
|
468
|
-
// pipeline until pipeline-level timeout fires (or forever, if none
|
|
469
|
-
// is set). Honouring task.timeout here makes the two stages
|
|
470
|
-
// symmetric. The cleanup paths in finally never run on the orphaned
|
|
471
|
-
// plugin promise (it's allowed to leak a watcher; the pipeline is
|
|
472
|
-
// being torn down anyway).
|
|
473
|
-
const triggerTimeoutMs = task.timeout ? parseDuration(task.timeout) : 0;
|
|
474
|
-
await new Promise((resolve, reject) => {
|
|
475
|
-
let settled = false;
|
|
476
|
-
let timer = null;
|
|
477
|
-
const onAbort = () => {
|
|
478
|
-
if (settled)
|
|
479
|
-
return;
|
|
480
|
-
settled = true;
|
|
481
|
-
if (timer !== null)
|
|
482
|
-
clearTimeout(timer);
|
|
483
|
-
reject(new Error('Pipeline aborted'));
|
|
484
|
-
};
|
|
485
|
-
if (abortController.signal.aborted) {
|
|
486
|
-
onAbort();
|
|
487
|
-
return;
|
|
488
|
-
}
|
|
489
|
-
abortController.signal.addEventListener('abort', onAbort, { once: true });
|
|
490
|
-
if (triggerTimeoutMs > 0) {
|
|
491
|
-
timer = setTimeout(() => {
|
|
492
|
-
if (settled)
|
|
493
|
-
return;
|
|
494
|
-
settled = true;
|
|
495
|
-
abortController.signal.removeEventListener('abort', onAbort);
|
|
496
|
-
reject(new TriggerTimeoutError(`Trigger "${task.trigger.type}" did not settle within ${task.timeout} (task-level timeout)`));
|
|
497
|
-
}, triggerTimeoutMs);
|
|
498
|
-
}
|
|
499
|
-
triggerPlugin
|
|
500
|
-
.watch(task.trigger, {
|
|
501
|
-
taskId: node.taskId,
|
|
502
|
-
trackId: track.id,
|
|
503
|
-
workDir: task.cwd ?? workDir,
|
|
504
|
-
signal: abortController.signal,
|
|
505
|
-
approvalGateway,
|
|
506
|
-
})
|
|
507
|
-
.then((v) => {
|
|
508
|
-
if (settled)
|
|
509
|
-
return;
|
|
510
|
-
settled = true;
|
|
511
|
-
if (timer !== null)
|
|
512
|
-
clearTimeout(timer);
|
|
513
|
-
abortController.signal.removeEventListener('abort', onAbort);
|
|
514
|
-
resolve(v);
|
|
515
|
-
}, (e) => {
|
|
516
|
-
if (settled)
|
|
517
|
-
return;
|
|
518
|
-
settled = true;
|
|
519
|
-
if (timer !== null)
|
|
520
|
-
clearTimeout(timer);
|
|
521
|
-
abortController.signal.removeEventListener('abort', onAbort);
|
|
522
|
-
reject(e);
|
|
523
|
-
});
|
|
524
|
-
});
|
|
525
|
-
log.debug(`[task:${taskId}]`, `trigger fired`);
|
|
526
|
-
}
|
|
527
|
-
catch (err) {
|
|
528
|
-
// If pipeline was aborted while we were still waiting for the trigger,
|
|
529
|
-
// this task never entered running state → skipped, not timeout.
|
|
530
|
-
state.finishedAt = nowISO();
|
|
531
|
-
if (abortReason !== null) {
|
|
532
|
-
setTaskStatus(taskId, 'skipped');
|
|
533
|
-
}
|
|
534
|
-
else if (err instanceof TriggerBlockedError) {
|
|
535
|
-
setTaskStatus(taskId, 'blocked'); // user/policy rejection
|
|
536
|
-
}
|
|
537
|
-
else if (err instanceof TriggerTimeoutError) {
|
|
538
|
-
setTaskStatus(taskId, 'timeout'); // genuine trigger wait timeout
|
|
539
|
-
}
|
|
540
|
-
else {
|
|
541
|
-
// A7 fallback: also check message strings for backward-compat with
|
|
542
|
-
// third-party trigger plugins that don't throw typed errors yet.
|
|
543
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
544
|
-
if (msg.includes('rejected') || msg.includes('denied')) {
|
|
545
|
-
setTaskStatus(taskId, 'blocked');
|
|
546
|
-
}
|
|
547
|
-
else if (msg.includes('timeout')) {
|
|
548
|
-
setTaskStatus(taskId, 'timeout');
|
|
549
|
-
}
|
|
550
|
-
else {
|
|
551
|
-
setTaskStatus(taskId, 'failed'); // plugin error, watcher crash, etc.
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
try {
|
|
555
|
-
await fireHook(taskId, 'task_failure');
|
|
556
|
-
}
|
|
557
|
-
catch (hookErr) {
|
|
558
|
-
log.error(`[task:${taskId}]`, `hook execution failed: ${hookErr instanceof Error ? hookErr.message : String(hookErr)}`);
|
|
559
|
-
}
|
|
560
|
-
return;
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
// 3. task_start hook (gate)
|
|
564
|
-
const hookResult = await executeHook(config.hooks, 'task_start', buildTaskContext('task_start', pipelineInfo, trackInfoOf(taskId), buildTaskInfoObj(taskId)), workDir, abortController.signal);
|
|
565
|
-
if (hookResult.exitCode !== 0 || config.hooks?.task_start) {
|
|
566
|
-
log.debug(`[task:${taskId}]`, `task_start hook exit=${hookResult.exitCode} allowed=${hookResult.allowed}`);
|
|
567
|
-
}
|
|
568
|
-
if (!hookResult.allowed) {
|
|
569
|
-
state.finishedAt = nowISO();
|
|
570
|
-
setTaskStatus(taskId, 'blocked');
|
|
571
|
-
try {
|
|
572
|
-
await fireHook(taskId, 'task_failure');
|
|
573
|
-
}
|
|
574
|
-
catch (hookErr) {
|
|
575
|
-
log.error(`[task:${taskId}]`, `hook execution failed: ${hookErr instanceof Error ? hookErr.message : String(hookErr)}`);
|
|
576
|
-
}
|
|
577
|
-
return;
|
|
578
|
-
}
|
|
579
|
-
// 3.5. Resolve port inputs from upstream outputs. This is the last
|
|
580
|
-
// gate before execution: missing-required inputs block the task
|
|
581
|
-
// without ever spawning a process, so the caller sees a clear
|
|
582
|
-
// "blocked: missing input X" rather than a cryptic runtime error
|
|
583
|
-
// from a command that expanded a placeholder to the empty string.
|
|
584
|
-
// Resolution runs even for tasks that declare no ports — the call
|
|
585
|
-
// is cheap and returns `{kind: 'ready', inputs: {}}` in that case,
|
|
586
|
-
// which downstream code handles uniformly.
|
|
587
|
-
//
|
|
588
|
-
// Prompt Tasks have no declared ports — their I/O contract is
|
|
589
|
-
// inferred from direct-neighbor Command Tasks (see ports.ts:
|
|
590
|
-
// `inferPromptPorts`). We synthesize a `TaskPorts` object and
|
|
591
|
-
// feed it into the same resolve/substitute/render/extract
|
|
592
|
-
// pipeline the Command path uses. Collisions that a Prompt can't
|
|
593
|
-
// disambiguate (same input name on two upstreams, incompatible
|
|
594
|
-
// downstream output types) block the task with a clear message.
|
|
595
|
-
const isPromptTask = isPromptTaskConfig(task);
|
|
596
|
-
let effectivePorts = task.ports;
|
|
597
|
-
let promptInferenceBlockReason = null;
|
|
598
|
-
if (isPromptTask) {
|
|
599
|
-
const inference = inferPromptPorts({
|
|
600
|
-
upstreams: node.dependsOn.map((upstreamId) => {
|
|
601
|
-
const upstream = dag.nodes.get(upstreamId);
|
|
602
|
-
const isUpstreamCommand = upstream ? isCommandTaskConfig(upstream.task) : false;
|
|
603
|
-
return {
|
|
604
|
-
taskId: upstreamId,
|
|
605
|
-
outputs: isUpstreamCommand ? upstream?.task.ports?.outputs : undefined,
|
|
606
|
-
};
|
|
607
|
-
}),
|
|
608
|
-
downstreams: (directDownstreams.get(taskId) ?? []).map((downstreamId) => {
|
|
609
|
-
const downstream = dag.nodes.get(downstreamId);
|
|
610
|
-
const isDownstreamCommand = downstream ? isCommandTaskConfig(downstream.task) : false;
|
|
611
|
-
return {
|
|
612
|
-
taskId: downstreamId,
|
|
613
|
-
inputs: isDownstreamCommand ? downstream?.task.ports?.inputs : undefined,
|
|
614
|
-
};
|
|
615
|
-
}),
|
|
616
|
-
});
|
|
617
|
-
effectivePorts = inference.ports;
|
|
618
|
-
if (inference.inputConflicts.length > 0 || inference.outputConflicts.length > 0) {
|
|
619
|
-
const lines = [];
|
|
620
|
-
for (const c of inference.inputConflicts)
|
|
621
|
-
lines.push(c.reason);
|
|
622
|
-
for (const c of inference.outputConflicts)
|
|
623
|
-
lines.push(c.reason);
|
|
624
|
-
promptInferenceBlockReason = lines.join('\n');
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
if (promptInferenceBlockReason !== null) {
|
|
628
|
-
log.error(`[task:${taskId}]`, `blocked — prompt port inference failed:\n${promptInferenceBlockReason}`);
|
|
629
|
-
state.result = {
|
|
630
|
-
exitCode: -1,
|
|
631
|
-
stdout: '',
|
|
632
|
-
stderr: `[engine] prompt port inference failed:\n${promptInferenceBlockReason}`,
|
|
633
|
-
stdoutPath: null,
|
|
634
|
-
stderrPath: null,
|
|
635
|
-
durationMs: 0,
|
|
636
|
-
sessionId: null,
|
|
637
|
-
normalizedOutput: null,
|
|
638
|
-
failureKind: 'spawn_error',
|
|
639
|
-
outputs: null,
|
|
640
|
-
};
|
|
641
|
-
state.finishedAt = nowISO();
|
|
642
|
-
setTaskStatus(taskId, 'blocked');
|
|
643
|
-
try {
|
|
644
|
-
await fireHook(taskId, 'task_failure');
|
|
645
|
-
}
|
|
646
|
-
catch (hookErr) {
|
|
647
|
-
log.error(`[task:${taskId}]`, `hook execution failed: ${hookErr instanceof Error ? hookErr.message : String(hookErr)}`);
|
|
648
|
-
}
|
|
649
|
-
if (getOnFailure(taskId) === 'stop_all')
|
|
650
|
-
applyStopAll(node.track.id);
|
|
651
|
-
return;
|
|
652
|
-
}
|
|
653
|
-
const bindingResolution = resolveTaskBindingInputs(task, bindingDataMap, node.dependsOn);
|
|
654
|
-
if (bindingResolution.kind === 'blocked') {
|
|
655
|
-
log.error(`[task:${taskId}]`, `blocked — cannot resolve task input bindings:\n${bindingResolution.reason}`);
|
|
656
|
-
state.result = {
|
|
657
|
-
exitCode: -1,
|
|
658
|
-
stdout: '',
|
|
659
|
-
stderr: `[engine] task input binding resolution failed:\n${bindingResolution.reason}`,
|
|
660
|
-
stdoutPath: null,
|
|
661
|
-
stderrPath: null,
|
|
662
|
-
durationMs: 0,
|
|
663
|
-
sessionId: null,
|
|
664
|
-
normalizedOutput: null,
|
|
665
|
-
failureKind: 'spawn_error',
|
|
666
|
-
outputs: null,
|
|
667
|
-
};
|
|
668
|
-
state.finishedAt = nowISO();
|
|
669
|
-
setTaskStatus(taskId, 'blocked');
|
|
670
|
-
try {
|
|
671
|
-
await fireHook(taskId, 'task_failure');
|
|
672
|
-
}
|
|
673
|
-
catch (hookErr) {
|
|
674
|
-
log.error(`[task:${taskId}]`, `hook execution failed: ${hookErr instanceof Error ? hookErr.message : String(hookErr)}`);
|
|
675
|
-
}
|
|
676
|
-
if (getOnFailure(taskId) === 'stop_all')
|
|
677
|
-
applyStopAll(node.track.id);
|
|
678
|
-
return;
|
|
679
|
-
}
|
|
680
|
-
if (bindingResolution.missingOptional.length > 0) {
|
|
681
|
-
log.debug(`[task:${taskId}]`, `optional input bindings unresolved (empty in placeholders): ${bindingResolution.missingOptional.join(', ')}`);
|
|
682
|
-
}
|
|
683
|
-
// Feed effective ports into `resolveTaskInputs` by shallow-cloning
|
|
684
|
-
// the task. Prompt tasks get the inferred ports; Command tasks are
|
|
685
|
-
// unchanged (effectivePorts === task.ports).
|
|
686
|
-
const taskForResolve = effectivePorts === task.ports ? task : { ...task, ports: effectivePorts };
|
|
687
|
-
const inputResolution = resolveTaskInputs(taskForResolve, outputValuesMap, node.dependsOn);
|
|
688
|
-
if (inputResolution.kind === 'blocked') {
|
|
689
|
-
log.error(`[task:${taskId}]`, `blocked — cannot resolve port inputs:\n${inputResolution.reason}`);
|
|
690
|
-
state.result = {
|
|
691
|
-
exitCode: -1,
|
|
692
|
-
stdout: '',
|
|
693
|
-
stderr: `[engine] port input resolution failed:\n${inputResolution.reason}`,
|
|
694
|
-
stdoutPath: null,
|
|
695
|
-
stderrPath: null,
|
|
696
|
-
durationMs: 0,
|
|
697
|
-
sessionId: null,
|
|
698
|
-
normalizedOutput: null,
|
|
699
|
-
failureKind: 'spawn_error',
|
|
700
|
-
outputs: null,
|
|
701
|
-
};
|
|
702
|
-
state.finishedAt = nowISO();
|
|
703
|
-
setTaskStatus(taskId, 'blocked');
|
|
704
|
-
try {
|
|
705
|
-
await fireHook(taskId, 'task_failure');
|
|
706
|
-
}
|
|
707
|
-
catch (hookErr) {
|
|
708
|
-
log.error(`[task:${taskId}]`, `hook execution failed: ${hookErr instanceof Error ? hookErr.message : String(hookErr)}`);
|
|
709
|
-
}
|
|
710
|
-
if (getOnFailure(taskId) === 'stop_all')
|
|
711
|
-
applyStopAll(node.track.id);
|
|
712
|
-
return;
|
|
713
|
-
}
|
|
714
|
-
const resolvedInputs = { ...bindingResolution.inputs, ...inputResolution.inputs };
|
|
715
|
-
resolvedInputsMap.set(taskId, resolvedInputs);
|
|
716
|
-
if (inputResolution.missingOptional.length > 0) {
|
|
717
|
-
log.debug(`[task:${taskId}]`, `optional inputs unresolved (empty in placeholders): ${inputResolution.missingOptional.join(', ')}`);
|
|
718
|
-
}
|
|
719
|
-
if (effectivePorts?.inputs && effectivePorts.inputs.length > 0) {
|
|
720
|
-
log.debug(`[task:${taskId}]`, `resolved inputs: ${JSON.stringify(resolvedInputs)}` +
|
|
721
|
-
(isPromptTask ? ' (inferred from upstream Commands)' : ''));
|
|
722
|
-
}
|
|
723
|
-
// 4. Mark running — set startedAt before emitting so subscribers see a
|
|
724
|
-
// complete task_update (startedAt non-null) on the status transition.
|
|
725
|
-
state.startedAt = nowISO();
|
|
726
|
-
setTaskStatus(taskId, 'running');
|
|
727
|
-
log.info(`[task:${taskId}]`, isCommandTaskConfig(task) ? `running: ${task.command}` : `running (driver task)`);
|
|
728
|
-
// File-only: resolved config for this task
|
|
729
|
-
const resolvedDriver = task.driver ?? track.driver ?? config.driver ?? 'opencode';
|
|
730
|
-
const resolvedModel = task.model ?? track.model ?? config.model ?? '(default)';
|
|
731
|
-
const resolvedPerms = task.permissions ?? track.permissions ?? '(default)';
|
|
732
|
-
const resolvedCwd = task.cwd ?? track.cwd ?? workDir;
|
|
733
|
-
log.debug(`[task:${taskId}]`, `resolved: driver=${resolvedDriver} model=${resolvedModel} cwd=${resolvedCwd}`);
|
|
734
|
-
log.debug(`[task:${taskId}]`, `permissions: ${JSON.stringify(resolvedPerms)}`);
|
|
735
|
-
if (task.continue_from) {
|
|
736
|
-
log.debug(`[task:${taskId}]`, `continue_from: "${task.continue_from}"`);
|
|
737
|
-
}
|
|
738
|
-
if (task.timeout) {
|
|
739
|
-
log.debug(`[task:${taskId}]`, `timeout: ${task.timeout}`);
|
|
740
|
-
}
|
|
741
|
-
try {
|
|
742
|
-
let result;
|
|
743
|
-
const timeoutMs = task.timeout ? parseDuration(task.timeout) : undefined;
|
|
744
|
-
// Stream child stdout/stderr directly to disk in the logger's run dir
|
|
745
|
-
// and keep only a bounded tail in the returned TaskResult. Filenames
|
|
746
|
-
// mirror the existing `.stderr` naming — dots in task ids are replaced
|
|
747
|
-
// so hierarchical ids (e.g. `track1.task2`) map cleanly to a flat dir.
|
|
748
|
-
const fsSafeTaskId = taskId.replace(/\./g, '_');
|
|
749
|
-
const stdoutPath = resolve(log.dir, `${fsSafeTaskId}.stdout`);
|
|
750
|
-
const stderrPath = resolve(log.dir, `${fsSafeTaskId}.stderr`);
|
|
751
|
-
const runOpts = {
|
|
752
|
-
timeoutMs,
|
|
753
|
-
signal: abortController.signal,
|
|
754
|
-
stdoutPath,
|
|
755
|
-
stderrPath,
|
|
756
|
-
};
|
|
757
|
-
if (isCommandTaskConfig(task)) {
|
|
758
|
-
// Substitute `{{inputs.X}}` placeholders into the command
|
|
759
|
-
// string. Tasks with no declared inputs always produce the same
|
|
760
|
-
// string back (no placeholders to match). Unresolved references
|
|
761
|
-
// render empty — validate-raw flags undeclared references as
|
|
762
|
-
// errors, so the only way to land here with an unresolved is an
|
|
763
|
-
// optional input that had no upstream producer and no default,
|
|
764
|
-
// which we surface in the log.
|
|
765
|
-
const { text: expandedCommand, unresolved } = substituteInputs(task.command, resolvedInputs);
|
|
766
|
-
if (unresolved.length > 0) {
|
|
767
|
-
log.debug(`[task:${taskId}]`, `command placeholders rendered empty: ${unresolved.join(', ')}`);
|
|
768
|
-
}
|
|
769
|
-
log.debug(`[task:${taskId}]`, `command: ${expandedCommand}`);
|
|
770
|
-
result = await runCommand(expandedCommand, task.cwd ?? workDir, runOpts);
|
|
771
|
-
}
|
|
772
|
-
else {
|
|
773
|
-
// AI task: apply middleware chain against a structured PromptDocument.
|
|
774
|
-
const driverName = task.driver ?? track.driver ?? config.driver ?? 'opencode';
|
|
775
|
-
const driver = registry.getHandler('drivers', driverName);
|
|
776
|
-
// Substitute placeholders in the user-authored prompt before
|
|
777
|
-
// wrapping into a PromptDocument so middlewares see the
|
|
778
|
-
// already-resolved task text.
|
|
779
|
-
const { text: expandedPrompt, unresolved } = substituteInputs(task.prompt, resolvedInputs);
|
|
780
|
-
if (unresolved.length > 0) {
|
|
781
|
-
log.debug(`[task:${taskId}]`, `prompt placeholders rendered empty: ${unresolved.join(', ')}`);
|
|
782
|
-
}
|
|
783
|
-
const originalLen = expandedPrompt.length;
|
|
784
|
-
let doc = promptDocumentFromString(expandedPrompt);
|
|
785
|
-
// Prepend port-related context blocks so the model sees them
|
|
786
|
-
// before any middleware-added retrieval / memory blocks. Order
|
|
787
|
-
// matters: [Output Format] first (sets the deliverable), then
|
|
788
|
-
// [Inputs] (the concrete data to operate on). Empty blocks are
|
|
789
|
-
// filtered out — tasks without ports get no extra blocks at all.
|
|
790
|
-
const outputFormatBlock = renderOutputSchemaBlock(effectivePorts?.outputs);
|
|
791
|
-
if (outputFormatBlock) {
|
|
792
|
-
doc = prependContext(doc, outputFormatBlock);
|
|
793
|
-
}
|
|
794
|
-
const inputsBlock = renderInputsBlock(effectivePorts?.inputs, resolvedInputs);
|
|
795
|
-
if (inputsBlock) {
|
|
796
|
-
doc = prependContext(doc, inputsBlock);
|
|
797
|
-
}
|
|
798
|
-
const mws = task.middlewares !== undefined ? task.middlewares : track.middlewares;
|
|
799
|
-
if (mws && mws.length > 0) {
|
|
800
|
-
log.debug(`[task:${taskId}]`, `middleware chain: ${mws.map((m) => m.type).join(' → ')}`);
|
|
801
|
-
const mwCtx = {
|
|
802
|
-
task,
|
|
803
|
-
track,
|
|
804
|
-
workDir: task.cwd ?? workDir,
|
|
805
|
-
};
|
|
806
|
-
for (const mwConfig of mws) {
|
|
807
|
-
const mwPlugin = registry.getHandler('middlewares', mwConfig.type);
|
|
808
|
-
const beforeBlocks = doc.contexts.length;
|
|
809
|
-
const beforeLen = serializePromptDocument(doc).length;
|
|
810
|
-
// Prefer the structured API. Fall back to the legacy
|
|
811
|
-
// `enhance(string) → string` path so v0.x plugins keep
|
|
812
|
-
// working — that fallback loses context structure (the
|
|
813
|
-
// middleware's output becomes the new task body) but never
|
|
814
|
-
// silently drops content.
|
|
815
|
-
if (typeof mwPlugin.enhanceDoc === 'function') {
|
|
816
|
-
const next = await mwPlugin.enhanceDoc(doc, mwConfig, mwCtx);
|
|
817
|
-
if (!next ||
|
|
818
|
-
typeof next !== 'object' ||
|
|
819
|
-
!Array.isArray(next.contexts) ||
|
|
820
|
-
typeof next.task !== 'string') {
|
|
821
|
-
throw new Error(`middleware "${mwConfig.type}".enhanceDoc() returned a malformed PromptDocument`);
|
|
822
|
-
}
|
|
823
|
-
doc = next;
|
|
824
|
-
}
|
|
825
|
-
else if (typeof mwPlugin.enhance === 'function') {
|
|
826
|
-
const asString = serializePromptDocument(doc);
|
|
827
|
-
const next = await mwPlugin.enhance(asString, mwConfig, mwCtx);
|
|
828
|
-
// R3: a middleware that returns undefined / null / a non-string
|
|
829
|
-
// would silently corrupt the prompt. Fail loud.
|
|
830
|
-
if (typeof next !== 'string') {
|
|
831
|
-
throw new Error(`middleware "${mwConfig.type}".enhance() returned ${next === null ? 'null' : typeof next}, expected string`);
|
|
832
|
-
}
|
|
833
|
-
// Legacy fallback: collapse the returned string into a
|
|
834
|
-
// fresh doc. Earlier structure is folded into the string
|
|
835
|
-
// (serializePromptDocument just ran), so bytes the driver
|
|
836
|
-
// sees match the old string pipeline.
|
|
837
|
-
doc = { contexts: [], task: next };
|
|
838
|
-
}
|
|
839
|
-
else {
|
|
840
|
-
throw new Error(`middleware "${mwConfig.type}" provides neither enhanceDoc nor enhance`);
|
|
841
|
-
}
|
|
842
|
-
const afterLen = serializePromptDocument(doc).length;
|
|
843
|
-
const addedBlocks = doc.contexts.length - beforeBlocks;
|
|
844
|
-
log.debug(`[task:${taskId}]`, ` ${mwConfig.type}: ${beforeLen} → ${afterLen} chars` +
|
|
845
|
-
(addedBlocks > 0
|
|
846
|
-
? ` (+${addedBlocks} context block${addedBlocks > 1 ? 's' : ''})`
|
|
847
|
-
: ''));
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
const prompt = serializePromptDocument(doc);
|
|
851
|
-
log.debug(`[task:${taskId}]`, `prompt: ${originalLen} chars (final: ${prompt.length} chars, ${doc.contexts.length} block${doc.contexts.length === 1 ? '' : 's'})`);
|
|
852
|
-
log.quiet(`--- prompt (final) ---\n${clip(prompt)}\n--- end prompt ---`, taskId);
|
|
853
|
-
// H1: hand the driver a continue_from that has already been
|
|
854
|
-
// qualified by dag.ts. Without this, drivers like codex/opencode/
|
|
855
|
-
// claude-code look up maps directly with
|
|
856
|
-
// the user's raw (possibly bare) string, which races whenever two
|
|
857
|
-
// tracks share a task name. dag.ts has the only authoritative
|
|
858
|
-
// resolver, so we use its precomputed answer here.
|
|
859
|
-
// Drivers key sessionMap/normalizedMap by fully-qualified id. buildDag
|
|
860
|
-
// guarantees `resolvedContinueFrom` is set for every task that has a
|
|
861
|
-
// `continue_from`, so if we see the bare form here something upstream
|
|
862
|
-
// is broken — fail loud instead of silently miskeying the lookup.
|
|
863
|
-
if (task.continue_from && !node.resolvedContinueFrom) {
|
|
864
|
-
throw new Error(`Internal: task "${taskId}" has continue_from "${task.continue_from}" ` +
|
|
865
|
-
`but no resolvedContinueFrom. buildDag should have qualified it.`);
|
|
866
|
-
}
|
|
867
|
-
const enrichedTask = {
|
|
868
|
-
...task,
|
|
869
|
-
prompt,
|
|
870
|
-
continue_from: node.resolvedContinueFrom,
|
|
871
|
-
// Hand the driver the EFFECTIVE port schema rather than the
|
|
872
|
-
// raw task.ports. For Prompt tasks this is the one inferred
|
|
873
|
-
// from neighbor Commands; Command tasks are unchanged.
|
|
874
|
-
// Drivers that introspect ports (e.g. to annotate a system
|
|
875
|
-
// prompt with the I/O contract) otherwise saw `undefined`
|
|
876
|
-
// for every prompt and had no way to know the contract.
|
|
877
|
-
ports: effectivePorts,
|
|
878
|
-
};
|
|
879
|
-
const driverCtx = {
|
|
880
|
-
sessionMap,
|
|
881
|
-
normalizedMap,
|
|
882
|
-
workDir: task.cwd ?? workDir,
|
|
883
|
-
// Structured view for drivers that want fine-grained control
|
|
884
|
-
// over serialization (e.g. inserting [Previous Output] between
|
|
885
|
-
// contexts and task). Drivers that read task.prompt see the
|
|
886
|
-
// default serialization and need no changes.
|
|
887
|
-
promptDoc: doc,
|
|
888
|
-
// Ports feature: resolved input values keyed by port name,
|
|
889
|
-
// already coerced to the declared port type. Drivers that
|
|
890
|
-
// need to re-substitute placeholders inside a custom envelope
|
|
891
|
-
// can read this and call `substituteInputs`; most drivers can
|
|
892
|
-
// ignore it because the engine has already expanded
|
|
893
|
-
// `{{inputs.X}}` into `task.prompt` upstream.
|
|
894
|
-
inputs: resolvedInputs,
|
|
895
|
-
};
|
|
896
|
-
const spec = await driver.buildCommand(enrichedTask, track, driverCtx);
|
|
897
|
-
log.debug(`[task:${taskId}]`, `driver=${driverName}`);
|
|
898
|
-
log.debug(`[task:${taskId}]`, `spawn args: ${JSON.stringify(spec.args)}`);
|
|
899
|
-
if (spec.cwd)
|
|
900
|
-
log.debug(`[task:${taskId}]`, `spawn cwd: ${spec.cwd}`);
|
|
901
|
-
if (spec.env)
|
|
902
|
-
log.debug(`[task:${taskId}]`, `spawn env overrides: ${Object.keys(spec.env).join(', ')}`);
|
|
903
|
-
if (spec.stdin)
|
|
904
|
-
log.debug(`[task:${taskId}]`, `spawn stdin: ${spec.stdin.length} chars`);
|
|
905
|
-
result = await runSpawn(spec, driver, runOpts);
|
|
906
|
-
}
|
|
907
|
-
// 6. Determine terminal status (without emitting yet — result must be complete first)
|
|
908
|
-
// H2: branch on failureKind so spawn errors no longer masquerade as
|
|
909
|
-
// timeouts. Old runners that don't set failureKind still work — we
|
|
910
|
-
// fall back to the historical `exitCode === -1 → timeout` heuristic so
|
|
911
|
-
// pre-existing third-party drivers don't regress.
|
|
912
|
-
let terminalStatus;
|
|
913
|
-
const kind = result.failureKind;
|
|
914
|
-
if (kind === 'timeout') {
|
|
915
|
-
terminalStatus = 'timeout';
|
|
916
|
-
}
|
|
917
|
-
else if (kind === 'spawn_error') {
|
|
918
|
-
terminalStatus = 'failed';
|
|
919
|
-
}
|
|
920
|
-
else if (kind === undefined && result.exitCode === -1) {
|
|
921
|
-
// Legacy path: pre-H2 driver returned -1 with no kind. Treat as
|
|
922
|
-
// timeout for backward compatibility (the previous behaviour).
|
|
923
|
-
terminalStatus = 'timeout';
|
|
924
|
-
}
|
|
925
|
-
else if (result.exitCode !== 0) {
|
|
926
|
-
terminalStatus = 'failed';
|
|
927
|
-
}
|
|
928
|
-
else if (task.completion) {
|
|
929
|
-
const plugin = registry.getHandler('completions', task.completion.type);
|
|
930
|
-
const completionCtx = { workDir: task.cwd ?? workDir, signal: abortController.signal };
|
|
931
|
-
const passed = await plugin.check(task.completion, result, completionCtx);
|
|
932
|
-
// R4: strict boolean check. Truthy strings/numbers used to be coerced
|
|
933
|
-
// to success — a check returning "ok" would let a failing task pass.
|
|
934
|
-
if (typeof passed !== 'boolean') {
|
|
935
|
-
throw new Error(`completion "${task.completion.type}".check() returned ${passed === null ? 'null' : typeof passed}, expected boolean`);
|
|
936
|
-
}
|
|
937
|
-
terminalStatus = passed ? 'success' : 'failed';
|
|
938
|
-
}
|
|
939
|
-
else {
|
|
940
|
-
terminalStatus = 'success';
|
|
941
|
-
}
|
|
942
|
-
// Extract declared outputs from the task's output stream. Only
|
|
943
|
-
// meaningful on success — a failed task's output is whatever the
|
|
944
|
-
// child happened to emit before exiting, and downstream tasks
|
|
945
|
-
// shouldn't receive partial data.
|
|
946
|
-
let extractedOutputs = null;
|
|
947
|
-
if (terminalStatus === 'success') {
|
|
948
|
-
const looseExtraction = extractTaskBindingOutputs(task.outputs, result.stdout, result.stderr, result.normalizedOutput);
|
|
949
|
-
if (task.outputs && Object.keys(task.outputs).length > 0) {
|
|
950
|
-
extractedOutputs = looseExtraction.outputs;
|
|
951
|
-
log.debug(`[task:${taskId}]`, `extracted binding outputs: ${JSON.stringify(looseExtraction.outputs)}`);
|
|
952
|
-
if (looseExtraction.diagnostic) {
|
|
953
|
-
log.debug(`[task:${taskId}]`, looseExtraction.diagnostic);
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
// Prompt tasks use inferred ports (from direct-downstream Command
|
|
957
|
-
// inputs); Command tasks use their declared ports. Either way,
|
|
958
|
-
// `extractTaskOutputs` is a no-op when there are no declared
|
|
959
|
-
// outputs to pull, so pre-ports tasks pay nothing for this call.
|
|
960
|
-
const extraction = extractTaskOutputs(effectivePorts, result.stdout, result.normalizedOutput);
|
|
961
|
-
if (effectivePorts?.outputs && effectivePorts.outputs.length > 0) {
|
|
962
|
-
extractedOutputs = { ...(extractedOutputs ?? {}), ...extraction.outputs };
|
|
963
|
-
log.debug(`[task:${taskId}]`, `extracted outputs: ${JSON.stringify(extraction.outputs)}` +
|
|
964
|
-
(isPromptTask ? ' (inferred from downstream Commands)' : ''));
|
|
965
|
-
if (extraction.diagnostic) {
|
|
966
|
-
log.error(`[task:${taskId}]`, extraction.diagnostic);
|
|
967
|
-
const note = `\n[engine] ${extraction.diagnostic}`;
|
|
968
|
-
result = { ...result, stderr: result.stderr + note };
|
|
969
|
-
}
|
|
970
|
-
}
|
|
971
|
-
}
|
|
972
|
-
// Attach outputs to the result (null when task has no declared
|
|
973
|
-
// outputs or extraction failed entirely). Consumers of TaskResult
|
|
974
|
-
// — hooks, wire events, test assertions — all go through this
|
|
975
|
-
// one field rather than re-running extraction.
|
|
976
|
-
result = { ...result, outputs: extractedOutputs };
|
|
977
|
-
if (extractedOutputs !== null) {
|
|
978
|
-
outputValuesMap.set(taskId, extractedOutputs);
|
|
979
|
-
}
|
|
980
|
-
bindingDataMap.set(taskId, {
|
|
981
|
-
outputs: extractedOutputs,
|
|
982
|
-
stdout: result.stdout,
|
|
983
|
-
stderr: result.stderr,
|
|
984
|
-
normalizedOutput: result.normalizedOutput,
|
|
985
|
-
exitCode: result.exitCode,
|
|
986
|
-
});
|
|
987
|
-
// Store normalized text separately (in-memory) for continue_from handoff.
|
|
988
|
-
// R15: clip oversized values so a runaway parseResult can't accumulate
|
|
989
|
-
// hundreds of MB across tasks.
|
|
990
|
-
if (result.normalizedOutput !== null) {
|
|
991
|
-
const clipped = result.normalizedOutput.length > MAX_NORMALIZED_BYTES
|
|
992
|
-
? result.normalizedOutput.slice(0, MAX_NORMALIZED_BYTES) +
|
|
993
|
-
`\n[…clipped at ${MAX_NORMALIZED_BYTES} bytes]`
|
|
994
|
-
: result.normalizedOutput;
|
|
995
|
-
normalizedMap.set(taskId, clipped);
|
|
996
|
-
}
|
|
997
|
-
// Note: stderr is already persisted by runner.ts as it streams; the
|
|
998
|
-
// old "write full string after the fact" block is gone — that's what
|
|
999
|
-
// the streaming rewrite fixed (unbounded in-memory buffering).
|
|
1000
|
-
if (result.sessionId) {
|
|
1001
|
-
// H1: qualified-only key.
|
|
1002
|
-
sessionMap.set(taskId, result.sessionId);
|
|
1003
|
-
}
|
|
1004
|
-
// Set result and finishedAt before emitting terminal status so listeners see complete state
|
|
1005
|
-
state.result = result;
|
|
1006
|
-
state.finishedAt = nowISO();
|
|
1007
|
-
setTaskStatus(taskId, terminalStatus);
|
|
1008
|
-
// Log task outcome with relevant details
|
|
1009
|
-
const durSec = (result.durationMs / 1000).toFixed(1);
|
|
1010
|
-
if (terminalStatus === 'success') {
|
|
1011
|
-
log.info(`[task:${taskId}]`, `success (${durSec}s)`);
|
|
1012
|
-
}
|
|
1013
|
-
else {
|
|
1014
|
-
log.error(`[task:${taskId}]`, `${terminalStatus} exit=${result.exitCode} duration=${durSec}s`);
|
|
1015
|
-
if (result.stderr) {
|
|
1016
|
-
const tail = tailLines(result.stderr, 10);
|
|
1017
|
-
log.error(`[task:${taskId}]`, `stderr tail:\n${tail}`);
|
|
1018
|
-
}
|
|
1019
|
-
}
|
|
1020
|
-
// File-only: byte counts (prefer full totals from the runner over the
|
|
1021
|
-
// bounded tail length so oversized outputs show their real size) +
|
|
1022
|
-
// paths to the on-disk full copies.
|
|
1023
|
-
const stdoutSize = result.stdoutBytes ?? result.stdout.length;
|
|
1024
|
-
const stderrSize = result.stderrBytes ?? result.stderr.length;
|
|
1025
|
-
log.debug(`[task:${taskId}]`, `stdout: ${stdoutSize} bytes, stderr: ${stderrSize} bytes`);
|
|
1026
|
-
if (result.sessionId) {
|
|
1027
|
-
log.debug(`[task:${taskId}]`, `sessionId: ${result.sessionId}`);
|
|
1028
|
-
}
|
|
1029
|
-
if (result.stdoutPath) {
|
|
1030
|
-
log.debug(`[task:${taskId}]`, `wrote stdout: ${result.stdoutPath}`);
|
|
1031
|
-
}
|
|
1032
|
-
if (result.stderrPath) {
|
|
1033
|
-
log.debug(`[task:${taskId}]`, `wrote stderr: ${result.stderrPath}`);
|
|
1034
|
-
}
|
|
1035
|
-
if (result.stdout) {
|
|
1036
|
-
log.quiet(`--- stdout (${taskId}) ---\n${clip(result.stdout)}\n--- end stdout ---`, taskId);
|
|
1037
|
-
}
|
|
1038
|
-
if (result.stderr) {
|
|
1039
|
-
log.quiet(`--- stderr (${taskId}) ---\n${clip(result.stderr)}\n--- end stderr ---`, taskId);
|
|
1040
|
-
}
|
|
1041
|
-
if (task.completion) {
|
|
1042
|
-
log.debug(`[task:${taskId}]`, `completion check: type=${task.completion.type} result=${terminalStatus}`);
|
|
1043
|
-
}
|
|
1044
|
-
}
|
|
1045
|
-
catch (err) {
|
|
1046
|
-
const errMsg = err instanceof Error ? (err.stack ?? err.message) : String(err);
|
|
1047
|
-
log.error(`[task:${taskId}]`, `failed before execution: ${errMsg}`);
|
|
1048
|
-
state.result = {
|
|
1049
|
-
exitCode: -1,
|
|
1050
|
-
stdout: '',
|
|
1051
|
-
stderr: errMsg,
|
|
1052
|
-
stdoutPath: null,
|
|
1053
|
-
stderrPath: null,
|
|
1054
|
-
stdoutBytes: 0,
|
|
1055
|
-
stderrBytes: errMsg.length,
|
|
1056
|
-
durationMs: 0,
|
|
1057
|
-
sessionId: null,
|
|
1058
|
-
normalizedOutput: null,
|
|
1059
|
-
// H2: Engine-level pre-execution errors (driver throw, middleware
|
|
1060
|
-
// throw, getHandler 404) classify as spawn_error — the process never
|
|
1061
|
-
// ran, so calling them "timeout" was actively misleading.
|
|
1062
|
-
failureKind: 'spawn_error',
|
|
1063
|
-
};
|
|
1064
|
-
state.finishedAt = nowISO();
|
|
1065
|
-
setTaskStatus(taskId, 'failed');
|
|
1066
|
-
}
|
|
1067
|
-
// 7. Fire hooks
|
|
1068
|
-
const finalStatus = state.status;
|
|
1069
|
-
try {
|
|
1070
|
-
await fireHook(taskId, finalStatus === 'success' ? 'task_success' : 'task_failure');
|
|
1071
|
-
}
|
|
1072
|
-
catch (hookErr) {
|
|
1073
|
-
log.error(`[task:${taskId}]`, `hook execution failed: ${hookErr instanceof Error ? hookErr.message : String(hookErr)}`);
|
|
1074
|
-
}
|
|
1075
|
-
// 8. Handle stop_all for failure states
|
|
1076
|
-
if (finalStatus !== 'success' && getOnFailure(taskId) === 'stop_all') {
|
|
1077
|
-
applyStopAll(node.track.id);
|
|
1078
|
-
}
|
|
1079
|
-
}
|
|
1080
191
|
// ── Event loop ──
|
|
1081
192
|
// Each task is launched as soon as ALL its deps reach a terminal state.
|
|
1082
193
|
// We track in-flight tasks in `running` so a task completing mid-batch
|
|
1083
194
|
// immediately unblocks its dependents without waiting for sibling tasks.
|
|
1084
195
|
const running = new Map();
|
|
1085
196
|
try {
|
|
1086
|
-
while (abortReason === null) {
|
|
197
|
+
while (ctx.abortReason === null) {
|
|
1087
198
|
// Launch every task whose deps are all terminal and that isn't already in-flight
|
|
1088
|
-
for (const
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
const p = processTask(id).finally(() => running.delete(id));
|
|
199
|
+
for (const id of findLaunchableTasks(ctx, new Set(running.keys()))) {
|
|
200
|
+
const p = executeTask({
|
|
201
|
+
taskId: id,
|
|
202
|
+
ctx,
|
|
203
|
+
registry,
|
|
204
|
+
log,
|
|
205
|
+
approvalGateway,
|
|
206
|
+
}).finally(() => running.delete(id));
|
|
1097
207
|
running.set(id, p);
|
|
1098
208
|
}
|
|
1099
209
|
// All tasks terminal — done
|
|
1100
|
-
if (
|
|
210
|
+
if (allTasksTerminal(ctx))
|
|
1101
211
|
break;
|
|
1102
212
|
if (running.size === 0) {
|
|
1103
213
|
// Nothing in-flight but non-terminal tasks exist (e.g. trigger-wait states
|
|
@@ -1109,19 +219,14 @@ export async function runPipeline(config, workDir, options = {}) {
|
|
|
1109
219
|
await Promise.race(running.values());
|
|
1110
220
|
}
|
|
1111
221
|
}
|
|
1112
|
-
if (abortReason !== null) {
|
|
222
|
+
if (ctx.abortReason !== null) {
|
|
1113
223
|
// Wait for in-flight tasks to honour the abort signal before marking states.
|
|
1114
224
|
if (running.size > 0)
|
|
1115
225
|
await Promise.allSettled(running.values());
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
// statuses remaining here are waiting/idle tasks that were never started.
|
|
1121
|
-
state.finishedAt = nowISO();
|
|
1122
|
-
setTaskStatus(id, 'skipped');
|
|
1123
|
-
}
|
|
1124
|
-
}
|
|
226
|
+
// By the time allSettled resolves, processTask's try/finally has already
|
|
227
|
+
// set running tasks to success/failed/timeout. The only non-terminal
|
|
228
|
+
// statuses remaining here are waiting/idle tasks that were never started.
|
|
229
|
+
skipNonTerminalTasks(ctx);
|
|
1125
230
|
}
|
|
1126
231
|
}
|
|
1127
232
|
finally {
|
|
@@ -1141,53 +246,33 @@ export async function runPipeline(config, workDir, options = {}) {
|
|
|
1141
246
|
unsubscribeApprovals();
|
|
1142
247
|
}
|
|
1143
248
|
// ── Summary ──
|
|
1144
|
-
const summary =
|
|
1145
|
-
for (const [, state] of states) {
|
|
1146
|
-
summary.total++;
|
|
1147
|
-
switch (state.status) {
|
|
1148
|
-
case 'success':
|
|
1149
|
-
summary.success++;
|
|
1150
|
-
break;
|
|
1151
|
-
case 'failed':
|
|
1152
|
-
summary.failed++;
|
|
1153
|
-
break;
|
|
1154
|
-
case 'skipped':
|
|
1155
|
-
summary.skipped++;
|
|
1156
|
-
break;
|
|
1157
|
-
case 'timeout':
|
|
1158
|
-
summary.timeout++;
|
|
1159
|
-
break;
|
|
1160
|
-
case 'blocked':
|
|
1161
|
-
summary.blocked++;
|
|
1162
|
-
break;
|
|
1163
|
-
}
|
|
1164
|
-
}
|
|
249
|
+
const summary = summarizeStates(ctx.states);
|
|
1165
250
|
const finishedAt = nowISO();
|
|
1166
251
|
const durationMs = new Date(finishedAt).getTime() - new Date(startedAt).getTime();
|
|
1167
|
-
if (abortReason !== null) {
|
|
1168
|
-
const reasonText = abortReason === 'timeout'
|
|
252
|
+
if (ctx.abortReason !== null) {
|
|
253
|
+
const reasonText = ctx.abortReason === 'timeout'
|
|
1169
254
|
? 'Pipeline timeout exceeded'
|
|
1170
|
-
: abortReason === 'stop_all'
|
|
255
|
+
: ctx.abortReason === 'stop_all'
|
|
1171
256
|
? 'Pipeline stopped (on_failure: stop_all)'
|
|
1172
257
|
: 'Pipeline aborted by host';
|
|
1173
|
-
await executeHook(config.hooks, 'pipeline_error', buildPipelineErrorContext(pipelineInfo, reasonText, undefined, abortReason), workDir);
|
|
258
|
+
await executeHook(config.hooks, 'pipeline_error', buildPipelineErrorContext(pipelineInfo, reasonText, undefined, ctx.abortReason), workDir);
|
|
1174
259
|
}
|
|
1175
260
|
else {
|
|
1176
261
|
await executeHook(config.hooks, 'pipeline_complete', buildPipelineCompleteContext({ ...pipelineInfo, finished_at: finishedAt, duration_ms: durationMs }, summary), workDir);
|
|
1177
262
|
}
|
|
1178
|
-
const allSuccess = abortReason === null &&
|
|
263
|
+
const allSuccess = ctx.abortReason === null &&
|
|
1179
264
|
summary.failed === 0 &&
|
|
1180
265
|
summary.timeout === 0 &&
|
|
1181
266
|
summary.blocked === 0;
|
|
1182
267
|
log.section('Pipeline summary');
|
|
1183
|
-
log.quiet(`status: ${abortReason !== null ? `aborted (${abortReason})` : 'completed'}`);
|
|
268
|
+
log.quiet(`status: ${ctx.abortReason !== null ? `aborted (${ctx.abortReason})` : 'completed'}`);
|
|
1184
269
|
log.quiet(`duration: ${(durationMs / 1000).toFixed(1)}s`);
|
|
1185
270
|
log.quiet(`counts: total=${summary.total} success=${summary.success} ` +
|
|
1186
271
|
`failed=${summary.failed} skipped=${summary.skipped} ` +
|
|
1187
272
|
`timeout=${summary.timeout} blocked=${summary.blocked}`);
|
|
1188
273
|
log.quiet('');
|
|
1189
274
|
log.quiet('per-task:');
|
|
1190
|
-
for (const [id, state] of states) {
|
|
275
|
+
for (const [id, state] of ctx.states) {
|
|
1191
276
|
const dur = state.result?.durationMs != null ? `${(state.result.durationMs / 1000).toFixed(1)}s` : '-';
|
|
1192
277
|
const exit = state.result?.exitCode ?? '-';
|
|
1193
278
|
log.quiet(` ${state.status.padEnd(8)} ${id} (exit=${exit}, ${dur})`);
|
|
@@ -1196,8 +281,8 @@ export async function runPipeline(config, workDir, options = {}) {
|
|
|
1196
281
|
log.info('[pipeline]', `Total: ${summary.total} | Success: ${summary.success} | Failed: ${summary.failed} | Skipped: ${summary.skipped} | Timeout: ${summary.timeout} | Blocked: ${summary.blocked}`);
|
|
1197
282
|
log.info('[pipeline]', `Duration: ${(durationMs / 1000).toFixed(1)}s`);
|
|
1198
283
|
log.info('[pipeline]', `Log: ${log.path}`);
|
|
1199
|
-
emit({ type: 'run_end', runId, success: allSuccess, abortReason });
|
|
1200
|
-
return { success: allSuccess, runId, logPath: log.path, summary, states: freezeStates(states) };
|
|
284
|
+
ctx.emit({ type: 'run_end', runId, success: allSuccess, abortReason: ctx.abortReason });
|
|
285
|
+
return { success: allSuccess, runId, logPath: log.path, summary, states: freezeStates(ctx.states) };
|
|
1201
286
|
}
|
|
1202
287
|
finally {
|
|
1203
288
|
// Close the persistent log file handle before pruning.
|
|
@@ -1209,56 +294,4 @@ export async function runPipeline(config, workDir, options = {}) {
|
|
|
1209
294
|
}
|
|
1210
295
|
}
|
|
1211
296
|
}
|
|
1212
|
-
/**
|
|
1213
|
-
* Delete the oldest subdirectories under `logsDir`, keeping only the most recent `keep`
|
|
1214
|
-
* total runs (including the currently-live run identified by `excludeRunId`).
|
|
1215
|
-
* Directories are sorted lexicographically; because runIds are prefixed with a base-36
|
|
1216
|
-
* timestamp, lexicographic order equals chronological order.
|
|
1217
|
-
*
|
|
1218
|
-
* `excludeRunId` is always skipped from deletion even if it would otherwise be pruned —
|
|
1219
|
-
* this prevents a concurrent run from removing a live log directory that is still in use.
|
|
1220
|
-
*
|
|
1221
|
-
* D10: The live run occupies one slot out of `keep`, so the maximum number of
|
|
1222
|
-
* *historical* dirs to retain is `keep - 1`. Without this adjustment the function
|
|
1223
|
-
* kept `keep` historical dirs plus 1 live dir = `keep + 1` total on disk.
|
|
1224
|
-
*/
|
|
1225
|
-
async function pruneLogDirs(logsDir, keep, excludeRunId) {
|
|
1226
|
-
let entries;
|
|
1227
|
-
try {
|
|
1228
|
-
entries = await readdir(logsDir);
|
|
1229
|
-
}
|
|
1230
|
-
catch {
|
|
1231
|
-
return; // logsDir doesn't exist yet — nothing to prune
|
|
1232
|
-
}
|
|
1233
|
-
// Only consider directories that look like run IDs (run_<...>), excluding the live run.
|
|
1234
|
-
const runDirs = entries.filter((e) => e.startsWith('run_') && e !== excludeRunId).sort();
|
|
1235
|
-
// keep - 1 historical slots (1 slot is reserved for the live excludeRunId).
|
|
1236
|
-
const historyKeep = Math.max(0, keep - 1);
|
|
1237
|
-
const toDelete = runDirs.slice(0, Math.max(0, runDirs.length - historyKeep));
|
|
1238
|
-
await Promise.all(toDelete.map((dir) => rm(resolve(logsDir, dir), { recursive: true, force: true }).catch(() => {
|
|
1239
|
-
// Ignore deletion errors — stale dirs are better than a crash
|
|
1240
|
-
})));
|
|
1241
|
-
}
|
|
1242
|
-
function isTerminal(status) {
|
|
1243
|
-
return (status === 'success' ||
|
|
1244
|
-
status === 'failed' ||
|
|
1245
|
-
status === 'timeout' ||
|
|
1246
|
-
status === 'skipped' ||
|
|
1247
|
-
status === 'blocked');
|
|
1248
|
-
}
|
|
1249
|
-
/** Return a deep-copied, caller-safe snapshot of the states map. */
|
|
1250
|
-
function freezeStates(states) {
|
|
1251
|
-
const copy = new Map();
|
|
1252
|
-
for (const [id, s] of states) {
|
|
1253
|
-
copy.set(id, {
|
|
1254
|
-
config: { ...s.config },
|
|
1255
|
-
trackConfig: { ...s.trackConfig },
|
|
1256
|
-
status: s.status,
|
|
1257
|
-
result: s.result ? { ...s.result } : null,
|
|
1258
|
-
startedAt: s.startedAt,
|
|
1259
|
-
finishedAt: s.finishedAt,
|
|
1260
|
-
});
|
|
1261
|
-
}
|
|
1262
|
-
return copy;
|
|
1263
|
-
}
|
|
1264
297
|
//# sourceMappingURL=engine.js.map
|