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

Merged opened this

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

Merged

scout/forge:scout/13484-when-an-upstream-update-conflicts-with-a → main 3 commits fork

  • Merged. scout/13484-when-an-upstream-update-conflicts-with-a was merged into main as 4150240.

Review

Sign in to review this change.
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
3535 - **MCP:** `line` in the answers of `following` and `follow_mode`.
3636
3737 *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(
542542 return rows.map(toIssue);
543543 }
544544
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
545559 export function countIssues(
546560 db: Database,
547561 repoId: string,
src/crew/converge.ts +33 −2
@@ -27,6 +27,7 @@ import {
2727 } from "../core/following.ts";
2828 import {
2929 addComment as insertComment,
30 hasIssueWithLine,
3031 listIssues,
3132 setAssignee,
3233 setIssueState,
@@ -76,6 +77,24 @@ export type Converge =
7677 /** The fork is archived: no issue, no run, until it is woken. */
7778 | { kind: "archived" };
7879
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
7998 /** The fork's open converge issue, if the parent's updates are in hand. */
8099 export function convergeIssue(
81100 app: App,
@@ -160,6 +179,9 @@ export async function convergeFork(
160179 return { kind: "nothing", relation };
161180 const inHand = convergeState(app, fork, parent);
162181 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 };
163185 const builders = listCrewRoles(app.db, fork.id)
164186 .filter((c) => c.role === "builder")
165187 .map((c) => c.agent);
@@ -177,16 +199,22 @@ export async function convergeFork(
177199 return { kind: "nothing", relation };
178200 }
179201 const files = relation.conflicts;
202 const own = await forkChanges(app, fork, staged, relation.forkTip);
180203 const opened = openIssue(app, as, fork, {
181204 title: convergeTitle(parent),
182205 body: [
183206 `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(", ")}`}.`,
184207 "",
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 "",
185211 `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.`,
186212 ].join("\n"),
187213 });
188214 if (!opened.ok) return { kind: "nobody" };
189215 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 };
190218
191219 const inFlight = new Map<string, number>();
192220 for (const r of listRuns(app.db, fork.id, { kind: "agent", limit: 50 }))
@@ -229,9 +257,12 @@ export function followDivergence(app: App): () => void {
229257 const parent =
230258 typeof parentId === "string" ? getRepoById(app.db, parentId) : undefined;
231259 if (fork === undefined || parent === undefined) return;
232 if (fork.autonomy === "off") return;
233260 // 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;
235266 diverged(app, fork, parent).catch((error) =>
236267 log.error("could not act on a diverged fork", {
237268 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);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs