Skip to content

Accept an idempotency key when arming a fault - #40

Closed
RhysSullivan wants to merge 1 commit into
mainfrom
fault-idempotency-key
Closed

RhysSullivan wants to merge 1 commit into
mainfrom
fault-idempotency-key

Conversation

@RhysSullivan

Copy link
Copy Markdown

POST /_emulate/faults accepts an optional idempotencyKey. A test harness whose arm lost its answer (a reset connection, a time-out) can send it again with the same key without arming a second fault.

  • Same key, same fault: nothing is stored; the answer is the fault first stored for the key, with replayed: true. This holds whether the first copy landed before or after the retry, and for as long as the instance's state lives: through consumption, removal by id, clearing all faults, POST /_emulate/reset and Durable Object eviction (the key records are kept in the persisted faults snapshot). A late copy whose fault is gone gets that fault back with remaining: 0 and arms nothing.
  • Same key, different fault: refused with 409 idempotency_key_conflict.
  • No key: stored as before; the answer has no replayed field.
  • GET /_emulate/faults adds idempotencyKeys: true, so a caller can tell an emulator that honors keys from an older deploy.
  • /__seed still wipes every fault and key with the rest of the instance.
  • No route changes.

The key check and the store happen in one synchronous step in FaultRegistry.armOnce, so concurrent arms with one key cannot both store.

Tests: core/src/__tests__/fault-keys.test.ts (sequence, concurrent, late copies after consumption, removal, clearing and reset, conflicting definition, no key, snapshot and restore) and one Durable Object case in cloudflare/src/__tests__/worker.test.ts (a retry lands, the instance is replaced, the late original arrives, the fault is consumed, the instance is reset). All seven fail on main and pass here. pnpm build, format:check, type-check, lint and test pass locally.

Deploy: merging does not deploy. No workflow deploys the emulate-hosts worker, and there is no Cloudflare build check on main. emulators.dev changes only when someone runs wrangler deploy from packages/@emulators/cloudflare after pnpm build. Until then, emulators.dev keeps today's behaviour: it ignores the key and does not list idempotencyKeys.

An arm may carry idempotencyKey. Another arm with the same key and the same
fault stores nothing and returns the fault first stored for that key, so a
caller that lost an answer can send the arm again. Each key is remembered
with the instance's state, through consumption, removal, clearing, reset and
Durable Object eviction, so a late copy cannot arm its fault again. The same
key with a different fault is refused with 409. Arms without a key behave as
before. The faults list says the emulator honors keys.
@RhysSullivan
RhysSullivan deleted the fault-idempotency-key branch November 18, 2026 23:50
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