The design in `design.md` is sound at its core and you can start building tomorrow, provided you first write down the answers to the first two problems below, which are about an hour of design edits. Both can charge a customer for an order marked failed, or let you sell stock you don't have.

The parts I would not change: reserving stock with a conditional `UPDATE`, keeping the provider call outside any transaction, using the order id as the provider idempotency key, and the reconciliation job for unknown results.

## Fix before building

**1. High: state changes are not guarded against running twice.**
The request handler and the reconciliation job can both settle the same order, and the job runs on both instances.
- **Failing case:** an order for 1 unit is declined. The handler and a reconciliation run (or two reconciliation runs) each mark it `FAILED` and each restore stock.
- **Wrong result:** `available` rises by 2, and you later sell a unit that does not exist.
- **Expected:** it rises by 1.
- **Fix:** make every transition `UPDATE orders SET status = … WHERE id = … AND status = 'PENDING'`, and restore stock or queue email only if that changed a row. Also ensure only one reconciliation run is active at a time, for example with a Postgres advisory lock.

**2. High: reconciliation has no rule for "provider has no record of this charge".**
Step 6 only says orders settle as `PAID` or `FAILED`.
- **Failing case:** the instance crashes after step 2 commits and before step 3 calls the provider. The provider answers "not found".
- **Wrong result:** as written, the order stays `PENDING` and holds its stock until someone acts on the 30-minute alert.
- **Risk in the obvious fix:** if "not found" is treated as `FAILED` after 2 minutes, a charge request still in flight can land afterwards, and the customer is charged for a failed order.
- **Fix:** treat "not found" as `FAILED` only after a wait comfortably longer than the worst-case retry window (about 30 s as designed). If the provider supports it, cancel or void against that idempotency key as well.
- **Also state:** step 6 should say explicitly that a reconciled `FAILED` restores stock, as step 5 does.

**3. Medium: the reconciliation job has no tests.**
The test list covers the request path only, but this job is what decides money and stock in every unclear case. Add tests for:
- a pending order that settles as paid;
- a pending order that settles as declined;
- provider "not found";
- the job and the handler settling the same order at once;
- two concurrent `POST /orders` with the same key;
- two buyers racing for the last unit.

**4. Medium: an order settled as `PAID` by reconciliation never gets a confirmation email.**
Only step 4 queues the email.
- **Failing case:** the charge succeeds, the database write for `PAID` fails, and reconciliation later marks it `PAID`.
- **Wrong result:** the customer is charged and gets no email.
- **Fix:** queue the email from one shared "mark paid" function used by both paths.

**5. Medium: the design does not say where prices come from.**
There is no price table, and step 1 takes "the cart" from the client. If `unit_price` or `total` comes from the request, a customer can set their own price. State that the server looks prices up and computes `total`.

## Fix during the build

**6. Medium-low: the idempotency key is unique globally, not per customer.**
- **Failing case:** customer B sends a key customer A already used.
- **Wrong result:** B gets A's order back.
- **Fix:** make the constraint unique on `(customer_id, idempotency_key)`. Also decide what happens when the same key arrives with a different cart; I'd return `422`.

**7. Low: concurrent duplicate `POST /orders` is unspecified.**
The second insert hits the unique constraint rather than finding an existing row. Catch that violation, roll back the stock reservation, and return the existing order instead of a `500`.

**8. Low: multi-item orders can deadlock.**
- **Failing case:** order 1 updates stock for SKU A then B while order 2 updates B then A.
- **Wrong result:** Postgres aborts one, and the customer sees a `500`.
- **Fix:** update stock rows in sorted SKU order.

**9. Low: a crash between committing `PAID` and queueing the email loses the email.**
This is rare at your volume. Fix it by writing the email job in the same transaction as the status change, or accept it and say so in the design.

**10. Low: the request can run about 27 s in the worst case.**
That is four 5 s attempts plus backoff. Check that the load balancer and client timeouts are longer, or the client sees a gateway error instead of the `202`.

**11. Low: the data model lists no keys or constraints.**
Add:
- primary keys on `orders.id`, `stock.sku` and `order_items (order_id, sku)`;
- a foreign key from `order_items` to `orders`;
- `CHECK (available >= 0)` and `CHECK (quantity > 0)`;
- an index on `(status, created_at)` for the reconciliation query.

**12. Low: the module rule and the single transaction need one sentence reconciling them.**
Steps 2 and 5 change `stock` and `orders` in one transaction, while the code structure says no module touches another's tables. State that `OrderCheckout` owns the transaction and passes it to the inventory and orders modules.

VERDICT: READY WITH CHANGES
