The design's structure holds up, but four correctness problems need fixing in the doc before you write code. Each fix adds a rule to the existing flow, so nothing has to be redesigned and you can still start tomorrow.

## What's already right
- Reserving stock with a conditional `UPDATE ... WHERE available >= :qty` is atomic, so the last unit can't be sold twice.
- No transaction stays open during the payment call.
- The order id is sent as the provider's idempotency key, so retries can't charge twice.
- An unknown payment result stays `PENDING` and gets settled by the reconciliation job, with an alert as a backstop.
- The email is sent after commit and never fails the order.

## Problems, by severity

### High: fix in the doc before building

**1. Two code paths can settle the same order, so stock gets restored twice.**
Two code paths can settle an order: the request (steps 4–5) and the reconciliation job (step 6). The job also runs on both instances. Nothing says a status change only happens while the order is still `PENDING`.
- *Case:* order X holds 1 unit, and stock is 0. Both instances' jobs pick up X, both get "declined" from the provider, and both run "mark FAILED and restore stock".
- *Wrong:* stock is 2, so you now sell a unit you don't have. *Expected:* stock is 1.
- *Fix:* every settlement is `UPDATE orders SET status = :new WHERE id = :id AND status = 'PENDING'`. Restore stock only if that changed one row, in the same transaction. Also make the job claim rows with `FOR UPDATE SKIP LOCKED` (or an advisory lock) so the two instances don't both work the same order.

**2. Reconciliation doesn't cover "the provider has no charge", and its FAILED path doesn't restore stock.**
- *Case:* an instance crashes after step 2 commits but before step 3 sends the charge.
- *Wrong:* the job asks the provider, gets "no such charge", and the design has no rule for that. The order stays `PENDING` forever and its stock stays reserved.
- *Expected:* a defined outcome, either "no charge → FAILED and restore stock" or "re-send the charge with the same idempotency key".
- *Fix:* also state that the job's FAILED path restores stock, and that "still processing" means leave the order alone.

**3. A negative quantity increases stock.**
- *Case:* the cart has `quantity: -5`. The check `available >= -5` passes, so `available - (-5)` adds 5 units.
- *Wrong:* stock goes up and the order total goes negative. *Expected:* `400`.
- *Fix:* validate `quantity > 0`, and merge duplicate SKU lines. Add `CHECK (available >= 0)` and `CHECK (quantity > 0)` as database backstops.

**4. The doc doesn't say where prices come from.**
The client sends the cart, and `order_items.unit_price` and `orders.total` are stored, but there's no price source in the data model. If the price comes from the client, anyone can set it to $0.01.
- *Fix:* state that the server looks up prices from the catalog and computes `total`. Say which module owns those prices.

### Medium: decide before or during the first sprint

**5. Two concurrent requests with the same idempotency key.** Both insert, and one hits the unique constraint. The doc should say that this error is caught and the request returns the existing order. It should also say what happens when a key is reused with a different cart (usually `422`).

**6. The idempotency key is unique across all customers.** If customer B sends a key that customer A already used, B gets A's order back. Make the constraint `UNIQUE (customer_id, idempotency_key)`.

**7. The client has no way to learn the result after a `202`.** No `GET /orders/{id}` endpoint is defined. Also specify the success and decline responses (for example `201` and `402`).

**8. Each request can be held open for up to about 27 seconds.** That's 4 attempts × 5 s, plus backoff. Check it against the load balancer and client timeouts. Also say which errors are retried: timeouts, 5xx and network errors only, never declines.

**9. The confirmation email can be lost.** If the process dies after the `PAID` commit but before the email is queued, no email goes out. If the queue lives outside Postgres, write the email job in the same transaction as `PAID` (an "outbox" table that a worker reads). The doc should also say whether orders the reconciliation job marks `PAID` get an email.

**10. The tests miss the riskiest paths.** Add tests for:
- every reconciliation outcome: paid, declined, no charge, still processing
- two settlements racing (problem 1)
- a crash between commit and charge
- truly concurrent buys of the last unit, not two requests one after the other
- concurrent duplicate keys
- negative or zero quantity
- client-supplied prices being ignored

### Low: tidy up in the doc
- **11. Transactions cross module boundaries.** Steps 2 and 5 change `orders` and `stock` in one transaction, which seems to contradict "no module reads another module's tables". State that the inventory module exposes `reserve` and `release` functions that take the caller's transaction.
- **12. Deadlocks between multi-item orders.** Two orders with the same SKUs, updated in different orders, can deadlock. Always update SKUs in sorted order.
- **13. Keys are missing from the data model.** Add primary and foreign keys: `stock.sku` as the primary key, `order_items (order_id, sku)` as its key, and `order_items.order_id` referencing `orders`. Make the `Idempotency-Key` header required (`400` if missing).

## Order lifecycle with the fixes applied

```mermaid
stateDiagram-v2
    [*] --> PENDING: stock reserved + order inserted (one transaction)
    PENDING --> PAID: charge succeeded (request or reconciliation job)
    PENDING --> FAILED: declined, or provider has no charge; stock restored in same transaction
    PAID --> [*]
    FAILED --> [*]
```

Every arrow out of `PENDING` only happens if the order is still `PENDING` (problem 1). That rule is what lets the request path and the reconciliation job run side by side safely.

Fixing problems 1–4 in the doc should take about an hour. They change what gets coded, not how it's structured.

VERDICT: READY WITH CHANGES
