Skip to content

fix(core): batch stream writes via writeMulti#2995

Draft
VaguelySerious wants to merge 1 commit into
mainfrom
peter/stream-writemulti-batching
Draft

fix(core): batch stream writes via writeMulti#2995
VaguelySerious wants to merge 1 commit into
mainfrom
peter/stream-writemulti-batching

Conversation

@VaguelySerious

Copy link
Copy Markdown
Member

Problem

Stream writes never batch. Every chunk becomes its own server round trip, even under a fast burst — world.streams.writeMulti is effectively dead code.

Two facts combine to defeat the buffer/writeMulti machinery in WorkflowServerWritableStream:

  1. The WHATWG WritableStream contract serializes the sink. write(chunk) for the next chunk is not invoked until the previous write()'s promise settles.
  2. flushablePipe awaits each writer.write() before reading the next chunk, and the sink's write() resolves only after its flush completes (needed so pendingOps/durability tracking is accurate).

Together these mean the sink's buffer never holds more than one chunk when the flush timer fires, so the batching path (chunksToFlush.length > 1) never runs.

Reproduction

Piping 20 chunks that are all available instantly through the current coordination:

writeMulti calls (batches >1): []
single write calls: 20

Fix

  • WorkflowServerWritableStream now exposes a durable batch write under a new STREAM_WRITE_BATCH_SYMBOL. It reuses the existing flush() path, so a batch goes out as one writeMulti (or sequential writes when the world lacks writeMulti) and resolves only once the whole batch has reached the server.
  • flushablePipe feature-detects the symbol and runs a producer/consumer: the producer reads into a queue, the consumer drains the whole queue into one batch write at a time. Chunks that arrive while a previous batch is in flight coalesce into the next server write — turning a burst into a handful of round trips instead of one per chunk. Bounded read-ahead (WORKFLOW_STREAM_MAX_INFLIGHT_CHUNKS, default 1000) provides backpressure.
  • Durability is preserved exactly. pendingOps counts chunks read-but-not-yet-durably-written (incremented at read time, decremented only after the batch write resolves), so the lock-release completion (pollWritableLock/pollReadableLock) never fires while data is still queued or in flight, and the source-done path awaits every batch before closing.
  • Sinks without the batch entry point (plain WritableStreams, TransformStream writables on the read path) keep the unchanged per-chunk path — so all existing behavior and tests are untouched.

After the fix, the same 20-chunk burst emits 1 initial single write + one writeMulti of the remaining 19.

Tests

  • flushable-stream.test.ts: coalescing during an in-flight batch, in-order delivery + close, pendingOps durability across lock-release, batch-write failure propagation, and backpressure.
  • writable-stream.test.ts: end-to-end on the real WorkflowServerWritableStream + mock world via flushablePipe, asserting one writeMulti with the coalesced chunks, plus the sequential-write fallback when the world has no writeMulti.
  • Full pnpm test scope (vitest run src, world=local): 69 files, 1489 passed, 0 failures. Typecheck clean; no new lint warnings.

Notes for reviewers

  • The producer/consumer concurrency (the 3-way Promise.race on read / sink-closed / consumer-failure, and the backpressure wait) is the part most worth a close read.
  • Opening as draft for design review of the coordination approach before polishing.

🤖 Generated with Claude Code

…chunk

flushablePipe awaited each writer.write() and the WritableStream sink
serializes chunks one at a time, so WorkflowServerWritableStream's buffer
never held more than one chunk and its writeMulti batching path (gated on
chunksToFlush.length > 1) never engaged — every chunk became its own
server round trip.

WorkflowServerWritableStream now exposes a durable batch write under
STREAM_WRITE_BATCH_SYMBOL. flushablePipe feature-detects it and runs a
producer/consumer that coalesces chunks arriving while a previous batch is
still in flight into a single writeMulti, preserving the existing
durability guarantees (pendingOps counts read-but-not-durable chunks;
source-done awaits every batch before closing). Sinks without the batch
entry point keep the unchanged per-chunk path.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Jul 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: fa004f3

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/nuxt Patch
@workflow/rollup Patch
@workflow/sveltekit Patch
@workflow/vite 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 Jul 18, 2026

Copy link
Copy Markdown
Contributor

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

Project Deployment Actions Updated (UTC)
example-nextjs-workflow-turbopack Ready Ready Preview, Comment Jul 18, 2026 1:20am
example-nextjs-workflow-webpack Ready Ready Preview, Comment Jul 18, 2026 1:20am
example-workflow Ready Ready Preview, Comment Jul 18, 2026 1:20am
workbench-astro-workflow Ready Ready Preview, Comment Jul 18, 2026 1:20am
workbench-express-workflow Ready Ready Preview, Comment Jul 18, 2026 1:20am
workbench-fastify-workflow Ready Ready Preview, Comment Jul 18, 2026 1:20am
workbench-hono-workflow Ready Ready Preview, Comment Jul 18, 2026 1:20am
workbench-nitro-workflow Ready Ready Preview, Comment Jul 18, 2026 1:20am
workbench-nuxt-workflow Ready Ready Preview, Comment Jul 18, 2026 1:20am
workbench-sveltekit-workflow Ready Ready Preview, Comment Jul 18, 2026 1:20am
workbench-tanstack-start-workflow Ready Ready Preview, Comment Jul 18, 2026 1:20am
workbench-vite-workflow Ready Ready Preview, Comment Jul 18, 2026 1:20am
workflow-docs Ready Ready Preview, Comment, Open in v0 Jul 18, 2026 1:20am
workflow-swc-playground Ready Ready Preview, Comment Jul 18, 2026 1:20am
workflow-tarballs Ready Ready Preview, Comment Jul 18, 2026 1:20am
workflow-web Ready Ready Preview, Comment Jul 18, 2026 1:20am

@github-actions

github-actions Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

🧪 E2E Test Results

All tests passed

Summary

Passed Failed Skipped Total
✅ ▲ Vercel Production 1453 0 230 1683
✅ 💻 Local Development 1617 0 219 1836
✅ 📦 Local Production 1617 0 219 1836
✅ 🐘 Local Postgres 1617 0 219 1836
✅ 🪟 Windows 153 0 0 153
✅ 📋 Other 894 0 177 1071
✅ vercel-multi-region 27 0 0 27
Total 7378 0 1064 8442

Details by Category

✅ ▲ Vercel Production
App Passed Failed Skipped
✅ astro 126 0 27
✅ example 126 0 27
✅ express 126 0 27
✅ fastify 126 0 27
✅ hono 126 0 27
✅ nextjs-turbopack 150 0 3
✅ nextjs-webpack 150 0 3
✅ nitro 126 0 27
✅ nuxt 126 0 27
✅ sveltekit 145 0 8
✅ vite 126 0 27
✅ 💻 Local Development
App Passed Failed Skipped
✅ astro-stable 128 0 25
✅ express-stable 128 0 25
✅ fastify-stable 128 0 25
✅ hono-stable 128 0 25
✅ nextjs-turbopack-canary 134 0 19
✅ nextjs-turbopack-stable 153 0 0
✅ nextjs-webpack-canary 134 0 19
✅ nextjs-webpack-stable 153 0 0
✅ nitro-stable 128 0 25
✅ nuxt-stable 128 0 25
✅ sveltekit-stable 147 0 6
✅ vite-stable 128 0 25
✅ 📦 Local Production
App Passed Failed Skipped
✅ astro-stable 128 0 25
✅ express-stable 128 0 25
✅ fastify-stable 128 0 25
✅ hono-stable 128 0 25
✅ nextjs-turbopack-canary 134 0 19
✅ nextjs-turbopack-stable 153 0 0
✅ nextjs-webpack-canary 134 0 19
✅ nextjs-webpack-stable 153 0 0
✅ nitro-stable 128 0 25
✅ nuxt-stable 128 0 25
✅ sveltekit-stable 147 0 6
✅ vite-stable 128 0 25
✅ 🐘 Local Postgres
App Passed Failed Skipped
✅ astro-stable 128 0 25
✅ express-stable 128 0 25
✅ fastify-stable 128 0 25
✅ hono-stable 128 0 25
✅ nextjs-turbopack-canary 134 0 19
✅ nextjs-turbopack-stable 153 0 0
✅ nextjs-webpack-canary 134 0 19
✅ nextjs-webpack-stable 153 0 0
✅ nitro-stable 128 0 25
✅ nuxt-stable 128 0 25
✅ sveltekit-stable 147 0 6
✅ vite-stable 128 0 25
✅ 🪟 Windows
App Passed Failed Skipped
✅ nextjs-turbopack 153 0 0
✅ 📋 Other
App Passed Failed Skipped
✅ e2e-local-dev-nest-stable 128 0 25
✅ e2e-local-dev-tanstack-start- 128 0 25
✅ e2e-local-postgres-nest-stable 128 0 25
✅ e2e-local-postgres-tanstack-start- 128 0 25
✅ e2e-local-prod-nest-stable 128 0 25
✅ e2e-local-prod-tanstack-start- 128 0 25
✅ e2e-vercel-prod-tanstack-start 126 0 27
✅ vercel-multi-region
App Passed Failed Skipped
✅ nextjs-turbopack 27 0 0

📋 View full workflow run

@github-actions

github-actions Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

📊 Workflow Benchmarks

commit fa004f3 · Sat, 18 Jul 2026 01:32:49 GMT · run logs

Backend: vercel · app: nextjs-turbopack

Metric Scenario Avg (ms) P10 (ms) P75 (ms) P90 (ms) P99 (ms) Samples
TTFS step 972 (-7.6%) 244 1300 🔴 1332 🔴 1726 🔴 30
TTFS stream 991 (-0.9%) 277 1276 🔴 1302 🔴 1324 🔴 30
TTFS hook + stream 1413 (+11%) 536 1508 🔴 1560 🔴 1758 🔴 30
STSO 1020 steps (1-20) 233 (-2.2%) 191 249 🔴 280 🔴 302 🔴 19
STSO 1020 steps (101-120) 268 (+3.9%) 217 285 🔴 340 🔴 349 🔴 19
STSO 1020 steps (1001-1020) 542 (+7.6%) 482 574 🔴 621 🔴 642 🔴 19
WO 1020 steps 415180 (+5.3%) 415180 415180 415180 415180 1
SL stream latency 224 (+48%) 108 337 🔴 389 🔴 578 🔴 30

Avg deltas compare against the most recent benchmark run on main at the time of this run.

Metrics — TTFS: time to first step body (in-deployment start() → first step body, deployment clocks) · STSO: step-to-step overhead (gap between consecutive step bodies) · WO: workflow overhead (whole-run time outside step bodies, in-deployment anchored) · SL: stream latency (in-deployment write → read propagation, readAt - writtenAt)

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 · stream latency: parallel reader/writer steps on a dedicated stream; SL is the in-deployment write->read propagation (readAt - writtenAt)

🟢/🔴 mark percentiles within/above target. Targets (p75/p90/p99, ms) — TTFS 200/300/600 · SL 50/60/125 · STSO (1-20) 20/30/60 · STSO (101-120) 30/45/90 · STSO (1001-1020) 40/60/120

All metrics are measured from deployment-side timestamps only. Runs are triggered by an in-deployment route that stamps the anchor (clientStart) right before start(), so the CI runner’s request and its path through api.vercel.com sit outside every measured window. TTFS = in-deployment start() → first step body (turbo uses the in-process fast path, non-turbo the dispatch path), and includes the VQS dispatch hop plus any /flow cold start. STSO/WO are measured between step bodies on the deployment. SL is measured inside the workflow (parallel reader/writer steps), so it no longer includes the api.vercel.com read path.

Cold starts are kept in the numbers on purpose — they are part of real bursty-workload latency. The workbench deployment cold-starts the /flow invocation for a large fraction of runs, inflating P75+; the P10 column shows the warm-start floor for comparison.

@VaguelySerious VaguelySerious changed the title fix(core): batch stream writes via writeMulti instead of one PUT per chunk fix(core): batch stream writes via writeMulti Jul 18, 2026
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.

1 participant