feat(placement): P-B1 U4 — exits: the exit ledger and its three acts, re-entry, the historical episode, left_care
What
Plan D4 (+ D2's historical episode; D5), on U1a's core; bare paths under services/craig-placement/ (src/ implied for code):
exit_responses: an event ledger on the core,FIRST_RECORDED—id·episode_id·exit_at((g)(1)) ·exit_reason·transfer_agency_type· D2's stamps (recorded_at DEFAULT now(), never from a request;first_recorded_atby trigger), under U3's response-row refusal trigger.exit_reason: TEXT, CHECK over theoohc.g3.2022-10-01tokens EXCEPTnot_applicable;transfer_agency_type: theoohc.g4.2022-10-01tokens, NOT NULL iffexit_reason = transfer_to_another_agency(no typed code named). (g)(2) = the jurisdiction-clock date of the live row'sfirst_recorded_at. ColumnsPlaintext.removal_episodes.exit_at: written only by the acts;exit_at > removal_at(400EXIT_BEFORE_REMOVAL);ck_episode_current_instantsextended:exit_atequals the live exit response's, NULL when none (a commit failure is a 500).EpisodeRow.exitserves it.- Routes (claimed; D2's authority + lock order and pair re-check; body instants ≤ now; answer the resulting
EpisodeRow;api/exits.rs; the three exit acts areAction::Updateon thePlacementref):POST /v1/placement/episodes/{id}/exit{client_request_id, exit_at, exit_reason, transfer_agency_type?}: 409EPISODE_HAS_OPEN_ARRANGEMENTwhile a planned or active arrangement exists;exit_at >=every effective arrangement'sended_at(409ARRANGEMENT_OUTSIDE_EPISODE); 409EPISODE_ALREADY_EXITEDwhen a live exit exists.…/exit/{row_id}/corrections(same body): supersede; containment + exclusion re-checked; D2's correction rules (404ledger-row-not-found; 400RESPONSE_AFTER_RECORDING).…/exit/{row_id}/retract{client_request_id, note}(1–1000 chars): re-opens the episode; 409LATER_EPISODE_EXISTSwhen the child has a later episode. A later exit is a new response with its own stamp (R-B18).- Any exit act on a retracted episode: 409
EPISODE_RETRACTED. POST /v1/placement/episodes/historical{client_request_id, child_id, case_id, removal_at, removal_manner, exit_at, exit_reason, transfer_agency_type?}: a MISSED past episode — episode + root removal response + root exit response in ONE tx; stampsnow()(honestly tardy under R14); a root birth (lock_case_assignmentfirst); arrangements follow by U5's historical record.
- Re-entry = U3's open after an exit (
'[)': same-instant exit + re-entry are adjacent). Each exit act publishesplacement.episode_facts_changed(ledger = exit_responses);placement.episode_exitedis U6a's. left_carejoinsPlacementEndReason(crates/craig-reference/src/enums.rs:1172) andchk_placements_end_reason(services/craig-placement/migrations/20260305200000_reference_data_validation.sql:20, widened); exit tokens stay writable until U6c. Not a placement cutover (end_reasonisOption<String>,store/models.rs:87); craig-web's strict decoder (services/craig-web/src/routes/placement/mod.rs:102) first.- Element map:
craig_placement_contracts::afcarsgains (g)(1)–(g)(4). - Seed (draws nothing new): a history row whose end reason is an exit reason closes the first episode (an exit recorded ≤ 30 days after,
left_careon the row); the active row opens a re-entry episode; one bless of the byte pins (tools/craig-seed/tests/sql_byte_identity.rs). History rows drawEND_REASONS(tools/craig-seed/src/datagen.rs:611): exit tokensreunification,adoption,aging_out. - Followers: craig-web exit form; craig-cli
craig placement episode exit…; the backfill rig / SQL invariants / zero-issue pin row (U3–U5). - Decided (the page's 2026-09-24 D2 erratum, and here):
- the historical episode is a root birth under
Action::Createwith the open's membership check (the placement-facts hop — a child whose case membership has ended cannot take one in P-B1), answers 201EpisodeRow, publishesplacement.episode_facts_changedforremoval_episodes/recordedand forexit_responses/recorded, and getscraig placement episode historical(no web form in P-B1); - the seed maps a history row's exit end reason to (g)(3):
reunification→reunify_with_parents_legal_guardians,adoption→adoption,aging_out→emancipation; - craig-security has nothing to roll for U4 (U3's arm and
placement.episodefamily already cover everyledgertoken); the Rollout row's order is kept; - the web end form offers
left_care; - in the U4 → U6a window an arrangement ended
left_careunder a pending SG agreement raises financial's existing non-guardianship WARN (warn_pending_guardianship_arms,services/craig-financial/src/main.rs:1516) — truthful, since the pre-U6b witness requiresend_reason = guardianship; the web guardianship handoff keeps writingguardianshipuntil U6c.
- the historical episode is a root birth under
Acceptance criteria
-
transaction_dates_are_first_recording_timestamps_never_row_creation(exit half);a_retracted_exit_reopens_the_episode;a_same_instant_exit_and_re_entry_are_adjacent;a_missed_past_episode_inserts_before_a_later_one(201EpisodeRow, both events). - Evil: exit at / before removal → 400
EXIT_BEFORE_REMOVAL; create on an exited episode → 409EPISODE_CLOSED; exit with an open arrangement → 409EPISODE_HAS_OPEN_ARRANGEMENT; second live exit → 409EPISODE_ALREADY_EXITED; exit retraction under a later episode → 409LATER_EPISODE_EXISTS; exit act on a retracted episode → 409EPISODE_RETRACTED. -
tests/api/concurrent_transitions.rs: exit vs create — one wins, never both. - DB:
ck_episode_current_instantsrefuses a disagreeingexit_at; the (g)(4) CHECK refuses both mismatches; anexit_responsesUPDATE other than RETIRE and any DELETE are refused. -
left_careaccepted on an ending and decoded by craig-web; exit tokens still accepted. - Element-map tests (hermetic; DB-backed
#[ignore = "requires devstack"]) cover (g)(1)–(g)(4). -
the_seeded_episodes_contain_their_arrangementsextended to exits + re-entry; one bless;verify-seed --expect keyedgreen. - Live: the placement e2e exits and re-enters; web exit form and CLI exit commands work.
- Per new route: evil-corpus, contention, route-role, coverage-matrix,
validate-authz-coverage; battery green. - Docs (D5 row):
data-model-placement.adoc,state-machines.adoc, implementation guide, ADR-003, deployment guide § AFCARS placement capture rollout; CHANGELOGAdded(routes) +Changed(end-reason set).
Context
- Plan page:
docs/modules/ROOT/pages/plans/afcars-placement-capture.adoc(P-B1 of #949, approved in plan mode 2026-09-24, rev 4 — the D-sections cited above are binding; rulings R-B1–R-B21 as recommended). - Program page:
docs/modules/ROOT/pages/plans/afcars-program.adoc(A5, R2, R14). - ADR-079 D2 / D4; ADR-078 D1 (via U1a); ADR-062; ADR-060; ADR-077 D6; ADR-063 D3 (only U3 / U5 are placement cutovers); ADR-003. Builds on U1a, U3.
- Rollout (U4): craig-security, craig-web (
left_care, the exit form) → placement. - ⁂ #1073: R-B18.
- Pre-1.0: every wire / schema change lands with its CHANGELOG
Changed/Removedentry; every new test carries// @axis:(happy|sad|evil|fault|replay|conc|invariant|property|recovery); each destructive migration bumpsschema_compat_floorin its own file and names the rollback (N−1 images AND a restore). - Relates to #1963 (closed) and #949; epic &99.
P-B1 issue map (plan docs/modules/ROOT/pages/plans/afcars-placement-capture.adoc): U1a #2015 (closed) · U1b #2016 (closed) · U2 #2017 (closed) · U3 #2018 (closed) · U4 #2019 (closed) · U5 #2020 (closed) · U6a #2021 (closed) · U6b #2022 (closed) · U6c #2023 (closed) · U7 #2024 (closed) · U8 #2025 (closed); P-B2 #2026 (closed); the P-A2 population issue #2027 (closed); follow-ups #2028 (closed) (intra-agency case move), #2029 (closed) (the hop's case lifecycle), #2030 (closed) (historical per-diem).
Edited by CRAIG-Bot-CA