Approve the two slices Codex proposed, let it build Slice 1 (one database transaction and one row lock per auction), learn in a side chat how that lock really works, then rerun the bidding war and watch the duplicate bids disappear.
GRILL.md in Part 3 holds your decisions. Keep that chat open; we continue in it, because Codex needs that context to build what you agreed on.In guide 12 you saw the same amount, $107, accepted three times on one auction. Here is why. When a bid arrives, the server does two things: it checks (“is $107 higher than the current price?”) and then it acts (“insert the bid”). If three requests arrive at the same moment, all three read the old price, all three pass the check, and all three insert. Nobody did anything wrong; they simply never saw each other. This is called a check-then-act race condition, and it is one of the most common bugs in any backend that many people use at once. The fix is not “check more carefully”. The fix is to make the bids for one auction take turns.
Scroll up a little in the grilling chat, to the end of Codex's last message. You will find a small Mermaid diagram (Mermaid is a way of drawing diagrams from plain text; Codex writes the text, the app renders the picture). Click the diagram once and it opens full size. There is a zoom control at the bottom of that view. When you are done, close it with the ✕ at the top-right.
Read the diagram from left to right. It is the whole fix in one picture:
201 Created means “bid accepted and stored”. 409 Conflict means “rejected because someone else got there first”. “Typed” means the 409 carries a structured body with a machine-readable code such as BID_TOO_LOW or AUCTION_CLOSED, so the frontend can react properly instead of showing a vague error.Below the diagram Codex lists two slices. A slice is a small, complete piece of work that you can test in the browser on its own. Slice 1 fixes the race (bids take turns, and a losing bidder gets a clean recovery on their page). Slice 2 makes the closing time authoritative (the server decides when an auction is over, using the app clock) and switches open pages to “Ended” without constantly asking the server. We build only Slice 1 in this guide.
Code that needs the current time usually calls Date.now() directly. That is a problem in tests: you cannot make “now” be 11:59:59 to test what happens one second before an auction closes. So instead we build one small clock object and hand it (“inject” it) into the API when the app starts. Real runs get a clock that reads the system time; tests get a fake clock they can set to any moment. The code that checks “has this auction ended?” never knows the difference. “Injected” just means “passed in from outside instead of created inside”.
A diagram similar to: React/Vite Web App → “Submit bid / refresh auction” → Fastify API → “Transaction + auction row lock” → PostgreSQL, with the API reading the Injected App Clock, and “201 or typed 409” going back to the browser.
Below it, two slices:
Your wording may differ slightly. The two ideas should be the same.
If the slices match what you decided in grilling, tell Codex to start. The message can be this short because all the detail already exists: your answers are in GRILL.md and in this chat's context. There is nothing to add. The root AGENTS.md of the workshop tells Codex to build one slice at a time and stop after each one, so “get started” means “start Slice 1”, not “build everything”.
looks good to me, get started!
Why this matters: one slice at a time keeps every change small enough for you to read. If Codex built both slices in one go, you would get a huge diff and no idea which part caused a problem.
Similar to: “I'm starting Slice 1 only: serialized bid acceptance plus stale-conflict recovery. I'll keep the clock/closing transition work for Slice 2…” A progress line like Step 1 / 4 appears, and a file counter starts growing, for example “3 files changed +147 -47”. The two numbers are lines added and lines removed, so you can watch how big the change is getting.
Building a slice runs for several minutes. Codex writes the code, runs the backend and frontend tests, runs a typecheck (the TypeScript compiler checks that every value has the type the code claims, without running the app) and runs the headed bidding war in Edge. All of that is normal, but with an API key it is not cheap. Do not interrupt it; use the waiting time for Step 3.
While Slice 1 builds, we learn. Click the right-panel button at the top-right of the Codex window (the last icon in the title bar). A menu opens with Review, Terminal, Browser, Files and Side chat. Click Side chat, or press ⌥⌘S. A side chat is a temporary second conversation in a panel on the right. It can read your project but it does not interrupt the main run on the left, and it disappears when you close the app. If an old side chat is still open from guide 13, close it first so this one starts with a clean context.
Now ask the question. I write it deliberately like a complete beginner. There is no prize for sounding smart to an AI; the simpler you ask, the simpler the answer you get, and a simple answer is what you need the first time you meet a concept. Paste it exactly as it is.
hey man. I'ma be honest. I thought postgres was a disease of some kind. what is database transaction locking and why does it work here?
A transaction is a group of database operations that either all happen or none happen. You open it with BEGIN, do your reads and writes, and finish with COMMIT (save everything) or ROLLBACK (throw everything away as if nothing happened). Think of a bank transfer: debit one account, credit another. You never want only the debit to survive a crash. For a bid, the transaction wraps “read the current price, check the amount, insert the bid” so the whole thing is one unit of work.
A database table is like a spreadsheet; each auction is one row. SELECT … FROM auctions WHERE slug = $1 FOR UPDATE reads that one row and locks it until the transaction ends. If a second request runs the same statement for the same auction, PostgreSQL makes it wait at that line until the first transaction commits or rolls back. Then the second one gets the row, reads the new price, and does its own check. This is why the lock serialises bids: bids for one auction now happen strictly one after another, like people taking turns with a single talking stick. Bids on a different auction lock a different row, so they are not slowed down at all. The lock is held only for a few milliseconds, so users never notice the waiting.
Similar to: “Totally fair. PostgreSQL (‘Postgres’) is the database—the program that stores auctions and bids.” Then a worked example: Maya bids $103 and Theo bids $106 at the same time, and both read $102 as the current price. With locking the backend does:BEGIN → lock this auction's row → read the latest bid → check closing time → check the amount → insert if valid → COMMIT.
The lock is described as “the only talking stick for that specific auction”. You will probably also get a small Mermaid diagram (Auction A lock / Auction B lock) showing that different auctions do not block each other.
If your Codex version has no Side chat, start a separate New chat in the workshop project and ask there. That chat does not know what the main chat is doing, so add “in part 3's server/src/app.ts” to the question so it looks at the right file.
The story with Maya and Theo is nice, but I want you to see the real code that does it. Two reasons. First, you should never accept code you cannot point to and explain. Second, asking for “something to google” gives you the official vocabulary. Once you know the term is “SELECT FOR UPDATE”, you can read the PostgreSQL documentation, blog posts and interview questions about it. Without the words, you cannot even search. Send this in the same side chat.
show me the exact lines of code and gimme something to google
When the answer arrives, match each quoted line to the picture from Step 3: open a connection, BEGIN, lock the row, re-read, insert, COMMIT. The ROLLBACK in the catch block is what runs if anything throws an error halfway, so a half-finished bid is never saved. The client.release() in finally gives the database connection back to the pool whether the bid succeeded or failed. A pool is a small set of ready-made connections the server reuses, because opening a fresh connection for every request would be slow.
The isolation level is a database setting that decides how much one running transaction can see of other transactions that have not finished yet. PostgreSQL's default is READ COMMITTED: you only ever see data that another transaction has already committed. That is good, but on its own it does not stop our race, because both bidders can read the same committed price before either of them writes. Stricter levels exist (REPEATABLE READ, SERIALIZABLE) that make the database detect such clashes and fail one of the transactions, which you then have to retry. We chose the explicit row lock instead because it is simple to read and needs no retry logic. Isolation levels come up again in guide 15.
Codex quotes the real lines from server/src/app.ts (your line numbers will differ), similar to:
const client = await pool.connect(); and await client.query('BEGIN');: reserve one connection and start the transactionSELECT … FROM auctions WHERE slug = $1 FOR UPDATE: lock this auction row until the transaction endsawait client.query('COMMIT');: save the bid and release the lockROLLBACK in the catch and client.release() in finallyThen a list titled Good things to Google: PostgreSQL SELECT FOR UPDATE, row-level locks, BEGIN COMMIT ROLLBACK, READ COMMITTED isolation, MVCC, check-then-act race condition, TOCTOU race condition, ACID transactions, critical section.
Codex may suggest a follow-up question such as “Explain PostgreSQL SELECT FOR UPDATE, MVCC, and READ COMMITTED using our auction bidding code. Show what each concurrent request sees step by step.” Send it if you have time; it is a good one. MVCC (multi-version concurrency control) is how PostgreSQL lets readers and writers work at the same time by keeping more than one version of a row. TOCTOU (“time of check to time of use”) is just the formal name for check-then-act.
Switch back to the main chat on the left. The side chat can stay open. Wait until Codex finishes Slice 1. Part of its own checking is to run the headed bidding war, so four Edge windows may suddenly pop up and tile themselves on your screen while you are reading in the side chat. Do not close them while Codex is working; they close by themselves when the test finishes.
When it is done, Codex writes a handoff: a summary of what changed, which tests passed, and a short list of steps you can try in the browser to see the change yourself. Then it stops and waits for you before touching Slice 2. Read the handoff slowly. The sentence about “deliberately slows inserts” matters: the concurrency test adds an artificial delay inside the transaction so that several bids are guaranteed to overlap. Without that delay the test might pass by luck, with the bids simply arriving one after another.
Similar to: “The automated suite passes: 7 backend tests and 6 frontend tests. The concurrency test deliberately slows inserts…” and “The headed four-window Edge test passed end to end. Under deliberately delayed inserts, accepted amounts were exactly $101, $102, $106, $107: no duplicate equal bids, $106 won the mixed stale wave, and the rejected $102 bidder kept their input while the UI refreshed to a $103 minimum.”
Then it stops, lists what changed with browser steps for you to try, and waits before starting Slice 2. “Strictly increasing amounts” is the one-line proof that the lock works.
Codex says the test passed. I believe it, and I still want to see it with my own eyes. This is a habit I want you to build: the agent's report is a claim, the running app is the evidence. Ask for the bidding war again in the main chat.
show me bidding wars again
Codex runs the same Playwright test you built in guide 12, but with the windows held open so you can look. Watch the four windows: three bidders and one seller. The labels at the top of each window are written by the test itself to tell you what it is checking at that moment.
An environment variable is a named setting you pass to a program from the outside, without changing its code. BIDDING_WAR_HOLD_MS=600000 npm run test:bidding-war sets the variable BIDDING_WAR_HOLD_MS to 600000 (milliseconds, so 10 minutes) just for this one command. The test reads it and keeps the browser windows open for that long instead of closing them immediately. The same test, run without the variable, closes everything the moment it finishes. This is the usual way to configure ports, database names and behaviour like this in backend projects.
Codex runs BIDDING_WAR_HOLD_MS=600000 npm run test:bidding-war and tiles four Edge windows on the Part 3 app (in my run localhost:5103/items/bidding-war-gpu-…; Codex chose your port in guide 13, so yours may differ). The labels show PROTECTED: $106 leads; stale lower bids rejected, then ENDED, with “$108 rejected: This auction has ended” on the bidders and “PROTECTED — accepted bid history is strictly increasing” on the seller.
Scroll the Bid history in any window: every amount is different, for example $101, $102, $104, $106, $107. In guide 12 the same test showed $107 three times. Same test, same four bidders, different result: that difference is the row lock.
The windows stay open for 10 minutes (600000 ms). Close them yourself when you are done looking; Codex's chat will report the test as finished either way.
Codex's handoff lists browser steps like the ones below. In class I usually skip them to save time, but if you have five minutes, do them: being the losing bidder yourself makes “stale-bid recovery” concrete in a way no test output can. If the Part 3 app is not already running, start it first. Run this in the Terminal app, or in Codex's bottom-panel terminal.
cd ~/codex-course/workshop/part-3-business-rules-concurrency && npm install && npm start
What the command does: cd … moves into the Part 3 folder; npm install makes sure every package the project needs is present (it is safe to run again, it finishes in seconds if nothing is missing); npm start starts the Part 3 API and web app. The terminal keeps printing logs while the app runs. Leave it open; when you are finished, press Ctrl+C in that terminal to stop the app.
http://localhost:5103) in two Chrome windows side by side.The second window is now a stale page: it still shows the old price, and the bid it sends is based on information that is already out of date. This is exactly what happens to a real user who leaves an auction tab open for a minute.
The first bid is accepted: “Bid accepted — you are the highest bidder.” The second is rejected with a message similar to “another bidder moved the minimum to $X”. Its page refreshes the current price and the minimum bid but keeps the amount you typed in the box. Nothing is resubmitted automatically. That is the behaviour you asked for in grilling: refresh the facts, keep the user's input, never auto-retry a bid on someone's behalf.
If the bidding war does not open any windows, check that Microsoft Edge is installed (guide 1, Step 2); the test is written for Edge. If the side chat's explanation is too technical, reply “explain it again, I have never used a database”; Codex adjusts its level without complaint.
If Slice 1 fails outright, or your app behaves very differently from what this guide describes, replace your Part 3 with the finished reference version. The command below deletes your Part 3 folder (this cannot be undone), copies the reference Part 3 into your workshop, and saves a Git commit so the catch-up is a clean snapshot. Note that the reference folder is the end state of the whole part: Slice 1 and Slice 2 (guide 15) are both already built. So after catching up, skip the build steps of guide 15 and go straight to its commit step.
cd ~/codex-course/workshop && rm -rf part-3-business-rules-concurrency && cp -R ../reference/part-3-business-rules-concurrency ./ && git add -A && git commit -m "Catch up: part 3 from reference"
BEGIN … SELECT … FOR UPDATE … COMMIT) makes bids for one auction take turns. Each bid re-reads the latest price after it gets the lock, so its check is always against fresh data.