@tagma/sdk 0.7.4 → 0.7.6

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 (191) hide show
  1. package/README.md +60 -53
  2. package/dist/completions/file-exists.js +1 -1
  3. package/dist/completions/file-exists.js.map +1 -1
  4. package/dist/completions/output-check.d.ts.map +1 -1
  5. package/dist/completions/output-check.js +17 -4
  6. package/dist/completions/output-check.js.map +1 -1
  7. package/dist/config.d.ts +4 -4
  8. package/dist/config.d.ts.map +1 -1
  9. package/dist/config.js +2 -2
  10. package/dist/config.js.map +1 -1
  11. package/dist/dataflow.d.ts +3 -0
  12. package/dist/dataflow.d.ts.map +1 -0
  13. package/dist/dataflow.js +2 -0
  14. package/dist/dataflow.js.map +1 -0
  15. package/dist/drivers/opencode.d.ts.map +1 -1
  16. package/dist/drivers/opencode.js +23 -71
  17. package/dist/drivers/opencode.js.map +1 -1
  18. package/dist/middlewares/static-context.d.ts.map +1 -1
  19. package/dist/middlewares/static-context.js +1 -2
  20. package/dist/middlewares/static-context.js.map +1 -1
  21. package/dist/pipeline-runner.d.ts.map +1 -1
  22. package/dist/pipeline-runner.js +2 -2
  23. package/dist/pipeline-runner.js.map +1 -1
  24. package/dist/schema.d.ts.map +1 -1
  25. package/dist/schema.js +3 -4
  26. package/dist/schema.js.map +1 -1
  27. package/dist/triggers/file.d.ts.map +1 -1
  28. package/dist/triggers/file.js +1 -2
  29. package/dist/triggers/file.js.map +1 -1
  30. package/dist/triggers/manual.d.ts.map +1 -1
  31. package/dist/triggers/manual.js +1 -2
  32. package/dist/triggers/manual.js.map +1 -1
  33. package/dist/types.d.ts +1 -2
  34. package/dist/types.d.ts.map +1 -1
  35. package/dist/types.js +1 -12
  36. package/dist/types.js.map +1 -1
  37. package/dist/utils-api.d.ts +1 -1
  38. package/dist/utils-api.d.ts.map +1 -1
  39. package/dist/utils-api.js +1 -1
  40. package/dist/utils-api.js.map +1 -1
  41. package/dist/validate-raw.d.ts +4 -4
  42. package/dist/validate-raw.d.ts.map +1 -1
  43. package/dist/validate-raw.js +45 -52
  44. package/dist/validate-raw.js.map +1 -1
  45. package/package.json +11 -24
  46. package/dist/adapters/stdin-approval.d.ts +0 -2
  47. package/dist/adapters/stdin-approval.d.ts.map +0 -1
  48. package/dist/adapters/stdin-approval.js +0 -2
  49. package/dist/adapters/stdin-approval.js.map +0 -1
  50. package/dist/adapters/websocket-approval.d.ts +0 -2
  51. package/dist/adapters/websocket-approval.d.ts.map +0 -1
  52. package/dist/adapters/websocket-approval.js +0 -2
  53. package/dist/adapters/websocket-approval.js.map +0 -1
  54. package/dist/core/dataflow.d.ts +0 -23
  55. package/dist/core/dataflow.d.ts.map +0 -1
  56. package/dist/core/dataflow.js +0 -99
  57. package/dist/core/dataflow.js.map +0 -1
  58. package/dist/core/log-prune.d.ts +0 -16
  59. package/dist/core/log-prune.d.ts.map +0 -1
  60. package/dist/core/log-prune.js +0 -34
  61. package/dist/core/log-prune.js.map +0 -1
  62. package/dist/core/preflight.d.ts +0 -13
  63. package/dist/core/preflight.d.ts.map +0 -1
  64. package/dist/core/preflight.js +0 -61
  65. package/dist/core/preflight.js.map +0 -1
  66. package/dist/core/run-context.d.ts +0 -55
  67. package/dist/core/run-context.d.ts.map +0 -1
  68. package/dist/core/run-context.js +0 -158
  69. package/dist/core/run-context.js.map +0 -1
  70. package/dist/core/run-state.d.ts +0 -25
  71. package/dist/core/run-state.d.ts.map +0 -1
  72. package/dist/core/run-state.js +0 -93
  73. package/dist/core/run-state.js.map +0 -1
  74. package/dist/core/scheduler.d.ts +0 -13
  75. package/dist/core/scheduler.d.ts.map +0 -1
  76. package/dist/core/scheduler.js +0 -35
  77. package/dist/core/scheduler.js.map +0 -1
  78. package/dist/core/task-executor.d.ts +0 -13
  79. package/dist/core/task-executor.d.ts.map +0 -1
  80. package/dist/core/task-executor.js +0 -610
  81. package/dist/core/task-executor.js.map +0 -1
  82. package/dist/core/trigger-errors.d.ts +0 -9
  83. package/dist/core/trigger-errors.d.ts.map +0 -1
  84. package/dist/core/trigger-errors.js +0 -15
  85. package/dist/core/trigger-errors.js.map +0 -1
  86. package/dist/dag.d.ts +0 -45
  87. package/dist/dag.d.ts.map +0 -1
  88. package/dist/dag.js +0 -177
  89. package/dist/dag.js.map +0 -1
  90. package/dist/hooks.d.ts +0 -73
  91. package/dist/hooks.d.ts.map +0 -1
  92. package/dist/hooks.js +0 -106
  93. package/dist/hooks.js.map +0 -1
  94. package/dist/pipeline-definition.d.ts +0 -3
  95. package/dist/pipeline-definition.d.ts.map +0 -1
  96. package/dist/pipeline-definition.js +0 -4
  97. package/dist/pipeline-definition.js.map +0 -1
  98. package/dist/ports.d.ts +0 -196
  99. package/dist/ports.d.ts.map +0 -1
  100. package/dist/ports.js +0 -688
  101. package/dist/ports.js.map +0 -1
  102. package/dist/prompt-doc.d.ts +0 -70
  103. package/dist/prompt-doc.d.ts.map +0 -1
  104. package/dist/prompt-doc.js +0 -154
  105. package/dist/prompt-doc.js.map +0 -1
  106. package/dist/registry.d.ts +0 -3
  107. package/dist/registry.d.ts.map +0 -1
  108. package/dist/registry.js +0 -2
  109. package/dist/registry.js.map +0 -1
  110. package/dist/task-ref.d.ts +0 -55
  111. package/dist/task-ref.d.ts.map +0 -1
  112. package/dist/task-ref.js +0 -103
  113. package/dist/task-ref.js.map +0 -1
  114. package/dist/utils.d.ts +0 -13
  115. package/dist/utils.d.ts.map +0 -1
  116. package/dist/utils.js +0 -177
  117. package/dist/utils.js.map +0 -1
  118. package/src/adapters/stdin-approval.ts +0 -1
  119. package/src/adapters/websocket-approval.ts +0 -1
  120. package/src/approval.ts +0 -9
  121. package/src/bootstrap.ts +0 -55
  122. package/src/completions/exit-code.ts +0 -34
  123. package/src/completions/file-exists.ts +0 -66
  124. package/src/completions/output-check.test.ts +0 -50
  125. package/src/completions/output-check.ts +0 -92
  126. package/src/config-ops.test.ts +0 -70
  127. package/src/config-ops.ts +0 -328
  128. package/src/config.ts +0 -26
  129. package/src/core/dataflow.test.ts +0 -166
  130. package/src/core/dataflow.ts +0 -161
  131. package/src/core/log-prune.test.ts +0 -58
  132. package/src/core/log-prune.ts +0 -43
  133. package/src/core/preflight.test.ts +0 -49
  134. package/src/core/preflight.ts +0 -89
  135. package/src/core/run-context.test.ts +0 -291
  136. package/src/core/run-context.ts +0 -211
  137. package/src/core/run-state.test.ts +0 -98
  138. package/src/core/run-state.ts +0 -122
  139. package/src/core/scheduler.test.ts +0 -83
  140. package/src/core/scheduler.ts +0 -42
  141. package/src/core/task-executor.ts +0 -752
  142. package/src/core/trigger-errors.ts +0 -15
  143. package/src/dag.test.ts +0 -56
  144. package/src/dag.ts +0 -245
  145. package/src/drivers/opencode.ts +0 -410
  146. package/src/engine-ports-mixed.test.ts +0 -182
  147. package/src/engine-ports.test.ts +0 -210
  148. package/src/engine-task-type.test.ts +0 -56
  149. package/src/engine.ts +0 -32
  150. package/src/hooks.ts +0 -193
  151. package/src/index.ts +0 -31
  152. package/src/logger.ts +0 -2
  153. package/src/middlewares/static-context.ts +0 -49
  154. package/src/package-split.test.ts +0 -15
  155. package/src/pipeline-definition.ts +0 -5
  156. package/src/pipeline-runner.test.ts +0 -144
  157. package/src/pipeline-runner.ts +0 -194
  158. package/src/plugin-registry.test.ts +0 -448
  159. package/src/plugins.ts +0 -21
  160. package/src/ports.test.ts +0 -678
  161. package/src/ports.ts +0 -925
  162. package/src/prompt-doc.test.ts +0 -174
  163. package/src/prompt-doc.ts +0 -169
  164. package/src/registry.ts +0 -7
  165. package/src/runner.test.ts +0 -142
  166. package/src/runner.ts +0 -1
  167. package/src/runtime/adapters/stdin-approval.ts +0 -1
  168. package/src/runtime/adapters/websocket-approval.ts +0 -1
  169. package/src/runtime/bun-process-runner.ts +0 -1
  170. package/src/runtime-adapters.test.ts +0 -10
  171. package/src/runtime.ts +0 -12
  172. package/src/schema-ports.test.ts +0 -172
  173. package/src/schema.test.ts +0 -213
  174. package/src/schema.ts +0 -379
  175. package/src/tagma.test.ts +0 -317
  176. package/src/tagma.ts +0 -67
  177. package/src/task-ref.test.ts +0 -401
  178. package/src/task-ref.ts +0 -121
  179. package/src/triggers/file.test.ts +0 -79
  180. package/src/triggers/file.ts +0 -131
  181. package/src/triggers/manual.ts +0 -86
  182. package/src/types.ts +0 -18
  183. package/src/utils-api.ts +0 -8
  184. package/src/utils.test.ts +0 -28
  185. package/src/utils.ts +0 -203
  186. package/src/validate-raw-plugin-types.test.ts +0 -60
  187. package/src/validate-raw-ports.test.ts +0 -136
  188. package/src/validate-raw.ts +0 -852
  189. package/src/yaml-compiler.test.ts +0 -108
  190. package/src/yaml-compiler.ts +0 -110
  191. package/src/yaml.ts +0 -11
@@ -1,410 +0,0 @@
1
- import type {
2
- DriverPlugin,
3
- DriverCapabilities,
4
- DriverResultMeta,
5
- TaskConfig,
6
- TrackConfig,
7
- DriverContext,
8
- SpawnSpec,
9
- } from '../types';
10
-
11
- const DEFAULT_MODEL = 'opencode/big-pickle';
12
-
13
- // NOTE on Windows multi-line prompts: `opencode` resolves to `opencode.cmd`,
14
- // an npm-generated batch wrapper. cmd.exe silently truncates argv elements
15
- // at the first newline, so a multi-line prompt reaches the model as only
16
- // its first line. The SDK's runner auto-unwraps npm .cmd shims into direct
17
- // `node <js-entry>` invocations so newlines survive, and this driver can
18
- // keep using the bare `opencode` name on every platform.
19
-
20
- // tagma uses a provider-neutral reasoning_effort vocabulary (low|medium|high)
21
- // but opencode's `--variant` is provider-specific (e.g. high|max|minimal).
22
- // Map the tagma values to the closest opencode variant:
23
- // low → minimal (least thinking)
24
- // medium → <no flag, provider default>
25
- // high → high (most thinking)
26
- // Unknown values pass through unchanged so users who target a specific
27
- // opencode variant (e.g. "max") still work.
28
- const EFFORT_TO_VARIANT: Record<string, string | null> = {
29
- low: 'minimal',
30
- medium: null,
31
- high: 'high',
32
- };
33
-
34
- // ── Auto-install + free-model picker ───────────────────────────────────────
35
- //
36
- // The opencode driver is SDK-built-in, but the `opencode` CLI isn't. Two
37
- // provisioning paths:
38
- //
39
- // 1. Desktop app — the Electron shell ships a platform-matched opencode
40
- // binary under resources/opencode/bin/, prepended to the sidecar's PATH
41
- // at launch (see apps/electron/src/runtime-paths.ts). In-app updates
42
- // drop a newer copy into userData/opencode/bin/ which wins via PATH
43
- // precedence. That path resolves on the first `opencode --version`
44
- // probe below; no auto-install ever fires.
45
- //
46
- // 2. SDK direct use — when bun is on PATH we fall through to
47
- // `bun install -g opencode-ai`, identical to the pre-desktop behavior.
48
- //
49
- // When BOTH paths are unavailable (no bundled binary, no bun) we fail with
50
- // an actionable error pointing at the desktop Settings panel instead of
51
- // silently letting `opencode run` ENOENT later — the old behavior swallowed
52
- // the root cause in runCapture's catch and left the user staring at an
53
- // opaque "exit code -1". The result is process-memoized so subsequent
54
- // tasks in the same run surface the same error without re-probing.
55
-
56
- interface OpencodeModelInfo {
57
- id?: string;
58
- providerID?: string;
59
- status?: string;
60
- cost?: { input?: number; output?: number };
61
- limit?: { context?: number };
62
- }
63
-
64
- // Memoize BOTH success and failure. On failure we stash the message so every
65
- // subsequent ensureOpencodeInstalled() throws the identical error — re-running
66
- // the bun-install probe for each task of a failed run would just be slow and
67
- // produce confusing interleaved stderr.
68
- let opencodeReady: boolean | undefined;
69
- let opencodeReadyError: string | undefined;
70
- let cachedDefaultModel: string | undefined;
71
-
72
- async function runCapture(
73
- args: string[],
74
- ): Promise<{ code: number; stdout: string; stderr: string }> {
75
- try {
76
- const proc = Bun.spawn(args, { stdout: 'pipe', stderr: 'pipe' });
77
- const [stdout, stderr, code] = await Promise.all([
78
- new Response(proc.stdout).text(),
79
- new Response(proc.stderr).text(),
80
- proc.exited,
81
- ]);
82
- return { code, stdout, stderr };
83
- } catch {
84
- return { code: -1, stdout: '', stderr: '' };
85
- }
86
- }
87
-
88
- // Shared tail for every failure message — the Tagma desktop app exposes a
89
- // one-click installer at the same npm source path this driver would reach
90
- // for, so point users there first. Users running the SDK as a library still
91
- // see the manual bun/npm hint.
92
- const SETUP_HINT =
93
- 'If you are using the Tagma desktop app, open Editor Settings → OpenCode CLI to install or update the bundled binary. ' +
94
- 'Otherwise install it manually: `bun install -g opencode-ai` or `npm install -g opencode-ai`.';
95
-
96
- async function ensureOpencodeInstalled(): Promise<void> {
97
- if (opencodeReady === true) return;
98
- if (opencodeReady === false && opencodeReadyError) {
99
- throw new Error(opencodeReadyError);
100
- }
101
-
102
- // Probe existing install first — this is the hot path for desktop users
103
- // (bundled binary in PATH) and for anyone who already has opencode.
104
- const probe = await runCapture(['opencode', '--version']);
105
- if (probe.code === 0) {
106
- opencodeReady = true;
107
- return;
108
- }
109
-
110
- // Distinguish "bun is missing" from "bun is here but install failed" so
111
- // the error we surface points at the right next step. If bun is absent we
112
- // skip the install attempt entirely — spawning with `bun` as argv[0]
113
- // would just ENOENT inside runCapture's catch and look identical to a
114
- // failed install.
115
- const bunProbe = await runCapture(['bun', '--version']);
116
- if (bunProbe.code !== 0) {
117
- opencodeReady = false;
118
- opencodeReadyError = `OpenCode CLI is not available and \`bun\` is not installed. ${SETUP_HINT}`;
119
- throw new Error(opencodeReadyError);
120
- }
121
-
122
- console.error(
123
- '[driver:opencode] opencode CLI not found — installing via `bun install -g opencode-ai`... (this may take up to a minute)',
124
- );
125
- // Use inherit here so the user sees bun's own progress during the one-time
126
- // install; runCapture would swallow it.
127
- const install = Bun.spawn(['bun', 'install', '-g', 'opencode-ai'], {
128
- stdout: 'inherit',
129
- stderr: 'inherit',
130
- });
131
- const installCode = await install.exited;
132
- if (installCode !== 0) {
133
- opencodeReady = false;
134
- opencodeReadyError = `\`bun install -g opencode-ai\` failed (exit code ${installCode}). ${SETUP_HINT}`;
135
- throw new Error(opencodeReadyError);
136
- }
137
-
138
- // Bun installs globals under `~/.bun/bin` (or `%USERPROFILE%\.bun\bin`),
139
- // which isn't on this process's cached PATH unless the user already has
140
- // bun set up. Ask bun for the directory and prepend it so bare `opencode`
141
- // resolves in this process without requiring a shell reload.
142
- const bin = await runCapture(['bun', 'pm', 'bin', '-g']);
143
- if (bin.code === 0) {
144
- const dir = bin.stdout.trim();
145
- const sep = process.platform === 'win32' ? ';' : ':';
146
- const current = process.env.PATH ?? '';
147
- if (dir && !current.split(sep).includes(dir)) {
148
- process.env.PATH = `${dir}${sep}${current}`;
149
- }
150
- }
151
-
152
- const verify = await runCapture(['opencode', '--version']);
153
- if (verify.code !== 0) {
154
- opencodeReady = false;
155
- opencodeReadyError =
156
- '`opencode` is not resolvable after `bun install -g opencode-ai` completed. ' +
157
- "Bun's global bin directory is probably not on PATH — add it manually or restart the app.";
158
- throw new Error(opencodeReadyError);
159
- }
160
- opencodeReady = true;
161
- }
162
-
163
- // `opencode models --verbose` emits "<provider>/<id>\n{...json...}\n" pairs.
164
- // Walk balanced braces rather than split on newlines so we survive any
165
- // whitespace oddities in the JSON payload.
166
- function parseVerboseModels(stdout: string): OpencodeModelInfo[] {
167
- const out: OpencodeModelInfo[] = [];
168
- let depth = 0;
169
- let start = -1;
170
- for (let i = 0; i < stdout.length; i++) {
171
- const c = stdout[i];
172
- if (c === '{') {
173
- if (depth === 0) start = i;
174
- depth++;
175
- } else if (c === '}') {
176
- depth--;
177
- if (depth === 0 && start !== -1) {
178
- try {
179
- out.push(JSON.parse(stdout.slice(start, i + 1)) as OpencodeModelInfo);
180
- } catch {
181
- /* skip malformed block */
182
- }
183
- start = -1;
184
- }
185
- }
186
- }
187
- return out;
188
- }
189
-
190
- function pickFreeModel(models: OpencodeModelInfo[]): string | null {
191
- const fullId = (m: OpencodeModelInfo): string =>
192
- `${m.providerID ?? 'opencode'}/${m.id ?? ''}`;
193
- const eligible = models.filter((m) => {
194
- if (!m.id || m.id === 'big-pickle') return false;
195
- if (m.status && m.status !== 'active') return false;
196
- const cost = m.cost;
197
- if (!cost || cost.input !== 0 || cost.output !== 0) return false;
198
- const ctx = m.limit?.context;
199
- if (typeof ctx !== 'number' || ctx <= 128000) return false;
200
- return true;
201
- });
202
- // Prefer models explicitly labelled "-free" by the provider — those are
203
- // a stronger stability signal than "cost happens to be 0 right now".
204
- const preferred = eligible.filter((m) => m.id?.endsWith('-free'));
205
- const pool = preferred.length > 0 ? preferred : eligible;
206
- if (pool.length === 0) return null;
207
- // Deterministic pick: sort by full id so upstream model-list reordering
208
- // doesn't flip our choice between runs.
209
- pool.sort((a, b) => fullId(a).localeCompare(fullId(b)));
210
- return fullId(pool[0]);
211
- }
212
-
213
- async function resolveDefaultModel(): Promise<string> {
214
- if (cachedDefaultModel !== undefined) return cachedDefaultModel;
215
- // ensureOpencodeInstalled now throws with an actionable message when the
216
- // CLI can't be provisioned, so we let the error bubble up to the task
217
- // runner instead of silently falling back to DEFAULT_MODEL (which would
218
- // produce a second confusing ENOENT a few lines later in `opencode run`).
219
- await ensureOpencodeInstalled();
220
- console.error('[driver:opencode] resolving free opencode model...');
221
- const { code, stdout } = await runCapture(['opencode', 'models', '--verbose']);
222
- if (code !== 0) {
223
- cachedDefaultModel = DEFAULT_MODEL;
224
- return cachedDefaultModel;
225
- }
226
- const picked = pickFreeModel(parseVerboseModels(stdout));
227
- cachedDefaultModel = picked ?? DEFAULT_MODEL;
228
- console.error(`[driver:opencode] default model: ${cachedDefaultModel}`);
229
- return cachedDefaultModel;
230
- }
231
-
232
- export const OpenCodeDriver: DriverPlugin = {
233
- name: 'opencode',
234
-
235
- capabilities: {
236
- sessionResume: true, // supports --session
237
- systemPrompt: false, // no --system-prompt flag; prepend to prompt instead
238
- outputFormat: true, // supports --format json
239
- } satisfies DriverCapabilities,
240
-
241
- resolveModel(): string {
242
- return DEFAULT_MODEL;
243
- },
244
-
245
- async buildCommand(task: TaskConfig, track: TrackConfig, ctx: DriverContext): Promise<SpawnSpec> {
246
- const explicitModel = task.model ?? track.model;
247
- // Always make sure the opencode CLI is usable before we spawn it — even
248
- // when the user pinned a model. ensureOpencodeInstalled throws with an
249
- // actionable message when the binary is neither present on PATH (desktop
250
- // bundles it there via runtime-paths.ts) nor installable via bun.
251
- if (explicitModel) await ensureOpencodeInstalled();
252
- // Otherwise resolveDefaultModel both ensures the CLI and picks a free
253
- // model from `opencode models --verbose` (cached per-process).
254
- const model = explicitModel ?? (await resolveDefaultModel());
255
- // Resolve reasoning_effort → opencode --variant. SDK schema layer already
256
- // resolved task → track → pipeline inheritance, so we only need to read
257
- // task.reasoning_effort here.
258
- const rawEffort = task.reasoning_effort ?? track.reasoning_effort;
259
- const variant = rawEffort
260
- ? rawEffort in EFFORT_TO_VARIANT
261
- ? EFFORT_TO_VARIANT[rawEffort]
262
- : rawEffort
263
- : null;
264
-
265
- let prompt = task.prompt!;
266
-
267
- // agent_profile has no dedicated flag; prepend to prompt
268
- const profile = task.agent_profile ?? track.agent_profile;
269
- if (profile) {
270
- prompt = `[Role]\n${profile}\n\n[Task]\n${prompt}`;
271
- }
272
-
273
- // continue_from: prefer session resume, fall back to text injection
274
- let sessionId: string | null = null;
275
- if (task.continue_from) {
276
- sessionId = ctx.sessionMap.get(task.continue_from) ?? null;
277
- if (!sessionId) {
278
- // no session — degrade to text context passthrough
279
- let prev: string | null = null;
280
- if (ctx.normalizedMap.has(task.continue_from)) {
281
- prev = ctx.normalizedMap.get(task.continue_from)!;
282
- }
283
- if (prev !== null) {
284
- prompt = `[Previous Output]\n${prev}\n\n[Current Task]\n${prompt}`;
285
- }
286
- }
287
- }
288
-
289
- // opencode run does not support stdin (no `-` placeholder like codex exec).
290
- // Prompt is always a positional argument. Flags must be declared before `--`;
291
- // the prompt follows after so that leading `--flag` content cannot be
292
- // misread by opencode's argument parser (flag-injection mitigation).
293
- // Shell-level injection is already prevented by Bun.spawn's direct argv array.
294
- // Windows cmd.exe argv truncation on the `.cmd` wrapper is handled by the
295
- // SDK runner's shim unwrapping — see note at the top of this file.
296
- const args: string[] = [
297
- 'opencode',
298
- 'run',
299
- '--model',
300
- model,
301
- '--format',
302
- 'json', // JSON output for parseResult
303
- ];
304
-
305
- // `--variant` must precede `--` like every other flag. opencode rejects
306
- // unknown variant names with a clear error, so we don't pre-validate.
307
- if (variant) {
308
- args.push('--variant', variant);
309
- }
310
-
311
- // session resume (must appear before --)
312
- if (sessionId) {
313
- args.push('--session', sessionId);
314
- }
315
-
316
- // `--` (POSIX end-of-options) isolates prompt from flag parsing
317
- args.push('--', prompt);
318
-
319
- return { args, cwd: task.cwd ?? ctx.workDir };
320
- },
321
-
322
- parseResult(stdout: string): DriverResultMeta {
323
- // opencode --format json emits NDJSON — one JSON object per line
324
- // (step_start / text / step_finish / …). The previous single
325
- // `JSON.parse(stdout)` always threw on this shape and fell through to
326
- // the catch, returning sessionId:null and losing session resume.
327
- // Walk line-by-line, pick up the first sessionID we see, concatenate
328
- // any text-type parts into normalizedOutput, and bail early on error
329
- // payloads.
330
- const lines = stdout.split(/\r?\n/);
331
- let sessionId: string | undefined;
332
- const textParts: string[] = [];
333
- let sawAnyJson = false;
334
- let errorReason: string | null = null;
335
-
336
- for (const raw of lines) {
337
- const line = raw.trim();
338
- if (!line) continue;
339
- let json: Record<string, unknown>;
340
- try {
341
- json = JSON.parse(line) as Record<string, unknown>;
342
- } catch {
343
- continue; // tolerate interleaved non-JSON noise
344
- }
345
- sawAnyJson = true;
346
-
347
- // M12: opencode sometimes emits {type:"error", error:{...}} with
348
- // exit 0 for transient API failures. Force-fail so downstream
349
- // skip_downstream / stop_all kicks in.
350
- if (json.type === 'error') {
351
- const err = json.error as { message?: unknown } | string | undefined;
352
- const msg =
353
- typeof err === 'object' && err !== null && typeof err.message === 'string'
354
- ? err.message
355
- : typeof err === 'string'
356
- ? err
357
- : null;
358
- errorReason = msg
359
- ? `opencode reported error: ${msg}`
360
- : 'opencode emitted an error JSON payload';
361
- // D21: stop at the first error. Continuing meant subsequent text
362
- // lines got accumulated into `textParts` only to be discarded by
363
- // the error-return below, and a later `{type:"error"}` would
364
- // silently overwrite the original cause — operators then debugged
365
- // a downstream symptom while the root-cause line scrolled past.
366
- break;
367
- }
368
-
369
- // Session id — opencode uses `sessionID` (camelCase with capital D).
370
- // Keep `session_id` / `sessionId` as fallbacks for forward/backward
371
- // compatibility with other shapes.
372
- if (!sessionId) {
373
- const sid =
374
- (json.sessionID as string | undefined) ??
375
- (json.session_id as string | undefined) ??
376
- (json.sessionId as string | undefined) ??
377
- null;
378
- if (typeof sid === 'string' && sid.length > 0) sessionId = sid;
379
- }
380
-
381
- // Extract human-readable text from text-type parts.
382
- if (json.type === 'text') {
383
- const part = json.part as { text?: unknown } | undefined;
384
- if (part && typeof part.text === 'string') {
385
- textParts.push(part.text);
386
- }
387
- } else if (typeof json.result === 'string') {
388
- textParts.push(json.result);
389
- } else if (typeof json.content === 'string') {
390
- textParts.push(json.content);
391
- }
392
- }
393
-
394
- if (errorReason) {
395
- return { forceFailure: true, forceFailureReason: errorReason };
396
- }
397
-
398
- // If nothing parsed as JSON, treat stdout as plain text.
399
- const normalizedOutput = !sawAnyJson
400
- ? stdout
401
- : textParts.length > 0
402
- ? textParts.join('\n')
403
- : stdout;
404
-
405
- return {
406
- sessionId,
407
- normalizedOutput,
408
- };
409
- },
410
- };
@@ -1,182 +0,0 @@
1
- import { describe, expect, test } from 'bun:test';
2
- import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
3
- import { tmpdir } from 'node:os';
4
- import { join } from 'node:path';
5
- import { bootstrapBuiltins } from './bootstrap';
6
- import { runPipeline, type RunEventPayload } from './engine';
7
- import { PluginRegistry } from './registry';
8
- import type { DriverPlugin, PipelineConfig, TagmaRuntime, TaskConfig, TaskResult } from './types';
9
-
10
- const PERMS = { read: true, write: false, execute: false };
11
-
12
- function makeDir(): string {
13
- return mkdtempSync(join(tmpdir(), 'tagma-bindings-mixed-'));
14
- }
15
-
16
- function registry(responses: Record<string, Record<string, unknown>>, records: Record<string, string>) {
17
- const reg = new PluginRegistry();
18
- bootstrapBuiltins(reg);
19
- const driver: DriverPlugin = {
20
- name: 'mock',
21
- capabilities: { sessionResume: false, systemPrompt: true, outputFormat: true },
22
- async buildCommand(task) {
23
- return {
24
- args: ['mock-driver', task.id],
25
- stdin: task.prompt ?? '',
26
- env: {
27
- MOCK_RESPONSE: JSON.stringify(responses[task.id] ?? {}),
28
- MOCK_RECORD_PATH: records[task.id] ?? join(process.cwd(), 'prompt.txt'),
29
- },
30
- };
31
- },
32
- parseResult(stdout) {
33
- return { normalizedOutput: stdout.trim() };
34
- },
35
- };
36
- reg.registerPlugin('drivers', 'mock', driver);
37
- return reg;
38
- }
39
-
40
- function task(overrides: Partial<TaskConfig> & { id: string }): TaskConfig {
41
- return { name: overrides.id, permissions: PERMS, driver: 'mock', ...overrides };
42
- }
43
-
44
- function pipeline(tasks: TaskConfig[]): PipelineConfig {
45
- return {
46
- name: 'mixed-bindings-test',
47
- tracks: [{ id: 't', name: 'T', permissions: PERMS, driver: 'mock', tasks }],
48
- };
49
- }
50
-
51
- async function run(config: PipelineConfig, workDir: string, reg: PluginRegistry) {
52
- const events: RunEventPayload[] = [];
53
- const result = await runPipeline(config, workDir, {
54
- registry: reg,
55
- runtime: fakeRuntime(),
56
- skipPluginLoading: true,
57
- onEvent: (e) => events.push(e),
58
- });
59
- return { events, success: result.success };
60
- }
61
-
62
- function taskResult(stdout: string, normalizedOutput: string | null = null): TaskResult {
63
- return {
64
- exitCode: 0,
65
- stdout,
66
- stderr: '',
67
- stdoutPath: null,
68
- stderrPath: null,
69
- stdoutBytes: stdout.length,
70
- stderrBytes: 0,
71
- durationMs: 1,
72
- sessionId: null,
73
- normalizedOutput,
74
- failureKind: null,
75
- };
76
- }
77
-
78
- function fakeRuntime(): TagmaRuntime {
79
- return {
80
- async runCommand(command) {
81
- if (command.startsWith('emit-city')) return taskResult('{"city":"Berlin"}\n');
82
- return taskResult('ok\n');
83
- },
84
- async runSpawn(spec) {
85
- const response = spec.env?.['MOCK_RESPONSE'] ?? '{}';
86
- const recordPath = spec.env?.['MOCK_RECORD_PATH'];
87
- if (recordPath) writeFileSync(recordPath, spec.stdin ?? '');
88
- return taskResult(response + '\n', response);
89
- },
90
- async ensureDir() {
91
- /* no-op */
92
- },
93
- async fileExists() {
94
- return false;
95
- },
96
- async *watch() {
97
- /* no-op */
98
- },
99
- logStore: {
100
- openRunLog({ runId }) {
101
- return {
102
- path: `mem://${runId}/pipeline.log`,
103
- dir: `mem://${runId}`,
104
- append() {
105
- /* memory sink */
106
- },
107
- close() {
108
- /* memory sink */
109
- },
110
- };
111
- },
112
- taskOutputPath({ runId, taskId, stream }) {
113
- return `mem://${runId}/${taskId}.${stream}`;
114
- },
115
- logsDir() {
116
- return 'mem://logs';
117
- },
118
- },
119
- now: () => new Date('2026-04-26T00:00:00.000Z'),
120
- sleep: () => Promise.resolve(),
121
- };
122
- }
123
-
124
- function finalUpdateFor(events: RunEventPayload[], qid: string): RunEventPayload | undefined {
125
- let last: RunEventPayload | undefined;
126
- for (const ev of events) {
127
- if (ev.type === 'task_update' && ev.taskId === qid) last = ev;
128
- }
129
- return last;
130
- }
131
-
132
- describe('engine — mixed prompt/command unified bindings', () => {
133
- test('prompt outputs are inferred from downstream command inputs', async () => {
134
- const dir = makeDir();
135
- try {
136
- const record = join(dir, 'prompt.txt');
137
- const reg = registry({ plan: { city: 'Paris' } }, { plan: record });
138
- const config = pipeline([
139
- task({ id: 'plan', prompt: 'Pick a city' }),
140
- task({
141
- id: 'fetch',
142
- driver: 'opencode',
143
- depends_on: ['plan'],
144
- command: 'echo-city "{{inputs.city}}"',
145
- inputs: { city: { from: 't.plan.outputs.city', type: 'string', required: true } },
146
- }),
147
- ]);
148
-
149
- const { events, success } = await run(config, dir, reg);
150
- expect(success).toBe(true);
151
- expect(readFileSync(record, 'utf8')).toContain('[Output Format]');
152
- expect(finalUpdateFor(events, 't.plan')?.outputs).toEqual({ city: 'Paris' });
153
- expect(finalUpdateFor(events, 't.fetch')?.inputs).toEqual({ city: 'Paris' });
154
- } finally {
155
- rmSync(dir, { recursive: true, force: true });
156
- }
157
- });
158
-
159
- test('prompt inputs are inferred from upstream command outputs', async () => {
160
- const dir = makeDir();
161
- try {
162
- const record = join(dir, 'prompt.txt');
163
- const reg = registry({ summarize: {} }, { summarize: record });
164
- const config = pipeline([
165
- task({
166
- id: 'up',
167
- driver: 'opencode',
168
- command: 'emit-city',
169
- outputs: { city: { type: 'string' } },
170
- }),
171
- task({ id: 'summarize', depends_on: ['up'], prompt: 'City is {{inputs.city}}' }),
172
- ]);
173
-
174
- const { events, success } = await run(config, dir, reg);
175
- expect(success).toBe(true);
176
- expect(readFileSync(record, 'utf8')).toContain('City is Berlin');
177
- expect(finalUpdateFor(events, 't.summarize')?.inputs).toEqual({ city: 'Berlin' });
178
- } finally {
179
- rmSync(dir, { recursive: true, force: true });
180
- }
181
- });
182
- });