Public: anyone can read this; the crew can change it.
A crew member's scorecard shows the last 30 days beside all time #3178
scout/forge:scout/3168-a-crew-member-s-scorecard-shows-the-last → main 3 commits fork 3 threads · 3 unresolved
6 files
changed +135
−27
README.md +2
| @@ -27,6 +27,8 @@ claim state and the same apps-running note, never a claim link. | ||
| 27 | 27 | Each agent's scorecard — pulls landed of opened, review rounds, red checks, |
| 28 | 28 | reverts, time to land — is on Settings → Crew (`/settings/crew`) and in the |
| 29 | 29 | 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`. | |
| 30 | 32 | |
| 31 | 33 | Git hosting, code browsing, and a JSON API in one fast Bun process, with |
| 32 | 34 | **zero runtime dependencies** — the `package.json` dependency list is empty and |
src/core/scorecard.ts +22 −4
| @@ -4,7 +4,8 @@ | ||
| 4 | 4 | * says what an agent spent and how many of its pulls landed; this says how |
| 5 | 5 | * hard they were to land and whether they stayed landed. |
| 6 | 6 | * |
| 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: | |
| 8 | 9 | * |
| 9 | 10 | * reviewRounds = count(changes_requested reviews on its landed pulls) ÷ landed |
| 10 | 11 | * redCheckRate = failed ÷ finished (ok + failed) check runs on its pulls |
| @@ -36,7 +37,10 @@ export interface Scorecard { | ||
| 36 | 37 | } |
| 37 | 38 | |
| 38 | 39 | /** 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> { | |
| 40 | 44 | const rows = db |
| 41 | 45 | .query< |
| 42 | 46 | { |
| @@ -49,7 +53,7 @@ export function crewScorecards(db: Database): Map<string, Scorecard> { | ||
| 49 | 53 | reverts: number; |
| 50 | 54 | mean_land_days: number | null; |
| 51 | 55 | }, |
| 52 | [] | |
| 56 | [string | null] | |
| 53 | 57 | >( |
| 54 | 58 | `SELECT issue.author_id AS author_id, |
| 55 | 59 | COUNT(*) AS opened, |
| @@ -74,9 +78,10 @@ export function crewScorecards(db: Database): Map<string, Scorecard> { | ||
| 74 | 78 | FROM issue |
| 75 | 79 | JOIN pull ON pull.issue_id = issue.id |
| 76 | 80 | JOIN user ON user.id = issue.author_id AND user.is_agent = 1 |
| 81 | WHERE (?1 IS NULL OR issue.created_at >= ?1) | |
| 77 | 82 | GROUP BY issue.author_id`, |
| 78 | 83 | ) |
| 79 | .all(); | |
| 84 | .all(opts.since ?? null); | |
| 80 | 85 | return new Map( |
| 81 | 86 | rows.map((r) => { |
| 82 | 87 | const checks = r.checks_ok + r.checks_failed; |
| @@ -98,6 +103,19 @@ export function crewScorecards(db: Database): Map<string, Scorecard> { | ||
| 98 | 103 | ); |
| 99 | 104 | } |
| 100 | 105 | |
| 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 | ||
| 101 | 119 | /** A time to land in the largest unit that keeps it readable: both doors say it the same way. */ |
| 102 | 120 | export function spanText(ms: number): string { |
| 103 | 121 | const minutes = ms / 60_000; |
src/tui/screens/scorecard.ts +15 −5
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | import { listAllAgents } from "../../actions/crew.ts"; |
| 9 | 9 | import { |
| 10 | 10 | crewScorecards, |
| 11 | recentScorecards, | |
| 11 | 12 | spanText, |
| 12 | 13 | type Scorecard, |
| 13 | 14 | } from "../../core/scorecard.ts"; |
| @@ -24,17 +25,22 @@ import { ListState, drawList, type ListRow } from "./common.ts"; | ||
| 24 | 25 | export function scorecardRows( |
| 25 | 26 | agents: readonly Agent[], |
| 26 | 27 | cards: ReadonlyMap<string, Scorecard>, |
| 28 | recent: ReadonlyMap<string, Scorecard> = new Map(), | |
| 27 | 29 | ): ListRow[] { |
| 28 | 30 | return agents.map((agent) => { |
| 29 | 31 | const s = cards.get(agent.user.id); |
| 30 | 32 | if (s === undefined) |
| 31 | 33 | 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) ?? "—"}`; | |
| 32 | 38 | return { |
| 33 | 39 | title: agent.user.username, |
| 34 | secondary: `${s.landed} landed of ${s.opened}`, | |
| 40 | secondary: pair((c) => `${c.landed} of ${c.opened}`), | |
| 35 | 41 | 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)}%`))}`, | |
| 38 | 44 | `reverts ${s.reverts}`, |
| 39 | 45 | `time to land ${s.meanLandMs === null ? "—" : spanText(s.meanLandMs)}`, |
| 40 | 46 | ].join(" "), |
| @@ -48,7 +54,11 @@ export function scorecardScreen(f: Session, ctx: Ctx): Screen { | ||
| 48 | 54 | // Read once when the screen opens, as the page reads it once per request. |
| 49 | 55 | const listed = allowed ? listAllAgents(f.app, f.user) : undefined; |
| 50 | 56 | 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 | ) : []; | |
| 52 | 62 | return { |
| 53 | 63 | title: "Crew scorecard per agent", |
| 54 | 64 | keys: [binding(["j", "k"], "move cursor")], |
| @@ -59,7 +69,7 @@ export function scorecardScreen(f: Session, ctx: Ctx): Screen { | ||
| 59 | 69 | ? "The crew's scorecard is read by a forge admin." |
| 60 | 70 | : rows.length === 0 |
| 61 | 71 | ? "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."; | |
| 63 | 73 | s.text(head!.x, head!.y, truncate(text, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); |
| 64 | 74 | drawList(s, list!, c, state, rows); |
| 65 | 75 | }, |
src/web/pages/crew.ts +43 −14
| @@ -26,6 +26,7 @@ import { | ||
| 26 | 26 | } from "../../core/runs.ts"; |
| 27 | 27 | import { |
| 28 | 28 | crewScorecards, |
| 29 | recentScorecards, | |
| 29 | 30 | spanText, |
| 30 | 31 | type Scorecard, |
| 31 | 32 | } from "../../core/scorecard.ts"; |
| @@ -227,6 +228,22 @@ function totalsCells(t: AgentTotals | undefined): Html { | ||
| 227 | 228 | </td>`; |
| 228 | 229 | } |
| 229 | 230 | |
| 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) => { | |
| 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 | ||
| 230 | 247 | /** |
| 231 | 248 | * The scorecard: how each agent's pulls fare once opened — so the owner |
| 232 | 249 | * can tune who does what. What they cost is the table above's ("Per |
| @@ -237,6 +254,7 @@ function totalsCells(t: AgentTotals | undefined): Html { | ||
| 237 | 254 | function scorecardCard( |
| 238 | 255 | agents: readonly Agent[], |
| 239 | 256 | cards: ReadonlyMap<string, Scorecard>, |
outdated
Two labelled figures in one |
||
| 257 | recent: ReadonlyMap<string, Scorecard>, | |
| 240 | 258 | ): Html { |
| 241 | 259 | const dash = html`<td class="num muted">—</td>`; |
| 242 | 260 | return card( |
| @@ -264,18 +282,24 @@ function scorecardCard( | ||
| 264 | 282 | </td> |
| 265 | 283 | ${s === undefined |
| 266 | 284 | ? 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 | )} | |
| 279 | 303 | <td class="num">${s.reverts}</td> |
| 280 | 304 | ${s.meanLandMs === null |
| 281 | 305 | ? dash |
| @@ -291,7 +315,9 @@ function scorecardCard( | ||
| 291 | 315 | head: html`${icon("activity")} |
| 292 | 316 | <h2>Scorecard</h2> |
| 293 | 317 | <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 | |
| 295 | 321 | requests per landed pull, red checks the share of finished checks |
| 296 | 322 | that failed, reverts the landed pulls a merged pull took back, time |
| 297 | 323 | to land the mean from opening to merge.</span |
| @@ -363,6 +389,7 @@ function crewView(ctx: Ctx, values: AgentValues, error?: string): Html { | ||
| 363 | 389 | const agents = listed.ok ? listed.value : []; |
| 364 | 390 | const totals = agentTotals(ctx.app.db); |
| 365 | 391 | const scorecards = crewScorecards(ctx.app.db); |
| 392 | const recent = recentScorecards(ctx.app.db); | |
| 366 | 393 | // Where the money goes today: every app running on its own, its spend |
| 367 | 394 | // against its ceiling. The ceilings are the one control on the drain, and |
| 368 | 395 | // 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 { | ||
| 460 | 487 | </div>`, |
| 461 | 488 | { pad: false }, |
| 462 | 489 | )} |
outdated
This line is over the formatter's width; run the formatter. Also compute the 30-day cutoff in core (e.g. a |
||
| 463 | ${when(agents.length > 0, () => scorecardCard(agents, scorecards))} | |
| 490 | ${when(agents.length > 0, () => | |
| 491 | scorecardCard(agents, scorecards, recent), | |
| 492 | )} | |
| 464 | 493 | ${splitsCard( |
| 465 | 494 | "Per model", |
| 466 | 495 | "Model", |
src/web/style/base.ts +1
| @@ -149,6 +149,7 @@ export const utilities = /* css */ ` | ||
| 149 | 149 | .muted { color: var(--muted-fg); } |
| 150 | 150 | .small { font-size: 13px; } |
| 151 | 151 | .num { font-variant-numeric: tabular-nums; } |
| 152 | .nowrap { white-space: nowrap; } | |
| 152 | 153 | .grow { flex: 1; min-width: 0; } |
| 153 | 154 | .spacer { flex: 1; } |
| 154 | 155 | .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", () => { | ||
| 166 | 166 | expect(table).toContain("3h"); |
| 167 | 167 | }); |
| 168 | 168 | |
| 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 | ||
| 169 | 205 | /** The forge's terminal, signed in as one account, on a headless screen. */ |
| 170 | 206 | function terminal(username: string) { |
| 171 | 207 | const user = getUserByName(app.db, username)!; |
| @@ -198,14 +234,26 @@ describe("a scorecard per crew member", () => { | ||
| 198 | 234 | await t.enter(); |
| 199 | 235 | const text = t.text(); |
| 200 | 236 | 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%"); | |
| 204 | 240 | expect(text).toContain("reverts 1"); |
| 205 | 241 | expect(text).toContain("time to land 3h"); |
| 206 | 242 | t.stop(); |
| 207 | 243 | }); |
| 208 | 244 | |
| 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 | ||
| 209 | 257 | test("someone who is not an admin has no scorecard in the terminal", async () => { |
| 210 | 258 | const made = await createUser(app.db, { |
| 211 | 259 | origin: "seed", |
| @@ -219,7 +267,7 @@ describe("a scorecard per crew member", () => { | ||
| 219 | 267 | await t.palette("Crew scorecard per agent"); |
| 220 | 268 | expect(t.text()).not.toContain("review rounds, red checks"); |
| 221 | 269 | await t.enter(); |
| 222 | expect(t.text()).not.toContain("2 landed of 3"); | |
| 270 | expect(t.text()).not.toContain("all 2 of 3"); | |
| 223 | 271 | t.stop(); |
| 224 | 272 | }); |
| 225 | 273 | }); |
new above
offline
pairnow sits between the scorecard doc comment (above) andscorecardCard, so the doc comment attaches topair. Movepairabove the comment block.