[PR #267·270·272·277·278] Making Each Day's Tasks Only Once: Shared Contracts, Rule Functions, Database Indexes, a Draft Seed
Five PRs built the server foundation for Detail 4 (quests, routines, head office review). In one line, it is “make each day’s tasks only once.” A task is one line of what a site has to do that day. Even if the scheduled run and the customer app’s first view arrive at the same moment, or the app goes unopened for days, a task must not go missing or be created twice.
The order was this: first agree on the contract the three areas (server, admin web, customer app) will use (PR 267), build rule functions that calculate without the database (PR 270), make the database tables guard “never create twice” (PR 272), then add task generation that makes only the day’s share (PR 278). Alongside, a seed (a command that fills in initial data) puts the quest candidate sheet in as a draft version (PR 277).
The models involved are below. In every PR, the lead session on my Windows computer (a Claude Code session that hands out work to other sessions, Claude Opus 5.5) directed the work, and Codex (gpt-6.1-terra) wrote either the tests or the implementation. The final review (codex exec review) was done by Codex in all five PRs.
- PR 267: The lead session wrote the work order, cross-checked, verified, and wrote the docs and PR; Codex wrote the code.
- PR 270: The lead session wrote the work order and implementation, verified, and wrote the docs and PR; Codex wrote the tests.
- PR 272: The lead session wrote the work order and implementation, verified, and wrote the docs and PR; Codex wrote the tests. The review ran twice.
- PR 278: The lead session wrote the work order and implementation, verified, and wrote the docs and PR; Codex wrote the tests.
- PR 277: The lead session wrote the tests and work order, verified, and wrote the docs and PR; Codex did the implementation. The sheet text was read and passed along by another lead session (Claude) in charge of Detail 4 development.
| PR | What was done | Who wrote it | Result |
|---|---|---|---|
| 267 | The shared contract for three areas (types, error codes, policy values) | Codex (code) | The lead session matched it line by line against the plan; review found nothing to fix |
| 270 | Rule functions that calculate without the database | Claude (implementation), Codex (tests) | 32 tests failed first, then passed |
| 272 | Database tables that guard “never create twice” | Claude (implementation), Codex (tests) | 11 tests passed; 2 review findings applied |
| 278 | Task creation that makes only the day’s share | Claude (implementation), Codex (tests) | 16 tests passed; 3 review findings applied |
| 277 | A seed that loads the quest candidate sheet as a draft version | Claude (tests), Codex (implementation) | 10 tests passed; 1 review finding applied |
PR 267: The shared contract first
What was done and why
This was the first task of Detail 4. It set the “contract” the three areas share. Once merged, the admin web and customer app sides can build their screens first with mock responses (server answers fixed in advance) instead of waiting for the real server.
- Types (the shape of requests and responses): the today tab, a single task and its history, a customer’s action request, the admin’s review of problem reports, quest list versions and the preview for moving to a new version, head office quests, routines, and rows of the input and refill lists.
- 12 error codes: for example “different content under the same request number,” “someone else handled it first,” and “an approved version can’t be edited.”
- Two fields in the error response: one holding the current task (or routine) on a conflict, and one holding the list of rows that failed a quest version check.
- 16 policy values: a 1,000-character memo, a 200-character skip reason, a 500-character head office reply, quest dates within ±365 days, and so on.
The database structure, server behavior, and screens did not change. The head office memo field went only into the admin types, not the customer types.
What was chosen, and what was dropped
| Chosen | Dropped alternative | Why it was dropped |
|---|---|---|
| Hand the code to Codex | The lead session writes it directly | Faster, but it keeps the split where “the lead session reviews” |
- Codex only: The task was only writing types and constants, with no running code, so there was nothing to split between a test-writing side and an implementing side. Checks were build, type check, lint, and the lead session comparing line by line.
- The two error-response fields as optional fields: The plan only said “put it in the body,” so the work order made them optional fields (fields that may or may not be present) of the error response. It’s the same approach as Detail 3 and easy to undo.
- Dropped alternative — the lead session writing it directly: faster, but it was handed to Codex to keep the split where “the lead session reviews.”
How it was checked
- On Codex’s side, the shared package build, type checks for every area, and lint passed.
- The lead session compared the three changed files, name by name and shape by shape, with the types, error codes, and policy values in the server plan. Nothing differed, and nothing clashed with an existing name.
- Codex review: nothing to fix.
- The full
npm run verify(the command that runs all tests and the build) passed in 198 seconds. It failed once before that, at the same time a Codex working copy was being prepared, and passed when rerun. The failing line wasn’t left in the output, so the cause is unconfirmed.
PR 270: Rule functions that calculate without the database
What was done and why
“When to create which task, and how its state changes when pressed” was gathered into one file of pure functions (functions that always give the same answer for the same input and never call the database or server). Task generation and customer actions later use these functions.
- Quest dates: the start date (or the planned start date if there isn’t one) or the planned end date, plus a number of days. “Day 30” on the sheet is the start date + 29 days.
- Quests to create: after the day the version was pinned, up to today, and not yet created. Even if the app goes unopened for days, all the past ones appear.
- Routine counts: daily, by weekday, or every two weeks; from N days before the planned end date, twice a day (once for site-level routines).
- State changes: only as the spec’s state table allows; any change not in the table is rejected.
- Normal or abnormal from check-item results, progress counts (skipped items leave the denominator), and sorting the today list.
What was chosen, and what was dropped
| Chosen | Dropped alternative | Why it was dropped |
|---|---|---|
A switch statement that spells out each case |
Keep the state table as data and look it up | The conditions are mixed, so the switch was easier to read |
- Dates are handled as
YYYY-MM-DDtext. Text of the same shape sorts in date order, so comparison is done as text too. Only when adding days is it converted to UTC midnight, so no time zone math creeps in. - When a cycle ends, the remaining tasks are closed as “cycle ended.” While writing tests, Codex asked about the spec’s state table: its “next state” column had no row for closing tasks awaiting head office or asked to recheck. The lead session followed the spec’s sentence “when the cycle ends, close the remaining tasks” and the plan’s rule list, chose to close them, and had Codex fix the test. It’s an easy decision to undo.
- Dropped alternative — keeping the state table as data and looking it up: the conditions (result type, task kind, whether head office has seen it) are mixed, so a
switchstatement that spells out each case was easier to read.
How it was checked
- Codex wrote 32 tests first (the whole list in the plan, one per allowed row of the state table, and that sorting doesn’t change its input), and they were confirmed to fail with “module not found” before implementation.
- On the first run after implementation, 31 passed and 1 failed because the “cycle ended” question above had been written differently from the plan. After Codex fixed the test, all passed.
- Full
npm run verifypassed in 394 seconds; Codex review found nothing to fix.
PR 272: The database tables guard “never create twice”
What was done and why
This created the tables Detail 4 stores into: quest list versions and their items, the version pinned to a cycle, routines, head office quests, the day’s tasks, and action history.
- Only one version can be approved, and items can be added, edited, or deleted only while their version is a draft. A trigger (a check the database runs by itself when a row is written) enforces this.
- Four partial unique indexes on the task table keep the same task from being created twice. A partial unique index is a rule that “among rows matching a condition, the same value can’t appear twice.” Quests are unique per cycle and code, a head office quest has one, routines are per date and occurrence, and site-level routines have one per site.
- Action history can’t be edited or deleted. It is a record that only grows.
What was chosen, and what was dropped
- The SQL generated from the table definitions in code was copied as is. An existing test checks that “the table definitions in code and the database change files describe the same tables,” so this leaves fewer places to go wrong than writing by hand. Only the triggers were written by hand.
- One foreign key not in the plan was added. A foreign key is a rule that a field pointing at another table may only point at rows that actually exist. The plan only said the task’s version field “may be empty”; this keeps a task from pointing at a version that doesn’t exist.
- A concurrent-approval gap found by Codex’s review: if the item-guard trigger only reads the version’s state, another admin could approve that version at the very moment an item is being edited, leaving a gap where “an approved version’s item changes.” The version row is now held with
FOR SHARE(reading the row while holding it so no one else can change it), so approval waits until the item write finishes. - Another point from the same review: action history is read per task, but there was no index, so an index by task and time was added.
How it was checked
- Codex wrote 11 tests: adding, editing, and deleting items of an approved version are rejected; drafts can be edited; two approved versions are rejected; editing or deleting history is rejected; the same quest twice is rejected; the same routine occurrence twice is rejected; and so on.
- The structure change was rolled back and applied again, confirming that all six new tables were dropped and recreated (done inside a transaction and undone at the end).
- The full
npm run verifypassed in 561 seconds before the review fixes and in 524 seconds after applying them and merging main. Codex’s review raised two points the first time (applied above) and found nothing the second time.
PR 278: Making only the day’s share
What was done and why
This is the heart of Detail 4: “make each day’s tasks only once.” Inside one transaction (a bundle of work that either all succeeds or is all cancelled), it creates the tasks for a module’s planned and running cycles up to today’s share.
- Quests: items of the pinned version dated after the pinning day, up to today, and not yet created.
- Head office quests: dated up to today and not cancelled.
- Routines: only for running cycles, only today’s share.
- Site-level routines: one per site if the site has at least one running cycle.
- Closing: to-do routines up to yesterday are closed as “no record” (morning and evening ones on the same day don’t close each other), and open tasks of ended cycles are closed as “cycle ended.” Each close leaves one system line in the action history.
- Scheduled run: once when the server starts, then every 5 minutes it goes through all modules if the Korean date has changed. If one module fails, it moves on to the next.
- Version pinning: in the two places that create a new cycle, the approved version is pinned within the same transaction. If there is no approved version, it is left empty, and the first approval pins every unpinned cycle.
What was chosen, and what was dropped
| Chosen | Dropped alternative | Why it was dropped |
|---|---|---|
| Make only the day’s share | Create all of a cycle’s tasks in advance | If the start date or planned end date changes, tasks not yet created have to follow the new date |
- Every insert is
INSERT … ON CONFLICT DO NOTHING(a save that does nothing if the row already exists). The partial unique indexes from PR 272 guard “never create twice” in the database, so even if the midnight scheduled run and the day’s first view arrive together, only one is created. - Closing grabs the row first (
FOR UPDATE) and then changes the state. If two arrive at once, the later one filters again after grabbing the row, so only one history line remains. - Dropped alternative — creating all of a cycle’s tasks in advance: if the start date or planned end date changes, tasks not yet created have to follow the new date. So only the day’s share is created.
Three points raised by Codex’s review were applied.
- The closing range was too wide. At first, every past to-do routine in the module was closed as “no record.” That would also close tasks on days the schedule doesn’t match (weekday or every-two-weeks routines) and tasks from the previous cycle. Following the plan’s rule, it was narrowed to “the same routine and the same target that got a task today.”
- Pinning could be missed when cycle creation and version approval came at the same time, because each can’t see the other’s uncommitted rows. Both now take the same transaction lock (
pg_advisory_xact_lock, a named lock that releases on its own when the transaction ends) so they run one after the other. - In the scheduled run, the day counted as done even if a module failed. Now, if anything failed, it runs again on the next 5-minute tick. Tasks already created aren’t created again.
How it was checked
- Codex wrote 16 tests: creating on first view, only one under concurrent runs, the Korean midnight boundary, skipping three days, pinning when a cycle is created, pinning on first approval, keeping the old version after a new one is approved, two runs on the same day not closing each other, yesterday’s to-do becoming “no record,” closing ended cycles, the rest continuing when one cycle fails, one site routine, and more.
- On the first run after implementation, 15 passed and 1 failed. The cause was the test, not the implementation: the clock was moved forward a day after the admin had logged in, so the login expired (admin logins end within a day). It went back to Codex to fix the test; the implementation was not changed.
- The full
npm run verifypassed in 520 seconds both before the review fixes and after fixes 1 and 2. Fix 3 (one line in the scheduled run) was checked with the tests for the changed parts, type check, and lint, and Codex’s review was not rerun after it.
PR 277: The seed goes in only as a draft
What was done and why
So that head office doesn’t have to enter the quest list from scratch, a command was built that puts the quest candidate sheet in as a draft version. It adds one draft version with 22 items and, if there are no routines at all, 4 routines (equipment check, cleaning, stock check, device inspection). If they already exist, it does nothing.
- The seed doesn’t pin a version to any cycle. It’s only used once head office approves it.
- Unconfirmed numbers and names were removed from the text, such as numbers still waiting on head office’s answer and product feature names.
- “Day N” on the sheet became start date + (N−1) days, and “−N days” became start date − N days. Items on the ending side are based on the planned end date.
What was chosen, and what was dropped
| Chosen | Dropped alternative | Why it was dropped |
|---|---|---|
| A transaction lock so concurrent seeds run in turn | A unique constraint on the version name | It changes the database structure, which was outside this scope |
- Tests first: the lead session wrote 10 tests first (confirmed to fail with “module not found”), and Codex implemented.
- The sheet text was copied into the work order. Codex couldn’t read the shared drive, so the lead session copied the table and original text it had received straight into the work order.
- A lock for concurrent seeding: Codex’s review pointed out that “if two people run the seed at once, both see ‘not there yet’ and insert twice.” A transaction lock now makes them run one after the other (Codex fixed it).
- Dropped alternative — a unique constraint on the version name: that changes the database structure, which was outside this scope.
How it was checked
- 10 tests: one draft version, 22 items and their order, no excluded rows, excluded numbers and names absent from the text, date bases and offsets, 4 routines, unchanged after running twice, no routines added if some exist, not pinned to any cycle, and so on.
- The first full
npm run verifyfailed with 3 customer app tests hitting the 5-second limit. The load came from server tests running in another working folder at the same time; running just those two files alone, all passed. After applying the review fix and merging main, the full verify passed in 505 seconds. - Codex’s review raised one point the first time (the lock above) and found nothing the second time.
Lessons
- Agreeing on the contract first means no area waits. Once types, error-response fields, and policy values are merged, the screen side can work first with mock responses.
- Leave “never create twice” to the database. With partial unique indexes and
ON CONFLICT DO NOTHING, there’s one task even when the scheduled run and the first view overlap. The code only has to make the day’s share. - A check that only reads has a concurrent-approval gap. If someone else can change the state between reading it and deciding, hold that row or line both sides up behind the same lock.
- Close narrowly, retry failures. Narrow closing to “the same target that got a task today,” and let a failed scheduled run go again on the next tick.
- Seed only as a draft, and drop unconfirmed numbers from the text. Whether it gets used is decided by head office’s approval.
- Moving the clock in a test can end the login too. Admin logins end within a day, so a test that crosses a date also has to consider whether the login is still alive afterward.
- Run the full check one at a time. Running it in two working folders at once hits time limits and fails unrelated tests.