opencastle 0.27.0 → 0.27.2

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 (242) hide show
  1. package/bin/cli.mjs +6 -0
  2. package/dist/cli/agents.d.ts +3 -0
  3. package/dist/cli/agents.d.ts.map +1 -0
  4. package/dist/cli/agents.js +161 -0
  5. package/dist/cli/agents.js.map +1 -0
  6. package/dist/cli/baselines.d.ts +3 -0
  7. package/dist/cli/baselines.d.ts.map +1 -0
  8. package/dist/cli/baselines.js +128 -0
  9. package/dist/cli/baselines.js.map +1 -0
  10. package/dist/cli/convoy/dashboard-types.d.ts +146 -0
  11. package/dist/cli/convoy/dashboard-types.d.ts.map +1 -0
  12. package/dist/cli/convoy/dashboard-types.js +2 -0
  13. package/dist/cli/convoy/dashboard-types.js.map +1 -0
  14. package/dist/cli/convoy/engine.d.ts +67 -2
  15. package/dist/cli/convoy/engine.d.ts.map +1 -1
  16. package/dist/cli/convoy/engine.js +2036 -28
  17. package/dist/cli/convoy/engine.js.map +1 -1
  18. package/dist/cli/convoy/engine.test.js +1659 -70
  19. package/dist/cli/convoy/engine.test.js.map +1 -1
  20. package/dist/cli/convoy/event-schemas.d.ts +9 -0
  21. package/dist/cli/convoy/event-schemas.d.ts.map +1 -0
  22. package/dist/cli/convoy/event-schemas.js +185 -0
  23. package/dist/cli/convoy/event-schemas.js.map +1 -0
  24. package/dist/cli/convoy/events.d.ts +12 -1
  25. package/dist/cli/convoy/events.d.ts.map +1 -1
  26. package/dist/cli/convoy/events.js +186 -13
  27. package/dist/cli/convoy/events.js.map +1 -1
  28. package/dist/cli/convoy/events.test.js +325 -28
  29. package/dist/cli/convoy/events.test.js.map +1 -1
  30. package/dist/cli/convoy/expertise.d.ts +16 -0
  31. package/dist/cli/convoy/expertise.d.ts.map +1 -0
  32. package/dist/cli/convoy/expertise.js +121 -0
  33. package/dist/cli/convoy/expertise.js.map +1 -0
  34. package/dist/cli/convoy/expertise.test.d.ts +2 -0
  35. package/dist/cli/convoy/expertise.test.d.ts.map +1 -0
  36. package/dist/cli/convoy/expertise.test.js +96 -0
  37. package/dist/cli/convoy/expertise.test.js.map +1 -0
  38. package/dist/cli/convoy/export.test.js +1 -0
  39. package/dist/cli/convoy/export.test.js.map +1 -1
  40. package/dist/cli/convoy/formula.d.ts +19 -0
  41. package/dist/cli/convoy/formula.d.ts.map +1 -0
  42. package/dist/cli/convoy/formula.js +142 -0
  43. package/dist/cli/convoy/formula.js.map +1 -0
  44. package/dist/cli/convoy/formula.test.d.ts +2 -0
  45. package/dist/cli/convoy/formula.test.d.ts.map +1 -0
  46. package/dist/cli/convoy/formula.test.js +342 -0
  47. package/dist/cli/convoy/formula.test.js.map +1 -0
  48. package/dist/cli/convoy/gates.d.ts +128 -0
  49. package/dist/cli/convoy/gates.d.ts.map +1 -0
  50. package/dist/cli/convoy/gates.js +606 -0
  51. package/dist/cli/convoy/gates.js.map +1 -0
  52. package/dist/cli/convoy/gates.test.d.ts +2 -0
  53. package/dist/cli/convoy/gates.test.d.ts.map +1 -0
  54. package/dist/cli/convoy/gates.test.js +976 -0
  55. package/dist/cli/convoy/gates.test.js.map +1 -0
  56. package/dist/cli/convoy/health.d.ts +11 -0
  57. package/dist/cli/convoy/health.d.ts.map +1 -1
  58. package/dist/cli/convoy/health.js +54 -0
  59. package/dist/cli/convoy/health.js.map +1 -1
  60. package/dist/cli/convoy/health.test.js +56 -1
  61. package/dist/cli/convoy/health.test.js.map +1 -1
  62. package/dist/cli/convoy/issues.d.ts +8 -0
  63. package/dist/cli/convoy/issues.d.ts.map +1 -0
  64. package/dist/cli/convoy/issues.js +98 -0
  65. package/dist/cli/convoy/issues.js.map +1 -0
  66. package/dist/cli/convoy/issues.test.d.ts +2 -0
  67. package/dist/cli/convoy/issues.test.d.ts.map +1 -0
  68. package/dist/cli/convoy/issues.test.js +107 -0
  69. package/dist/cli/convoy/issues.test.js.map +1 -0
  70. package/dist/cli/convoy/knowledge.d.ts +5 -0
  71. package/dist/cli/convoy/knowledge.d.ts.map +1 -0
  72. package/dist/cli/convoy/knowledge.js +116 -0
  73. package/dist/cli/convoy/knowledge.js.map +1 -0
  74. package/dist/cli/convoy/knowledge.test.d.ts +2 -0
  75. package/dist/cli/convoy/knowledge.test.d.ts.map +1 -0
  76. package/dist/cli/convoy/knowledge.test.js +87 -0
  77. package/dist/cli/convoy/knowledge.test.js.map +1 -0
  78. package/dist/cli/convoy/lessons.d.ts +17 -0
  79. package/dist/cli/convoy/lessons.d.ts.map +1 -0
  80. package/dist/cli/convoy/lessons.js +149 -0
  81. package/dist/cli/convoy/lessons.js.map +1 -0
  82. package/dist/cli/convoy/lessons.test.d.ts +2 -0
  83. package/dist/cli/convoy/lessons.test.d.ts.map +1 -0
  84. package/dist/cli/convoy/lessons.test.js +135 -0
  85. package/dist/cli/convoy/lessons.test.js.map +1 -0
  86. package/dist/cli/convoy/lock.d.ts +13 -0
  87. package/dist/cli/convoy/lock.d.ts.map +1 -0
  88. package/dist/cli/convoy/lock.js +88 -0
  89. package/dist/cli/convoy/lock.js.map +1 -0
  90. package/dist/cli/convoy/lock.test.d.ts +2 -0
  91. package/dist/cli/convoy/lock.test.d.ts.map +1 -0
  92. package/dist/cli/convoy/lock.test.js +136 -0
  93. package/dist/cli/convoy/lock.test.js.map +1 -0
  94. package/dist/cli/convoy/log-merge.test.d.ts +2 -0
  95. package/dist/cli/convoy/log-merge.test.d.ts.map +1 -0
  96. package/dist/cli/convoy/log-merge.test.js +147 -0
  97. package/dist/cli/convoy/log-merge.test.js.map +1 -0
  98. package/dist/cli/convoy/merge.d.ts +4 -0
  99. package/dist/cli/convoy/merge.d.ts.map +1 -1
  100. package/dist/cli/convoy/merge.js +18 -1
  101. package/dist/cli/convoy/merge.js.map +1 -1
  102. package/dist/cli/convoy/merge.test.js +6 -7
  103. package/dist/cli/convoy/merge.test.js.map +1 -1
  104. package/dist/cli/convoy/partition.d.ts +51 -0
  105. package/dist/cli/convoy/partition.d.ts.map +1 -0
  106. package/dist/cli/convoy/partition.js +186 -0
  107. package/dist/cli/convoy/partition.js.map +1 -0
  108. package/dist/cli/convoy/partition.test.d.ts +2 -0
  109. package/dist/cli/convoy/partition.test.d.ts.map +1 -0
  110. package/dist/cli/convoy/partition.test.js +315 -0
  111. package/dist/cli/convoy/partition.test.js.map +1 -0
  112. package/dist/cli/convoy/pipeline.test.js +6 -0
  113. package/dist/cli/convoy/pipeline.test.js.map +1 -1
  114. package/dist/cli/convoy/store.d.ts +99 -7
  115. package/dist/cli/convoy/store.d.ts.map +1 -1
  116. package/dist/cli/convoy/store.js +764 -31
  117. package/dist/cli/convoy/store.js.map +1 -1
  118. package/dist/cli/convoy/store.test.js +1810 -18
  119. package/dist/cli/convoy/store.test.js.map +1 -1
  120. package/dist/cli/convoy/types.d.ts +427 -5
  121. package/dist/cli/convoy/types.d.ts.map +1 -1
  122. package/dist/cli/convoy/types.js +42 -1
  123. package/dist/cli/convoy/types.js.map +1 -1
  124. package/dist/cli/log.d.ts +11 -0
  125. package/dist/cli/log.d.ts.map +1 -1
  126. package/dist/cli/log.js +114 -2
  127. package/dist/cli/log.js.map +1 -1
  128. package/dist/cli/run/adapters/claude.d.ts +2 -0
  129. package/dist/cli/run/adapters/claude.d.ts.map +1 -1
  130. package/dist/cli/run/adapters/claude.js +89 -49
  131. package/dist/cli/run/adapters/claude.js.map +1 -1
  132. package/dist/cli/run/adapters/claude.test.d.ts +2 -0
  133. package/dist/cli/run/adapters/claude.test.d.ts.map +1 -0
  134. package/dist/cli/run/adapters/claude.test.js +205 -0
  135. package/dist/cli/run/adapters/claude.test.js.map +1 -0
  136. package/dist/cli/run/adapters/copilot.d.ts +1 -0
  137. package/dist/cli/run/adapters/copilot.d.ts.map +1 -1
  138. package/dist/cli/run/adapters/copilot.js +84 -46
  139. package/dist/cli/run/adapters/copilot.js.map +1 -1
  140. package/dist/cli/run/adapters/copilot.test.d.ts +2 -0
  141. package/dist/cli/run/adapters/copilot.test.d.ts.map +1 -0
  142. package/dist/cli/run/adapters/copilot.test.js +195 -0
  143. package/dist/cli/run/adapters/copilot.test.js.map +1 -0
  144. package/dist/cli/run/adapters/cursor.d.ts +1 -0
  145. package/dist/cli/run/adapters/cursor.d.ts.map +1 -1
  146. package/dist/cli/run/adapters/cursor.js +83 -47
  147. package/dist/cli/run/adapters/cursor.js.map +1 -1
  148. package/dist/cli/run/adapters/cursor.test.d.ts +2 -0
  149. package/dist/cli/run/adapters/cursor.test.d.ts.map +1 -0
  150. package/dist/cli/run/adapters/cursor.test.js +129 -0
  151. package/dist/cli/run/adapters/cursor.test.js.map +1 -0
  152. package/dist/cli/run/adapters/opencode.d.ts +1 -0
  153. package/dist/cli/run/adapters/opencode.d.ts.map +1 -1
  154. package/dist/cli/run/adapters/opencode.js +81 -47
  155. package/dist/cli/run/adapters/opencode.js.map +1 -1
  156. package/dist/cli/run/adapters/opencode.test.d.ts +2 -0
  157. package/dist/cli/run/adapters/opencode.test.d.ts.map +1 -0
  158. package/dist/cli/run/adapters/opencode.test.js +119 -0
  159. package/dist/cli/run/adapters/opencode.test.js.map +1 -0
  160. package/dist/cli/run/executor.js +1 -1
  161. package/dist/cli/run/executor.js.map +1 -1
  162. package/dist/cli/run/schema.d.ts.map +1 -1
  163. package/dist/cli/run/schema.js +245 -4
  164. package/dist/cli/run/schema.js.map +1 -1
  165. package/dist/cli/run/schema.test.js +669 -0
  166. package/dist/cli/run/schema.test.js.map +1 -1
  167. package/dist/cli/run.d.ts.map +1 -1
  168. package/dist/cli/run.js +362 -22
  169. package/dist/cli/run.js.map +1 -1
  170. package/dist/cli/types.d.ts +85 -2
  171. package/dist/cli/types.d.ts.map +1 -1
  172. package/dist/cli/types.js.map +1 -1
  173. package/dist/cli/watch.d.ts +15 -0
  174. package/dist/cli/watch.d.ts.map +1 -0
  175. package/dist/cli/watch.js +279 -0
  176. package/dist/cli/watch.js.map +1 -0
  177. package/package.json +5 -1
  178. package/src/cli/agents.ts +177 -0
  179. package/src/cli/baselines.ts +143 -0
  180. package/src/cli/convoy/TELEMETRY.md +203 -0
  181. package/src/cli/convoy/dashboard-types.ts +141 -0
  182. package/src/cli/convoy/engine.test.ts +1937 -70
  183. package/src/cli/convoy/engine.ts +2350 -40
  184. package/src/cli/convoy/event-schemas.ts +195 -0
  185. package/src/cli/convoy/events.test.ts +384 -39
  186. package/src/cli/convoy/events.ts +202 -16
  187. package/src/cli/convoy/expertise.test.ts +128 -0
  188. package/src/cli/convoy/expertise.ts +163 -0
  189. package/src/cli/convoy/export.test.ts +1 -0
  190. package/src/cli/convoy/formula.test.ts +405 -0
  191. package/src/cli/convoy/formula.ts +174 -0
  192. package/src/cli/convoy/gates.test.ts +1169 -0
  193. package/src/cli/convoy/gates.ts +774 -0
  194. package/src/cli/convoy/health.test.ts +64 -2
  195. package/src/cli/convoy/health.ts +80 -2
  196. package/src/cli/convoy/issues.test.ts +143 -0
  197. package/src/cli/convoy/issues.ts +136 -0
  198. package/src/cli/convoy/knowledge.test.ts +101 -0
  199. package/src/cli/convoy/knowledge.ts +132 -0
  200. package/src/cli/convoy/lessons.test.ts +188 -0
  201. package/src/cli/convoy/lessons.ts +164 -0
  202. package/src/cli/convoy/lock.test.ts +181 -0
  203. package/src/cli/convoy/lock.ts +103 -0
  204. package/src/cli/convoy/log-merge.test.ts +179 -0
  205. package/src/cli/convoy/merge.test.ts +6 -7
  206. package/src/cli/convoy/merge.ts +19 -1
  207. package/src/cli/convoy/partition.test.ts +423 -0
  208. package/src/cli/convoy/partition.ts +232 -0
  209. package/src/cli/convoy/pipeline.test.ts +6 -0
  210. package/src/cli/convoy/store.test.ts +2041 -20
  211. package/src/cli/convoy/store.ts +945 -46
  212. package/src/cli/convoy/types.ts +278 -4
  213. package/src/cli/log.ts +120 -2
  214. package/src/cli/run/adapters/claude.test.ts +234 -0
  215. package/src/cli/run/adapters/claude.ts +45 -5
  216. package/src/cli/run/adapters/copilot.test.ts +224 -0
  217. package/src/cli/run/adapters/copilot.ts +34 -4
  218. package/src/cli/run/adapters/cursor.test.ts +144 -0
  219. package/src/cli/run/adapters/cursor.ts +33 -2
  220. package/src/cli/run/adapters/opencode.test.ts +135 -0
  221. package/src/cli/run/adapters/opencode.ts +30 -2
  222. package/src/cli/run/executor.ts +1 -1
  223. package/src/cli/run/schema.test.ts +758 -0
  224. package/src/cli/run/schema.ts +300 -25
  225. package/src/cli/run.ts +341 -21
  226. package/src/cli/types.ts +86 -1
  227. package/src/cli/watch.ts +298 -0
  228. package/src/dashboard/dist/_astro/{index.DtnyD8a5.css → index.6L3_HsPT.css} +1 -1
  229. package/src/dashboard/dist/data/.gitkeep +0 -0
  230. package/src/dashboard/dist/data/convoy-list.json +1 -0
  231. package/src/dashboard/dist/data/overall-stats.json +24 -0
  232. package/src/dashboard/dist/index.html +701 -3
  233. package/src/dashboard/node_modules/.vite/deps/_metadata.json +6 -6
  234. package/src/dashboard/public/data/.gitkeep +0 -0
  235. package/src/dashboard/public/data/convoy-list.json +1 -0
  236. package/src/dashboard/public/data/overall-stats.json +24 -0
  237. package/src/dashboard/scripts/etl.test.ts +210 -0
  238. package/src/dashboard/scripts/etl.ts +108 -0
  239. package/src/dashboard/scripts/integration-test.ts +504 -0
  240. package/src/dashboard/src/pages/index.astro +854 -15
  241. package/src/dashboard/src/styles/dashboard.css +557 -1
  242. package/src/orchestrator/prompts/generate-convoy.prompt.md +212 -13
@@ -0,0 +1,405 @@
1
+ import {
2
+ mkdtempSync,
3
+ rmSync,
4
+ realpathSync,
5
+ writeFileSync,
6
+ mkdirSync,
7
+ } from 'node:fs'
8
+ import { tmpdir } from 'node:os'
9
+ import { join } from 'node:path'
10
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
11
+ import {
12
+ parseFormula,
13
+ substituteVariables,
14
+ validateTemplate,
15
+ FormulaValidationError,
16
+ } from './formula.js'
17
+ import type { FormulaTemplate } from './formula.js'
18
+
19
+ // ── Helpers ───────────────────────────────────────────────────────────────────
20
+
21
+ function makeBase(): string {
22
+ return realpathSync(mkdtempSync(join(tmpdir(), 'formula-test-')))
23
+ }
24
+
25
+ /**
26
+ * Minimal valid TaskSpec content for use in formula `spec:` sections.
27
+ * After substitution, this must pass parseTaskSpecText validation.
28
+ */
29
+ const MINIMAL_SPEC = {
30
+ name: 'Test convoy',
31
+ version: 1,
32
+ tasks: [{ id: 'task-1', prompt: 'Do the thing' }],
33
+ }
34
+
35
+ function writeFormula(dir: string, content: string): string {
36
+ const path = join(dir, 'formula.convoy.yml')
37
+ writeFileSync(path, content)
38
+ return path
39
+ }
40
+
41
+ let tmpDir: string
42
+
43
+ beforeEach(() => {
44
+ tmpDir = makeBase()
45
+ })
46
+
47
+ afterEach(() => {
48
+ rmSync(tmpDir, { recursive: true, force: true })
49
+ })
50
+
51
+ // ── parseFormula ──────────────────────────────────────────────────────────────
52
+
53
+ describe('parseFormula', () => {
54
+ it('reads a formula file and returns a FormulaTemplate', () => {
55
+ const path = writeFormula(
56
+ tmpDir,
57
+ [
58
+ 'name: My Formula',
59
+ 'description: A test formula',
60
+ 'variables:',
61
+ ' app_name:',
62
+ ' description: App name',
63
+ ' required: true',
64
+ 'spec:',
65
+ ' name: Test convoy',
66
+ ' version: 1',
67
+ ' tasks:',
68
+ ' - id: task-1',
69
+ ' prompt: Do the thing',
70
+ ].join('\n'),
71
+ )
72
+
73
+ const template = parseFormula(path)
74
+ expect(template.name).toBe('My Formula')
75
+ expect(template.description).toBe('A test formula')
76
+ expect(template.variables['app_name']).toMatchObject({ required: true })
77
+ expect(template.spec).toBeTruthy()
78
+ })
79
+
80
+ it('throws when file does not exist', () => {
81
+ expect(() => parseFormula(join(tmpDir, 'missing.yml'))).toThrow(
82
+ /Cannot read formula file/,
83
+ )
84
+ })
85
+
86
+ it('throws on invalid YAML', () => {
87
+ const path = writeFormula(tmpDir, ': {{{')
88
+ expect(() => parseFormula(path)).toThrow(/Formula YAML parse error/)
89
+ })
90
+
91
+ it('throws when name field is missing', () => {
92
+ const path = writeFormula(
93
+ tmpDir,
94
+ 'spec:\n name: Test convoy\n version: 1\n tasks:\n - id: t1\n prompt: p\n',
95
+ )
96
+ expect(() => parseFormula(path)).toThrow(/must have a "name" field/)
97
+ })
98
+
99
+ it('throws when spec field is missing', () => {
100
+ const path = writeFormula(tmpDir, 'name: My Formula\n')
101
+ expect(() => parseFormula(path)).toThrow(/must have a "spec" field/)
102
+ })
103
+
104
+ it('returns empty variables when no variables section', () => {
105
+ const path = writeFormula(
106
+ tmpDir,
107
+ 'name: Simple\nspec:\n name: Test\n version: 1\n tasks:\n - id: t1\n prompt: p\n',
108
+ )
109
+ const template = parseFormula(path)
110
+ expect(template.variables).toEqual({})
111
+ })
112
+
113
+ it('parses optional flag and default value', () => {
114
+ const path = writeFormula(
115
+ tmpDir,
116
+ [
117
+ 'name: With defaults',
118
+ 'variables:',
119
+ ' env:',
120
+ ' required: false',
121
+ ' default: staging',
122
+ 'spec:',
123
+ ' name: Test',
124
+ ' version: 1',
125
+ ' tasks:',
126
+ ' - id: t1',
127
+ ' prompt: p',
128
+ ].join('\n'),
129
+ )
130
+ const template = parseFormula(path)
131
+ expect(template.variables['env']).toMatchObject({ required: false, default: 'staging' })
132
+ })
133
+ })
134
+
135
+ // ── substituteVariables ───────────────────────────────────────────────────────
136
+
137
+ describe('substituteVariables', () => {
138
+ function makeTemplate(overrides: Partial<FormulaTemplate> = {}): FormulaTemplate {
139
+ return {
140
+ name: 'Test Formula',
141
+ variables: {},
142
+ spec: MINIMAL_SPEC,
143
+ ...overrides,
144
+ }
145
+ }
146
+
147
+ it('substitutes a plain variable', () => {
148
+ const template = makeTemplate({
149
+ variables: { feature: { required: true } },
150
+ spec: {
151
+ name: '{{feature}}',
152
+ version: 1,
153
+ tasks: [{ id: 'task-1', prompt: 'Build {{feature}}' }],
154
+ },
155
+ })
156
+ const result = substituteVariables(template, { feature: 'auth' })
157
+ expect(result.name).toBe('auth')
158
+ })
159
+
160
+ it('applies kebab filter', () => {
161
+ const template = makeTemplate({
162
+ variables: { feature: { required: true } },
163
+ spec: {
164
+ name: 'test',
165
+ version: 1,
166
+ tasks: [{ id: 'task-1', prompt: '{{feature | kebab}}' }],
167
+ },
168
+ })
169
+ const result = substituteVariables(template, { feature: 'MyFeature Name' })
170
+ expect(result.tasks![0].prompt).toContain('my-feature-name')
171
+ })
172
+
173
+ it('applies snake filter', () => {
174
+ const template = makeTemplate({
175
+ variables: { feature: { required: true } },
176
+ spec: {
177
+ name: 'test',
178
+ version: 1,
179
+ tasks: [{ id: 'task-1', prompt: '{{feature | snake}}' }],
180
+ },
181
+ })
182
+ const result = substituteVariables(template, { feature: 'MyFeature Name' })
183
+ expect(result.tasks![0].prompt).toContain('my_feature_name')
184
+ })
185
+
186
+ it('applies upper filter', () => {
187
+ const template = makeTemplate({
188
+ variables: { feature: { required: true } },
189
+ spec: {
190
+ name: 'test',
191
+ version: 1,
192
+ tasks: [{ id: 'task-1', prompt: '{{feature | upper}}' }],
193
+ },
194
+ })
195
+ const result = substituteVariables(template, { feature: 'my feature' })
196
+ expect(result.tasks![0].prompt).toContain('MY_FEATURE')
197
+ })
198
+
199
+ it('throws FormulaValidationError for missing required variable', () => {
200
+ const template = makeTemplate({
201
+ variables: { required_var: { required: true } },
202
+ spec: {
203
+ name: 'test',
204
+ version: 1,
205
+ tasks: [{ id: 'task-1', prompt: '{{required_var}}' }],
206
+ },
207
+ })
208
+ expect(() => substituteVariables(template, {})).toThrow(FormulaValidationError)
209
+ })
210
+
211
+ it('includes missing variable name in FormulaValidationError', () => {
212
+ const template = makeTemplate({
213
+ variables: { missing_var: { required: true } },
214
+ spec: {
215
+ name: 'test',
216
+ version: 1,
217
+ tasks: [{ id: 'task-1', prompt: '{{missing_var}}' }],
218
+ },
219
+ })
220
+ let err: FormulaValidationError | undefined
221
+ try {
222
+ substituteVariables(template, {})
223
+ } catch (e) {
224
+ if (e instanceof FormulaValidationError) err = e
225
+ }
226
+ expect(err).toBeDefined()
227
+ expect(err!.missingVariables).toContain('missing_var')
228
+ })
229
+
230
+ it('collects all missing required variables', () => {
231
+ const template = makeTemplate({
232
+ variables: {
233
+ var_a: { required: true },
234
+ var_b: { required: true },
235
+ },
236
+ spec: {
237
+ name: 'test',
238
+ version: 1,
239
+ tasks: [{ id: 'task-1', prompt: '{{var_a}} and {{var_b}}' }],
240
+ },
241
+ })
242
+ let err: FormulaValidationError | undefined
243
+ try {
244
+ substituteVariables(template, {})
245
+ } catch (e) {
246
+ if (e instanceof FormulaValidationError) err = e
247
+ }
248
+ expect(err!.missingVariables).toEqual(expect.arrayContaining(['var_a', 'var_b']))
249
+ })
250
+
251
+ it('uses default value for optional variable without provided value', () => {
252
+ const template = makeTemplate({
253
+ variables: { env: { required: false, default: 'production' } },
254
+ spec: {
255
+ name: 'test',
256
+ version: 1,
257
+ tasks: [{ id: 'task-1', prompt: 'Deploy to {{env}}' }],
258
+ },
259
+ })
260
+ const result = substituteVariables(template, {})
261
+ expect(result.tasks![0].prompt).toContain('production')
262
+ })
263
+
264
+ it('uses empty string for optional variable with no default', () => {
265
+ const template = makeTemplate({
266
+ variables: { opt: { required: false } },
267
+ spec: {
268
+ name: 'test-task',
269
+ version: 1,
270
+ tasks: [{ id: 'task-1', prompt: 'prefix-{{opt}}-suffix' }],
271
+ },
272
+ })
273
+ const result = substituteVariables(template, {})
274
+ expect(result.tasks![0].prompt).toContain('prefix--suffix')
275
+ })
276
+
277
+ it('returns a valid TaskSpec', () => {
278
+ const template = makeTemplate({
279
+ variables: { app: { required: true } },
280
+ spec: {
281
+ name: 'Deploy {{app}}',
282
+ version: 1,
283
+ tasks: [{ id: 'task-1', prompt: 'Deploy {{app}}' }],
284
+ },
285
+ })
286
+ const result = substituteVariables(template, { app: 'myapp' })
287
+ expect(result).toMatchObject({ name: expect.any(String) })
288
+ expect(Array.isArray(result.tasks)).toBe(true)
289
+ })
290
+ })
291
+
292
+ // ── validateTemplate ──────────────────────────────────────────────────────────
293
+
294
+ describe('validateTemplate', () => {
295
+ function makeTemplate(overrides: Partial<FormulaTemplate> = {}): FormulaTemplate {
296
+ return {
297
+ name: 'Valid Template',
298
+ variables: {},
299
+ spec: MINIMAL_SPEC,
300
+ ...overrides,
301
+ }
302
+ }
303
+
304
+ it('returns valid for a correct template', () => {
305
+ const result = validateTemplate(makeTemplate())
306
+ expect(result.valid).toBe(true)
307
+ expect(result.errors).toHaveLength(0)
308
+ })
309
+
310
+ it('returns error when name is missing', () => {
311
+ const template = makeTemplate({ name: '' })
312
+ const result = validateTemplate(template)
313
+ expect(result.valid).toBe(false)
314
+ expect(result.errors.some(e => e.includes('"name"'))).toBe(true)
315
+ })
316
+
317
+ it('returns error when spec is null', () => {
318
+ const template = makeTemplate({ spec: null as unknown as unknown })
319
+ const result = validateTemplate(template)
320
+ expect(result.valid).toBe(false)
321
+ expect(result.errors.some(e => e.includes('"spec"'))).toBe(true)
322
+ })
323
+
324
+ it('returns error for invalid variable identifier', () => {
325
+ const template = makeTemplate({
326
+ variables: { 'invalid-name': { required: false } },
327
+ })
328
+ const result = validateTemplate(template)
329
+ expect(result.valid).toBe(false)
330
+ expect(result.errors.some(e => e.includes('invalid-name'))).toBe(true)
331
+ })
332
+
333
+ it('allows valid variable identifiers', () => {
334
+ const template = makeTemplate({
335
+ variables: {
336
+ app_name: { required: true },
337
+ env2: { required: false },
338
+ MY_VAR: { required: false },
339
+ },
340
+ })
341
+ const result = validateTemplate(template)
342
+ expect(result.valid).toBe(true)
343
+ })
344
+
345
+ it('writes a warning for undeclared placeholder', () => {
346
+ const stderrSpy = vi.spyOn(process.stderr, 'write').mockImplementation(() => true)
347
+ const template = makeTemplate({
348
+ variables: {},
349
+ spec: {
350
+ name: 'test',
351
+ version: 1,
352
+ tasks: [{ id: 'task-1', prompt: 'Do {{undeclared_var}}' }],
353
+ },
354
+ })
355
+ validateTemplate(template)
356
+ expect(stderrSpy).toHaveBeenCalledWith(expect.stringContaining('undeclared_var'))
357
+ stderrSpy.mockRestore()
358
+ })
359
+
360
+ it('does not warn for declared placeholders', () => {
361
+ const stderrSpy = vi.spyOn(process.stderr, 'write').mockImplementation(() => true)
362
+ const template = makeTemplate({
363
+ variables: { declared_var: { required: true } },
364
+ spec: {
365
+ name: 'test',
366
+ version: 1,
367
+ tasks: [{ id: 'task-1', prompt: 'Do {{declared_var}}' }],
368
+ },
369
+ })
370
+ validateTemplate(template)
371
+ expect(stderrSpy).not.toHaveBeenCalled()
372
+ stderrSpy.mockRestore()
373
+ })
374
+ })
375
+
376
+ // ── Filter helpers ────────────────────────────────────────────────────────────
377
+
378
+ describe('filter conversions via substituteVariables', () => {
379
+ function makeFilterTemplate(filter: string): FormulaTemplate {
380
+ return {
381
+ name: 'Filter Test',
382
+ variables: { val: { required: true } },
383
+ spec: {
384
+ name: `{{val | ${filter}}}`,
385
+ version: 1,
386
+ tasks: [{ id: 'task-1', prompt: 'x' }],
387
+ },
388
+ }
389
+ }
390
+
391
+ it('kebab: converts spaces and underscores to hyphens, lowercases', () => {
392
+ const result = substituteVariables(makeFilterTemplate('kebab'), { val: 'Hello World_Test' })
393
+ expect(result.name).toBe('hello-world-test')
394
+ })
395
+
396
+ it('snake: converts spaces and hyphens to underscores, lowercases', () => {
397
+ const result = substituteVariables(makeFilterTemplate('snake'), { val: 'Hello World-Test' })
398
+ expect(result.name).toBe('hello_world_test')
399
+ })
400
+
401
+ it('upper: converts to uppercase with underscores', () => {
402
+ const result = substituteVariables(makeFilterTemplate('upper'), { val: 'hello world' })
403
+ expect(result.name).toBe('HELLO_WORLD')
404
+ })
405
+ })
@@ -0,0 +1,174 @@
1
+ import { readFileSync } from 'node:fs'
2
+ import { parse as yamlParse, stringify as yamlStringify } from 'yaml'
3
+ import { parseTaskSpecText } from '../run/schema.js'
4
+ import type { TaskSpec, ValidationResult } from '../types.js'
5
+
6
+ export interface FormulaTemplate {
7
+ name: string
8
+ description?: string
9
+ variables: Record<string, {
10
+ description?: string
11
+ required: boolean
12
+ default?: string
13
+ }>
14
+ spec: unknown
15
+ }
16
+
17
+ export class FormulaValidationError extends Error {
18
+ readonly missingVariables: string[]
19
+ constructor(missingVariables: string[]) {
20
+ super(`Missing required formula variables: ${missingVariables.join(', ')}`)
21
+ this.name = 'FormulaValidationError'
22
+ this.missingVariables = missingVariables
23
+ }
24
+ }
25
+
26
+ // Matches {{varname}} and {{varname | filter}} with optional whitespace
27
+ const PLACEHOLDER_RE = /\{\{(\s*\w+\s*(?:\|\s*\w+\s*)?)\}\}/g
28
+
29
+ function toKebab(value: string): string {
30
+ return value
31
+ .replace(/([a-z])([A-Z])/g, '$1-$2')
32
+ .replace(/[\s_]+/g, '-')
33
+ .toLowerCase()
34
+ }
35
+
36
+ function toSnake(value: string): string {
37
+ return value
38
+ .replace(/([a-z])([A-Z])/g, '$1_$2')
39
+ .replace(/[\s-]+/g, '_')
40
+ .toLowerCase()
41
+ }
42
+
43
+ function toUpper(value: string): string {
44
+ return value
45
+ .replace(/[\s-]+/g, '_')
46
+ .toUpperCase()
47
+ }
48
+
49
+ export function parseFormula(templatePath: string): FormulaTemplate {
50
+ let raw: string
51
+ try {
52
+ raw = readFileSync(templatePath, 'utf8')
53
+ } catch (err: unknown) {
54
+ throw new Error(`Cannot read formula file: ${(err as Error).message}`)
55
+ }
56
+
57
+ let parsed: unknown
58
+ try {
59
+ parsed = yamlParse(raw)
60
+ } catch (err: unknown) {
61
+ throw new Error(`Formula YAML parse error: ${(err as Error).message}`)
62
+ }
63
+
64
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
65
+ throw new Error('Formula file must be a YAML mapping')
66
+ }
67
+
68
+ const obj = parsed as Record<string, unknown>
69
+
70
+ if (!obj.name || typeof obj.name !== 'string') {
71
+ throw new Error('Formula template must have a "name" field')
72
+ }
73
+ if (obj.spec === undefined || obj.spec === null) {
74
+ throw new Error('Formula template must have a "spec" field')
75
+ }
76
+
77
+ const variables: FormulaTemplate['variables'] = {}
78
+ if (obj.variables && typeof obj.variables === 'object' && !Array.isArray(obj.variables)) {
79
+ for (const [key, val] of Object.entries(obj.variables as Record<string, unknown>)) {
80
+ if (!val || typeof val !== 'object' || Array.isArray(val)) continue
81
+ const v = val as Record<string, unknown>
82
+ variables[key] = {
83
+ description: typeof v.description === 'string' ? v.description : undefined,
84
+ required: v.required === true,
85
+ default: typeof v.default === 'string' ? v.default : undefined,
86
+ }
87
+ }
88
+ }
89
+
90
+ return {
91
+ name: obj.name,
92
+ description: typeof obj.description === 'string' ? obj.description : undefined,
93
+ variables,
94
+ spec: obj.spec,
95
+ }
96
+ }
97
+
98
+ export function substituteVariables(
99
+ template: FormulaTemplate,
100
+ vars: Record<string, string>,
101
+ ): TaskSpec {
102
+ const specYaml = yamlStringify(template.spec)
103
+ const missing: string[] = []
104
+
105
+ const result = specYaml.replace(PLACEHOLDER_RE, (match, inner: string) => {
106
+ const parts = inner.split('|').map((p: string) => p.trim())
107
+ const varName = parts[0]
108
+ const filter = parts[1] ?? null
109
+
110
+ let value: string
111
+ if (varName in vars) {
112
+ value = vars[varName]
113
+ } else if (varName in template.variables) {
114
+ const def = template.variables[varName]
115
+ if (def.required) {
116
+ missing.push(varName)
117
+ return match // keep placeholder; collect all missing vars
118
+ }
119
+ value = def.default ?? ''
120
+ } else {
121
+ value = ''
122
+ }
123
+
124
+ if (filter === 'kebab') return toKebab(value)
125
+ if (filter === 'snake') return toSnake(value)
126
+ if (filter === 'upper') return toUpper(value)
127
+ return value
128
+ })
129
+
130
+ if (missing.length > 0) {
131
+ throw new FormulaValidationError(missing)
132
+ }
133
+
134
+ return parseTaskSpecText(result)
135
+ }
136
+
137
+ export function validateTemplate(template: FormulaTemplate): ValidationResult {
138
+ const errors: string[] = []
139
+ const VALID_IDENTIFIER = /^[a-zA-Z0-9_]+$/
140
+
141
+ if (!template.name || typeof template.name !== 'string') {
142
+ errors.push('"name" field is required')
143
+ }
144
+ if (template.spec === undefined || template.spec === null) {
145
+ errors.push('"spec" field is required')
146
+ }
147
+
148
+ for (const key of Object.keys(template.variables)) {
149
+ if (!VALID_IDENTIFIER.test(key)) {
150
+ errors.push(
151
+ `Variable name "${key}" is not a valid identifier (alphanumeric + underscore only)`,
152
+ )
153
+ }
154
+ }
155
+
156
+ // Warn on undeclared {{variable}} placeholders in spec
157
+ if (template.spec !== undefined && template.spec !== null) {
158
+ try {
159
+ const specYaml = yamlStringify(template.spec)
160
+ for (const match of specYaml.matchAll(PLACEHOLDER_RE)) {
161
+ const varName = match[1].split('|')[0].trim()
162
+ if (!(varName in template.variables)) {
163
+ process.stderr.write(
164
+ `Warning: template contains undeclared placeholder "{{${varName}}}"\n`,
165
+ )
166
+ }
167
+ }
168
+ } catch {
169
+ // If stringify fails, skip placeholder checking
170
+ }
171
+ }
172
+
173
+ return { valid: errors.length === 0, errors }
174
+ }