Skip to main content
Custom Kata rules extend the built-in policy when a repository has domain conventions that cannot be expressed by configuration alone. They use the same selection, suppression, deterministic ordering, and remediation output as built-ins. Custom rule codes use XSQBK<family><three digits>. Keep codes stable after adoption because they become part of configuration, CI output, and exceptions.

Define a rule

The function signature is exactly two keyword-only arguments named model and ctx. Return an empty list when the model passes or one or more KataFault values when it fails. RuleContext exposes the compiled model, authored SQL, raw Polyglot AST, references, parsed model name, materialization, declared columns, audit and test counts, public declarations, active policy, and fault constructors. Repository files can be read safely through project_read_text and project_glob.

Load and select rules

Load repository-owned files or dotted modules from sqlbuild_project.toml:
A directory in rule_paths is scanned recursively for Python files containing @kata. Dotted modules must resolve beneath the project root. Codes must be unique across built-in and custom rules. Custom rules require exact selectors by default. Set enabled_by_default=True on the decorator to include a rule in matching prefix selections. This does not activate Kata when [kata].select is empty.

Typed options

Declare options with RuleOption.boolean, integer, string, string_list, or integer_list:
Configure options under the exact rule code. Unknown rules, option names, or invalid values fail configuration:

Test every rule

Use the public harness so tests exercise normal SQLBuild discovery, compilation, rule loading, and structured fault evaluation:
RuleCase.files can add supporting project files and RuleCase.config supplies the rule’s option values. Keep conventional RuleCase and evaluate_rule calls under tests/ so SQBKX201 can count statically discoverable harness cases. This coverage check does not execute the tests, so run the test suite separately in CI.

Execution and caching

Selected custom rules execute in a bounded Python subprocess with a 30-second timeout. Exceptions are reported with the rule code and model path, and returned faults rejoin normal suppressions and deterministic ordering. Selecting any custom rule disables the model cache by default. To keep the built-in cache available, require hermetic custom rules explicitly:
Cacheable rules may import supported pure modules such as collections, dataclasses, enum, math, re, typing, and sqlbuild.kata. Use RuleContext rather than direct filesystem calls. SQLBuild validates these constraints before evaluation. Custom findings are still recomputed on each invocation. require_cacheable preserves the native model cache around them; it does not cache custom subprocess output. Return to Kata SQL Architecture Checks for built-in rules, selectors, and exceptions.