Skip to main content
RuleContext is the supported interface between a custom rule and Fensu’s analysis engine. It is passed to every rule check as ctx. Import all public protocols, handles, locations, facts, and dependency models from fensu, never from internal package paths.
The context groups its surface into five analysis zones plus fault construction, position, AST helpers, and configuration. Every method is a convenience: a rule may ignore the context and walk module directly when it needs unrestricted AST access.

ctx.facts

Backend-neutral semantic facts for the current file, computed once and shared by all rules through a single traversal.
evaluate_rule_calls() supports policy analysis such as FFR707. Custom-rule tests should use the public testing harness rather than inspect these facts directly.

ctx.project

Cross-file and filesystem queries. Every call records its input for cache invalidation, so a result stays reusable only while its answers hold. Pass the file whose result depends on the query as requester, normally ctx.path.
Read project state only through ctx.project, never the filesystem directly. Untracked reads break cache correctness and fail cacheability verification.

ctx.text, ctx.syntax, ctx.relations

Source text, opaque syntax identities, and syntax relationships for the current file. The zone protocols are FactAnalysis, ProjectAnalysis, TextAnalysis, SyntaxAnalysis, and RelationAnalysis, all exported from fensu.

Fault construction

Construct diagnostics through the context so the rule’s code, and optionally its declared message and remediation, are wired automatically. Omitted message and remediation use the rule’s envelope values.

Position and roles

Location facts derived from configured roots and scopes, not hardcoded package names.

AST helpers

Convenience operations over the current file’s parsed tree. Prefer ctx.nodes() over ast.walk(module) for module-wide scans so cooperating rules share Fensu’s single traversal.

Configuration

Public models

All models below are exported from fensu and returned by the calls above. They are frozen dataclasses; inspect exact fields with your editor’s type information. Locations and handles Function and dataflow facts Annotation facts Declaration and reference facts Project and dependency facts Test facts

Custom rules

Authoring, loading, testing, and cacheability.

Configuration

Thresholds and contracts the context resolves.