Plan: Child-Keyed Records Carry the Custody Unit (#1909 b)

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_id NOT NULL, no FK; placement_id nullable FK) and health_records (child_id only) carry no case_id, no admin_unit and no trigger (craig-placement/migrations/20260321000000_*, …001_*).

  • Writers. create_education (api/education.rs:148, replay branch :218) and create_health (api/health.rs:69, replay :136) answer a replay with the stored row and no check; updates store/education.rs:200, store/health.rs:223 (return the row); proposal accept store/governed.rs:175-183 (returns ()), whose lock_target (:140-150) returns only whether the row exists; reject field_proposal_decide.rs:253 mutates 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) and ChildPath::authorize (child_facts/acts.rs:127; RoutePath: Copy + 'static, authorize(self, &Ctx, …) with Ctx.app the 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) treat ScopeFilter::Units as 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_mutation clause 3, :114-118). Its writers are insert_episode, set_removal_at, set_exit_at and retire_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 invariant craig-cases/migrations/20260916140000_investigation_admin_unit.sql:30-43 and craig-test-lib/sql/invariants/craig-cases/investigation_admin_unit_from_referral.sql; builders placements.rs:59, :114; birth stamp under FOR SHARE placements.rs:1026; list bind store/placements.rs:656, :705; heals inbox/case_assignment.rs:86-144, backfill_cross_service_assignment.rs:387-391; the delete lock-and-compare home_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 in store/removal_episodes.rs return Option<(episode_id, admin_unit)>: latest_live_episode_for_child(executor, child_id) (unlocked) and latest_live_episode_for_child_shared(tx, child_id) (FOR SHARE).

  • Serialization. lock_child_episodes(tx, child_id) takes pg_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 _shared read. 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_id never sources the unit: it is caller-supplied and unchecked against child_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 (severity high, 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_ref and health_create_ref replace nil_record_ref for checks and the field consult. The handler derives (episode_id, unit) unlocked for the check. Inside the transaction: the child lock, then the _shared read. A different episode answers 409 concurrent-modification and 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. TargetRef loads 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> and unit_filled: bool, set by an async ChildPath::resolve (D1 unlocked) where the path is built (child_facts/acts.rs:308-309, :349-350). authorize reads that episode’s unit through ctx.app and carries it, or leaves the key absent when unit_filled is false. guard takes the child lock, runs the _shared read 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. RoutePath is 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 lock_child_episodes with every episode-writer caller; the creates' in-transaction stamp (D6 write half); the heal (D4) in inbox/case_assignment.rs and the backfill xtask; the seed (tools/craig-seed datagen.rs, sql_row_impls.rs; reseed before the first push); data-model-placement; Added with the ops note (floor, replica order, rollback)

5

U2

AUTHZ: the builders, the create refs and D6’s check half; the replay check; TargetRef; ChildPath; the three list binds; D8; the gate (UNIT_BEARING_TYPES 13 → 15; the nil-id census stays at eight); list_scope_units_tests.rs (the three Denied pins become binds on purpose); quality-gates; ADR-077 D5/D6 and ADR-060 amendments; Changed

5

U3

TEXAS AND CLOSE-OUT: D9; engine.rs TEXAS_UNIT_FILES 12 → 14; default_rulesets.rs lists and legs; a live leg in regional_check_legs_tests.rs; rulesets.adoc; ADR-077 D7 amendment; Changed (the Texas regional access and its 503 exposure); Status Done, the archive move and row; D11

3

Test matrix

Test Axis Unit Asserts

a_new_record_takes_the_latest_live_episode_unit

happy

U1

both columns set, from the open episode and from the last exited one

no_episode_or_a_null_unit_stamps_null

sad

U1

NULL stamp; unit_episode_id set only when an episode exists

the_placement_id_never_sources_the_unit

evil

U1

another child’s placement leaves the stamp unchanged

a_retracted_episode_never_sources_the_unit

evil

U1

the retracted episode is skipped

a_create_racing_a_first_episode_is_serialized

conc

U1

a create parked behind an episode insert holding the child lock sees it

a_create_racing_a_newer_episode_is_serialized

conc

U1

over an exited predecessor, the parked create picks the newer episode

the_heal_fills_once_and_never_overwrites

replay

U1

a redelivered event fills once, then changes nothing

a_heal_racing_a_create_never_strands_null

conc

U1

the heal waits on the create’s FOR SHARE; the record ends stamped

the_backfill_picks_the_episode_by_removal_at

happy

U1

migration test over windows, a retraction and no episode (NULL/NULL)

the_invariant_flags_a_sourced_row_that_disagrees

sad

U1

a missed heal and a differing stamp are each reported; an unsourced row is not

the_migration_bumps_the_floor_last

happy

U1

the floor equals the migration version

the_refs_carry_the_stamp_and_omit_a_null

sad

U2

the attr key is absent, never Null

a_create_after_the_episode_changed_is_refused

conc

U2

409, no row

a_replay_is_answered_only_if_the_stored_record_is_readable

evil

U2

a replay whose stored stamp the caller can no longer read is 403 and discloses nothing

an_update_delete_accept_or_reject_after_a_fill_is_refused

conc

U2

the D8 re-check refuses each

a_child_facts_write_after_a_fill_is_refused

conc

U2

same episode, unit filled between check and guard: 409

the_three_lists_bind_units

happy

U2

NULL rows absent from page and count

a_decide_on_an_absent_record_is_404

sad

U2

404; a soft-deleted target still rejects

child_facts_checks_carry_the_derived_unit

happy

U2

read and write checks carry the episode unit

a_unit_less_record_literal_fails_the_gate

evil

U2

the gate fails; census unchanged

texas_regional_in_unit_child_records

happy

U3

every action and the list scope allowed

texas_regional_out_of_unit_or_roleless_refused

evil

U3

403

texas_regional_unstamped_refused

sad

U3

403

texas_field_writes_mirror_update

happy

U3

the field twins match update

georgia_ignores_the_unit

happy

U3

no Georgia verdict moves

child_record_lists_resolve_scope_only_through_auto_scope_list

evil

U3

zero direct check(List) sites on the two types in services/craig-placement/src, so the LIST marker row only ever resolves scope

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

Edit this page · latest