Skip to content

Loading the guide…

    Rules

    Updated

    Rules are plain YAML files that you own. HoundWatch™ ships example rule sets and forces none. This page covers the format, the check types, where rules live and which wins, the example sets, your written rule documents, and the commands for writing and testing rules.

    Every rule file on this page was checked with houndwatch rules validate. The architecture example was checked with houndwatch rules test.

    Where rules live

    LevelWhereApplies to
    User~/.houndwatch/rules/*.yaml, and sets enabled in ~/.houndwatch/config.yamlevery project
    Project<project>/.houndwatch/rules/*.yaml, and sets enabled in <project>/.houndwatch/config.yamlthat project
    Example setsinside the packageonly where a config enables them

    Only *.yaml and *.yml files directly in a rules/ folder load. Drafts in .houndwatch/rules/drafts/ never do.

    .houndwatch/rules/ and .houndwatch/config.yaml can be committed with the project. The rest of .houndwatch/ is local state, and init adds it to .gitignore.

    Which rule wins

    User rules are the floor. A project can add rules and can make a user rule stricter (warn to block). It cannot weaken or replace a user rule, unless that user rule says overridable: true. A project rule that tries is reported, and the user rule is kept:

    text
    .houndwatch/rules/weaken.yaml:2: rule 'commit-subject-format' cannot weaken the user rule from /Users/you/.houndwatch/rules/commits.yaml (user rules are the floor; a project may only raise warn to block unless the user rule sets overridable: true); kept the user rule

    Within one level, a rule file overrides an example rule with the same id. That is how you tune one rule from an example set: copy it into a rule file at the same level, keep the id, and change what you need.

    Rule ids must be unique within a level. A second rule with the same id in the same place is reported, and the first one wins.

    The project's ignore list (in its config) applies to project rules only. User rules still see files a project ignores.

    The format

    A rule file holds a rules: list. Here is a user-level file, ~/.houndwatch/rules/commits.yaml:

    yaml
    rules:
      - id: commit-subject-format
        says: The first line of a commit message is capitalised and 10 to 72 characters
        applies_to:
          subject: commit_message
        check: pattern
        pattern:
          must_match: '^[A-Z].{9,71}$'
        severity: block
        tell_agent: "Rewrite the commit subject: capital first letter, 10 to 72 characters"
    
      - id: no-ai-attribution
        says: Commit messages carry no AI tool attribution
        applies_to:
          subject: commit_message
        check: pattern
        pattern:
          preset: banned-trailers
        severity: block
        tell_agent: "Remove the AI tool attribution from the commit message"
    FieldMeaning
    idLetters, digits, -, _ and .. Unique within its level.
    saysThe rule in one sentence. Shown in rules list and findings.
    applies_tosubject, plus optional paths and exclude (globs relative to the project root) and event.
    checkpattern, structure, llm or command. A mapping with the same name holds its settings.
    severityblock or warn.
    tell_agentWhat the agent (and you) are told. Defaults to says.
    baselinefalse means touching a file means fixing it: no baseline grace. Default true.
    overridableOn a user rule: lets a project weaken or replace it. Default false.

    Placeholders in tell_agent: {file}, {line}, {count}, {missing}, {reason}, {rule}. Structure checks with a limit also fill {limit}. Model rules fill {reason} with the model's one-line answer.

    A rule id that starts with codehound- is refused with the new houndwatch- name. See Upgrading.

    Subjects

    subjectWhat is checkedWhen
    fileone file's contentthe agent's edits, the watcher on save, each changed file at commit
    commit_messagethe commit messagecommit-msg hook, the agent's git commit command, the watcher
    commita commit's diff and changed pathspre-commit hook, the watcher for commits that skipped it
    sessionwhat the agent has done in its sessionthe agent hook (event: before_first_edit, before_edit, end_of_turn)
    planthe current plan filethe agent hook (event: session_start, end_of_turn) and after a commit

    Severities

    • block stops the action: the agent's edit or command is refused, or the commit fails.
    • warn is shown and recorded as a finding, and never stops anything.

    Some findings are always warn, whatever the rule says: a skipped model check (llm-check-skipped), a hook that was skipped or edited, a config or rule file edited by hand. See Day-to-day use.

    Check types

    pattern: regular expressions

    Instant. No model, and nothing is run.

    SettingMeaning
    must_matchtext that must be present (a string or a list)
    must_not_matchtext that must not be present
    within_linesonly look at the first N lines
    changed_paths_must_not_matchfor commit rules: changed file paths that are not allowed
    preseta built-in list of patterns, used as must_not_match
    flagsletters from imsx. Multiline is always on, so ^ and $ match at each line.

    On a commit, content patterns look only at the lines the diff adds. On a commit_message, must_match tests the first line unless within_lines is set.

    The presets are kept up to date with HoundWatch, so you don't have to maintain the regexes:

    PresetFinds
    secretscommon key and token shapes, private keys, passwords in URLs, quoted credentials
    env-files.env files (not .env.example, .sample, .template, .dist), key and certificate files, private SSH keys, credential and service-account files
    backup-files.bak, .backup, .orig, .save, .copy, .old, editor swap files, names ending in ~
    banned-trailersAI tool attribution trailers and "generated with" footers; the tools come from banned_trailer_tools in config
    debug-leftoversprint(, console.log, breakpoint(), pdb.set_trace(), debugger
    todo-markersTODO, FIXME, XXX, HACK

    structure: parsed code and recorded events

    Instant. Python files are parsed with the standard library. Other languages get pattern checks only. A structure mapping names exactly one check:

    CheckSettingsWhat it checks
    max_code_linesa numberlines of code, comments excluded
    chunk_commentsmarker, applies (function, class or both)a comment containing the marker directly above each function or class
    imports_allowedpackage, allow, public_onlyimport directions between a package's layers
    architecturechecks, declare (or {} to use .houndwatch/architecture.yaml)declared modules, required and forbidden paths, folder conventions
    changed_togetherscope (commit or session), when, requireif these paths change, those must change too
    session_has_reada list of pathsthe agent has read these files in its session
    plan_progress{}where the plan stands (nudges)
    test_file_existstest_direach module has a test file in the mirrored test location
    fixtures_not_inlinemax_lines, fixtures_dirlarge mock data lives in fixtures
    fix_changes_a_testmessage_matchesa "fix" commit adds or changes a test

    session_has_read paths may use {spec} (the current spec), {read_first_docs} and {global_rules_dir} (below). changed_together paths may use {today}.

    HoundWatch checks these names, their options and their values when it loads rules. It skips a rule with an unknown name, an unknown option (such as alow: for allow:) or a bad value, such as max_code_lines: lots. The module names under allow: and public_only: are up to you. Until a human fixes it, commits are blocked by rules-invalid. Agent edits only get a warning. rules validate shows the file and line:

    text
    .houndwatch/rules/r.yaml:2: rule 'short': unknown structure check 'max_code_line' (did you mean 'max_code_lines'?)

    llm: a question for a local model

    Runs in the background through the watcher, never inside a hook. See Local models.

    SettingMeaning
    promptthe question. Placeholders: {spec}, {diff}, {file}, {text}, {plan}, {program}
    contextwhat to supply, e.g. [diff] or [spec, diff]
    tierauto (the router decides), 1 or 2
    skip_unlessremoves_lines: pass without asking when the change removes no lines in applies_to.paths

    The prompt should ask for PASS or FAIL and one line why.

    command: run a command at commit

    Runs only at commit (pre-commit), never on an edit or save, because it runs code.

    SettingMeaning
    runthe command. {tests} becomes the test files for the changed files. Without run, the user config's test_command is used.
    maphow test files are found: mirror (the only one)
    timeoutseconds, default 120. See below for what happens when it runs out.
    taillines of output to keep, default 20

    The command is not run through a shell, and it runs in the project root.

    If the command can't run, a trusted block rule blocks the commit. This covers a rule with no command (no run, and no test_command in your user config), a command that can't start, and one that times out. The message says how to fix it. If you change a project rule to fix it, trust the rule again, or it will only warn and its command won't run. Untrusted rules and warn rules only warn.

    A project-level command rule needs your trust. A command rule from a project's rule files, or from a set enabled in the project config, does not run until you trust it. Until then each commit gets one warning:

    text
    [warn] tests-pass: command rule tests-pass is not trusted for this project (it runs code): review it, then run `houndwatch rules trust tests-pass`

    Review the rule, then from your terminal:

    houndwatch rules trust tests-pass

    It shows what the rule will run, asks you to confirm, and asks for a reason. Any later change to the rule needs trust again. Command rules at user level need no trust.

    A project rule file

    .houndwatch/rules/python.yaml, with one rule of each kind:

    yaml
    rules:
      - id: max-file-length
        says: Python files stay within 400 lines of code, comments excluded
        applies_to:
          subject: file
          paths: ["**/*.py"]
          exclude: ["tests/fixtures/**"]
        check: structure
        structure:
          max_code_lines: 400
        severity: block
        tell_agent: "{file} has {count} lines of code; split it into modules under {limit}"
    
      - id: no-print-in-src
        says: Source files under src/ use the logger, not print()
        applies_to:
          subject: file
          paths: ["src/**/*.py"]
        check: pattern
        pattern:
          must_not_match: '(?<![\w.])print\('
        severity: warn
        tell_agent: "Replace the print() at {file}:{line} with the project's logger"
    
      - id: no-key-files
        says: Key and certificate files are never committed
        applies_to:
          subject: commit
        check: pattern
        pattern:
          changed_paths_must_not_match: ['\.(pem|key|p12)$']
        severity: block
        tell_agent: "Unstage {file}; key files must not be committed"
    
      - id: tests-run
        says: The tests covering the changed files pass before a commit
        applies_to:
          subject: commit
          paths: ["**/*.py"]
        check: command
        command:
          run: "pytest -q {tests}"
          map: mirror
          timeout: 120
        severity: block
        tell_agent: "Tests fail for this commit: {reason}"
    
      - id: diff-matches-message
        says: The commit's diff does what its message says
        applies_to:
          subject: commit
        check: llm
        llm:
          prompt: |
            Diff: {diff}
            Does the diff match a short, honest description of the change? Answer PASS or FAIL, then one line why.
          context: [diff]
          tier: 2
        severity: warn
        tell_agent: "The change may not match its description: {reason}"
    
      - id: read-api-guide
        says: The agent reads the API guide before its first edit
        applies_to:
          subject: session
          event: before_first_edit
        check: structure
        structure:
          session_has_read: ["docs/api-guide.md"]
        severity: block
        tell_agent: "Read {missing} before editing any file"

    Tune one example rule

    With owner-standard enabled in the project config, this file in .houndwatch/rules/ replaces its owner-tests-not-weakened rule: it asks the tier 2 model and only warns. The first trial used an override like this.

    yaml
    rules:
      - id: owner-tests-not-weakened
        says: Tests are not deleted, skipped or loosened to make a change pass
        applies_to:
          subject: commit
          paths: ["tests/**", "**/test_*.py"]
        check: llm
        llm:
          prompt: |
            Diff: {diff}
            Lines starting with "-" were removed, lines starting with "+" were added. Adding new tests is fine.
            Does this diff delete tests, add skips, or loosen assertions without a stated reason? PASS or FAIL, one line why.
          context: [diff]
          tier: 2
          skip_unless: removes_lines
        severity: warn
        tell_agent: "Tests look weakened: {reason}"

    A user rule a project may relax

    yaml
    rules:
      - id: no-todo-markers
        says: Source files carry no TODO or FIXME markers
        applies_to:
          subject: file
          paths: ["**/*.py"]
        check: pattern
        pattern:
          preset: todo-markers
        severity: warn
        overridable: true
        tell_agent: "Resolve or remove the marker at {file}:{line}"

    Docs that change with code

    yaml
    rules:
      - id: changelog-with-code
        says: A commit that changes source code also updates CHANGELOG.md
        applies_to:
          subject: commit
        check: structure
        structure:
          changed_together:
            scope: commit
            when: ["src/**/*.py"]
            require: ["CHANGELOG.md"]
        severity: warn
        tell_agent: "{file} changed: add a line to {missing} in this commit"

    Declared architecture

    .houndwatch/architecture.yaml declares modules and what each may import, plus required and forbidden paths:

    yaml
    modules:
      api:
        paths: ["src/api/**"]
        may_import: [core]
      core:
        paths: ["src/core/**"]
        may_import: []
    forbidden_paths:
      - path: src/utils.py
        use: src/core/utils.py
        reason: shared helpers live in core

    A rule turns it on:

    yaml
    rules:
      - id: layers
        says: Modules import only in the declared directions
        applies_to:
          subject: file
          paths: ["src/**/*.py"]
        check: structure
        structure:
          architecture: {}
        severity: block
        tell_agent: "{missing}"

    What the agent is told for a file in src/core/ that imports from src/api/:

    text
    [block] layers: module core may not import module api (src.api); core may import: nothing (src/core/model.py:1)

    A user-level ~/.houndwatch/architecture.yaml is the floor: a project can add modules and narrow may_import, never widen it.

    The example rule sets

    All ship inactive. Enable one with houndwatch rules enable <set> (add --user for every project).

    SetWhat it holdsNeeds a model
    starterNo env, key, credential or backup files committed; no secrets; debug leftovers (warn); files over 500 lines of code (warn); tests not weakenedone rule (starter-tests-not-weakened)
    testsEvery Python module has a test file; test functions carry a CHUNK COMMENT (warn); no large inline mock data (warn); a fix commit changes a test (warn); the tests covering a commit pass (a command rule)no
    judgementChanges stay within the spec (warn); tests not weakened; no new skip or xfail markerstwo rules
    progressWhere the plan stands at session start and end of turn (warn nudges)no
    progress-programA commit serves the plan and the program target above it (warn)yes
    docs-in-stepAPI route changes update the OpenAPI file; a session that changes code updates today's work log and the README status blockno
    architecture-nextjsNext.js layout: route groups, proxy.ts instead of middleware.ts, pages inside route groupsno
    owner-standardThe HoundWatch author's own standard: MODULE HEADER and CHUNK COMMENT, 450 lines of code, docs in docs/ with timestamps, rule documents and spec read before the first edit, stays on spec, tests not weakened, no secrets, env or backup files, no AI attributiontwo rules
    houndwatch-boundariesHoundWatch's own import boundaries; useful only as an example of imports_allowedno

    Rule ids differ between sets, so sets can be enabled together. owner-standard is a worked example of turning a written standard into rules. Use it as a model for your own set, not as a default.

    A few things to know before enabling:

    • owner-standard's owner-spec-before-code blocks the agent's first edit until it has read a spec. With no *-design.md in spec_dir, it blocks every edit. owner-stays-on-spec is a warn rule whose prompt uses {spec}, so it doesn't run when there is no spec (Local models).
    • tests's tests-pass needs houndwatch rules trust tests-pass when enabled at project level, and reads its test command from run. Set test_command in your user config to use another runner by default.
    • docs-in-step's session rules expect docs/work-log-YYYY-MM-DD.md and docs/README.md.

    Rule documents

    Your written rules (a coding standard, a CLAUDE.md, an AGENTS.md) can live anywhere. List them under rule_docs in your user config, ~/.houndwatch/config.yaml:

    yaml
    rule_docs:
      - path: ~/Projects/library/rules/global.md
        read_first: true
      - path: ~/Projects/library/rules/stvd.md
        read_first: true
      - path: ~/Projects/library/rules
    • path is a file, a folder (every *.md directly in it) or a glob (** searches folders). ~ is your home folder. A relative path is taken from your home folder too.
    • read_first: true marks a document the agent must read before its first edit. A file listed twice is read_first if any entry says so.
    • rule_docs is read from the user config only. In a project config it is ignored and rules validate reports it, because it names the documents user rules read.

    Where the documents are used:

    • {read_first_docs} in a session_has_read rule stands for every read_first document. owner-standard's owner-global-rules-read-first uses it, so the agent's first edit is refused until it has read them. With no read_first documents, the rule passes and says so once.
    • The session briefing lists them under "Read first" (Day-to-day use).
    • houndwatch rules lint compares them with the active rules (below).

    global_rules_dir: <folder> is the older setting. It still works: it counts as rule_docs: [{path: <folder>}], and {global_rules_dir} still fills rules that name it.

    Commands for rule authors

    Validate

    houndwatch rules validate
    text
    houndwatch: 8 rule(s) valid

    Problems are printed as file: message, with the line where known. The command exits 1. Invalid rules are skipped; the valid ones still load. It lists all the problems it finds in config and rule files at once:

    • rule files or rules folders that can't be read or parsed;
    • invalid rules, unknown check names, bad check values, repeated keys and repeated rule ids;
    • mistakes in the config files, and user-only keys in a project config;
    • mistakes in an architecture file, if a rule uses it.

    Most of these problems block commits until a human fixes them. A user-only key in a project config, or a project rule that tries to weaken one of your user rules, only warns: HoundWatch ignores the key and keeps your rule. Configuration says which config mistakes block. A missing rules folder is fine, and so is an empty rule file.

    Test one rule

    houndwatch rules test max-file-length src/big.py
    text
    [block] max-file-length: src/big.py has 410 lines of code; split it into modules under 400 (src/big.py)

    The target is a file path for file rules, a message file for commit_message rules, and a commit sha for commit rules. It prints <rule-id>: pass when the rule passes, and exits 1 on a block. session and plan rules can't be tested this way yet.

    Lint your documents against your rules

    houndwatch rules lint

    It reads every document rule_docs names, finds file-name conventions in them (such as .test.py or test_*.py) and compares them with the active rules' paths:

    text
    /Users/you/rules/team.md:3: tests-file-exists: the document names test files `<name>.test.py`; the rule expects `test_<module>.py` (.../houndwatch/rules/examples/tests.yaml)
    houndwatch: 1 conflict(s) between 1 rule document(s) and the active rules

    --deep also asks the tier 2 model for contradictions between the documents and the rules' says lines. If the model can't be reached, the deep part is skipped. Lint never blocks anything and always exits 0. The first trial found exactly this kind of conflict: a document asked for <name>.test.py while the rule wanted test_<module>.py.

    Draft rules from a document

    houndwatch rules draft AGENTS.md
    text
    houndwatch: 3 draft rule(s) in /Users/you/Projects/myapp/.houndwatch/rules/drafts/AGENTS.yaml (inactive)
    review them, then run `houndwatch rules accept AGENTS`

    The tier 2 model reads the document and writes draft rules, each with a comment citing its source line. Drafts are never enforced. --force replaces an existing draft of the same name.

    Review every draft. They are a starting point. In the run used for this guide, "Never commit .env files" became a pattern that would have matched any added line, and "Keep functions under 50 lines" became a 50-line limit on whole files. Command rules are never drafted, because a document from a cloned repository must not be able to propose commands.

    Edit the draft by hand, then accept it from your terminal:

    houndwatch rules accept AGENTS

    accept checks the draft first. It refuses a draft with invalid rules, or with rule ids that are already active. Otherwise it lists the rules, asks you to confirm and for a reason, and moves the file into .houndwatch/rules/, where its rules are active. rules validate does not look at drafts until you accept them.