# Protect branches and objects

AI agent getting started? Read [Agents: start here](https://docs.openeng.io/agents/start-here.md) first.

Choose review requirements, branch rules and object locks.

Source: https://docs.openeng.io/concepts/branches-and-rules/

Protection controls how a change reaches a branch. Use branch rules for team review policies and object locks when a particular record needs additional protection. You still need write access to the affected projects.

## Choose what to protect

| Need | Control |
| --- | --- |
| Review changes before accepting them | Require a proposal and approvals on the target branch |
| Keep one project’s accepted data under review | A project rule with extent `rows` on `main` |
| Protect the organisation trunk | An organisation rule with extent `ref` on `main` |
| Preserve a fixed snapshot | A [read-only baseline](https://docs.openeng.io/guides/baselines.md) |
| Prevent changes to one object | An object lock |

Several rules can match a branch. Each applies unless you qualify for that rule's bypass. Bypass does not grant project access or override a frozen object.

## Set rules in the workspace

Open **Admin**, then **Project branch rules** for a project's rules or **Organisation branch rules** for the organisation's. Everyone who reads the project sees its rules; a person with **administer projects** can change them.

To add a rule, complete its three sections:

1. **Branches:** choose the data covered and the branch pattern.
2. **Changes:** choose the update rule, proposal requirement and required approvals. Set a minimum approver role or named approvers. Project rules covering entire branches can also restrict deletion.
3. **Who can skip this rule:** choose the roles, people, service identities or apps that may bypass this rule.

Choose **Edit** on a row to change its settings in a dialog; the pattern and what the rule covers cannot change. When the host refuses a rule, the refusal appears in the field it names, such as the branch pattern or the approvals needed, and the form keeps your values. Adding a rule the project already has returns the existing rule.

## Configure rules with the CLI

For example, require review for one project's rows on `main`, then require two approvals:

```sh
openeng rules add --project <id> --extent rows --pattern main --consequence merge-only
openeng rules list --project <id> --json
openeng rules update --project <id> --rule-id <rule-id> --required-approvals 2
```

Replace the identifiers with values from your instance. You need **administer projects** to change these rules. An agent-mode update waits for your browser confirmation.

See [Branch rule reference](https://docs.openeng.io/lookups/branch-rules.md#configure-branch-rules) for all flags, defaults, bypass entries and refusals.

## Type rules and instance locks

A type can be `direct` (directly editable), `immutable` or `owned` (writable only by its designated connector). An immutable type can be set at first publication or tightened later, but never relaxed. Updates and deletes are refused. An owned type can only be written by its designated connector integration.

A person with the **lock objects** capability can set an object-level edit restriction (instance lock) to `frozen` or `merge-only`. A frozen object cannot be changed. These rules can result in `immutable`, `owned` or `frozen` refusals.

## Merge and schema updates

Merge changes into any writable branch. If a project branch needs an operator update to its database schema, the platform returns `branch-needs-schema-update` and asks you to update this branch's schema.

The CLI commands for branch rules are `openeng rules add`, `openeng rules update`,
`openeng rules list` and `openeng rules remove`. Organisation branches and baselines use `openeng org-refs create`,
`openeng org-refs list`, `openeng org-refs delete` and `openeng org-refs mark-read-only`.

Set an object's lock with `openeng locks set --project <id> --ref <ref> --object <objectId>
--lock <none|merge-only|frozen> --reason <text>`. Use `--lock none` to lift the lock. The `--reason` flag is required: write it like a commit message, as [Propose, compare and merge](https://docs.openeng.io/guides/change-control.md#develop-the-change) teaches. The platform writes it first in the commit message. The `--ref` flag takes a branch name.

1. Request a branch schema update with `openeng schema-update request --ref <ref>`. This records a request for your platform operator; you cannot execute the update yourself as a customer. When the operator runs the update, it merges the organisation trunk into the branch only if the branch lacks the trunk's latest schema change, bringing both the schema and rows saved on the trunk since the branch was cut.
2. Read the request's state with `openeng schema-update show --ref <ref>`. If the branch and trunk changed the same row, the request ends `failed` with `merge-conflict`, the branch stays unchanged, and the next update tries again.

The `--ref` flag of both commands takes a branch name.

Run `openeng help <command>` for exact flags. See [Refusals](https://docs.openeng.io/reference/refusals.md) for
each refusal and its remedy.

## Patterns

See [the exact patterns](https://docs.openeng.io/lookups/branch-rules.md#patterns).

## Rule settings

See [the exact rule settings](https://docs.openeng.io/lookups/branch-rules.md#rule-settings).

## Branch rules

See [the exact branch rules](https://docs.openeng.io/lookups/branch-rules.md#branch-rules).

## Bypass lists

See [the exact bypass lists](https://docs.openeng.io/lookups/branch-rules.md#bypass-lists).
