The design is close to ready. The core is sound: stock is reserved atomically, the payment call happens outside any database transaction, retries are idempotent, and there's a reconciliation job with an alert behind it. You can start building tomorrow, but fix the three gaps below before you write the reconciliation job. All three are in what happens when two code paths try to settle the same order.

## What has to change

**1. Settling an order isn't guarded against running twice.** Steps 5 and 6 say "mark the order FAILED and restore the stock" but never say "only if it's still PENDING". You have two instances, so if the reconciliation job runs on both (or overlaps with a slow request) the same order can be settled twice.
- *Concrete case:* order A holds 1 unit of SKU X, and stock is 0. Both instances' reconciliation jobs pick up A, both get "declined" from the provider, and both run FAILED + restore. Stock becomes **2** when it should be **1**, and you've oversold.
- *Fix:* `UPDATE orders SET status = :new WHERE id = :id AND status = 'PENDING'`, and restore stock (or queue the email) only if that update changed exactly one row. Write it into the design as the rule for every status change.

**2. Reconciliation doesn't say it restores stock or sends the email.** Step 5 restores stock on decline and step 4 queues the email on success, but both are only described for the request path. Step 6 just says reconciliation "settles them as PAID or FAILED".
- *Concrete case:* a charge times out, and 5 minutes later reconciliation learns it was declined and marks it FAILED. Stock stays decremented forever. Learning it was paid instead means the customer never gets a confirmation.
- *Fix:* make "settle as PAID" and "settle as FAILED" one function each in `OrderCheckout`, used by both the request path and the job.

**3. "Provider has no record of this charge" isn't handled.** If an instance crashes after step 2 commits but before step 3 sends the charge, the order is PENDING and the provider has never heard of it. The design only covers PAID and FAILED answers.
- *Concrete case:* the instance dies right after commit. Reconciliation asks about order B, the provider returns "not found", and B sits PENDING until the 30-minute alert pages someone, with stock held the whole time.
- *Fix:* decide the rule. My recommendation: if the provider has no record and the order is older than 2 minutes, settle it as FAILED and restore stock. 2 minutes is safely longer than the longest request (4 attempts × 5 s plus backoff ≈ 25–30 s). Don't charge from the job.

Here's the order lifecycle with those rules applied:

```mermaid
stateDiagram-v2
    [*] --> PENDING: POST /orders (stock reserved)
    PENDING --> PAID: charge succeeded (request or reconciliation)
    PENDING --> FAILED: declined, or provider has no record after 2 min
    PAID --> [*]: queue confirmation email
    FAILED --> [*]: restore stock
```

Each arrow out of PENDING is taken only if the conditional update changes one row. That guard is what makes double-settling harmless.

## Open question to answer before step 1
**Where does `unit_price` come from?** There's no products or prices table, and the flow says the client sends "the cart". If the price comes from the client, anyone can buy at a price they pick. If prices come from another service, say which one and when it's read.

## Smaller items (fix while building)
- **Scope the idempotency key per customer.** Use a unique constraint on `(customer_id, idempotency_key)`, not on the key alone. Otherwise one customer reusing another customer's key gets that customer's order back. Also reject a reused key that comes with a different cart (`422`).
- **Lock stock rows in SKU order.** Reserve SKUs sorted by `sku`. Without that, two concurrent orders for {X, Y} and {Y, X} can deadlock, and Postgres will abort one of them.
- **Add `CHECK (available >= 0)` on `stock`** as a backstop, and state the primary keys (`stock.sku`, `order_items (order_id, sku)`).
- **The transaction in step 2 crosses modules.** It touches inventory's table and orders' table at once. That's fine, but say how: for example, inventory exposes `reserve(tx, items)`. Otherwise "no module reads another module's tables" gets broken quietly.
- **Emails can be lost.** If the instance dies between the PAID commit and queueing the email, that email never goes out. At 200–2,000 orders a day that's an acceptable known risk. If it's not, write the email job to an outbox table in the same transaction. Either way, write the choice down.
- **Requests can take ~30 s.** With retries, a request can be held open about that long. Check that the load balancer's idle timeout is longer, or the client will see a `504` while the charge is still in progress.

## Tests to add
The list covers the request path well but none of the reconciliation job, which is where the gaps above are. Add tests for:
- reconciliation settles PAID and queues the email
- reconciliation settles FAILED and restores stock
- provider has no record of the charge
- two reconciliation runs on the same order restore stock only once
- two concurrent requests for the last unit: exactly one succeeds and the other gets `409`
- a decline restores stock (check the actual `available` value, not just the status)

**What to do next:** add the "only if still PENDING" rule, the shared settle functions, and the no-record rule to design.md, and answer the price question. Then you can start with steps 1–2 tomorrow.

VERDICT: READY WITH CHANGES
