@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
package/src/runner.ts DELETED
@@ -1,666 +0,0 @@
1
- import { existsSync, readFileSync, statSync } from 'node:fs';
2
- import { mkdir, open, type FileHandle } from 'node:fs/promises';
3
- import { dirname, isAbsolute, join, resolve as pathResolve } from 'node:path';
4
- import type { SpawnSpec, DriverPlugin, TaskResult } from './types';
5
- import { shellArgs } from './utils';
6
-
7
- // Delay before escalating SIGTERM to SIGKILL when killing a timed-out process.
8
- const SIGKILL_DELAY_MS = 3_000;
9
-
10
- /**
11
- * Default cap for the in-memory tail retained for each stream. Picked so that
12
- * a task producing runaway output (AI agent bug, adversarial input) cannot
13
- * balloon the sidecar's RSS, while still being large enough that typical AI
14
- * responses (which top out around low-MB of text) are returned whole. Callers
15
- * that need different limits supply `RunOptions.maxStdoutTailBytes` /
16
- * `.maxStderrTailBytes`.
17
- */
18
- const DEFAULT_STDOUT_TAIL_BYTES = 8 * 1024 * 1024; // 8 MB
19
- const DEFAULT_STDERR_TAIL_BYTES = 4 * 1024 * 1024; // 4 MB
20
-
21
- /**
22
- * On Windows, proc.kill('SIGTERM') / proc.kill('SIGKILL') only terminate the
23
- * direct child process. When the child is a .cmd/.bat wrapper (e.g. claude.cmd),
24
- * cmd.exe spawns the real process as a grandchild — proc.kill misses it entirely.
25
- * `taskkill /F /T /PID` kills the entire process tree rooted at the given PID.
26
- */
27
- function killProcessTree(pid: number): void {
28
- if (process.platform !== 'win32') return;
29
- try {
30
- const result = Bun.spawnSync(['taskkill', '/F', '/T', '/PID', String(pid)], {
31
- stdout: 'pipe',
32
- stderr: 'pipe',
33
- });
34
- if (result.exitCode !== 0) {
35
- const stderr = new TextDecoder().decode(result.stderr);
36
- // Exit code 128 = process not found (already exited) — not worth warning about
37
- if (result.exitCode !== 128) {
38
- console.error(
39
- `[killProcessTree] taskkill exited ${result.exitCode} for PID ${pid}: ${stderr.trim()}`,
40
- );
41
- }
42
- }
43
- } catch {
44
- /* best-effort — process may have already exited */
45
- }
46
- }
47
-
48
- export interface RunOptions {
49
- readonly timeoutMs?: number;
50
- readonly signal?: AbortSignal; // pipeline-level abort
51
- /**
52
- * If set, stream the child's stdout to this file path as it arrives. The
53
- * returned `TaskResult.stdout` is still a bounded in-memory tail
54
- * (`maxStdoutTailBytes`) — callers that need the full output should read
55
- * from the returned `stdoutPath`. Parent directories are created as needed.
56
- */
57
- readonly stdoutPath?: string;
58
- /** Symmetric to `stdoutPath` for stderr. */
59
- readonly stderrPath?: string;
60
- /**
61
- * Cap on bytes retained in memory for the returned `TaskResult.stdout`
62
- * string. Defaults to `DEFAULT_STDOUT_TAIL_BYTES`. Bytes beyond this cap
63
- * from the HEAD of the stream are dropped from the in-memory string; the
64
- * on-disk file (if `stdoutPath` is set) is still the full output.
65
- */
66
- readonly maxStdoutTailBytes?: number;
67
- readonly maxStderrTailBytes?: number;
68
- }
69
-
70
- /**
71
- * Read a stream to completion, persisting every chunk to `filePath` (when
72
- * provided) while keeping only the last `maxTailBytes` bytes in memory.
73
- *
74
- * Why the split: large child outputs (multi-MB AI responses, verbose debug
75
- * dumps) used to accumulate entirely in memory via `new Response(s).text()`,
76
- * which let a runaway task balloon the sidecar's RSS. Streaming to disk +
77
- * bounded tail gives callers: (a) unbounded data fidelity on disk, (b) fixed
78
- * memory footprint, (c) the tail — which is almost always what callers
79
- * actually consume (final AI answer, error summary, last N lines).
80
- *
81
- * Backpressure: we `await fh.write(chunk)` per chunk, so if disk is slow we
82
- * naturally slow the reader — but we do NOT stop reading the pipe, so the
83
- * child never blocks on a full stdout pipe. Disk errors don't abort the
84
- * stream; we close the handle, null it, and keep consuming into the tail
85
- * buffer only (with a breadcrumb in the returned text).
86
- *
87
- * Tail eviction: drops whole chunks from the front until total retained is
88
- * at or below the cap. If a single chunk alone exceeds the cap (rare — would
89
- * require a >cap-bytes chunkless burst from the child), we slice its tail.
90
- * UTF-8 boundaries at the slice point may emit replacement characters when
91
- * decoded — acceptable (the trailing/leading codepoint is a cosmetic loss).
92
- */
93
- async function collectStream(
94
- stream: ReadableStream<Uint8Array> | undefined,
95
- filePath: string | undefined,
96
- maxTailBytes: number,
97
- ): Promise<{ text: string; totalBytes: number; path: string | null }> {
98
- if (!stream) return { text: '', totalBytes: 0, path: null };
99
-
100
- let fh: FileHandle | null = null;
101
- let diskWriteFailed = false;
102
- if (filePath) {
103
- try {
104
- await mkdir(dirname(filePath), { recursive: true });
105
- fh = await open(filePath, 'w');
106
- } catch (err) {
107
- console.error(
108
- `[runner] failed to open ${filePath} for output streaming: ${err instanceof Error ? err.message : String(err)}`,
109
- );
110
- diskWriteFailed = true;
111
- }
112
- }
113
-
114
- const chunks: Uint8Array[] = [];
115
- let tailBytes = 0;
116
- let totalBytes = 0;
117
- let streamError: Error | null = null;
118
-
119
- try {
120
- // Use for await...of to avoid Bun bug where getReader() returns an
121
- // incomplete reader missing releaseLock() under concurrent spawn.
122
- // https://github.com/oven-sh/bun/issues/28952
123
- //
124
- // Bun 1.3.x also has sporadic failures iterating a spawned process's
125
- // stream under concurrent Bun.spawn — the iterator throws mid-drain even
126
- // when the child exited 0. We record the error as a breadcrumb instead
127
- // of propagating, so the caller still sees the real exitCode from
128
- // proc.exited and a task that the OS considered successful doesn't get
129
- // marked failed over a runtime stream glitch.
130
- for await (const value of stream as AsyncIterable<Uint8Array>) {
131
- totalBytes += value.length;
132
-
133
- // Disk: persist every byte. Failure here degrades to tail-only mode
134
- // without interrupting the stream (child must not block on pipe fill).
135
- if (fh) {
136
- try {
137
- await fh.write(value);
138
- } catch (err) {
139
- console.error(
140
- `[runner] disk write failed for ${filePath}: ${err instanceof Error ? err.message : String(err)}`,
141
- );
142
- try {
143
- await fh.close();
144
- } catch {
145
- /* ignore */
146
- }
147
- fh = null;
148
- diskWriteFailed = true;
149
- }
150
- }
151
-
152
- // Tail: append then evict whole chunks from the head while the total
153
- // retained exceeds the cap. Keep at least one chunk so short outputs
154
- // aren't lost entirely. Post-condition: tailBytes <= maxTailBytes OR
155
- // only one chunk remains (handled by the next block).
156
- chunks.push(value);
157
- tailBytes += value.length;
158
- while (chunks.length > 1 && tailBytes > maxTailBytes) {
159
- tailBytes -= chunks.shift()!.length;
160
- }
161
- // Pathological: a single chunk larger than the cap. Slice its tail.
162
- if (chunks.length === 1 && chunks[0]!.length > maxTailBytes) {
163
- const only = chunks[0]!;
164
- chunks[0] = only.slice(only.length - maxTailBytes);
165
- tailBytes = chunks[0]!.length;
166
- }
167
- }
168
- } catch (err) {
169
- streamError = err instanceof Error ? err : new Error(String(err));
170
- console.error(
171
- `[runner] stream read failed: ${streamError.message} — returning partial output`,
172
- );
173
- } finally {
174
- if (fh) {
175
- try {
176
- await fh.close();
177
- } catch {
178
- /* ignore */
179
- }
180
- }
181
- }
182
-
183
- // Decode retained chunks. `stream: true` lets the decoder buffer partial
184
- // code points across chunks, handling all boundaries except the very first
185
- // chunk (which may itself start mid-codepoint after eviction) — that
186
- // boundary gets a U+FFFD replacement, which is preferable to throwing.
187
- const decoder = new TextDecoder();
188
- let text = '';
189
- for (const c of chunks) text += decoder.decode(c, { stream: true });
190
- text += decoder.decode();
191
-
192
- if (totalBytes > tailBytes) {
193
- const dropped = totalBytes - tailBytes;
194
- const pathHint = filePath
195
- ? diskWriteFailed
196
- ? `${filePath} (partial — disk write failed mid-stream)`
197
- : filePath
198
- : 'not persisted (no path configured)';
199
- text = `[…${dropped} bytes truncated from head — full output at: ${pathHint}]\n${text}`;
200
- }
201
-
202
- if (streamError) {
203
- text = text + `\n[runner] stream read aborted: ${streamError.message}`;
204
- }
205
-
206
- return {
207
- text,
208
- totalBytes,
209
- // Return the path even on partial-write failure so operators can still
210
- // inspect the head bytes we managed to persist.
211
- path: filePath ?? null,
212
- };
213
- }
214
-
215
- /**
216
- * On Windows, Bun.spawn does NOT auto-append PATHEXT extensions like
217
- * CreateProcess does. A bare command like `claude` fails with ENOENT if the
218
- * actual file on disk is `claude.cmd` / `claude.bat` / `claude.ps1`. We
219
- * manually resolve the command against PATH + PATHEXT here so Drivers can
220
- * keep using short names (`claude`, `npx`, etc.) cross-platform.
221
- *
222
- * We also auto-unwrap npm-generated .cmd shims into direct `node <js>`
223
- * invocations. Spawning the .cmd routes argv through cmd.exe, which silently
224
- * truncates any argv element at the first newline — a multi-line prompt
225
- * reaches the child as just its first line. By targeting the underlying JS
226
- * entry point directly we bypass cmd.exe entirely and newlines survive.
227
- *
228
- * Results are cached by (cmd, envPath) key so repeated spawns of the same
229
- * command don't block the event loop with synchronous PATH/shim scans.
230
- *
231
- * Returns the original name if resolution fails; Bun will raise the same
232
- * ENOENT it would have otherwise.
233
- */
234
- const RESOLVED_EXE_CACHE_MAX = 128;
235
- // A cache entry is the replacement argv head for the command:
236
- // - [path] — a single resolved executable (e.g. `foo.exe`)
237
- // - [node, jsEntry] — an npm-shim unwrapped into `node <js>`
238
- // - null — resolution failed, leave the original name
239
- const resolvedExeCache = new Map<string, readonly string[] | null>();
240
-
241
- /** Evict the oldest entry when the cache is at capacity. */
242
- function evictIfFull(): void {
243
- if (resolvedExeCache.size >= RESOLVED_EXE_CACHE_MAX) {
244
- // Map iteration order is insertion order — delete the first (oldest) key.
245
- const oldest = resolvedExeCache.keys().next().value;
246
- if (oldest !== undefined) resolvedExeCache.delete(oldest);
247
- }
248
- }
249
-
250
- /**
251
- * Parse an npm-generated .cmd shim and return the underlying JS entry path.
252
- *
253
- * npm's shim has the shape:
254
- * "%_prog%" "%dp0%\node_modules\<pkg>\bin\<script>" %*
255
- *
256
- * We extract the second double-quoted path, substitute `%dp0%` with the
257
- * wrapper's own directory, and return the absolute JS path. Returns null for
258
- * anything that doesn't match the npm-shim pattern (user-written .cmd
259
- * scripts, non-node tools, etc.), which keeps the caller on the .cmd path.
260
- */
261
- function parseNpmCmdShim(wrapperPath: string): string | null {
262
- let contents: string;
263
- try {
264
- contents = readFileSync(wrapperPath, 'utf8');
265
- } catch {
266
- return null;
267
- }
268
- const execLine = contents
269
- .split(/\r?\n/)
270
- .find((l) => l.includes('%*') && l.includes('%dp0%'));
271
- if (!execLine) return null;
272
- const quoted = execLine.match(/"([^"]+)"/g);
273
- if (!quoted || quoted.length < 2) return null;
274
- const rawTarget = quoted[1]!.slice(1, -1); // strip surrounding quotes
275
- const wrapperDir = dirname(wrapperPath);
276
- // %dp0% expands to wrapper dir with a trailing backslash; strip either form.
277
- const expanded = rawTarget.replace(/%dp0%\\?/i, '').replace(/\//g, '\\');
278
- const abs = isAbsolute(expanded) ? expanded : pathResolve(wrapperDir, expanded);
279
- return existsSync(abs) ? abs : null;
280
- }
281
-
282
- /**
283
- * Given a resolved .cmd/.bat path, return the argv prefix that should be
284
- * spawned instead. For npm shims this is `[node, js-entry]`; for everything
285
- * else it's `[wrapperPath]` (unchanged, caller keeps using the wrapper).
286
- */
287
- function unwrapCmdShim(wrapperPath: string): readonly string[] {
288
- if (!/\.(cmd|bat)$/i.test(wrapperPath)) return [wrapperPath];
289
- const jsEntry = parseNpmCmdShim(wrapperPath);
290
- if (!jsEntry) return [wrapperPath];
291
- // Prefer node colocated with the wrapper (npm global bin often ships one).
292
- const colocated = join(dirname(wrapperPath), 'node.exe');
293
- const nodeExe = existsSync(colocated) ? colocated : 'node';
294
- return [nodeExe, jsEntry];
295
- }
296
-
297
- function resolveWindowsExe(args: readonly string[], envPath: string): readonly string[] {
298
- if (process.platform !== 'win32' || args.length === 0) return args;
299
- const cmd = args[0]!;
300
- // Already a full path or has an extension → trust caller. We still attempt
301
- // shim unwrapping when the caller handed us a bare .cmd/.bat so drivers
302
- // that resolve the shim themselves still benefit from the cmd.exe bypass.
303
- if (isAbsolute(cmd) || /\.[a-z0-9]+$/i.test(cmd)) {
304
- if (/\.(cmd|bat)$/i.test(cmd) && existsSync(cmd)) {
305
- const unwrapped = unwrapCmdShim(cmd);
306
- if (unwrapped.length === 2) return [...unwrapped, ...args.slice(1)];
307
- }
308
- return args;
309
- }
310
-
311
- const cacheKey = `${cmd}\x00${envPath}`;
312
- if (resolvedExeCache.has(cacheKey)) {
313
- // ?? null coerces undefined→null so the subsequent guard narrows cleanly.
314
- const cached = resolvedExeCache.get(cacheKey) ?? null;
315
- return cached !== null ? [...cached, ...args.slice(1)] : args;
316
- }
317
-
318
- const exts = (process.env.PATHEXT ?? '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC')
319
- .split(';')
320
- .filter(Boolean);
321
- const dirs = envPath.split(';').filter(Boolean);
322
-
323
- for (const dir of dirs) {
324
- for (const ext of exts) {
325
- const candidate = join(dir, cmd + ext);
326
- try {
327
- if (existsSync(candidate) && statSync(candidate).isFile()) {
328
- const head = unwrapCmdShim(candidate);
329
- evictIfFull();
330
- resolvedExeCache.set(cacheKey, head);
331
- return [...head, ...args.slice(1)];
332
- }
333
- } catch {
334
- /* stat race — skip */
335
- }
336
- }
337
- }
338
- evictIfFull();
339
- resolvedExeCache.set(cacheKey, null);
340
- return args;
341
- }
342
-
343
- /**
344
- * H2: Build a "failed before spawn" result. Tagged as 'spawn_error' so the
345
- * engine can show a useful classification ("driver tried to launch X but
346
- * the binary wasn't found") rather than the misleading "timeout".
347
- *
348
- * Pre-spawn failures never opened the output files, so stdoutPath /
349
- * stderrPath are null regardless of what the caller passed in opts — there
350
- * is nothing on disk to point at.
351
- */
352
- function failResult(stderr: string, durationMs: number): TaskResult {
353
- return {
354
- exitCode: -1,
355
- stdout: '',
356
- stderr,
357
- stdoutPath: null,
358
- stderrPath: null,
359
- stdoutBytes: 0,
360
- stderrBytes: stderr.length,
361
- durationMs,
362
- sessionId: null,
363
- normalizedOutput: null,
364
- failureKind: 'spawn_error',
365
- };
366
- }
367
-
368
- /**
369
- * R2: Validate a SpawnSpec returned by a third-party driver. Returns null on
370
- * success or a human-readable error message describing the first violation.
371
- *
372
- * Catching this here is critical: an undetected bad spec ends up calling
373
- * Bun.spawn with garbage and the resulting TypeError leaks into engine
374
- * processTask's catch block as "Cannot read properties of undefined". By
375
- * validating here we surface a clear "Driver X returned invalid args" message
376
- * instead, and short-circuit before holding any process resources.
377
- */
378
- export function validateSpawnSpec(spec: unknown, driverName: string): string | null {
379
- if (!spec || typeof spec !== 'object') {
380
- return `Driver "${driverName}".buildCommand returned ${spec === null ? 'null' : typeof spec}, expected SpawnSpec object`;
381
- }
382
- const s = spec as Record<string, unknown>;
383
- if (!Array.isArray(s.args)) {
384
- return `Driver "${driverName}".buildCommand returned spec.args of type ${typeof s.args}, expected string[]`;
385
- }
386
- if (s.args.length === 0) {
387
- return `Driver "${driverName}".buildCommand returned an empty spec.args array`;
388
- }
389
- for (let i = 0; i < s.args.length; i++) {
390
- if (typeof s.args[i] !== 'string') {
391
- return `Driver "${driverName}".buildCommand returned spec.args[${i}] of type ${typeof s.args[i]}, expected string`;
392
- }
393
- }
394
- if (typeof s.args[0] !== 'string' || s.args[0].length === 0) {
395
- return `Driver "${driverName}".buildCommand returned an empty executable name in spec.args[0]`;
396
- }
397
- if (s.cwd !== undefined && typeof s.cwd !== 'string') {
398
- return `Driver "${driverName}".buildCommand returned spec.cwd of type ${typeof s.cwd}, expected string or undefined`;
399
- }
400
- if (s.stdin !== undefined && typeof s.stdin !== 'string') {
401
- return `Driver "${driverName}".buildCommand returned spec.stdin of type ${typeof s.stdin}, expected string or undefined`;
402
- }
403
- if (s.env !== undefined) {
404
- if (!s.env || typeof s.env !== 'object' || Array.isArray(s.env)) {
405
- return `Driver "${driverName}".buildCommand returned spec.env that is not a plain object`;
406
- }
407
- for (const [k, v] of Object.entries(s.env as Record<string, unknown>)) {
408
- if (typeof v !== 'string') {
409
- return `Driver "${driverName}".buildCommand returned spec.env.${k} of type ${typeof v}, expected string`;
410
- }
411
- }
412
- }
413
- return null;
414
- }
415
-
416
- export async function runSpawn(
417
- spec: SpawnSpec,
418
- driver: DriverPlugin | null,
419
- opts: RunOptions = {},
420
- ): Promise<TaskResult> {
421
- const { timeoutMs, signal } = opts;
422
- const start = performance.now();
423
- const elapsed = () => Math.round(performance.now() - start);
424
-
425
- if (signal?.aborted) {
426
- return failResult('Pipeline aborted before spawn', 0);
427
- }
428
-
429
- // R2: validate the spec before touching it. A third-party driver that
430
- // returns a malformed SpawnSpec used to crash deep inside Bun.spawn with
431
- // an opaque TypeError; now we report a clear "Driver X returned …" message.
432
- const validationError = validateSpawnSpec(spec, driver?.name ?? '<unknown>');
433
- if (validationError !== null) {
434
- return failResult(validationError, elapsed());
435
- }
436
-
437
- const mergedEnv = { ...process.env, ...(spec.env ?? {}) };
438
- const resolvedArgs = resolveWindowsExe(spec.args, mergedEnv.PATH ?? process.env.PATH ?? '');
439
-
440
- // ── 1. Spawn (catch ENOENT / bad-cwd up front) ────────────────────────
441
- let proc: ReturnType<typeof Bun.spawn>;
442
- try {
443
- proc = Bun.spawn(resolvedArgs as string[], {
444
- cwd: spec.cwd,
445
- env: mergedEnv,
446
- stdout: 'pipe',
447
- stderr: 'pipe',
448
- stdin: spec.stdin ? 'pipe' : undefined,
449
- });
450
- } catch (err) {
451
- return failResult(String(err), elapsed());
452
- }
453
-
454
- // ── 2. Write stdin ─────────────────────────────────────────────────────
455
- // Child may exit before reading (e.g. quick-fail commands that don't
456
- // touch stdin) → swallow EPIPE rather than surfacing it as an
457
- // engine-level error.
458
- if (spec.stdin && proc.stdin && typeof proc.stdin !== 'number') {
459
- try {
460
- await proc.stdin.write(spec.stdin);
461
- await proc.stdin.end();
462
- } catch {
463
- /* ignore EPIPE / closed-pipe errors */
464
- }
465
- }
466
-
467
- // ── 3. Timeout & abort handling ────────────────────────────────────────
468
- let killedByUs = false;
469
- let timedOut = false;
470
- let timer: ReturnType<typeof setTimeout> | null = null;
471
- let forceTimer: ReturnType<typeof setTimeout> | null = null;
472
-
473
- const killGracefully = () => {
474
- if (killedByUs) return;
475
- killedByUs = true;
476
-
477
- if (process.platform === 'win32') {
478
- // On Windows, kill the entire process tree via taskkill. This handles
479
- // .cmd wrappers and nested child processes that proc.kill() misses.
480
- killProcessTree(proc.pid);
481
- } else {
482
- proc.kill('SIGTERM');
483
- // If the child ignores SIGTERM, escalate to SIGKILL after 3 s.
484
- forceTimer = setTimeout(() => {
485
- try {
486
- proc.kill('SIGKILL');
487
- } catch {
488
- /* already exited */
489
- }
490
- }, SIGKILL_DELAY_MS);
491
- }
492
- };
493
-
494
- if (timeoutMs && timeoutMs > 0) {
495
- timer = setTimeout(() => {
496
- timedOut = true;
497
- killGracefully();
498
- }, timeoutMs);
499
- }
500
-
501
- const onAbort = () => killGracefully();
502
- if (signal) {
503
- if (signal.aborted) {
504
- killGracefully();
505
- } else {
506
- signal.addEventListener('abort', onAbort, { once: true });
507
- }
508
- }
509
-
510
- // ── 4. Collect output & wait ──────────────────────────────────────────
511
- // Both streams are drained concurrently with `proc.exited` to avoid the
512
- // classic pipe-buffer deadlock (child blocks on a full stdout pipe, parent
513
- // is blocked waiting on exit which the child can't reach). Each stream is
514
- // persisted to disk via `collectStream` as it arrives so we never hold the
515
- // full output in memory — only the bounded tail.
516
- const stdoutStream = typeof proc.stdout === 'object' ? proc.stdout : undefined;
517
- const stderrStream = typeof proc.stderr === 'object' ? proc.stderr : undefined;
518
- const stdoutCap = opts.maxStdoutTailBytes ?? DEFAULT_STDOUT_TAIL_BYTES;
519
- const stderrCap = opts.maxStderrTailBytes ?? DEFAULT_STDERR_TAIL_BYTES;
520
-
521
- const [exitCode, stdoutResult, stderrResult] = await Promise.all([
522
- proc.exited,
523
- collectStream(stdoutStream, opts.stdoutPath, stdoutCap),
524
- collectStream(stderrStream, opts.stderrPath, stderrCap),
525
- ]);
526
- const stdout = stdoutResult.text;
527
- const stderr = stderrResult.text;
528
- const stdoutPath = stdoutResult.path;
529
- const stderrPath = stderrResult.path;
530
- const stdoutBytes = stdoutResult.totalBytes;
531
- const stderrBytes = stderrResult.totalBytes;
532
-
533
- // ── 5. Cleanup timers & listeners ──────────────────────────────────────
534
- if (timer) clearTimeout(timer);
535
- if (forceTimer) clearTimeout(forceTimer);
536
- if (signal) signal.removeEventListener('abort', onAbort);
537
-
538
- const durationMs = elapsed();
539
-
540
- // We initiated the kill (timeout or abort) — always treat as non-success
541
- // regardless of exit code. A process that catches SIGTERM and exits 0 still
542
- // hit the timeout; letting it pass as success would unblock downstream tasks
543
- // incorrectly. The `timedOut` flag guards against the narrow race where the
544
- // process exits naturally at the exact moment the timeout fires — even if
545
- // killedByUs wasn't set in time, the timeout intention still applies.
546
- if (killedByUs || timedOut) {
547
- return {
548
- exitCode: -1,
549
- stdout,
550
- stderr,
551
- stdoutPath,
552
- stderrPath,
553
- stdoutBytes,
554
- stderrBytes,
555
- durationMs,
556
- sessionId: null,
557
- normalizedOutput: null,
558
- // H2: explicit kind so engine.ts no longer has to guess "is exitCode -1
559
- // a timeout or a spawn-failure?" Both used to share the same code.
560
- failureKind: 'timeout',
561
- };
562
- }
563
-
564
- // ── 6. Let driver extract metadata ─────────────────────────────────────
565
- // R1: parseResult is third-party code — wrap it in try/catch so a buggy
566
- // extractor doesn't discard a perfectly good spawn result. R5: even on
567
- // success, type-guard sessionId/normalizedOutput so a mistyped return
568
- // value doesn't poison sessionMap/normalizedMap downstream.
569
- let sessionId: string | null = null;
570
- let normalizedOutput: string | null = null;
571
- // M12: drivers can flip a task's terminal status to failed even when the
572
- // process exited 0 (e.g. opencode returning `{type:"error"}` JSON). When
573
- // the flag is set, we synthesize a non-zero exit code and append a reason
574
- // line to stderr so engine.ts marks the task as failed with a useful
575
- // explanation instead of letting the error JSON pass through as success.
576
- let forcedFailureMessage: string | null = null;
577
- if (driver?.parseResult) {
578
- try {
579
- const meta = driver.parseResult(stdout, stderr);
580
- if (meta && typeof meta === 'object') {
581
- if (typeof meta.sessionId === 'string' && /^[\w.-]{1,256}$/.test(meta.sessionId)) {
582
- sessionId = meta.sessionId;
583
- }
584
- if (typeof meta.normalizedOutput === 'string') {
585
- normalizedOutput = meta.normalizedOutput;
586
- }
587
- if (meta.forceFailure === true) {
588
- forcedFailureMessage =
589
- typeof meta.forceFailureReason === 'string'
590
- ? meta.forceFailureReason
591
- : 'Driver flagged task as failed (forceFailure)';
592
- }
593
- }
594
- } catch (err) {
595
- // The spawn itself succeeded; only metadata extraction failed.
596
- // Fall through with sessionId/normalizedOutput = null and append a
597
- // breadcrumb to stderr so the user can see WHY continue_from broke.
598
- const msg = err instanceof Error ? err.message : String(err);
599
- const note = `\n[runner] driver "${driver.name}".parseResult threw: ${msg}`;
600
- return {
601
- exitCode,
602
- stdout,
603
- stderr: stderr + note,
604
- stdoutPath,
605
- stderrPath,
606
- stdoutBytes,
607
- stderrBytes,
608
- durationMs,
609
- sessionId: null,
610
- normalizedOutput: null,
611
- // H2: parseResult threw — the spawn itself succeeded, so the failure
612
- // is "the process exited but the driver couldn't parse it". Surface
613
- // that as exit_nonzero (when the actual exit was non-zero) or null
614
- // (when the underlying exit was 0 — UI will still mark it failed via
615
- // engine.ts because the result is incomplete).
616
- failureKind: exitCode === 0 ? null : 'exit_nonzero',
617
- };
618
- }
619
- }
620
-
621
- // M12: when the driver forced a failure, treat as exit_nonzero with the
622
- // reason appended to stderr so users see WHY the task failed without
623
- // having to dig through driver-specific JSON.
624
- if (forcedFailureMessage !== null) {
625
- return {
626
- exitCode: exitCode === 0 ? 1 : exitCode,
627
- stdout,
628
- stderr: stderr + (stderr.endsWith('\n') ? '' : '\n') + `[driver] ${forcedFailureMessage}`,
629
- stdoutPath,
630
- stderrPath,
631
- stdoutBytes,
632
- stderrBytes,
633
- durationMs,
634
- sessionId,
635
- normalizedOutput,
636
- failureKind: 'exit_nonzero',
637
- };
638
- }
639
- return {
640
- exitCode,
641
- stdout,
642
- stderr,
643
- stdoutPath,
644
- stderrPath,
645
- stdoutBytes,
646
- stderrBytes,
647
- durationMs,
648
- sessionId,
649
- normalizedOutput,
650
- // H2: success vs nonzero exit. Engine uses this to short-circuit the
651
- // timeout branch even if a third-party driver returns -1 by mistake.
652
- failureKind: exitCode === 0 ? null : 'exit_nonzero',
653
- };
654
- }
655
-
656
- export async function runCommand(
657
- command: string,
658
- cwd: string,
659
- opts: RunOptions = {},
660
- ): Promise<TaskResult> {
661
- const spec: SpawnSpec = {
662
- args: shellArgs(command),
663
- cwd,
664
- };
665
- return runSpawn(spec, null, opts);
666
- }
package/src/runtime.ts DELETED
@@ -1,20 +0,0 @@
1
- import type { DriverPlugin, SpawnSpec, TaskResult } from './types';
2
- import { runCommand, runSpawn, type RunOptions } from './runner';
3
-
4
- export type { RunOptions };
5
-
6
- export interface TagmaRuntime {
7
- runSpawn(
8
- spec: SpawnSpec,
9
- driver: DriverPlugin | null,
10
- options?: RunOptions,
11
- ): Promise<TaskResult>;
12
- runCommand(command: string, cwd: string, options?: RunOptions): Promise<TaskResult>;
13
- }
14
-
15
- export function bunRuntime(): TagmaRuntime {
16
- return {
17
- runSpawn,
18
- runCommand,
19
- };
20
- }