Skip to content

fix(core): isolate lifecycle reporting and drain stream work - #4216

Merged
TooTallNate merged 1 commit into
mainfrom
please-sync-pr-httpsgithubcomvercelworkflowpull3-review-follow-up
Sep 25, 2026
Merged

TooTallNate merged 1 commit into
mainfrom
please-sync-pr-httpsgithubcomvercelworkflowpull3-review-follow-up

Conversation

@TooTallNate

@TooTallNate TooTallNate commented Sep 16, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #3678, implementing the correctness fixes and targeted simplifications from Alex's review.

  • Isolate throwing hook-property getters, synchronous scheduling failures, and failures in error formatting/logging. A bad registration cannot escape into a terminal writer or prevent later registrations from running.
  • Move the deployment-guard, max-deliveries, and QuickJS-completion dispatch calls outside their terminal-write try/catch blocks. Rejected writes still never dispatch.
  • Keep the hydration ops array in the dispatcher's waitUntil scope. Drain it after the handler loop, including on partial hydration or handler failure. Use allSettled and drain subsequent batches so one rejected pipe cannot shorten another pipe's lifetime and nested operations are included.
  • Pass ExternalReviverOptions directly to hydrateRunError, preserving default hydration and custom-reviver compatibility while avoiding a redundant external-reviver set.
  • Share hook-parameter documentation and error logging, use realm-safe error messages, and document workflow identifiers, hydration differences, and re-registration behavior.

Review decisions

  • Keep lazy readable hydration: observing a failed run should not read an error's streams unless a handler consumes them.
  • Take the review's documentation option for writables: explicitly document their eager forwarding/lock-polling setup and cover their background lifetime with a real writable regression test.
  • Keep explicit terminal writers: their ordering barriers, conflict policies, encryption/compression choices, and QuickJS pre-serialized fallback paths are distinct. The no-throw dispatcher and post-write call placement address the correctness issue without expanding this into a seven-writer persistence refactor.
  • Keep the typed prepare/invoke pair rather than introducing the proposed params as never assertion. Shared diagnostic logging and parameter interfaces provide the smaller simplifications without that cast.
  • Document unregister-before-reregister for hot reload; an optional registration-key API would be a separate feature decision.

Tests

  • Getter, error-accessor, log-sink, scheduling, and cross-realm error regressions.
  • Actual readable and forwarded-writable operations remain protected after handlers return or throw; partial hydration failures and later-added operations also drain.
  • Success/write-conflict/expiry/failure coverage for all three moved dispatch sites.
  • Real world-local metadata reads through created, started, and completed transitions with payload resolution disabled.
  • Shared waitUntil test capture that fails when expected dispatches never arrive; negative assertions observe the synchronous scheduling boundary.
  • Hydration policy coverage for legacy arrays, persisted bytes, unused writables, and existing eager defaults.

Verified locally:

  • pnpm test --filter=@workflow/core --output-logs=errors-only
  • pnpm build --output-logs=errors-only (28 tasks)
  • pnpm typecheck --output-logs=errors-only (43 tasks)
  • pnpm test:docs -t 'lifecycle-hooks|register-lifecycle-hooks' (3 passed)
  • Biome checks on all changed source/test files, git diff --check, and node scripts/check-changesets.mjs

Docs Preview

Page Preview
Lifecycle Hooks guide Lifecycle Hooks
registerLifecycleHooks API reference registerLifecycleHooks

Preview links require Vercel team access.

Co-Authored-By: Nathan Rajlich <71256+TooTallNate@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings September 16, 2026 23:05
@TooTallNate
TooTallNate requested a review from a team as a code owner September 16, 2026 23:05
@changeset-bot

changeset-bot Bot commented Sep 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b506cc1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
@workflow/core Patch
@workflow/builders Patch
@workflow/cli Patch
@workflow/next Patch
@workflow/nitro Patch
@workflow/vitest Patch
@workflow/web-shared Patch
@workflow/web Patch
workflow Patch
@workflow/world-testing Patch
@workflow/astro Patch
@workflow/nest Patch
@workflow/rollup Patch
@workflow/sveltekit Patch
@workflow/vite Patch
@workflow/nuxt Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
example-nextjs-workflow-turbopack Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
example-nextjs-workflow-webpack Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
example-workflow Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
workbench-astro-workflow Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
workbench-express-workflow Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
workbench-fastify-workflow Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
workbench-hono-workflow Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
workbench-nestjs-workflow Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
workbench-nitro-workflow Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
workbench-nuxt-workflow Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
workbench-python-workflow Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
workbench-sveltekit-workflow Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
workbench-tanstack-start-workflow Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
workbench-vite-workflow Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
workflow-docs Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
workflow-swc-playground Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
workflow-tarballs Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC
workflow-web Ready Ready Preview, v0 Sep 16, 2026 11:09pm UTC

@github-actions

github-actions Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

🧪 E2E Test Results

✅ All tests passed

⚠️ Flaky E2E Tests (passed on retry)

These tests failed at least once and passed on a retry. A recurring entry here is a real race worth investigating.

  • sleepWinsRaceWorkflow (tanstack-start · local-dev / local / node)
  • webhookWorkflow (vite · local-dev / local / quickjs / stable)

🛠 Infra Events (absorbed by the harness)

Platform anomalies the e2e harness detected and worked around (e.g. a run the queue never picked up, replaced by a fresh run). Clustered timestamps indicate a backend blip; a steady drip indicates a platform issue worth escalating.

  • cold-start-warmup · suite warmup (tanstack-start) · at 23:10:53Z · abandoned wrun_01M2P7RM5A0PSSHHH292TKSDBE
  • run-pickup-stall · hookCleanupTestWorkflow - hook token reuse after workflow completion (nextjs-webpack) · at 23:16:06Z · abandoned wrun_01M2P82M8BMAV3AF9AT70NGW6X

E2E Test Summary

Summary
Passed Failed Skipped Total
✅ ▲ Vercel Production 3670 0 731 4401
✅ 💻 Local Development 4014 0 550 4564
✅ 📦 Local Production 4014 0 550 4564
✅ 🐘 Local Postgres 4014 0 550 4564
✅ 🪟 Windows 324 0 2 326
✅ 🌐 Cross-language Conformance 68 0 76 144
✅ vercel-http-transport 825 0 153 978
✅ vercel-multi-region 27 0 0 27
✅ vercel-ws-transport 559 0 93 652
Total 17515 0 2705 20220
Details by Category

✅ ▲ Vercel Production

App Passed Failed Skipped
✅ astro-node 133 0 30
✅ astro-quickjs 133 0 30
✅ example-node 133 0 30
✅ example-quickjs 133 0 30
✅ express-node 133 0 30
✅ express-quickjs 133 0 30
✅ fastify-node 133 0 30
✅ fastify-quickjs 133 0 30
✅ hono-node 133 0 30
✅ hono-quickjs 133 0 30
✅ nest-node 133 0 30
✅ nest-quickjs 133 0 30
✅ nextjs-turbopack-node 160 0 3
✅ nextjs-turbopack-quickjs 160 0 3
✅ nextjs-webpack-node 160 0 3
✅ nextjs-webpack-quickjs 160 0 3
✅ nitro-node 133 0 30
✅ nitro-quickjs 133 0 30
✅ nuxt-node 133 0 30
✅ nuxt-quickjs 133 0 30
✅ python-node 66 0 97
✅ sveltekit-node 152 0 11
✅ sveltekit-quickjs 152 0 11
✅ tanstack-start-node 133 0 30
✅ tanstack-start-quickjs 133 0 30
✅ vite-node 133 0 30
✅ vite-quickjs 133 0 30

✅ 💻 Local Development

App Passed Failed Skipped
✅ astro-stable-node 134 0 29
✅ astro-stable-quickjs 134 0 29
✅ express-stable-node 134 0 29
✅ express-stable-quickjs 134 0 29
✅ fastify-stable-node 134 0 29
✅ fastify-stable-quickjs 134 0 29
✅ hono-stable-node 134 0 29
✅ hono-stable-quickjs 134 0 29
✅ nest-stable-node 134 0 29
✅ nest-stable-quickjs 134 0 29
✅ nextjs-turbopack-canary-node 162 0 1
✅ nextjs-turbopack-canary-quickjs 162 0 1
✅ nextjs-turbopack-stable-node 162 0 1
✅ nextjs-turbopack-stable-quickjs 162 0 1
✅ nextjs-webpack-canary-node 162 0 1
✅ nextjs-webpack-canary-quickjs 162 0 1
✅ nextjs-webpack-stable-node 162 0 1
✅ nextjs-webpack-stable-quickjs 162 0 1
✅ nitro-stable-node 134 0 29
✅ nitro-stable-quickjs 134 0 29
✅ nuxt-stable-node 134 0 29
✅ nuxt-stable-quickjs 134 0 29
✅ sveltekit-stable-node 153 0 10
✅ sveltekit-stable-quickjs 153 0 10
✅ tanstack-start-node 134 0 29
✅ tanstack-start-quickjs 134 0 29
✅ vite-stable-node 134 0 29
✅ vite-stable-quickjs 134 0 29

✅ 📦 Local Production

App Passed Failed Skipped
✅ astro-stable-node 134 0 29
✅ astro-stable-quickjs 134 0 29
✅ express-stable-node 134 0 29
✅ express-stable-quickjs 134 0 29
✅ fastify-stable-node 134 0 29
✅ fastify-stable-quickjs 134 0 29
✅ hono-stable-node 134 0 29
✅ hono-stable-quickjs 134 0 29
✅ nest-stable-node 134 0 29
✅ nest-stable-quickjs 134 0 29
✅ nextjs-turbopack-canary-node 162 0 1
✅ nextjs-turbopack-canary-quickjs 162 0 1
✅ nextjs-turbopack-stable-node 162 0 1
✅ nextjs-turbopack-stable-quickjs 162 0 1
✅ nextjs-webpack-canary-node 162 0 1
✅ nextjs-webpack-canary-quickjs 162 0 1
✅ nextjs-webpack-stable-node 162 0 1
✅ nextjs-webpack-stable-quickjs 162 0 1
✅ nitro-stable-node 134 0 29
✅ nitro-stable-quickjs 134 0 29
✅ nuxt-stable-node 134 0 29
✅ nuxt-stable-quickjs 134 0 29
✅ sveltekit-stable-node 153 0 10
✅ sveltekit-stable-quickjs 153 0 10
✅ tanstack-start-node 134 0 29
✅ tanstack-start-quickjs 134 0 29
✅ vite-stable-node 134 0 29
✅ vite-stable-quickjs 134 0 29

✅ 🐘 Local Postgres

App Passed Failed Skipped
✅ astro-stable-node 134 0 29
✅ astro-stable-quickjs 134 0 29
✅ express-stable-node 134 0 29
✅ express-stable-quickjs 134 0 29
✅ fastify-stable-node 134 0 29
✅ fastify-stable-quickjs 134 0 29
✅ hono-stable-node 134 0 29
✅ hono-stable-quickjs 134 0 29
✅ nest-stable-node 134 0 29
✅ nest-stable-quickjs 134 0 29
✅ nextjs-turbopack-canary-node 162 0 1
✅ nextjs-turbopack-canary-quickjs 162 0 1
✅ nextjs-turbopack-stable-node 162 0 1
✅ nextjs-turbopack-stable-quickjs 162 0 1
✅ nextjs-webpack-canary-node 162 0 1
✅ nextjs-webpack-canary-quickjs 162 0 1
✅ nextjs-webpack-stable-node 162 0 1
✅ nextjs-webpack-stable-quickjs 162 0 1
✅ nitro-stable-node 134 0 29
✅ nitro-stable-quickjs 134 0 29
✅ nuxt-stable-node 134 0 29
✅ nuxt-stable-quickjs 134 0 29
✅ sveltekit-stable-node 153 0 10
✅ sveltekit-stable-quickjs 153 0 10
✅ tanstack-start-node 134 0 29
✅ tanstack-start-quickjs 134 0 29
✅ vite-stable-node 134 0 29
✅ vite-stable-quickjs 134 0 29

✅ 🪟 Windows

App Passed Failed Skipped
✅ nextjs-turbopack-node 162 0 1
✅ nextjs-turbopack-quickjs 162 0 1

✅ 🌐 Cross-language Conformance

App Passed Failed Skipped
✅ python 68 0 76

✅ vercel-http-transport

App Passed Failed Skipped
✅ example 133 0 30
✅ express 133 0 30
✅ hono 133 0 30
✅ nextjs-turbopack 160 0 3
✅ nitro 133 0 30
✅ vite 133 0 30

✅ vercel-multi-region

App Passed Failed Skipped
✅ nextjs-turbopack 27 0 0

✅ vercel-ws-transport

App Passed Failed Skipped
✅ example 133 0 30
✅ express 133 0 30
✅ nextjs-turbopack 160 0 3
✅ vite 133 0 30

📋 View full workflow run

@github-actions

github-actions Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

📊 Workflow Benchmarks

commit b506cc1 · Wed, 16 Sep 2026 23:33:00 GMT · run logs

Backend: vercel · app: nextjs-turbopack

Metric Scenario Best (ms) P75 (ms) P90 (ms) P99 (ms) Samples
TTFS step 312 (-77%) 💚 2308 🔴 (+31%) 🔻 2393 🔴 (+30%) 🔻 2827 🔴 (+12%) 30
TTFS stream 227 (+48%) 🔻 2276 🔴 (+35%) 🔻 2326 🔴 (+36%) 🔻 2432 🔴 (+19%) 🔻 30
TTFS hook + stream 659 (+22%) 🔻 2698 🔴 (+24%) 🔻 2757 🔴 (+23%) 🔻 2856 🔴 (+13%) 30
Fan-out TTFS Promise.all(100 steps) 647 (+23%) 🔻 856 (-57%) 💚 945 (-55%) 💚 2817 (+32%) 🔻 10
Fan-out TTLS Promise.all(100 steps) 1996 (+22%) 🔻 3893 (+8.7%) 3959 (-1.0%) 10726 (+31%) 🔻 10
STSO 1020 steps (inline) 128 (+4.1%) 153 (+2.7%) 173 (+3.6%) 246 (+2.1%) 1019
WO 1020 steps 153303 (+2.3%) 153303 (+2.3%) 153303 (+2.3%) 153303 (+2.3%) 1
CRTT first chunk (pooled) 74 (+21%) 🔻 143 (+39%) 🔻 187 (+51%) 🔻 452 (+223%) 🔻 28

Streams

Scenario CRTT 1st p75 p90 p99 CDV max iters
paced control (100/s, 60B) 91.5 (+10%) 238 (+20%) 404 (+48%) 897 (+97%) 185 (-2%) 10
size sweep (100/s, 160B-12KB) 98 (+7%) 189 (-26%) 260 (-23%) 714 (+13%) 161 (-33%) 10
replay gateway-gpt-5.4-nano-2000t (1x) 144 (+73%) 150 (-3%) 192 (-4%) 789 (+148%) 198 (-1%) 3
replay eve-gpt-5.6-sol-2000t (1x) 112 (+43%) 167 (-14%) 209 (-33%) 891 (+3%) 652 (±0%) 2
replay eve-gpt-5.6-sol-2000t (2x) 161 (+41%) 291 (-10%) 353 (-19%) 456 (-48%) 221 (-21%) 3
📈 STSO distribution vs main (inline / queue-hop histograms)

1020 steps (inline)

Cumulative STSO time: main 149528ms → this run 153078ms (Δ +3550ms, +2%)

100-150 ms  █████████████████████┃██  main 773  this 713   -60
150-200 ms  ███████┃                  main 215  this 270   +55
200-250 ms  ┃                         main  21  this  26    +5
250-300 ms  ┃                         main   6  this   5    -1
300-350 ms  ┃                         main   1  this   1    +0
350-400 ms  ┃                         main   1  this   3    +2
400-450 ms  ┃                         main   1  this   1    +0
550-600 ms  ┃                         main   1  this   0    -1
📈 CRTT drill-down vs main (RTT distributions & profiles)
variant  RTT 1ms→5s+             avg         p50         p90          p99     n
control  ······▁█▄▁···  186.8 (+24%)  163 (+20%)  404 (+48%)   897 (+97%)  3000
sweep    ······▁█▂▁···   159.7 (-6%)   144 (-1%)  260 (-23%)   714 (+13%)  3000
gw 1x    ·····▁▃█▁▁···   136.4 (+2%)   127 (+2%)   192 (-4%)  789 (+148%)  5295
eve 1x   ·····▁▃█▂▁▁··  147.7 (-12%)   125 (-4%)  209 (-33%)    891 (+3%)  5186
eve 2x   ·····▁▁█▇▁···     201 (-3%)   172 (+3%)  353 (-19%)   456 (-48%)  7779

RTT over stream progress (avg per tenth of stream, bars scaled min→max):

control  ▂▆█▆▄▄▁▃▂▁  153–238ms
sweep    ▅▆▆█▂▄▄▃▁█  144–173ms
gw 1x    ▃▁▁█▇▁▁▁▂▅  124–166ms
eve 1x   ▂▁▁▁▂▁▂█▂▁  126–267ms
eve 2x   ▃▁▄▄▃▆▆█▅▃  141–268ms

RTT by chunk size (avg per log size bin, ~160B → ~12KB serialized, bars scaled min→max):

sweep  ▁▃▃█▄▂▁  159–161ms

Delivery jitter over stream progress (avg positive CDV per tenth of stream, bars scaled min→max):

control  ▃█▄▁▃▄▄▃▂▁  48–83ms
sweep    ▁▃█▄▃▂▂▃▅▄  58–83ms
gw 1x    ▄▅▂▆▄▆▃▅▁█  36–46ms
eve 1x   ▃▃▁▄▃▁▃█▁▁  23–36ms
eve 2x   ▅▃▁▅▃█▇▃▂▆  26–33ms
ℹ️ Metric definitions & methodology

Streams: first-chunk RTT (the stream-open path, before any buffering/backpressure), CRTT percentiles, and worst delivery stall (CDV max). Cells are medians across iterations; per-run values in the artifacts. No 🔴/🟢 marks until targets attach.

The collapsed STSO distribution section above buckets every step gap, split inline (same warm process — pure framework overhead) vs queue-hop (fresh process — dispatch, reinit, replay). █ = main, ┃ = this run, ░ = fill.

The collapsed CRTT drill-down: per-variant RTT histograms (fixed log bins, · = empty) and mean RTT/positive-CDV profile lines over stream progress and chunk size. Histograms, avgs, and profiles merge exactly across runs; p50–p99 are percentile-of-percentiles. Per-index rows live in the artifacts.

Best/P75/P90/P99 deltas compare against the most recent benchmark run on main at the time of this run. 🔻 flags a delta worse than +15%, 💚 one better than −15%.

Metrics — TTFS: time to first step body (in-deployment start() → first step body) · Fan-out TTFS: fan-out time to first step (in-deployment start() → first of the parallel step bodies to complete) · Fan-out TTLS: fan-out time to last step (in-deployment start() → last of the parallel step bodies to complete, i.e. when the Promise.all resolves) · STSO: step-to-step overhead (gap between consecutive step bodies) · WO: workflow overhead (whole-run time outside step bodies, in-deployment anchored) · CRTT: chunk round-trip time (per-chunk write → read latency, one clock domain: deployment → stream backend → same deployment) · CDV: chunk delay variation / delivery jitter (inter-arrival gap minus inter-write gap per seq-adjacent pair; skew-free; the row is each run's MAX positive value, so one stall moves it)

Scenarios — step: one trivial no-op step, no stream; no hooks, so the run stays in turbo mode (in-process fast path) · stream: one streaming step; no hooks, so the run stays in turbo mode (in-process fast path) · hook + stream: registers a hook before one step, which exits turbo mode (dispatch path) · 1020 steps: 1020 trivial sequential steps; STSO is measured between consecutive steps in the given step ranges, and WO is the whole-run overhead outside step bodies · Promise.all(100 steps): 100 trivial no-op steps started together in a single Promise.all; Fan-out TTFS is the first of them to complete and Fan-out TTLS the last, both from the in-deployment clientStart, so their gap is the spread the runtime adds across the fan-out · paced control (100/s, 60B): the control: 300 tiny (~60B) deltas metronome-paced at 100/s — zero workload structure, so it reads the transport floor and flush cadence, and disambiguates transport-wide vs workload-specific when a replay row moves · size sweep (100/s, 160B-12KB): same pacing as the control with deltas padded in rotation across seven log-spaced sizes (~160B–12KB) — rotation decouples size from stream position, so it isolates whether chunk size causes latency · replay gateway-gpt-5.4-nano-2000t (1x): raw provider SSE cadence captured at the AI gateway boundary (gpt-5.4-nano, the most popular gateway model; per-token deltas p50 208B = the modal production chunk size), replayed exactly as measured — the typical customer's workload; its CDV is the typical customer's real delivery jitter · replay eve-gpt-5.6-sol-2000t (1x): a captured eve turn (gpt-5.6-sol, the most-used demanding eve model; ~2000 output tokens = production p50 turn length) replayed exactly as measured — eve's envelope protocol re-ships the cumulative message so sizes ramp 142B→13KB; the demanding outlier tenant's reality · replay eve-gpt-5.6-sol-2000t (2x): the same eve capture at 2x — the headroom/stress row; real fast-tier models emit the same chunk sizes at proportionally higher rate, so time compression is a faithful speed model · first chunk (pooled): every run's seq-0 RTT pooled across all stream scenarios — the first chunk precedes any workload differentiation, so pooling samples one shared stream-open path with exact percentiles

Replay cadences (semantic sha256) — eve-gpt-5.6-sol-2000t eaf22f5946e7c61f3c65c7006d550df180cfabd4e706254a09f22aec0cfb420d · gateway-gpt-5.4-nano-2000t 6f24ac518b6b83ff1d0e85a5fe78230db192716d66a7fc6b2fe022752001d041

🔴 marks a percentile over its target (within target is left unmarked). Targets (p75/p90/p99, ms) — TTFS 200/300/600

All timestamps are deployment-side; runs are triggered in-deployment, so the CI runner and api.vercel.com sit outside every measured window. TTFS = start() → first step body (includes dispatch + any cold start); Fan-out TTFS/TTLS = first/last step completion of one Promise.all from the same anchor (the gap is the runtime’s fan-out spread); STSO/WO between step bodies; CRTT inside the workflow (excludes the api.vercel.com read path).

Cold starts stay in the numbers (real bursty-workload latency, inflates P75+); Best is the warm floor.

@github-actions

Copy link
Copy Markdown
Contributor

Sim World

Simulated world deterministic testing for races. Traces

🟠 world-sim scenario book — 1 fail of 41 total

fence=per-spec

scenario outcome events virt replay violations
✅ smoke-no-steps completed 3 0ms ok 0
✅ smoke-one-step completed 6 0ms ok 0
✅ hook-at-step-started completed 12 0ms ok 0
✅ hook-at-step-completed completed 12 0ms ok 0
✅ hook-at-hook-created completed 12 0ms ok 0
✅ deadline-hook-wins completed 7 1.0h ok 0
✅ deadline-expires completed 7 1.0h ok 0
✅ long-sleep completed 11 30.0d ok 0
✅ hook-never-arrives stalled 3 0ms skipped 0
✅ step-retries-twice completed 10 2.0s ok 0
✅ parallel-steps completed 9 0ms ok 0
✅ hook-on-execution-state completed 12 0ms ok 0
✅ peek-hook-before-branch completed 12 0ms ok 0
✅ peek-hook-after-branch completed 12 0ms ok 0
✅ peek-hook-at-registration completed 12 0ms ok 0
✅ race-hook-before-probe completed 12 0ms ok 0
✅ race-hook-after-probe completed 12 0ms ok 0
✅ race-duplicate-delivery completed 13 0ms ok 0
✅ attr-hook-before-step completed 11 0ms ok 0
✅ attr-hook-after-step completed 11 0ms ok 0
✅ attr-from-step-body completed 13 0ms ok 0
✅ fork-hook-after-timeout completed 14 1.0m ok 0
✅ fork-hook-before-timeout completed 14 1.0m ok 0
✅ count-hook-after-timeout completed 17 1.0m ok 0
✅ count-hook-before-timeout completed 20 1.0m ok 0
✅ stale-read-step-count-fork completed 20 1.0m ok 0
✅ stale-read-equal-step-counts completed 14 1.0m ok 0
✅ step-vs-step-fork completed 12 0ms ok 0
✅ step-vs-step-fork-fenced completed 12 0ms ok 0
✅ fence-catches-benign-direction completed 12 5ms ok 0
✅ in-flight-before-decision completed 17 1.0m ok 0
❌ in-flight-before-decision-counted completed 17 1.0m ok 0
✅ in-flight-after-decision completed 19 2.0m ok 0
✅ stale-read-step-count-fork-fenced completed 20 1.0m ok 0
✅ fork-hook-wins completed 13 1.0m ok 0
✅ fork-timeout-wins completed 13 1.0m ok 0
✅ unclaimed-payload-under-fork completed 17 1.0m ok 0
✅ claimed-payload-under-fork completed 17 1.0m ok 0
✅ writers-independent-step-bodies completed 12 0ms ok 0
✅ writers-scripted-tempo completed 12 0ms ok 0
✅ cancel-mid-step cancelled 7 0ms skipped 0

Full trace: world-sim.txt

@github-actions

Copy link
Copy Markdown
Contributor
Framework Flow route Step reg. Framework output
hono 251.0 KiB (±0) 93.4 KiB (-3 B) 1.90 MiB (+178 B)
nextjs-turbopack 257.5 KiB (±0) 426 B (±0) 901.5 KiB (+68 B)
About these numbers

Sizes are gzip; parentheses show the change against main.
Flow route and Step reg. gate this job, on raw bytes rather than the gzip shown, at max(2%, 50.0 KiB). Framework output is informational.

b506cc1 · run

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Address the completion-dispatch logging path and add current Docs Preview links before approval.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Hardens @workflow/core lifecycle reporting so terminal writes remain authoritative and hydrated stream work drains correctly.

Changes:

  • Isolates hook, scheduling, logging, and handler failures.
  • Moves lifecycle dispatch after successful terminal writes.
  • Adds hydration, stream-draining, regression tests, documentation, and a changeset.
File summaries
File Description
packages/core/test-utils/lifecycle-hooks.ts Shared lifecycle test utilities
packages/core/src/types.ts Realm-safe error formatting
packages/core/src/types.test.ts Error-formatting regression tests
packages/core/src/serialization.ts Hydration policy support
packages/core/src/serialization-lifecycle.test.ts Stream hydration and writable tests
packages/core/src/runtime/run-metadata.test.ts Metadata hydration coverage
packages/core/src/runtime/replay-budget.test.ts Dispatch ordering tests
packages/core/src/runtime/quickjs-lifecycle.test.ts QuickJS lifecycle coverage
packages/core/src/runtime/quickjs-entrypoint.ts Post-write completion dispatch
packages/core/src/runtime/max-deliveries-lifecycle.test.ts Max-delivery tests
packages/core/src/runtime/lifecycle-hooks.ts Safe dispatch and stream draining
packages/core/src/runtime/lifecycle-hooks.test.ts Dispatcher and hydration regression tests
packages/core/src/runtime/deployment-guard.ts Post-write failure dispatch
packages/core/src/runtime/deployment-guard.test.ts Deployment-guard coverage
packages/core/src/runtime.ts Max-delivery dispatch ordering
packages/core/README.md Lifecycle behavior documentation
docs/content/docs/v5/observability/lifecycle-hooks.mdx Lifecycle guide updates
docs/content/docs/v5/api-reference/workflow-api/register-lifecycle-hooks.mdx Lifecycle API documentation updates
.changeset/lifecycle-reporting-isolation.md Core patch changeset
Review details

Suppressed comments (1)

packages/core/src/runtime/quickjs-entrypoint.ts:2031

  • terminalCreateEvent has already succeeded before this dispatch, but wfdiag('exit_completed', ...) is still inside the surrounding try. If the diagnostic logger (which ultimately calls console.debug) throws, the catch path exits before this line and the persisted run never invokes onRunCompleted. Keep the diagnostic call from being able to suppress lifecycle dispatch (for example, isolate it from the write catch).
    dispatchRunCompletedHooks(runId, workflowName);
  • Files reviewed: 19/19 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Keep the dynamic `workflow/api` import inside the `NEXT_RUNTIME === "nodejs"` guard. Next.js also compiles `instrumentation.ts` for the Edge runtime. A top-level static import pulls Node.js-only dependencies into that compilation and breaks webpack Edge builds, even if the registration call is guarded.

`registerLifecycleHooks` returns an unregister function. You can register multiple hook sets, and handlers run in registration order.
`registerLifecycleHooks` returns an unregister function. You can register multiple hook sets, and handlers run in registration order. Registrations are not deduplicated: register each hook set once per process, and call its unregister function before registering it again during hot reload or module re-evaluation. Otherwise, repeated registrations invoke the same handler multiple times for each transition.

@pranaygp pranaygp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of the follow-up. Short version: the terminal-write isolation and the stream drain are correct, and I found no correctness bugs in the diff. Most of what follows is about docs and topology. Several of the items below date from #3678, but this PR's docs are the ones people will copy, so they seem worth settling here or in linked follow-ups.

Verified locally

  • Unit tests:
    • The 8 changed test files: 116 tests pass.
    • The full @workflow/core unit suite: 2610 pass. The only failures are e2e suites that need DEPLOYMENT_URL.
  • The docs code samples typecheck, 3/3.
  • Dispatch sites: in runtime.ts:865-891, deployment-guard.ts:196-237 and quickjs-entrypoint.ts:2003-2031, every catch branch now returns or throws before dispatch. Conflict, expired and failed writes never dispatch.
  • The drain loop (lifecycle-hooks.ts:181-197) held up under a randomized property test against the real dispatcher:
    • 300 trials, with nested ops up to depth 3, random pipe rejections, and throws during partial hydration and in handlers.
    • waitUntil never settled while an op pushed before settlement was still outstanding.
    • The loop terminates because drained strictly increases.
  • The documented Next.js pattern, await import("workflow/api") in instrumentation.ts, works end to end:
    • Next 16.3.4 on turbopack and on webpack, under next start and standalone node server.js.
    • Each handler fired exactly once per transition, and the registry held 1 entry.
    • The Symbol.for registry correctly bridges the Instrumentation layer's copy of @workflow/core and the App Route layer's copy.

Topology: the docs' "register everywhere" claim does not hold on some Vercel builds

  1. Nuxt 4 / Nitro v2, Astro, Nest, and the CLI vercel-build-output-api target on Vercel: hooks never fire, silently.
    • These integrations emit .well-known/workflow/v1/flow.func as a self-contained esbuild bundle through VercelBuildOutputAPIBuilder:
      • packages/builders/src/vercel-build-output-api.ts:26-48
      • packages/nitro/src/index.ts:230-239 and builders.ts:38
      • packages/astro/src/plugin.ts:68-69
      • packages/nest/src/vercel-builder.ts:229-262
      • packages/cli/src/commands/build.ts:84-85
    • That function carries the queue trigger, so it is the one that writes terminal events. It never loads the app's startup code (Nitro plugins, Astro middleware, Nest bootstrap), so the registry is empty there.
    • Dispatch then returns without logging anything (lifecycle-hooks.ts:168). I confirmed this with a scratch Nitro v2 build using the Vercel preset.
    • The obvious workaround, registering at the top level of a workflow file, makes things worse. The VM bundle resolves workflow/api to the throwing stub, which breaks every workflow in that file.
    • "Any module that loads at startup works" (lifecycle-hooks.mdx:18) and "(which instrumentation.ts guarantees)" (:95) are therefore not accurate in general.
    • Nitro v3 (Vite, TanStack Start, and the Express/Hono/Fastify setups), SvelteKit (hooks.server.ts) and Next are fine, because their flow route is compiled into the app's own server.
  2. Next.js before 16.3.0 on Vercel has a cold-start race.
    • Before 16.3.0, RouteModule.prepare does not await ensureInstrumentationRegistered (vercel/next.js#93993, fixed in #94306 and first released in v16.3.0).
    • So a cold flow invocation can write the terminal event before the dynamic import("workflow/api") has resolved, and the handlers are skipped.
    • @workflow/next allows next: >13 (packages/next/package.json:54).
    • 16.3.4 awaits it (route-module.js:367-373).
  3. Nitro calls plugins without awaiting them. An async plugin that copies the Next await import() pattern has the same race. Nitro users should register with a static import and a synchronous call.
  4. No diagnostics. Every failure above is invisible. Consider a one-time debug log when a terminal event is written and the registry is empty, at least in dev.

Suggestion: have each builder own registration. Something like withWorkflow(config, { lifecycleHooks: "./lib/workflow-hooks.ts" }) or workflow({ lifecycleHooks }) would inject a host-side import of that module into the generated flow and webhook routes. It must never go into the VM bundle.

  • That guarantees presence on every function that writes terminal events.
  • It removes the Next version requirement, the NEXT_RUNTIME guard and the dynamic-import caveat.
  • With a fixed internal key (see the hot-reload section), it also fixes HMR.

Until then, please replace the prose with a per-framework table: where to register, "Next.js ≥ 16.3", and which Vercel builds are not supported yet.

Module copies: instanceof is unreliable in handlers (docs change needed)

Next bundles instrumentation.ts separately from app routes. withWorkflow does not externalize workflow or @workflow/core (packages/next/src/index.ts:493-499). So the process holds two copies of @workflow/core, @workflow/errors and the user's own modules. This happens with a static import too; the dynamic import is not the cause. The ESM/CJS dual-package hazard does not apply, because these packages are ESM-only.

The registry, World cache, step registry and OTel global are all shared via globalThis (scripts/lint/module-scope-state.mjs reports 0 findings). Class identity is not shared. Tested on both bundlers, inside handlers registered from instrumentation.ts:

Check Result
run instanceof Run false
error instanceof WorkflowRunFailedError false; .is() works
error.cause instanceof MyError for a WORKFLOW_SERIALIZE class false (the SWC registration is last-writer-wins, and the route copy evaluates later)
Plain Error subclass (no custom serialization) comes back as a generic Error, name preserved
cause instanceof FatalError depends on which @workflow/errors copy loaded first (first-writer-wins global, packages/errors/src/index.ts:1114-1170)

Side effect on route code. On a standalone or Vercel-style server, where next.config is not evaluated at runtime, adding the documented instrumentation.ts import makes the Instrumentation layer's @workflow/errors the process-wide FatalError. Route-side err.cause instanceof FatalError then flips from true to false. I A/B tested this on the same build, on turbopack and on webpack. The root cause predates this PR, but these docs route every lifecycle-hooks user into it.

Asks:

  • In both pages, say that handler parameters come from the runtime's module copy. Show WorkflowRunFailedError.is(error), FatalError.is(error.cause), error.cause.name and errorCode in the examples instead of instanceof.
  • Soften "registered Error subclass identity preserved" (lifecycle-hooks.mdx:53, and the JSDoc at lifecycle-hooks.ts:41).
  • Longer term, a Symbol.hasInstance brand on Run, WorkflowRunFailedError and the errors classes would make instanceof work across copies.
  • The "module copies" unit test (lifecycle-hooks.test.ts:320-331) never loads a second module instance. Using vi.resetModules() to register through copy A and dispatch through copy B would cover it. Today only the Next-only e2e test covers this.

Hot reload: dedupe should live in the SDK, not in user code

The PR asks users to "unregister the previous hooks before registering again", which in practice means writing a globalThis singleton themselves. What I measured:

  • next dev (turbopack and webpack), documented instrumentation.ts pattern: no duplicates. Next memoizes register() per process (instrumentation-globals.external.js:81-86). The flip side is that edits to the handlers are ignored until the dev server restarts, and the docs don't say so.
  • Duplicates occur on the other paths the docs point people to:
    • With Vite plus nitro/vite, the Nitro plugin re-runs on every server-file edit in the same globalThis. In the worst case I saw 5 handlers fire for one run. close hooks and import.meta.hot.dispose never fire, so no framework-native cleanup is possible.
    • Editing SvelteKit's hooks.server.ts re-runs init.
    • Module-scope registration inside a Next route or lib re-registers on every HMR update. Webpack dev also evaluates it once per route entry.
    • nitro dev (v3) is safe, because each rebuild gets a fresh worker.
  • Step functions accumulate handlers. Only the workflow export condition gets the throwing stub; step bundles get the real function. Calling it from a step therefore adds a handler on every execution. The stub's error message (api-workflow.ts:15-19) says "Move this call to a step function", which leads users straight into that leak. Please give registerLifecycleHooks its own stub message ("register at startup, e.g. in instrumentation.ts"), and warn or throw when it is called inside step context.

Suggestion: ship the registration-key API the PR description deferred: registerLifecycleHooks(hooks, { key?: string }).

  • The same key replaces the existing registration in place, keeping its position.
  • A stale unregister from an older module instance becomes a no-op.
  • No key keeps today's append semantics, so libraries and the app can still coexist.
  • Store it in the same Symbol.for registry.

A userland copy of these semantics under vite dev kept the registry at 1, and edits applied. The docs then drop the unregister instructions and just show { key: "app" }. The builder-owned registration above can use a fixed internal key.

Pre-seed the handler's Run (avoid the reads the docs warn about)

Handlers get a bare new Run(runId) (lifecycle-hooks.ts:220, :273). One handler awaiting run.workflowName, run.status and run.createdAt makes 3 runs.get calls, and returnValue adds 1–2 more. The dispatcher already has every one of these values in memory.

What every one of the 8 dispatch sites already has:

  • runtime.ts:455, :885, :3466, :3679, :5299
  • deployment-guard.ts:230
  • replay-budget.ts:186
  • quickjs-entrypoint.ts:2031, :2383

The known values are the immutable workflowName, the terminal status (terminal states never change, run.ts:585), the exact persisted output or error bytes and the key they were written with, errorCode, and usually the updated run record. world.events.create returns EventResult.run (world/src/events.ts:962-966) in world-local, Postgres and the hosted backend, but all 8 sites discard it.

Proposal: add an @internal createTerminalRun(runId, seed) in run.ts, backed by a module-level WeakMap.

  • The public constructor and WORKFLOW_SERIALIZE stay unchanged, and nothing new is exported from workflow/api.
  • status and workflowName resolve with no read.
  • The timestamps come from the record when there is one.
  • returnValue goes through the existing #resolveTerminalReturnValue, using the writer's bytes and key.
  • exists, streams, cancel and wakeUp still read.
  • The seed does not survive serialization, so a Run passed to start() falls back to reads.
  • The writer's steady-state path gains nothing: dispatch still returns early when no hooks are registered.

Two correctness constraints:

  • Retention. Only world-local returns the purged record with expiredAt from the terminal write. Postgres purges in a separate transaction (retention.ts:128-137), and the hosted backend purges after it responds. For a run with zero retention (purgesUserDataOnFinish(record.attributes)), do not seed returnValue. Otherwise the handler would get a value where getRun(id).returnValue throws RunExpiredError. Always take payloads from the writer's bytes, never from record.output, which may be a ref descriptor on the hosted backend.
  • Deployment guard. Its undefined key means "this payload is unencrypted", not "this run has no key" (deployment-guard.ts:196-200). Keep the payload key separate from the run's key. Prime #getEncryptionKey only when the key is defined; otherwise run.getReadable() breaks on encrypted runs.

Tests to add:

  • Zero reads for the seeded accessors.
  • Decryption with an encryption key.
  • A failed run's returnValue rejects with the seeded errorCode and the same cause identity.
  • Expiry with zero retention.
  • No record → fall back to reads.
  • Deployment-guard key handling.
  • The seed does not survive serialization.

With this in, the "lazy access defers those reads; it does not make them free" paragraphs (lifecycle-hooks.mdx:48, register-lifecycle-hooks.mdx:56) can instead say that these accessors resolve from the terminal write.

Stream-lifetime and observability (from my earlier pass)

  1. Leaked lock → unbounded waitUntil. A handler that calls getReader() on a readable in error.cause and never releases it keeps the drain, and the workflow.lifecycle.onRunFailed span, open until the function's max duration (lifecycle-hooks.ts:185-196); I reproduced this. reader.cancel() settles it, and an unused writable settles in about 50 ms. Please make this a warning callout with a try { … } finally { await reader.cancel() } snippet. Optionally, bound only the post-handler drain (cancel and log after N seconds), which keeps the cost on the rare path.
  2. logFailure can drop a handler failure (lifecycle-hooks.ts:141-155). All its fields are built inside one try. If String(err) throws (for example a thrown object whose toString throws), no log line is emitted at all; I reproduced this. Compute each field defensively.
  3. A throwing hook getter is logged as handler threw (:164). Something like handler property access threw would let people tell a bad registration apart from a failing handler.
  4. The span is thin. workflow.lifecycle.${event} has no workflow.run_id or workflow.name attribute. Handler errors are swallowed before the span sees them, so its status is OK even when every handler threw. Its duration now includes the drain. Please add those attributes and a span event (or a count) for handler failures, and mention the span in observability/tracing.mdx.
  5. Hydration fallback loses information. Any revive failure (for example a WORKFLOW_SERIALIZE class that isn't registered on the host) replaces the whole cause with Error('Failed to hydrate workflow run error'). The original name and message are lost, and the exception is never logged. That undercuts the Sentry use case. Degrading per value, or at least logging the reason, would help. Run.returnValue (run.ts:574-579) has the same catch-all.
  6. Docs scope for stream lifetime. The drain covers streams from error.cause only. Run.returnValue hydrates with a throwaway ops array (run.ts:551, :569). So stream pipes started through await run.returnValue in onRunCompleted, or by un-awaited or setTimeout consumption, are not kept alive by waitUntil. Worth one sentence. The "waitUntil scope" sentence is also Vercel-specific and could start with "On Vercel, …".
  7. Writables. A hydrated writable in the cause still forwards to the failed run's stream, or to the parent run's stream when forwarded. A reporting handler can therefore append to a run's stream after its terminal event. This matches run.returnValue, but an explicit "writes still reach the run's stream" line would stop anyone assuming error.cause is inert.

Nits

  • The Docs Preview links in the description return 404. The pages live at /v5/docs/observability/lifecycle-hooks and /v5/docs/api-reference/workflow-api/register-lifecycle-hooks.
  • In the API reference, the dedupe note is under Returns but missing from Behavior.
  • RunCompletedHookParams extends RunHookParams {} could be type RunCompletedHookParams = RunHookParams, which avoids Biome's noEmptyInterface.
  • Missing tests: a thrown value whose toString throws; a handler that cancels a readable mid-read; the randomized nested-ops drain check (cheap to keep as a regression test).
  • Separate from this PR: whats-new.mdx:103-118 says plain user Error subclasses "keep their class". In my tests a subclass without WORKFLOW_SERIALIZE came back as a generic Error with only its name.

Recommendation: the code changes look good to merge. Before this ships, I'd fix the docs:

  • the per-framework and Next ≥ 16.3 caveats;
  • .is() instead of instanceof;
  • the leaked-lock warning;
  • a restart note for next dev.

I'd track builder-owned registration, the { key } API and Run pre-seeding as follow-ups (happy to open issues).

@pranaygp pranaygp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving: the code changes are correct and well tested (see my review above for what I verified). The docs and topology items there, and the builder-owned registration / { key } API / Run pre-seeding follow-ups, don't block this PR. Please pick up the docs caveats (.is() over instanceof, Next ≥ 16.3 and unsupported Vercel builds, the leaked-lock warning, the next dev restart note) here or in a fast follow.

@TooTallNate
TooTallNate merged commit 25ec4ba into main Sep 25, 2026
187 checks passed
@TooTallNate
TooTallNate deleted the please-sync-pr-httpsgithubcomvercelworkflowpull3-review-follow-up branch September 25, 2026 21:44
@github-actions

Copy link
Copy Markdown
Contributor

No backport to stable for 25ec4ba (AI decision).

This is a follow-up correctness pass on the lifecycle-hooks feature (registerLifecycleHooks, dispatchRunFailedHooks/dispatchRunCompletedHooks) introduced in #3678, which exists only on main: packages/core/src/runtime/lifecycle-hooks.ts, deployment-guard.ts, the new lifecycle tests, and the v5 lifecycle-hooks docs are all absent from origin/stable (verified with git ls-tree origin/stable). Every substantive change — hook-getter isolation, moving dispatch calls outside terminal-write try/catch, draining the hydration ops array in the dispatcher's waitUntil scope, and the new ExternalReviverOptions parameter on hydrateRunError — exists to serve that main-only API, so there is no defect on stable for it to fix.

To override, re-run the Backport to stable workflow manually via workflow_dispatch and paste this commit SHA into the ref input:

25ec4ba7edb2cb96d792cab7e9f74c88bf86c472

This branch was successfully deployed

18 active deployments
Preview – workflow-swc-playground — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – example-nextjs-workflow-webpack — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – workflow-docs — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – workbench-nuxt-workflow — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – example-workflow — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – workbench-tanstack-start-workflow — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – example-nextjs-workflow-turbopack — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – workbench-vite-workflow — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – workbench-sveltekit-workflow — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – workbench-hono-workflow — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – workbench-astro-workflow — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – workbench-nestjs-workflow — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – workbench-express-workflow — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – workbench-fastify-workflow — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – workbench-nitro-workflow — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – workflow-tarballs — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – workflow-web — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Preview – workbench-python-workflow — b506cc17 Deployed Sep 16, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants