Skip to content

[workflows-shared] Emulate deterministic-ID uniqueness in the local Workflows binding - #14847

Open
TheSaiEaranti wants to merge 2 commits into
cloudflare:mainfrom
TheSaiEaranti:fix-workflows-duplicate-ids
Open

[workflows-shared] Emulate deterministic-ID uniqueness in the local Workflows binding#14847
TheSaiEaranti wants to merge 2 commits into
cloudflare:mainfrom
TheSaiEaranti:fix-workflows-duplicate-ids

Conversation

@TheSaiEaranti

@TheSaiEaranti TheSaiEaranti commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #14836.

The local Workflows binding did not emulate the documented deterministic-ID uniqueness contract, in the dangerous direction: code relying on "duplicate create with the same ID is safely deduplicated" appeared to work locally while double-executing workflow bodies.

This PR matches the documented production behavior:

  • create({ id }) with an ID that already exists throws (Workflow instance with id "<id>" already exists) and the existing instance is retained.
  • createBatch() skips IDs that already exist — or that repeat within the batch — excluding them from the result, and creates instances in batch order.
  • Auto-generated (crypto.randomUUID()) IDs skip the existence check; their behavior is unchanged.

How existence is decided: a new Engine.hasInstance() RPC reads INSTANCE_METADATA from Durable Object storage, which is written exactly once by the first init() for the ID. Consulting the engine DO keeps the answer consistent across isolates rather than relying on binding-local state, and the DO's input gates order a sequential duplicate create() after the first create()'s init() dispatch, so the sequential case in the issue's repro is deterministic. init() itself is untouched — it is intentionally re-entrant (the engine calls it on restart/wake paths).

One boundary note: two create() calls for the same ID racing in the same tick (Promise.all) remain best-effort, since fully closing that window would require moving creation into a single engine RPC and changing init()'s contract. The issue's repro (sequential awaited creates) is fully handled.

Tests: four new cases in binding.test.ts — duplicate create() throws; createBatch() excludes an existing ID (returns [], and [fresh] for a mixed batch); within-batch duplicates collapse to one; auto-generated IDs unaffected. Full workflows-shared suite: 153 passed. tsc clean, oxfmt applied.


  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: this aligns local emulation with behavior that is already documented for production.

Open in Devin Review

…orkflows binding

The local binding created duplicate executions for deterministic instance ids:
create() never checked for an existing instance and createBatch() mapped every
input through create(), so code relying on the documented idempotency contract
appeared to work locally while double-executing workflow bodies.

Match the documented production behavior: create() with an id that already
exists throws and retains the existing instance, and createBatch() skips ids
that already exist, or repeat within the batch, excluding them from the result
and creating instances in batch order. Existence is decided by the engine
Durable Object via a new hasInstance() method that reads INSTANCE_METADATA,
which is written exactly once by the first init(), so the check is consistent
across isolates rather than relying on binding-local state. Auto-generated ids
skip the check.

Fixes cloudflare#14836
@changeset-bot

changeset-bot Bot commented Jul 25, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 30229c0

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

This PR includes changesets to release 8 packages
Name Type
miniflare Patch
wrangler Patch
@cloudflare/deploy-helpers Patch
@cloudflare/pages-shared Patch
@cloudflare/remote-bindings Patch
@cloudflare/runtime-types Patch
@cloudflare/vite-plugin Patch
@cloudflare/vitest-pool-workers 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

@workers-devprod
workers-devprod requested review from a team and jamesopstad and removed request for a team July 25, 2026 23:30
@workers-devprod

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/workflows
  • @cloudflare/wrangler
Show detailed file reviewers
  • .changeset/fix-workflows-duplicate-ids.md: [@cloudflare/wrangler]
  • packages/workflows-shared/src/binding.ts: [@cloudflare/workflows @cloudflare/wrangler]
  • packages/workflows-shared/src/engine.ts: [@cloudflare/workflows @cloudflare/wrangler]
  • packages/workflows-shared/tests/binding.test.ts: [@cloudflare/workflows @cloudflare/wrangler]

devin-ai-integration[bot]

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants