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

A crew member's scorecard shows the last 30 days beside all time #3178

Parked opened this
Closed without merging

scout/forge:scout/3168-a-crew-member-s-scorecard-shows-the-last → main 3 commits fork 3 threads · 3 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
6 files changed +135 −27
README.md +2
@@ -27,6 +27,8 @@ claim state and the same apps-running note, never a claim link.
2727 Each agent's scorecard — pulls landed of opened, review rounds, red checks,
2828 reverts, time to land — is on Settings → Crew (`/settings/crew`) and in the
2929 terminal's palette as "Crew scorecard per agent"; both are a forge admin's only.
30 Landed, review rounds and red checks show the last 30 days (pulls opened in that
31 window) beside all time as `30d X · all Y`.
3032
3133 Git hosting, code browsing, and a JSON API in one fast Bun process, with
3234 **zero runtime dependencies** — the `package.json` dependency list is empty and
src/core/scorecard.ts +22 −4
@@ -4,7 +4,8 @@
44 * says what an agent spent and how many of its pulls landed; this says how
55 * hard they were to land and whether they stayed landed.
66 *
7 * Every figure is over the pulls an agent authored, all time:
7 * Every figure is over the pulls an agent authored, all time unless `since`
8 * (an ISO time) limits it to the pulls opened at or after that moment:
89 *
910 * reviewRounds = count(changes_requested reviews on its landed pulls) ÷ landed
1011 * redCheckRate = failed ÷ finished (ok + failed) check runs on its pulls
@@ -36,7 +37,10 @@ export interface Scorecard {
3637 }
3738
3839 /** Each agent's scorecard, keyed by its user id: one grouped query. */
39 export function crewScorecards(db: Database): Map<string, Scorecard> {
40 export function crewScorecards(
41 db: Database,
42 opts: { since?: string } = {},
43 ): Map<string, Scorecard> {
4044 const rows = db
4145 .query<
4246 {
@@ -49,7 +53,7 @@ export function crewScorecards(db: Database): Map<string, Scorecard> {
4953 reverts: number;
5054 mean_land_days: number | null;
5155 },
52 []
56 [string | null]
5357 >(
5458 `SELECT issue.author_id AS author_id,
5559 COUNT(*) AS opened,
@@ -74,9 +78,10 @@ export function crewScorecards(db: Database): Map<string, Scorecard> {
7478 FROM issue
7579 JOIN pull ON pull.issue_id = issue.id
7680 JOIN user ON user.id = issue.author_id AND user.is_agent = 1
81 WHERE (?1 IS NULL OR issue.created_at >= ?1)
7782 GROUP BY issue.author_id`,
7883 )
79 .all();
84 .all(opts.since ?? null);
8085 return new Map(
8186 rows.map((r) => {
8287 const checks = r.checks_ok + r.checks_failed;
@@ -98,6 +103,19 @@ export function crewScorecards(db: Database): Map<string, Scorecard> {
98103 );
99104 }
100105
106 /** The window "recent" means on the page and in the terminal. */
107 export const RECENT_DAYS = 30;
108
109 /** Scorecards over pulls opened in the last `RECENT_DAYS`: one definition for both doors. */
110 export function recentScorecards(
111 db: Database,
112 now: number = Date.now(),
113 ): Map<string, Scorecard> {
114 return crewScorecards(db, {
115 since: new Date(now - RECENT_DAYS * 86_400_000).toISOString(),
116 });
117 }
118
101119 /** A time to land in the largest unit that keeps it readable: both doors say it the same way. */
102120 export function spanText(ms: number): string {
103121 const minutes = ms / 60_000;
src/tui/screens/scorecard.ts +15 −5
@@ -8,6 +8,7 @@
88 import { listAllAgents } from "../../actions/crew.ts";
99 import {
1010 crewScorecards,
11 recentScorecards,
1112 spanText,
1213 type Scorecard,
1314 } from "../../core/scorecard.ts";
@@ -24,17 +25,22 @@ import { ListState, drawList, type ListRow } from "./common.ts";
2425 export function scorecardRows(
2526 agents: readonly Agent[],
2627 cards: ReadonlyMap<string, Scorecard>,
28 recent: ReadonlyMap<string, Scorecard> = new Map(),
2729 ): ListRow[] {
2830 return agents.map((agent) => {
2931 const s = cards.get(agent.user.id);
3032 if (s === undefined)
3133 return { title: agent.user.username, secondary: "no pull yet" };
34 const r = recent.get(agent.user.id);
35 /** `30d X · all Y`: the last 30 days beside all time, a dash where there is nothing. */
36 const pair = (say: (c: Scorecard) => string | null): string =>
37 `30d ${(r === undefined ? null : say(r)) ?? "—"} · all ${say(s) ?? "—"}`;
3238 return {
3339 title: agent.user.username,
34 secondary: `${s.landed} landed of ${s.opened}`,
40 secondary: pair((c) => `${c.landed} of ${c.opened}`),
3541 meta: [
36 `review rounds ${s.reviewRounds === null ? "—" : s.reviewRounds.toFixed(1)}`,
37 `red checks ${s.redCheckRate === null ? "—" : `${Math.round(100 * s.redCheckRate)}%`}`,
42 `review rounds ${pair((c) => c.reviewRounds?.toFixed(1) ?? null)}`,
43 `red checks ${pair((c) => (c.redCheckRate === null ? null : `${Math.round(100 * c.redCheckRate)}%`))}`,
3844 `reverts ${s.reverts}`,
3945 `time to land ${s.meanLandMs === null ? "—" : spanText(s.meanLandMs)}`,
4046 ].join(" "),
@@ -48,7 +54,11 @@ export function scorecardScreen(f: Session, ctx: Ctx): Screen {
4854 // Read once when the screen opens, as the page reads it once per request.
4955 const listed = allowed ? listAllAgents(f.app, f.user) : undefined;
5056 const rows =
51 listed?.ok === true ? scorecardRows(listed.value, crewScorecards(f.db)) : [];
57 listed?.ok === true ? scorecardRows(
58 listed.value,
59 crewScorecards(f.db),
60 recentScorecards(f.db),
61 ) : [];
5262 return {
5363 title: "Crew scorecard per agent",
5464 keys: [binding(["j", "k"], "move cursor")],
@@ -59,7 +69,7 @@ export function scorecardScreen(f: Session, ctx: Ctx): Screen {
5969 ? "The crew's scorecard is read by a forge admin."
6070 : rows.length === 0
6171 ? "No agents yet."
62 : "Over every pull each agent opened, as Settings → Crew shows it.";
72 : "Pulls each agent opened: the last 30 days beside all time, as Settings → Crew shows it.";
6373 s.text(head!.x, head!.y, truncate(text, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w);
6474 drawList(s, list!, c, state, rows);
6575 },
src/web/pages/crew.ts +43 −14
@@ -26,6 +26,7 @@ import {
2626 } from "../../core/runs.ts";
2727 import {
2828 crewScorecards,
29 recentScorecards,
2930 spanText,
3031 type Scorecard,
3132 } from "../../core/scorecard.ts";
@@ -227,6 +228,22 @@ function totalsCells(t: AgentTotals | undefined): Html {
227228 </td>`;
228229 }
229230
231 /** A figure over the last 30 days beside the same figure over all time. */
232 function pair(
233 recent: Scorecard | undefined,
234 all: Scorecard,
235 say: (c: Scorecard) => string | null,
236 ): Html {
237 const part = (label: string, c: Scorecard | undefined) => {
outdated

pair now sits between the scorecard doc comment (above) and scorecardCard, so the doc comment attaches to pair. Move pair above the comment block.

238 const text = c === undefined ? null : say(c);
239 return html`<span class="muted small">${label}</span> ${text ?? "—"}`;
240 };
241 return html`<td class="num nowrap">
242 ${part("30d", recent)}
243 <span class="muted small">·</span> ${part("all", all)}
244 </td>`;
245 }
246
230247 /**
231248 * The scorecard: how each agent's pulls fare once opened — so the owner
232249 * can tune who does what. What they cost is the table above's ("Per
@@ -237,6 +254,7 @@ function totalsCells(t: AgentTotals | undefined): Html {
237254 function scorecardCard(
238255 agents: readonly Agent[],
239256 cards: ReadonlyMap<string, Scorecard>,
outdated

Two labelled figures in one td.num will wrap on a 360px phone and break column alignment. Keep each pair on one line (nowrap) and let the table scroll sideways as it already does.

257 recent: ReadonlyMap<string, Scorecard>,
240258 ): Html {
241259 const dash = html`<td class="num muted">—</td>`;
242260 return card(
@@ -264,18 +282,24 @@ function scorecardCard(
264282 </td>
265283 ${s === undefined
266284 ? html`${dash}${dash}${dash}${dash}${dash}`
267 : html`<td class="num">
268 ${s.landed}
269 <span class="muted small">of ${s.opened}</span>
270 </td>
271 ${s.reviewRounds === null
272 ? dash
273 : html`<td class="num">${s.reviewRounds.toFixed(1)}</td>`}
274 ${s.redCheckRate === null
275 ? dash
276 : html`<td class="num">
277 ${Math.round(100 * s.redCheckRate)}%
278 </td>`}
285 : html`${pair(
286 recent.get(agent.user.id),
287 s,
288 (c) => `${c.landed} of ${c.opened}`,
289 )}
290 ${pair(
291 recent.get(agent.user.id),
292 s,
293 (c) => c.reviewRounds?.toFixed(1) ?? null,
294 )}
295 ${pair(
296 recent.get(agent.user.id),
297 s,
298 (c) =>
299 c.redCheckRate === null
300 ? null
301 : `${Math.round(100 * c.redCheckRate)}%`,
302 )}
279303 <td class="num">${s.reverts}</td>
280304 ${s.meanLandMs === null
281305 ? dash
@@ -291,7 +315,9 @@ function scorecardCard(
291315 head: html`${icon("activity")}
292316 <h2>Scorecard</h2>
293317 <span class="muted small"
294 >Over every pull each agent opened: review rounds are change
318 >Over the pulls each agent opened, the last 30 days (by when a pull
319 was opened) beside all time; reverts and time to land are all time.
320 Review rounds are change
295321 requests per landed pull, red checks the share of finished checks
296322 that failed, reverts the landed pulls a merged pull took back, time
297323 to land the mean from opening to merge.</span
@@ -363,6 +389,7 @@ function crewView(ctx: Ctx, values: AgentValues, error?: string): Html {
363389 const agents = listed.ok ? listed.value : [];
364390 const totals = agentTotals(ctx.app.db);
365391 const scorecards = crewScorecards(ctx.app.db);
392 const recent = recentScorecards(ctx.app.db);
366393 // Where the money goes today: every app running on its own, its spend
367394 // against its ceiling. The ceilings are the one control on the drain, and
368395 // this is the page where the crew is wielded, so they are read here.
@@ -460,7 +487,9 @@ function crewView(ctx: Ctx, values: AgentValues, error?: string): Html {
460487 </div>`,
461488 { pad: false },
462489 )}
outdated

This line is over the formatter's width; run the formatter. Also compute the 30-day cutoff in core (e.g. a recentScorecards(db) helper) so the terminal screen and this page share one definition of 'last 30 days' rather than each building Date.now() - 30 * 86_400_000.

463 ${when(agents.length > 0, () => scorecardCard(agents, scorecards))}
490 ${when(agents.length > 0, () =>
491 scorecardCard(agents, scorecards, recent),
492 )}
464493 ${splitsCard(
465494 "Per model",
466495 "Model",
src/web/style/base.ts +1
@@ -149,6 +149,7 @@ export const utilities = /* css */ `
149149 .muted { color: var(--muted-fg); }
150150 .small { font-size: 13px; }
151151 .num { font-variant-numeric: tabular-nums; }
152 .nowrap { white-space: nowrap; }
152153 .grow { flex: 1; min-width: 0; }
153154 .spacer { flex: 1; }
154155 .truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
test/crew-scorecard.test.ts +52 −4
@@ -166,6 +166,42 @@ describe("a scorecard per crew member", () => {
166166 expect(table).toContain("3h");
167167 });
168168
169 test("thirty days and all time differ when some pulls are old", async () => {
170 const made = await fetch(`${base}/api/v1/agents`, {
171 method: "POST",
172 headers: { cookie, "content-type": "application/json" },
173 body: JSON.stringify({ username: "jones", model: "opus" }),
174 });
175 expect(made.ok).toBe(true);
176 const jones = getUserByName(app.db, "jones")!.id;
177 const day = 86_400_000;
178 const ago = (days: number) => new Date(Date.now() - days * day).toISOString();
179 const oldOne = pull(jones, "Old change", { openedAt: ago(90), mergedAt: ago(89) });
180 pull(jones, "New change", { openedAt: ago(2), mergedAt: ago(1) });
181 pull(jones, "New open change", { openedAt: ago(1) });
182 submitReview(app.db, { issueId: oldOne, authorId: adminId, state: "changes_requested", body: "", commitOid: "b" });
183 check(oldOne, "failed");
184
185 const since = ago(30);
186 const all = crewScorecards(app.db).get(jones)!;
187 const recent = crewScorecards(app.db, { since }).get(jones)!;
188 expect([all.opened, all.landed]).toEqual([3, 2]);
189 expect([recent.opened, recent.landed]).toEqual([2, 1]);
190 expect(all.reviewRounds).toBe(0.5);
191 expect(recent.reviewRounds).toBe(0);
192 expect(all.redCheckRate).toBe(1);
193 expect(recent.redCheckRate).toBeNull();
194 // An agent with nothing in the window is absent from it.
195 expect(crewScorecards(app.db, { since: ago(0) }).has(jones)).toBe(false);
196
197 const page = await fetch(`${base}/settings/crew`, { headers: { cookie } }).then((r) => r.text());
198 const row = page.slice(page.indexOf(`id="crew-scorecard"`));
199 expect(row).toContain("30d");
200 expect(row).toContain(`<td class="num nowrap">`);
201 expect(row).toContain("1 of 2");
202 expect(row).toContain("2 of 3");
203 });
204
169205 /** The forge's terminal, signed in as one account, on a headless screen. */
170206 function terminal(username: string) {
171207 const user = getUserByName(app.db, username)!;
@@ -198,14 +234,26 @@ describe("a scorecard per crew member", () => {
198234 await t.enter();
199235 const text = t.text();
200236 expect(text).toContain("smith");
201 expect(text).toContain("2 landed of 3");
202 expect(text).toContain("review rounds 1.0");
203 expect(text).toContain("red checks 25%");
237 expect(text).toContain("30d 2 of 3 · all 2 of 3");
238 expect(text).toContain("review rounds 30d 1.0 · all 1.0");
239 expect(text).toContain("red checks 30d 25% · all 25%");
204240 expect(text).toContain("reverts 1");
205241 expect(text).toContain("time to land 3h");
206242 t.stop();
207243 });
208244
245 test("the terminal shows the 30 days beside all time where they differ", async () => {
246 const t = terminal("ada");
247 await t.palette("Crew scorecard per agent");
248 await t.enter();
249 const text = t.text();
250 expect(text).toContain("jones");
251 expect(text).toContain("30d 1 of 2 · all 2 of 3");
252 expect(text).toContain("review rounds 30d 0.0 · all 0.5");
253 expect(text).toContain("red checks 30d — · all 100%");
254 t.stop();
255 });
256
209257 test("someone who is not an admin has no scorecard in the terminal", async () => {
210258 const made = await createUser(app.db, {
211259 origin: "seed",
@@ -219,7 +267,7 @@ describe("a scorecard per crew member", () => {
219267 await t.palette("Crew scorecard per agent");
220268 expect(t.text()).not.toContain("review rounds, red checks");
221269 await t.enter();
222 expect(t.text()).not.toContain("2 landed of 3");
270 expect(t.text()).not.toContain("all 2 of 3");
223271 t.stop();
224272 });
225273 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs