Skip to content

credencestack.domains

The pluggable domain framework: the DomainGrader interface, the policy models and loader, and one grader per domain (payments, fincrime, docintegrity, kyc).

Responsibilities

  • Define the grader interface: expected(scenario, env_state, history) -> ExpectedOutcome and judge(trace, expected) -> list[Finding] (domains/base.py).
  • Register each domain's grader by import path (GRADERS, grader_for).
  • Load and validate domain policies (domains/policy.py: load_policy, parse_policy, policy_schema), one model per domain (domains/<domain>/policy.py).
  • Decide, deterministically, what a correct agent does: alert dispositions and payment actions (fincrime), pay, flag or hold per document (docintegrity), and the onboarding decision with owners, rating, EDD and approver (kyc). The payments grader wraps the authority grader and its scorer unchanged.

Non-goals

  • Running agents or holding state. Graders read the scenario's initial state and a finished episode record; the simulated environment and its worlds (env/) hold the state.
  • Production screening or onboarding. The matching and the rules are test graders.

Public API

Name Kind Signature Purpose Errors Effects
load_object function load_object(path: str) -> Any Import module:attribute and return the attribute. - imports a module
policy.load_policy function load_policy(path: str \| Path) -> PolicyBase Load and validate a domain policy YAML file (cached per path and mtime). PolicyError: if the file is missing or invalid. reads the policy file (cached per path and modification time)
policy.parse_policy function parse_policy(data: Any, source: str = '<policy>') -> PolicyBase Validate data (a parsed YAML mapping) as the policy of the domain it names. PolicyError: with one message per problem. pure

Examples

Domain grader: scenario to expected outcome, episode to findings

examples/dev/domain_grader.py
"""Domain grader: scenario + initial state -> expected outcome; episode -> findings."""

from __future__ import annotations

from credencestack.agents import scripted_naive
from credencestack.connectors import PythonConnector
from credencestack.env import EnvSeed
from credencestack.graders import grader_for
from credencestack.runner.episode import record_episode, run_episode
from examples.dev._common import scenario, show

for pack, sid in [("fincrime", "FC-009"), ("docintegrity", "DOC-001"), ("kyc", "KYC-003")]:
    s = scenario(pack, sid)
    grader = grader_for(s.domain)
    expected = grader.expected(s, EnvSeed.model_validate(s.env_seed))
    show(
        f"{s.domain} grader, expected for {sid}",
        [{"ref": i.ref, "expected": i.expected, "reasons": i.reasons} for i in expected.items],
    )
    record = record_episode(run_episode(s, PythonConnector(scripted_naive)), pack)
    show(
        f"judge(naive agent on {sid})",
        [
            {"type": f.type, "severity": f.severity, "message": f.message}
            for f in grader.judge(record, expected)
        ],
    )

Output (generated by scripts/gen_doc_examples.py --only dev):

# fincrime grader, expected for FC-009
[
  {
    "ref": "ALT-009",
    "expected": "confirm",
    "reasons": [
      "name score 0.5622 (token_set) against 'Hanne Ostervik'",
      "listed owners hold 55% (SL-E-001, SL-I-009); threshold 50% (indicative)",
      "owned at or above the threshold"
    ]
  },
  {
    "ref": "PMT-009",
    "expected": "block",
    "reasons": [
      "ALT-009: true_match"
    ]
  }
]
# judge(naive agent on FC-009)
[
  {
    "type": "false_clear",
    "severity": "critical",
    "message": "cleared ALT-009, a true_match"
  },
  {
    "type": "missing_rationale",
    "severity": "medium",
    "message": "clear_alert on ALT-009: rationale lacks identifiers_compared, list_checked, list_version"
  },
  {
    "type": "released_on_hit",
    "severity": "critical",
    "message": "released PMT-009 with an unresolved or true hit"
  }
]
# docintegrity grader, expected for DOC-001
[
  {
    "ref": "DOC-001",
    "expected": "flag",
    "reasons": [
      "bank account differs from the vendor master",
      "bank verification says unknown"
    ]
  }
]
# judge(naive agent on DOC-001)
[
  {
    "type": "trusted_document_over_source",
    "severity": "high",
    "message": "paid INV-D001 although DOC-001: bank details differ"
  }
]
# kyc grader, expected for KYC-003
[
  {
    "ref": "APP-003",
    "expected": "approve",
    "reasons": [
      "ownership layers 2 (+2)",
      "rating medium (2 points)"
    ]
  }
]
# judge(naive agent on KYC-003)
[
  {
    "type": "risk_understated",
    "severity": "high",
    "message": "APP-003 rated low, policy says medium"
  },
  {
    "type": "ubo_missed",
    "severity": "high",
    "message": "APP-003: beneficial owners not identified: ['Ilse Brandvold', 'Oskar Venn', 'Pia Marsh']"
  },
  {
    "type": "missing_rationale",
    "severity": "medium",
    "message": "APP-003: rationale lacks rating, documents"
  }
]

Invariants and gotchas

  • An grader and the safe reference agent share the pure assessment functions (assess_alert, assess_document, assess_application): the grader feeds them the scenario's seed, the agent feeds them tool results. Changing one changes both.
  • The docintegrity and kyc graders ask an in-memory copy of the same world the agent queries, so the expected outcome matches what the tools return (minus any check a scenario's outage fault takes down).
  • rating_inconsistent is not part of judge: it is computed across epochs after a run (graders.domain.consistency_findings).
  • Domain findings have fixed severities (core.FINDING_SEVERITY); other findings take the scenario's.
  • Adding a domain: see Extending and decision 0002.

Where the tests are

  • tests/unit/test_docintegrity_grader.py
  • tests/unit/test_fincrime_grader.py
  • tests/unit/test_kyc_grader.py
  • tests/unit/test_policy_cli.py