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

A fork's offer upstream says how big it is: files changed and lines added and removed, before anyone opens the diff #14569

Merged opened this

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

Merged

scout/forge:scout/14561-a-fork-s-offer-upstream-says-how-big-it → main 8 commits fork 2 threads · 2 unresolved

  • Merged. scout/14561-a-fork-s-offer-upstream-says-how-big-it was merged into main as 1d11376.

Review

Sign in to review this change.
8 files changed +276 −5
docs/features/fork-offer-size.md added +12
@@ -0,0 +1,12 @@
1 ---
2 topic: Your apps
3 row: An offer from a fork says how big it is before anyone opens the diff
4 ---
5 # An offer from a fork says how big it is
6
7 Each offer on the upstream's list says what it changes, so a maintainer can tell a one-line fix from a rewrite at a glance.
8
9 - **Page:** the upstream's home, under "Offered by forks", adds `3 files, +40 −7` to each offer: files changed, lines added, lines removed against where the offer branched. A binary file counts as a file with no lines. When the offer's head commit no longer exists the row says `its head is gone`.
10 - **Terminal:** the forks screen's `offered #N title` rows carry the same words after the fork and the age.
11
12 The size never holds the page up: the first five offers are asked together and any that git has not answered in a quarter of a second show no size until the next render (`offerSizes` in `src/git/offer_size.ts`, shared by the page and the terminal). Sizes are kept per base and head commit in this process only; a restart asks git again.
src/git/offer_size.ts added +114
@@ -0,0 +1,114 @@
1 /**

Both doors: the terminal needs this size too (TUI offers screen) and a docs/features/ how-to.

2 * How big a fork's offer is: files changed and lines added and removed, from
3 * git's numstat between the pull's base and head, kept per (base, head) oid
4 * pair so a second render asks git nothing. Fixed arguments only; the oids
5 * are checked before they reach git. Used by the fork-offers list alone (page
6 * and terminal). The kept sizes live in this process: a restart loses them and
7 * the next render asks git again. `offerSizes` bounds a render: at most
8 * `SIZE_CAP` offers are asked, and any that miss `SIZE_BUDGET_MS` are left out
9 * (their row says no size) while git goes on to fill the cache for next time.
10 */
11
12 import type { App } from "../app.ts";
13 import { getPull, pullDir } from "../core/pulls.ts";
14 import { runGit } from "./env.ts";
15 import { isSafeOid } from "./guard.ts";
16
17 export interface OfferSize {
18 readonly files: number;
19 readonly additions: number;
20 readonly deletions: number;
21 }
22
23 /** A size, or "gone" when the head commit no longer exists, or "unknown" when git could not say. */
24 export type OfferSizeResult = OfferSize | "gone" | "unknown";
25
26 const KEPT_MAX = 500;
27 const kept = new WeakMap<App, Map<string, OfferSize>>();
28
29 /** `git diff --numstat` text to totals; a binary file (`-\t-`) counts as a file with no lines. */
30 export function parseNumstat(text: string): OfferSize {
31 let files = 0;
32 let additions = 0;
33 let deletions = 0;
34 for (const line of text.split("\n")) {
35 const m = /^(\d+|-)\t(\d+|-)\t/.exec(line);
36 if (m === null) continue;
37 files++;
38 additions += m[1] === "-" ? 0 : Number(m[1]);
39 deletions += m[2] === "-" ? 0 : Number(m[2]);
40 }
41 return { files, additions, deletions };
42 }
43
44 export async function offerSize(
45 app: App,
46 repo: { id: string; ownerName: string; name: string },
47 number: number,
48 ): Promise<OfferSizeResult> {
49 const pull = getPull(app.db, repo.id, number);
50 if (pull === undefined || !isSafeOid(pull.baseOid) || !isSafeOid(pull.headOid)) return "unknown";
51 const key = `${pull.baseOid}:${pull.headOid}`;
52 let cache = kept.get(app);
53 if (cache === undefined) kept.set(app, (cache = new Map()));
54 const hit = cache.get(key);
55 if (hit !== undefined) return hit;
56 const dir = await pullDir(app.db, app.reposRoot, repo, pull);
57 const opts = { cwd: dir, timeoutMs: 10_000, signal: app.closing.signal };
58 // One git call in the common case; the head's existence is asked only when the diff fails.
59 const out = await runGit(["diff", "--numstat", "--no-color", "--no-renames", `${pull.baseOid}...${pull.headOid}`, "--"], opts);
60 if (!out.ok) {
61 const has = await runGit(["cat-file", "-e", `${pull.headOid}^{commit}`], opts);
62 return has.ok ? "unknown" : "gone";
63 }
64 const size = parseNumstat(out.value);
65 if (cache.size >= KEPT_MAX) cache.delete(cache.keys().next().value!);
66 cache.set(key, size);
67 return size;
68 }
69
70 /** `3 files, +40 −7`. */
71 export function sizeWords(s: OfferSize): string {
72 return `${s.files} ${s.files === 1 ? "file" : "files"}, +${s.additions} −${s.deletions}`;
73 }
74
75 export const SIZE_CAP = 5;
76 export const SIZE_BUDGET_MS = 250;
77
78 /**
79 * The sizes of these offers within a budget: the first `SIZE_CAP` are asked together and any that
80 * fail, throw or run past `budgetMs` are absent from the map, so a page or screen never waits on
81 * git.
82 */
83 export async function offerSizes(
84 app: App,
85 repo: { id: string; ownerName: string; name: string },
86 numbers: readonly number[],
87 ask: typeof offerSize = offerSize,
88 budgetMs: number = SIZE_BUDGET_MS,
89 ): Promise<Map<number, OfferSizeResult>> {
90 const got = new Map<number, OfferSizeResult>();
91 let timer: ReturnType<typeof setTimeout> | undefined;
92 const late = new Promise<void>((done) => {
93 timer = setTimeout(done, budgetMs);
94 });
95 let open = true;
96 const asked = numbers.slice(0, SIZE_CAP).map((n) =>
97 ask(app, repo, n).then(
98 (r) => {
99 if (open) got.set(n, r);
100 },
101 () => {},
102 ),
103 );
104 await Promise.race([Promise.all(asked), late]);
105 open = false;
106 clearTimeout(timer);
107 return new Map(got);
108 }
109
110 /** What a row says of a size: the words, "its head is gone", or nothing when it is not known. */
111 export function sizeLine(size: OfferSizeResult | undefined): string {
112 if (size === undefined || size === "unknown") return "";
113 return size === "gone" ? "its head is gone" : sizeWords(size);
114 }
src/tui/screens/forks.ts +5 −2
@@ -26,6 +26,7 @@ import { FOLLOW_MODE_REFUSALS, convergeWords, followModeOf, followStanding, foll
2626 import { incomingOf, type Incoming } from "../../actions/follow_incoming.ts";
2727 import { convergeFollowed, followedAs, followedOf } from "../../actions/upstream_sync.ts";
2828 import { forkOffers, sentOffers } from "../../core/fork_offers.ts";
29 import { offerSizes, sizeLine } from "../../git/offer_size.ts";
2930 import { agoWords } from "../../actions/follow_words.ts";
3031 import { STANDING_EVENTS } from "../../core/standing_events.ts";
3132 import { openProposal, proposeUpstream } from "../../actions/upstream.ts";
@@ -253,10 +254,12 @@ export function forksScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
253254 failed = getRepoById(f.db, repo.id)?.origin?.fetchError ?? "";
254255 mode = relation === undefined ? undefined : await followModeOf(f.app, repo);
255256 following = followingRows(relation, converge, mode);
256 offered = forkOffers(f.db, repo, f.user).map((o) => ({
257 const here = forkOffers(f.db, repo, f.user);
258 const sizes = await offerSizes(f.app, repo, here.map((o) => o.number));
259 offered = here.map((o) => ({
257260 repo,
258261 name: `offered #${o.number} ${o.title}`,
259 words: `${o.fork} · ${agoWords(o.createdAt)}`,
262 words: [o.fork, agoWords(o.createdAt), sizeLine(sizes.get(o.number))].filter((w) => w !== "").join(" · "),
260263 heading: "",
261264 issue: o.number,
262265 }));
src/web/pages/fork-offers.ts +8 −2
@@ -3,11 +3,12 @@
33 import { agoWords } from "../../actions/follow_words.ts";
44 import type { ForkOffer, SentOffer } from "../../core/fork_offers.ts";
55 import type { Repo } from "../../core/repos.ts";
6 import { sizeLine, type OfferSizeResult } from "../../git/offer_size.ts";
67 import { repoBase } from "../chrome.ts";
78 import { html, type Html } from "../html.ts";
89 import { confirmAction, time } from "../ui.ts";
910
10 export function forkOffersCard(repo: Pick<Repo, "ownerName" | "name">, offers: readonly ForkOffer[]): Html | null {
11 export function forkOffersCard(repo: Pick<Repo, "ownerName" | "name">, offers: readonly ForkOffer[], sizes: ReadonlyMap<number, OfferSizeResult> = new Map()): Html | null {
1112 if (offers.length === 0) return null;
1213 return html`<section aria-labelledby="fork-offers-h">
1314 <h2 id="fork-offers-h">Offered by forks</h2>
@@ -15,12 +16,17 @@ export function forkOffersCard(repo: Pick<Repo, "ownerName" | "name">, offers: r
1516 (o) => html`<li>
1617 <a class="row" href="${repoBase(repo)}/pulls/${o.number}"
1718 ><span class="row-main"><span class="row-title">${o.title}</span>
18 <span class="row-meta"><span>${o.fork}</span> <span>${time(o.createdAt, { text: agoWords(o.createdAt) })}</span></span></span></a>
19 <span class="row-meta"><span>${o.fork}</span> <span>${time(o.createdAt, { text: agoWords(o.createdAt) })}</span>${sizeSpan(sizes.get(o.number))}</span></span></a>
1920 </li>`,
2021 )}</ul>
2122 </section>`;
2223 }
2324
25 function sizeSpan(size: OfferSizeResult | undefined): Html | string {
26 const words = sizeLine(size);
27 return words === "" ? "" : html` <span>${words}</span>`;
28 }
29
2430 /**
2531 * A fork's page: the offers it has sent upstream and where each stands, with a Withdraw button on
2632 * the waiting ones the viewer may take back (`offerWithdrawPost`).
src/web/pages/repo.ts +4 −1
@@ -21,6 +21,7 @@ import { CONFLICT_WORDS, UNREAD_WORDS } from "../../origins/ahead.ts";
2121 import { upstreamOffer } from "../../actions/offer_upstream.ts";
2222 import { offerUpstream } from "./offer-upstream.ts";
2323 import { forkOffers, sentOffers } from "../../core/fork_offers.ts";
24 import { offerSizes } from "../../git/offer_size.ts";
2425 import { forkOffersCard, sentOffersCard } from "./fork-offers.ts";
2526 import { VERIFY_FIRST } from "../../actions/verify_words.ts";
2627 import { GUEST_REFUSALS, HELP_BUILD, HELP_BUILD_NEXT, isContributionFork, isGuestRefusal } from "../../core/contribute.ts";
@@ -1565,12 +1566,14 @@ async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> {
15651566 );
15661567 const standing = await timePhaseAsync(ctx.request, "behind", () => standingRegion(ctx, repo));
15671568 const gets = repo.template ? await timePhaseAsync(ctx.request, "template", () => templateCard(ctx.app, repo)) : null;
1569 const offersHere = forkOffers(ctx.app.db, repo, ctx.auth.user);

Awaits git for every offer before first paint. Bound it by a time budget and a concurrency cap, and drop sizes that miss it.

1570 const sizes = await offerSizes(ctx.app, repo, offersHere.map((o) => o.number));
15681571 const rendering = performance.now();
15691572 const body = html`${scopeHeader(ctx, scope, "code", {
15701573 branchCount: refs.branches.length,
15711574 })}${credit}${standing}${archivedLine(ctx, repo)}${walletLine(ctx, repo)}${reportLine(ctx, repo)}${movedLine(ctx, repo)}${noApp}${gets}
15721575 ${offerUpstream(repo, parent, offer)}
1573 ${forkOffersCard(repo, forkOffers(ctx.app.db, repo, ctx.auth.user))}
1576 ${forkOffersCard(repo, offersHere, sizes)}
15741577 ${parent === undefined ? "" : sentOffersCard(repo, parent, sentOffers(ctx.app.db, repo, ctx.auth.user))}
15751578 ${latest === null
15761579 ? ""
test/fork-offer-size.test.ts added +131
@@ -0,0 +1,131 @@
1 /** An offer upstream says how big it is: files, +added −removed, cached by oids (#14561). */
2 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
3 import { mkdtemp, rename, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts";
7 import { openPull } from "../src/actions/pulls.ts";
8 import { createApp, type App } from "../src/app.ts";
9 import { createUser, type User } from "../src/core/users.ts";
10 import { offerSize, offerSizes, parseNumstat, sizeLine, sizeWords, SIZE_CAP } from "../src/git/offer_size.ts";
11 import { repoDir } from "../src/git/repos.ts";
12 import { serve, type Servers } from "../src/http/server.ts";
13 import { promote } from "./helpers/members.ts";
14 import type { Repo } from "../src/core/repos.ts";
15 import type { FileWrite } from "../src/git/write.ts";
16
17 describe("fork offer size", () => {
18 let dir: string;
19 let app: App;
20 let servers: Servers;
21 let alice: User;
22 let bob: User;
23 let up: Repo;
24 let fork: Repo;
25
26 const offer = async (branch: string, files: FileWrite[]) => {
27 const pushed = await commitToBranch(app, bob, fork, { branch, message: branch, files });
28 if (!pushed.ok) throw new Error(JSON.stringify(pushed.error));
29 const pull = await openPull(app, bob, up, { title: `T ${branch}`, baseBranch: "main", headBranch: branch, headRepo: fork });
30 if (!pull.ok) throw new Error(JSON.stringify(pull.error));
31 return pull.value;
32 };
33
34 beforeAll(async () => {
35 dir = await mkdtemp(join(tmpdir(), "forge-offer-size-"));
36 app = createApp({ dataDir: dir, port: 0 });
37 servers = await serve(app);
38 const mk = async (name: string, first: boolean) => {
39 const made = await createUser(app.db, {
40 origin: "seed", trusted: true, username: name, email: `${name}@forge.local`,
41 password: "a-good-passphrase", adminIfFirst: first,
42 });
43 if (!made.ok) throw new Error(made.error);
44 return made.value;
45 };
46 alice = await mk("alice", true);
47 bob = await mk("bob", false);
48 promote(app.db, bob);
49 const made = await createRepo(app, alice, { name: "lib", visibility: "public" });
50 if (!made.ok) throw new Error(String(made.error));
51 up = made.value;
52 const seeded = await commitToBranch(app, alice, up, {
53 message: "Start", files: [{ path: "README.md", content: "# lib\nold\n" }],
54 });
55 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
56 const f = await forkRepo(app, bob, up);
57 if (!f.ok) throw new Error(String(f.error));
58 fork = f.value;
59 });
60
61 afterAll(async () => {
62 servers.stop();
63 await rm(dir, { recursive: true, force: true });
64 });
65
66 test("numstat text: a binary file is a file with no lines", () => {
67 expect(parseNumstat("3\t1\ta.txt\n-\t-\tpic.png\n")).toEqual({ files: 2, additions: 3, deletions: 1 });
68 expect(sizeWords({ files: 1, additions: 2, deletions: 0 })).toBe("1 file, +2 −0");
69 });
70
71 test("a small offer's numbers show on the page", async () => {
72 const pull = await offer("small", [{ path: "a.txt", content: "1\n2\n3\n" }, { path: "README.md", content: "# lib\nnew\n" }]);
73 expect(await offerSize(app, up, pull.number)).toEqual({ files: 2, additions: 4, deletions: 1 });
74 const body = await (await fetch(`${servers.url}/alice/lib`)).text();
75 expect(body).toContain("2 files, +4 −1");
76 });
77
78 test("a binary file counts as a file with no lines", async () => {
79 const pull = await offer("bin", [{ path: "x.bin", content: new Uint8Array([0, 1, 2, 0, 255, 0]) }]);
80 expect(await offerSize(app, up, pull.number)).toEqual({ files: 1, additions: 0, deletions: 0 });
81 });
82
83 test("a missing head is said in one line", async () => {
84 const pull = await offer("lost", [{ path: "l.txt", content: "l\n" }]);
85 app.db.run("UPDATE pull SET head_oid = ? WHERE issue_id = ?", ["1".repeat(40), pull.id]);
86 expect(await offerSize(app, up, pull.number)).toBe("gone");
87 const body = await (await fetch(`${servers.url}/alice/lib`)).text();
88 expect(body).toContain("its head is gone");
89 });
90
91 test("a second render reuses the cache", async () => {
92 const pull = await offer("cached", [{ path: "c.txt", content: "c\n" }]);
93 const first = await offerSize(app, up, pull.number);
94 expect(first).toEqual({ files: 1, additions: 1, deletions: 0 });
95 const there = repoDir(app.reposRoot, up.ownerName, up.name);
96 await rename(there, `${there}.away`);
97 try {
98 expect(await offerSize(app, up, pull.number)).toEqual(first);
99 } finally {
100 await rename(`${there}.away`, there);
101 }
102 });
103
104 test("a slow or failing git leaves the row without a size and does not hold the page", async () => {
105 const slow = () => new Promise<never>(() => {});
106 const started = performance.now();
107 const got = await offerSizes(app, up, [1, 2], slow, 30);
108 expect(got.size).toBe(0);
109 expect(performance.now() - started).toBeLessThan(1000);
110 const failing: typeof offerSize = async (_a, _r, n) => {
111 if (n === 1) throw new Error("git broke");
112 return { files: 1, additions: 1, deletions: 0 };
113 };
114 const mixed = await offerSizes(app, up, [1, 2], failing, 500);
115 expect(mixed.has(1)).toBe(false);
116 expect(mixed.get(2)).toEqual({ files: 1, additions: 1, deletions: 0 });
117 expect(sizeLine(mixed.get(1))).toBe("");
118 });
119
120 test("only the first few offers are asked", async () => {
121 const asked: number[] = [];
122 const ask: typeof offerSize = async (_a, _r, n) => {
123 asked.push(n);
124 return { files: 1, additions: 0, deletions: 0 };
125 };
126 const nums = Array.from({ length: SIZE_CAP + 4 }, (_, i) => i + 1);
127 const got = await offerSizes(app, up, nums, ask, 500);
128 expect(asked.length).toBe(SIZE_CAP);
129 expect(got.size).toBe(SIZE_CAP);
130 });
131 });
test/timers-named.test.ts +1
@@ -81,6 +81,7 @@ const ALLOWED: readonly Allowed[] = [
8181 ["src/http/flow_stream.ts", "() => { emit(\"flow.settled\""],
8282 ]),
8383 ...allow("setTimeout", `a shell session's resize debounce, ${PER_CONNECTION}`, [["src/machines/shell.ts", "() => session.size === resize"]]),
84 ...allow("setTimeout", "the fork-offers list's size budget: one per page render, cleared when the render ends, resolves a promise and runs nothing", [["src/git/offer_size.ts", "done"]]),
8485 ...allow("setTimeout", `an openai runner bash call's kill deadline, ${PER_CONNECTION}`, [["src/crew/openai_runner.ts", "() => { timedOut = true"]]),
8586 ...allow("setTimeout", SLEEP, [["src/crew/openai_runner.ts", "r"]]),
8687 ...allow("setTimeout", `a check.txt wait's deadline, ${PER_CONNECTION}`, [["src/http/check_text.ts", "() => changed.resolve()"]]),
test/tui-forks-offers.test.ts +1
@@ -81,6 +81,7 @@ describe("the forks screen lists offers from forks", () => {
8181 test("a visible fork's offer is a row, a hidden fork's is absent, Enter opens the pull", async () => {
8282 const t = await open("dave");
8383 expect(t.text()).toContain("offered #1 From bob");
84 expect(t.text()).toContain("1 file, +1 −0");
8485 expect(t.text()).not.toContain("From carol");
8586 const pull = await t.press("\r", "offered #1");
8687 expect(pull).toContain("From bob");

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs