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

Each /review row shows the crew's recommendation with its reason and the evidence: diff stat, check, preview and look #4495

Closed opened this
Closed without merging

scout/forge:scout/4384-each-review-row-shows-the-crew-s-recomme → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +262 −5
src/web/studio/review_evidence.ts added +119
@@ -0,0 +1,119 @@
1 /**
2 * What `/review` shows beside an ask on a pull request so nobody has to
3 * open it: the diff stat, the check, the preview link and the before/after
4 * look. Plain data out (`Evidence`); the page words it. The diff stat is the
5 * only part that asks git, so it is read once per pair of oids, in parallel,
6 * and kept in memory (a head never changes its numbers).
7 */
8
9 import { previewEnv } from "../../apps/data.ts";
10 import { appsForRepos } from "../../core/apps.ts";
11 import { DEFAULT_PAGE, looksForApps, lookUrl, type Look } from "../../core/looks.ts";
12 import { diffBetween, pullDir, type Pull } from "../../core/pulls.ts";
13 import type { Repo } from "../../core/repos.ts";
14 import { latestRunsForIssues, type Run } from "../../core/runs.ts";
15 import type { Ctx } from "../../http/context.ts";
16 import { repoBase } from "../chrome.ts";
17 import { mayFetchLook, pictured } from "../pages/looks.ts";
18
19 export interface Evidence {
20 readonly stat?: { readonly files: number; readonly additions: number; readonly deletions: number };
21 readonly check?: Run;
22 readonly preview?: string;
23 readonly before?: string;
24 readonly after?: string;
25 }
26
27 interface Subject {
28 readonly repo: Repo;
29 readonly pull: Pull;
30 }
31
32 let reads = 0;
33 /** How many times git was asked for a diff stat since boot (tests count it). */
34 export const diffReads = (): number => reads;
35 const KEPT = 500;
36 const WIDTH = 4;
37 /** Successful stats only: a failure is asked again on the next visit. */
38 const stats = new Map<string, NonNullable<Evidence["stat"]>>();
39 const queued: Array<() => Promise<void>> = [];
40 const running = new Set<Promise<void>>();
41 const wanted = new Set<string>();
42
43 function pump(): void {
44 while (running.size < WIDTH && queued.length > 0) {
45 const job = queued.shift()!;
46 const p: Promise<void> = job().finally(() => {
47 running.delete(p);
48 pump();
49 });
50 running.add(p);
51 }
52 }
53
54 /** Resolves once every diff stat started so far has finished (tests, shutdown). */
55 export async function statsSettled(): Promise<void> {
56 while (running.size > 0 || queued.length > 0) await Promise.all([...running]);
57 }
58
59 /**
60 * The stat if it is kept; otherwise undefined, and git is asked in the
61 * background (at most `WIDTH` at once) so the next visit has it. A page
62 * never waits on git.
63 */
64 function statOf(ctx: Ctx, { repo, pull }: Subject): Evidence["stat"] {
65 const key = `${repo.id}:${pull.baseOid}:${pull.headOid}`;
66 const kept = stats.get(key);
67 if (kept !== undefined || wanted.has(key)) return kept;
68 wanted.add(key);
69 queued.push(async () => {
70 reads++;
71 try {
72 const dir = await pullDir(ctx.app.db, ctx.app.reposRoot, repo, pull);
73 const { diff, failed } = await diffBetween(dir, pull.baseOid, pull.headOid);
74 if (!failed) {
75 if (stats.size >= KEPT) stats.delete(stats.keys().next().value!);
76 stats.set(key, { files: diff.files.length, additions: diff.additions, deletions: diff.deletions });
77 }
78 } catch {
79 // asked again on the next visit
80 } finally {
81 wanted.delete(key);
82 }
83 });
84 pump();
85 return undefined;
86 }
87
88 /** The evidence for each pull, by pull id. */
89 export function evidenceFor(ctx: Ctx, subjects: readonly Subject[]): Map<string, Evidence> {
90 const db = ctx.app.db;
91 const checks = latestRunsForIssues(db, subjects.map((s) => s.pull.id));
92 const records = appsForRepos(db, [...new Set(subjects.map((s) => s.repo.id))]);
93 const looks = looksForApps(db, [...records.values()].map((r) => r.id));
94 const stat = subjects.map((s) => statOf(ctx, s));
95 const out = new Map<string, Evidence>();
96 subjects.forEach((s, i) => {
97 const record = records.get(s.repo.id);
98 const check = checks.get(s.pull.id);
99 let before: Look | undefined;
100 let after: Look | undefined;
101 if (record !== undefined) {
102 const home = looks.filter((l) => l.appId === record.id && l.page === DEFAULT_PAGE);
103 const pick = (env: string) => pictured(home.filter((l) => l.env === env), "thumb");
104 before = pick("production");
105 after = pick(previewEnv(s.pull.number));
106 if (before !== undefined && !mayFetchLook(ctx, s.repo, record, before)) before = undefined;
107 if (after !== undefined && !mayFetchLook(ctx, s.repo, record, after)) after = undefined;
108 }
109 const base = repoBase(s.repo);
110 out.set(s.pull.id, {
111 ...(stat[i] === undefined ? {} : { stat: stat[i] }),
112 ...(check === undefined ? {} : { check }),
113 ...(record === undefined ? {} : { preview: `${base}/app/pull-${s.pull.number}/open` }),
114 ...(before === undefined ? {} : { before: lookUrl(base, before) }),
115 ...(after === undefined ? {} : { after: lookUrl(base, after) }),
116 });
117 });
118 return out;
119 }
src/web/studio/review_page.ts +50 −1
@@ -12,8 +12,47 @@ import { repoBase, signInFirst } from "../chrome.ts";
1212 import { html } from "../html.ts";
1313 import { page } from "../layout.ts";
1414 import { empty, listCard, row, time } from "../ui.ts";
15 import type { Recommendation } from "../../core/pull_wait.ts";
16 import { evidenceFor, type Evidence } from "./review_evidence.ts";
17 import { testsClause } from "./words.ts";
1518 import { waitingOn } from "./asks.ts";
1619
20 /** What the crew advised, or that it has not: the first row under an ask. */
21 const advice = (rec: Recommendation | undefined) =>
22 rec === undefined
23 ? row({ title: "The crew has not advised yet" })
24 : row({ title: `The crew advises: ${rec.verdict}`, ...(rec.reason === "" ? {} : { meta: html`${rec.reason}` }) });
25
26 /** A look as a row under its pull: the picture is named, never a link of its own. */
27 const lookRow = (title: string, src: string) =>
28 row({
29 title,
30 lead: html`<span class="look" data-slot="thumb"><img src="${src}" width="30" height="40" alt="${title}" loading="lazy" /></span>`,
31 });
32
33 /** The evidence rows of a pull: diff stat and check, preview, then the looks. */
34 function evidenceRows(e: Evidence) {
35 const check =
36 e.check === undefined
37 ? "no check yet"
38 : e.check.state === "ok"
39 ? `check passed${testsClause(e.check) === "" ? "" : ` (${testsClause(e.check)})`}`
40 : e.check.state === "failed"
41 ? "check failed"
42 : e.check.state === "cancelled"
43 ? "check cancelled"
44 : "check running";
45 return [
46 row({
47 title: e.stat === undefined ? "Diff loading" : `${e.stat.files} ${e.stat.files === 1 ? "file" : "files"}, +${e.stat.additions} −${e.stat.deletions}`,
48 meta: html`${check}`,
49 }),
50 ...(e.preview === undefined ? [] : [row({ href: e.preview, title: "Open the preview" })]),
51 ...(e.before === undefined ? [] : [lookRow("Look before: production", e.before)]),
52 ...(e.after === undefined ? [] : [lookRow("Look after: this pull", e.after)]),
53 ];
54 }
55
1756 export function reviewPage(ctx: Ctx): Response {
1857 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
1958 const user = ctx.auth.user;
@@ -22,6 +61,12 @@ export function reviewPage(ctx: Ctx): Response {
2261 const repos = listReposVisibleTo(ctx.app.db, user, { limit: 200, withoutArchived: true });
2362 return waitingOn(ctx.app.db, user, repos).sort((a, b) => Date.parse(b.since) - Date.parse(a.since));
2463 });
64 const evidence = timePhase(ctx.request, "evidence", () =>
65 evidenceFor(
66 ctx,
67 asks.flatMap((a) => (a.pull === undefined ? [] : [{ repo: a.repo, pull: a.pull }])),
68 ),
69 );
2570 const rendering = performance.now();
2671 const rows = asks.map((a) => {
2772 const base = repoBase(a.repo);
@@ -31,12 +76,16 @@ export function reviewPage(ctx: Ctx): Response {
3176 : a.issue !== undefined
3277 ? `${base}/issues/${a.issue.number}`
3378 : base;
79 const title = a.pull?.title ?? a.issue?.title ?? a.proposal?.title ?? `${a.repo.name} is waiting on you`;
80 const found = a.pull === undefined ? undefined : evidence.get(a.pull.id);
3481 return row({
3582 href,
3683 nav: true,
37 title: a.pull?.title ?? a.issue?.title ?? a.proposal?.title ?? `${a.repo.name} is waiting on you`,
84 data: { ask: a.id },
85 title,
3886 titleAttr: `${a.repo.ownerName}/${a.repo.name}: ${a.why ?? a.consequence.why}`,
3987 meta: html`${a.repo.ownerName}/${a.repo.name} · ${time(a.since)} · ${a.why ?? a.consequence.why}`,
88 children: [advice(a.recommendation), ...(found === undefined ? [] : evidenceRows(found))],
4089 });
4190 });
4291 const body = html`<h1>Review</h1>
test/review-page.test.ts +93 −4
@@ -5,6 +5,12 @@ import { tmpdir } from "node:os";
55 import { join } from "node:path";
66 import { createApp, type App } from "../src/app.ts";
77 import { addComment, openIssue } from "../src/actions/issues.ts";
8 import { openPull } from "../src/actions/pulls.ts";
9 import { commitToBranch, createRepo as createRepoAction } from "../src/actions/repos.ts";
10 import { createApp as createAppRecord } from "../src/core/apps.ts";
11 import { diffReads, statsSettled } from "../src/web/studio/review_evidence.ts";
12 import { recordLook } from "../src/core/looks.ts";
13 import { claimRun, createRun, finishRun } from "../src/core/runs.ts";
814 import { addCrew, createAgentRow, setCrewRole } from "../src/core/agents.ts";
915 import { createRepo as createRepoRow } from "../src/core/repos.ts";
1016 import { submitReview } from "../src/core/reviews.ts";
@@ -76,11 +82,40 @@ beforeAll(async () => {
7682 const handed = pull(builder.id, "Handed over pull", 3);
7783 for (let r = 0; r < 3; r++)
7884 submitReview(app.db, { issueId: handed, authorId: reviewer.id, state: "changes_requested", body: `Round ${r}.`, commitOid: ZERO });
85 app.db
86 .query("INSERT INTO comment (id, issue_id, author_id, body, created_at) VALUES (?1, ?2, ?3, ?4, ?5)")
87 .run(id(), handed, reviewer.id, "Recommendation: close — three rounds, still failing.", hoursAgo(1));
7988 pull(alice.id, "Stale pull", 30);
8089 pull(builder.id, "Young crew pull", 2);
8190 const issue = openIssue(app, alice, repo, { title: "Crew asked about this" });
8291 if (!issue.ok) throw new Error(String(issue.error));
8392 addComment(app, alice, repo, issue.value, BREAKER_NOTE);
93 const shown = await createRepoAction(app, alice, { name: "shown", visibility: "public" });
94 if (!shown.ok) throw new Error(String(shown.error));
95 await commitToBranch(app, alice, shown.value, { message: "seed", files: [{ path: "README.md", content: "# shown\n" }] });
96 await commitToBranch(app, alice, shown.value, {
97 branch: "feat",
98 base: "main",
99 message: "Feature",
100 files: [{ path: "a.md", content: "one\ntwo\n" }, { path: "b.md", content: "x\n" }],
101 });
102 const feature = await openPull(app, alice, shown.value, { title: "Evidence pull", body: "", baseBranch: "main", headBranch: "feat" });
103 if (!feature.ok) throw new Error(String(feature.error));
104 app.db.query("UPDATE issue SET created_at = ?2 WHERE id = ?1").run(feature.value.id, hoursAgo(30));
105 const run = createRun(app.db, {
106 repoId: shown.value.id,
107 kind: "check",
108 issueId: feature.value.id,
109 env: `pull-${feature.value.number}`,
110 commitOid: feature.value.headOid,
111 reason: "pull",
112 });
113 claimRun(app.db, run.id);
114 finishRun(app.db, run.id, { state: "ok", summary: "green" });
115 const record = createAppRecord(app.db, { repoId: shown.value.id, slug: "shown", access: "public" });
116 if (!record.ok) throw new Error(record.error);
117 for (const [env, oid] of [["production", ZERO], [`pull-${feature.value.number}`, feature.value.headOid]] as const)
118 recordLook(app.db, { appId: record.value.id, env, commitOid: oid, kind: "thumb", path: `looks/${env}.png`, takenAs: "nobody" });
84119 const signin = await fetch(`${servers.url}/signin`, {
85120 method: "POST",
86121 headers: { origin: servers.url },
@@ -96,17 +131,17 @@ afterAll(async () => {
96131 rmSync(root, { recursive: true, force: true });
97132 });
98133
99 test("lists exactly the three asks, each with title, repository and why", async () => {
134 test("lists exactly the four asks, each with title, repository and why", async () => {
100135 const res = await fetch(`${servers.url}/review`, { headers: { cookie } });
101136 const body = await res.text();
102137 expect(res.status).toBe(200);
103 const rows = body.split('<li').filter((r) => r.includes('class="row"'));
104 expect(rows.length).toBe(3);
138 const rows = body.split("<li data-ask").slice(1);
139 expect(rows.length).toBe(4);
105140 const find = (t: string) => rows.find((r) => r.includes(t))!;
106141 expect(find("Handed over pull")).toContain("three reviews asked for changes");
107142 expect(find("Stale pull")).toContain("waiting 30 hours");
108143 expect(find("Crew asked about this")).toContain("parked by the crew");
109 for (const r of rows) expect(r).toContain("alice/wait");
144 for (const r of rows) expect(r).toMatch(/alice\/(wait|shown)/);
110145 expect(body).not.toContain("Young crew pull");
111146 expect(body).toContain("/alice/wait/pulls/");
112147 expect(body).toContain("/alice/wait/issues/");
@@ -124,3 +159,57 @@ test("the asks page links to /review", async () => {
124159 const res = await fetch(`${servers.url}/asks`, { headers: { cookie } });
125160 expect(await res.text()).toContain('href="/review"');
126161 });
162
163 test("a pull's row shows the crew's advice, diff stat, check, preview and look", async () => {
164 const cold = await fetch(`${servers.url}/review`, { headers: { cookie } });
165 expect(await cold.text()).toContain("Diff loading");
166 await statsSettled();
167 const res = await fetch(`${servers.url}/review`, { headers: { cookie } });
168 const body = await res.text();
169 const rows = body.split("<li data-ask").slice(1);
170 const find = (t: string) => rows.find((r) => r.includes(t))!;
171 expect(find("Evidence pull")).toContain("2 files, +3 −0");
172 expect(find("Evidence pull")).toContain("check passed");
173 expect(find("Evidence pull")).toContain('href="/alice/shown/app/pull-');
174 expect(find("Evidence pull")).toContain("/alice/shown/looks/production/");
175 expect(find("Evidence pull")).toMatch(/\/alice\/shown\/looks\/pull-\d+\//);
176 expect(find("Evidence pull")).toContain('alt="Look after: this pull"');
177 expect(find("Evidence pull")).not.toMatch(/<a[^>]*>\s*<span class="look"/);
178 expect(find("Evidence pull")).toContain("The crew has not advised yet");
179 expect(find("Handed over pull")).toContain("The crew advises: close");
180 expect(find("Handed over pull")).toContain("three rounds, still failing.");
181 expect(find("Crew asked about this")).not.toContain("check");
182 });
183
184 test("fifty asks never wait on git, read each diff stat once", async () => {
185 const shown = app.db.query<{ id: string }, []>("SELECT id FROM repo WHERE name = 'shown'").get()!;
186 const real = app.db.query<{ base_oid: string; head_oid: string }, [string]>("SELECT base_oid, head_oid FROM pull WHERE head_branch = ?1").get("feat")!;
187 const alice = getUserByName(app.db, "alice")!;
188 for (let n = 0; n < 50; n++) {
189 const issueId = id();
190 app.db
191 .query(
192 `INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state, created_at)
193 VALUES (?1, ?2, ?3, 'pull', ?4, '', ?5, 'open', ?6)`,
194 )
195 .run(issueId, shown.id, 100 + n, `Bulk ${n}`, alice.id, hoursAgo(40));
196 app.db
197 .query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id) VALUES (?1, 'main', ?2, ?3, ?4, ?5)")
198 .run(issueId, `bulk/${n}`, real.base_oid, real.head_oid, shown.id);
199 }
200 const get = async () => (await fetch(`${servers.url}/review`, { headers: { cookie } })).text();
201 // The asks come from the 50 most recently touched open pulls (`askCandidatesFor`,
202 // shared with /asks): the 50 bulk pulls push the three older ones out, and the
203 // parked issue is the 51st ask.
204 const before = diffReads();
205 const first = await get();
206 expect(first.split("<li data-ask").length - 1).toBe(51);
207 expect(first).not.toContain("Handed over pull");
208 await statsSettled();
209 expect(diffReads() - before).toBeLessThanOrEqual(1);
210 const reads = diffReads();
211 const warm = await get();
212 expect(diffReads()).toBe(reads);
213 expect(warm).toContain("2 files, +3 −0");
214 expect(warm).not.toContain("Diff loading");
215 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs