Cross-Service Reconciliation

Why

CRAIG’s services own their own Postgres databases (per the OpenStack-style independence pattern), but cross-service references travel as UUIDs in JSONB / FK-shaped columns rather than database-level foreign keys. A row in placement.placements with case_id = X is asserting that cases.cases.id = X exists, but Postgres can’t enforce that across DB boundaries.

Drift happens. Dual-writes, missed events, manual SQL fixups, partial restores, half-merged migrations — any of these can leave a row pointing at a target that no longer exists. Without an eyes-on signal, that drift compounds silently.

craig-manage reconcile (ADR-022 §D7 / platform-stabilization Step 11; an xtask verb until #3131) is the eyes-on signal: a read-only walker that enumerates every documented cross-service UUID reference and reports the orphans. No --fix flag — orphans need human triage; auto-deletion would destroy evidence of the underlying drift.

Usage

$ craig-manage reconcile
=== Cross-service reconciliation (ADR-022 §D7 / Step 11) ===
  OK   cases.reports.partner_id → security.partners.id | scanned=12 orphans=0
  OK   cases.report_persons.linked_by → (Keycloak sub) | scanned=0 orphans=0
  OK   placement.placements.case_id → cases.cases.id | scanned=18 orphans=0
  …
  OK   financial.subsidy_agreements.placement_id → placement.placements.id | scanned=4 orphans=0

  Report: test-results/reconciliation/2026-10-29.md

The walk prints one line per selected reference: 13 with the default scope (the elided lines above follow the table under References tracked).

The exit code is zero only when every selected reference was walked and none has an orphan:

  • A reference with orphans prints FAIL <from> → <to> | scanned=N orphans=M.

  • A reference that could not be walked (its database is unreachable, or its query no longer matches the schema) prints FAIL <from> → <to>: <error chain>.

Either way the report is written first, then the command exits non-zero with a message counting the orphans and the references not walked. Each database connect gives up after 5 seconds, so an unreachable server fails every reference in about a minute instead of stalling.

Databases

Each database a selected reference touches is named by its flag (--cases-database-url, --exchange-database-url, --financial-database-url, --placement-database-url, --security-database-url), else by its service’s own CRAIG_<SERVICE>__DATABASE_URL. There is no default: a sweep with one of them unnamed is refused before anything connects, naming the flag and the variable. Only the databases the selection touches are needed, so a narrow --scope needs fewer. Every session refuses writes (default_transaction_read_only), and a failed connect names the host and port, never the URL.

Filtering

--scope defaults to all. Pass a comma-separated list of reference keys (the first column of the §D7 table — report_to_partner, placement_to_case, etc.) to walk a subset; the selected references are walked in table order, each once:

$ craig-manage reconcile --scope placement_to_case,placement_to_child

An unknown or empty key (a typo, --scope "", --scope ",", a trailing comma) is refused before anything connects, even beside valid keys: the error names each such key, escaped, and lists the valid keys.

Output path

--output <path> overrides the default location (test-results/reconciliation/<UTC-date>.md).

Reading the report

The markdown report opens with three counts (references selected, walked, and not walked; the walked count says how many were count-only) and a verdict line. "No orphans found across N reference(s)" appears only when every selected reference was walked, with N the number checked against a target database; an incomplete sweep says "Incomplete sweep" instead. Then:

  • Not walked: each reference that could not be walked, with its error chain.

  • Count-only (no orphan check): each external-target reference walked, with its source-row count; no orphan check ran.

  • When any orphan was found, one section per checked reference with its scanned-row count, orphan count, and a (source_id, missing_target_id) table for triage.

Triage flow per orphan:

  1. Reproduce in dev if possible — restore a prod snapshot or replicate the steps that produced the row.

  2. Find the producing event in the audit log (craig-security audit_log). The event payload + timestamp tell you whether the FK was wrong at write time or whether the target row was later deleted.

  3. Decide:

    • Target was deleted by an explicit operation: the orphan represents broken referential intent. Either restore the target or null the FK with operator approval.

    • Target was never written: the orphan is upstream-bug evidence. File an issue against the producing service.

    • Reference is stale by design (e.g. archived target retained by policy): the reference table in tools/craig-manage/src/reconcile.rs should be updated to mark this case allow-listed, not the row deleted.

There is no "auto-fix" because each orphan’s correct resolution depends on context the tool cannot recover.

When to run

The walk runs on demand; no CI job runs it. It reaches whichever databases it is given (Databases), a deployment’s included, and only reads.

  • On demand, before a release or after a change to a migration or a flow that writes one of the references below.

  • After a SQL fixup applied to the devstack, to confirm it left no reference dangling.

A devstack that has run the test battery carries fixture orphans by design (for example, services/craig-placement/tests/api/afcars_export_seed.rs commits removal episodes whose cases are not in craig_cases), so the walk is meaningful right after cargo xtask dev reseed.

References tracked

The current §D7 table (live in tools/craig-manage/src/reconcile.rs) has 13 references: 12 checked against a target database and one count-only external reference (report_persons_linked_by).

Key From To Notes

report_to_partner

cases.reports.partner_id

security.partners.id

Public-intake report ↔ partner identity

report_persons_linked_by

cases.report_persons.linked_by

(Keycloak sub)

External target — count only, no orphan check (Keycloak is not queried)

placement_to_case

placement.placements.case_id

cases.cases.id

The single most-load-bearing cross-service reference

placement_to_child

placement.placements.child_id

cases.persons.id

Child identity owned by cases; placements reference

removal_episode_to_case

placement.removal_episodes.case_id

cases.cases.id

The removal episode’s owning case (#2036). Also covers an episode with no arrangement yet, which the placements rows cannot see. Never deleted, and case_id / child_id never change (refuse_episode_row_mutation), so every row is walked, retracted episodes included

removal_episode_to_child

placement.removal_episodes.child_id

cases.persons.id

The removed child (#2036)

payment_to_case

financial.payments.case_id

cases.cases.id

Placement-origin payments are allocated against a case; subsidy-origin rows (#1068) may have none (case_id nullable — the walker’s IS NOT NULL filter skips them by design)

payment_to_placement

financial.payments.placement_id

placement.placements.id

Placement-origin payments only — subsidy-origin rows (#1068) have a NULL placement_id by payments_origin_exclusive; the walker’s IS NOT NULL filter skips them by design

icpc_to_case

exchange.icpc_requests.case_id

cases.cases.id

ICPC request originates from a case

subsidy_agreement_to_case

financial.subsidy_agreements.case_id

cases.cases.id

Subsidy agreement’s owning case (#1067; existence-only — semantic consistency is the creating flows' job)

subsidy_agreement_to_child

financial.subsidy_agreements.child_id

cases.persons.id

The subsidized child

subsidy_party_to_person

financial.subsidy_agreement_parties.person_id

cases.persons.id

Agreement signatory (caregiver/co-caregiver)

subsidy_agreement_to_placement

financial.subsidy_agreements.placement_id

placement.placements.id

Anchoring placement (ERR evidence linkage)

Adding a new reference: append a row to the REFERENCES array in tools/craig-manage/src/reconcile.rs, supply the source query + target table/column, and the test suite enforces the schema invariants (unique key; external targets have to_table = None; each row’s from names the craig_<service> database, table and column its query reads, and an internal to names its target triple) and pins the row count, which the new row bumps.

Edit this page · latest