Public: anyone can read this; the crew can change it.
When an upstream update conflicts with a fork's change, the forge files one issue for the crew naming both sides and the files #13499
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13484-when-an-upstream-update-conflicts-with-a → main 3 commits fork
4 files
changed +168
−2
docs/features/follow-mode.md +4
| @@ -35,3 +35,7 @@ Every fork says in one plain line how it follows and when it last took: `Takes u | ||
| 35 | 35 | - **MCP:** `line` in the answers of `following` and `follow_mode`. |
| 36 | 36 | |
| 37 | 37 | *Last took* is the newest `push` event of the fork carrying `followed`: a fast-forward, a clean merge (`takeOrTell`, `take_updates`, `t`), or a spawned forge's take of its parent forge. `lastTookAt` in `src/core/fork_follow.ts` reads it through `event_repo_kind_seq`, newest first, and stops at the first hit. The words are `followStandingLine` in `src/actions/follow_words.ts`; `followStanding` in `src/actions/follow.ts` assembles them for every door. |
| 38 | ||
| 39 | ## A conflict goes to the crew | |
| 40 | ||
| 41 | A fork that takes updates and meets one that conflicts with its own change stops that update and leaves its code as it was. The crew gets one issue, "Take updates from <parent>", naming the upstream commit, the fork's own commits and each conflicted file, even where autonomy is off. The same upstream commit never opens a second issue, open or closed (`src/crew/converge.ts`). |
src/core/issues.ts +14
| @@ -542,6 +542,20 @@ export function listIssues( | ||
| 542 | 542 | return rows.map(toIssue); |
| 543 | 543 | } |
| 544 | 544 | |
| 545 | /** | |
| 546 | * Whether the repository has an issue, open or closed, with exactly this title and a body line | |
| 547 | * equal to `line`. No page cap: it asks the database, not a list. | |
| 548 | */ | |
| 549 | export function hasIssueWithLine(db: Database, repoId: string, title: string, line: string): boolean { | |
| 550 | const row = db | |
| 551 | .query< | |
| 552 | { n: number }, | |
| 553 | [string, string, string] | |
| 554 | >("SELECT 1 AS n FROM issue WHERE repo_id = ?1 AND kind = 'issue' AND title = ?2 AND instr(char(10) || replace(body, char(13), '') || char(10), char(10) || ?3 || char(10)) > 0 LIMIT 1") | |
| 555 | .get(repoId, title, line); | |
| 556 | return row !== null && row !== undefined; | |
| 557 | } | |
| 558 | ||
| 545 | 559 | export function countIssues( |
| 546 | 560 | db: Database, |
| 547 | 561 | repoId: string, |
src/crew/converge.ts +33 −2
| @@ -27,6 +27,7 @@ import { | ||
| 27 | 27 | } from "../core/following.ts"; |
| 28 | 28 | import { |
| 29 | 29 | addComment as insertComment, |
| 30 | hasIssueWithLine, | |
| 30 | 31 | listIssues, |
| 31 | 32 | setAssignee, |
| 32 | 33 | setIssueState, |
| @@ -76,6 +77,24 @@ export type Converge = | ||
| 76 | 77 | /** The fork is archived: no issue, no run, until it is woken. */ |
| 77 | 78 | | { kind: "archived" }; |
| 78 | 79 | |
| 80 | /** The line of a converge issue's body that names the upstream commit it is about. */ | |
| 81 | const UPSTREAM_MARK = "Upstream commit:"; | |
| 82 | ||
| 83 | /** Whether this fork has ever had a converge issue (open or closed) for this upstream commit. */ | |
| 84 | function alreadyFiled(app: App, fork: Repo, parent: Repo, parentTip: string): boolean { | |
| 85 | if (parentTip === "") return false; | |
| 86 | return hasIssueWithLine(app.db, fork.id, convergeTitle(parent), `${UPSTREAM_MARK} ${parentTip}`); | |
| 87 | } | |
| 88 | ||
| 89 | /** Up to five of the fork's own commits the staged parent lacks, as `abc1234 subject`. */ | |
| 90 | async function forkChanges(app: App, fork: Repo, staged: string, forkTip: string): Promise<string[]> { | |
| 91 | if (!isSafeOid(staged) || !isSafeOid(forkTip)) return []; | |
| 92 | const out = await runGit(["log", "-5", "--format=%h %s", `${staged}..${forkTip}`], { | |
| 93 | cwd: repoDir(app.reposRoot, fork.ownerName, fork.name), | |
| 94 | }); | |
| 95 | return out.ok ? out.value.split("\n").filter((l) => l !== "").map((l) => l.slice(0, 120)) : []; | |
| 96 | } | |
| 97 | ||
| 79 | 98 | /** The fork's open converge issue, if the parent's updates are in hand. */ |
| 80 | 99 | export function convergeIssue( |
| 81 | 100 | app: App, |
| @@ -160,6 +179,9 @@ export async function convergeFork( | ||
| 160 | 179 | return { kind: "nothing", relation }; |
| 161 | 180 | const inHand = convergeState(app, fork, parent); |
| 162 | 181 | if (inHand !== undefined) return inHand; |
| 182 | // The fork's own attempt, with nobody asking, files once per upstream commit: a closed issue for | |
| 183 | // this very commit is the answer already given, and a second attempt does not repeat it. | |
| 184 | if (actor === undefined && alreadyFiled(app, fork, parent, relation.parentTip)) return { kind: "nothing", relation }; | |
| 163 | 185 | const builders = listCrewRoles(app.db, fork.id) |
| 164 | 186 | .filter((c) => c.role === "builder") |
| 165 | 187 | .map((c) => c.agent); |
| @@ -177,16 +199,22 @@ export async function convergeFork( | ||
| 177 | 199 | return { kind: "nothing", relation }; |
| 178 | 200 | } |
| 179 | 201 | const files = relation.conflicts; |
| 202 | const own = await forkChanges(app, fork, staged, relation.forkTip); | |
| 180 | 203 | const opened = openIssue(app, as, fork, { |
| 181 | 204 | title: convergeTitle(parent), |
| 182 | 205 | body: [ |
| 183 | 206 | `This repository follows ${parent.ownerName}/${parent.name}. Both have moved since they last agreed: ${relation.behind} change${relation.behind === 1 ? "" : "s"} there, ${relation.ahead} here, and ${files.length === 0 ? "git cannot merge them" : `${files.length === 1 ? "one file" : `${files.length} files`} changed on both sides: ${files.join(", ")}`}.`, |
| 184 | 207 | "", |
| 208 | `${UPSTREAM_MARK} ${relation.parentTip}`, | |
| 209 | `This repository's change: ${own.length === 0 ? `${relation.ahead} commit${relation.ahead === 1 ? "" : "s"} not in the parent` : own.join("; ")}.`, | |
| 210 | "", | |
| 185 | 211 | `The parent's \`${parent.defaultBranch}\` is staged in this repository at \`refs/following/${parent.defaultBranch}\` (${staged.slice(0, 7)}). The crew merges it into a branch, keeps this repository's direction where the two cannot both be kept, and proposes the result here as a pull.`, |
| 186 | 212 | ].join("\n"), |
| 187 | 213 | }); |
| 188 | 214 | if (!opened.ok) return { kind: "nobody" }; |
| 189 | 215 | const issue = opened.value; |
| 216 | // Autonomy off, nobody asking: the issue is filed for the people to see and no agent work starts. | |
| 217 | if (actor === undefined && fork.autonomy === "off") return { kind: "working", issue, agent: undefined }; | |
| 190 | 218 | |
| 191 | 219 | const inFlight = new Map<string, number>(); |
| 192 | 220 | for (const r of listRuns(app.db, fork.id, { kind: "agent", limit: 50 })) |
| @@ -229,9 +257,12 @@ export function followDivergence(app: App): () => void { | ||
| 229 | 257 | const parent = |
| 230 | 258 | typeof parentId === "string" ? getRepoById(app.db, parentId) : undefined; |
| 231 | 259 | if (fork === undefined || parent === undefined) return; |
| 232 | if (fork.autonomy === "off") return; | |
| 233 | 260 | // A fork that chose not to follow is never converged on its behalf. |
| 234 | if (forkFollowMode(app.db, fork.id) === "dont-follow") return; | |
| 261 | const mode = forkFollowMode(app.db, fork.id); | |
| 262 | if (mode === "dont-follow") return; | |
| 263 | // Autonomy off keeps the crew still, except for a fork that chose to take updates: its | |
| 264 | // conflict is for the crew, never left in the owner's lap. | |
| 265 | if (fork.autonomy === "off" && mode !== "take-updates") return; | |
| 235 | 266 | diverged(app, fork, parent).catch((error) => |
| 236 | 267 | log.error("could not act on a diverged fork", { |
| 237 | 268 | fork: `${fork.ownerName}/${fork.name}`, |
test/fork-follow-conflict.test.ts added +117
| @@ -0,0 +1,117 @@ | ||
| 1 | /** | |
| 2 | * A fork that takes updates and meets a conflicting one: the update is stopped, the fork's code is | |
| 3 | * as it was, and the crew gets one issue naming the upstream commit, the fork's change and the | |
| 4 | * files; a clean update files none, and a second attempt for the same commit files no second. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 11 | import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { convergeFork, followDivergence } from "../src/crew/converge.ts"; | |
| 14 | import { setForkFollowMode } from "../src/core/fork_follow.ts"; | |
| 15 | import { forkRelation } from "../src/core/following.ts"; | |
| 16 | import { listIssues, setIssueState } from "../src/core/issues.ts"; | |
| 17 | import { listRuns } from "../src/core/runs.ts"; | |
| 18 | import { getRepoById, type Repo } from "../src/core/repos.ts"; | |
| 19 | import { createUser, type User } from "../src/core/users.ts"; | |
| 20 | import { resolveOid } from "../src/git/merge.ts"; | |
| 21 | import { repoDir } from "../src/git/repos.ts"; | |
| 22 | import { promote } from "./helpers/members.ts"; | |
| 23 | ||
| 24 | let dataDir = ""; | |
| 25 | let app: App; | |
| 26 | let alice: User; | |
| 27 | let bob: User; | |
| 28 | let cal: User; | |
| 29 | let parent: Repo; | |
| 30 | let off: () => void; | |
| 31 | ||
| 32 | const must = <T>(r: { ok: true; value: T } | { ok: false; error: unknown }): T => { | |
| 33 | if (!r.ok) throw new Error(JSON.stringify(r.error)); | |
| 34 | return r.value; | |
| 35 | }; | |
| 36 | const fresh = (r: Repo): Repo => getRepoById(app.db, r.id)!; | |
| 37 | const commit = async (who: User, repo: Repo, path: string, content: string) => | |
| 38 | must(await commitToBranch(app, who, repo, { message: `${who.username}: ${path}`, files: [{ path, content }] })); | |
| 39 | const issuesOf = (r: Repo) => listIssues(app.db, r.id, { sealed: true, state: "all", limit: 50 }); | |
| 40 | const tipOf = (r: Repo) => resolveOid(repoDir(app.reposRoot, r.ownerName, r.name), `refs/heads/${r.defaultBranch}`); | |
| 41 | async function until(f: () => boolean | Promise<boolean>, ms = 5000): Promise<void> { | |
| 42 | const start = Date.now(); | |
| 43 | while (!(await f())) { | |
| 44 | if (Date.now() - start > ms) throw new Error("not settled"); | |
| 45 | await Bun.sleep(40); // real-timer: polls for the async bus subscriber to settle | |
| 46 | } | |
| 47 | } | |
| 48 | ||
| 49 | beforeAll(async () => { | |
| 50 | dataDir = await mkdtemp(join(tmpdir(), "forge-fork-conflict-")); | |
| 51 | app = createApp({ dataDir, port: 0 }); | |
| 52 | const mk = async (username: string, first = false) => { | |
| 53 | const u = must( | |
| 54 | await createUser(app.db, { | |
| 55 | origin: "seed", | |
| 56 | trusted: true, | |
| 57 | username, | |
| 58 | email: `${username}@f.dev`, | |
| 59 | password: "a-good-passphrase", | |
| 60 | adminIfFirst: first, | |
| 61 | }), | |
| 62 | ); | |
| 63 | promote(app.db, u); | |
| 64 | return u; | |
| 65 | }; | |
| 66 | alice = await mk("alice", true); | |
| 67 | bob = await mk("bob"); | |
| 68 | cal = await mk("cal"); | |
| 69 | parent = must(await createRepo(app, alice, { name: "game", visibility: "public" })); | |
| 70 | await commit(alice, parent, "README.md", "# game\n"); | |
| 71 | must(await createAgent(app, alice, { username: "bot", brief: "Be brief." })); | |
| 72 | off = followDivergence(app); | |
| 73 | }, 30_000); | |
| 74 | ||
| 75 | afterAll(async () => { | |
| 76 | off(); | |
| 77 | app.close(); | |
| 78 | await rm(dataDir, { recursive: true, force: true }); | |
| 79 | }); | |
| 80 | ||
| 81 | test("a conflicting update opens exactly one issue; a repeat and a clean update open none", async () => { | |
| 82 | const fork = must(await forkRepo(app, bob, fresh(parent), "game-bob")); | |
| 83 | expect(addToCrew(app, bob, fresh(fork), "bot").ok).toBe(true); | |
| 84 | setForkFollowMode(app.db, fork.id, "take-updates"); | |
| 85 | await commit(bob, fork, "README.md", "# game, bob's way\n"); | |
| 86 | const before = await tipOf(fresh(fork)); | |
| 87 | ||
| 88 | await commit(alice, parent, "README.md", "# game, alice's way\n"); | |
| 89 | await until(() => issuesOf(fork).length === 1); | |
| 90 | const [issue] = issuesOf(fork); | |
| 91 | const parentTip = await tipOf(fresh(parent)); | |
| 92 | expect(issue!.title).toBe("Take updates from alice/game"); | |
| 93 | expect(issue!.body).toContain(`Upstream commit: ${parentTip}`); | |
| 94 | expect(issue!.body).toContain("bob: README.md"); | |
| 95 | expect(issue!.body).toContain("one file changed on both sides: README.md."); | |
| 96 | // Stopped: the fork's code is as it was, the update staged beside it and the default branch untouched. | |
| 97 | const dir = repoDir(app.reposRoot, fork.ownerName, fork.name); | |
| 98 | expect(await tipOf(fresh(fork))).toBe(before); | |
| 99 | expect(await resolveOid(dir, "refs/following/main")).toBe(parentTip); | |
| 100 | // Autonomy is off: the issue is for people, and no agent work is queued for it. | |
| 101 | expect(listRuns(app.db, fork.id, { kind: "agent", limit: 50 }).length).toBe(0); | |
| 102 | ||
| 103 | // The same upstream commit asked again, with the first issue closed: nothing new, deterministically. | |
| 104 | setIssueState(app.db, issue!.id, "closed", "dropped"); | |
| 105 | const again = await convergeFork(app, fresh(fork), fresh(parent), undefined); | |
| 106 | expect(again.kind).toBe("nothing"); | |
| 107 | expect(issuesOf(fork).length).toBe(1); | |
| 108 | ||
| 109 | // A clean update, on another fork, files none. | |
| 110 | const calm = must(await forkRepo(app, cal, fresh(parent), "game-calm")); | |
| 111 | expect(addToCrew(app, cal, fresh(calm), "bot").ok).toBe(true); | |
| 112 | setForkFollowMode(app.db, calm.id, "take-updates"); | |
| 113 | await commit(cal, calm, "mods/cal.txt", "cal\n"); | |
| 114 | await commit(alice, parent, "levels/two.txt", "two\n"); | |
| 115 | await until(async () => (await forkRelation(app, fresh(calm), fresh(parent)))?.state === "ahead"); | |
| 116 | expect(issuesOf(calm).length).toBe(0); | |
| 117 | }, 30_000); |
new above
offline