canopy-rules API Reference

Overview

Cross-link: canopy-rules Data Model (#419)

Manages JDM (JSON Decision Model) rulesets used by all program services for eligibility logic (ADR-003). Rulesets are loaded from disk on startup (not created over HTTP); the API provides read access to the loaded rulesets plus a generic evaluation endpoint that any service can call.

Base URL

container port 8001 (host: CANOPY_PORT_CANOPY_RULES_8001 in .ports.env); every route below is mounted under /v1

Authentication

Bearer token: a canopy-identity token for this receiver (its aud is urn:canopy:service:canopy-rules), authenticated by the service’s identity receiver (ADR-049 Amendment 14): no bearer is 401 token_missing, a token it refuses 401 token_rejected, an X-Canopy-Actor header 400 actor_header_retired, and no trustworthy key set in time 503 keys_unavailable.

Access

canopy-caps, canopy-eligibility, canopy-medicaid, canopy-snap, canopy-tanf and canopy-wic hold a system edge to canopy-rules; canopy-cli holds a delegation edge (canopy rules, canopy CLI Reference). Every route admits a system caller; the ruleset reads and POST /v1/evaluate also admit a caseworker+ worker through canopy-cli, and GET /v1/evaluations and GET /v1/corpus are system-only. A refusal is 403 with its ReceiverDenial code (ADR-049 Amendment 14); each route’s constant is in the authorization inventory.

Swagger UI

/swagger-ui on the same host port

Database

canopy_rules

Endpoints

GET /v1/rule-sets

List the rulesets known to the loader, with pagination and search.

Access

system; caseworker+ via canopy-cli (delegated::RULES_GET_RULE_SETS)

Query parameters:

Parameter Type Default Description

limit

integer

200 (max 500)

Page size

offset

integer

0

Offset for pagination

search

string

—

Filter by ruleset name (substring match)

Response (200): Array of RuleSetSummary (logical ruleset names only).

[
  { "name": "snap-eligibility" },
  { "name": "georgia-snap-deductions" }
]

GET /v1/rule-sets/{name}

Get a single ruleset’s JDM document by its logical name. Reads the backing JDM file from disk.

Access

system; caseworker+ via canopy-cli (delegated::RULES_GET_RULE_SETS_NAME)

Path parameters: name — logical ruleset name (the name field inside the JDM file).

Response (200): Raw JDM document (nodes/edges object) for the ruleset.

Response (404): Rule set not found.

POST /v1/evaluate

Evaluate a ruleset against input data. This is the primary endpoint called by program services during eligibility determination.

Access

system; caseworker+ via canopy-cli (delegated::RULES_POST_EVALUATE)

Query parameters:

Parameter Type Default Description

trace

boolean

false

When true, the response includes a node-by-node execution trace (for debugging broken expressions) and the typed derivation_edges folded from it (#679).

corpus_hash

string

—

#680: pin the evaluation to a stored corpus version for replay. Omitted (or equal to the live corpus hash) evaluates the live corpus; a stored non-live hash replays that exact ruleset version from ruleset_corpus_versions and the response corpus_hash echoes the pinned value; an unknown hash → 422.

audit

boolean

true

#680: when false, the evaluation is ephemeral — it writes no rule_evaluations audit row and stages no rules.evaluated event (the write-free dry-run path, ADR-027 §6).

Request: EvaluateRequest

{
  "rule_set_name": "snap-eligibility",
  "context_type": "household",
  "context_id": "uuid-of-household",
  "input": {
    "gross_monthly_income": 1500,
    "household_size": 3,
    "has_elderly_disabled": false
  }
}

Response (200): EvaluateResponse — structured output from the ruleset’s output node, the wall-clock duration_ms, and (when trace=true) a node-by-node trace plus derivation_edges: a typed list of RuleFirings (the per-node rule_ref + namespaced input fields + declared outputs with their values), folded from the trace joined to the parsed ruleset graph. The program services rewrite these into the determination snapshot’s derivation_graph (#679, ADR-028 Amendment 2). Both fields are null when trace was not requested.

Numbers in output and trace keep their fractions (#2926). An integral value is a JSON integer, and a fractional one is a JSON number at f64 precision (about 15 significant digits, so every cent figure exactly). A rule that computes 412.38 answers 412.38. This is zen-engine’s own serialization since 2.0; under 0.55, which truncated every fractional number to a whole one, canopy-rules wrote the numbers itself to the same result.

{
  "output": { "eligible": true, "allotment": 535 },
  "duration_ms": 4,
  "trace": null,
  "derivation_edges": null
}

Response (404): Rule set not found.

GET /v1/evaluations

List recent evaluations (audit trail).

Access

system (inline Admission::system()); a user is 403 system_caller_required

Query parameters:

Parameter Type Default Description

limit

integer

50 (max 200)

Page size

offset

integer

0

Offset for pagination

search

string

—

Filter by ruleset name (substring match)

Response (200): Array of RuleEvaluation objects (id, ruleset name, input, output, duration_ms, created_at, optional context type/id).

GET /v1/corpus

The live ruleset-corpus content hash (#1469; ADR-002 Amendment 1). A bulk cohort run resolves this once at enact time as the corpus half of its pinned policy target; the value equals the corpus_hash every unpinned POST /v1/evaluate response echoes, and is persisted in ruleset_corpus_versions at boot so a later pinned replay always resolves.

Access

system (inline Admission::system()); a user is 403 system_caller_required

Response (200): CorpusInfo — { "corpus_hash": "<hex sha-256>" }.

Error Codes

Code Meaning

401

No bearer (token_missing) or a token the receiver refuses (token_rejected)

403

The route’s admission refuses the caller: system_caller_required (a user on GET /v1/evaluations or GET /v1/corpus), actor_not_allowed (a user through a client other than canopy-cli), required_role_missing (a canopy-cli user without a caseworker+ role)

404

Ruleset not found (GET /v1/rule-sets/{name}, POST /v1/evaluate)

422

POST /v1/evaluate?corpus_hash= names a corpus version this service never stored (CorpusUnavailable, #680)

500

Internal error reading/parsing a JDM file or evaluating a ruleset. An evaluation whose output or trace does not serialize to JSON is one of these, never a 200 with a null output (#3602)

503

keys_unavailable: the receiver holds no trustworthy key set in time. Otherwise a transient overload — the DB connection pool was exhausted (PoolTimedOut) while loading a pinned corpus version; retryable, not a defect (#1296). Callers (e.g. canopy-medicaid /v1/determine) should retry rather than fail the determination.

Notes

  • Rulesets are loaded at startup from the configuration layers CANOPY_CONFIG_SOURCE names (NamedFilesystemLoader::from_snapshot in services/canopy-rules/src/engine.rs; Configuration Reference › Configuration source): every layer’s top-level *.json JDM file, lowest layer first. A higher layer’s decision replaces a lower layer’s of the same embedded name only when the higher layer’s manifest lists that name under overrides; an undeclared collision, or two files of one layer carrying one name, refuses the start, naming both files (#3372; declared overrides). rulesets_dir and federal_rulesets_dir are retired

  • Evaluations run on a pool of pinned worker threads (tokio_util::task::LocalPoolHandle, engine.rs) because zen-engine futures are !Send; the pool size is eval_workers (CANOPY_RULES__EVAL_WORKERS; 0 = auto, clamp(2..=16) of available_parallelism)

  • Audited evaluations (the audit=true default) record a rule_evaluations row + a rules.evaluated event, written best-effort off the request path since #1296 — so GET /v1/evaluations is eventually consistent, and the telemetry write can never exhaust the pool or fail an evaluation. This is internal service telemetry, not the compliance audit-of-record (ADR-019: auditors read canopy-eligibility per-case views)

  • See ADR-003 for the design rationale

Edit this page · default