@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.
Files changed (125) hide show
  1. package/README.md +56 -15
  2. package/dist/bootstrap.d.ts +6 -6
  3. package/dist/bootstrap.d.ts.map +1 -1
  4. package/dist/bootstrap.js +5 -6
  5. package/dist/bootstrap.js.map +1 -1
  6. package/dist/config.d.ts +8 -0
  7. package/dist/config.d.ts.map +1 -0
  8. package/dist/config.js +5 -0
  9. package/dist/config.js.map +1 -0
  10. package/dist/core/dataflow.d.ts +23 -0
  11. package/dist/core/dataflow.d.ts.map +1 -0
  12. package/dist/core/dataflow.js +99 -0
  13. package/dist/core/dataflow.js.map +1 -0
  14. package/dist/core/log-prune.d.ts +16 -0
  15. package/dist/core/log-prune.d.ts.map +1 -0
  16. package/dist/core/log-prune.js +34 -0
  17. package/dist/core/log-prune.js.map +1 -0
  18. package/dist/core/preflight.d.ts +13 -0
  19. package/dist/core/preflight.d.ts.map +1 -0
  20. package/dist/core/preflight.js +61 -0
  21. package/dist/core/preflight.js.map +1 -0
  22. package/dist/core/run-context.d.ts +52 -0
  23. package/dist/core/run-context.d.ts.map +1 -0
  24. package/dist/core/run-context.js +156 -0
  25. package/dist/core/run-context.js.map +1 -0
  26. package/dist/core/run-state.d.ts +25 -0
  27. package/dist/core/run-state.d.ts.map +1 -0
  28. package/dist/core/run-state.js +93 -0
  29. package/dist/core/run-state.js.map +1 -0
  30. package/dist/core/scheduler.d.ts +13 -0
  31. package/dist/core/scheduler.d.ts.map +1 -0
  32. package/dist/core/scheduler.js +35 -0
  33. package/dist/core/scheduler.js.map +1 -0
  34. package/dist/core/task-executor.d.ts +13 -0
  35. package/dist/core/task-executor.d.ts.map +1 -0
  36. package/dist/core/task-executor.js +623 -0
  37. package/dist/core/task-executor.js.map +1 -0
  38. package/dist/core/trigger-errors.d.ts +9 -0
  39. package/dist/core/trigger-errors.d.ts.map +1 -0
  40. package/dist/core/trigger-errors.js +15 -0
  41. package/dist/core/trigger-errors.js.map +1 -0
  42. package/dist/engine.d.ts +6 -14
  43. package/dist/engine.d.ts.map +1 -1
  44. package/dist/engine.js +68 -1035
  45. package/dist/engine.js.map +1 -1
  46. package/dist/index.d.ts +9 -0
  47. package/dist/index.d.ts.map +1 -0
  48. package/dist/index.js +6 -0
  49. package/dist/index.js.map +1 -0
  50. package/dist/pipeline-definition.d.ts +3 -0
  51. package/dist/pipeline-definition.d.ts.map +1 -0
  52. package/dist/pipeline-definition.js +4 -0
  53. package/dist/pipeline-definition.js.map +1 -0
  54. package/dist/pipeline-runner.d.ts +2 -1
  55. package/dist/pipeline-runner.d.ts.map +1 -1
  56. package/dist/pipeline-runner.js +2 -2
  57. package/dist/pipeline-runner.js.map +1 -1
  58. package/dist/plugins.d.ts +5 -0
  59. package/dist/plugins.d.ts.map +1 -0
  60. package/dist/plugins.js +3 -0
  61. package/dist/plugins.js.map +1 -0
  62. package/dist/ports.d.ts +4 -0
  63. package/dist/ports.d.ts.map +1 -1
  64. package/dist/ports.js +27 -4
  65. package/dist/ports.js.map +1 -1
  66. package/dist/registry.d.ts +3 -19
  67. package/dist/registry.d.ts.map +1 -1
  68. package/dist/registry.js +7 -35
  69. package/dist/registry.js.map +1 -1
  70. package/dist/tagma.d.ts +24 -0
  71. package/dist/tagma.d.ts.map +1 -0
  72. package/dist/tagma.js +23 -0
  73. package/dist/tagma.js.map +1 -0
  74. package/dist/utils-api.d.ts +2 -0
  75. package/dist/utils-api.d.ts.map +1 -0
  76. package/dist/utils-api.js +2 -0
  77. package/dist/utils-api.js.map +1 -0
  78. package/dist/validate-raw.d.ts +4 -4
  79. package/dist/validate-raw.js +91 -132
  80. package/dist/validate-raw.js.map +1 -1
  81. package/dist/yaml.d.ts +4 -0
  82. package/dist/yaml.d.ts.map +1 -0
  83. package/dist/yaml.js +3 -0
  84. package/dist/yaml.js.map +1 -0
  85. package/package.json +53 -8
  86. package/src/bootstrap.ts +6 -6
  87. package/src/config.ts +26 -0
  88. package/src/core/dataflow.test.ts +166 -0
  89. package/src/core/dataflow.ts +161 -0
  90. package/src/core/log-prune.test.ts +58 -0
  91. package/src/core/log-prune.ts +43 -0
  92. package/src/core/preflight.test.ts +49 -0
  93. package/src/core/preflight.ts +89 -0
  94. package/src/core/run-context.test.ts +244 -0
  95. package/src/core/run-context.ts +207 -0
  96. package/src/core/run-state.test.ts +98 -0
  97. package/src/core/run-state.ts +122 -0
  98. package/src/core/scheduler.test.ts +83 -0
  99. package/src/core/scheduler.ts +42 -0
  100. package/src/core/task-executor.ts +769 -0
  101. package/src/core/trigger-errors.ts +15 -0
  102. package/src/engine-ports-mixed.test.ts +68 -411
  103. package/src/engine-ports.test.ts +37 -341
  104. package/src/engine.ts +80 -1248
  105. package/src/index.ts +28 -0
  106. package/src/pipeline-definition.ts +5 -0
  107. package/src/pipeline-runner.test.ts +5 -9
  108. package/src/pipeline-runner.ts +3 -2
  109. package/src/plugin-registry.test.ts +7 -10
  110. package/src/plugins.ts +18 -0
  111. package/src/ports.test.ts +80 -0
  112. package/src/ports.ts +36 -4
  113. package/src/registry.ts +7 -49
  114. package/src/schema-ports.test.ts +41 -214
  115. package/src/tagma.test.ts +84 -0
  116. package/src/tagma.ts +47 -0
  117. package/src/utils-api.ts +8 -0
  118. package/src/validate-raw-ports.test.ts +80 -393
  119. package/src/validate-raw.ts +93 -137
  120. package/src/yaml.ts +11 -0
  121. package/dist/sdk.d.ts +0 -32
  122. package/dist/sdk.d.ts.map +0 -1
  123. package/dist/sdk.js +0 -41
  124. package/dist/sdk.js.map +0 -1
  125. package/src/sdk.ts +0 -151
@@ -0,0 +1,623 @@
1
+ import { resolve } from 'path';
2
+ import { runCommand, runSpawn } from '../runner';
3
+ import { parseDuration, nowISO } from '../utils';
4
+ import { promptDocumentFromString, serializePromptDocument, prependContext, renderInputsBlock, renderOutputSchemaBlock, } from '../prompt-doc';
5
+ import { resolveTaskBindingInputs, resolveTaskInputs, substituteInputs } from '../ports';
6
+ import { executeHook, buildTaskContext } from '../hooks';
7
+ import { clip, tailLines } from '../logger';
8
+ import { extractSuccessfulOutputs, inferEffectivePorts } from './dataflow';
9
+ import { TriggerBlockedError, TriggerTimeoutError } from './trigger-errors';
10
+ const MAX_NORMALIZED_BYTES = 1_000_000;
11
+ function isPromptTaskConfig(task) {
12
+ return task.prompt !== undefined && task.command === undefined;
13
+ }
14
+ function isCommandTaskConfig(task) {
15
+ return task.command !== undefined && task.prompt === undefined;
16
+ }
17
+ export async function executeTask(options) {
18
+ const { taskId, ctx, registry, log, approvalGateway } = options;
19
+ const dag = ctx.dag;
20
+ const config = ctx.config;
21
+ const workDir = ctx.workDir;
22
+ const pipelineInfo = ctx.pipelineInfo;
23
+ const state = ctx.states.get(taskId);
24
+ const node = dag.nodes.get(taskId);
25
+ const task = node.task;
26
+ const track = node.track;
27
+ log.section(`Task ${taskId}`, taskId);
28
+ log.debug(`[task:${taskId}]`, `type=${isPromptTaskConfig(task) ? 'ai' : 'cmd'} track=${track.id} deps=[${node.dependsOn.join(', ') || '(root)'}]`);
29
+ // 1. Check dependencies
30
+ for (const depId of node.dependsOn) {
31
+ const result = ctx.isDependencySatisfied(depId);
32
+ if (result === 'skip') {
33
+ const depStatus = ctx.states.get(depId)?.status ?? 'unknown';
34
+ log.debug(`[task:${taskId}]`, `skipped (upstream "${depId}" status=${depStatus})`);
35
+ state.finishedAt = nowISO();
36
+ ctx.setTaskStatus(taskId, 'skipped');
37
+ return;
38
+ }
39
+ if (result === 'unsatisfied')
40
+ return; // still waiting
41
+ }
42
+ // 2. Check trigger
43
+ if (task.trigger) {
44
+ log.debug(`[task:${taskId}]`, `trigger wait: type=${task.trigger.type} ${JSON.stringify(task.trigger)}`);
45
+ try {
46
+ const triggerPlugin = registry.getHandler('triggers', task.trigger.type);
47
+ // R6: race the plugin's watch() against the pipeline's abort signal
48
+ // AND the task-level timeout. Third-party triggers may forget to
49
+ // wire up ctx.signal — without the abort race, an aborted pipeline
50
+ // would hang forever waiting for the plugin's watch promise to
51
+ // resolve. And without the timeout race, a buggy watch() that never
52
+ // settles would ignore the user's `task.timeout` (which the spawn
53
+ // path at step 4 already honours) — a task could wedge the whole
54
+ // pipeline until pipeline-level timeout fires (or forever, if none
55
+ // is set). Honouring task.timeout here makes the two stages
56
+ // symmetric. The cleanup paths in finally never run on the orphaned
57
+ // plugin promise (it's allowed to leak a watcher; the pipeline is
58
+ // being torn down anyway).
59
+ const triggerTimeoutMs = task.timeout ? parseDuration(task.timeout) : 0;
60
+ await new Promise((resolve, reject) => {
61
+ let settled = false;
62
+ let timer = null;
63
+ const onAbort = () => {
64
+ if (settled)
65
+ return;
66
+ settled = true;
67
+ if (timer !== null)
68
+ clearTimeout(timer);
69
+ reject(new Error('Pipeline aborted'));
70
+ };
71
+ if (ctx.abortController.signal.aborted) {
72
+ onAbort();
73
+ return;
74
+ }
75
+ ctx.abortController.signal.addEventListener('abort', onAbort, { once: true });
76
+ if (triggerTimeoutMs > 0) {
77
+ timer = setTimeout(() => {
78
+ if (settled)
79
+ return;
80
+ settled = true;
81
+ ctx.abortController.signal.removeEventListener('abort', onAbort);
82
+ reject(new TriggerTimeoutError(`Trigger "${task.trigger.type}" did not settle within ${task.timeout} (task-level timeout)`));
83
+ }, triggerTimeoutMs);
84
+ }
85
+ triggerPlugin
86
+ .watch(task.trigger, {
87
+ taskId: node.taskId,
88
+ trackId: track.id,
89
+ workDir: task.cwd ?? workDir,
90
+ signal: ctx.abortController.signal,
91
+ approvalGateway,
92
+ })
93
+ .then((v) => {
94
+ if (settled)
95
+ return;
96
+ settled = true;
97
+ if (timer !== null)
98
+ clearTimeout(timer);
99
+ ctx.abortController.signal.removeEventListener('abort', onAbort);
100
+ resolve(v);
101
+ }, (e) => {
102
+ if (settled)
103
+ return;
104
+ settled = true;
105
+ if (timer !== null)
106
+ clearTimeout(timer);
107
+ ctx.abortController.signal.removeEventListener('abort', onAbort);
108
+ reject(e);
109
+ });
110
+ });
111
+ log.debug(`[task:${taskId}]`, `trigger fired`);
112
+ }
113
+ catch (err) {
114
+ // If pipeline was aborted while we were still waiting for the trigger,
115
+ // this task never entered running state → skipped, not timeout.
116
+ state.finishedAt = nowISO();
117
+ if (ctx.abortReason !== null) {
118
+ ctx.setTaskStatus(taskId, 'skipped');
119
+ }
120
+ else if (err instanceof TriggerBlockedError) {
121
+ ctx.setTaskStatus(taskId, 'blocked'); // user/policy rejection
122
+ }
123
+ else if (err instanceof TriggerTimeoutError) {
124
+ ctx.setTaskStatus(taskId, 'timeout'); // genuine trigger wait timeout
125
+ }
126
+ else {
127
+ // A7 fallback: also check message strings for backward-compat with
128
+ // third-party trigger plugins that don't throw typed errors yet.
129
+ const msg = err instanceof Error ? err.message : String(err);
130
+ if (msg.includes('rejected') || msg.includes('denied')) {
131
+ ctx.setTaskStatus(taskId, 'blocked');
132
+ }
133
+ else if (msg.includes('timeout')) {
134
+ ctx.setTaskStatus(taskId, 'timeout');
135
+ }
136
+ else {
137
+ ctx.setTaskStatus(taskId, 'failed'); // plugin error, watcher crash, etc.
138
+ }
139
+ }
140
+ try {
141
+ await ctx.fireHook(taskId, 'task_failure');
142
+ }
143
+ catch (hookErr) {
144
+ log.error(`[task:${taskId}]`, `hook execution failed: ${hookErr instanceof Error ? hookErr.message : String(hookErr)}`);
145
+ }
146
+ return;
147
+ }
148
+ }
149
+ // 3. task_start hook (gate)
150
+ const hookResult = await executeHook(config.hooks, 'task_start', buildTaskContext('task_start', pipelineInfo, ctx.trackInfoOf(taskId), ctx.buildTaskInfoObj(taskId)), workDir, ctx.abortController.signal);
151
+ if (hookResult.exitCode !== 0 || config.hooks?.task_start) {
152
+ log.debug(`[task:${taskId}]`, `task_start hook exit=${hookResult.exitCode} allowed=${hookResult.allowed}`);
153
+ }
154
+ if (!hookResult.allowed) {
155
+ state.finishedAt = nowISO();
156
+ ctx.setTaskStatus(taskId, 'blocked');
157
+ try {
158
+ await ctx.fireHook(taskId, 'task_failure');
159
+ }
160
+ catch (hookErr) {
161
+ log.error(`[task:${taskId}]`, `hook execution failed: ${hookErr instanceof Error ? hookErr.message : String(hookErr)}`);
162
+ }
163
+ return;
164
+ }
165
+ // 3.5. Resolve port inputs from upstream outputs. This is the last
166
+ // gate before execution: missing-required inputs block the task
167
+ // without ever spawning a process, so the caller sees a clear
168
+ // "blocked: missing input X" rather than a cryptic runtime error
169
+ // from a command that expanded a placeholder to the empty string.
170
+ // Resolution runs even for tasks that declare no ports — the call
171
+ // is cheap and returns `{kind: 'ready', inputs: {}}` in that case,
172
+ // which downstream code handles uniformly.
173
+ //
174
+ // Prompt Tasks have no declared ports — their I/O contract is
175
+ // inferred from direct-neighbor Command Tasks (see ports.ts:
176
+ // `inferPromptPorts`). We synthesize a `TaskPorts` object and
177
+ // feed it into the same resolve/substitute/render/extract
178
+ // pipeline the Command path uses. Collisions that a Prompt can't
179
+ // disambiguate (same input name on two upstreams, incompatible
180
+ // downstream output types) block the task with a clear message.
181
+ const effectivePortsResult = inferEffectivePorts(ctx, taskId);
182
+ if (effectivePortsResult.kind === 'blocked') {
183
+ log.error(`[task:${taskId}]`, `blocked — prompt port inference failed:\n${effectivePortsResult.reason}`);
184
+ state.result = {
185
+ exitCode: -1,
186
+ stdout: '',
187
+ stderr: `[engine] prompt port inference failed:\n${effectivePortsResult.reason}`,
188
+ stdoutPath: null,
189
+ stderrPath: null,
190
+ durationMs: 0,
191
+ sessionId: null,
192
+ normalizedOutput: null,
193
+ failureKind: 'spawn_error',
194
+ outputs: null,
195
+ };
196
+ state.finishedAt = nowISO();
197
+ ctx.setTaskStatus(taskId, 'blocked');
198
+ try {
199
+ await ctx.fireHook(taskId, 'task_failure');
200
+ }
201
+ catch (hookErr) {
202
+ log.error(`[task:${taskId}]`, `hook execution failed: ${hookErr instanceof Error ? hookErr.message : String(hookErr)}`);
203
+ }
204
+ if (ctx.getOnFailure(taskId) === 'stop_all')
205
+ ctx.applyStopAll();
206
+ return;
207
+ }
208
+ const isPromptTask = effectivePortsResult.isPromptTask;
209
+ const effectivePorts = effectivePortsResult.effectivePorts;
210
+ const bindingResolution = resolveTaskBindingInputs(task, ctx.bindingDataMap, node.dependsOn);
211
+ if (bindingResolution.kind === 'blocked') {
212
+ log.error(`[task:${taskId}]`, `blocked — cannot resolve task input bindings:\n${bindingResolution.reason}`);
213
+ state.result = {
214
+ exitCode: -1,
215
+ stdout: '',
216
+ stderr: `[engine] task input binding resolution failed:\n${bindingResolution.reason}`,
217
+ stdoutPath: null,
218
+ stderrPath: null,
219
+ durationMs: 0,
220
+ sessionId: null,
221
+ normalizedOutput: null,
222
+ failureKind: 'spawn_error',
223
+ outputs: null,
224
+ };
225
+ state.finishedAt = nowISO();
226
+ ctx.setTaskStatus(taskId, 'blocked');
227
+ try {
228
+ await ctx.fireHook(taskId, 'task_failure');
229
+ }
230
+ catch (hookErr) {
231
+ log.error(`[task:${taskId}]`, `hook execution failed: ${hookErr instanceof Error ? hookErr.message : String(hookErr)}`);
232
+ }
233
+ if (ctx.getOnFailure(taskId) === 'stop_all')
234
+ ctx.applyStopAll();
235
+ return;
236
+ }
237
+ if (bindingResolution.missingOptional.length > 0) {
238
+ log.debug(`[task:${taskId}]`, `optional input bindings unresolved (empty in placeholders): ${bindingResolution.missingOptional.join(', ')}`);
239
+ }
240
+ let inferredPromptInputs = {};
241
+ if (isPromptTask && effectivePorts?.inputs && effectivePorts.inputs.length > 0) {
242
+ const inputResolution = resolveTaskInputs({ ...task, ports: effectivePorts }, ctx.outputValuesMap, node.dependsOn);
243
+ if (inputResolution.kind === 'blocked') {
244
+ log.error(`[task:${taskId}]`, `blocked — cannot resolve inferred prompt inputs:\n${inputResolution.reason}`);
245
+ state.result = {
246
+ exitCode: -1,
247
+ stdout: '',
248
+ stderr: `[engine] inferred prompt input resolution failed:\n${inputResolution.reason}`,
249
+ stdoutPath: null,
250
+ stderrPath: null,
251
+ durationMs: 0,
252
+ sessionId: null,
253
+ normalizedOutput: null,
254
+ failureKind: 'spawn_error',
255
+ outputs: null,
256
+ };
257
+ state.finishedAt = nowISO();
258
+ ctx.setTaskStatus(taskId, 'blocked');
259
+ try {
260
+ await ctx.fireHook(taskId, 'task_failure');
261
+ }
262
+ catch (hookErr) {
263
+ log.error(`[task:${taskId}]`, `hook execution failed: ${hookErr instanceof Error ? hookErr.message : String(hookErr)}`);
264
+ }
265
+ if (ctx.getOnFailure(taskId) === 'stop_all')
266
+ ctx.applyStopAll();
267
+ return;
268
+ }
269
+ inferredPromptInputs = inputResolution.inputs;
270
+ }
271
+ const resolvedInputs = { ...inferredPromptInputs, ...bindingResolution.inputs };
272
+ ctx.resolvedInputsMap.set(taskId, resolvedInputs);
273
+ if (effectivePorts?.inputs && effectivePorts.inputs.length > 0) {
274
+ log.debug(`[task:${taskId}]`, `resolved inputs: ${JSON.stringify(resolvedInputs)}` +
275
+ (isPromptTask ? ' (inferred from upstream Commands)' : ''));
276
+ }
277
+ // 4. Mark running — set startedAt before emitting so subscribers see a
278
+ // complete task_update (startedAt non-null) on the status transition.
279
+ state.startedAt = nowISO();
280
+ ctx.setTaskStatus(taskId, 'running');
281
+ log.info(`[task:${taskId}]`, isCommandTaskConfig(task) ? `running: ${task.command}` : `running (driver task)`);
282
+ // File-only: resolved config for this task
283
+ const resolvedDriver = task.driver ?? track.driver ?? config.driver ?? 'opencode';
284
+ const resolvedModel = task.model ?? track.model ?? config.model ?? '(default)';
285
+ const resolvedPerms = task.permissions ?? track.permissions ?? '(default)';
286
+ const resolvedCwd = task.cwd ?? track.cwd ?? workDir;
287
+ log.debug(`[task:${taskId}]`, `resolved: driver=${resolvedDriver} model=${resolvedModel} cwd=${resolvedCwd}`);
288
+ log.debug(`[task:${taskId}]`, `permissions: ${JSON.stringify(resolvedPerms)}`);
289
+ if (task.continue_from) {
290
+ log.debug(`[task:${taskId}]`, `continue_from: "${task.continue_from}"`);
291
+ }
292
+ if (task.timeout) {
293
+ log.debug(`[task:${taskId}]`, `timeout: ${task.timeout}`);
294
+ }
295
+ try {
296
+ let result;
297
+ const timeoutMs = task.timeout ? parseDuration(task.timeout) : undefined;
298
+ // Stream child stdout/stderr directly to disk in the logger's run dir
299
+ // and keep only a bounded tail in the returned TaskResult. Filenames
300
+ // mirror the existing `.stderr` naming — dots in task ids are replaced
301
+ // so hierarchical ids (e.g. `track1.task2`) map cleanly to a flat dir.
302
+ const fsSafeTaskId = taskId.replace(/\./g, '_');
303
+ const stdoutPath = resolve(log.dir, `${fsSafeTaskId}.stdout`);
304
+ const stderrPath = resolve(log.dir, `${fsSafeTaskId}.stderr`);
305
+ const runOpts = {
306
+ timeoutMs,
307
+ signal: ctx.abortController.signal,
308
+ stdoutPath,
309
+ stderrPath,
310
+ };
311
+ if (isCommandTaskConfig(task)) {
312
+ // Substitute `{{inputs.X}}` placeholders into the command
313
+ // string. Tasks with no declared inputs always produce the same
314
+ // string back (no placeholders to match). Unresolved references
315
+ // render empty — validate-raw flags undeclared references as
316
+ // errors, so the only way to land here with an unresolved is an
317
+ // optional input that had no upstream producer and no default,
318
+ // which we surface in the log.
319
+ const { text: expandedCommand, unresolved } = substituteInputs(task.command, resolvedInputs);
320
+ if (unresolved.length > 0) {
321
+ log.debug(`[task:${taskId}]`, `command placeholders rendered empty: ${unresolved.join(', ')}`);
322
+ }
323
+ log.debug(`[task:${taskId}]`, `command: ${expandedCommand}`);
324
+ result = await runCommand(expandedCommand, task.cwd ?? workDir, runOpts);
325
+ }
326
+ else {
327
+ // AI task: apply middleware chain against a structured PromptDocument.
328
+ const driverName = task.driver ?? track.driver ?? config.driver ?? 'opencode';
329
+ const driver = registry.getHandler('drivers', driverName);
330
+ // Substitute placeholders in the user-authored prompt before
331
+ // wrapping into a PromptDocument so middlewares see the
332
+ // already-resolved task text.
333
+ const { text: expandedPrompt, unresolved } = substituteInputs(task.prompt, resolvedInputs);
334
+ if (unresolved.length > 0) {
335
+ log.debug(`[task:${taskId}]`, `prompt placeholders rendered empty: ${unresolved.join(', ')}`);
336
+ }
337
+ const originalLen = expandedPrompt.length;
338
+ let doc = promptDocumentFromString(expandedPrompt);
339
+ // Prepend port-related context blocks so the model sees them
340
+ // before any middleware-added retrieval / memory blocks. Order
341
+ // matters: [Output Format] first (sets the deliverable), then
342
+ // [Inputs] (the concrete data to operate on). Empty blocks are
343
+ // filtered out — tasks without ports get no extra blocks at all.
344
+ const outputFormatBlock = renderOutputSchemaBlock(effectivePorts?.outputs);
345
+ if (outputFormatBlock) {
346
+ doc = prependContext(doc, outputFormatBlock);
347
+ }
348
+ const inputsBlock = renderInputsBlock(effectivePorts?.inputs, resolvedInputs);
349
+ if (inputsBlock) {
350
+ doc = prependContext(doc, inputsBlock);
351
+ }
352
+ const mws = task.middlewares !== undefined ? task.middlewares : track.middlewares;
353
+ if (mws && mws.length > 0) {
354
+ log.debug(`[task:${taskId}]`, `middleware chain: ${mws.map((m) => m.type).join(' → ')}`);
355
+ const mwCtx = {
356
+ task,
357
+ track,
358
+ workDir: task.cwd ?? workDir,
359
+ };
360
+ for (const mwConfig of mws) {
361
+ const mwPlugin = registry.getHandler('middlewares', mwConfig.type);
362
+ const beforeBlocks = doc.contexts.length;
363
+ const beforeLen = serializePromptDocument(doc).length;
364
+ // Prefer the structured API. Fall back to the legacy
365
+ // `enhance(string) → string` path so v0.x plugins keep
366
+ // working — that fallback loses context structure (the
367
+ // middleware's output becomes the new task body) but never
368
+ // silently drops content.
369
+ if (typeof mwPlugin.enhanceDoc === 'function') {
370
+ const next = await mwPlugin.enhanceDoc(doc, mwConfig, mwCtx);
371
+ if (!next ||
372
+ typeof next !== 'object' ||
373
+ !Array.isArray(next.contexts) ||
374
+ typeof next.task !== 'string') {
375
+ throw new Error(`middleware "${mwConfig.type}".enhanceDoc() returned a malformed PromptDocument`);
376
+ }
377
+ doc = next;
378
+ }
379
+ else if (typeof mwPlugin.enhance === 'function') {
380
+ const asString = serializePromptDocument(doc);
381
+ const next = await mwPlugin.enhance(asString, mwConfig, mwCtx);
382
+ // R3: a middleware that returns undefined / null / a non-string
383
+ // would silently corrupt the prompt. Fail loud.
384
+ if (typeof next !== 'string') {
385
+ throw new Error(`middleware "${mwConfig.type}".enhance() returned ${next === null ? 'null' : typeof next}, expected string`);
386
+ }
387
+ // Legacy fallback: collapse the returned string into a
388
+ // fresh doc. Earlier structure is folded into the string
389
+ // (serializePromptDocument just ran), so bytes the driver
390
+ // sees match the old string pipeline.
391
+ doc = { contexts: [], task: next };
392
+ }
393
+ else {
394
+ throw new Error(`middleware "${mwConfig.type}" provides neither enhanceDoc nor enhance`);
395
+ }
396
+ const afterLen = serializePromptDocument(doc).length;
397
+ const addedBlocks = doc.contexts.length - beforeBlocks;
398
+ log.debug(`[task:${taskId}]`, ` ${mwConfig.type}: ${beforeLen} → ${afterLen} chars` +
399
+ (addedBlocks > 0
400
+ ? ` (+${addedBlocks} context block${addedBlocks > 1 ? 's' : ''})`
401
+ : ''));
402
+ }
403
+ }
404
+ const prompt = serializePromptDocument(doc);
405
+ log.debug(`[task:${taskId}]`, `prompt: ${originalLen} chars (final: ${prompt.length} chars, ${doc.contexts.length} block${doc.contexts.length === 1 ? '' : 's'})`);
406
+ log.quiet(`--- prompt (final) ---\n${clip(prompt)}\n--- end prompt ---`, taskId);
407
+ // H1: hand the driver a continue_from that has already been
408
+ // qualified by dag.ts. Without this, drivers like codex/opencode/
409
+ // claude-code look up maps directly with
410
+ // the user's raw (possibly bare) string, which races whenever two
411
+ // tracks share a task name. dag.ts has the only authoritative
412
+ // resolver, so we use its precomputed answer here.
413
+ // Drivers key sessionMap/normalizedMap by fully-qualified id. buildDag
414
+ // guarantees `resolvedContinueFrom` is set for every task that has a
415
+ // `continue_from`, so if we see the bare form here something upstream
416
+ // is broken — fail loud instead of silently miskeying the lookup.
417
+ if (task.continue_from && !node.resolvedContinueFrom) {
418
+ throw new Error(`Internal: task "${taskId}" has continue_from "${task.continue_from}" ` +
419
+ `but no resolvedContinueFrom. buildDag should have qualified it.`);
420
+ }
421
+ const enrichedTask = {
422
+ ...task,
423
+ prompt,
424
+ continue_from: node.resolvedContinueFrom,
425
+ };
426
+ const driverCtx = {
427
+ sessionMap: ctx.sessionMap,
428
+ normalizedMap: ctx.normalizedMap,
429
+ workDir: task.cwd ?? workDir,
430
+ // Structured view for drivers that want fine-grained control
431
+ // over serialization (e.g. inserting [Previous Output] between
432
+ // contexts and task). Drivers that read task.prompt see the
433
+ // default serialization and need no changes.
434
+ promptDoc: doc,
435
+ // Resolved input values keyed by input name. Typed bindings have
436
+ // already been coerced when a binding declares `type`.
437
+ inputs: resolvedInputs,
438
+ };
439
+ const spec = await driver.buildCommand(enrichedTask, track, driverCtx);
440
+ log.debug(`[task:${taskId}]`, `driver=${driverName}`);
441
+ log.debug(`[task:${taskId}]`, `spawn args: ${JSON.stringify(spec.args)}`);
442
+ if (spec.cwd)
443
+ log.debug(`[task:${taskId}]`, `spawn cwd: ${spec.cwd}`);
444
+ if (spec.env)
445
+ log.debug(`[task:${taskId}]`, `spawn env overrides: ${Object.keys(spec.env).join(', ')}`);
446
+ if (spec.stdin)
447
+ log.debug(`[task:${taskId}]`, `spawn stdin: ${spec.stdin.length} chars`);
448
+ result = await runSpawn(spec, driver, runOpts);
449
+ }
450
+ // 6. Determine terminal status (without emitting yet — result must be complete first)
451
+ // H2: branch on failureKind so spawn errors no longer masquerade as
452
+ // timeouts. Old runners that don't set failureKind still work — we
453
+ // fall back to the historical `exitCode === -1 → timeout` heuristic so
454
+ // pre-existing third-party drivers don't regress.
455
+ let terminalStatus;
456
+ const kind = result.failureKind;
457
+ if (kind === 'timeout') {
458
+ terminalStatus = 'timeout';
459
+ }
460
+ else if (kind === 'spawn_error') {
461
+ terminalStatus = 'failed';
462
+ }
463
+ else if (kind === undefined && result.exitCode === -1) {
464
+ // Legacy path: pre-H2 driver returned -1 with no kind. Treat as
465
+ // timeout for backward compatibility (the previous behaviour).
466
+ terminalStatus = 'timeout';
467
+ }
468
+ else if (result.exitCode !== 0) {
469
+ terminalStatus = 'failed';
470
+ }
471
+ else if (task.completion) {
472
+ const plugin = registry.getHandler('completions', task.completion.type);
473
+ const completionCtx = { workDir: task.cwd ?? workDir, signal: ctx.abortController.signal };
474
+ const passed = await plugin.check(task.completion, result, completionCtx);
475
+ // R4: strict boolean check. Truthy strings/numbers used to be coerced
476
+ // to success — a check returning "ok" would let a failing task pass.
477
+ if (typeof passed !== 'boolean') {
478
+ throw new Error(`completion "${task.completion.type}".check() returned ${passed === null ? 'null' : typeof passed}, expected boolean`);
479
+ }
480
+ terminalStatus = passed ? 'success' : 'failed';
481
+ }
482
+ else {
483
+ terminalStatus = 'success';
484
+ }
485
+ // Extract declared outputs from the task's output stream. Only
486
+ // meaningful on success — a failed task's output is whatever the
487
+ // child happened to emit before exiting, and downstream tasks
488
+ // shouldn't receive partial data.
489
+ let extractedOutputs = null;
490
+ if (terminalStatus === 'success') {
491
+ const outputExtraction = extractSuccessfulOutputs({
492
+ task,
493
+ effectivePorts,
494
+ result,
495
+ });
496
+ extractedOutputs = outputExtraction.outputs;
497
+ if (task.outputs && Object.keys(task.outputs).length > 0) {
498
+ log.debug(`[task:${taskId}]`, `extracted binding outputs: ${JSON.stringify(extractedOutputs ?? {})}`);
499
+ if (outputExtraction.bindingDiagnostic) {
500
+ log.debug(`[task:${taskId}]`, outputExtraction.bindingDiagnostic);
501
+ const note = `\n[engine] ${outputExtraction.bindingDiagnostic}`;
502
+ result = { ...result, stderr: result.stderr + note };
503
+ }
504
+ }
505
+ if (effectivePorts?.outputs && effectivePorts.outputs.length > 0) {
506
+ log.debug(`[task:${taskId}]`, `extracted outputs: ${JSON.stringify(extractedOutputs ?? {})}` +
507
+ (isPromptTask ? ' (inferred from downstream Commands)' : ''));
508
+ if (outputExtraction.portDiagnostic) {
509
+ log.error(`[task:${taskId}]`, outputExtraction.portDiagnostic);
510
+ const note = `\n[engine] ${outputExtraction.portDiagnostic}`;
511
+ result = { ...result, stderr: result.stderr + note };
512
+ }
513
+ }
514
+ }
515
+ // Attach outputs to the result (null when task has no declared
516
+ // outputs or extraction failed entirely). Consumers of TaskResult
517
+ // — hooks, wire events, test assertions — all go through this
518
+ // one field rather than re-running extraction.
519
+ result = { ...result, outputs: extractedOutputs };
520
+ if (extractedOutputs !== null) {
521
+ ctx.outputValuesMap.set(taskId, extractedOutputs);
522
+ }
523
+ ctx.bindingDataMap.set(taskId, {
524
+ outputs: extractedOutputs,
525
+ stdout: result.stdout,
526
+ stderr: result.stderr,
527
+ normalizedOutput: result.normalizedOutput,
528
+ exitCode: result.exitCode,
529
+ });
530
+ // Store normalized text separately (in-memory) for continue_from handoff.
531
+ // R15: clip oversized values so a runaway parseResult can't accumulate
532
+ // hundreds of MB across tasks.
533
+ if (result.normalizedOutput !== null) {
534
+ const clipped = result.normalizedOutput.length > MAX_NORMALIZED_BYTES
535
+ ? result.normalizedOutput.slice(0, MAX_NORMALIZED_BYTES) +
536
+ `\n[…clipped at ${MAX_NORMALIZED_BYTES} bytes]`
537
+ : result.normalizedOutput;
538
+ ctx.normalizedMap.set(taskId, clipped);
539
+ }
540
+ // Note: stderr is already persisted by runner.ts as it streams; the
541
+ // old "write full string after the fact" block is gone — that's what
542
+ // the streaming rewrite fixed (unbounded in-memory buffering).
543
+ if (result.sessionId) {
544
+ // H1: qualified-only key.
545
+ ctx.sessionMap.set(taskId, result.sessionId);
546
+ }
547
+ // Set result and finishedAt before emitting terminal status so listeners see complete state
548
+ state.result = result;
549
+ state.finishedAt = nowISO();
550
+ ctx.setTaskStatus(taskId, terminalStatus);
551
+ // Log task outcome with relevant details
552
+ const durSec = (result.durationMs / 1000).toFixed(1);
553
+ if (terminalStatus === 'success') {
554
+ log.info(`[task:${taskId}]`, `success (${durSec}s)`);
555
+ }
556
+ else {
557
+ log.error(`[task:${taskId}]`, `${terminalStatus} exit=${result.exitCode} duration=${durSec}s`);
558
+ if (result.stderr) {
559
+ const tail = tailLines(result.stderr, 10);
560
+ log.error(`[task:${taskId}]`, `stderr tail:\n${tail}`);
561
+ }
562
+ }
563
+ // File-only: byte counts (prefer full totals from the runner over the
564
+ // bounded tail length so oversized outputs show their real size) +
565
+ // paths to the on-disk full copies.
566
+ const stdoutSize = result.stdoutBytes ?? result.stdout.length;
567
+ const stderrSize = result.stderrBytes ?? result.stderr.length;
568
+ log.debug(`[task:${taskId}]`, `stdout: ${stdoutSize} bytes, stderr: ${stderrSize} bytes`);
569
+ if (result.sessionId) {
570
+ log.debug(`[task:${taskId}]`, `sessionId: ${result.sessionId}`);
571
+ }
572
+ if (result.stdoutPath) {
573
+ log.debug(`[task:${taskId}]`, `wrote stdout: ${result.stdoutPath}`);
574
+ }
575
+ if (result.stderrPath) {
576
+ log.debug(`[task:${taskId}]`, `wrote stderr: ${result.stderrPath}`);
577
+ }
578
+ if (result.stdout) {
579
+ log.quiet(`--- stdout (${taskId}) ---\n${clip(result.stdout)}\n--- end stdout ---`, taskId);
580
+ }
581
+ if (result.stderr) {
582
+ log.quiet(`--- stderr (${taskId}) ---\n${clip(result.stderr)}\n--- end stderr ---`, taskId);
583
+ }
584
+ if (task.completion) {
585
+ log.debug(`[task:${taskId}]`, `completion check: type=${task.completion.type} result=${terminalStatus}`);
586
+ }
587
+ }
588
+ catch (err) {
589
+ const errMsg = err instanceof Error ? (err.stack ?? err.message) : String(err);
590
+ log.error(`[task:${taskId}]`, `failed before execution: ${errMsg}`);
591
+ state.result = {
592
+ exitCode: -1,
593
+ stdout: '',
594
+ stderr: errMsg,
595
+ stdoutPath: null,
596
+ stderrPath: null,
597
+ stdoutBytes: 0,
598
+ stderrBytes: errMsg.length,
599
+ durationMs: 0,
600
+ sessionId: null,
601
+ normalizedOutput: null,
602
+ // H2: Engine-level pre-execution errors (driver throw, middleware
603
+ // throw, getHandler 404) classify as spawn_error — the process never
604
+ // ran, so calling them "timeout" was actively misleading.
605
+ failureKind: 'spawn_error',
606
+ };
607
+ state.finishedAt = nowISO();
608
+ ctx.setTaskStatus(taskId, 'failed');
609
+ }
610
+ // 7. Fire hooks
611
+ const finalStatus = state.status;
612
+ try {
613
+ await ctx.fireHook(taskId, finalStatus === 'success' ? 'task_success' : 'task_failure');
614
+ }
615
+ catch (hookErr) {
616
+ log.error(`[task:${taskId}]`, `hook execution failed: ${hookErr instanceof Error ? hookErr.message : String(hookErr)}`);
617
+ }
618
+ // 8. Handle stop_all for failure states
619
+ if (finalStatus !== 'success' && ctx.getOnFailure(taskId) === 'stop_all') {
620
+ ctx.applyStopAll();
621
+ }
622
+ }
623
+ //# sourceMappingURL=task-executor.js.map