[PR #233·235·236·240·243·250] Building the Server Requests Screens Call: Locking Concurrent Requests and Overlapping Codex Runs
These six PRs are server tasks 3 to 8 of part 3. They built, one after another, the server requests (the addresses a screen calls to tell the server “save this” or “show me this”) that admin web and customer app screens use. Some of those screens had been built first against fake responses, and this time the real server filled in.
Everything ran on my Windows computer. A Claude Code session (Claude Opus 5.5) did the implementation, and Codex (gpt-6.1-terra) wrote and fixed the tests and did the final code review (codex exec review). A separate coordinating session (Claude Opus) set the order of work and some of the rules. Codex wrote the tests and the implementation side did not edit the test files (track A).
What was built
| PR | Server requests built |
|---|---|
| 233 | Admin web: calculation standard versions (create draft, edit, approve), input material types (add, rename, deactivate), cycle start value, count adjustment |
| 235 | Customer app: daily plan (created the first time it is viewed that day), recalculation after records change, customer plan changes and accepting a new proposal |
| 236 | Customer app: input records, post-input checks, material preparation records |
| 240 | Customer app: count change records (decrease, sent out, moved) |
| 243 | Customer app: device input and refill records; admin web: device settings |
| 250 | Admin web: plans and work records in one list, notes on corrected records |
Response shapes (types) live in a shared package so the server and the screens use the same definitions.
Processing only one of two concurrent requests
When the same request arrives twice at almost the same moment, both see “nothing here yet” and try to save, so one fails or two rows appear. So wherever requests can overlap, a lock (a device that makes the next request wait until the first one finishes) was added.
- Approvals one at a time (PR 233). A database unique index (a device that stops the same value from appearing in two rows) enforces “only one approved version”. Two approvals at once could hit that index and return a 500 error, so approvals run in turn under a transaction lock (
pg_advisory_xact_lock, a lock held until a batch of saves finishes). So that approving and deactivating a material at the same time cannot approve a material that is no longer used, the material rows read during approval also get a shared lock. - Only one daily plan (PR 235). The plan is created the first time it is viewed that day, so two screens opening at once could create two plans. It is created while holding a lock on the module row, so only one appears.
- Records use the same lock (PR 236). So that checks stay consistent when a customer records a round while changing the plan, saving a record uses the same module row lock.
The Codex review on PR 236 found two more gaps.
- If the app missed a response and resent the same request 48 hours later, the server returned a “record too old” error instead of the saved record. It now checks whether the request number is the same before checking the time.
- If the same request arrived twice at once, the second could get a “round already recorded” error. It now checks the request number once more after taking the lock.
Database checks as the last line of defense
There was already a database trigger (a check the database runs by itself when saving) that blocks edits to an approved standard version. But relying only on the trigger means the user gets a 500 error with no reason. So the service code checks first and returns “cannot edit, not a draft” (409), and the trigger stays as a last line of defense for anything the code misses.
In the same spirit, integers with no defined range are accepted only up to the database integer limit. Larger values are rejected with 400 at the input step instead of reaching the database and becoming a 500.
Other choices:
- Lists in one query. The PR 240 review pointed out that the list read the database separately for each record, which would slow down as records pile up. It was changed to one query, and the combined list in PR 250 also joins the per-type tables with
UNION ALL(syntax that chains results from several tables into one list) and reads them at once. - Add decimals as integers (PR 243). Device input amounts are converted to integers in units of 0.1 before adding, so floating-point addition errors cannot tip a boundary check the wrong way.
- Old records use the settings of their time (PR 243). Editing a device record checks it against the settings version from when it was first recorded. If an admin changes the settings later, old records do not shift.
- Use the same calculation as the customer screen (PR 250). The admin plan view was not built separately; a read-only function was added to the calculation the customer screen uses. Building it separately could let the round status on the two screens drift apart.
- Send the block reason together with the plan (PR 240). When records do not add up and recalculation is blocked, the reason used to be left out of the response. The coordinating session chose to send “the reason and the current plan together”. Hiding the plan would hide rounds already recorded, and hiding the reason would mean the customer never sees the prompt to check their records.
The type check found a bug the tests missed
In PR 236, after all 14 new tests passed, the type check (the computer confirming code keeps the data shapes it declared) found a bug. The edit history field in the record detail response held raw database rows instead of the screen-facing shape.
The tests compared responses with toMatchObject (a comparison that checks only the expected fields and ignores the rest). The overlapping fields matched, so the tests passed even with extra fields attached. Claude fixed it, and the devlog noted “treat the type check as evidence as trustworthy as the tests”.
PR 235 had a different kind of silent failure. Three services ended up importing each other in a loop, and Nest (the server framework) could not find a dependency. When services import each other, an injected value can quietly end up empty, so the shared calculation function moved to a file that is not a service.
When several jobs overlap on one computer
During this period, server, admin web and customer app jobs ran at the same time on the same computer. Because of that, Codex test runs ended in unexpected ways several times.
- Stopped because another job’s clone changed (PR 233). The first run ended after about 14 minutes with exit code 3. The watcher had seen another job’s clone (the copy of the working folder Codex uses) change. This job’s clone only had 3 new test files, and I decided to use that result as is.
- Exit code 0 but nothing written (PR 240). Another job’s full verification was running, so all of Codex’s command and file-reading tools timed out. The run ended as a “success”, but the clone was unchanged. With the coordinating session’s permission, the same instructions were run again and succeeded. The first run for PR 250 also wrote nothing for the same reason.
- Stopped because Claude edited the same folder (PR 243). While Codex was writing tests, Claude wrote the implementation into the same working folder, and the watcher saw the change and ended the run with exit code 3. That was Claude’s mistake, breaking the track A rule. Only the two test files had changed in the clone, and I decided to accept the tests as is without running again. At the same time Docker Desktop (the program that starts the test database) was off, and the coordinating session turned it back on.
- Stopped after finding a contradiction in the instructions (PR 250). On the second run, Codex found that the “admin name” in the instructions did not exist in the real admin table and stopped by itself. The admin table has no name column, only an email. After the instructions were fixed, the third run wrote 8 tests.
- A review overlapped another run (PR 236). The review started right away when the busy check was empty, but the coordinating session’s “hold” message had not arrived yet, so it overlapped with a Codex run from the customer app job. The coordinating session said the review did not need to be run again.
The rules that came out of this:
- When a Codex run ends, do not look only at the exit code; check that the clone actually changed.
- Do not edit the same working folder while Codex is running. Only read while waiting.
- Before writing instructions, check that every field named in them exists in the real table.
How it was checked
- For each PR, the new tests Codex wrote were first confirmed to all fail before implementation (PR 233: 12, 235: 25, 236: 14, 240: 11, 243: 14, 250: 8).
- When a test-side error showed up after implementation, the implementation side did not edit the tests and sent a fix instruction back to Codex. PR 233 and 250 had the same cause: the test helper
factory(db)restarts its numbering every time it is called, so calling it twice in one test produced duplicate module codes. In PR 235, after a test moved the clock forward a day, the 15-minute login token expired and the request got a 401. - The full
npm run verifypassed in five PRs, taking between 481 and 597 seconds. Server tests grew from 746 in PR 233 to 813 in PR 243. For PR 250 the end-of-response automatic verification passed, but the time taken was not recorded. No tests were rerun individually. codex exec reviewfound nothing in PR 233, 243 and 250. In PR 235 it raised two points (the total when every round is locked, and a first implementation that reduced the amount to hand out directly, which differed from the contract), in PR 236 the two above, and in PR 240 one about the list query; all were fixed.npm run e2e(tests that click through the screens in a browser) was run only in PR 233 and 235, where admin web code changed, and 7 tests passed.
Lessons
- Recheck after taking the lock for requests that can arrive together. A value checked before the lock can change while waiting for it.
- Keep database checks as the last line of defense. The error a user sees should come from code that attaches a reason first.
- Loose comparison tests pass even when extra fields are attached. Protect response shapes with the type check too.
- Look at the result even when a run ends in “success”. When several jobs overlap on one computer, tools can time out and finish having done nothing.