fix(ttp): /api/v1/ttp/rules returns the live rule catalogue

The endpoint was a contract-phase stub returning `[]` even though the
RuleStore loaded all 58 YAML rules at worker startup. UI saw an empty
table; operators couldn't tell whether anything was wired up.

- `api_list_rules` now calls `get_rule_store().load_compiled()` and
  serializes each CompiledRule + its operational state into a
  RuleCatalogueRow. Sorted by rule_id for stable golden snapshots.
- Add `description: str` to RuleSchema (pydantic) and CompiledRule
  (NamedTuple, defaulted) + propagate through `_compile_one` so the
  catalogue surfaces the human-readable YAML description, not just
  the slug-style `name`.
- Update `tests/ttp/test_rule_engine.py` _fields assertion for the
  new column; new `tests/api/ttp/test_rules_catalogue.py` pins the
  catalogue contents (R0001/R0014 presence, row shape, sort order).

Worker behaviour is unchanged: it was already loading rules
correctly. This is purely a read-side wiring fix on the operator API.
This commit is contained in:
2026-05-02 01:54:06 -04:00
parent 7ab0df3680
commit e08bfc4a73
5 changed files with 95 additions and 2 deletions

View File

@@ -45,8 +45,34 @@ router = APIRouter()
async def api_list_rules(
user: dict[str, Any] = Depends(require_viewer),
) -> list[RuleCatalogueRow]:
"""Operator-facing rule catalogue. Empty at contract phase."""
return []
"""Operator-facing rule catalogue.
Reads from the active :class:`RuleStore` (filesystem or database
per ``DECNET_TTP_RULE_STORE_TYPE``). Each row is a compiled rule
plus the operational state the store has stamped on it; rules that
never had a state set come back as the default ``enabled``.
"""
from decnet.ttp.store.factory import get_rule_store # noqa: PLC0415
store = get_rule_store()
compiled = await store.load_compiled()
rows: list[RuleCatalogueRow] = []
for rule in compiled:
state = rule.state
rows.append(RuleCatalogueRow(
rule_id=rule.rule_id,
rule_version=rule.rule_version,
name=rule.name,
description=rule.description,
state=state.state,
confidence_max=state.confidence_max,
expires_at=state.expires_at,
reason=state.reason,
set_by=state.set_by,
set_at=state.set_at,
))
rows.sort(key=lambda r: r.rule_id)
return rows
@router.post(