Add a rule
Checks are data: a metadata object plus a function from a CheckContext to
findings. Everything else — the catalogue entry, docs/reference/rules.md,
the CLI --rule filter, the MCP surface — follows automatically.
The working example for every step is the rules module
src/app_anything/checks/rules/backend.py,
whose tests live in tests/rules/.
1. Pick the identity first
Rule IDs are permanent: AA0xx manifest, AA1xx files, AA2xx backend
runtime, AA3xx sandbox, AA4xx protocol, AA5xx assets, AA6xx
targets. The severity and phase decide when the rule runs; look at
AA205 for a dynamic rule or AA101 for a static one.
2. Write the rule
@rule(
RuleMeta(
id=RuleId("AA2xx"),
title="One sentence a developer reads in a report",
severity=Severity.ERROR,
phase=Phase.DYNAMIC,
applies=has_backend(),
rationale="Why the rule exists, in one paragraph.",
fix="What to change, addressed to the developer.",
contract="B5",
)
)
def no_orphans(ctx: CheckContext) -> Iterator[Finding]:
probe = ctx.fact(ProbeFact)
if probe.survivors:
yield ctx.fail(None, f"{len(prove.survivors)} process(es) survived the backend")
Facts (ctx.fact(...)) are computed lazily and cached; a rule declares
what it reads, and the engine runs only what the options select.
3. Fixtures are part of the rule
Add a pass and a fail case to tests/rules/fixtures/AA2xx/{pass,fail} —
tests/rules/build_fixtures.py generates them, and
tests/rules/test_rules.py asserts that the fail fixture triggers only
this rule and the pass fixture is clean under the whole catalogue. The
meta-test in tests/meta/test_registry.py fails if a rule has no fixture
pair, so you cannot forget.
4. Ship it
app-anything docs regenerates docs/reference/rules.md; explain AA2xx
now answers from the catalogue on every machine. The freshness test fails
in CI until you commit the regenerated page.
Rendered from docs/how-to/add-a-rule.md on main.