Agent Architecture Weekly

Review guide

GitHub CODEOWNERS: syntax, examples and required reviews

Where the file lives, how a path finds its owners, which version of the file a pull request reads, and how to make an owner's approval a merge requirement when agents are opening the pull requests.

Agent Architecture Weekly · Updated

A CODEOWNERS file maps paths in a repository to the people or teams responsible for them. When a pull request changes a file that has an owner, GitHub requests a review from that owner automatically, and with one branch setting it can refuse to merge until an owner approves.

That matters more once coding agents open pull requests. An agent's change can touch a payment module, a migration and a workflow file in one diff, and nobody is watching the queue to decide who should look at which part. CODEOWNERS makes that decision from the paths, every time, before any human reads the diff.

This guide follows GitHub's documentation as it stands today (linked under Sources). Plans and settings change, so check About code owners before you plan around them.

Who can use it

From GitHub's documentation:

  • Code owners work in public repositories on GitHub Free and GitHub Free for organizations, and in public and private repositories on GitHub Pro, GitHub Team, GitHub Enterprise Cloud and GitHub Enterprise Server. Check GitHub's plans for your own account.
  • Every owner needs write access to the repository. A team must be visible and have write access granted to the team itself, even when each of its members already has write access some other way.
  • Turning on required code owner review is done by someone with admin or owner permissions, in branch protection or a ruleset.

Where the file goes

Create a file named CODEOWNERS in one of three places on the branch it should apply to:

  1. .github/CODEOWNERS
  2. CODEOWNERS at the repository root
  3. docs/CODEOWNERS

If more than one exists, GitHub searches in that order and uses the first it finds. Keep a single file, in .github/, so nobody edits one that is being ignored.

Each branch has its own CODEOWNERS file, and a pull request reads the file on its base branch. A pull request into main uses main's CODEOWNERS, whatever the head branch contains. Two consequences:

  • A pull request that edits CODEOWNERS doesn't change who reviews that pull request. The new owners apply from the moment it merges.
  • A pull request from a fork into the upstream repository uses the upstream base branch's file.

The file must be under 3 MB. A larger file isn't loaded at all: no owners are shown and no reviews are requested. Wildcards that cover many paths in one line keep it small.

The syntax

Each line is a path pattern followed by one or more owners. Owners are written as @username, @org/team-name, or in most cases an email address added to a user's account (not for managed user accounts). Lines starting with # are comments, and a # after the owners starts an inline comment.

# Default owners for everything in the repository
*                       @acme/platform

# Any .tf file, anywhere
*.tf                    @acme/infra

# The /payments directory at the root, and everything under it
/payments/              @acme/payments @lee

# Any directory named migrations, at any depth
**/migrations           @acme/data

# Files directly in /docs, but not in its subdirectories
/docs/*                 @acme/docs

# Workflows and this file itself
/.github/workflows/     @acme/platform
/.github/CODEOWNERS     @acme/repo-admins

Patterns follow most of the rules of .gitignore:

Pattern Matches
* Every file in the repository
*.js Files ending in .js, at any depth
/build/logs/ The build/logs directory at the root and everything beneath it
apps/ Any directory named apps, anywhere, and everything in it
docs/* Files directly inside docs, but not docs/guides/intro.md
**/logs Any logs directory at any depth, such as /build/logs and /deeply/nested/logs

Three .gitignore features don't work in CODEOWNERS:

  • ! to negate a pattern;
  • [ ] character ranges;
  • \# to escape a pattern that begins with #.

Paths are case sensitive, even when your own machine's file system isn't, because GitHub evaluates the file. /Payments/ and /payments/ are different patterns.

The last match wins

This is the rule behind most surprises. GitHub doesn't combine the owners of every line that matches a file. Only the last matching line counts.

*           @acme/platform
*.js        @acme/frontend

A pull request that only changes .js files requests @acme/frontend, not @acme/platform. The default owners on the first line apply only to files no later line matches.

So write the file from general to specific: the catch-all first, then directories, then the narrow exceptions. And when two owners should share a path, put them on the same line. Listing the same pattern twice with one owner each leaves only the second.

A pattern with no owners after it makes the matching files unowned. This carves an exception out of a broader rule:

/apps/          @acme/apps
/apps/sandbox

Changes under /apps/sandbox then need no code owner review; anyone with write access can approve them. Giving that line its own owner (/apps/sandbox @lee) hands the subdirectory to someone else instead.

Making an owner's approval required

On its own, CODEOWNERS only requests reviews. To make an owner's approval a merge requirement, a repository administrator turns it on for the branch, in either place:

  • Branch protection rule: edit the rule for the branch and enable Require review from Code Owners (it sits with the required pull request reviews).
  • Ruleset: in the rule that requires a pull request before merging, enable the code owner review requirement. GitHub points out that rulesets can apply several at once and that people without admin access can see which rules apply.

With it on, any pull request that changes owned files can't merge until an owner of those files approves. Some details from GitHub's documentation:

  • One owner's approval is enough. For *.js @alex @sam, an approval from either satisfies the requirement. Raising the number of required approving reviews adds reviewers, but doesn't make both named owners approve.
  • Every owned file needs one of its owners. A pull request that touches /payments/ and *.tf files needs approval from an owner of each.
  • Approvals can go stale. With Dismiss stale pull request approvals when new commits are pushed, a push that changes the diff dismisses earlier approvals. Require approval of the most recent reviewable push keeps approvals but needs someone other than the last pusher to approve the latest changes. When an agent keeps pushing fixes after review, one of the two is what stops new, unreviewed changes riding in on an old approval.
  • Admins bypass branch protection by default. A branch protection rule doesn't apply to repository administrators unless Do not allow bypassing the above settings is on.

Protect the file itself

If anyone with write access can change CODEOWNERS, anyone can change who must approve their next pull request. GitHub's advice is to keep the file in .github/ and give it (or the whole directory) an owner you trust:

/.github/               @acme/repo-admins

Put the same guard on the files that steer an agent's work: CI workflows, AGENTS.md and other agent instruction files, dependency manifests. A pull request that changes how agents are told to behave, or what CI checks, should need the people responsible for it, not just any reviewer. Our piece on AGENTS.md context files covers what goes in those files.

Teams as owners, and spreading the load

Naming a team as owner (@acme/payments) is usually better than naming people: it survives people leaving and changing roles. By default the whole team is requested and notified. A team's Code review settings change that:

  • Auto assignment replaces the team request with a set number of members, chosen by round robin (whoever had the least recent request) or load balance (evening out review requests over 30 days, taking outstanding reviews into account). Members whose status is set to Busy are skipped. You can also exclude members and include child teams.
  • Only notify requested team members stops the whole team being notified when a specific member has also been requested.

When code owner review is required, the team request stays on the pull request alongside the assigned members, and is removed once one of them completes a review.

Auto assignment matters when agents open many pull requests a day: without it, a whole team is notified of every one, and reviews go to whoever happens to look first.

Draft pull requests

Code owners aren't requested on draft pull requests. They're notified when the draft is marked ready for review. If your agents open drafts and a human promotes them, owners hear about a change only at that point; if your agents open ready pull requests directly, every one requests its owners straight away.

When owners aren't requested

Symptom Likely cause
No one is ever requested The file isn't on the pull request's base branch, it's in a location GitHub ignores because another CODEOWNERS file comes first, or it's over 3 MB
One line has no effect Invalid syntax: GitHub skips that line. Open the file on GitHub to see errors highlighted
A team or user is never requested They don't have explicit write access, the team is secret rather than visible, or the name doesn't exist
The wrong owner is requested A later line also matches the file; only the last match counts
Only one of two owners is requested The pattern appears twice, one owner per line; put both owners on one line
A path never matches Case differs between the pattern and the path, or the pattern uses ! or [ ]
Owners weren't asked on an agent's pull request It was opened as a draft; they're requested when it's marked ready
The pull request merged without an owner Code owner review isn't required on that branch, the files were unowned, or an administrator bypassed the rule

Check the file automatically

The REST API lists the syntax errors GitHub found in the file, line by line, with a suggestion where it has one:

gh api repos/OWNER/REPO/codeowners/errors

Add ?ref=<branch> to check another branch's file; the default is the repository's default branch. An empty errors array means every line parsed. Running this in CI after a change to CODEOWNERS catches a mistyped team before it silently drops a review.

Ownership is routing, not review

CODEOWNERS decides who looks at a change; it doesn't decide what they look for. When agents produce more pull requests than owners can read line by line, the owners' approval is only as good as the attention behind it. Our pieces on code review bottlenecks when agents outpace reviewers, reviewing by invariant boundaries and risk scoring agent pull requests cover where that attention is best spent. Once the owners approve, the merge queue guide covers getting the change into main without breaking it.

Sources

  1. About code owners · GitHub Docs
  2. About protected branches · GitHub Docs
  3. Available rules for rulesets · GitHub Docs
  4. Managing code review settings for your team · GitHub Docs
  5. List CODEOWNERS errors · GitHub REST API

Related reading