ADR-081: Custody Episodes — Legal-Status History as a Ledger under an Episode Identity Row

Status

Accepted (2026-10-13), landed by U2 (#2342) with the tables, the acts and the events it describes. Proposed (2026-10-13) as U0 of the plan Custody Episodes and the DOE-SLDS Bind (#1804, #1806, #1821), approved in plan mode on 2026-10-13 after two review rounds (epic &107). D1–D4 here are the plan’s D1–D3, condensed. D3’s lock order gains the per-child advisory lock of the plan’s erratum (2026-10-13). D2’s in-day order is § Amendment (2026-11-06, #2353).

Context

CRAIG records no legal status anywhere: only the partner wire crates carry one (crates/craig-partner-doe-slds, 24 CLEGSTAT codes). The nearest facts do not fit. court_orders is case-scoped, names no child and emits no custody fact. Placement’s removal episodes hold placement-and-care, and custody is "a separate craig-cases fact": an in-agency move is a reassignment on the same case (data-model-placement.adoc, #2028).

Four outbound binds need custody (ADR-075). The first, DOE-SLDS, lists a child "whose most recent, active Legal Status is In DFCS custody" and removes the child on the way out. The ADR-075 substrate is stage-shaped: a recipient row is insert-once per (interface, partner, stage_kind, stage_id, person), so a re-entry into custody can only send DOE a new INC under a new stage id. A custody stay therefore needs an identity of its own that never moves while the stay is corrected.

Workers also record legal status outside custody: an initial "not in custody" status, or a later one after custody ends. Corrections must keep history and the stay’s identity; a court order can state the status of the children it names.

Decision

D1 — Vocabulary is bundle data with a custody class

StateBundle::legal_statuses() returns LegalStatusSpec { token, label, custody_class }, where custody_class is the closed neutral CustodyClass { InCustody, NotInCustody }. Georgia contributes its 24 codes; craig-cases resolves the list at boot, and an empty list (a stub bundle) refuses custody acts. The class is stamped on each row at write, so a later reclassification never rewrites history (ADR-072: tier-1 vocabulary stays out of the neutral crates).

  • custody_episodes: a v7 id, the child, one owning case for life, custody_from and custody_until (DATE, inclusive), with an EXCLUDE over live episodes per child, the ADR-079 removal-episode shape (deferred instants, a row guard).

  • legal_status_facts: an ADR-078 event ledger keyed by child-in-case, with episode_id and court_order_id as nullable fact columns corrections inherit, and a stored chain_seq that orders rows within a day: the per-child sequence an original takes under D3’s per-child advisory lock, which its corrections inherit (§ Amendment (2026-11-06, #2353); chain_root_id before). An in-custody row always belongs to an episode; a not-in-custody row either closes one or is history.

  • An episode’s instants follow its live rows. Retiring an episode retires its live rows with it, as episode_retracted, so the rows an outbound removal must report survive any later sweep.

D3 — Acts state their intent

A record without episode_id opens an episode (an overlap is a typed 409); with one, it changes or closes that episode. A not-in-custody record without episode_id is history, refused while the child has an open episode in the case. Court orders carry an optional custody block whose rows take the intent the child’s state implies. Every act is ADR-062 idempotent and follows one lock order: claim, case row, a transaction-scoped advisory lock per named child in child-id order, then the named children’s live episodes in one statement. A child with no live episode has no row to lock, so the advisory lock is what serializes two cases' acts on one child.

D4 — Case lifecycle and events

A database trigger refuses closing or transferring a case while one of its episodes is open, so no binary, old or new, can leave an open episode on a closed case. Events are ids-only, one key per transition: case.custody_opened, _closed, _retracted, _close_corrected, and case.legal_status_changed for every other act.

Consequences

  • An outbound bind addresses one custody stay by a stable id; a re-entry is a new stay and a new stage. The DOE-SLDS legs (#1821) build on this, with the ordering and GTID pinning the plan’s D7 adds in craig-exchange.

  • Two tables and a deferred trigger, against one for a derived model; consumers read episodes, never re-derive them.

  • A same-day exit and re-entry is not representable (inclusive ranges, DATE precision); a re-entry opens the next day at the earliest.

  • Custody is independent of placement’s removal episodes; an advisory cross-check is #2347.

Open questions

  • The custody classes of the 24 codes, and the DOE-side readings the bind relies on, are confirmations asked on #1073. A ruling that moves a class changes new writes only.

Alternatives considered

  • One status ledger, the episode derived as the maximal in-custody run (plan F2 B). Rejected: the stage id drifts when a correction splits or merges a run unless roots are tracked, non-overlap is not a database fact, and every consumer re-derives the episode.

  • Placement’s removal episode as the custody stay (F2 C). Rejected: it contradicts the ruling that custody is a separate cases fact, needs a cross-service reference, and conflates placement-and-care with legal custody.

  • Vocabulary in craig-reference (F1 B, as #1804 was written). Rejected: a Georgia list in a neutral crate (ADR-072).

  • TIMESTAMPTZ instants (F9 B). Rejected: court and CLEGSTAT dates are days; in-day order comes from chain_seq, the per-chain sequence assigned under the per-child lock (§ Amendment (2026-11-06, #2353)).

Amendment (2026-11-06, #2353) — in-day order from a per-chain sequence

Ruled 2026-11-06 (Option A on #2353), recorded with the plan’s erratum of the same date. D2 as accepted ordered a day’s rows by chain_root_id, a handler-minted v7. A v7 is monotonic only within one process: on two craig-cases replicas with clock skew, a same-day close recorded after a change could mint the smaller id, sort first and be refused as custody-row-out-of-order.

A day’s rows now order by (effective_on, chain_seq). chain_seq is a per-child number: an original takes the next one over the child’s rows in every case, read under the per-child advisory lock every custody act already takes (D3), so the order is the order the acts took the lock and no replica clock enters it. A correction keeps its chain root’s number. A per-child unique index over originals makes a writer that skipped the lock fail rather than mis-order. Migration 20261106140000_legal_status_chain_seq back-fills existing rows in their chain_root_id order, so recorded history keeps its order, and bumps the compatibility floor (ADR-063): an N−1 binary writes no chain_seq.

Edit this page · latest