Rules
groundlens.rules
¶
Rule-based interpretable layer — deterministic, auditable, no LLM.
This module provides a checklist-style rule engine that complements the
geometric SGI/DGI scores with human-readable audit evidence. A trained
auditor or compliance officer can read the textual explanation produced
by a :class:RuleSet evaluation and verify, item by item, why a response
passed or failed.
The rule engine is intentionally rule-based rather than learning-based:
- Deterministic. Same inputs → same outputs, byte-identical.
- Auditable. Every pass/fail decision cites the rule, the weight, and the matched evidence span in the response text.
- No LLM. Pattern matching, substring tests, and regular expressions. Compatible with the no-second-LLM constraint of groundlens.
- Domain-specific. Built-in factories (:func:
banking_rules) exist for regulated domains; custom rule sets can be assembled from :class:ChecklistRuleinstances or loaded from configuration.
Sub-scores follow the structure of compliance rationale evaluation in regulated AI literature: specificity (does the response cite concrete case details?), explanatory linkage (does it explain the reasoning?), and boundary shift (does it state what would resolve the case?). Each is in [0, 1] and aggregated via a non-compensatory geometric mean so a zero sub-score collapses the overall quality signal — a rationale that names parameters but offers no resolution path is not partial credit, it is structurally incomplete.
References
Toulmin, S. E. (2003). The Uses of Argument. Cambridge University Press.
McCarthy, P. M., & Jarvis, S. (2010). MTLD, vocd-D, and HD-D: A validation study of sophisticated approaches to lexical diversity assessment. Behavior Research Methods, 42(2), 381-392.
Karwowski, J., et al. (2024). Goodhart's Law in Reinforcement Learning. ICLR 2024.
De la Chica Rodríguez, J. M., & Martí-González, C. (2026). Mechanical Enforcement for LLM Governance. arXiv:2605.14744.
Classes¶
RuleEvidence(matched: bool, span: str, explanation: str)
dataclass
¶
A single piece of evidence supporting a rule's pass/fail decision.
Attributes:
| Name | Type | Description |
|---|---|---|
matched |
bool
|
Whether the rule pattern matched the input text. |
span |
str
|
The substring (lowercased) that triggered the match, or
|
explanation |
str
|
Short human-readable note describing what was checked. |
ChecklistRule(id: str, description: str, weight: float, sub_score: str, check: Callable[[str, str, str | None, dict[str, Any]], RuleEvidence], citation: str = '')
dataclass
¶
A single rule with an id, a pattern check, and a weight.
Rules are designed to be readable: id and description are
surfaced verbatim in the audit explanation. The check callable
returns a :class:RuleEvidence so the audit trail records why the
rule fired, not just that it did.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Stable identifier (e.g. |
description |
str
|
One-line human-readable description of the rule. |
weight |
float
|
Contribution to the parent sub-score when matched, in [0, 1]. Sub-scores are capped at 1.0 even when weights sum higher. |
sub_score |
str
|
Which sub-score this rule contributes to. For the legacy
|
check |
Callable[[str, str, str | None, dict[str, Any]], RuleEvidence]
|
Pure function |
citation |
str
|
Free-text academic / industry / regulatory provenance for
the rule, suitable for inclusion in an audit explanation or a
regulatory submission. Empty string when no citation is provided.
Example: |
RuleResult(rule_id: str, sub_score: str, weight: float, matched: bool, evidence_span: str, explanation: str)
dataclass
¶
Outcome of evaluating a single rule.
Attributes:
| Name | Type | Description |
|---|---|---|
rule_id |
str
|
The :attr: |
sub_score |
str
|
Which sub-score this rule contributes to. |
weight |
float
|
The weight of the rule (echo of :attr: |
matched |
bool
|
Whether the rule fired. |
evidence_span |
str
|
The substring that triggered the match, if any. |
explanation |
str
|
The rule's human-readable explanation. |
RuleSetResult(sub_scores: dict[str, float], quality: float, flagged: bool, rule_results: tuple[RuleResult, ...], audit_explanation: str)
dataclass
¶
Aggregated result of evaluating a :class:RuleSet against a response.
Each sub-score is a capped weight sum of matched rules in that category,
stored in the :attr:sub_scores mapping. quality is the geometric
mean of all sub-score values: any zero sub-score yields quality = 0.0,
reflecting that a rationale missing any audited dimension is structurally
incomplete for human review.
Backward-compatible read accessors are exposed for the legacy De-La-Chica
style sub-scores (spec, expl, bshift) and for the current
GroundLens five-category skeleton (groundedness, completeness,
calibration, traceability, robustness). Accessors return
0.0 when the underlying ruleset did not define the requested sub-score.
Attributes:
| Name | Type | Description |
|---|---|---|
sub_scores |
dict[str, float]
|
Mapping from sub-score name to its capped value in [0, 1]. By convention, do not mutate. |
quality |
float
|
Geometric mean of all sub-score values in :attr: |
flagged |
bool
|
|
rule_results |
tuple[RuleResult, ...]
|
One :class: |
audit_explanation |
str
|
Multi-line human-readable summary suitable for inclusion in an audit log. |
Attributes¶
spec: float
property
¶
Legacy specificity sub-score. Returns 0.0 if not defined by ruleset.
expl: float
property
¶
Legacy explanatory-linkage sub-score. Returns 0.0 if not defined by ruleset.
bshift: float
property
¶
Legacy boundary-shift sub-score. Returns 0.0 if not defined by ruleset.
groundedness: float
property
¶
Groundedness sub-score. Returns 0.0 if not defined by ruleset.
completeness: float
property
¶
Completeness sub-score. Returns 0.0 if not defined by ruleset.
calibration: float
property
¶
Calibration sub-score. Returns 0.0 if not defined by ruleset.
traceability: float
property
¶
Traceability sub-score. Returns 0.0 if not defined by ruleset.
robustness: float
property
¶
Robustness sub-score. Returns 0.0 if not defined by ruleset.
RuleSet(name: str, rules: tuple[ChecklistRule, ...], sub_scores: tuple[str, ...] = ('spec', 'expl', 'bshift'), quality_floor: float = _DEFAULT_QUALITY_FLOOR, flag_predicate: Callable[[dict[str, float]], bool] | None = None)
dataclass
¶
A collection of rules evaluated together against a (q, r, ctx) triple.
Use :func:groundlens_banking_rules for the current canonical
five-category ruleset, :func:banking_rules for the legacy three-category
ruleset, or construct your own by passing a sequence of
:class:ChecklistRule along with the list of sub-score categories the
rules contribute to.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Identifier (e.g. |
rules |
tuple[ChecklistRule, ...]
|
The rules to evaluate. |
sub_scores |
tuple[str, ...]
|
Ordered tuple of sub-score category names this ruleset
produces. Rules whose |
quality_floor |
float
|
Default flag-predicate threshold below which a sub-score
triggers the audit-deficiency flag. Applied to |
flag_predicate |
Callable[[dict[str, float]], bool] | None
|
Optional pure function |
Methods:¶
evaluate(*, question: str, response: str, context: str | None = None, metadata: dict[str, Any] | None = None) -> RuleSetResult
¶
Evaluate the ruleset against a single (question, response) pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The user query / prompt the LLM received. |
required |
response
|
str
|
The LLM's rationale text being audited. |
required |
context
|
str | None
|
Optional retrieved context (RAG-style). May be |
None
|
metadata
|
dict[str, Any] | None
|
Optional dict carrying domain-specific structured data that some rules may consult (e.g. the case parameters in a banking decision: risk score, flags, amount, etc.). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
RuleSetResult
|
class: |
RuleSetResult
|
quality, and a full audit explanation. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/groundlens/rules.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
Functions:¶
banking_rules(quality_floor: float = _DEFAULT_QUALITY_FLOOR) -> RuleSet
¶
Curated ruleset for regulated banking governance decisions.
The rules cover the three sub-scores that an auditor or compliance officer typically inspects in a deferral or escalation rationale:
- Specificity (spec): does the rationale cite the case parameters that triggered the decision? Flags, risk score, numeric thresholds, gates, completeness, jurisdictional details, sufficient length, and specificity-marking language.
- Explanatory linkage (expl): does the rationale link the case facts to the decision? Conditional structure, pending actions, causal connectives, epistemic limits, domain references, modal verbs, length, and temporal ordering.
- Boundary shift (bshift): does the rationale state what would change the decision? Conditional approval pathways, information requests, risk-reduction proposals, alternative framings, threshold references, and length.
The default quality_floor=0.3 follows the cosmetic-deadlock
threshold introduced in the financial-decisions governance literature.
A response that falls below this floor on either spec or expl
is flagged as audit-deficient even if the geometric SGI/DGI score
looks acceptable in isolation — a structurally typical "false
negative" of embedding-based detection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quality_floor
|
float
|
Threshold below which a sub-score triggers the cosmetic-deadlock flag. Tune per deployment risk tolerance. |
_DEFAULT_QUALITY_FLOOR
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
RuleSet
|
class: |
Source code in src/groundlens/rules.py
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 | |
groundlens_banking_rules(quality_floor: float = _DEFAULT_QUALITY_FLOOR) -> RuleSet
¶
Canonical rule set for LLM rationale evaluation in banking governance.
Returns the 20-rule reference set whose provenance is triangulated across five independent research tracks: peer-reviewed NLP literature, tier-1 bank public reports, banking regulator whitepapers, cross-industry frameworks, and financial-domain NLP benchmarks. The rules are organized into five empirically-emergent sub-score categories:
- groundedness (5 rules): claims linked to and supported by source.
- completeness (3 rules): coverage of the governance question.
- calibration (4 rules): uncertainty expression and abstention.
- traceability (5 rules): citation, audit trail, validation references.
- robustness (3 rules): resistance to noise, conflict, injection.
Each rule carries a citation field pointing to at least one of its
academic, industrial, or regulatory provenance sources. The companion
paper (Marin, 2026) documents the full per-rule provenance.
The default flag predicate :func:_groundlens_banking_flag_predicate
triggers when any regulator-non-negotiable sub-score falls below its
threshold (groundedness < 0.5, calibration < 0.3, or traceability < 0.4).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quality_floor
|
float
|
Legacy floor exposed for users who want a uniform
threshold across sub-scores. Not used by the default flag
predicate; kept for compatibility with the legacy |
_DEFAULT_QUALITY_FLOOR
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
RuleSet
|
class: |
RuleSet
|
sub-scores and 20 rules. |
Source code in src/groundlens/rules.py
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 | |
decision_rationale_rules(domain: str = 'finance', regulations: tuple[str, ...] = (), quality_floor: float = _DEFAULT_QUALITY_FLOOR) -> RuleSet
¶
Rule set for decision-rationale agents (credit / AML / KYC / sanctions).
Canonical factory for the 20-rule, 5-sub-score decision-rationale
rule set. Replaces :func:groundlens_banking_rules under the
archetype-as-function naming convention introduced in ADR 0001
(release 2026.6.13).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
domain
|
str
|
Deployment domain. Currently only |
'finance'
|
regulations
|
tuple[str, ...]
|
Optional tuple of regulation keys. When non-empty,
Implementation note (2026.6.13): the kwarg is accepted and
validated, but provenance-filtered rendering of
|
()
|
quality_floor
|
float
|
Threshold below which a sub-score triggers the
cosmetic-deadlock flag. Kept for compatibility with the
legacy |
_DEFAULT_QUALITY_FLOOR
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
RuleSet
|
class: |
RuleSet
|
five sub-scores and 20 rules. The rules and weights are identical |
|
RuleSet
|
to those of :func: |
|
RuleSet
|
name is updated. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example::
from groundlens import decision_rationale_rules
rs = decision_rationale_rules(
domain="finance",
regulations=("eu_ai_act", "sr_26_2"),
)
result = rs.evaluate(question=q, response=r, context=ctx)
Source code in src/groundlens/rules.py
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 | |