Last reviewed: 2026-09-30
Direct answer
Treat code ownership as a merge control, not as a courtesy notification. If you are implementing code owner approval for coding agent pull requests, first identify the repository paths where an agent change needs domain expertise: deployment definitions, authentication boundaries, data migrations, billing logic, or other high-impact modules. Map each path to a person or team in a CODEOWNERS file on the base branch. Then protect the target branch and require an approving review from the relevant owner before merging. Add ordinary status checks as a second condition, and make the gate inspect the final diff rather than an earlier draft.
On GitHub, the documented model is a CODEOWNERS file plus branch protection. GitHub automatically requests owners when a ready-for-review pull request changes a path they own; an administrator can additionally require code-owner approval before merge. The file is searched in .github/, the repository root, then docs/, and the first file found for the base branch is used. Paths are case-sensitive, and the last matching pattern takes precedence. Those details make the base branch and pattern order part of your security contract, not an implementation detail. See GitHub’s code owner documentation
for the supported locations, syntax, access requirements, and review-request behavior.
On GitLab, use Code Owners together with a protected target branch and enabled Code Owner approval. GitLab also supports approval rules for expertise that does not map cleanly to a path. Its documentation warns that an account allowed to push and merge can bypass merge-request protections, so automation identities need a deliberate permission decision. See GitLab’s Code Owners documentation when adapting the workflow to GitLab.
A useful policy is: an agent may propose and test a change, but it cannot satisfy the ownership gate itself. The gate should fail closed when no owner matches, an owner lacks repository write access, a required review is stale, or a protected-branch rule is absent. That separation keeps speed benefits from silently becoming authority to merge.
Who this is for
This guide is for maintainers of repositories where coding agents open pull requests, repair tests, update dependencies, or edit deployment and service code. It is especially useful for teams with several ownership domains, parallel agents, or a release branch that must remain reviewable. It assumes you can edit repository policy and branch settings, but it does not assume a particular agent framework or CI vendor.
Key takeaways
- Define ownership for risk-bearing paths, including a deliberate fallback for files outside those paths.
- Keep the authoritative
CODEOWNERSfile on the pull request’s base branch and verify its location, case, pattern order, and owner access. - Pair ownership with protected-branch requirements for approving reviews, code-owner approval, and required status checks.
- Validate syntax, duplicate patterns, file existence, and owner definitions before relying on a gate. The public codeowners-validator project documents checks for these conditions and optional checks for unowned files and shadowing.
- Treat every new commit as a new review surface. Configure stale-review behavior or require a review of the latest reviewable push.
- Log the decision and matching policy without recording prompts, source contents, or credentials.
Sources checked
The workflow is grounded in four public sources that were checked for this article:
- About code owners — GitHub Docs documents file locations, path syntax, owner permissions, automatic review requests, and the relationship with branch protection.
- About protected branches — GitHub Docs documents required reviews, code-owner approval, status checks, stale approvals, and bypass settings.
- Code Owners — GitLab Docs documents path ownership, approval rules, protected branches, multiple approvals, and the direct-push bypass consideration.
- codeowners-validator documents automated checks for syntax, duplicate patterns, file existence, valid owners, unowned files, and shadowing.
Contract details to verify
Write the contract down before changing repository settings. Start with a small risk matrix: path pattern, owner group, minimum approvals, and the reason the path is sensitive. Keep patterns narrow enough that the responsible reviewer can understand the change. A broad global owner can be a useful fallback, but it should not hide an important directory that has no specialist owner.
For GitHub, a minimal illustrative file could look like this:
# Default review for files without a more specific owner
* @org/engineering-reviewers
# Higher-risk areas
/infra/ @org/platform-reviewers
/.github/workflows/ @org/release-reviewers
/services/billing/ @org/billing-reviewers
Replace the placeholder teams with teams that have explicit write access. Keep all owners for a pattern on the same line, and review later, more-specific patterns for accidental shadowing. Do not assume a file on a feature branch is active: the relevant CODEOWNERS file is on the base branch used by the pull request.
Next, configure the target branch. Require pull-request reviews, require approval from code owners for owned paths, and require the status checks that prove the agent’s change was tested. Decide whether to dismiss stale approvals when commits change the diff or to require approval from someone other than the latest pusher. Also decide who, if anyone, can bypass the rules; record that exception as a separate operational policy rather than leaving it implicit.
Run a validator as an early check and again when ownership policy changes. The validator’s documented checks can be invoked without placing sensitive values in a log:
REPOSITORY_PATH=. \
CHECKS=files,owners,duppatterns,syntax \
codeowners-validator
If your repository needs complete coverage, evaluate the validator’s optional unowned-file and shadowing checks as well. Pin the validator to a reviewed release and update it through the same ownership process as any other build tool. A validator confirms the policy file is coherent; it does not replace a human owner who understands the behavior being changed.
Happy-path operator workflow
- The agent opens a pull request from an isolated branch and supplies a concise change scope, test evidence, and the list of paths it touched. A useful companion is a change-scope note before an agent pull request .
- The gate resolves each changed path against the base branch’s ownership file. It records the matching owner group, whether the branch is protected, and whether the required status checks are complete.
- The platform requests the owner review. The owner inspects the diff, test evidence, and any generated migration or configuration details; the owner, not the agent, makes the approval decision.
- When all required reviews and checks pass, the normal merge or queue process proceeds. Retain a small decision record so an incident investigator can reconstruct which policy and diff were approved.
Use a sanitized event such as the following for that record:
{
"event": "code_owner_gate",
"pull_request": 184,
"base_branch": "main",
"changed_path_count": 3,
"matched_owner_groups": ["platform-reviewers"],
"required_owner_approval": true,
"review_state": "approved",
"status_checks": "passed",
"diff_reference": "[REDACTED]",
"decision": "eligible_for_merge"
}
Keep the record to routing and outcome fields. Do not copy prompts, source files, patch contents, or credentials into the event. If a human needs the actual diff, point to the pull request’s access-controlled review surface instead.
Error-path operator workflow
If no owner is requested, stop the merge and classify the error before retrying. Check, in order: the base branch contains the intended file; the file is in a supported location; path casing and pattern order are correct; the owner account or team exists and has the required access; and the pull request is ready for review rather than still a draft. Run the validator and inspect its syntax and file-existence output. If the branch rule is missing, restore the rule before asking for another approval. If a new commit changed the diff after approval, request a fresh review rather than treating the old approval as reusable. If an automation identity can push directly to the protected branch, remove that bypass or route the change through a merge request. Record the failure reason, corrective action, and final policy version, then rerun the gate.
Failure modes
The wrong ownership file is active. GitHub searches supported locations in a defined order and uses the first file it finds. A stale file in .github/ can therefore win over a carefully edited root file. Keep one authoritative location where possible, and test the file from the target branch.
A valid-looking line is skipped. GitHub notes that some gitignore features do not work in CODEOWNERS; invalid syntax causes a line to be skipped. Case errors, unsupported negation, and owners without the necessary access can leave a sensitive path unowned. The validator’s syntax, owner, and file checks catch many of these issues before a pull request arrives.
Pattern shadowing routes a change to the wrong team. Pattern order matters, and a later match can replace an earlier owner. Review the complete match set for every high-risk path, not just the line that appears most obvious. Enable a shadowing check where practical.
Draft status is mistaken for protection. GitHub does not automatically request code-owner review for a draft pull request. Make “ready for review” an explicit transition in the agent runbook, and do not use draft status as evidence that a merge gate has passed.
An approval is stale. A new commit, a changed merge base, or an updated branch can alter the approved diff. With stale-dismissal enabled, the platform blocks the merge until approval returns; without it, require approval of the latest reviewable push to prevent unreviewed additions from riding on an old decision.
The platform policy is bypassed. A direct push permission, an administrator exception, or a release account with merge rights can make the review contract ineffective. Audit bypass actors and test the failure path with a harmless change in a non-production branch.
The policy becomes unmaintainable. A huge or duplicated file is difficult to review and may exceed platform limits. Consolidate repeated patterns, keep ownership teams current, and make validator output a required status check. Ownership should evolve with the codebase, not become a one-time setup artifact.
FAQ
Does every agent pull request need a specialist?
No. Map specialists to risk-bearing paths and define a sensible default owner for the rest. The point is to make the required expertise explicit; it is not to create an approval bottleneck for every documentation-only change.
Is a review request the same as an approval gate?
No. A request is a notification. A protected-branch rule that requires an approving review from a code owner is the enforceable condition. Verify both the notification and the merge rule in a test pull request.
Can a validator decide whether a change is safe?
No. It can find malformed patterns, duplicate entries, missing files, invalid owners, and (when enabled) unowned or shadowed paths. It cannot understand whether a migration is compatible or whether a reviewer made a sound design decision. Keep the human owner in the loop.
What should happen when ownership is disputed?
Block the merge, record the disputed path and proposed owner groups, and ask the repository maintainers to update the policy. Do not solve a dispute by granting a temporary broad bypass to the agent. After the policy change lands on the base branch, rerun validation and obtain a fresh review of the final diff.
Reader next step
Choose one repository and one protected target branch. List its five highest-risk path groups, assign owners with the necessary repository access, and add a deliberately harmless pull request that touches one file in each group. Confirm that the expected owner is requested, the protected branch blocks an unapproved merge, the validator reports a clean policy, and a follow-up commit invalidates approval according to your chosen rule. Save the sanitized event fields and the final policy decision. For the surrounding handoff process, see how to hand off coding-agent pull requests for review .
Once that exercise passes, make the validator and ownership gate required checks for real agent pull requests. Revisit the matrix whenever a sensitive directory, reviewer team, branch rule, or automation permission changes.