Public: anyone can read this; the crew can change it.

A push to main alone rebuilds combined/N for every open kept-fork pull into it #10003

Parked opened this
Closed without merging

scout/forge:scout/9994-a-push-to-main-alone-rebuilds-combined-n → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
5 files changed +230 −5
src/core/recombine.ts added +77
@@ -0,0 +1,77 @@
1 /**
2 * Main moved: every open pull from a kept fork into it is combined again
3 * (#9994), so `combined/N` shows what would land now, not what would have
4 * landed before. One pass at a time per repository; pushes that arrive during
5 * a pass coalesce into one more pass, which reads the tip as it stands then.
6 */
7
8 import type { Database } from "bun:sqlite";
9 import { combine } from "../git/combined.ts";
10 import { resolveOid } from "../git/merge.ts";
11 import { repoDir } from "../git/repos.ts";
12 import { createLog } from "../kernel/log.ts";
13 import { fromKeptFork } from "./landing_kind.ts";
14 import { listOpenPulls } from "./pulls.ts";
15 import type { Repo } from "./repos.ts";
16
17 const log = createLog("recombine");
18
19 interface Slot {
20 running: Promise<void> | undefined;
21 again: boolean;
22 }
23 const slots = new Map<string, Slot>();
24 const passes = new Map<string, number>();
25
26 /** How many passes have run for a repository in this process. */
27 export const recombinePasses = (repoId: string): number => passes.get(repoId) ?? 0;
28
29 /** One pass: each open kept-fork pull into the default branch, combined with the tip now. */
30 async function pass(
31 forge: { readonly db: Database; readonly reposRoot: string },
32 repo: Repo,
33 ): Promise<void> {
34 passes.set(repo.id, recombinePasses(repo.id) + 1);
35 const dir = repoDir(forge.reposRoot, repo.ownerName, repo.name);
36 for (const pull of listOpenPulls(forge.db, repo.id)) {
37 if (!pull.crossRepo || pull.sealed || pull.baseBranch !== repo.defaultBranch) continue;
38 if (!fromKeptFork(forge.db, pull)) continue;
39 try {
40 const baseOid = await resolveOid(dir, `refs/heads/${pull.baseBranch}`);
41 const headOid = await resolveOid(dir, `refs/pull/${pull.number}/head`);
42 if (baseOid !== undefined && headOid !== undefined)
43 await combine(dir, { number: pull.number, baseOid, headOid });
44 } catch (error) {
45 log.warn("could not recombine a pull", { pull: pull.number, error: String(error) });
46 }
47 }
48 }
49
50 /**
51 * Ask for a pass. Starts one when none runs; otherwise marks one more.
52 * Answers when the repository is idle, for callers that wish to wait.
53 */
54 export function recombine(
55 forge: { readonly db: Database; readonly reposRoot: string },
56 repo: Repo,
57 ): Promise<void> {
58 const slot = slots.get(repo.id) ?? { running: undefined, again: false };
59 slots.set(repo.id, slot);
60 if (slot.running !== undefined) {
61 slot.again = true;
62 return slot.running;
63 }
64 slot.running = (async () => {
65 try {
66 do {
67 slot.again = false;
68 await pass(forge, repo);
69 } while (slot.again);
70 } catch (error) {
71 log.warn("recombine pass failed", { repo: repo.id, error: String(error) });
72 } finally {
73 slot.running = undefined;
74 }
75 })();
76 return slot.running;
77 }
src/http/server.ts +10
@@ -5,6 +5,7 @@
55 * verdict. Auth is resolved once per request and handed to handlers as data.
66 */
77
8 import { recombine } from "../core/recombine.ts";
89 import { isTokenScheme, tokenSecret } from "./token_header.ts";
910 import {
1011 followSourceRemove,
@@ -2031,6 +2032,15 @@ async function handleHook(app: App, request: Request): Promise<Response> {
20312032 );
20322033 }
20332034
2035 // Main moved: kept forks' combined branches follow it (`core/recombine.ts`),
2036 // off the hook's answer.
2037 if (
2038 input.updates.some(
2039 (u) => u.ref === `refs/heads/${repo.defaultBranch}` && !ZERO_OID.test(u.newOid),
2040 )
2041 )
2042 void recombine(app, repo);
2043
20342044 // post-receive: the push is durable — record it and wake listeners.
20352045 app.bus.append({
20362046 kind: "push",
src/web/pages/pull.ts +1 −1
@@ -1054,7 +1054,7 @@ export async function pullFilesPage(ctx: Ctx): Promise<Response> {
10541054 ${holdCard(ctx, repo, pull, { act: false })}
10551055 ${teamsCard(teamVerdicts(ctx.app.db, { ...pull, headOid: status.headOid }))}
10561056 ${mergeBox(ctx, repo, pull, status, mayMerge, by, check, isApp, tier, stale)}
1057 ${slicesCard(base, thread, slices, status.ahead, ends, pull.state === "open" && pull.mergedAt === null, mayLand)}
1057 ${slicesCard(base, thread, slices, ends.length > 0 ? ends.length : status.ahead, ends, pull.state === "open" && pull.mergedAt === null, mayLand)}
10581058 ${await combinedCard(dir, pull, status.headOid, status.baseOid)}
10591059 <div class="split">
10601060 <div>
test/combined-fork.test.ts +9 −4
@@ -181,14 +181,19 @@ describe("a conflicting fork pull is combined on our side (#9012)", () => {
181181 expect((await git(["log", "-1", "--format=%P", retip], bare)).split(" ")).toEqual([bobNext, aliceMain]);
182182 expect(await previewCommit(bare, getPull(app.db, repo.id, number)!)).toBe(retip);
183183
184 // Main moves on its own: the card says the preview is stale, and on what it was built.
184 // Main moves on its own: the pass of #9994 combines again, so the card
185 // is not left stale (it was, before: the old test here asserted that).
185186 await Bun.write(join(a, "four.txt"), "four\n");
186187 await git(["add", "."], a);
187188 await git(["commit", "-m", "alice's four"], a);
188189 await git(["push", "origin", "main"], a);
189 const stale = await (await call(`/alice/app/pulls/${number}`, alice)).text();
190 expect(stale).toContain(`Stale: main has moved since ${aliceMain.slice(0, 10)}`);
191 expect(await screenText()).toContain("Combined with main · stale");
190 let fresh = "";
191 for (let i = 0; i < 100; i++) {
192 fresh = await (await call(`/alice/app/pulls/${number}`, alice)).text();
193 if (!fresh.includes("Stale: main has moved")) break;
194 await Bun.sleep(50);
195 }
196 expect(fresh).not.toContain("Stale: main has moved");
192197
193198 // The merge lands the combined branch: his head is an ancestor of main.
194199 const merged = await call(`/api/v1/repos/alice/app/pulls/${number}/merge`, alice, { strategy: "merge" });
test/combined-recombine.test.ts added +133
@@ -0,0 +1,133 @@
1 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2 import { mkdtemp, rm } from "node:fs/promises";
3 import { tmpdir } from "node:os";
4 import { join } from "node:path";
5 import { createApp, type App } from "../src/app.ts";
6 import { serve, type Servers } from "../src/http/server.ts";
7 import { gitClientEnv } from "./helpers/git-client.ts";
8 import { proveAll } from "./helpers/verified.ts";
9 import { promote } from "./helpers/members.ts";
10 import { tokenFor } from "./helpers/tokens.ts";
11 import { repoDir } from "../src/git/repos.ts";
12 import { getRepo } from "../src/core/repos.ts";
13 import { recombinePasses } from "../src/core/recombine.ts";
14
15 const gitEnv = gitClientEnv({
16 GIT_AUTHOR_NAME: "Jay Moran",
17 GIT_AUTHOR_EMAIL: "[email protected]",
18 GIT_COMMITTER_NAME: "Jay Moran",
19 GIT_COMMITTER_EMAIL: "[email protected]",
20 });
21
22 describe("a push to main alone recombines kept forks' pulls (#9994)", () => {
23 let dataDir: string;
24 let work: string;
25 let app: App;
26 let servers: Servers;
27 let base: string;
28 let alice = "";
29 let bob = "";
30 let number = 0;
31 let bare = "";
32
33 async function git(args: string[], cwd: string): Promise<string> {
34 const p = Bun.spawn(["git", ...args], { cwd, env: gitEnv, stdout: "pipe", stderr: "pipe" });
35 const [out, err, code] = await Promise.all([
36 new Response(p.stdout).text(),
37 new Response(p.stderr).text(),
38 p.exited,
39 ]);
40 if (code !== 0) throw new Error(`git ${args.join(" ")}: ${err}`);
41 return out.trim();
42 }
43 const call = (path: string, token: string, body?: unknown): Promise<Response> =>
44 fetch(`${base}${path}`, {
45 method: body === undefined ? "GET" : "POST",
46 headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
47 ...(body === undefined ? {} : { body: JSON.stringify(body) }),
48 });
49 const authed = (user: string, token: string) => base.replace("http://", `http://${user}:${token}@`);
50 async function signup(username: string): Promise<string> {
51 await fetch(`${base}/signup`, {
52 method: "POST",
53 body: new URLSearchParams({ username, email: `${username}@f.dev`, password: "kept-fork-pass" }),
54 redirect: "manual",
55 });
56 return tokenFor(app, username, "admin", "k");
57 }
58 const a = () => join(work, "a");
59 async function pushMain(file: string): Promise<string> {
60 await Bun.write(join(a(), file), `${file}\n`);
61 await git(["add", "."], a());
62 await git(["commit", "-m", `add ${file}`], a());
63 await git(["push", "origin", "main"], a());
64 return git(["rev-parse", "HEAD"], a());
65 }
66 const parents = async (): Promise<string[]> => {
67 try {
68 return (await git(["log", "-1", "--format=%P", `refs/heads/combined/${number}`], bare)).split(" ");
69 } catch {
70 return []; // not written yet: the pass has not reached it
71 }
72 };
73 async function until(oid: string): Promise<void> {
74 for (let i = 0; i < 100 && !(await parents()).includes(oid); i++) await Bun.sleep(50);
75 }
76
77 beforeAll(async () => {
78 dataDir = await mkdtemp(join(tmpdir(), "forge-rc-"));
79 work = await mkdtemp(join(tmpdir(), "forge-rc-work-"));
80 app = createApp({ dataDir, port: 0 });
81 servers = await serve(app);
82 base = servers.url;
83 alice = await signup("alice");
84 bob = await signup("bob");
85 proveAll(app);
86 promote(app.db);
87 await call("/api/v1/repos", alice, { name: "app", visibility: "public" });
88 await git(["clone", `${authed("alice", alice)}/alice/app.git`, "a"], work);
89 await pushMain("one.txt");
90 await call("/api/v1/repos/alice/app/forks", bob, {});
91 await git(["clone", `${authed("bob", bob)}/bob/app.git`, "b"], work);
92 const b = join(work, "b");
93 await Bun.write(join(b, "bob.txt"), "bob\n");
94 await git(["add", "."], b);
95 await git(["commit", "-m", "bob's work"], b);
96 await git(["push", "origin", "main"], b);
97 const opened = await call("/api/v1/repos/alice/app/pulls", bob, {
98 title: "Jay's work",
99 base: "main",
100 head: "main",
101 headRepo: "bob/app",
102 });
103 expect(opened.status).toBe(201);
104 number = ((await opened.json()) as { number: number }).number;
105 bare = repoDir(app.reposRoot, "alice", "app");
106 });
107
108 afterAll(async () => {
109 servers.stop();
110 app.close();
111 await rm(dataDir, { recursive: true, force: true });
112 await rm(work, { recursive: true, force: true });
113 });
114
115 test("main moving alone puts the new main under combined/N", async () => {
116 const next = await pushMain("two.txt");
117 await until(next);
118 expect(await parents()).toContain(next);
119 expect(await git(["show", `refs/heads/combined/${number}:two.txt`], bare)).toBe("two.txt");
120 }, 30_000);
121
122 test("two quick pushes run at most two passes", async () => {
123 const repo = getRepo(app.db, "alice", "app")!;
124 const before = recombinePasses(repo.id);
125 await pushMain("three.txt");
126 const last = await pushMain("four.txt");
127 await until(last);
128 expect(await parents()).toContain(last);
129 const ran = recombinePasses(repo.id) - before;
130 expect(ran).toBeGreaterThanOrEqual(1);
131 expect(ran).toBeLessThanOrEqual(2);
132 }, 30_000);
133 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs