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

The family tree shows how far ahead of and behind its upstream each fork is, on the node #16618

opened this

Check · Review · Land · Live — Its check is red. Next: builder, its check is red.

Ready to land

scout/forge:scout/16305-the-family-tree-shows-how-far-ahead-of-a → main 7 commits fork 4 threads · 4 unresolved

Review

Sign in to review this change.
6 files changed +216 −16
docs/features/fork-tree-counts.md added +21
@@ -0,0 +1,21 @@
1 ---
2 topic: What a forge is
3 row: Commits ahead and behind on each fork's node in the family tree
4 ---
5 # Commits ahead and behind on each fork's node in the family tree
6
7 Each fork's node in the family tree (an app's `/forks` page) says how many
8 commits it is ahead of, then behind, its upstream: `↑2 ↓5`. Screen readers hear
9 "2 commits ahead, 5 commits behind its upstream". The numbers come from the
10 relation the tree already reads, cached by the two tips, never a walk of their
11 own.
12
13 When a count cannot be known the node shows a dash with its reason, never zero:
14 `upstream archived`, `upstream frozen`, or `upstream could not be compared`.
15
16 - On the web: open an app's `/forks` page and read the two numbers on each node.
17 - In the terminal: the fork tree screen adds `↑2 ↓5` after each node's
18 `N behind` once the pair has been counted. A pair that cannot be counted shows
19 `behind unknown`, which is the terminal's one wording for every reason: the
20 tree reader keeps only the stored counts, and naming an archived or frozen
21 upstream would need the page's own read of each repository.
src/actions/fork_tree.ts +5 −1
@@ -14,7 +14,7 @@
1414
1515 import type { Database } from "bun:sqlite";
1616 import { FOLLOW_WORD, followedWords } from "./follow_words.ts";
17 import { keptBehind, type Following } from "../core/following.ts";
17 import { keptBehind, keptStanding, type Following } from "../core/following.ts";
1818 import { lastFollows } from "../core/last_follow.ts";
1919 import { FORK_DEFAULT_MODE, forkFollowModes, lastTookAts } from "../core/fork_follow.ts";
2020 import { canRead, forkParentFor, visibleForksByParent, type Repo } from "../core/repos.ts";
@@ -47,6 +47,8 @@ export interface TreeNode {
4747 * tips.
4848 */
4949 readonly behind: number | null;
50 /** Commits ahead of its parent, from the same kept count as `behind`; absent when not counted. */
51 readonly ahead?: number;
5052 /** When it last followed, in one line from the sync record; absent for the root. */
5153 readonly followed?: string;
5254 /** Set on a root that is a fork of something the caller may not read: a hidden fork, not a gap.
@@ -127,6 +129,7 @@ export function forkTree(
127129 if (r.parent === null && !hides && severedRoot(db, r.repo))
128130 return { repo: nameOf(r.repo), parent: null, depth: r.depth, follow: "dont-follow" as const, word: FOLLOW_WORD["dont-follow"], behind: null };
129131 const mode = r.parent === null && !hides ? undefined : (modes.get(r.repo.id) ?? FORK_DEFAULT_MODE);
132 const standing = r.parent === null ? undefined : keptStanding(forge, r.repo, r.parent);
130133 return {
131134 repo: nameOf(r.repo),
132135 parent: r.parent === null ? null : nameOf(r.parent),
@@ -134,6 +137,7 @@ export function forkTree(
134137 follow: mode === undefined ? "autonomous" : treeFollow(modes.get(r.repo.id)),
135138 word: mode === undefined ? "" : FOLLOW_WORD[mode],
136139 behind: r.parent === null ? null : (keptBehind(forge, r.repo, r.parent) ?? null),
outdated

keptStanding is called twice with a ! assertion. Compute it once into a const above the object and spread from that.

140 ...(standing === undefined ? {} : { ahead: standing[2] }),
137141 ...(r.parent === null ? {} : { followed: followedWords(follows.get(r.repo.id) ?? null, took.get(r.repo.id) ?? null) }),
138142 ...(hides ? { hiddenParent: true as const } : {}),
139143 };
src/tui/screens/fork_tree.ts +2 −1
@@ -24,7 +24,8 @@ export function treeLines(tree: ForkTree | undefined, asked: string, width = 80,
2424 if (tree === undefined) return ["Nothing to show."];
2525 const lines = tree.nodes.map((n) => {
2626 const phrase = n.word === "" ? "" : treeFollowPhrase(n.follow);
27 const behind = ` · ${behindChip(n.behind)}`;
27 const counts = n.ahead === undefined || n.behind === null ? "" : ` · ↑${n.ahead} ↓${n.behind}`;
28 const behind = ` · ${behindChip(n.behind)}${counts}`;
2829 const took = n.followed === undefined ? "" : ` · ${n.followed}`;
2930 const follow = (phrase === "" ? "" : ` ${phrase}${behind}${took}`) + (n.hiddenParent === true ? ` ${HIDDEN_FORK_WORD}` : "");
3031 const you = n.repo !== asked && viewer !== undefined && n.repo.startsWith(`${viewer}/`) ? " (you)" : "";
src/web/pages/following.ts +53 −14
@@ -63,18 +63,44 @@ function firstOwn(n: LineageNode, hereId: string, viewerId: string): string | un
6363 return undefined;
6464 }
6565
66 /** Why a node shows a dash and not a count. */
67 export const NO_COUNT = {
68 archived: "upstream archived",
69 frozen: "upstream frozen",
70 unread: "upstream could not be compared",
71 } as const;
72
73 const dash = (reason: string, tone: Tone): Html =>
outdated

The doc comment for lineageCard (lines 70-72) now attaches to NO_COUNT. Move this block above it.

74 badge(html`<span data-counts="none">—<span class="sr-only"> ${reason}</span></span>`, { tone, title: reason });
75
6676 /**
67 * The family as a tree: nested lists, so the nesting a sighted reader sees
68 * as indentation is the nesting a screen reader announces, and a phone
69 * never has to scroll sideways to follow it. The repository being looked at
70 * is marked "you are here", the viewer's other forks say "you", and every
71 * fork carries its standing against its own parent.
77 * The one chip on a fork's node for where it stands against its upstream: commits ahead, then
78 * behind (`↑a ↓b`), from the relation the tree already read (cached by the two tips, never a walk
79 * of its own). Words for a screen reader; a count that cannot be known is a dash with its reason,
80 * never zero. Toned as `relationBadge` is: attention for diverged or frozen, success in step.
7281 */
73 export function lineageCard(lineage: Lineage, hereId: string, viewerId?: string): Html {
74 // The viewer's own node carries the page's one anchor, so `#your-fork` scrolls to it and
75 // `:target` marks it; rows of theirs beyond the first say "you" without the id.
76 const mineId = viewerId === undefined ? undefined : firstOwn(lineage.root, hereId, viewerId);
77 const node = (n: LineageNode, depth: number): Html => {
82 export function countsOf(relation: ForkRelation | undefined, parent: Repo): Html {
83 if (parent.archivedAt !== null) return dash(NO_COUNT.archived, "default");
84 if (relation === undefined) return dash(NO_COUNT.unread, "default");
85 if (relation.parent.detached !== null) return dash(NO_COUNT.frozen, "attn");
86 const n = (c: number) => `${c} ${c === 1 ? "commit" : "commits"}`;
87 const tone: Tone = relation.state === "synced" ? "success" : relation.state === "diverged" ? "attn" : "default";
88 return badge(
89 html`<span data-counts>${relation.state === "diverged" ? "diverged · " : ""}<span aria-hidden="true">↑<span data-ahead>${relation.ahead}</span> ↓<span data-behind>${relation.behind}</span></span><span class="sr-only">${n(relation.ahead)} ahead, ${n(relation.behind)} behind its upstream</span></span>`,
90 { tone },
91 );
92 }
93
outdated

|| relation === undefined is only there to narrow the type. Restructure so the narrowing comes naturally.

94 interface RowContext {
95 readonly lineage: Lineage;
96 readonly hereId: string;
97 readonly viewerId: string | undefined;
98 readonly mineId: string | undefined;
99 }
100
101 /** One fork's row, and under it the rows of the forks of it. */
102 function lineageRow(ctx: RowContext, n: LineageNode, depth: number, parent?: Repo): Html {
103 const { lineage, hereId, viewerId, mineId } = ctx;
78104 const here = n.repo.id === hereId;
79105 const trail = [
80106 here ? badge("you are here", { variant: "outline" }) : null,
@@ -83,7 +109,7 @@ export function lineageCard(lineage: Lineage, hereId: string, viewerId?: string)
83109 !here && viewerId !== undefined && n.repo.ownerId === viewerId ? badge("you", { variant: "outline" }) : null,
84110 depth === 0 && lineage.hiddenParent ? html`<span class="muted small">${HIDDEN_FORK_WORD}</span>` : null,
85111 n.mode === undefined ? null : html`<span class="muted small">${FOLLOW_WORD[n.mode]}</span>`,
86 n.relation === undefined ? null : relationBadge(n.relation),
112 parent === undefined ? null : countsOf(n.relation, parent),
87113 ].filter((t): t is Html => t !== null);
outdated

Design principle 1: relationBadge on the next line already says N behind, N ahead or parent frozen. Two chips on one row say the same thing. Merge them into one.

88114 return row({
89115 href: `/${n.repo.ownerName}/${n.repo.name}`,
@@ -95,12 +121,25 @@ export function lineageCard(lineage: Lineage, hereId: string, viewerId?: string)
95121 title: `${n.repo.ownerName}/${n.repo.name}`,
96122 ...(n.repo.description === "" ? {} : { meta: html`${n.repo.description}` }),
97123 ...(trail.length === 0 ? {} : { trail: html`${trail}` }),
98 children: n.children.map((c) => node(c, depth + 1)),
124 children: n.children.map((c) => lineageRow(ctx, c, depth + 1, n.repo)),
99125 });
100 };
126 }
127
128 /**
129 * The family as a tree: nested lists, so the nesting a sighted reader sees
130 * as indentation is the nesting a screen reader announces, and a phone
131 * never has to scroll sideways to follow it. The repository being looked at
132 * is marked "you are here", the viewer's other forks say "you", and every
133 * fork carries its standing against its own parent.
134 */
135 export function lineageCard(lineage: Lineage, hereId: string, viewerId?: string): Html {
136 // The viewer's own node carries the page's one anchor, so `#your-fork` scrolls to it and
137 // `:target` marks it; rows of theirs beyond the first say "you" without the id.
138 const mineId = viewerId === undefined ? undefined : firstOwn(lineage.root, hereId, viewerId);
139 const ctx: RowContext = { lineage, hereId, viewerId, mineId };
101140 return html`<section class="settings-block">
102141 <ul class="list" aria-label="Forks, by who follows whom">
103 ${node(lineage.root, 0)}
142 ${lineageRow(ctx, lineage.root, 0)}
104143 </ul>
105144 ${when(
106145 lineage.truncated,
test/family-tree-counts.test.ts added +122
@@ -0,0 +1,122 @@
1 /**
2 * The family tree says, on each fork's node, how many commits it is ahead of and behind its
3 * upstream (#16305): two numbers with words for a screen reader, from the relation the tree
4 * already read, and a dash with its reason where nothing can be said.
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 { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts";
11 import { createApp, type App } from "../src/app.ts";
12 import { comparisons, lineageOf } from "../src/core/following.ts";
13 import { setForkFollowMode } from "../src/core/fork_follow.ts";
14 import { canRead, type Repo } from "../src/core/repos.ts";
15 import { createUser, type User } from "../src/core/users.ts";
16 import { forkTree } from "../src/actions/fork_tree.ts";
17 import { countsOf, lineageCard, NO_COUNT } from "../src/web/pages/following.ts";
18 import { promote } from "./helpers/members.ts";
19
20 const must = <T>(r: { ok: true; value: T } | { ok: false; error: unknown }): T => {
21 if (!r.ok) throw new Error(JSON.stringify(r.error));
22 return r.value;
23 };
24
25 let dataDir: string;
26 let app: App;
27 const users: Record<string, User> = {};
28 let a: Repo, b: Repo, c: Repo;
29
30 const commit = async (who: string, repo: Repo, path: string) =>
31 must(await commitToBranch(app, users[who]!, repo, { message: path, files: [{ path, content: `${path}
32 ` }] }));
33
34 beforeAll(async () => {
35 dataDir = await mkdtemp(join(tmpdir(), "forge-tree-counts-"));
36 app = createApp({ dataDir, port: 0 });
37 for (const name of ["ann", "ben", "cy"]) {
38 users[name] = must(
39 await createUser(app.db, {
40 origin: "seed", trusted: true, username: name, email: `${name}@f.dev`,
41 password: "a-good-passphrase", adminIfFirst: name === "ann",
42 }),
43 );
44 promote(app.db, users[name]!);
45 }
46 a = must(await createRepo(app, users["ann"]!, { name: "app", visibility: "public" }));
47 await commit("ann", a, "one.txt");
48 b = must(await forkRepo(app, users["ben"]!, a, undefined, { visibility: "public" }));
49 c = must(await forkRepo(app, users["cy"]!, b, undefined, { visibility: "public" }));
50 // Forks that follow would take the parent's commits; these hold still so the counts are known.
51 setForkFollowMode(app.db, b.id, "dont-follow");
52 setForkFollowMode(app.db, c.id, "dont-follow");
53 // a is two ahead of b; b has one of its own that c lacks; c has two of its own.
54 await commit("ann", a, "two.txt");
55 await commit("ann", a, "three.txt");
56 await commit("ben", b, "b.txt");
57 await commit("cy", c, "c1.txt");
58 await commit("cy", c, "c2.txt");
59 }, 60_000);
60
61 afterAll(async () => {
62 app?.close();
63 await rm(dataDir, { recursive: true, force: true });
64 });
65
66 test("each fork on a three-level tree shows ahead and behind against its own upstream", async () => {
67 const lineage = await lineageOf(app, a, () => true);
68 const html = String(lineageCard(lineage, a.id));
69 // The root has no upstream: no counts. b: 1 ahead, 2 behind. c: 2 ahead, 1 behind.
70 expect([...html.matchAll(/data-counts/g)]).toHaveLength(2);
71 expect(html).toContain('<span data-ahead>1</span> ↓<span data-behind>2</span>');
72 expect(html).toContain('<span data-ahead>2</span> ↓<span data-behind>1</span>');
73 expect(html).toContain("1 commit ahead, 2 commits behind its upstream");
74 expect(html).toContain("2 commits ahead, 1 commit behind its upstream");
75 expect(html).toContain('aria-hidden="true">↑');
76 });
77
78 test("rendering the counts costs no git comparison of its own", async () => {
79 const lineage = await lineageOf(app, a, () => true);
80 const before = comparisons.count;
81 lineageCard(lineage, a.id);
82 expect(comparisons.count).toBe(before);
83 });
84
85 test("a fork the viewer cannot read is not on the tree, and its counts go with it", async () => {
86 const lineage = await lineageOf(app, a, (r) => canRead(app.db, r, undefined) && r.id !== b.id);
87 const html = String(lineageCard(lineage, a.id));
88 expect(html).not.toContain("data-counts");
89 });
90
91 test("an upstream that cannot be compared is a dash with its reason, never zero", () => {
92 const none = String(countsOf(undefined, a));
93 expect(none).toContain("—");
94 expect(none).toContain(NO_COUNT.unread);
95 expect(none).not.toContain("data-ahead");
96 const archived = String(countsOf(undefined, { ...a, archivedAt: "2026-01-01T00:00:00Z" }));
97 expect(archived).toContain(NO_COUNT.archived);
98 expect(archived).not.toContain(">0<");
99 });
100
101 test("a frozen upstream is a dash with its reason, not the counts", async () => {
102 const lineage = await lineageOf(app, a, () => true);
103 const relation = lineage.root.children[0]!.relation!;
104 const frozen = String(countsOf({ ...relation, parent: { ...relation.parent, detached: { fullName: "x/y", htmlUrl: "", detachedAt: "2026-01-01T00:00:00Z", frozenSince: null } } }, a));
105 expect(frozen).toContain(NO_COUNT.frozen);
106 expect(frozen).not.toContain("data-ahead");
107 });
108
109 test("one chip per node, not a count beside a second badge", async () => {
110 const html = String(lineageCard(await lineageOf(app, a, () => true), a.id));
111 expect(html).not.toContain(" behind</span>");
112 // The state's word survives inside the one chip, once per diverged node (b and c both).
113 expect([...html.matchAll(/diverged/g)]).toHaveLength(2);
114 });
115
116 test("the tree reader carries ahead beside behind from the same kept count", async () => {
117 await lineageOf(app, a, () => true); // counts each pair, which keeps them
118 const by = Object.fromEntries(forkTree(app.db, a, users["ann"]!, app)!.nodes.map((n) => [n.repo, n]));
119 expect([by["ben/app"]!.ahead, by["ben/app"]!.behind]).toEqual([1, 2]);
120 expect([by["cy/app"]!.ahead, by["cy/app"]!.behind]).toEqual([2, 1]);
121 expect(by["ann/app"]!.ahead).toBeUndefined();
122 });
test/fork-tree-chip.test.ts +13
@@ -86,3 +86,16 @@ test("the terminal tree prints the same chip: N behind, up to date, behind unkno
8686 expect(lines[2]).toContain("up to date");
8787 expect(lines[3]).toContain("behind unknown");
8888 });
89
90 test("the terminal tree adds the ahead and behind counts when counted", async () => {
91 const { treeLines } = await import("../src/tui/screens/fork_tree.ts");
92 const tree = {
93 root: "ann/lib",
94 nodes: [
95 { repo: "ann/lib", parent: null, depth: 0, follow: "autonomous" as const, word: "", behind: null },
96 { repo: "a/x", parent: "ann/lib", depth: 1, follow: "offers" as const, word: "proposes", behind: 5, ahead: 2 },
97 ],
98 truncated: false,
99 };
100 expect(treeLines(tree, "ann/lib")[1]).toContain("↑2 ↓5");
101 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs