Public: anyone can read this; the crew can change it.
A merge conflict is not a failed try: the pull is brought up to date and its tries are kept #7918
mason/forge:mason/7909-a-merge-conflict-is-not-a-failed-try-the → main 3 commits fork
7 files
changed +305
−5
src/actions/list-conflicts.ts added +76
| @@ -0,0 +1,76 @@ | ||
| 1 | /** | |
| 2 | * A pull that conflicts only in list-like docs (`LIST_DOCS`) is brought up to | |
| 3 | * date by the forge: the base merged into its head with both sides' lines | |
| 4 | * kept, one commit on the head branch. No clone, no run, no model — the | |
| 5 | * conflicts every landing on docs/MCP.md makes are not worth one. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { App } from "../app.ts"; | |
| 9 | import { forgeHost, noreplyAddress } from "../core/commit_identity.ts"; | |
| 10 | import { trackPullHead, type Pull } from "../core/pulls.ts"; | |
| 11 | import type { Repo } from "../core/repos.ts"; | |
| 12 | import { conflictedTree } from "../git/conflict-file.ts"; | |
| 13 | import { runGit } from "../git/env.ts"; | |
| 14 | import { mergeable, mergeBase, resolveOid } from "../git/merge.ts"; | |
| 15 | import { repoDir } from "../git/repos.ts"; | |
| 16 | import { isListDoc, unionMergeFile } from "../git/union-merge.ts"; | |
| 17 | import { commitFiles } from "../git/write.ts"; | |
| 18 | ||
| 19 | /** Whether every conflicted path is one a list-merge settles. */ | |
| 20 | export const onlyListDocs = (files: readonly string[]): boolean => | |
| 21 | files.length > 0 && files.every(isListDoc); | |
| 22 | ||
| 23 | /** | |
| 24 | * Merge the base into the pull's head, keeping both sides of every list-doc | |
| 25 | * clash, when those are the only files that conflict. The commit id when the head moved; undefined when this is not a pull | |
| 26 | * the forge can settle, or the branch moved meanwhile (the next look decides). | |
| 27 | */ | |
| 28 | export async function resolveListConflicts( | |
| 29 | app: App, | |
| 30 | repo: Repo, | |
| 31 | pull: Pull, | |
| 32 | ): Promise<string | undefined> { | |
| 33 | if (pull.state !== "open" || pull.mergedAt !== null) return undefined; | |
| 34 | if (pull.crossRepo || pull.originHeadRepo !== null || repo.origin !== null) return undefined; | |
| 35 | const dir = repoDir(app.reposRoot, repo.ownerName, repo.name); | |
| 36 | const head = await resolveOid(dir, `refs/heads/${pull.headBranch}`); | |
| 37 | const tip = await resolveOid(dir, `refs/heads/${pull.baseBranch}`); | |
| 38 | if (head === undefined || tip === undefined) return undefined; | |
| 39 | // Asked of git now, not of the red that sent us: the base may have moved. | |
| 40 | const now = await mergeable(dir, tip, head); | |
| 41 | if (now.ok || now.error.kind !== "conflict" || !onlyListDocs(now.error.files)) return undefined; | |
| 42 | const files = now.error.files; | |
| 43 | const base = await mergeBase(dir, tip, head); | |
| 44 | const tree = await conflictedTree(dir, tip, head); | |
| 45 | if (base === undefined || tree === undefined) return undefined; | |
| 46 | const written: { path: string; content: string; mode: "100644" | "100755" }[] = []; | |
| 47 | for (const path of files) { | |
| 48 | const content = await unionMergeFile(dir, base, head, tip, path); | |
| 49 | const entry = await runGit(["ls-tree", tree, "--", path], { cwd: dir }); | |
| 50 | const mode = entry.ok ? entry.value.split(" ")[0] : undefined; | |
| 51 | if (content === undefined || (mode !== "100644" && mode !== "100755")) return undefined; | |
| 52 | written.push({ path, content, mode }); | |
| 53 | } | |
| 54 | const committed = await commitFiles(dir, { | |
| 55 | branch: pull.headBranch, | |
| 56 | message: `Merge ${pull.baseBranch} into ${pull.headBranch}\n\nKept both sides in ${files.join(", ")}.`, | |
| 57 | files: written, | |
| 58 | seedTree: tree, | |
| 59 | mergeParents: [tip], | |
| 60 | expectTip: head, | |
| 61 | author: { name: "forge", email: noreplyAddress("forge", forgeHost(app.config)) }, | |
| 62 | }); | |
| 63 | if (!committed.ok) return undefined; | |
| 64 | const commitOid = committed.value.commitOid; | |
| 65 | trackPullHead(app.db, repo.id, pull.headBranch, commitOid); | |
| 66 | // `update-ref` runs no hooks: announce the push as a git push would be. | |
| 67 | app.bus.append({ | |
| 68 | kind: "push", | |
| 69 | repoId: repo.id, | |
| 70 | payload: { | |
| 71 | updates: [{ ref: `refs/heads/${pull.headBranch}`, newOid: commitOid }], | |
| 72 | viaApi: true, | |
| 73 | }, | |
| 74 | }); | |
| 75 | return commitOid; | |
| 76 | } |
src/core/runs.ts +6 −4
| @@ -1004,8 +1004,10 @@ export function latestRunsForIssues( | ||
| 1004 | 1004 | } |
| 1005 | 1005 | |
| 1006 | 1006 | /** |
| 1007 | * How many automatic follow-ups an agent has had on a pull since a person | |
| 1008 | * last spoke: the bound behind rule 7 in docs/APPS.md. `since` is the | |
| 1007 | * How many automatic follow-ups for a failed check an agent has had on a | |
| 1008 | * pull since a person last spoke: the bound behind rule 7 in docs/APPS.md. | |
| 1009 | * A bring-up-to-date run (`conflict`) is no try: main moving is not the | |
| 1010 | * change's failing, and those runs are bounded where they are queued. `since` is the | |
| 1009 | 1011 | * moment of the last human comment or review, or the pull's creation. |
| 1010 | 1012 | */ |
| 1011 | 1013 | export function automaticRunsSince( |
| @@ -1017,7 +1019,7 @@ export function automaticRunsSince( | ||
| 1017 | 1019 | .query<{ n: number }, [string, string]>( |
| 1018 | 1020 | `SELECT COUNT(*) AS n FROM run |
| 1019 | 1021 | WHERE kind = 'agent' AND issue_id = ?1 AND created_at > ?2 |
| 1020 | AND reason IN ('check_failed','conflict')`, | |
| 1022 | AND reason = 'check_failed'`, | |
| 1021 | 1023 | ) |
| 1022 | 1024 | .get(issueId, since); |
| 1023 | 1025 | return row?.n ?? 0; |
| @@ -1041,7 +1043,7 @@ export function automaticRunsSinceFor( | ||
| 1041 | 1043 | `WITH since (issue_id, at) AS (VALUES ${batch.map((_, n) => `(?${2 * n + 1}, ?${2 * n + 2})`).join(",")}) |
| 1042 | 1044 | SELECT run.issue_id, COUNT(*) AS n FROM run JOIN since ON since.issue_id = run.issue_id |
| 1043 | 1045 | WHERE run.kind = 'agent' AND run.created_at > since.at |
| 1044 | AND run.reason IN ('check_failed','conflict') | |
| 1046 | AND run.reason = 'check_failed' | |
| 1045 | 1047 | GROUP BY run.issue_id`, |
| 1046 | 1048 | ) |
| 1047 | 1049 | .all(...batch.flat()); |
src/crew/autonomy.ts +9
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | * repository's origin. |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | import { onlyListDocs, resolveListConflicts } from "../actions/list-conflicts.ts"; | |
| 15 | 16 | import { closedToday, closeStale } from "./stale_pulls.ts"; |
| 16 | 17 | import { authorsInRepoPull } from "./authors_pull.ts"; |
| 17 | 18 | import type { Database } from "bun:sqlite"; |
| @@ -1821,6 +1822,14 @@ export async function sweepConflicts( | ||
| 1821 | 1822 | // push must not be queued again on every tick. |
| 1822 | 1823 | const tried = asked.get(pull.id); |
| 1823 | 1824 | if (tried?.tip === baseTip && tried.n >= RUNS_PER_TIP) continue; |
| 1825 | // A clash only in list docs is settled by the forge, with no run: its | |
| 1826 | // push is a check, and the next tick sees the pull clean (#7909). | |
| 1827 | if (onlyListDocs(files) && room > 0 && (await resolveListConflicts(app, repo, pull)) !== undefined) { | |
| 1828 | room--; | |
| 1829 | acted++; | |
| 1830 | checked.delete(pull.id); | |
| 1831 | continue; | |
| 1832 | } | |
| 1824 | 1833 | // Its push is a check: none starts that the lane has no slot for (#4459). |
| 1825 | 1834 | if (room <= 0) continue; |
| 1826 | 1835 | if (bringUpToDate(app, repo, pull, files, since) !== undefined) { |
src/crew/dispatch.ts +37
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | |
| 11 | 11 | import { twinNote, twinOf, type Twin } from "./twin.ts"; |
| 12 | 12 | import { NO_MERGE } from "../checks/merged.ts"; |
| 13 | import { resolveListConflicts } from "../actions/list-conflicts.ts"; | |
| 13 | 14 | import { markHeard } from "../core/issues.ts"; |
| 14 | 15 | import { capacityCount, noteBooted } from "../core/capacity.ts"; |
| 15 | 16 | import { tmpdir } from "node:os"; |
| @@ -115,6 +116,30 @@ const log = createLog("crew"); | ||
| 115 | 116 | export const CHECK_FAILED_LIMIT = 2; |
| 116 | 117 | const TICK_MS = 5_000; |
| 117 | 118 | |
| 119 | /** Whether the check run's red is a pull that does not merge into its base. */ | |
| 120 | function conflictSummary(app: App, runId: unknown): boolean { | |
| 121 | if (typeof runId !== "string") return false; | |
| 122 | return getRun(app.db, runId)?.summary.includes(NO_MERGE) === true; | |
| 123 | } | |
| 124 | ||
| 125 | /** | |
| 126 | * A pull's check could not start for a conflict: the forge keeps both sides | |
| 127 | * of a list-doc clash itself, else queues one bring-up-to-date run. Neither | |
| 128 | * is a try (`automaticRunsSince` counts failed checks only); one in flight is | |
| 129 | * enough, and a day's cap and the breaker still bound the queueing. | |
| 130 | */ | |
| 131 | async function bringUpAfterNoMerge(app: App, repo: Repo, issue: Issue, agent: Agent): Promise<void> { | |
| 132 | const pull = getPull(app.db, repo.id, issue.number); | |
| 133 | if (!pull) return; | |
| 134 | if (await resolveListConflicts(app, repo, pull)) return; | |
| 135 | if (activeRunFor(app.db, "agent", issue.id) !== undefined) return; | |
| 136 | queueAgentRun(app, repo, issue, agent, "conflict", undefined, { | |
| 137 | notBefore: new Date().toISOString(), | |
| 138 | attempt: 0, | |
| 139 | summary: "main moved: the pull does not merge into its base", | |
| 140 | }); | |
| 141 | } | |
| 142 | ||
| 118 | 143 | export interface CrewHandle { |
| 119 | 144 | /** Whether a usable Claude Code was found at start. */ |
| 120 | 145 | readonly ready: boolean; |
| @@ -732,6 +757,18 @@ function route(app: App, event: ForgeEvent): void { | ||
| 732 | 757 | }); |
| 733 | 758 | return; |
| 734 | 759 | } |
| 760 | // A pull that does not merge is no failed try: the forge settles a | |
| 761 | // list-doc clash itself, else a bring-up-to-date run is queued. It | |
| 762 | // counts towards no limit and parks nothing (#7909). | |
| 763 | if (conflictSummary(app, event.payload["runId"])) { | |
| 764 | void bringUpAfterNoMerge(app, repo, issue, agent).catch((error) => | |
| 765 | log.error("bringing a conflicted pull up to date failed", { | |
| 766 | issue: issue.number, | |
| 767 | error: String(error), | |
| 768 | }), | |
| 769 | ); | |
| 770 | return; | |
| 771 | } | |
| 735 | 772 | // A red that main shares (#1971), or that three pulls share within |
| 736 | 773 | // the hour (#2266), is not the pull's: no rework, no park, no |
| 737 | 774 | // strike; it waits and is checked again when main is green. |
src/git/union-merge.ts added +60
| @@ -0,0 +1,60 @@ | ||
| 1 | /** | |
| 2 | * Three versions of one text file merged with `git merge-file --union`: a | |
| 3 | * clash keeps both sides' lines instead of leaving markers. For a file whose | |
| 4 | * content is a list, where two pulls each add an entry at the same place. | |
| 5 | */ | |
| 6 | ||
| 7 | import { mkdtemp, rm, writeFile } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { runGitCapture } from "./env.ts"; | |
| 11 | import { isSafeOid, isSafeTreePath } from "./guard.ts"; | |
| 12 | ||
| 13 | const MAX_BYTES = 1_000_000; | |
| 14 | ||
| 15 | /** The files a pull's conflict is settled in without a run: lists, where each pull adds a line. */ | |
| 16 | export const LIST_DOCS: readonly string[] = ["docs/MCP.md", "README.md", "CHANGELOG.md"]; | |
| 17 | ||
| 18 | export const isListDoc = (path: string): boolean => LIST_DOCS.includes(path); | |
| 19 | ||
| 20 | async function blobAt(dir: string, oid: string, path: string): Promise<string | undefined> { | |
| 21 | const out = await runGitCapture(["cat-file", "blob", `${oid}:${path}`], { cwd: dir }); | |
| 22 | return out.code === 0 && out.stdout.length <= MAX_BYTES ? out.stdout : undefined; | |
| 23 | } | |
| 24 | ||
| 25 | /** | |
| 26 | * `path` at `ours` and `theirs`, merged over what they share at `base`, both | |
| 27 | * sides kept. Undefined when the file is missing on any side or too large. | |
| 28 | */ | |
| 29 | export async function unionMergeFile( | |
| 30 | dir: string, | |
| 31 | base: string, | |
| 32 | ours: string, | |
| 33 | theirs: string, | |
| 34 | path: string, | |
| 35 | ): Promise<string | undefined> { | |
| 36 | if (!isSafeOid(base) || !isSafeOid(ours) || !isSafeOid(theirs) || !isSafeTreePath(path)) | |
| 37 | return undefined; | |
| 38 | const [b, o, t] = await Promise.all([ | |
| 39 | blobAt(dir, base, path), | |
| 40 | blobAt(dir, ours, path), | |
| 41 | blobAt(dir, theirs, path), | |
| 42 | ]); | |
| 43 | if (b === undefined || o === undefined || t === undefined) return undefined; | |
| 44 | const work = await mkdtemp(join(tmpdir(), "forge-union-")); | |
| 45 | try { | |
| 46 | await Promise.all([ | |
| 47 | writeFile(join(work, "ours"), o), | |
| 48 | writeFile(join(work, "base"), b), | |
| 49 | writeFile(join(work, "theirs"), t), | |
| 50 | ]); | |
| 51 | const merged = await runGitCapture( | |
| 52 | ["merge-file", "-p", "--union", join(work, "ours"), join(work, "base"), join(work, "theirs")], | |
| 53 | { cwd: dir }, | |
| 54 | ); | |
| 55 | // Exit 0 is clean; a positive count is the clashes kept as both sides. | |
| 56 | return merged.code >= 0 && merged.code < 128 ? merged.stdout : undefined; | |
| 57 | } finally { | |
| 58 | await rm(work, { recursive: true, force: true }); | |
| 59 | } | |
| 60 | } |
test/asks-counts.test.ts +1 −1
| @@ -254,7 +254,7 @@ describe("home's asks are read grouped, and once", () => { | ||
| 254 | 254 | const handovers = sql.filter((s) => s.startsWith("SELECT r.issue_id AS issueId, COUNT(*)")); |
| 255 | 255 | const rounds = sql.filter((s) => s.startsWith("SELECT r.issue_id AS issueId, r.id AS id")); |
| 256 | 256 | const verdicts = sql.filter((s) => s.startsWith("SELECT review.issue_id, review.id")); |
| 257 | const tries = sql.filter((s) => s.includes("reason IN ('check_failed','conflict')")); | |
| 257 | const tries = sql.filter((s) => s.includes("run.reason = 'check_failed'")); | |
| 258 | 258 | const reviewers = sql.filter((s) => s.includes("WHERE repo_crew.role = ?1")); |
| 259 | 259 | expect(active.length).toBe(1); |
| 260 | 260 | expect(handovers.length).toBe(1); |
test/crew-merge-conflict.test.ts added +116
| @@ -0,0 +1,116 @@ | ||
| 1 | /** | |
| 2 | * A merge conflict is not a failed try (#7909): a check that could not start | |
| 3 | * because the pull "does not merge into main" never counts towards | |
| 4 | * CHECK_FAILED_LIMIT and never parks it. A clash only in a list doc is | |
| 5 | * settled by the forge keeping both sides; any other queues one | |
| 6 | * bring-up-to-date run. | |
| 7 | */ | |
| 8 | ||
| 9 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 10 | import type { App } from "../src/app.ts"; | |
| 11 | import { announceRun } from "../src/actions/runs.ts"; | |
| 12 | import { commitToBranch } from "../src/actions/repos.ts"; | |
| 13 | import { openPull } from "../src/actions/pulls.ts"; | |
| 14 | import { NO_MERGE } from "../src/checks/merged.ts"; | |
| 15 | import { CHECK_FAILED_LIMIT } from "../src/crew/dispatch.ts"; | |
| 16 | import { getIssueById } from "../src/core/issues.ts"; | |
| 17 | import { getPull, type Pull } from "../src/core/pulls.ts"; | |
| 18 | import type { Repo } from "../src/core/repos.ts"; | |
| 19 | import { automaticRunsSince, claimRun, createRun, finishRun, listRuns } from "../src/core/runs.ts"; | |
| 20 | import type { User } from "../src/core/users.ts"; | |
| 21 | import { onlyListDocs } from "../src/actions/list-conflicts.ts"; | |
| 22 | import { unionMergeFile } from "../src/git/union-merge.ts"; | |
| 23 | import { repoDir } from "../src/git/repos.ts"; | |
| 24 | import { startStandInCrew, stopStandInCrew, type StandInCrew } from "./helpers/crew-standin.ts"; | |
| 25 | ||
| 26 | describe("the crew, with a pull that does not merge", () => { | |
| 27 | let standIn: StandInCrew; | |
| 28 | let app: App; | |
| 29 | let alice: User; | |
| 30 | let bot: User; | |
| 31 | let repo: Repo; | |
| 32 | let n = 0; | |
| 33 | ||
| 34 | beforeAll(async () => { | |
| 35 | standIn = await startStandInCrew(); | |
| 36 | ({ app, alice, bot, repo } = standIn); | |
| 37 | standIn.checks.request = async () => undefined; | |
| 38 | app.db.run("UPDATE repo SET autonomy = 'autonomous', autonomy_by = ?1 WHERE id = ?2", [alice.id, repo.id]); | |
| 39 | }, 30_000); | |
| 40 | afterAll(() => stopStandInCrew(standIn)); | |
| 41 | ||
| 42 | const commit = async (branch: string, path: string, content: string) => { | |
| 43 | const c = await commitToBranch(app, alice, repo, { | |
| 44 | message: `${branch} ${path}`, | |
| 45 | branch, | |
| 46 | ...(branch === "main" ? {} : { base: "main" }), | |
| 47 | files: [{ path, content }], | |
| 48 | }); | |
| 49 | if (!c.ok) throw new Error(JSON.stringify(c.error)); | |
| 50 | }; | |
| 51 | ||
| 52 | /** A pull whose automatic tries are spent, conflicting with main in `path`. */ | |
| 53 | const conflicted = async (path: string): Promise<Pull> => { | |
| 54 | const branch = `clash-${++n}`; | |
| 55 | await commit("main", path, "- base\n"); | |
| 56 | await commit(branch, path, "- base\n- theirs\n"); | |
| 57 | const o = await openPull(app, bot, repo, { title: branch, baseBranch: "main", headBranch: branch }); | |
| 58 | if (!o.ok) throw new Error(String(o.error)); | |
| 59 | app.db.run("UPDATE issue SET assignee_id = ?1 WHERE id = ?2", [bot.id, o.value.id]); | |
| 60 | for (let i = 0; i < CHECK_FAILED_LIMIT; i++) { | |
| 61 | const tried = createRun(app.db, { repoId: repo.id, kind: "agent", issueId: o.value.id, reason: "check_failed" }); | |
| 62 | claimRun(app.db, tried.id); | |
| 63 | finishRun(app.db, tried.id, { state: "ok", summary: "reworked" }); | |
| 64 | } | |
| 65 | await commit("main", path, "- base\n- ours\n"); | |
| 66 | return getPull(app.db, repo.id, o.value.number)!; | |
| 67 | }; | |
| 68 | const redForConflict = (pull: Pull) => { | |
| 69 | const run = createRun(app.db, { | |
| 70 | repoId: repo.id, | |
| 71 | kind: "check", | |
| 72 | env: `pull-${pull.number}`, | |
| 73 | commitOid: pull.headOid, | |
| 74 | reason: "push", | |
| 75 | issueId: pull.id, | |
| 76 | }); | |
| 77 | claimRun(app.db, run.id); | |
| 78 | announceRun( | |
| 79 | app, | |
| 80 | finishRun(app.db, run.id, { state: "failed", summary: `${NO_MERGE} main: docs/x.ts` })!, | |
| 81 | ); | |
| 82 | }; | |
| 83 | const conflictRuns = (pull: Pull) => | |
| 84 | listRuns(app.db, repo.id, { kind: "agent" }).filter((r) => r.issueId === pull.id && r.reason === "conflict"); | |
| 85 | const settle = () => new Promise((r) => setTimeout(r, 1500)); | |
| 86 | ||
| 87 | test("a clash outside the list docs queues a bring-up-to-date run, is no try and parks nothing", async () => { | |
| 88 | const pull = await conflicted("src/shared.ts"); | |
| 89 | redForConflict(pull); | |
| 90 | await settle(); | |
| 91 | expect(getIssueById(app.db, pull.id)?.state).toBe("open"); | |
| 92 | expect(conflictRuns(pull)).toHaveLength(1); | |
| 93 | expect(automaticRunsSince(app.db, pull.id, "1970-01-01T00:00:00.000Z")).toBe(CHECK_FAILED_LIMIT); | |
| 94 | // One in flight is enough. | |
| 95 | redForConflict(pull); | |
| 96 | await settle(); | |
| 97 | expect(conflictRuns(pull)).toHaveLength(1); | |
| 98 | }); | |
| 99 | ||
| 100 | test("a clash only in docs/MCP.md keeps both sides with no run", async () => { | |
| 101 | const pull = await conflicted("docs/MCP.md"); | |
| 102 | redForConflict(pull); | |
| 103 | await settle(); | |
| 104 | const after = getPull(app.db, repo.id, pull.number)!; | |
| 105 | expect(after.headOid).not.toBe(pull.headOid); | |
| 106 | expect(conflictRuns(pull)).toHaveLength(0); | |
| 107 | expect(getIssueById(app.db, pull.id)?.state).toBe("open"); | |
| 108 | }); | |
| 109 | ||
| 110 | test("union merge keeps both sides; only list docs qualify", async () => { | |
| 111 | expect(onlyListDocs(["docs/MCP.md"])).toBe(true); | |
| 112 | expect(onlyListDocs(["docs/MCP.md", "src/a.ts"])).toBe(false); | |
| 113 | expect(onlyListDocs([])).toBe(false); | |
| 114 | expect(await unionMergeFile(repoDir(app.reposRoot, alice.username, repo.name), "x", "y", "z", "docs/MCP.md")).toBeUndefined(); | |
| 115 | }); | |
| 116 | }); |
new above
offline