@kb-labs/workflow-runtime 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 +313 -0
- package/dist/index.d.ts +524 -0
- package/dist/index.js +873 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
package/README.md
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
# @kb-labs/workflow-runtime
|
|
2
|
+
|
|
3
|
+
Runtime adapters and step executors for the KB Labs workflow engine.
|
|
4
|
+
|
|
5
|
+
## Vision & Purpose
|
|
6
|
+
|
|
7
|
+
**@kb-labs/workflow-runtime** provides runtime adapters and step executors for workflow execution. It includes local runner for in-process execution, sandbox runner for plugin commands, context management, and signal handling.
|
|
8
|
+
|
|
9
|
+
### Core Goals
|
|
10
|
+
|
|
11
|
+
- **Local Runner**: Execute steps in-process
|
|
12
|
+
- **Sandbox Runner**: Execute plugin commands in sandboxed environment
|
|
13
|
+
- **Context Management**: Step execution context with environment and secrets
|
|
14
|
+
- **Signal Handling**: Proper cancellation and timeout handling
|
|
15
|
+
|
|
16
|
+
## Package Status
|
|
17
|
+
|
|
18
|
+
- **Version**: 0.1.0
|
|
19
|
+
- **Stage**: Stable
|
|
20
|
+
- **Status**: Production Ready ✅
|
|
21
|
+
|
|
22
|
+
## Architecture
|
|
23
|
+
|
|
24
|
+
### High-Level Overview
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
Workflow Runtime
|
|
28
|
+
│
|
|
29
|
+
├──► Local Runner (in-process execution)
|
|
30
|
+
├──► Sandbox Runner (plugin commands)
|
|
31
|
+
├──► Context Management
|
|
32
|
+
└──► Signal Handling
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Key Components
|
|
36
|
+
|
|
37
|
+
1. **LocalRunner** (`runners/local-runner.ts`): Execute steps in-process
|
|
38
|
+
2. **SandboxRunner** (`runners/sandbox-runner.ts`): Execute plugin commands in sandbox
|
|
39
|
+
3. **Context** (`context.ts`): Step execution context creation
|
|
40
|
+
4. **Types** (`types.ts`): Type definitions
|
|
41
|
+
|
|
42
|
+
## ✨ Features
|
|
43
|
+
|
|
44
|
+
- **Local execution** for shell commands
|
|
45
|
+
- **Sandbox execution** for plugin commands
|
|
46
|
+
- **Context management** with environment and secrets
|
|
47
|
+
- **Signal handling** for cancellation and timeouts
|
|
48
|
+
- **Artifact support** for step outputs
|
|
49
|
+
- **Event emission** for step observability
|
|
50
|
+
- **Tracing support** for distributed tracing
|
|
51
|
+
|
|
52
|
+
## 📦 API Reference
|
|
53
|
+
|
|
54
|
+
### Main Exports
|
|
55
|
+
|
|
56
|
+
#### Runner Classes
|
|
57
|
+
|
|
58
|
+
- `LocalRunner`: Execute steps in-process
|
|
59
|
+
- `SandboxRunner`: Execute plugin commands in sandbox
|
|
60
|
+
|
|
61
|
+
#### Context Functions
|
|
62
|
+
|
|
63
|
+
- `createStepContext(input)`: Create step execution context
|
|
64
|
+
|
|
65
|
+
#### Types & Interfaces
|
|
66
|
+
|
|
67
|
+
- `Runner`: Runner interface
|
|
68
|
+
- `StepContext`: Step execution context
|
|
69
|
+
- `StepExecutionRequest`: Step execution request
|
|
70
|
+
- `StepExecutionResult`: Step execution result
|
|
71
|
+
|
|
72
|
+
### Types & Interfaces
|
|
73
|
+
|
|
74
|
+
#### `Runner`
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
interface Runner {
|
|
78
|
+
execute(request: StepExecutionRequest): Promise<StepExecutionResult>;
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
#### `StepContext`
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
interface StepContext {
|
|
86
|
+
runId: string;
|
|
87
|
+
jobId: string;
|
|
88
|
+
stepId: string;
|
|
89
|
+
attempt: number;
|
|
90
|
+
env: Record<string, string>;
|
|
91
|
+
secrets: Record<string, string>;
|
|
92
|
+
artifacts?: ArtifactClient;
|
|
93
|
+
events?: RuntimeEvents;
|
|
94
|
+
logger: RuntimeLogger;
|
|
95
|
+
trace?: RuntimeTrace;
|
|
96
|
+
pluginContext?: PluginContext;
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
#### `StepExecutionRequest`
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
interface StepExecutionRequest {
|
|
104
|
+
spec: StepSpec;
|
|
105
|
+
context: StepContext;
|
|
106
|
+
workspace?: string;
|
|
107
|
+
signal?: AbortSignal;
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
#### `StepExecutionResult`
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
type StepExecutionResult =
|
|
115
|
+
| StepExecutionSuccess
|
|
116
|
+
| StepExecutionFailure;
|
|
117
|
+
|
|
118
|
+
interface StepExecutionSuccess {
|
|
119
|
+
status: 'success';
|
|
120
|
+
outputs?: Record<string, unknown>;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
interface StepExecutionFailure {
|
|
124
|
+
status: 'failed' | 'cancelled';
|
|
125
|
+
error: {
|
|
126
|
+
message: string;
|
|
127
|
+
code?: string;
|
|
128
|
+
stack?: string;
|
|
129
|
+
details?: Record<string, unknown>;
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## 🔧 Configuration
|
|
135
|
+
|
|
136
|
+
### Configuration Options
|
|
137
|
+
|
|
138
|
+
#### LocalRunner Options
|
|
139
|
+
|
|
140
|
+
- **shell**: Shell to use (default: `process.env.SHELL` or `'bash'`)
|
|
141
|
+
|
|
142
|
+
#### SandboxRunner Options
|
|
143
|
+
|
|
144
|
+
- **timeoutMs**: Timeout in milliseconds
|
|
145
|
+
- **resolveCommand**: Command resolver function
|
|
146
|
+
|
|
147
|
+
### Environment Variables
|
|
148
|
+
|
|
149
|
+
- `SHELL`: Shell to use for local runner
|
|
150
|
+
- `LOG_LEVEL`: Logging level
|
|
151
|
+
|
|
152
|
+
## 🔗 Dependencies
|
|
153
|
+
|
|
154
|
+
### Runtime Dependencies
|
|
155
|
+
|
|
156
|
+
- `@kb-labs/workflow-artifacts` (`workspace:*`): Workflow artifacts
|
|
157
|
+
- `@kb-labs/workflow-constants` (`workspace:*`): Workflow constants
|
|
158
|
+
- `@kb-labs/workflow-contracts` (`workspace:*`): Workflow contracts
|
|
159
|
+
- `@kb-labs/plugin-manifest` (`link:../../../kb-labs-plugin/packages/manifest`): Plugin manifest
|
|
160
|
+
- `@kb-labs/plugin-runtime` (`link:../../../kb-labs-plugin/packages/runtime`): Plugin runtime
|
|
161
|
+
- `execa` (`^9.4.0`): Process execution
|
|
162
|
+
- `pino` (`^9.4.0`): Logger
|
|
163
|
+
- `pathe` (`^1.1.1`): Path utilities
|
|
164
|
+
|
|
165
|
+
### Development Dependencies
|
|
166
|
+
|
|
167
|
+
- `@kb-labs/devkit` (`link:../../../kb-labs-devkit`): DevKit presets
|
|
168
|
+
- `@types/node` (`^24.3.3`): Node.js types
|
|
169
|
+
- `tsup` (`^8.5.0`): TypeScript bundler
|
|
170
|
+
- `typescript` (`^5.6.3`): TypeScript compiler
|
|
171
|
+
- `vitest` (`^3.2.4`): Test runner
|
|
172
|
+
|
|
173
|
+
## 🧪 Testing
|
|
174
|
+
|
|
175
|
+
### Test Structure
|
|
176
|
+
|
|
177
|
+
```
|
|
178
|
+
src/__tests__/
|
|
179
|
+
└── (tests to be added)
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### Test Coverage
|
|
183
|
+
|
|
184
|
+
- **Current Coverage**: ~0% (tests to be added)
|
|
185
|
+
- **Target Coverage**: 90%
|
|
186
|
+
|
|
187
|
+
## 📈 Performance
|
|
188
|
+
|
|
189
|
+
### Performance Characteristics
|
|
190
|
+
|
|
191
|
+
- **Time Complexity**: O(1) for execution setup, O(n) for step execution
|
|
192
|
+
- **Space Complexity**: O(1)
|
|
193
|
+
- **Bottlenecks**: Step execution time
|
|
194
|
+
|
|
195
|
+
## 🔒 Security
|
|
196
|
+
|
|
197
|
+
### Security Considerations
|
|
198
|
+
|
|
199
|
+
- **Sandbox Execution**: Plugin commands execute in sandbox
|
|
200
|
+
- **Permission Checking**: Capability checks before execution
|
|
201
|
+
- **Secrets Management**: Secrets passed via context
|
|
202
|
+
- **Signal Handling**: Proper cancellation handling
|
|
203
|
+
|
|
204
|
+
### Known Vulnerabilities
|
|
205
|
+
|
|
206
|
+
- None
|
|
207
|
+
|
|
208
|
+
## 🐛 Known Issues & Limitations
|
|
209
|
+
|
|
210
|
+
### Known Issues
|
|
211
|
+
|
|
212
|
+
- None currently
|
|
213
|
+
|
|
214
|
+
### Limitations
|
|
215
|
+
|
|
216
|
+
- **Local Runner**: Only supports shell commands
|
|
217
|
+
- **Sandbox Runner**: Requires command resolver configuration
|
|
218
|
+
|
|
219
|
+
### Future Improvements
|
|
220
|
+
|
|
221
|
+
- **More Runner Types**: Additional runner types
|
|
222
|
+
- **Enhanced Context**: More context features
|
|
223
|
+
|
|
224
|
+
## 🔄 Migration & Breaking Changes
|
|
225
|
+
|
|
226
|
+
### Migration from Previous Versions
|
|
227
|
+
|
|
228
|
+
No breaking changes in current version (0.1.0).
|
|
229
|
+
|
|
230
|
+
### Breaking Changes in Future Versions
|
|
231
|
+
|
|
232
|
+
- None planned
|
|
233
|
+
|
|
234
|
+
## 📚 Examples
|
|
235
|
+
|
|
236
|
+
### Example 1: Local Runner
|
|
237
|
+
|
|
238
|
+
```typescript
|
|
239
|
+
import { LocalRunner } from '@kb-labs/workflow-runtime';
|
|
240
|
+
import { createStepContext } from '@kb-labs/workflow-runtime';
|
|
241
|
+
|
|
242
|
+
const runner = new LocalRunner({ shell: 'bash' });
|
|
243
|
+
|
|
244
|
+
const context = createStepContext({
|
|
245
|
+
runId: 'run-123',
|
|
246
|
+
jobId: 'job-abc',
|
|
247
|
+
stepId: 'step-xyz',
|
|
248
|
+
env: { NODE_ENV: 'production' },
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
const result = await runner.execute({
|
|
252
|
+
spec: {
|
|
253
|
+
name: 'build',
|
|
254
|
+
uses: 'builtin:shell',
|
|
255
|
+
with: { command: 'npm run build' },
|
|
256
|
+
},
|
|
257
|
+
context,
|
|
258
|
+
});
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### Example 2: Sandbox Runner
|
|
262
|
+
|
|
263
|
+
```typescript
|
|
264
|
+
import { SandboxRunner } from '@kb-labs/workflow-runtime';
|
|
265
|
+
|
|
266
|
+
const runner = new SandboxRunner({
|
|
267
|
+
timeoutMs: 30000,
|
|
268
|
+
resolveCommand: async (commandRef, request) => {
|
|
269
|
+
// Resolve plugin command
|
|
270
|
+
return {
|
|
271
|
+
manifest,
|
|
272
|
+
handler,
|
|
273
|
+
permissions,
|
|
274
|
+
pluginRoot,
|
|
275
|
+
};
|
|
276
|
+
},
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const result = await runner.execute({
|
|
280
|
+
spec: {
|
|
281
|
+
name: 'review',
|
|
282
|
+
uses: 'plugin:ai-review:review',
|
|
283
|
+
with: { input: '...' },
|
|
284
|
+
},
|
|
285
|
+
context,
|
|
286
|
+
});
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### Example 3: Context with Artifacts
|
|
290
|
+
|
|
291
|
+
```typescript
|
|
292
|
+
import { createStepContext } from '@kb-labs/workflow-runtime';
|
|
293
|
+
import { createFileSystemArtifactClient } from '@kb-labs/workflow-artifacts';
|
|
294
|
+
|
|
295
|
+
const artifacts = createFileSystemArtifactClient({
|
|
296
|
+
baseDir: '/tmp/artifacts',
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
const context = createStepContext({
|
|
300
|
+
runId: 'run-123',
|
|
301
|
+
jobId: 'job-abc',
|
|
302
|
+
stepId: 'step-xyz',
|
|
303
|
+
artifacts,
|
|
304
|
+
});
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
## 🤝 Contributing
|
|
308
|
+
|
|
309
|
+
See [CONTRIBUTING.md](../../CONTRIBUTING.md) for development guidelines.
|
|
310
|
+
|
|
311
|
+
## 📄 License
|
|
312
|
+
|
|
313
|
+
MIT © KB Labs
|