# Rules and learning

> Teach MergeHarbor your team's conventions with plain-English rules, approve rules learned from feedback, and add design documents to the knowledge base.

## Rules

Rules are short, plain-English statements of how your team writes code. MergeHarbor applies the rules that match the files a pull request changes.

Create rules in three ways:

- **In the console** — **Memory → Custom rules → Add rule**. A rule applies to all repositories or to the repositories you select, and optionally only to files matching path globs.
- **In a pull request** — comment `@mergeharbor remember <rule>`, or scope it with `@mergeharbor remember for services/**, jobs/**: <rule>`.
- **From feedback** — when engineers consistently react to similar comments with thumbs up or thumbs down, MergeHarbor proposes a rule that captures the pattern, with the evidence.
- **In the repository** — `rules.md` and `rules:` in `.mergeharbor/` folders ([Repository configuration](https://mergeharbor.com/docs/repository-config)). Console rules and repository rules both apply.

### Where a rule applies

Choose **All repositories** or **Selected repositories**. With selected repositories you can pick individual repositories, [clusters](#cross-repository-context), or both. A cluster is read when each review starts, so a repository you add to the cluster later picks up the cluster's rules without editing them. You can change a rule's repositories at any time from **Edit**. To create or change a rule that covers several repositories you need permission to manage rules on every one of them.

The rules list shows each rule's scope; hover over it for the full list of repositories and clusters. In the API, a rule's `scope` is `org`, `repo` (one repository) or `repos` (with `repo_ids` and `cluster_ids`).

**Memory → Custom rules** lists rules from every source together, each labelled **Console** or **Repository file**. For each rule you see how many review comments it produced and the share of those comments on merged pull requests that later code fixed, so you can tell which rules earn their place and which only add noise.

## Context files

Under **Memory → Custom rules → Add file**, point MergeHarbor at a file in one of your repositories — a style guide, `CONTRIBUTING.md`, an API description or a database schema (Markdown, text, YAML or JSON). Choose which repositories' reviews read it and, optionally, the paths whose changes make it relevant. The file is read from the default branch each time its own repository is reviewed, so it stays current without copying it anywhere.

## Approval

Organization owners choose who may create rules from pull-request comments (everyone, administrators, or nobody) and whether new rules require approval. Rules learned from feedback always start as **proposed** and take effect only after an administrator approves them.

## Knowledge base

Add design documents, conventions, and architecture notes under **Memory → Knowledge base → Team documents**, for every repository or selected ones. The reviewer searches them when they are relevant to a change.

MergeHarbor also writes docs for each repository on its own; see [Generated docs](https://mergeharbor.com/docs/knowledge-base).

## Cross-repository context

Under **Memory → Cross-repo context**, group repositories that change together into **clusters**: every repository in a cluster can search the code and symbols of the others while it is reviewed. MergeHarbor suggests clusters from people who opened pull requests in several repositories over the last 90 days; create one with a click or mark the suggestion as not useful.

A repository can also name its own context repositories with `context.repos` in `.mergeharbor` configuration; those come first. One-way related repositories (search B when reviewing A, not the reverse) are still available on the same page.

## Feedback

Reactions on review comments are collected automatically on GitHub, GitLab and Gitea, and on Azure DevOps from likes and how a thread is resolved. On Bitbucket Cloud and Bitbucket Data Center, rate comments in the console. Comments similar to ones your team has rejected are suppressed in later reviews.
