Public: anyone can read this; the crew can change it.
commitToBranch refuses an agent's commit to the default branch, as the push hook does #14945
Check · Review · Land · Live done — Landed and live in production.
commitToBranch now refuses an agent's commit to the default branch with protectedbranch, whatever protectDefault says. That closes the gap where the API (POST …/contents) and the MCP writefile tool let an agent commit straight to main.
shipped production is serving it deploy 6b7374e
commitToBranch now refuses an agent's commit to the default branch with protected_branch, whatever protectDefault says. That closes the gap where the API (POST …/contents) and the MCP write_file tool let an agent commit straight to main. The refusal applies the same rule the push hook already does (src/http/server.ts:2413): agents propose through pulls and a person merges.
What changed
src/actions/repos.ts: the agent check runs before the existing protected-default admin check. Without it, an agent writing to a repository where it is admin got through the old check on main. A person's commit to the default branch, and an agent's commit to any other branch, work as before.src/actions/edit_words.ts: the sharedprotected_branchsentence now adds "and an agent never does: open a pull from a branch". The old sentence alone would tell an admin agent that "only a repo admin may commit".test/edit-refusal-words.test.tspins that sentence, so I updated it to the new wording on purpose.test/commit-to-branch-agent.test.ts(new): an agent writes to a repository it owns (so it is admin there). It is refused on the default branch withprotectDefaultoff and on, whether the branch is named or left out. It can still commit to its own branch, and a person's commits to the default branch still land. The two refusal tests fail on today's main.
Checked
- The
src/callers ofcommitToBranchthat target the default branch don't act as agents:newAppfollowscreateRepo, which already refuses agents withforbidden_owner, and the preview seed and chaos world run as people. - These pass: the new test,
edit-refusal-words,protected-default-pushandmcp-size.scripts/gate.ts --fixandbun x tsc --noEmitare clean. scripts/fast.tsnamed 2966 files, too broad to run here. A wider run of 845 files that reachcommitToBranchgave 6747 pass and 21 fail under load. When I re-ran the failing files alone they passed, excepttest/mcp/forks.test.ts, which fails the same way on main.
Left out: the agent refusal reuses the protected_branch kind, as the issue asks, rather than adding its own kind.
Try it: as an agent, call MCP write_file (or POST /api/v1/repos/<owner>/<name>/contents) with no branch or the default branch: it answers 403 protected_branch. The same call on a branch of the agent's own commits.
🤖 Generated with Claude Code
AI agents can no longer commit to the default branch through commitToBranch (src/actions/repos.ts), whether or not the branch is protected. The push hook already enforced this rule; now the API and MCP do too. An agent asking to write there gets protected_branch, and the same write to a branch of its own still commits. A person's commit works as before. The protected_branch message (src/actions/edit_words.ts) now also says that an agent never commits to the default branch and should open a pull from a branch.
This change has no terminal part. The terminal has no screen that commits files, so commitToBranch is only reached through the API and MCP, and there is nothing new to show there. An empty commit carries the No door: line that says so, which is what the "both doors" gate rule asks for.
test/commit-to-branch-agent.test.ts covers an agent writing with no branch, to the default branch, and to its own branch, plus a person writing to the default branch. test/edit-refusal-words.test.ts follows the new wording. bun scripts/gate.ts, bun x tsc --noEmit and both test files pass here.
To try it: as an agent, call the MCP write_file tool or POST /api/v1/repos/<owner>/<name>/contents with no branch or the default branch. It answers protected_branch. The same call on a branch of the agent's own commits.
🤖 Generated with Claude Code
The rule matches the push hook (src/http/server.ts:2413) and runs before the admin check, so an agent that is admin of its own repo is still refused. The test covers named and implied branch, protectDefault on and off, the agent's own branch, and a person's commit. No page or terminal surface changes, so the preview has nothing to show here. The only pin of the old sentence is the updated words test.
Two things I would do next:
- Add a test that the API (POST contents) and MCP write_file answer 403 protected_branch, so the door is pinned and not just the action.
- Share one predicate (agent and default branch) between the push hook and commitToBranch so the two cannot drift.
This is not pipeline work, but it is a small, single-purpose security fix.
commitToBranchnow refuses an agent's commit to the default branch withprotected_branch, whateverprotectDefaultsays. That closes the gap where the API (POST …/contents) and the MCPwrite_filetool let an agent commit straight to main. The refusal applies the same rule the push hook already does (src/http/server.ts:2413): agents propose through pulls and a person merges.What changed
src/actions/repos.ts: the agent check runs before the existing protected-default admin check. Without it, an agent writing to a repository where it is admin got through the old check on main. A person's commit to the default branch, and an agent's commit to any other branch, work as before.src/actions/edit_words.ts: the sharedprotected_branchsentence now adds "and an agent never does: open a pull from a branch". The old sentence alone would tell an admin agent that "only a repo admin may commit".test/edit-refusal-words.test.tspins that sentence, so I updated it to the new wording on purpose.test/commit-to-branch-agent.test.ts(new): an agent writes to a repository it owns (so it is admin there). It is refused on the default branch withprotectDefaultoff and on, whether the branch is named or left out. It can still commit to its own branch, and a person's commits to the default branch still land. The two refusal tests fail on today's main.Checked
src/callers ofcommitToBranchthat target the default branch don't act as agents:newAppfollowscreateRepo, which already refuses agents withforbidden_owner, and the preview seed and chaos world run as people.edit-refusal-words,protected-default-pushandmcp-size.scripts/gate.ts --fixandbun x tsc --noEmitare clean.scripts/fast.tsnamed 2966 files, too broad to run here. A wider run of 845 files that reachcommitToBranchgave 6747 pass and 21 fail under load. When I re-ran the failing files alone they passed, excepttest/mcp/forks.test.ts, which fails the same way on main.Left out: the agent refusal reuses the
protected_branchkind, as the issue asks, rather than adding its own kind.Try it: as an agent, call MCP
write_file(orPOST /api/v1/repos/<owner>/<name>/contents) with no branch or the default branch: it answers 403protected_branch. The same call on a branch of the agent's own commits.🤖 Generated with Claude Code
Closes #14913