AC-047 Scenario not caught

A batch operation is not atomic

State, retries and concurrency

How to run this card's commands yourself

1. The problem

In one sentence

A batch request contains one invalid item, and the server does not reject the batch: it creates the valid items, skips the bad one and answers 201.

A story

A client sends three items in one request: Widget, an item with an empty name, and Gadget. The spec implies all or nothing: if any item is invalid, the answer is 422 and nothing is created. The server creates Widget and Gadget, skips the empty one, and answers 201 Created. The client sees success and goes on. One item is missing, and nobody knows which, or why.

Why it hurts

A batch is a promise of one unit of work. A partial result means the client cannot tell what happened. Retrying the whole batch then creates duplicates of the items that did succeed.

  • For developers: per-item results must be reported, or the batch must roll back. Silently skipping an item is the worst of both choices.
  • For managers: imports and bulk updates leave data half done. Reconciliation after the fact is manual, slow and often wrong.
  • For DevOps: a failed batch must leave the system as it was, or must say exactly which items were applied. Test with a batch that contains a bad item.

Example

In a failure-catalog demo:

POST /items/batch {"items":[{"name":"Widget"},{"name":""},{"name":"Gadget"}]}
    ->  201 Created        (should be 422, and nothing created)

The server created the two valid items and skipped the invalid one.

How to detect it without clrsig

  • Send a batch with one deliberately invalid item, then list the records. With all-or-nothing behaviour there are none.
  • Read the code for a loop that creates items one by one with no transaction around it.

How to fix it

There are two usual causes:

  1. The code is wrong (more likely). Validate every item first. Then create them all in one transaction, so that one failure undoes all.
  2. The spec is silent about the semantics. Say whether a batch is all-or-nothing or best-effort. For best-effort, return a result per item (a 207 Multi-Status-style body), so the client knows what happened.

To tell which: ask what the client should do after a failed batch. If the answer is “send it again”, the batch must be atomic or safe to repeat.

AC-040 A business rule is broken though every response looks fine, AC-042 A repeated write creates duplicates, AC-028 A request without a required field is accepted. See also the site’s Distributed Transaction and Partial-Workflow Failures.

Words used on this page are explained once in the glossary.

2. How clrsig helps

Check

No built-in check, and the state is not caught. The answer (201 with a valid body) can match the spec. A scenario that sends the bad batch and asserts both the status and the stored count finds it.

Command

This demo is a runnable server in the failure catalog. The scenario sends a POST to its own local toy server, so it writes data there:

Safety. Run this against a test server, never production. clrsig conform sends POST, PUT and DELETE requests built from the spec: start with --read-only, and take secrets from the environment (${NAME}), never from files in the repository.

cd failures/fm-020-01-batch-atomicity
go run broken/main.go &
clrsig run scenarios/detect.yaml --env environments/local.yaml

Exit code: 0 all steps pass, 1 a step failed, 2 the scenario or the setup is broken. Here it is 1. Against fixed/main.go both steps pass and the exit code is 0.

What you see

Output from clrsig v0.21.0, recorded on 2026-11-30:

Scenario: fm-020-01 — Batch Operation Atomicity

  FAIL  batch with one invalid item (empty name) must be rejected with 422  201  14ms
         ✗ status eq 422: expected "422"; got "201"
  FAIL  after rejected batch — no items must have been created (count = 0)  200  0ms
         ✗ jsonpath $.count eq 0: expected "0"; got "2"

FAILED  0 passed, 2 failed, 0 skipped  (0/2 assertions passed)  14ms

How to read it: the batch with a bad item was accepted (201) when it should have been refused (422). The second step then shows the damage: the count is 2, not 0. The valid items were created, and only the invalid one was skipped.

Where to look

  • Desktop app: open the scenario and run it; each step shows its assertion result.
  • Reports: --format json and junit on clrsig run.
  • GitHub Action: use command: run with the scenario and env inputs. The demo checks with its own assertions. To compare your own responses with your spec, name it with specFile in the scenario, or give the spec input, which replaces it; older releases of the Action ignored that input with run (setup and upgrading).

How clrsig helps you fix it

It shows that a bad batch was accepted, and the second step shows the stored count of 2, which proves the partial result. It cannot say why the server skipped only the invalid item.

Limits

  • The scenario shows one bad item. Test the bad item in first, middle and last position as well.

More ways

Nothing yet.

3. Video brief for Google NotebookLM

VIDEO BRIEF — AC-047 · A batch operation is not atomic
Length: 5 to 7 minutes. Language: English. Audience: developers, with a line each for managers and DevOps.
One idea: a batch is a promise of one unit of work; a partial result leaves the client unable to tell what happened.
Open with this story: A client sends three items in one request: a widget, an item with an empty name, and a gadget. The spec implies all or nothing: if any item is invalid, the answer is 422 and nothing is created. The server creates the widget and the gadget, skips the empty one, and answers 201 Created. The client sees success and goes on. One item is missing, and nobody knows which, or why.
Say these facts, in this order:
1. The scenario sends a batch with one invalid item and expects 422.
2. The broken server answers 201; the step fails: expected 422, got 201.
3. The second step checks that nothing was created: it finds a count of 2, so the valid items were created and only the invalid one was skipped.
4. Retrying the whole batch would create duplicates of the items that did succeed.
5. Two usual causes: the code is wrong (validate every item first, then create all in one transaction), or the spec is silent about the semantics; for best-effort batches, return a result per item.
6. For managers: imports and bulk updates leave data half done, and the fix is manual.
7. For DevOps: a failed batch must leave the system as it was, or must say exactly which items were applied.
Show on screen: the example request and its answer, the commands (go run broken/main.go and clrsig run scenarios/detect.yaml --env environments/local.yaml), and the output block, copied exactly from the card.
Name the check: none built in; found by a scenario that sends a bad batch and checks the status and the stored count. Say the clrsig version: v0.21.0.
Explain these words once: batch (many items sent in one request); atomic (all of it happens, or none of it); transaction (a group of changes that succeed or fail together); exit code (0 clean, 1 a finding, 2 setup or spec broken).
Say what clrsig does not see here: the scenario shows one bad item, not every position.
Safety line (if a write is shown): Run this against a test server, never production. clrsig conform sends POST, PUT and DELETE requests built from the spec: start with --read-only, and take secrets from the environment (${NAME}), never from files in the repository.
Finish with three recap questions:
1. What does atomic mean for a batch? (All of it happens, or none.)
2. Which status should a batch with a bad item answer in the all-or-nothing design? (422.)
3. What does a best-effort batch need to return? (A result per item.)
Tease the next video: AC-048 · The server is slow.
Do not: say "contract testing" alone; say clrsig "catches everything"; invent flags, IDs or numbers; read tables aloud; use any fact that is not in the card.