Plan: Child-Keyed Records Carry the Custody Unit (#1909 b)
- Archive record
- Status
- Context (census 2026-10-09, on
main) - Scope
- Design
- D1 — The source, under a child-level lock (F1)
- D2 — No episode, or a NULL unit
- D3 — Columns and wire
- D4 — Heal, fill-only in app SQL
- D5 — Backfill and invariant (F2)
- D6 — Creates and replays
- D7 — Reads, child facts, lists
- D8 — ADR-060 re-check (F4)
- D9 — Texas rows (F5)
- D10 — Floor (replaces "no bump")
- D11 — Close-out order
- Units
- Test matrix
- Rollout and risks
- Review log
Archive record
Merge requests: !1850 (U0 plan page + the epic and unit issues, Relates #1909) + !1851 (U1, Closes #2233) + !1852 (U2, Closes #2234); the close-out !1853 (U3, Closes #2235 and #1909)
2026-10-09 — approved in plan mode 2026-10-09 (forks F1–F5 ruled) after a contextless round 1 (no-ship, 3 blockers fixed,
10 minors folded) and the maintainer’s round 2 (no-ship, 8 findings fixed). What landed:
the stamp (U1, D1–D5, D10: education_records / health_records gain admin_unit and unit_episode_id, copied at birth
from the child’s latest live removal episode read FOR SHARE under the child-episode advisory lock every episode writer
takes after its case lock; no episode, or no unit yet, stamps NULL and fails closed; both inbox handlers and
backfill-cross-service-assignment fill a NULL record from its source episode; the migration backfills best-effort and
bumps schema_compat_floor last; the BLOCKING invariant child_record_admin_unit_from_episode checks sourced rows);
the checks (U2, D6–D8: one builder per type carries the stamp, a NULL leaving the key absent; the create refs carry the
pre-transaction episode unit and a moved selection is 409; a create replay answers only after read on the stored record;
a field-proposal decision loads its target first, absent = 404; the child-fact routes carry the child’s latest episode unit
and re-check its id and unit presence under the lock; update, delete, accept and reject compare the locked stamp — 409
concurrent-modification; the three lists bind admin_unit = ANY($n); UNIT_BEARING_TYPES fifteen, the nil-id census
eight);
the Texas rows (U3, D9: both rulesets 1.4.0 in the foster_home shape — the unit LIST row above the supervisor row, the
wildcard unit CHECK row and its two field twins, the supervisor rows untouched; fourteen Texas files consult units; zero
direct check(List) sites on the two types, pinned). Georgia is unchanged (realm-flat, ADR-077 D3). ADR-077 D5 / D6 / D7
and ADR-060 amended. Risks recorded: Texas education and health routes answer 503 while the units source is not ready;
the backfill approximates the create-time pick; records of children never removed stay NULL. Follow-ups: #1912 (the dead
Texas supervisor rows), #2075 (the foster-home proposal target’s unit re-check), #2236 (the implementation guide’s
field-proposal decide rows predate the U2 re-checks).
Status
Done (2026-10-09) — !1853
| Anchor | What binds here |
|---|---|
#1909 (b); ruling 2026-10-06 |
a creation-time unit stamp on education and health records, fill-only, NULL fails closed, the ADR-077 D5/D6 shape; schema, stamp and list binds jurisdiction-neutral; whether a role consults the unit is each jurisdiction’s rules |
Forks ruled 2026-10-09 |
the latest live episode sources the stamp (F1); the migration backfills (F2); child-facts checks derive live and recheck (F3); update, accept, reject and delete re-check in their transaction (F4); regional rows are added, never the supervisor rows re-keyed (F5) |
ADR-077 D3, D5, D6, D7 |
a jurisdiction opts in through its rulesets; a NULL stamp leaves the key absent; the stamp is copied at birth under a lock; fill-only is app SQL; the Texas regional rows for these types were deleted for want of a column |
ADR-060 D1, D3; ADR-063 D3 |
a check that reads a mutable row value re-checks it in the write transaction; source carries no ruleset knowledge; a floor bump keeps N-1 writers off a changed schema |
Context (census 2026-10-09, on main)
-
Schema.
education_records(child_idNOT NULL, no FK;placement_idnullable FK) andhealth_records(child_idonly) carry nocase_id, noadmin_unitand no trigger (craig-placement/migrations/20260321000000_*,…001_*). -
Writers.
create_education(api/education.rs:148, replay branch:218) andcreate_health(api/health.rs:69, replay:136) answer a replay with the stored row and no check; updatesstore/education.rs:200,store/health.rs:223(return the row); proposal acceptstore/governed.rs:175-183(returns()), whoselock_target(:140-150) returns only whether the row exists; rejectfield_proposal_decide.rs:253mutates the proposal without re-reading the target. -
Refs.
education_resource_ref(education.rs:30),health_resource_ref(health.rs:37),nil_record_ref(api/governed.rs:73, both creates),TargetRef(field_proposal_decide.rs:76) andChildPath::authorize(child_facts/acts.rs:127;RoutePath: Copy + 'static,authorize(self, &Ctx, …)withCtx.appthe service state,craig-ledger/src/act.rs:43-66) all carry empty attrs. The coverage gate skips literals whose type is a variable (validate_authz_coverage.rs:714), so the last three are pinned by tests. -
Lists.
education.rs:245,health.rs:163,:416(overdue) treatScopeFilter::Unitsas Denied (list_scope_units_tests.rs:121-139); their store fns take no unit. -
The unit source.
removal_episodes(child_id,case_id,admin_unit TEXT NULL) has non-overlapping live windows per child (excl_removal_episodes_child_window,removal_episodes_recut.sql:146); its unit is fill-only in the DB (refuse_episode_row_mutationclause 3,:114-118). Its writers areinsert_episode,set_removal_at,set_exit_atandretire_episode(store/removal_episodes.rs:446,:510,:535,:566); episode creation takes a case-level lock (api/episodes/acts.rs:245), which does not span cases. -
Templates. Stamp
20260916140000_admin_unit_stamp.sql:30-48; backfill plus invariantcraig-cases/migrations/20260916140000_investigation_admin_unit.sql:30-43andcraig-test-lib/sql/invariants/craig-cases/investigation_admin_unit_from_referral.sql; buildersplacements.rs:59,:114; birth stamp underFOR SHAREplacements.rs:1026; list bindstore/placements.rs:656,:705; healsinbox/case_assignment.rs:86-144,backfill_cross_service_assignment.rs:387-391; the delete lock-and-comparehome_documents.rs(#2232).
Scope
In: D1–D11 on both tables; refs, creates, replays, child-facts checks, proposal decide and lists; the child lock on the episode writers; the heal; the backfill and its invariant; the seed; the Texas rulesets; the gate and census lists; ADR-077, ADR-060 amendments; docs and CHANGELOG.
Out: Georgia rulesets (realm-flat, ADR-077 D3); any change to the supervisor rows (#1912); a case link on these tables.
Design
D1 — The source, under a child-level lock (F1)
-
Selection. The child’s latest live episode: retracted excluded,
ORDER BY removal_at DESC LIMIT 1(the open episode when there is one, since windows never overlap). Two store fns instore/removal_episodes.rsreturnOption<(episode_id, admin_unit)>:latest_live_episode_for_child(executor, child_id)(unlocked) andlatest_live_episode_for_child_shared(tx, child_id)(FOR SHARE). -
Serialization.
lock_child_episodes(tx, child_id)takespg_advisory_xact_lock(hashtext('child-episodes:' || child_id)). Every caller of the four episode writers takes it right after its existing case-level lock; every record create and every child-facts write takes it before the_sharedread. Lock order everywhere: case lock → child lock → episode row → record row. The selection therefore cannot change between the read and the commit, including from no episode to one. -
The education
placement_idnever sources the unit: it is caller-supplied and unchecked againstchild_id.
D2 — No episode, or a NULL unit
The record is stamped NULL with unit_episode_id NULL (no episode) or set (an episode with no unit yet): the key is absent,
i_unit_match is false, and unit-scoped lists omit it. The create is never refused for it.
D3 — Columns and wire
Both tables gain admin_unit TEXT NULL and unit_episode_id UUID NULL REFERENCES removal_episodes(id) ON DELETE RESTRICT, a partial
index WHERE admin_unit IS NOT NULL, and a heal index on unit_episode_id WHERE admin_unit IS NULL. On the wire admin_unit is an
Option<String> on both record views (the Placement shape, models.rs:179); unit_episode_id is #[serde(skip)].
Erratum (2026-10-09, U1, #2233): D3’s unit_episode_id is not a model field; it is off the wire by absence, since nothing reads it (a never-read field fails dead_code under the battery’s -D warnings).
D4 — Heal, fill-only in app SQL
Both case-assignment inbox handlers run, after HEAL_UNIT_REMOVAL_EPISODES, one unit-only UPDATE per table joined on
unit_episode_id = e.id and filtered to the case, setting admin_unit only where the record’s is NULL and the episode’s is not.
backfill-cross-service-assignment adds two statements in the joined shape of UNIT_FILL_PAYMENT_ADJUSTMENTS_SQL (binds
(case_id, admin_unit)), run after its episode statement. The create’s FOR SHARE on the episode makes the heal wait for the
record’s commit, so a racing heal never strands a NULL. No other SQL writes the column.
D5 — Backfill and invariant (F2)
-
Backfill. The migration sets both columns for each existing record from the child’s live episode with the greatest
removal_at ⇐ created_at. A record with none stays NULL/NULL: unsourced, failing closed. Best-effort, and recorded as such. -
Invariant.
child_record_admin_unit_from_episode(severityhigh, grace 0 s, the investigation template’s shape) checks only SOURCED rows (unit_episode_id IS NOT NULL), per table: a NULL stamp whose source episode has a unit (a missed heal), and a stamp that differs from its source’s unit. It infers nothing from timestamps, so a later recut or a commit-order race cannot trip it.
D6 — Creates and replays
-
Create. Typed
education_create_refandhealth_create_refreplacenil_record_reffor checks and the field consult. The handler derives(episode_id, unit)unlocked for the check. Inside the transaction: the child lock, then the_sharedread. A different episode answers 409concurrent-modificationand writes nothing; the same episode with its unit filled since stamps the live value (safe: the check ran under the stricter absent key; the #1902 precedent). -
Replay. Before answering, the replay branch builds the stored record’s ref (its stamp) and checks
Action::Read. A refusal is the ordinary 403, and nothing of the record is disclosed.
D7 — Reads, child facts, lists
-
The two builders read
row.admin_unit; a NULL leaves the key absent.TargetRefloads the record (soft-deleted included, so stale proposals still drain; absent is 404 before the check) and uses them. -
ChildPath (F3). It gains
episode_id: Option<Uuid>andunit_filled: bool, set by an asyncChildPath::resolve(D1 unlocked) where the path is built (child_facts/acts.rs:308-309,:349-350).authorizereads that episode’s unit throughctx.appand carries it, or leaves the key absent whenunit_filledis false.guardtakes the child lock, runs the_sharedread and answers 409 when the episode id OR the unit’s presence differs. Since the episode unit only ever fills, equal id and presence mean equal value.RoutePathis unchanged. -
Lists. The three bind
($n::TEXT[] IS NULL OR admin_unit = ANY($n))in page and count; Worker and Supervisor scopes keep their empty pages (#1912); portal callers pass none; the proposals list keeps its Denied pin.
D8 — ADR-060 re-check (F4)
Update, proposal accept and proposal reject compare the locked admin_unit with the checked one. lock_target returns the locked
unit for the two record tables, and reject now calls it before writing the proposal. Delete takes the row lock and compares (the
#2232 shape). A mismatch is 409 concurrent-modification, nothing written. The exemption note at health.rs:29-36 is rewritten.
D9 — Texas rows (F5)
Both texas-authz-{education_record,health_record}.json (1.3.0 → 1.4.0) declare i_regional_director and i_unit_match in the
texas-authz-foster_home.json shape and add a Regional director: unit LIST (#1909) row above the supervisor row (scope resolution
for the bound lists), a wildcard-action unit CHECK row, and the state and shared field twins keyed on i_unit_match. Georgia is
unchanged.
D10 — Floor (replaces "no bump")
U1 bumps schema_compat_floor LAST (voluntary under ADR-063 D3, taken): an N-1 writer would insert unsourced rows that nothing can
tell apart from a compliant no-episode row. Replace every replica before traffic; rollback is N-1 images plus a database restore.
D11 — Close-out order
U3 runs plan-lint --done docs/modules/ROOT/pages/plans/archive/child-record-unit-stamp.adoc --allow-open 1909 --allow-open 1912
before merge (#1909 closes with U3’s merge; #1912 is out of scope), and the same with --allow-open 1912 only on main after the
merge, recorded in U3’s closing note.
Units
U1 and U2 Relate to #1909 and close their own unit issues; U3 closes its issue and Closes #1909 (part (a) and the training strike
are done).
| MR | Scope | W |
|---|---|---|
U0 |
this page (Active nav); the epic and U1–U3 issues; a note on #1909 |
1 |
U1 |
SCHEMA, LOCK AND STAMP: the migration (D3, D5, D10 last) and the invariant; D1’s store fns and |
5 |
U2 |
AUTHZ: the builders, the create refs and D6’s check half; the replay check; |
5 |
U3 |
TEXAS AND CLOSE-OUT: D9; |
3 |
Test matrix
| Test | Axis | Unit | Asserts |
|---|---|---|---|
|
happy |
U1 |
both columns set, from the open episode and from the last exited one |
|
sad |
U1 |
NULL stamp; |
|
evil |
U1 |
another child’s placement leaves the stamp unchanged |
|
evil |
U1 |
the retracted episode is skipped |
|
conc |
U1 |
a create parked behind an episode insert holding the child lock sees it |
|
conc |
U1 |
over an exited predecessor, the parked create picks the newer episode |
|
replay |
U1 |
a redelivered event fills once, then changes nothing |
|
conc |
U1 |
the heal waits on the create’s |
|
happy |
U1 |
migration test over windows, a retraction and no episode (NULL/NULL) |
|
sad |
U1 |
a missed heal and a differing stamp are each reported; an unsourced row is not |
|
happy |
U1 |
the floor equals the migration version |
|
sad |
U2 |
the attr key is absent, never |
|
conc |
U2 |
409, no row |
|
evil |
U2 |
a replay whose stored stamp the caller can no longer read is 403 and discloses nothing |
|
conc |
U2 |
the D8 re-check refuses each |
|
conc |
U2 |
same episode, unit filled between check and guard: 409 |
|
happy |
U2 |
NULL rows absent from page and count |
|
sad |
U2 |
404; a soft-deleted target still rejects |
|
happy |
U2 |
read and write checks carry the episode unit |
|
evil |
U2 |
the gate fails; census unchanged |
|
happy |
U3 |
every action and the list scope allowed |
|
evil |
U3 |
403 |
|
sad |
U3 |
403 |
|
happy |
U3 |
the field twins match update |
|
happy |
U3 |
no Georgia verdict moves |
|
evil |
U3 |
zero direct |
Rollout and risks
-
Rollout. U1 bumps the floor: replace every replica before traffic; rollback is N-1 images plus a restore. Bootstrap the Texas rulesets before U3’s image.
-
Risks. Texas health and education routes answer 503 while the units source is not ready. The backfill approximates the create-time pick. Record creates and child-facts writes wait on episode writers through the child lock. Records of children never removed stay NULL.
Erratum (2026-10-09, U3): the Rollout’s "bootstrap the Texas rulesets before U3’s image" reads: promote both files (existing families, rules-promote) over the verified gauge after the craig-placement image carrying U2; U3 ships no image change.
Review log
| Round | Vendor | Resolved model | Verdict |
|---|---|---|---|
1 |
Anthropic — one fresh contextless reviewer (the page, the rules, the four questions) |
Claude Opus 5.5 |
no-ship — 3 blockers fixed (ChildPath carries the checked episode; the invariant’s scope; U3 closes #1909); 10 minors folded |
2 |
maintainer-run on another vendor’s model (2026-10-09) |
OpenAI GPT Astra |
no-ship — 8 findings fixed: the replay checks the stored stamp; ChildPath carries a Copy episode id and unit presence with RoutePath unchanged; a child-level lock serializes the selection; the floor bump removes N-1 writers so the invariant checks sourced rows only; the child-facts guard compares presence and reject is re-checked; the LIST test asserts zero direct sites; the close-out order is explicit |