Public: anyone can read this; the crew can change it.
What the tokens bought, by pillar and by milestone: a limb trimmed on evidence #1040
scout/forge:scout/828-what-the-tokens-bought-by-pillar-and-by → main 2 commits fork
7 files
changed +516
−1
src/core/milestones.ts +66
| @@ -92,6 +92,29 @@ export interface Milestone extends Limb { | ||
| 92 | 92 | readonly title: string; |
| 93 | 93 | readonly at: string; |
| 94 | 94 | }[]; |
| 95 | /** | |
| 96 | * What its limb has cost and landed, for a viewer who may see owner | |
| 97 | * facts: every run beneath it, whatever the date, set against the issues | |
| 98 | * a merged pull closed. | |
| 99 | */ | |
| 100 | readonly cost?: { | |
| 101 | readonly usd: number; | |
| 102 | /** Issues beneath it a merged pull closed, and what they cost. */ | |
| 103 | readonly landed: { readonly issues: number; readonly usd: number }; | |
| 104 | /** Dollars per issue landed; 0 when none has. */ | |
| 105 | readonly perLanded: number; | |
| 106 | /** Spend on issues closed with nothing merged. */ | |
| 107 | readonly droppedUsd: number; | |
| 108 | /** Spend on issues still open. */ | |
| 109 | readonly openUsd: number; | |
| 110 | /** The issues beneath it that cost the most. */ | |
| 111 | readonly costliest: readonly { | |
| 112 | readonly number: number; | |
| 113 | readonly title: string; | |
| 114 | readonly usd: number; | |
| 115 | readonly outcome: "landed" | "dropped" | "open"; | |
| 116 | }[]; | |
| 117 | }; | |
| 95 | 118 | /** A plain sentence: when it lands at this week's pace, or why nobody can say. */ |
| 96 | 119 | readonly estimate: string; |
| 97 | 120 | /** What it waits for directly that has a tree of its own. */ |
| @@ -284,6 +307,20 @@ export function readPlan( | ||
| 284 | 307 | } |
| 285 | 308 | } |
| 286 | 309 | |
| 310 | // Which issues a merged pull closed, ever: what "landed" means for cost. | |
| 311 | const landedIds = new Set<string>(); | |
| 312 | if (options.spend) | |
| 313 | for (const row of db | |
| 314 | .query<{ body: string }, [string]>( | |
| 315 | `SELECT i.body FROM pull p JOIN issue i ON i.id = p.issue_id | |
| 316 | WHERE i.repo_id = ?1 AND p.merged_at IS NOT NULL`, | |
| 317 | ) | |
| 318 | .all(repo.id)) | |
| 319 | for (const number of closesFromBody(row.body)) { | |
| 320 | const id = byNumber.get(number)?.id; | |
| 321 | if (id !== undefined) landedIds.add(id); | |
| 322 | } | |
| 323 | ||
| 287 | 324 | const stateOf = (issue: Issue): LeafState => { |
| 288 | 325 | if (issue.state === "closed") return counted(issue) ? "done" : "gone"; |
| 289 | 326 | if (building.has(issue.id)) return "building"; |
| @@ -517,8 +554,36 @@ export function readPlan( | ||
| 517 | 554 | done: 5, |
| 518 | 555 | gone: 6, |
| 519 | 556 | }; |
| 557 | const sum = (list: readonly Issue[]): number => | |
| 558 | list.reduce((t, i) => t + (cost.get(i.id) ?? 0), 0); | |
| 559 | const cents = (n: number): number => Math.round(n * 100) / 100; | |
| 560 | const outcomeOf = (i: Issue): "landed" | "dropped" | "open" => | |
| 561 | i.state === "open" ? "open" : landedIds.has(i.id) ? "landed" : "dropped"; | |
| 562 | const done = under.filter((i) => outcomeOf(i) === "landed"); | |
| 520 | 563 | return { |
| 521 | 564 | ...limb, |
| 565 | ...(options.spend | |
| 566 | ? { | |
| 567 | cost: { | |
| 568 | usd: cents(sum(under)), | |
| 569 | landed: { issues: done.length, usd: cents(sum(done)) }, | |
| 570 | perLanded: | |
| 571 | done.length === 0 ? 0 : cents(sum(done) / done.length), | |
| 572 | droppedUsd: cents(sum(under.filter((i) => outcomeOf(i) === "dropped"))), | |
| 573 | openUsd: cents(sum(under.filter((i) => i.state === "open"))), | |
| 574 | costliest: under | |
| 575 | .filter((i) => (cost.get(i.id) ?? 0) > 0) | |
| 576 | .sort((a, b) => cost.get(b.id)! - cost.get(a.id)! || a.number - b.number) | |
| 577 | .slice(0, 5) | |
| 578 | .map((i) => ({ | |
| 579 | number: i.number, | |
| 580 | title: i.title, | |
| 581 | usd: cents(cost.get(i.id)!), | |
| 582 | outcome: outcomeOf(i), | |
| 583 | })), | |
| 584 | }, | |
| 585 | } | |
| 586 | : {}), | |
| 522 | 587 | building: under |
| 523 | 588 | .filter((i) => i.state === "open" && building.has(i.id)) |
| 524 | 589 | .map((i) => ({ issue: i, ...building.get(i.id)! })), |
| @@ -594,6 +659,7 @@ export function milestoneJson( | ||
| 594 | 659 | ...limbJson(milestone), |
| 595 | 660 | title: milestone.issue.title, |
| 596 | 661 | estimate: milestone.estimate, |
| 662 | ...(milestone.cost === undefined ? {} : { cost: milestone.cost }), | |
| 597 | 663 | // `building` is the count, as on a limb; this is who is on what. |
| 598 | 664 | buildingNow: milestone.building.map((b) => ({ |
| 599 | 665 | ...brief(b.issue), |
src/core/returns.ts +104
| @@ -15,7 +15,9 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | |
| 17 | 17 | import type { Database } from "bun:sqlite"; |
| 18 | import { NO_PILLAR, pillarNames, pillarsOf, servedBy } from "./pillars.ts"; | |
| 18 | 19 | import { closesFromBody } from "./pulls.ts"; |
| 20 | import { getReposByIds } from "./repos.ts"; | |
| 19 | 21 | |
| 20 | 22 | export interface ReturnsWindow { |
| 21 | 23 | readonly since: string; |
| @@ -42,6 +44,20 @@ export interface CostlyIssue extends Money { | ||
| 42 | 44 | readonly outcome: Outcome; |
| 43 | 45 | } |
| 44 | 46 | |
| 47 | /** The same figures for the work one pillar of the direction stands for. */ | |
| 48 | export interface PillarReturns extends Money { | |
| 49 | /** The pillar's name (`P7 Steering`), `Later`, or `no pillar`. */ | |
| 50 | readonly pillar: string; | |
| 51 | /** Changes that merged for it, and what the runs on them cost. */ | |
| 52 | readonly landed: Money & { readonly changes: number }; | |
| 53 | /** Dollars on work that landed, over the changes that landed; 0 when none did. */ | |
| 54 | readonly perChange: number; | |
| 55 | /** Spend on work closed without a merge. */ | |
| 56 | readonly closed: Money; | |
| 57 | /** Spend on work still open, nothing merged yet. */ | |
| 58 | readonly open: Money; | |
| 59 | } | |
| 60 | ||
| 45 | 61 | export interface Returns { |
| 46 | 62 | readonly window: ReturnsWindow; |
| 47 | 63 | /** Every agent run in the window. */ |
| @@ -72,6 +88,11 @@ export interface Returns { | ||
| 72 | 88 | readonly usd: { readonly median: number; readonly p90: number }; |
| 73 | 89 | readonly minutes: { readonly median: number; readonly p90: number }; |
| 74 | 90 | }; |
| 91 | /** | |
| 92 | * The same by pillar, in the direction's order, `Later` and `no pillar` | |
| 93 | * last; empty where no repository read has pillars. | |
| 94 | */ | |
| 95 | readonly byPillar: readonly PillarReturns[]; | |
| 75 | 96 | /** The issues that cost the most, with what came of each. */ |
| 76 | 97 | readonly costliest: readonly CostlyIssue[]; |
| 77 | 98 | } |
| @@ -223,6 +244,54 @@ export function tokenReturns( | ||
| 223 | 244 | return merged === undefined ? { candidates } : { merged, candidates }; |
| 224 | 245 | }; |
| 225 | 246 | |
| 247 | // What each pillared repository's issues serve; a repository with none is not split. | |
| 248 | const pillared = new Map< | |
| 249 | string, | |
| 250 | { served: Map<string, string>; idOf: Map<number, string>; names: string[] } | |
| 251 | >(); | |
| 252 | for (const [id, repo] of getReposByIds(db, [ | |
| 253 | ...new Set(runs.map((r) => r.repo_id)), | |
| 254 | ])) { | |
| 255 | const set = pillarsOf(db, repo); | |
| 256 | if (set.pillars.length === 0) continue; | |
| 257 | pillared.set(id, { | |
| 258 | served: servedBy(db, set), | |
| 259 | names: pillarNames(set), | |
| 260 | idOf: new Map( | |
| 261 | db | |
| 262 | .query<{ id: string; number: number }, [string]>( | |
| 263 | "SELECT id, number FROM issue WHERE repo_id = ?1 AND kind = 'issue'", | |
| 264 | ) | |
| 265 | .all(id) | |
| 266 | .map((row) => [row.number, row.id]), | |
| 267 | ), | |
| 268 | }); | |
| 269 | } | |
| 270 | /** The pillar a run's work served: its issue's, else the first issue a change of it closes that serves one. */ | |
| 271 | const pillarOf = (r: RunRow, candidates: readonly PullRow[]): string | null => { | |
| 272 | const where = pillared.get(r.repo_id); | |
| 273 | if (where === undefined) return null; | |
| 274 | const issue = r.issue_id === null ? undefined : issues.get(r.issue_id); | |
| 275 | const own = issue?.kind === "issue" ? where.served.get(issue.id) : undefined; | |
| 276 | if (own !== undefined) return own; | |
| 277 | for (const p of candidates) | |
| 278 | for (const k of closesFromBody(p.body)) { | |
| 279 | const id = where.idOf.get(k); | |
| 280 | const found = id === undefined ? undefined : where.served.get(id); | |
| 281 | if (found !== undefined) return found; | |
| 282 | } | |
| 283 | return NO_PILLAR; | |
| 284 | }; | |
| 285 | interface Slot { | |
| 286 | runs: number; | |
| 287 | usd: number; | |
| 288 | landed: { runs: number; usd: number }; | |
| 289 | changes: Set<string>; | |
| 290 | closed: { runs: number; usd: number }; | |
| 291 | open: { runs: number; usd: number }; | |
| 292 | } | |
| 293 | const slots = new Map<string, Slot>(); | |
| 294 | ||
| 226 | 295 | const byState = new Map<string, { runs: number; usd: number }>(); |
| 227 | 296 | const perChange = new Map<string, number>(); |
| 228 | 297 | const nothing = new Map<string, { runs: number; usd: number }>(); |
| @@ -273,6 +342,24 @@ export function tokenReturns( | ||
| 273 | 342 | add(failures, reasonOf(r.summary), usd); |
| 274 | 343 | } else if (r.state === "cancelled") add(nothing, "cancelled", usd); |
| 275 | 344 | } |
| 345 | if (pillared.size > 0) { | |
| 346 | const name = pillarOf(r, change.candidates) ?? NO_PILLAR; | |
| 347 | const slot = slots.get(name) ?? { | |
| 348 | runs: 0, | |
| 349 | usd: 0, | |
| 350 | landed: { runs: 0, usd: 0 }, | |
| 351 | changes: new Set<string>(), | |
| 352 | closed: { runs: 0, usd: 0 }, | |
| 353 | open: { runs: 0, usd: 0 }, | |
| 354 | }; | |
| 355 | slot.runs++; | |
| 356 | slot.usd += usd; | |
| 357 | const part = outcome === "landed" ? slot.landed : slot[outcome]; | |
| 358 | part.runs++; | |
| 359 | part.usd += usd; | |
| 360 | if (change.merged !== undefined) slot.changes.add(change.merged.id); | |
| 361 | slots.set(name, slot); | |
| 362 | } | |
| 276 | 363 | if (issue !== undefined) { |
| 277 | 364 | const m = perIssue.get(issue.id) ?? { runs: 0, usd: 0, outcome }; |
| 278 | 365 | m.runs++; |
| @@ -354,6 +441,23 @@ export function tokenReturns( | ||
| 354 | 441 | ), |
| 355 | 442 | }, |
| 356 | 443 | }, |
| 444 | byPillar: [ | |
| 445 | ...[...pillared.values()].flatMap((p) => p.names), | |
| 446 | NO_PILLAR, | |
| 447 | ] | |
| 448 | .filter((name, i, all) => all.indexOf(name) === i && slots.has(name)) | |
| 449 | .map((name) => { | |
| 450 | const slot = slots.get(name)!; | |
| 451 | return { | |
| 452 | pillar: name, | |
| 453 | ...money(slot), | |
| 454 | landed: { ...money(slot.landed), changes: slot.changes.size }, | |
| 455 | perChange: | |
| 456 | slot.changes.size === 0 ? 0 : cents(slot.landed.usd / slot.changes.size), | |
| 457 | closed: money(slot.closed), | |
| 458 | open: money(slot.open), | |
| 459 | }; | |
| 460 | }), | |
| 357 | 461 | costliest: [...perIssue] |
| 358 | 462 | .sort((a, b) => b[1].usd - a[1].usd) |
| 359 | 463 | .slice(0, TOP_ISSUES) |
src/http/mcp.ts +1 −1
| @@ -1307,7 +1307,7 @@ const TOOLS: readonly Tool[] = [ | ||
| 1307 | 1307 | { |
| 1308 | 1308 | name: "returns", |
| 1309 | 1309 | description: |
| 1310 | "What the crew's tokens bought over the last days (1–90, default 7), so you can ask before you plan: dollars and runs by state; changes landed; dollars on work that landed and the median per landed change; dollars that landed nothing (closed without a merge, or still open), with failed and cancelled runs and the costliest failure reasons; what a good run costs and takes; and the costliest issues with what came of each. Over every app whose owner facts you hold, or `repo` (owner/name). Read-only; an app you cannot write is not found.", | |
| 1310 | "What the crew's tokens bought over the last days (1–90, default 7), so you can ask before you plan: dollars and runs by state; changes landed; dollars on work that landed and the median per landed change; dollars that landed nothing (closed without a merge, or still open), with failed and cancelled runs and the costliest failure reasons; what a good run costs and takes; the same by pillar (`byPillar`: dollars, changes landed, dollars per landed change, closed with nothing, still open); and the costliest issues with what came of each. Over every app whose owner facts you hold, or `repo` (owner/name). Read-only; an app you cannot write is not found.", | |
| 1311 | 1311 | inputSchema: { |
| 1312 | 1312 | type: "object", |
| 1313 | 1313 | properties: { |
src/web/pages/milestones.ts +37
| @@ -203,6 +203,39 @@ function watchButton(base: string, m: Milestone, watching: boolean): Html { | ||
| 203 | 203 | </form>`; |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | const OUTCOME_WORDS = { | |
| 207 | landed: "landed", | |
| 208 | dropped: "closed, nothing merged", | |
| 209 | open: "still open", | |
| 210 | } as const; | |
| 211 | ||
| 212 | /** What the limb has cost and landed, with its costliest issues: owner facts only. */ | |
| 213 | function costSection(base: string, c: NonNullable<Milestone["cost"]>): Html { | |
| 214 | return html`<section class="ms-section" id="ms-cost"> | |
| 215 | <h2>What it has cost</h2> | |
| 216 | <p class="small"> | |
| 217 | <b>${dollars(c.usd)}</b> in all · ${c.landed.issues} | |
| 218 | ${c.landed.issues === 1 ? "issue" : "issues"} landed for | |
| 219 | ${dollars(c.landed.usd)}${c.landed.issues === 0 | |
| 220 | ? null | |
| 221 | : html` (${dollars(c.perLanded)} each)`} · | |
| 222 | ${dollars(c.droppedUsd)} on work closed with nothing merged · ${dollars(c.openUsd)} | |
| 223 | on work still open | |
| 224 | </p> | |
| 225 | <ul class="ms-rows"> | |
| 226 | ${c.costliest.map( | |
| 227 | (i) => | |
| 228 | html`<li> | |
| 229 | <a href="${base}/issues/${i.number}">${i.title}</a> | |
| 230 | <span class="muted small" | |
| 231 | >#${i.number} · ${OUTCOME_WORDS[i.outcome]} · ${dollars(i.usd)}</span | |
| 232 | > | |
| 233 | </li>`, | |
| 234 | )} | |
| 235 | </ul> | |
| 236 | </section>`; | |
| 237 | } | |
| 238 | ||
| 206 | 239 | function milestoneBody( |
| 207 | 240 | repo: Repo, |
| 208 | 241 | m: Milestone, |
| @@ -251,6 +284,10 @@ function milestoneBody( | ||
| 251 | 284 | </p> |
| 252 | 285 | ${actions} |
| 253 | 286 | </div> |
| 287 | ${when( | |
| 288 | m.cost !== undefined && m.cost.usd > 0, | |
| 289 | () => costSection(base, m.cost!), | |
| 290 | )} | |
| 254 | 291 | ${when( |
| 255 | 292 | m.limbs.length > 0, |
| 256 | 293 | () => |
src/web/pages/returns.ts +16
| @@ -85,6 +85,22 @@ export function returnsPage(ctx: Ctx): Response { | ||
| 85 | 85 | ], |
| 86 | 86 | { head: html`<h2>Where it went</h2>` }, |
| 87 | 87 | )} |
| 88 | ${r.byPillar.length === 0 | |
| 89 | ? null | |
| 90 | : listCard( | |
| 91 | r.byPillar.map((p) => | |
| 92 | row({ | |
| 93 | title: p.pillar, | |
| 94 | meta: html`${runs(p)} · ${p.landed.changes} | |
| 95 | ${p.landed.changes === 1 ? "change" : "changes"} landed${p.landed.changes === 0 | |
| 96 | ? "" | |
| 97 | : ` · ${usd(p.perChange)} each`} · ${usd(p.closed.usd)} closed with | |
| 98 | nothing · ${usd(p.open.usd)} still open`, | |
| 99 | trail: html`<b>${usd(p.usd)}</b>`, | |
| 100 | }), | |
| 101 | ), | |
| 102 | { head: html`<h2>By pillar</h2>` }, | |
| 103 | )} | |
| 88 | 104 | ${listCard( |
| 89 | 105 | [ |
| 90 | 106 | line("Runs that failed", nothing.failed), |
test/capacity.test.ts +17
| @@ -96,7 +96,21 @@ describe("capacity: only a forge admin reads or changes it", () => { | ||
| 96 | 96 | }; |
| 97 | 97 | }; |
| 98 | 98 | |
| 99 | // The boot defaults are the environment's: a check runs with | |
| 100 | // FORGE_CHECK_SHARDS set, and these tests start from the unset defaults. | |
| 101 | const KEYS = [ | |
| 102 | "FORGE_CREW_CONCURRENCY", | |
| 103 | "FORGE_CHECKS_CONCURRENCY", | |
| 104 | "FORGE_CHECK_SHARDS", | |
| 105 | "FORGE_CELLS_EGRESS", | |
| 106 | ] as const; | |
| 107 | const saved = new Map<string, string | undefined>(); | |
| 108 | ||
| 99 | 109 | beforeAll(async () => { |
| 110 | for (const key of KEYS) { | |
| 111 | saved.set(key, process.env[key]); | |
| 112 | delete process.env[key]; | |
| 113 | } | |
| 100 | 114 | dataDir = await mkdtemp(join(tmpdir(), "forge-capacity-")); |
| 101 | 115 | app = createApp({ dataDir, port: 0 }); |
| 102 | 116 | servers = await serve(app); |
| @@ -127,6 +141,9 @@ describe("capacity: only a forge admin reads or changes it", () => { | ||
| 127 | 141 | }); |
| 128 | 142 | |
| 129 | 143 | afterAll(async () => { |
| 144 | for (const [key, value] of saved) | |
| 145 | if (value === undefined) delete process.env[key]; | |
| 146 | else process.env[key] = value; | |
| 130 | 147 | servers.stop(); |
| 131 | 148 | app.close(); |
| 132 | 149 | await rm(dataDir, { recursive: true, force: true }); |
test/returns-pillars.test.ts added +275
| @@ -0,0 +1,275 @@ | ||
| 1 | /** | |
| 2 | * What the tokens bought, by pillar and by milestone (#828): a fixture | |
| 3 | * across two pillars and no pillar adds up, a milestone shows what its limb | |
| 4 | * cost, and a reader and a stranger get none of it. | |
| 5 | */ | |
| 6 | ||
| 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 13 | import { openIssue, setIssueState } from "../src/actions/issues.ts"; | |
| 14 | import { openPull } from "../src/actions/pulls.ts"; | |
| 15 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 16 | import { addDependency } from "../src/core/dependencies.ts"; | |
| 17 | import { getIssueById, type Issue } from "../src/core/issues.ts"; | |
| 18 | import type { Repo } from "../src/core/repos.ts"; | |
| 19 | import { tokenReturns } from "../src/core/returns.ts"; | |
| 20 | import { createRun } from "../src/core/runs.ts"; | |
| 21 | import { createToken } from "../src/core/tokens.ts"; | |
| 22 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 23 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 24 | import { quietLogs } from "./helpers/globals.ts"; | |
| 25 | ||
| 26 | quietLogs(); | |
| 27 | ||
| 28 | const PASSWORD = "a-long-pillar-returns-passphrase"; | |
| 29 | ||
| 30 | describe("what the tokens bought, by pillar and milestone", () => { | |
| 31 | let dataDir = ""; | |
| 32 | let app: App; | |
| 33 | let servers: Servers; | |
| 34 | let alice: User; | |
| 35 | let repo: Repo; | |
| 36 | let p1: Issue; | |
| 37 | let aliceCookie = ""; | |
| 38 | let bobCookie = ""; | |
| 39 | let aliceRw = ""; | |
| 40 | let bobRw = ""; | |
| 41 | ||
| 42 | const signIn = async (username: string): Promise<string> => { | |
| 43 | const res = await fetch(`${servers.url}/signin`, { | |
| 44 | method: "POST", | |
| 45 | body: new URLSearchParams({ username, password: PASSWORD }), | |
| 46 | redirect: "manual", | |
| 47 | }); | |
| 48 | return `${(res.headers.get("set-cookie") ?? "").split(";")[0]!}; forge_mode=studio`; | |
| 49 | }; | |
| 50 | ||
| 51 | const get = (path: string, auth: { cookie?: string; token?: string } = {}) => | |
| 52 | fetch(`${servers.url}${path}`, { | |
| 53 | headers: { | |
| 54 | ...(auth.cookie ? { cookie: auth.cookie } : {}), | |
| 55 | ...(auth.token ? { authorization: `Bearer ${auth.token}` } : {}), | |
| 56 | }, | |
| 57 | redirect: "manual", | |
| 58 | }); | |
| 59 | ||
| 60 | const run = ( | |
| 61 | issue: Issue, | |
| 62 | state: "ok" | "failed" | "cancelled", | |
| 63 | usd: number, | |
| 64 | branch = "", | |
| 65 | ): void => { | |
| 66 | const made = createRun(app.db, { | |
| 67 | repoId: repo.id, | |
| 68 | kind: "agent", | |
| 69 | issueId: issue.id, | |
| 70 | branch, | |
| 71 | actorId: getUserByName(app.db, "bot")!.id, | |
| 72 | reason: "assigned", | |
| 73 | }); | |
| 74 | app.db | |
| 75 | .query(`UPDATE run SET state = ?2, cost_usd = ?3, summary = ?2 WHERE id = ?1`) | |
| 76 | .run(made.id, state, usd); | |
| 77 | }; | |
| 78 | ||
| 79 | const open = (title: string): Issue => { | |
| 80 | const opened = openIssue(app, alice, repo, { title }); | |
| 81 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 82 | return opened.value; | |
| 83 | }; | |
| 84 | ||
| 85 | const under = (umbrella: Issue, issue: Issue): void => { | |
| 86 | const r = addDependency(app.db, umbrella, issue, alice.id); | |
| 87 | if (!r.ok) throw new Error(r.error); | |
| 88 | }; | |
| 89 | ||
| 90 | beforeAll(async () => { | |
| 91 | dataDir = await mkdtemp(join(tmpdir(), "forge-returns-pillars-")); | |
| 92 | app = createApp({ dataDir, port: 0 }); | |
| 93 | servers = await serve(app); | |
| 94 | const a = await createUser(app.db, { | |
| 95 | username: "alice", | |
| 96 | email: "[email protected]", | |
| 97 | password: PASSWORD, | |
| 98 | adminIfFirst: true, | |
| 99 | trusted: true, | |
| 100 | }); | |
| 101 | const b = await createUser(app.db, { | |
| 102 | username: "bob", | |
| 103 | email: "[email protected]", | |
| 104 | password: PASSWORD, | |
| 105 | trusted: true, | |
| 106 | }); | |
| 107 | if (!a.ok || !b.ok) throw new Error("users"); | |
| 108 | alice = a.value; | |
| 109 | aliceCookie = await signIn("alice"); | |
| 110 | bobCookie = await signIn("bob"); | |
| 111 | aliceRw = createToken(app.db, alice.id, "rw", "rw").token; | |
| 112 | bobRw = createToken(app.db, b.value.id, "rw", "rw").token; | |
| 113 | const made = await createRepo(app, alice, { name: "mill", visibility: "public" }); | |
| 114 | if (!made.ok) throw new Error(String(made.error)); | |
| 115 | repo = made.value; | |
| 116 | await commitToBranch(app, alice, repo, { | |
| 117 | message: "seed", | |
| 118 | files: [{ path: "README.md", content: "# mill\n" }], | |
| 119 | }); | |
| 120 | await createAgent(app, alice, { username: "bot" }); | |
| 121 | addToCrew(app, alice, repo, "bot"); | |
| 122 | ||
| 123 | p1 = open("P1 Alpha: the first thing"); | |
| 124 | const p2 = open("P2 Beta: the second thing"); | |
| 125 | // P1: one issue landed (a failed try, then a merged change), one closed with nothing. | |
| 126 | const landed = open("Alpha landed"); | |
| 127 | const dropped = open("Alpha dropped"); | |
| 128 | // P2: one issue still open. | |
| 129 | const beta = open("Beta open"); | |
| 130 | // Serves no pillar. | |
| 131 | const loose = open("Loose work"); | |
| 132 | under(p1, landed); | |
| 133 | under(p1, dropped); | |
| 134 | under(p2, beta); | |
| 135 | ||
| 136 | run(landed, "failed", 0.5); | |
| 137 | run(landed, "ok", 1, "bot/landed"); | |
| 138 | await commitToBranch(app, alice, repo, { | |
| 139 | branch: "bot/landed", | |
| 140 | base: "main", | |
| 141 | message: "landed", | |
| 142 | files: [{ path: "landed.md", content: "work\n" }], | |
| 143 | }); | |
| 144 | const opened = await openPull(app, getUserByName(app.db, "bot")!, repo, { | |
| 145 | title: "Alpha landed", | |
| 146 | body: `Closes #${landed.number}`, | |
| 147 | baseBranch: "main", | |
| 148 | headBranch: "bot/landed", | |
| 149 | }); | |
| 150 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 151 | app.db | |
| 152 | .query("UPDATE pull SET merged_at = ?2 WHERE issue_id = ?1") | |
| 153 | .run(opened.value.id, new Date().toISOString()); | |
| 154 | setIssueState(app, alice, repo, getIssueById(app.db, landed.id)!, "closed"); | |
| 155 | run(dropped, "failed", 2); | |
| 156 | setIssueState(app, alice, repo, getIssueById(app.db, dropped.id)!, "closed"); | |
| 157 | run(beta, "cancelled", 0.25); | |
| 158 | run(loose, "ok", 0.75); | |
| 159 | }, 30_000); | |
| 160 | ||
| 161 | afterAll(async () => { | |
| 162 | await servers.stop(); | |
| 163 | await rm(dataDir, { recursive: true, force: true }); | |
| 164 | }); | |
| 165 | ||
| 166 | test("a reader and an anonymous visitor get none of it", async () => { | |
| 167 | expect((await get("/api/v1/returns")).status).toBe(401); | |
| 168 | const bobs = (await (await get("/api/v1/returns", { token: bobRw })).json()) as { | |
| 169 | byPillar: unknown[]; | |
| 170 | }; | |
| 171 | expect(bobs.byPillar).toEqual([]); | |
| 172 | const page = await ( | |
| 173 | await get(`/alice/mill/milestones/${p1.number}`, { cookie: bobCookie }) | |
| 174 | ).text(); | |
| 175 | expect(page).not.toContain("What it has cost"); | |
| 176 | expect(page).not.toMatch(/\$\d/); | |
| 177 | const json = await ( | |
| 178 | await get(`/api/v1/repos/alice/mill/milestones/${p1.number}`, { token: bobRw }) | |
| 179 | ).text(); | |
| 180 | expect(json).not.toContain('"cost"'); | |
| 181 | const anon = await (await get(`/alice/mill/milestones/${p1.number}`)).text(); | |
| 182 | expect(anon).not.toContain("What it has cost"); | |
| 183 | }); | |
| 184 | ||
| 185 | test("the pillars add up to the whole", () => { | |
| 186 | const now = new Date(Date.now() + 1000); | |
| 187 | const r = tokenReturns(app.db, [repo.id], { | |
| 188 | since: new Date(now.getTime() - 7 * 86_400_000).toISOString(), | |
| 189 | until: now.toISOString(), | |
| 190 | }); | |
| 191 | expect(r.byPillar).toEqual([ | |
| 192 | { | |
| 193 | pillar: "P1 Alpha", | |
| 194 | runs: 3, | |
| 195 | usd: 3.5, | |
| 196 | landed: { runs: 2, usd: 1.5, changes: 1 }, | |
| 197 | perChange: 1.5, | |
| 198 | closed: { runs: 1, usd: 2 }, | |
| 199 | open: { runs: 0, usd: 0 }, | |
| 200 | }, | |
| 201 | { | |
| 202 | pillar: "P2 Beta", | |
| 203 | runs: 1, | |
| 204 | usd: 0.25, | |
| 205 | landed: { runs: 0, usd: 0, changes: 0 }, | |
| 206 | perChange: 0, | |
| 207 | closed: { runs: 0, usd: 0 }, | |
| 208 | open: { runs: 1, usd: 0.25 }, | |
| 209 | }, | |
| 210 | { | |
| 211 | pillar: "no pillar", | |
| 212 | runs: 1, | |
| 213 | usd: 0.75, | |
| 214 | landed: { runs: 0, usd: 0, changes: 0 }, | |
| 215 | perChange: 0, | |
| 216 | closed: { runs: 0, usd: 0 }, | |
| 217 | open: { runs: 1, usd: 0.75 }, | |
| 218 | }, | |
| 219 | ]); | |
| 220 | expect(r.byPillar.reduce((t, p) => t + p.usd, 0)).toBeCloseTo(r.spent.usd, 2); | |
| 221 | }); | |
| 222 | ||
| 223 | test("the owner sees it on the page, the API and the MCP", async () => { | |
| 224 | const page = (await (await get("/board/returns", { cookie: aliceCookie })).text()).replace( | |
| 225 | /\s+/g, | |
| 226 | " ", | |
| 227 | ); | |
| 228 | expect(page).toContain("By pillar"); | |
| 229 | expect(page).toContain("P1 Alpha"); | |
| 230 | expect(page).toContain("1 change landed · $1.50 each"); | |
| 231 | const api = (await ( | |
| 232 | await get("/api/v1/returns?repo=alice/mill", { token: aliceRw }) | |
| 233 | ).json()) as { byPillar: { pillar: string; usd: number }[] }; | |
| 234 | expect(api.byPillar.map((p) => [p.pillar, p.usd])).toEqual([ | |
| 235 | ["P1 Alpha", 3.5], | |
| 236 | ["P2 Beta", 0.25], | |
| 237 | ["no pillar", 0.75], | |
| 238 | ]); | |
| 239 | const res = await fetch(`${servers.url}/mcp`, { | |
| 240 | method: "POST", | |
| 241 | headers: { | |
| 242 | "content-type": "application/json", | |
| 243 | accept: "application/json, text/event-stream", | |
| 244 | authorization: `Bearer ${aliceRw}`, | |
| 245 | }, | |
| 246 | body: JSON.stringify({ | |
| 247 | jsonrpc: "2.0", | |
| 248 | id: 1, | |
| 249 | method: "tools/call", | |
| 250 | params: { name: "returns", arguments: {} }, | |
| 251 | }), | |
| 252 | }); | |
| 253 | const tool = (await res.json()) as { result: { content: { text: string }[] } }; | |
| 254 | expect(JSON.parse(tool.result.content[0]!.text).byPillar).toHaveLength(3); | |
| 255 | }); | |
| 256 | ||
| 257 | test("a milestone shows what its limb cost and landed, with its costliest issues", async () => { | |
| 258 | const page = ( | |
| 259 | await (await get(`/alice/mill/milestones/${p1.number}`, { cookie: aliceCookie })).text() | |
| 260 | ).replace(/\s+/g, " "); | |
| 261 | expect(page).toContain("What it has cost"); | |
| 262 | expect(page).toContain("$3.50</b> in all"); | |
| 263 | expect(page).toContain("1 issue landed for $1.50 ($1.50 each)"); | |
| 264 | expect(page).toContain("$2.00 on work closed with nothing merged"); | |
| 265 | expect(page.indexOf("Alpha dropped")).toBeLessThan(page.lastIndexOf("Alpha landed")); | |
| 266 | const api = (await ( | |
| 267 | await get(`/api/v1/repos/alice/mill/milestones/${p1.number}`, { token: aliceRw }) | |
| 268 | ).json()) as { cost: { usd: number; costliest: { title: string; outcome: string }[] } }; | |
| 269 | expect(api.cost.usd).toBe(3.5); | |
| 270 | expect(api.cost.costliest.map((c) => [c.title, c.outcome])).toEqual([ | |
| 271 | ["Alpha dropped", "dropped"], | |
| 272 | ["Alpha landed", "landed"], | |
| 273 | ]); | |
| 274 | }); | |
| 275 | }); |
new above
offline