Harn rules

From burin-labs/harn · View interactive version

Harn rules is an AI agent skill from the GitHub repository burin-labs/harn. Use for authoring and running declarative structural rules — patterns, lint diagnostics, and codemods.

How to use this skill

Skill file

Harn rule engine

Use this skill when matching or rewriting code structurally — a search pattern,

a lint rule, or a codemod — instead of with regex/glob. The engine is the

harn-rules crate; it is exposed to Harn as std/rules and to the CLI as

harn scan (read-only) and harn codemod (apply).

Pair it with [[harn-language]] for .harn syntax and [[harn-testing]] for

conformance fixtures.

Start here

relational/composite algebra, where/transform/fix, safety tiers).

(harn scan / harn codemod, authoring a rule).

(via std/rules) without recompiling the binary.

A rule, end to end

matcher block, and an optional fix.

later top-level scalar would bind to it.

no fix is a lint; a bare matcher is a search.

Matcher forms (the `[rule]` block)

compiled to a tree-sitter query. Operators/keywords match literally

(?? ≠ ||); a repeated $VAR unifies; a metavar-free pattern is a

literal pattern (foo() matches calls to foo, not any call).

Matching is exact-shape: the target must have exactly the pattern's

children and keywords (only a terminating ; may be omitted), so

getById($A) does not match svc.getById(x) or getById(a, b). Put a hole

where code varies ($OBJ.getById($A)); there is no variadic $$$ yet.

Java/C#/C/C++/PHP expressions are written without ;. A trailing ; roots

the pattern at the statement.

expr/stmt/type/ident aliases resolve to the grammar's supertype

(TypeScript/JS/Python), else use an exact tree-sitter kind. Unknown kind =

compile error.

(ancestor), has (descendant), follows/precedes (siblings).

[utils.NAME] utility rule by id). Every key on a node is ANDed.

Predicates, rewrite, and safety

a recursive pattern, or Harn-only semantic filters

resolvesTo = { name, kind, line, column, id } and type = "int".

Harn match dictionaries keep text in captures and add

capture_metadata with resolved / type where available.

replace = { regex, by }, substring = { start, end }).

format-preserving edits.

scope-local (default) → surface-changing → capability-changing →

needs-human. The two safest are machine-applicable; the rest are

suggestions (opt-in). auto_apply refuses anything riskier;

apply_checked also fails a non-idempotent fix.

From the CLI

--rule <file> / --rule-pack <pack>, --report-only (per-file counts),

--json.

per file with safety + idempotency); --apply writes (capability-gated),

--allow-unsafe applies above machine-applicable safety, --json.

rules and imperative *.lint.harn modules in [rules] ruleDirs merge into

the normal output. A script rule exports lint(source) and returns a

{message, severity?, line?, column?} finding, a list of findings, or the

rules_diagnostics(...) result. .harn rules run read-only and fail safe.

Trusted native Rust rule libraries can also be loaded from

[rules] nativeRuleDirs; they export harn_native_lint_register_v1 and emit

the same diagnostic/fix shape as built-ins.

From `.harn` (`std/rules`)

bindings. rules_report(...) → a report-only data table (counts + rows).

allow_unsafe}) → codemod (gated; dry_run` by default).

escape hatch: the visitor returns its report(s) (nil/false to skip,

true to flag, a {message, fix, safety, severity} dict, or a list).

Gotchas

.harn paths resolve from their extension.

first argument, including for dry runs. The nominal handle is the authority;

there is no ambient enable step.

HARN_BIN_NO_BUILD=1 ./scripts/harn_bin.sh -- fmt <paths>.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.