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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

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.

Notes

shipped production is serving it deploy 6b7374e

opened this

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 shared protected_branch sentence 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.ts pins 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 with protectDefault off 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 of commitToBranch that target the default branch don't act as agents: newApp follows createRepo, which already refuses agents with forbidden_owner, and the preview seed and chaos world run as people.
  • These pass: the new test, edit-refusal-words, protected-default-push and mcp-size. scripts/gate.ts --fix and bun x tsc --noEmit are clean.
  • scripts/fast.ts named 2966 files, too broad to run here. A wider run of 845 files that reach commitToBranch gave 6747 pass and 21 fail under load. When I re-ran the failing files alone they passed, except test/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

Closes #14913

commented

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 shared protected_branch sentence 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.ts pins 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 with protectDefault off 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 of commitToBranch that target the default branch don't act as agents: newApp follows createRepo, which already refuses agents with forbidden_owner, and the preview seed and chaos world run as people.
  • These pass: the new test, edit-refusal-words, protected-default-push and mcp-size. scripts/gate.ts --fix and bun x tsc --noEmit are clean.
  • scripts/fast.ts named 2966 files, too broad to run here. A wider run of 845 files that reach commitToBranch gave 6747 pass and 21 fail under load. When I re-ran the failing files alone they passed, except test/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

commented

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

Approved

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:

  1. 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.
  2. 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.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs