@kb-labs/workflow-contracts 1.1.0

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.
package/README.md ADDED
@@ -0,0 +1,359 @@
1
+ # @kb-labs/workflow-contracts
2
+
3
+ Contracts, types and schemas for the KB Labs workflow engine.
4
+
5
+ ## Vision & Purpose
6
+
7
+ **@kb-labs/workflow-contracts** provides contracts, types, and schemas for the KB Labs workflow engine. It includes Zod validation schemas, TypeScript type definitions, and workflow specification formats.
8
+
9
+ ### Core Goals
10
+
11
+ - **Zod Schemas**: Validation schemas for workflow specifications
12
+ - **TypeScript Types**: Complete type definitions for all workflow entities
13
+ - **Workflow Spec**: Type-safe workflow specification format
14
+
15
+ ## Package Status
16
+
17
+ - **Version**: 0.1.0
18
+ - **Stage**: Stable
19
+ - **Status**: Production Ready ✅
20
+
21
+ ## Architecture
22
+
23
+ ### High-Level Overview
24
+
25
+ ```
26
+ Workflow Contracts
27
+
28
+ ├──► Zod Schemas (validation)
29
+ ├──► TypeScript Types (type safety)
30
+ └──► Workflow Spec Format
31
+ ```
32
+
33
+ ### Key Components
34
+
35
+ 1. **Schemas** (`schemas.ts`): Zod validation schemas
36
+ 2. **Types** (`types.ts`): TypeScript type definitions
37
+
38
+ ## ✨ Features
39
+
40
+ - **Zod schemas** for workflow validation
41
+ - **TypeScript types** derived from schemas
42
+ - **Workflow spec** format definition
43
+ - **Run state** types and schemas
44
+ - **Job and step** types and schemas
45
+ - **Nested workflows** support via `workflow:` uses
46
+ - **Conditional execution** with `if` expressions
47
+ - **Step outputs** and context interpolation
48
+ - **Job hooks** (pre/post/onSuccess/onFailure)
49
+
50
+ ## 📦 API Reference
51
+
52
+ ### Main Exports
53
+
54
+ #### Schemas
55
+
56
+ - `WorkflowSpecSchema`: Workflow specification schema
57
+ - `JobSpecSchema`: Job specification schema
58
+ - `StepSpecSchema`: Step specification schema
59
+ - `RunSchema`: Workflow run schema
60
+ - `JobRunSchema`: Job run schema
61
+ - `StepRunSchema`: Step run schema
62
+ - `RetryPolicySchema`: Retry policy schema
63
+
64
+ #### Types
65
+
66
+ - `WorkflowSpec`: Workflow specification type
67
+ - `JobSpec`: Job specification type
68
+ - `StepSpec`: Step specification type
69
+ - `WorkflowRun`: Workflow run type
70
+ - `JobRun`: Job run type
71
+ - `StepRun`: Step run type
72
+ - `RetryPolicy`: Retry policy type
73
+ - `RunTrigger`: Run trigger type
74
+ - `RunMetadata`: Run metadata type
75
+ - `IdempotencyKey`: Idempotency key type
76
+ - `ConcurrencyGroup`: Concurrency group type
77
+ - `ExecutionResult`: Execution result type
78
+
79
+ ## 🔧 Configuration
80
+
81
+ ### Configuration Options
82
+
83
+ No configuration needed - pure type definitions and schemas.
84
+
85
+ ## 🔗 Dependencies
86
+
87
+ ### Runtime Dependencies
88
+
89
+ - `@kb-labs/workflow-constants` (`workspace:*`): Workflow constants
90
+ - `zod` (`^4.1.5`): Schema validation
91
+
92
+ ### Development Dependencies
93
+
94
+ - `@kb-labs/devkit` (`link:../../../kb-labs-devkit`): DevKit presets
95
+ - `@types/node` (`^24.3.3`): Node.js types
96
+ - `tsup` (`^8.5.0`): TypeScript bundler
97
+ - `typescript` (`^5.6.3`): TypeScript compiler
98
+ - `vitest` (`^3.2.4`): Test runner
99
+
100
+ ## 🧪 Testing
101
+
102
+ ### Test Structure
103
+
104
+ ```
105
+ src/__tests__/
106
+ └── (tests to be added)
107
+ ```
108
+
109
+ ### Test Coverage
110
+
111
+ - **Current Coverage**: ~0% (tests to be added)
112
+ - **Target Coverage**: 90%
113
+
114
+ ## 📈 Performance
115
+
116
+ ### Performance Characteristics
117
+
118
+ - **Time Complexity**: O(1) for type operations, O(n) for schema validation
119
+ - **Space Complexity**: O(1)
120
+ - **Bottlenecks**: Schema validation for large specs
121
+
122
+ ## 🔒 Security
123
+
124
+ ### Security Considerations
125
+
126
+ - **Schema Validation**: Input validation via Zod schemas
127
+ - **Type Safety**: TypeScript type safety
128
+
129
+ ### Known Vulnerabilities
130
+
131
+ - None
132
+
133
+ ## 🐛 Known Issues & Limitations
134
+
135
+ ### Known Issues
136
+
137
+ - None currently
138
+
139
+ ### Limitations
140
+
141
+ - **Schema Validation**: Basic validation only
142
+
143
+ ### Future Improvements
144
+
145
+ - **Enhanced Validation**: More validation rules
146
+
147
+ ## 🔄 Migration & Breaking Changes
148
+
149
+ ### Migration from Previous Versions
150
+
151
+ No breaking changes in current version (0.1.0).
152
+
153
+ ### Breaking Changes in Future Versions
154
+
155
+ - None planned
156
+
157
+ ## 📚 Examples
158
+
159
+ ### Example 1: Validate Workflow Spec
160
+
161
+ ```typescript
162
+ import { WorkflowSpecSchema } from '@kb-labs/workflow-contracts';
163
+ import { z } from 'zod';
164
+
165
+ const spec = WorkflowSpecSchema.parse(yamlContent);
166
+ ```
167
+
168
+ ### Example 2: Type-Safe Workflow Definition
169
+
170
+ ```typescript
171
+ import type { WorkflowSpec } from '@kb-labs/workflow-contracts';
172
+
173
+ const myWorkflow: WorkflowSpec = {
174
+ name: 'my-workflow',
175
+ version: '0.1.0',
176
+ jobs: {
177
+ build: {
178
+ steps: [
179
+ { name: 'build', uses: 'plugin:@kb-labs/build/cli' },
180
+ ],
181
+ },
182
+ },
183
+ };
184
+ ```
185
+
186
+ ### Example 3: Nested Workflows
187
+
188
+ ```yaml
189
+ name: parent-workflow
190
+ version: 1.0.0
191
+ on:
192
+ manual: true
193
+ jobs:
194
+ orchestration:
195
+ runsOn: local
196
+ steps:
197
+ - name: Run Child Workflow
198
+ id: child
199
+ uses: workflow:workspace:child-workflow
200
+ with:
201
+ input: value
202
+
203
+ - name: Use Child Output
204
+ uses: builtin:shell
205
+ with:
206
+ command: echo ${{ steps.child.outputs.result }}
207
+ ```
208
+
209
+ ### Example 4: Conditional Execution
210
+
211
+ ```yaml
212
+ name: conditional-workflow
213
+ version: 1.0.0
214
+ on:
215
+ push: true
216
+ jobs:
217
+ deploy:
218
+ runsOn: local
219
+ if: ${{ trigger.type == 'push' && trigger.payload.ref == 'refs/heads/main' }}
220
+ steps:
221
+ - name: Run Tests
222
+ id: tests
223
+ uses: builtin:shell
224
+ with:
225
+ command: npm test
226
+
227
+ - name: Deploy
228
+ uses: builtin:shell
229
+ with:
230
+ command: npm run deploy
231
+ if: ${{ steps.tests.outputs.exitCode == 0 }}
232
+ ```
233
+
234
+ ### Example 5: Job Hooks
235
+
236
+ ```yaml
237
+ name: workflow-with-hooks
238
+ version: 1.0.0
239
+ on:
240
+ manual: true
241
+ jobs:
242
+ main:
243
+ runsOn: local
244
+ hooks:
245
+ pre:
246
+ - name: Setup
247
+ uses: builtin:shell
248
+ with:
249
+ command: echo "Setting up..."
250
+ post:
251
+ - name: Cleanup
252
+ uses: builtin:shell
253
+ with:
254
+ command: echo "Cleaning up..."
255
+ onSuccess:
256
+ - name: Notify Success
257
+ uses: builtin:shell
258
+ with:
259
+ command: echo "✓ Success"
260
+ onFailure:
261
+ - name: Notify Failure
262
+ uses: builtin:shell
263
+ with:
264
+ command: echo "✗ Failed"
265
+ steps:
266
+ - name: Main Task
267
+ uses: builtin:shell
268
+ with:
269
+ command: echo "Running main task..."
270
+ ```
271
+
272
+ ## 🔧 Advanced Features
273
+
274
+ ### Nested Workflows
275
+
276
+ Workflows can call other workflows using the `workflow:` prefix in step `uses`:
277
+
278
+ ```yaml
279
+ steps:
280
+ - name: Call Child
281
+ uses: workflow:workspace:child-workflow
282
+ # or
283
+ uses: workflow:plugin:@kb-labs/plugin/workflow-id
284
+ ```
285
+
286
+ **Supported modes:**
287
+ - `mode: 'wait'` (default): Wait for child workflow to complete
288
+ - `mode: 'fire-and-forget'`: Not supported in MVP (will throw error)
289
+
290
+ **Parent/Child Linkage:**
291
+ - Child runs include `parentRunId`, `parentJobId`, `parentStepId` in metadata
292
+ - Parent cancellation automatically cancels child runs
293
+ - Depth limit enforced via `maxDepth` configuration
294
+
295
+ ### Conditional Execution
296
+
297
+ Steps and jobs can be conditionally executed using `if` expressions:
298
+
299
+ ```yaml
300
+ steps:
301
+ - name: Conditional Step
302
+ if: ${{ env.NODE_ENV == 'production' }}
303
+ uses: builtin:shell
304
+ with:
305
+ command: echo "Production only"
306
+ ```
307
+
308
+ **Expression Context:**
309
+ - `env.*`: Environment variables
310
+ - `trigger.*`: Run trigger information
311
+ - `steps.<id>.outputs.*`: Step outputs
312
+ - `matrix.*`: Matrix variables (future)
313
+
314
+ **Supported Operators:**
315
+ - `==`, `!=`: Equality comparison
316
+ - `contains()`, `startsWith()`, `endsWith()`: String functions
317
+ - Boolean literals: `true`, `false`
318
+
319
+ ### Step Outputs
320
+
321
+ Steps can produce outputs that are accessible in subsequent steps:
322
+
323
+ ```yaml
324
+ steps:
325
+ - name: Generate Version
326
+ id: version
327
+ uses: builtin:shell
328
+ with:
329
+ command: echo "1.0.0"
330
+ # Outputs automatically captured from step execution
331
+
332
+ - name: Use Output
333
+ uses: builtin:shell
334
+ with:
335
+ command: echo "Version: ${{ steps.version.outputs.result }}"
336
+ ```
337
+
338
+ **Output Access:**
339
+ - `steps.<id>.outputs.*`: Access step outputs in expressions
340
+ - Outputs are automatically captured from step execution results
341
+
342
+ ### Job Hooks
343
+
344
+ Jobs can define hooks that run at different lifecycle stages:
345
+
346
+ - **pre**: Runs before main steps
347
+ - **post**: Runs after main steps (always)
348
+ - **onSuccess**: Runs only if job succeeds
349
+ - **onFailure**: Runs only if job fails
350
+
351
+ Hooks are executed as simplified steps (no nested hooks support in MVP).
352
+
353
+ ## 🤝 Contributing
354
+
355
+ See [CONTRIBUTING.md](../../CONTRIBUTING.md) for development guidelines.
356
+
357
+ ## 📄 License
358
+
359
+ MIT © KB Labs