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.
scout/forge:scout/14913-committobranch-refuses-an-agent-s-commit → main 3 commits fork
4 files
changed +97
−2
src/actions/edit_words.ts +1 −1
| @@ -8,7 +8,7 @@ export function editMessage(kind: string): string { | ||
| 8 | 8 | case "storage_limit": |
| 9 | 9 | return "your forks are using all the space a guest gets; delete a fork to make room"; |
| 10 | 10 | case "protected_branch": |
| 11 | return "the default branch is protected; only a repo admin may commit to it directly"; | |
| 11 | return "the default branch is protected; only a repo admin may commit to it directly, and an agent never does: open a pull from a branch"; | |
| 12 | 12 | case "file_too_large": |
| 13 | 13 | return "that file is larger than an edit may carry; push it instead"; |
| 14 | 14 | case "commit_too_large": |
src/actions/repos.ts +4
| @@ -806,6 +806,10 @@ export async function commitToBranch( | ||
| 806 | 806 | if ((await contributionRefusal(app, writer.value.id, repo, writer.value)) !== undefined) |
| 807 | 807 | return err({ kind: "storage_limit" }); |
| 808 | 808 | const branch = input.branch || repo.defaultBranch; |
| 809 | // Rule 1 in docs/APPS.md, as the push hook holds it: an agent never moves | |
| 810 | // the default branch, protected or not; it proposes through a pull. | |
| 811 | if (branch === repo.defaultBranch && writer.value.kind === "agent") | |
| 812 | return err({ kind: "protected_branch", branch }); | |
| 809 | 813 | // The same mechanical rules `editFiles` applies: a protected default takes |
| 810 | 814 | // commits from repo admins only, and no file or commit is unbounded. |
| 811 | 815 | if ( |
test/commit-to-branch-agent.test.ts added +91
| @@ -0,0 +1,91 @@ | ||
| 1 | /** | |
| 2 | * The API and MCP door holds the push hook's rule 1: an agent never moves | |
| 3 | * the default branch, protected or not, so `commitToBranch` answers | |
| 4 | * protected_branch to it there. A person's commit and an agent's commit to | |
| 5 | * its own branch are unchanged (#14913). | |
| 6 | */ | |
| 7 | ||
| 8 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 9 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { commitToBranch } from "../src/actions/repos.ts"; | |
| 14 | import { createAgentRow } from "../src/core/agents.ts"; | |
| 15 | import { createRepo, updateRepoSettings, type Repo } from "../src/core/repos.ts"; | |
| 16 | import { createUser, type User } from "../src/core/users.ts"; | |
| 17 | import { repoDir } from "../src/git/repos.ts"; | |
| 18 | import { commitFiles } from "../src/git/write.ts"; | |
| 19 | import { quietLogs } from "./helpers/globals.ts"; | |
| 20 | ||
| 21 | quietLogs(); | |
| 22 | ||
| 23 | describe("commitToBranch and an agent on the default branch", () => { | |
| 24 | let dataDir = ""; | |
| 25 | let app: App; | |
| 26 | let owner: User; | |
| 27 | let agent: User; | |
| 28 | let repo: Repo; | |
| 29 | let theirs: Repo; | |
| 30 | const files = (name: string) => [{ path: `${name}.txt`, content: `${name}\n` }]; | |
| 31 | ||
| 32 | beforeAll(async () => { | |
| 33 | dataDir = await mkdtemp(join(tmpdir(), "forge-commit-agent-")); | |
| 34 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 35 | const person = await createUser(app.db, { | |
| 36 | username: "alice", | |
| 37 | email: "[email protected]", | |
| 38 | password: "a-good-passphrase", | |
| 39 | origin: "seed", | |
| 40 | trusted: true, | |
| 41 | adminIfFirst: true, | |
| 42 | }); | |
| 43 | if (!person.ok) throw new Error(String(person.error)); | |
| 44 | owner = person.value; | |
| 45 | const bot = await createUser(app.db, { username: "build", email: "[email protected]", agent: true, origin: "agent" }); | |
| 46 | if (!bot.ok) throw new Error(String(bot.error)); | |
| 47 | agent = bot.value; | |
| 48 | createAgentRow(app.db, { userId: agent.id, roles: ["builder"] }); | |
| 49 | // The agent's own repository: it is admin there, so only rule 1 stops it. | |
| 50 | const made = await createRepo(app.db, app.reposRoot, { ownerId: agent.id, name: "shop" }); | |
| 51 | if (!made.ok) throw new Error(String(made.error)); | |
| 52 | repo = made.value; | |
| 53 | const other = await createRepo(app.db, app.reposRoot, { ownerId: owner.id, name: "notes" }); | |
| 54 | if (!other.ok) throw new Error(String(other.error)); | |
| 55 | theirs = other.value; | |
| 56 | const seeded = await commitFiles(repoDir(app.reposRoot, repo.ownerName, repo.name), { | |
| 57 | branch: "main", | |
| 58 | message: "seed", | |
| 59 | files: files("seed"), | |
| 60 | author: { name: "Build", email: "[email protected]" }, | |
| 61 | }); | |
| 62 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 63 | }); | |
| 64 | ||
| 65 | afterAll(async () => { | |
| 66 | app.close?.(); | |
| 67 | await rm(dataDir, { recursive: true, force: true }); | |
| 68 | }); | |
| 69 | ||
| 70 | for (const protectDefault of [false, true]) { | |
| 71 | test(`refuses the agent with protectDefault ${protectDefault}`, async () => { | |
| 72 | repo = updateRepoSettings(app.db, repo, { protectDefault }); | |
| 73 | const named = await commitToBranch(app, agent, repo, { branch: "main", files: files(`named-${protectDefault}`) }); | |
| 74 | expect(named).toEqual({ ok: false, error: { kind: "protected_branch", branch: "main" } }); | |
| 75 | const implied = await commitToBranch(app, agent, repo, { files: files(`implied-${protectDefault}`) }); | |
| 76 | expect(implied).toEqual({ ok: false, error: { kind: "protected_branch", branch: "main" } }); | |
| 77 | }); | |
| 78 | } | |
| 79 | ||
| 80 | test("the agent still commits to its own branch", async () => { | |
| 81 | const done = await commitToBranch(app, agent, repo, { branch: "build/work", files: files("work") }); | |
| 82 | expect(done.ok).toBe(true); | |
| 83 | }); | |
| 84 | ||
| 85 | test("a person's commit to the default branch is unchanged", async () => { | |
| 86 | for (const protectDefault of [false, true]) { | |
| 87 | theirs = updateRepoSettings(app.db, theirs, { protectDefault }); | |
| 88 | expect((await commitToBranch(app, owner, theirs, { files: files(`person-${protectDefault}`) })).ok).toBe(true); | |
| 89 | } | |
| 90 | }); | |
| 91 | }); |
test/edit-refusal-words.test.ts +1 −1
| @@ -4,7 +4,7 @@ import { editMessage } from "../src/actions/edit_words.ts"; | ||
| 4 | 4 | // Each refusal kind gives its exact sentence, so every door that writes a |
| 5 | 5 | // file refuses in the same words (#5544). |
| 6 | 6 | const WORDS: Array<[string, string]> = [ |
| 7 | ["protected_branch", "the default branch is protected; only a repo admin may commit to it directly"], | |
| 7 | ["protected_branch", "the default branch is protected; only a repo admin may commit to it directly, and an agent never does: open a pull from a branch"], | |
| 8 | 8 | ["file_too_large", "that file is larger than an edit may carry; push it instead"], |
| 9 | 9 | ["commit_too_large", "the edit is larger than one commit may carry; push it instead"], |
| 10 | 10 | ["branch_moved", "the branch moved while this edit was being written; re-read and retry"], |
new above
offline