Data Model: Rules Engine

Overview

The rules engine stores JDM (JSON Decision Model) rule sets and a full audit trail of every evaluation. Rule sets are cached in memory at startup; at runtime the cache converges on the database through identity-aware conditional applies keyed on the trigger-enforced (id, revision) pair (#1188 / the ADR-006 #1188 amendment) — driven by RabbitMQ rules.cache_invalidated events from peers AND a periodic reconcile sweep (CRAIG_RULES__DECISION_REFRESH_SECONDS) that bounds staleness when an event is lost.

The fleet-standard platform tables also live in this schema and are documented centrally: event_outbox / event_inbox / event_inbox_collisions (ADR-022 and the ADR-062 §A attempt machine), request_claims (ADR-062 §B), and schema_compat_floor (ADR-063). A claim’s actor is the ADR-085 D3 column pair (#2740): request_claims.created_by_sub holds a worker’s UUID and created_by_sub_service a service’s service:<name> encoding, with a CHECK that exactly one is set.

Recorders name the acting worker (#2321). No craig-rules recorder (a who-acted column or an event’s actor key) names the request token’s own sub / preferred_username: under a BFF-lifted token that token is craig-web’s service principal, so a staff write names claims.acting_worker() or the claim’s effective actor (ADR-028, ADR-062), the lifted worker, or the token’s own subject when nothing is lifted. That covers rule_sets.created_by / created_by_name / updated_by / updated_by_name (a soft delete restamps both halves, #2320), rule_set_snapshots.actor_sub / actor_display, and the rules.ruleset_promoted / rules.ruleset_rolled_back user_id, beside rule_evaluations.evaluated_by (#1060). Each name column is the acting worker’s username. Since #2522 the Claims fields are private and crates/craig-test-lib/tests/acting_identity_census.rs pins it: the token’s own identity is read only through a bearer() accessor, each such read under services/craig-rules/src is pinned with its reason, and a recorded worker is claims.worker_subject(), which reads the claims.acting_worker() view, never the bare claims (#2740 removed parse_sub / parse_actor_sub).

Tables

Table Purpose

rule_sets

JDM rule set definitions — content stored as JSONB, soft-deleted via active flag

rule_evaluations

Audit trail — every evaluation recorded with input, output, context, and evaluator (evaluated_by: the acting principal in the ADR-085 D3 actor encoding, a worker’s UUID or service:<name> for a direct service caller, or the raw sub of claims that name no actor, #2740; the #1130 SINGLE copy; past the archive hot window rows move COLD via #1129/ADR-058 — resolvable forever through archive_ledger + craig-manage archive-fetch)

archive_ledger

#1129 (ADR-058 D8): rules' OWN durable archive ledger — one row per archived batch (object keys, sha256, D9 store identity, the [min, max] id range per D17), written inside the prune transaction; the authority orphan GC consults (the event to security is secondary bookkeeping)

rule_set_snapshots

#1556 (D2): insert-only content history — one row per API-path mutation, same-tx, keyed UNIQUE(rule_set_id, revision) (the cache CAS token), opened by a migration-time baseline row per pre-existing set. Records operation (baseline|create|update|import|promote|rollback|delete), mandatory reason for the consequential three (CHECK), the zen compile vintage, the canonical content sha256, and (promote) the persisted canary report. Audit-class, keep-forever-hot. Since #2954 (ADR-087) a config-managed deployment’s boot reconcile writes rows too (import then promote per changed rule set, delete for one no layer supplies; actor_sub config:<layer>@<sha>, reason config reconcile <layer>@<sha>), each stamped with config_layers (every loaded layer, [{name, sha}], lowest precedence first) and config_source (the layer whose file supplied the content; NULL on a delete). Both are NULL on an API row. An evaluation reaches its commit by (rule_set_id, rule_set_revision) → this row → config_layers.

rule_set_promotion_previews

#1556 (D2): the promote two-step’s DB-backed evidence — candidate hash + from_revision binding, corpus fingerprint, thin-corpus flag, the categorical diff report. Never burned (a successful execute bumps the live revision, so replays 409 on the CAS). Audit-class, keep-forever-hot.

ER Diagram

rule_setsuuididPKtextnameUKunique rule set nametextversionbigintrevisiontrigger-bumped per-row counter — the (id, revision) cache token (#1188)textdescriptionjsonbcontentJDM decision modelbooleanactivesoft-delete flagtimestamptzcreated_attextcreated_bytimestamptzupdated_attextupdated_bytextupdated_by_namerestamped with updated_by, a soft-delete too (#2320)rule_evaluationsuuididPKuuidrule_set_idFKtextrule_set_nametextrule_set_versionbigintrule_set_revisionnullable — the revision that ran (ؔ NULL = pre-D2)booleanpinnedsnapshot replay (#1556) — non-operative, no rules.evaluated stagedjsonbinputjsonboutputtimestamptzevaluated_attextevaluated_bytextcontext_typenullableuuidcontext_idnullablerule_set_snapshotsuuididPKuuidrule_set_idFKbigintrevisionUNIQUE with rule_set_id — the CAS tokenbigintfrom_revisionnullable — promote/rollback provenancetextnametextversionjsonbcontenttextengine_versionnullable — zen compile vintage (NULL = baseline)textoperationbaseline|create|update|import|promote|rollback|deletetextreasonmandatory for promote/rollback/delete (CHECK)textcontent_sha256nullable — canonical hash (NULL = baseline)jsonbcanary_reportnullable — the promote evidencetextactor_subjsonbconfig_layersnullable — the loaded config layers [{name, sha}] (ஊ NULL = API write)textconfig_sourcenullable — the layer that supplied the file (ஊ NULL = API write or delete)timestamptzrecorded_atrule_set_promotion_previewsuuididPKuuidrule_set_idFKbigintfrom_revisiontextcandidate_versiontextcandidate_sha256textcorpus_fingerprintbooleanthin_corpusjsonbreporttextreasontextactor_subtimestamptzcreated_atevaluated ashistory ofpreviewed against

Key Indexes

  • idx_rule_evaluations_rule_set_id — FK lookup

  • idx_rule_evaluations_context — composite on (context_type, context_id) for audit queries

  • idx_rule_evaluations_evaluated_at — time-range queries

Edit this page · latest