@bratsos/workflow-engine 0.7.0 → 0.8.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.
Files changed (38) hide show
  1. package/README.md +93 -0
  2. package/dist/{chunk-2HEV5ZJL.js → chunk-NANAXHS5.js} +2 -2
  3. package/dist/chunk-NANAXHS5.js.map +1 -0
  4. package/dist/{chunk-Q2XDO3UF.js → chunk-TWYPSP7P.js} +92 -3
  5. package/dist/chunk-TWYPSP7P.js.map +1 -0
  6. package/dist/{chunk-5C7LRNM7.js → chunk-XPWAEYOO.js} +449 -59
  7. package/dist/chunk-XPWAEYOO.js.map +1 -0
  8. package/dist/{client-DYs5wlHp.d.ts → client-YFKVt4p7.d.ts} +3 -3
  9. package/dist/client.d.ts +4 -4
  10. package/dist/conventions/index.d.ts +114 -0
  11. package/dist/conventions/index.js +95 -0
  12. package/dist/conventions/index.js.map +1 -0
  13. package/dist/{events-D_P24UaY.d.ts → events-B3XPPu0c.d.ts} +23 -1
  14. package/dist/{index-aNuJ2QgN.d.ts → index-CL0KmlyW.d.ts} +4 -1
  15. package/dist/index.d.ts +10 -10
  16. package/dist/index.js +3 -3
  17. package/dist/{interface-BeEPzTFy.d.ts → interface-BPz138Hf.d.ts} +108 -1
  18. package/dist/kernel/index.d.ts +6 -6
  19. package/dist/kernel/index.js +2 -2
  20. package/dist/kernel/testing/index.d.ts +3 -3
  21. package/dist/persistence/index.d.ts +2 -2
  22. package/dist/persistence/index.js +2 -2
  23. package/dist/persistence/prisma/index.d.ts +2 -2
  24. package/dist/persistence/prisma/index.js +2 -2
  25. package/dist/{plugins-Cl0WVVrE.d.ts → plugins-zT-aIcEZ.d.ts} +63 -4
  26. package/dist/{ports-swhiWFw4.d.ts → ports-DUL4hlQr.d.ts} +11 -2
  27. package/dist/{stage-_7BKqqUG.d.ts → stage-WuK0mfrC.d.ts} +81 -1
  28. package/dist/testing/index.d.ts +8 -1
  29. package/dist/testing/index.js +86 -1
  30. package/dist/testing/index.js.map +1 -1
  31. package/package.json +6 -1
  32. package/skills/workflow-engine/SKILL.md +58 -1
  33. package/skills/workflow-engine/migrations/README.md +275 -0
  34. package/skills/workflow-engine/migrations/migrate-0.7-to-0.8.md +528 -0
  35. package/skills/workflow-engine/references/10-annotations.md +479 -0
  36. package/dist/chunk-2HEV5ZJL.js.map +0 -1
  37. package/dist/chunk-5C7LRNM7.js.map +0 -1
  38. package/dist/chunk-Q2XDO3UF.js.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bratsos/workflow-engine",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Type-safe, distributed workflow engine for AI-orchestrated processes with suspend/resume, parallel execution, and cost tracking",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -68,6 +68,11 @@
68
68
  "types": "./dist/kernel/testing/index.d.ts",
69
69
  "import": "./dist/kernel/testing/index.js",
70
70
  "default": "./dist/kernel/testing/index.js"
71
+ },
72
+ "./conventions": {
73
+ "types": "./dist/conventions/index.d.ts",
74
+ "import": "./dist/conventions/index.js",
75
+ "default": "./dist/conventions/index.js"
71
76
  }
72
77
  },
73
78
  "files": [
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: workflow-engine
3
- description: Guide for @bratsos/workflow-engine - a type-safe workflow engine with AI integration, stage pipelines, and persistence. Use when building multi-stage workflows, AI-powered pipelines, implementing workflow persistence, defining stages, or working with batch AI operations.
3
+ description: Guide for @bratsos/workflow-engine - a type-safe workflow engine with AI integration, stage pipelines, and persistence. Use when building multi-stage workflows, AI-powered pipelines, implementing workflow persistence, defining stages, or working with batch AI operations. For upgrades between published versions, route through `migrations/README.md`.
4
4
  license: MIT
5
5
  metadata:
6
6
  author: bratsos
@@ -8,6 +8,10 @@ metadata:
8
8
  repository: https://github.com/bratsos/workflow-engine
9
9
  ---
10
10
 
11
+ ## Upgrading between versions
12
+
13
+ When the user wants to upgrade `@bratsos/workflow-engine` in their project (e.g., "upgrade my project to the latest workflow-engine version", "I just bumped workflow-engine, walk me through the migration"), route to `migrations/README.md` — the upgrade router. It explains how to detect the installed and previous versions, find the relevant migration guides, and apply them in order. Multi-version upgrades (e.g., 0.6 → 0.8) load and apply multiple migration files sequentially.
14
+
11
15
  # @bratsos/workflow-engine Skill
12
16
 
13
17
  Type-safe workflow engine for building AI-powered, multi-stage pipelines with persistence and batch processing support. Uses a **command kernel** architecture with environment-agnostic design.
@@ -115,6 +119,8 @@ await kernel.dispatch({
115
119
  | `assertValidStageId` | Function | `@bratsos/workflow-engine` | Assert stage ID validity (throws) |
116
120
  | `definePlugin` | Function | `@bratsos/workflow-engine/kernel` | Define kernel plugins |
117
121
  | `createPluginRunner` | Function | `@bratsos/workflow-engine/kernel` | Create plugin event processor |
122
+ | `typedKey` | Function | `@bratsos/workflow-engine/conventions` | Define a well-known annotation key with linked value type |
123
+ | `Trigger` / `Decision` / `Approval` / `Revision` | Constants | `@bratsos/workflow-engine/conventions` | Well-known annotation key namespaces (v0.8+) |
118
124
 
119
125
  ## Kernel Commands
120
126
 
@@ -345,6 +351,55 @@ const tick = await host.runMaintenanceTick();
345
351
  // Note: resumed suspended stages are automatically followed by run.transition.
346
352
  ```
347
353
 
354
+ ## Annotations (Provenance)
355
+
356
+ Attach typed key-value facts to runs and stages for queryable provenance — trigger context, decisions, approvals, anything else you'd want to ask back later. Writes are buffered during a stage and flushed atomically with the stage outcome (durable, not fire-and-forget).
357
+
358
+ ```typescript
359
+ import { Decision, Trigger } from "@bratsos/workflow-engine/conventions";
360
+
361
+ // Inside a stage's execute()
362
+ ctx.annotate(Decision.outcome, "low"); // typed
363
+ ctx.annotate(Decision.confidence, 0.42); // typed
364
+ ctx.annotate("acme.compliance.signoff", "alice@acme.com"); // custom key
365
+ ctx.annotate({
366
+ actor: { kind: "agent", id: "triage-v3" },
367
+ attributes: {
368
+ "decision.outcome": "low",
369
+ "decision.rationale": "below threshold",
370
+ "decision.used_fallback": true,
371
+ },
372
+ });
373
+
374
+ // At run creation — captures trigger context
375
+ await kernel.dispatch({
376
+ type: "run.create",
377
+ workflowId: "ticket-triage",
378
+ input: { ticket },
379
+ annotations: [{
380
+ actor: { kind: "system", id: "zendesk" },
381
+ attributes: {
382
+ "trigger.source": "webhook:zendesk",
383
+ "trigger.parent_run_id": previousRunId,
384
+ },
385
+ }],
386
+ });
387
+
388
+ // External attach (plugins, post-hoc reviews)
389
+ await kernel.annotations.attach(runId, {
390
+ actor: { kind: "user", id: "alice@acme.com" },
391
+ attributes: { "review.disposition": "approved-anyway" },
392
+ idempotencyKey: "review-2026-05-24-alice",
393
+ });
394
+
395
+ // Query — flat key namespace, indexed
396
+ await kernel.annotations.list(runId);
397
+ await kernel.annotations.list(runId, { keyPrefix: "decision." });
398
+ await kernel.annotations.list(runId, { actorId: "triage-v3" });
399
+ ```
400
+
401
+ Annotations replace the deprecated `WorkflowRun.metadata` column. Existing `metadata` is automatically surfaced as `legacy.metadata.*` virtual rows when consumers call `kernel.annotations.list()` (no dual-write, lazy synthesis). See [`references/10-annotations.md`](references/10-annotations.md) for the full API and conventions catalog.
402
+
348
403
  ## AI Integration & Cost Tracking
349
404
 
350
405
  ```typescript
@@ -446,6 +501,7 @@ await kernel.dispatch({ type: "run.transition", workflowRunId: job.workflowRunId
446
501
  - [07-testing-patterns.md](references/07-testing-patterns.md) - Testing with kernel
447
502
  - [08-common-patterns.md](references/08-common-patterns.md) - Kernel patterns & best practices
448
503
  - [09-troubleshooting.md](references/09-troubleshooting.md) - Debugging stuck runs, P2002 errors, ghost jobs
504
+ - [10-annotations.md](references/10-annotations.md) - First-class provenance surface: `ctx.annotate`, `kernel.annotations.*`, conventions catalog
449
505
 
450
506
  ## Key Principles
451
507
 
@@ -459,3 +515,4 @@ await kernel.dispatch({ type: "run.transition", workflowRunId: job.workflowRunId
459
515
  8. **Self-Healing**: Stage creation is idempotent (upsert), orchestration steps are isolated, stuck runs are automatically reaped
460
516
  9. **Cost Tracking**: All AI calls automatically track tokens and costs
461
517
  10. **BlobStore-Only Artifacts**: All artifact storage goes through the BlobStore port. `run.rerunFrom` cleans up artifacts by key prefix
518
+ 11. **Durable Provenance**: `ctx.annotate(...)` writes are buffered and flushed inside the stage-completion transaction. Annotations are atomic with the stage outcome — a stage's annotations either all persist or all roll back together with the stage update and outbox events.
@@ -0,0 +1,275 @@
1
+ ---
2
+ name: workflow-engine-upgrade-router
3
+ description: Use when the user wants to upgrade @bratsos/workflow-engine in their project. Detects the installed version, identifies the prior version, and loads the relevant migration guides to walk the user through code/schema changes.
4
+ ---
5
+
6
+ # @bratsos/workflow-engine upgrade router
7
+
8
+ This skill activates when a user says things like:
9
+ - "upgrade my project to the latest workflow-engine version"
10
+ - "I just bumped workflow-engine, walk me through the migration"
11
+ - "what do I need to change after upgrading workflow-engine?"
12
+
13
+ The router's job is to figure out **which migration guides to apply**, in order, given the version delta. The user should already have run their package manager's upgrade command before invoking this — if not, ask them to do that first or do it for them. See "Package manager reference" below for the right command per ecosystem.
14
+
15
+ ## Step 0 — Detect the project layout
16
+
17
+ Before reading versions, figure out **what package manager** the project uses and **whether it's a monorepo**. This determines where to look in steps 1 and 2.
18
+
19
+ ### Package manager — check in this order
20
+
21
+ | Signal | Manager |
22
+ |---|---|
23
+ | `pnpm-lock.yaml` exists | pnpm |
24
+ | `bun.lockb` or `bun.lock` exists | bun |
25
+ | `yarn.lock` exists AND `.yarnrc.yml` exists | yarn berry (v2+, may use PnP — no node_modules) |
26
+ | `yarn.lock` exists, no `.yarnrc.yml` | yarn classic (v1) |
27
+ | `package-lock.json` exists | npm |
28
+ | None of the above | ask the user; default to npm |
29
+
30
+ ```bash
31
+ ls pnpm-lock.yaml bun.lockb bun.lock yarn.lock package-lock.json .yarnrc.yml 2>/dev/null
32
+ ```
33
+
34
+ ### Monorepo detection
35
+
36
+ ```bash
37
+ # pnpm workspaces
38
+ cat pnpm-workspace.yaml 2>/dev/null
39
+
40
+ # npm / yarn workspaces (look for `workspaces` field in root package.json)
41
+ grep -A 5 '"workspaces"' package.json 2>/dev/null
42
+
43
+ # Turborepo / Nx (also signal "this is a monorepo")
44
+ ls turbo.json nx.json 2>/dev/null
45
+ ```
46
+
47
+ If it's a monorepo, run `grep -rl '@bratsos/workflow-engine' --include=package.json` from the repo root to find every workspace that depends on the package. Migrations apply to each — but **schema migrations** (Prisma) only run once, against whichever workspace owns the database.
48
+
49
+ ## Step 1 — Detect the currently installed version
50
+
51
+ The `version` field in the installed `package.json` is authoritative. Don't trust the root `package.json`'s declared range (`^0.7.0` etc.) — it tells you what's allowed, not what's resolved.
52
+
53
+ ### By package manager
54
+
55
+ ```bash
56
+ # pnpm — works in workspaces too; --filter scopes to a specific workspace
57
+ pnpm list @bratsos/workflow-engine --depth 0 --json
58
+
59
+ # pnpm in a specific workspace
60
+ pnpm --filter <workspace-name> list @bratsos/workflow-engine --depth 0 --json
61
+
62
+ # npm
63
+ npm list @bratsos/workflow-engine --depth 0 --json
64
+
65
+ # yarn classic
66
+ yarn list --pattern @bratsos/workflow-engine --depth 0
67
+
68
+ # yarn berry (works with PnP — no node_modules required)
69
+ yarn info @bratsos/workflow-engine --json | head -5
70
+
71
+ # bun
72
+ bun pm ls | grep '@bratsos/workflow-engine'
73
+ ```
74
+
75
+ ### Fallback: read `package.json` directly
76
+
77
+ ```bash
78
+ # Standard (hoisted) install — pnpm, npm, yarn classic, bun
79
+ cat node_modules/@bratsos/workflow-engine/package.json | grep '"version"'
80
+
81
+ # pnpm non-hoisted / strict — package lives under each consumer
82
+ cat packages/<workspace>/node_modules/@bratsos/workflow-engine/package.json
83
+ ```
84
+
85
+ ### Yarn Berry with PnP
86
+
87
+ PnP setups have no `node_modules` directory. Use `yarn info` or read `.pnp.cjs`:
88
+
89
+ ```bash
90
+ yarn info @bratsos/workflow-engine | grep version
91
+ # or, if yarn isn't available:
92
+ grep -A 2 '"@bratsos/workflow-engine' .pnp.cjs | head -5
93
+ ```
94
+
95
+ ### Can't find it?
96
+
97
+ Ask the user to run their package manager's install command:
98
+
99
+ | Manager | Install / refresh command |
100
+ |---|---|
101
+ | pnpm | `pnpm install` |
102
+ | npm | `npm install` |
103
+ | yarn classic | `yarn install` |
104
+ | yarn berry | `yarn install` |
105
+ | bun | `bun install` |
106
+
107
+ In monorepos, also confirm the user is in the right directory (root vs. a specific workspace).
108
+
109
+ ## Step 2 — Detect the previous version
110
+
111
+ Three strategies, in order of preference. **Bun users**: skip strategy 1 if the lockfile is `bun.lockb` (binary) — go straight to strategy 2 or 3.
112
+
113
+ ### Strategy 1 — Git diff on the lockfile (preferred)
114
+
115
+ Use the lockfile that matches the project's package manager:
116
+
117
+ ```bash
118
+ # pnpm
119
+ git log -p -- pnpm-lock.yaml | grep -B 1 -A 2 "'@bratsos/workflow-engine" | head -30
120
+
121
+ # npm
122
+ git log -p -- package-lock.json | grep -B 1 -A 2 '"@bratsos/workflow-engine' | head -30
123
+
124
+ # yarn classic
125
+ git log -p -- yarn.lock | grep -B 1 -A 2 '"@bratsos/workflow-engine' | head -30
126
+
127
+ # yarn berry — same file as classic, different format
128
+ git log -p -- yarn.lock | grep -B 1 -A 4 '@bratsos/workflow-engine' | head -40
129
+
130
+ # bun text lockfile (bun.lock — newer versions)
131
+ git log -p -- bun.lock | grep -B 1 -A 2 '@bratsos/workflow-engine' | head -30
132
+
133
+ # bun binary lockfile (bun.lockb) — cannot grep; use Strategy 2
134
+ ```
135
+
136
+ Look at the most recent commit touching the dependency. The `-` line is the previous version; the `+` line is the new one.
137
+
138
+ ### Strategy 2 — Diff against the project root's `package.json`
139
+
140
+ Lockfile-agnostic; works even with `bun.lockb`:
141
+
142
+ ```bash
143
+ git log -p -- package.json | grep '"@bratsos/workflow-engine' | head -10
144
+
145
+ # In a monorepo, also check the workspace package.json files
146
+ git log -p -- 'packages/*/package.json' | grep '"@bratsos/workflow-engine' | head -10
147
+ ```
148
+
149
+ If the project's declared dep is a pinned version (e.g., `"0.7.0"`), this gives you the previous pin directly. If it's a range (`"^0.7.0"`), the range itself may not have changed across the upgrade — fall back to Strategy 3.
150
+
151
+ ### Strategy 3 — Check the working tree before commit
152
+
153
+ If the upgrade hasn't been committed yet, both versions may still be visible in the unstaged diff:
154
+
155
+ ```bash
156
+ git diff HEAD -- pnpm-lock.yaml package-lock.json yarn.lock bun.lock package.json
157
+ ```
158
+
159
+ ### Strategy 4 — Ask the user
160
+
161
+ Last resort: "Before the upgrade, what version of `@bratsos/workflow-engine` were you on?" — quoting the installed version (from Step 1) helps frame the question.
162
+
163
+ If the previous version is older than 0.7.0, warn the user that some migration guides may not exist yet (we only started writing them at 0.7→0.8).
164
+
165
+ ## Step 3 — List available migrations
166
+
167
+ Look in the installed package's skill directory:
168
+
169
+ ```
170
+ node_modules/@bratsos/workflow-engine/skills/workflow-engine/migrations/
171
+ ```
172
+
173
+ Files are named `migrate-X.Y-to-A.B.md` (semver-tagged). Each one is a self-contained guide for one version step.
174
+
175
+ ## Step 4 — Build the ordered migration chain
176
+
177
+ If the user upgraded `0.6.0 → 0.8.0`, you need to apply migrations in order:
178
+ 1. `migrate-0.6-to-0.7.md`
179
+ 2. `migrate-0.7-to-0.8.md`
180
+
181
+ Sort migration files by their source version (the `X.Y` part), filter to those whose source version is `>= previousVersion` and target version is `<= installedVersion`. Apply in ascending order.
182
+
183
+ ### Skipping over patch releases
184
+
185
+ We only ship migration guides at minor/major version bumps. If the user went `0.7.3 → 0.7.5`, there's nothing to migrate — that's a patch release and no schema/API changes apply. Confirm with the user that they don't see breaking behavior; otherwise, refer them to the changelog.
186
+
187
+ ## Step 5 — Walk through each migration
188
+
189
+ For each migration file, in order:
190
+
191
+ 1. Read the full migration doc.
192
+ 2. Surface the **Required actions** section to the user — these are non-optional.
193
+ 3. Apply schema changes if the migration includes them (Prisma migrations, etc.) — confirm with the user before running `prisma migrate dev` or equivalent.
194
+ 4. Apply code changes — usually with grep + edit, occasionally manual review.
195
+ 5. Note any **Deprecations** — these don't break the current version but the user should plan to address them.
196
+ 6. Verify by running the project's tests if available.
197
+
198
+ ### Monorepo: applying code changes across workspaces
199
+
200
+ If you detected a monorepo in Step 0 and multiple workspaces depend on `@bratsos/workflow-engine`, the **code** changes apply to each of them. Use grep at the repo root to find all call sites:
201
+
202
+ ```bash
203
+ grep -rln 'workflow-engine' --include='*.ts' --include='*.tsx' --include='*.js' \
204
+ packages/ apps/ src/ 2>/dev/null
205
+ ```
206
+
207
+ Apply the migration's recommended edits per workspace. Run typecheck per workspace if the project supports it:
208
+
209
+ ```bash
210
+ pnpm -r typecheck # all workspaces
211
+ pnpm --filter <ws> typecheck # one workspace
212
+ ```
213
+
214
+ **Schema migrations are different.** Prisma migrations live with the workspace that owns the database — usually one specific package. Run `prisma migrate dev` only in that workspace.
215
+
216
+ ## Step 6 — Final check
217
+
218
+ After all migrations applied, run typecheck and the test suite. Use the project's package manager:
219
+
220
+ | Manager | Typecheck | Test |
221
+ |---|---|---|
222
+ | pnpm | `pnpm typecheck` (or `pnpm -r typecheck` in a monorepo) | `pnpm test` (or `pnpm -r test`) |
223
+ | npm | `npm run typecheck` | `npm test` |
224
+ | yarn classic | `yarn typecheck` | `yarn test` |
225
+ | yarn berry | `yarn typecheck` | `yarn test` |
226
+ | bun | `bun run typecheck` | `bun test` |
227
+
228
+ If the project doesn't define these scripts, fall back to `npx tsc --noEmit` for typecheck. Surface any **Bug fixes** sections from the migration docs — these describe behavior changes the user may want to verify against.
229
+
230
+ ## Package manager reference — upgrade commands
231
+
232
+ If the user hasn't run the upgrade yet, run it for them:
233
+
234
+ | Manager | Upgrade to latest published version |
235
+ |---|---|
236
+ | pnpm | `pnpm update @bratsos/workflow-engine --latest` |
237
+ | npm | `npm install @bratsos/workflow-engine@latest` |
238
+ | yarn classic | `yarn upgrade @bratsos/workflow-engine --latest` |
239
+ | yarn berry | `yarn up @bratsos/workflow-engine` |
240
+ | bun | `bun update @bratsos/workflow-engine --latest` |
241
+
242
+ In a monorepo, if the package is a dependency of multiple workspaces, the upgrade typically updates all of them at once for pnpm/yarn/npm workspaces. Bun: confirm by re-running Step 1 against each workspace afterward.
243
+
244
+ ## Convention for writing new migration docs
245
+
246
+ Each `migrate-X.Y-to-A.B.md` file uses this structure:
247
+
248
+ ```markdown
249
+ # Migrating from X.Y to A.B
250
+
251
+ ## Summary
252
+ One paragraph: what changed and why.
253
+
254
+ ## Required actions
255
+ Checklist of non-optional steps. Schema migrations, breaking API changes.
256
+
257
+ ## New features
258
+ What's available that wasn't before. Optional to adopt.
259
+
260
+ ## Deprecations
261
+ What's marked deprecated. Still works in this version, removed in a later one.
262
+
263
+ ## Bug fixes
264
+ Behavior changes the user might notice. Not breaking, but worth knowing.
265
+
266
+ ## Code examples
267
+ Before/after snippets for the most common patterns.
268
+ ```
269
+
270
+ ## Maintainer note
271
+
272
+ When publishing a new minor/major version of `@bratsos/workflow-engine`:
273
+ 1. Write `migrations/migrate-PREV-to-CURRENT.md` before tagging the release.
274
+ 2. Bundle the whole `migrations/` directory in the published npm package — consumers need the full history to walk multi-version upgrades.
275
+ 3. Update this router doc if the convention changes.