@tagma/sdk 0.7.3 → 0.7.5

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