[PR #222·223·227·229] Laying the Ground for Detail 3: Choosing the Scope, Shared Types, Locking Approved Standards, and Rounding
These four PRs lay the ground for Detail 3 (input records that people enter, and a daily plan the server calculates). In order: the planning that set the scope (PR 222), the types shared by the server, admin web and customer app (PR 223), the database tables (PR 227) and the calculation functions (PR 229). Each one had to finish before the next could start, so I put them in one post in sequence.
All the work ran on my Windows computer. In all four PRs a Claude Code session (Claude Opus 5.5) handled the planning, work instructions, implementation, checks and PR. Codex (gpt-6.1-terra) wrote the code for PR 223 and the tests for PRs 227 and 229, and the implementation side did not edit those test files. The final review for PRs 227 and 229 was codex exec review (Codex’s default model, name unknown). In this post, words that would reveal the business field are replaced with general ones (input material, device, count, and so on).
| PR | What it did | Who | How it was checked |
|---|---|---|---|
| 222 | Planning that set the scope (spec and plans) | I decided the scope, how to calculate and more; Claude wrote the spec and plans | A subagent read the spec and raised 8 questions |
| 223 | Types, error codes and policy values shared by three areas | Codex wrote the code; Claude compared it | Build, type checks, lint, line-by-line comparison with the plan |
| 227 | Database tables and the standard lock | I chose the lock option; Codex wrote tests; Claude implemented | 38 tests, full verify, codex exec review |
| 229 | Daily plan calculation functions and rounding | Codex wrote tests; Claude implemented | 19 tests, full verify, codex exec review twice |
PR 222: Choosing the Scope First
What was done and why
The head office sent a development brief and screen mockups for Detail 3. But the material was much wider than Detail 3. It also covered today’s tasks and routines, home warnings and alerts, and automatic input using the devices. The pilot has no device control link, and the head office’s calculation standard data had not arrived yet. So before writing the spec (the design document describing what to build), Claude had the scope decided first.
Chosen approach and rejected options
- Scope: build only the input and refill records that people enter, plus the daily plan the server calculates. Automatic runs get only a place in the data.
- Rejected: building the automatic flow with a fake controller takes more than a week. Re-splitting all the received material across later Details would shake the overall schedule.
- Daily plan (
option 1): the server calculates it from a calculation standard version that the head office approved in the admin web, and saves the plan as versions. If there is no standard, the screen shows “No standard”. That lets development go ahead before the head office answers.- Rejected: having the head office enter it by hand every day creates daily work for them. Calculating in the app breaks the information boundary “do not send the formula to customers”.
- Where devices belong: devices sit at the customer facility level. That matches a setup where several areas share one device.
- What carries over to the next day: round times, the amount handed out directly, and the material type carry over; the daily total is recalculated every day.
- The calculation basis shows only in the admin web. An older standard document said to show it on the customer screen, but the newer specification was followed.
I decided the first four.
How it was checked
- Claude read the whole brief and compared it against screen text extracted from the scripts inside the mockup HTML. It did not open the mockups and click through them.
- Claude read the server’s existing record code to match file and function names in the plans.
- A subagent (Claude Opus 5.5) read the spec in the role of “a developer writing a plan from this spec alone”. It raised 8 questions. Two (where devices belong, what carries over) were put to me, and for the other six Claude wrote its judgment into the spec, such as the total formula used when recalculating, which reading to pick when there are several temperature records, and how to handle late records.
PR 223: Agreeing on the Shared “Vocabulary” First
What was done and why
This is Detail 3’s first code task. The types (shapes of requests and responses) shared by the server, admin web and customer app, 15 error codes and 8 policy values (a capacity limit, 48 hours for late records, 1,000 characters for memos, and so on) went into the shared package first. Once merged, the admin web and customer app can build screens against fake responses without waiting for the real server.
The database structure, server behavior and screens did not change. The type the customer receives has no calculation basis; the basis is only in the type the admin receives. The information boundary chosen in PR 222 is kept from the type level up.
Chosen approach and rejected options
- Claude left the code to Codex alone. The task only writes types and constants, so there is no running code and nothing to split between a test writer and an implementer. The lead session writing it directly would have been faster, but that was rejected to keep the split “implementation elsewhere, the lead session reviews”.
- Lists that never change are written
as const. The plan wrote plain arrays, but this gives narrow types like'A'|'B'|'C'|'D'(types that name exactly which values are allowed). - Values that already existed were not added again.
How it was checked
- In the Codex clone, the shared package build, type checks in every area and lint passed.
- Claude compared the three changed files line by line against the type, error code and policy value lines in the server plan, by name and shape. Nothing differed.
- The app’s message test uses
Partial<Record<ErrorCode, string>>(a shape that does not require a message for every error code), so Claude confirmed in the code that new error codes do not break existing tests.
PR 227: The Database Guards Approved Standards
What was done and why
All of Detail 3’s server work needs the tables first. So this PR added migration 0006 (a step file that changes the database structure) and 22 entities (tables written as code): material types, calculation standard versions and their row tables, plans and rounds, record headers, version tables for input, count change and refill records, devices and their setting versions, and more. The API and screens did not change.
- The 11 record version tables cannot be edited or deleted. They reuse
detail2_append_only()from Detail 2. - For refill records, the database itself checks “final amount = remaining + added”, “final amount ≤ capacity” and “added > 0”.
- A partial unique index (a guard that stops only rows matching a condition from overlapping) keeps one “current plan” and one “new proposal” per area and date.
Chosen approach and rejected options
- Locking calculation standard versions (I picked
option B): the plan said to put a no-edit trigger (a check the database runs on its own) on every version table. But a calculation standard must be editable while it is a draft, and its status changes when approved. So Claude wrote a new function,detail3_standard_locked().- Drafts can be edited and deleted. Status only moves draft → approved and approved → past. Approved and past standards cannot be edited or deleted.
- The 4 row tables accept inserts, edits and deletes only while the parent version is a draft.
- All five tables block TRUNCATE (emptying a whole table). Row-level triggers do not run on TRUNCATE, so an approved standard could have been wiped in one go.
codex exec reviewpointed this out. - Rejected (
option A): no triggers on these tables, trusting the service code alone. If a code mistake quietly changed an approved standard that a past plan points to, nobody would know.
- Claude generated the migration SQL from the entities. It ran the earlier migrations on an empty PostgreSQL and copied the difference SQL that TypeORM (a library for handling the database from code) produced. Hand-written SQL easily drifts from the entities in names and order and breaks the “entities and migrations match” test. Only the triggers and the device defaults were added by hand.
- Common columns of the version tables live in one base class. The primary key is
(record_id, version_no). - Column lengths and type values the plan did not specify were chosen by Claude and marked “owner check needed” in the PR.
How it was checked
- Before the implementation: the tests Codex wrote and the 2 lock tests were confirmed failing (the tables did not exist yet).
- After: 38 tests in four table-related test files passed.
- In a throwaway PostgreSQL 17 box, 0006 was applied separately to confirm no difference from the entities, correct device defaults, and that rolling back and applying again (
down→up) works. - Full
npm run verify: the first run failed because one admin web test went over its 5-second limit under load (5,094ms); that file and the whole admin web suite were rerun and passed. On the last commit, with TRUNCATE blocked, the full verify passed in 602 seconds (715 server, 325 admin web, 464 customer app tests). codex exec reviewraised only the TRUNCATE issue, which was blocked with a statement-level trigger, and a check was added to the test.
What happened during this task
While Codex was writing tests, Claude wrote implementation files into the same working folder, and the script guarding Codex stopped with exit code 3 because “the working folder changed”. Codex had not written outside its own clone. By my decision, Codex’s tests were committed in the clone and brought over, placed before the implementation commits.
The 2 lock tests came after my decision, so the implementing session wrote them itself with the lead session’s approval. That is an exception to the rule “tests are written by the side that does not implement”, so the log notes it for reviewers to keep in mind.
PR 229: The Calculation Functions and Rounding
What was done and why
The daily plan has more number rules than anything else in Detail 3. So the calculation was split out into pure functions (functions that produce an answer from their inputs alone, without calling the database or server framework). Later tasks only gather inputs and call them. The API and screens did not change.
computeDailyPlan: calculates in the order remaining count → days elapsed → rate by days elapsed → weight per individual → temperature correction → daily total → share per round → split by material. For the 6 cases where it cannot calculate, it returns the first reason in a fixed order and does not fall back to defaults.splitRemaining: remaining share per round = new daily total − the planned amounts of rounds already recorded or skipped − the amount handed out directly. At 0 or below, everything is 0 with “already all given”. An amount given short is not piled onto later rounds.roundKg: rounds at the third decimal place (1g). The difference left over from splitting goes to the last round.
Chosen approach and rejected options
- Rounding: with
Math.round(x * 1000) / 1000alone, the standard document’s example of 4.8195kg becomes 4.81949… because of binary fraction error (the tiny difference that comes from computers storing decimals in base 2) and rounds down to 4.819. So the value times 1000 is first trimmed to 12 digits and then rounded. AddingNumber.EPSILON(a very small value) was rejected because it has no effect on large values. - Checking that ratios add up: when checking whether ratios sum to 100, they are also trimmed with
roundKgbefore comparing, so 33.333 + 33.333 + 33.334 does not miss 100 by a rounding error. - Round ratios: if there is a usual setting, its number of times gets equal ratios. Borrowing the standard version’s ratios from the front was rejected, following the plan’s review guidance.
- A cap on the direct amount (not in the plan, left as “owner check needed”): if the usual setting’s “amount handed out directly” is larger than that day’s total, the direct amount is cut to the daily total and every round is 0. Left as is, the check “sum of rounds + direct amount = daily total” would fail and later tasks could not save the plan.
codex exec reviewpointed this out. Adding a new “cannot calculate” reason was rejected because it would change PR 223’s shared types. - The spec’s “cases where it does not calculate” table was not meant to set an order, so the order written in the plan was followed. No numbers differed.
How it was checked
- Codex wrote 18 tests, and on the commit with only the tests they were confirmed failing because the calculation module did not exist. After the implementation, all 18 passed.
- For the direct amount finding from the first
codex exec review, the implementation side does not write tests, so a short fix instruction had Codex add 1 test. After seeing that test fail, the implementation was fixed and all 19 passed. - Full
npm run verify: passed in 627 seconds on the last commit (734 server, 379 admin web, 501 customer app tests). No test needed a rerun. - The second
codex exec reviewpointed out thatroundKgalso rounds up a value 0.0000000000001kg below the rounding boundary. It was not changed. All inputs have at most three decimal places, so a value that close to the boundary only appears through floating-point error, and correcting that error is the whole point of this step. The real weight difference is far below 1g.
What was not checked
- PR 222’s screen mockups were only compared by text; they were not opened and clicked through.
- Cases the PR 229 tests deliberately left out (days elapsed beyond the end of the table, a start date after today, an empty ratio list, and so on) have behavior defined by the current implementation but no tests.
- The name of the Codex default model used for the final reviews is unknown.
Lessons
- When received material is wider than the scope, choose the scope before the spec. The first two questions (scope, how to calculate) settled half of the remaining decisions on their own.
- Defining shared types first lets three areas start at once. Leaving out values that customers must not get, right from the types, leaves no room for that mistake.
- If the goal is a lock, close the side doors too. Claude knew about the TRUNCATE gap in advance but dismissed it as “foreign keys stop it”. Adding
CASCADEwipes the table anyway. - Test decimal rounding with the examples people wrote. Checking that a documented value like 4.8195 comes out as written exposes binary fraction error.
- Do not touch the same working folder while Codex is running. Codex now runs only one task at a time across all streams.