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

A pull's /review row shows its diff stat, check, preview and look without a click #4763

Parked opened this
Closed without merging

scout/forge:scout/4731-a-pull-s-review-row-shows-its-diff-stat → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +241 −4
README.md +5 −1
@@ -449,7 +449,11 @@ only, shows a guest nothing, and names nothing of a placement in a repository
449449 you cannot read.
450450
451451 Review: `/review` lists the asks waiting on you, one row each (title, repository,
452 age, why, and the crew's advice or that it has not advised yet), newest first. It only reads; the buttons stay on `/asks`, which links
452 age, why, and the crew's advice or that it has not advised yet), newest first. A
453 pull's row also carries its evidence: files changed and +/−, the check verdict, the preview
454 link and the before/after look thumbnails (only those you may fetch). The diff stat is kept
455 per base/head commit pair; a request never waits on git, so an uncached one reads "Diff loading"
456 and shows from the next visit (at most 4 are computed at once, and a failed one is retried). It only reads; the buttons stay on `/asks`, which links
453457 to it, and signed out it sends you to sign-in. The terminal has no page of its
454458 own for it: the same asks are the dashboard's On you card.
455459
src/web/studio/review_evidence.ts added +141
@@ -0,0 +1,141 @@
1 /**
2 * What a pull's `/review` row shows without a click: files changed and
3 * +/−, the check verdict, the preview link and the before/after look. All
4 * of it is read from the database except the diff stat, which needs git: it
5 * is kept per base/head oid pair, and a request never waits for it — an
6 * uncached stat reads "Diff loading" while it is computed in the
7 * background (at most `LIMIT` at once), and shows from the next visit on.
8 */
9
10 import type { App } from "../../app.ts";
11 import { getAppByRepo } from "../../core/apps.ts";
12 import { previewEnv } from "../../apps/data.ts";
13 import { latestRunsForIssues } from "../../core/runs.ts";
14 import { LOOK_SIZES, looksForEnv, lookUrl } from "../../core/looks.ts";
15 import { diffBetween, pullDir, type Pull } from "../../core/pulls.ts";
16 import type { Repo } from "../../core/repos.ts";
17 import type { Ctx } from "../../http/context.ts";
18 import { repoBase } from "../chrome.ts";
19 import { html, type Html } from "../html.ts";
20 import { row } from "../ui.ts";
21 import { thumbLook } from "../pages/looks.ts";
22
23 export interface DiffStat {
24 readonly files: number;
25 readonly additions: number;
26 readonly deletions: number;
27 }
28
29 /** Reads one stat from git; `undefined` when git could not produce it. */
30 export type StatReader = (app: App, repo: Repo, pull: Pull) => Promise<DiffStat | undefined>;
31
32 const LIMIT = 4;
33 const KEPT = 2000;
34
35 export const readStat: StatReader = async (app, repo, pull) => {
36 const dir = await pullDir(app.db, app.reposRoot, repo, pull);
37 const { diff, failed } = await diffBetween(dir, pull.baseOid, pull.headOid);
38 return failed ? undefined : { files: diff.files.length, additions: diff.additions, deletions: diff.deletions };
39 };
40
41 export class DiffStats {
42 /** Replaced by a test that counts reads. */
43 read: StatReader = readStat;
44 private readonly kept = new Map<string, DiffStat>();
45 private readonly waiting: Array<() => void> = [];
46 private readonly started = new Set<string>();
47 private running = 0;
48
49 /** The kept stat for this pull, or `undefined` after starting its computation. No git runs in the caller's turn. */
50 stat(app: App, repo: Repo, pull: Pull): DiffStat | undefined {
51 const key = `${pull.baseOid}:${pull.headOid}`;
52 const hit = this.kept.get(key);
53 if (hit !== undefined) return hit;
54 if (this.started.has(key)) return undefined;
55 this.started.add(key);
56 this.waiting.push(() => void this.compute(key, app, repo, pull));
57 this.pump();
58 return undefined;
59 }
60
61 private pump(): void {
62 while (this.running < LIMIT && this.waiting.length > 0) {
63 this.running++;
64 this.waiting.shift()!();
65 }
66 }
67
68 private async compute(key: string, app: App, repo: Repo, pull: Pull): Promise<void> {
69 try {
70 // Queued, so the request that asked has already been answered.
71 const stat = await this.read(app, repo, pull);
72 if (stat !== undefined) {
73 this.kept.set(key, stat);
74 if (this.kept.size > KEPT) this.kept.delete(this.kept.keys().next().value!);
75 }
76 } catch {
77 // A failed read is not kept: the next visit asks again.
78 } finally {
79 this.started.delete(key);
80 this.running--;
81 this.pump();
82 }
83 }
84 }
85
86 const perApp = new WeakMap<App["db"], DiffStats>();
87
88 export function diffStatsOf(app: App): DiffStats {
89 let stats = perApp.get(app.db);
90 if (stats === undefined) perApp.set(app.db, (stats = new DiffStats()));
91 return stats;
92 }
93
94 const CHECK_WORDS: Record<string, string> = {
95 ok: "Check passed",
96 failed: "Check failed",
97 running: "Check running",
98 queued: "Check queued",
99 cancelled: "Check cancelled",
100 none: "No check yet",
101 };
102
103 /** The evidence line under each pull's advice, keyed by pull id; one batch of reads for every row. */
104 export function evidenceFor(ctx: Ctx, pulls: ReadonlyArray<{ repo: Repo; pull: Pull }>): Map<string, Html> {
105 const out = new Map<string, Html>();
106 const checks = latestRunsForIssues(ctx.app.db, pulls.map((p) => p.pull.id));
107 const stats = diffStatsOf(ctx.app);
108 for (const { repo, pull } of pulls) {
109 const stat = stats.stat(ctx.app, repo, pull);
110 const state = checks.get(pull.id)?.state ?? "none";
111 const record = getAppByRepo(ctx.app.db, repo.id);
112 const base = repoBase(repo);
113 const thumb = (env: string, alt: string): Html => {
114 if (record === undefined) return html``;
115 const look = thumbLook(
116 ctx,
117 repo,
118 record,
119 looksForEnv(ctx.app.db, record.id, env),
120 );
121 if (look === undefined) return html``;
122 const { width, height } = LOOK_SIZES.thumb;
123 return html`<img src="${lookUrl(base, look)}" width="${width / 5}" height="${height / 5}" alt="${alt}" loading="lazy" />`;
124 };
125 const before = thumb("production", `${repo.name} on main`);
126 const after = thumb(previewEnv(pull.number), `${repo.name} in #${pull.number}`);
127 const stated = stat === undefined
128 ? "Diff loading"
129 : `${stat.files} ${stat.files === 1 ? "file" : "files"} changed, +${stat.additions} −${stat.deletions}`;
130 out.set(
131 pull.id,
132 row({
133 title: html`${stated} · ${CHECK_WORDS[state] ?? state}${record === undefined
134 ? ""
135 : html` · <a href="${base}/app/pull-${pull.number}/open">Preview</a>`}`,
136 ...(before.text === "" && after.text === "" ? {} : { meta: html`${before}${after}` }),
137 }),
138 );
139 }
140 return out;
141 }
src/web/studio/review_page.ts +7 −1
@@ -1,6 +1,6 @@
11 /**
22 * `/review`: every ask waiting on the viewer as one row — the title, the
3 * repository, how long it has waited and why a person must decide, and what the crew advises — newest
3 * repository, how long it has waited and why a person must decide, what the crew advises, and for a pull its diff stat, check, preview and look — newest
44 * decision first. Read from the same candidates the asks page reads
55 * (`waitingOn`); no polling, and nothing here changes anything.
66 */
@@ -14,6 +14,7 @@ import { html } from "../html.ts";
1414 import { page } from "../layout.ts";
1515 import { empty, listCard, row, time } from "../ui.ts";
1616 import { waitingOn } from "./asks.ts";
17 import { evidenceFor } from "./review_evidence.ts";
1718
1819 export function reviewPage(ctx: Ctx): Response {
1920 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
@@ -31,6 +32,10 @@ export function reviewPage(ctx: Ctx): Response {
3132 return id === undefined ? [] : [id];
3233 }),
3334 );
35 const evidence = evidenceFor(
36 ctx,
37 asks.flatMap((a) => (a.pull === undefined ? [] : [{ repo: a.repo, pull: a.pull }])),
38 );
3439 const rows = asks.map((a) => {
3540 const base = repoBase(a.repo);
3641 const href =
@@ -49,6 +54,7 @@ export function reviewPage(ctx: Ctx): Response {
4954 nav: true,
5055 title: a.pull?.title ?? a.issue?.title ?? a.proposal?.title ?? `${a.repo.name} is waiting on you`,
5156 titleAttr: `${a.repo.ownerName}/${a.repo.name}: ${a.why ?? a.consequence.why}`,
57 children: a.pull === undefined ? [] : [evidence.get(a.pull.id)!],
5258 meta: html`${a.repo.ownerName}/${a.repo.name} · ${time(a.since)} · ${a.why ?? a.consequence.why} · ${advised}`,
5359 });
5460 });
test/review-page.test.ts +88 −2
@@ -8,11 +8,16 @@ import { addComment, openIssue } from "../src/actions/issues.ts";
88 import { addComment as addRawComment } from "../src/core/issues.ts";
99 import { addCrew, createAgentRow, setCrewRole } from "../src/core/agents.ts";
1010 import { createRepo as createRepoRow } from "../src/core/repos.ts";
11 import { createApp as createAppRow } from "../src/core/apps.ts";
12 import { previewEnv } from "../src/apps/data.ts";
13 import { recordLook } from "../src/core/looks.ts";
14 import { createRun } from "../src/core/runs.ts";
1115 import { submitReview } from "../src/core/reviews.ts";
1216 import { createUser, getUserByName } from "../src/core/users.ts";
1317 import { BREAKER_NOTE } from "../src/crew/breaker.ts";
1418 import { serve, type Servers } from "../src/http/server.ts";
1519 import { id } from "../src/kernel/id.ts";
20 import { DiffStats, diffStatsOf } from "../src/web/studio/review_evidence.ts";
1621 import { quietLogs } from "./helpers/globals.ts";
1722
1823 quietLogs();
@@ -29,6 +34,8 @@ beforeAll(async () => {
2934 root = mkdtempSync(join(tmpdir(), "forge-review-"));
3035 app = createApp({ dataDir: root, port: 0 });
3136 servers = await serve(app);
37 // No test here waits on a real git read.
38 diffStatsOf(app).read = async () => undefined;
3239 const mk = async (username: string, agent: boolean) => {
3340 const made = await createUser(app.db, {
3441 username,
@@ -102,7 +109,7 @@ test("lists exactly the three asks, each with title, repository and why", async
102109 const res = await fetch(`${servers.url}/review`, { headers: { cookie } });
103110 const body = await res.text();
104111 expect(res.status).toBe(200);
105 const rows = body.split('<li').filter((r) => r.includes('class="row"'));
112 const rows = body.split('<li').filter((r) => r.includes('class="row"') && r.includes("data-nav"));
106113 expect(rows.length).toBe(3);
107114 const find = (t: string) => rows.find((r) => r.includes(t))!;
108115 expect(find("Handed over pull")).toContain("three reviews asked for changes");
@@ -129,9 +136,88 @@ test("the asks page links to /review", async () => {
129136
130137 test("each row says what the crew advises and why, or that it has not advised yet", async () => {
131138 const body = await (await fetch(`${servers.url}/review`, { headers: { cookie } })).text();
132 const rows = body.split("<li").filter((r) => r.includes('class="row"'));
139 const rows = body.split("<li").filter((r) => r.includes('class="row"') && r.includes("data-nav"));
133140 const find = (t: string) => rows.find((r) => r.includes(t))!;
134141 expect(find("Stale pull")).toContain("The crew advises: change — rename the &lt;flag&gt;.");
135142 expect(find("Handed over pull")).toContain("The crew has not advised yet");
136143 expect(find("Crew asked about this")).toContain("The crew has not advised yet");
137144 });
145
146 const visit = async () => (await fetch(`${servers.url}/review`, { headers: { cookie } })).text();
147 const settle = () => new Promise((r) => setTimeout(r, 20));
148
149 test("a pull's row carries its diff stat, check and preview; a warm request makes no git reads; a failed diff is retried", async () => {
150 const stats = diffStatsOf(app);
151 let reads = 0;
152 let fail = true;
153 stats.read = async () => {
154 reads++;
155 return fail ? undefined : { files: 2, additions: 5, deletions: 1 };
156 };
157 try {
158 await settle();
159 const cold = await visit();
160 expect(cold).toContain("Diff loading");
161 expect(cold).toContain("No check yet");
162 await settle();
163 expect(reads).toBe(1); // both pulls share one base/head pair
164 // The failure was not kept: the next visit asks git again.
165 expect(await visit()).toContain("Diff loading");
166 await settle();
167 expect(reads).toBe(2);
168 fail = false;
169 expect(await visit()).toContain("Diff loading");
170 await settle();
171 expect(reads).toBe(3);
172 const warm = await visit();
173 expect(warm).toContain("2 files changed, +5 −1");
174 expect(warm).not.toContain("Diff loading");
175 await visit();
176 expect(reads).toBe(3);
177 // Beside the row's link, not inside it; a repository that is no app has no preview link.
178 expect(warm).toMatch(/<\/a><ul class="row-children"><li>\s*<div class="row"/);
179 expect(warm).not.toContain(">Preview<");
180 } finally {
181 stats.read = async () => undefined;
182 }
183 });
184
185 test("at most four diff stats are computed at once", async () => {
186 const stats = new DiffStats();
187 let live = 0;
188 let peak = 0;
189 stats.read = async () => {
190 peak = Math.max(peak, ++live);
191 await settle();
192 live--;
193 return { files: 1, additions: 1, deletions: 0 };
194 };
195 const repo = {} as never;
196 for (let i = 0; i < 10; i++) stats.stat(app, repo, { baseOid: ZERO, headOid: String(i) } as never);
197 await new Promise((r) => setTimeout(r, 150));
198 expect(peak).toBe(4);
199 });
200
201 test("an app's pull row links its preview and shows the check and both thumbnails", async () => {
202 const alice = getUserByName(app.db, "alice")!;
203 const repo = app.db.query<{ id: string }, []>("SELECT id FROM repo WHERE name = 'wait'").get()!;
204 const record = createAppRow(app.db, { repoId: repo.id, slug: "waitapp", access: "public" });
205 if (!record.ok) throw new Error(record.error);
206 const pullRow = app.db
207 .query<{ id: string; number: number }, []>("SELECT id, number FROM issue WHERE title = 'Handed over pull'")
208 .get()!;
209 const run = createRun(app.db, { repoId: repo.id, kind: "check", issueId: pullRow.id, commitOid: ZERO, actorId: alice.id });
210 app.db.query("UPDATE run SET state = 'ok' WHERE id = ?1").run(run.id);
211 for (const env of ["production", previewEnv(pullRow.number)])
212 recordLook(app.db, { appId: record.value.id, env, commitOid: ZERO, kind: "thumb", path: "looks/none.png" });
213 await settle();
214 const body = await visit();
215 const from = body.indexOf("Handed over pull");
216 const found = body.slice(from, body.indexOf("Stale pull"));
217 expect(found).toContain("Check passed");
218 expect(found).toContain(`href="/alice/wait/app/pull-${pullRow.number}/open">Preview</a>`);
219 expect(found.match(/<img /g)?.length).toBe(2);
220 // The pictures are not inside the row's link.
221 expect(found.slice(0, found.indexOf("</a>"))).not.toContain("<img");
222 expect(found.indexOf("<img")).toBeGreaterThan(found.indexOf("</a>"));
223 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs