canopy-rules Data Model

Cross-link: canopy-rules API Reference · Source: migrations/

Tables

Table Purpose

rule_evaluations

Per-evaluation audit row. One row per audited POST /v1/evaluate (EVALUATE, crates/canopy-contracts-rules/src/paths.rs): a ?audit=false dry run (EvaluateParams::audit → EvalMode::audit, services/canopy-rules/src/api/mod.rs) writes no row and no outbox event, and an audited one hands the row to the bounded off-hot-path EvaluationAuditor (#1296) so the response never waits on the INSERT (RulesEngine::evaluate, services/canopy-rules/src/engine.rs). Carries rule_set_name (text reference to the JDM file served from the filesystem via the NamedFilesystemLoader, not a foreign key — see ADR-003 / jdm-ruleset-rewrite Step 2.5), an optional (context_type, context_id) pair that callers use to tie an evaluation to a determination / case / application, the full input and output JSONB envelopes, and duration_ms for performance monitoring. This is the canopy-rules audit trail used to replay any historical determination against the same input.

ruleset_corpus_versions

The #680 corpus-version history for non-persisting dry-run replay (ADR-027 §6, ADR-028). One row per (corpus_hash, ruleset_name): the winning JDM content (JSONB) of each corpus version the service has booted with, keyed by the #682 SHA-256 corpus_hash. The current corpus is persisted idempotently (ON CONFLICT DO NOTHING) at startup, before the router serves traffic, so a determination minted in the same boot is immediately replayable (closes the startup race). POST /v1/evaluate?corpus_hash=<hash> replays that exact version from this table; an unknown hash → 422. Append-only — ADR-003 keeps rulesets as code with no runtime authoring path, so this is a record of versions seen, not an editing surface.

event_outbox, event_inbox

The generated ADR-018 / ADR-039 outbox and the generated consumer inbox, single-sourced in crates/canopy-mq/outbox-migrations/ — schema, indexes and migration files are described once in the data-models index. canopy-rules publishes through the outbox (canopy_mq::Publisher in services/canopy-rules/src) but runs no Subscriber (no .subscribe( call in services/canopy-rules/src), so event_inbox exists here unused.

idempotency_keys

The fleet-wide idempotent-POST cache (#434 / #1463), created outside this service’s migrations/ directory and described once in the data-models index.

rule_sets no longer exists. The original schema (20260326000000) provisioned it as a startup-cache table that auto-imported JDM files; the JDM ruleset rewrite (see the JDM ruleset rewrite plan, Step 2.5) replaced it with direct filesystem serving via a NamedFilesystemLoader wrapping zen-engine’s FilesystemLoader. Migration 20260412000000 drops the now-vestigial table; rule_evaluations.rule_set_name is intentionally a TEXT column (not an FK) so the drop is safe for the audit trail.

Relationships

No table in canopy_rules references another: the audit trail, the corpus-version history and the generated outbox/inbox are independent. ruleset_corpus_versions is keyed by corpus_hash; rule_evaluations carries no corpus hash at all — its columns are id, rule_set_name, context_type, context_id, input, output, duration_ms and created_at (20260326000000_create_rules_tables.sql; ADR-006 Amendment 2) — so the two tables share no value, there is no FK, and there is no ER diagram to draw. The corpus an evaluation ran against is returned on the wire as EvaluateResponse::corpus_hash (crates/canopy-contracts-rules/src/rule_sets.rs), not persisted here.

Cross-service FKs (ADR-001 boundary)

canopy-rules holds no Postgres-level foreign keys to other services. rule_evaluations.context_id is an opaque UUID supplied by the caller (canopy-snap, canopy-tanf, canopy-medicaid, canopy-caps, canopy-wic, canopy-eligibility); canopy-rules makes no attempt to validate it against any other database. The rule_set_name column is also unenforced — it refers to a logical name in the filesystem ruleset loader, not a DB row. This is intentional: rulesets evolve via JDM file revisions in the configuration layers, and the audit trail must remain readable even after a ruleset is renamed or retired. event_outbox likewise holds no cross-service FKs — its payload is a self-contained JSON event envelope routed by routing_key, with no DB-level reference to any consumer.

Retention

Retention floors (eCFR, checked 2026-09-16): a rule_evaluations row must outlive every determination that cites it, so the program floors govern — SNAP program records no less than 3 years from the month of origin of each record (7 CFR 272.1(f)); Medicaid the active case plus a minimum of 3 years (42 CFR 431.17(c)(1)) and fiscal records 3 years from submission of the final expenditure report, longer while audit findings are unresolved (42 CFR 433.32(b)–(c)); TANF the HHS award-record floor of 3 years from the final financial report (2 CFR 200.334, applied to HHS awards by 2 CFR 300.1 — 45 CFR part 75 was removed effective 2025-10-01, 89 FR 80055); CCDF 3 years from submission of the financial reports (45 CFR 98.90(e)). State series, one per program (Georgia Archives, checked 2026-09-18): DHS retention schedule (agency 144) — https://www.georgiaarchives.org/records/agencyprint/144 — series 1976-0110 "Food Stamp Administrative Files" (3 years) for SNAP, 1973-0476 "Public Assistance Authorization Files" (3 years or until resolution of audit, whichever is longer) for TANF and 1982-0547 "Public Assistance Client Medical Eligibility Closed Case Files" (Central Medical Eligibility Office, 3 years after the client is no longer eligible for assistance) for Medicaid; DECAL retention schedule (agency 157) — https://www.georgiaarchives.org/records/agencyprint/157 — series 0469-06-003 "Childcare and Parent Services (CAPS)" (3 years after the end of the calendar year) for CCDF. Each state series matches its federal floor’s 3-year length and differs only in trigger, so the later-ending of the state and federal windows governs a rule_evaluations row. In practice rule_evaluations and ruleset_corpus_versions are append-only and retained indefinitely; archive moves are operator-driven, never migration-driven (ADR-016).

Indexes

  • idx_rule_evaluations_rule_set_name — per-ruleset evaluation listing (regression analysis, audit replay)

  • idx_rule_evaluations_context — composite (context_type, context_id) for retrieving the evaluation history of a specific determination

  • ruleset_corpus_versions PK (corpus_hash, ruleset_name) — the idempotent ON CONFLICT DO NOTHING boot insert and the ?corpus_hash= replay lookup

  • generated event_outbox_* / event_inbox_* indexes — described once in the data-models index

Migration files

The eight event_outbox / event_inbox files (20260508000000, 20260516000000, 20260518004851, 20260713000000, 20260720000000, 20260803000000, 20260815000000, 20260904000000) are generated copies of crates/canopy-mq/outbox-migrations/ (cargo xtask outbox-migrations --write, parity-gated by --check); they are described once in the data-models index. The service-owned files, in timestamp order:

  • 20260326000000_create_rules_tables.sql — original schema (rule_sets startup cache + rule_evaluations audit trail) with the per-rule-set-name and per-context indexes

  • 20260412000000_drop_rule_sets_table.sql — drops the rule_sets cache table after the JDM ruleset rewrite moved ruleset serving to the filesystem via NamedFilesystemLoader; rule_evaluations is untouched

  • 20260626000000_create_ruleset_corpus_versions.sql — #680 corpus-version history table, primary-keyed (corpus_hash, ruleset_name) for idempotent boot insert and corpus-version replay

  • 20260805000000_v7_pk_defaults.sql — #1173: drops the gen_random_uuid() (UUID v4) DEFAULT on rule_evaluations.id; the application binds a v7 id at every INSERT, so a missed bind now fails loud instead of minting a time-unordered v4

All migrations are forward-only per ADR-016.

Edit this page · default