Public: anyone can read this; the crew can change it.
/week says in one line what the allowance has bought so far and what the rest is expected to buy #15825
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15818-week-says-in-one-line-what-the-allowance → main 0 commits fork 1 thread · 1 unresolved
6 files
changed +75
−5
docs/features/week-allowance-line.md added +11
| @@ -0,0 +1,11 @@ | ||
| 1 | --- | |
| 2 | topic: Tell the crew | |
| 3 | row: What the allowance bought | |
| 4 | --- | |
| 5 | # What the allowance has bought (#15818) | |
| 6 | ||
| 7 | One line under the heading of `/<owner>/<repo>/week` says what the weekly allowance has bought and what the rest should buy at that pace, for example "23 landed for 41% so far; the rest buys about 33 at this pace." | |
| 8 | ||
| 9 | - **Where**: the page, the terminal's "The week" screen (its own line from `src/actions/usage.ts`, #15876), and the `allowance` field of the MCP `week` tool. | |
| 10 | - **Unmeasured or no landings**: the line says so in words, never 0 or NaN. | |
| 11 | - Words come from `allowanceWords` (`src/crew/week.ts`); the figures from `allowanceLine` (`src/crew/week_read.ts`). Tested in `test/week-doors.test.ts`. |
src/crew/week.ts +14
| @@ -102,3 +102,17 @@ export function efficiencyWords(e: Efficiency): string { | ||
| 102 | 102 | const runs = Math.round((e.runs / e.landings) * 10) / 10; |
| 103 | 103 | return `Each landing cost ${usd(e.costUsd / e.landings)} and ${runs} runs this week (${e.landings} landed, pulls closed as already on the base not counted).`; |
| 104 | 104 | } |
| 105 | ||
| 106 | /** | |
| 107 | * What the weekly allowance has bought so far and what the rest is expected | |
| 108 | * to buy at that pace, in one line. `usedPct` is the provider's percent of | |
| 109 | * the week used (null while unmeasured); plain words, never 0 or NaN. | |
| 110 | */ | |
| 111 | export function allowanceWords(landed: number, usedPct: number | null): string { | |
| 112 | if (usedPct === null || !Number.isFinite(usedPct)) return "The weekly allowance is not measured yet, so nothing can be said of what it buys."; | |
| 113 | const used = Math.round(usedPct); | |
| 114 | if (landed <= 0) return `Nothing has landed this week yet (${used}% of the allowance used), so there is no pace to project.`; | |
| 115 | if (usedPct <= 0) return `${landed} landed and none of the allowance is counted as used, so there is no pace to project.`; | |
| 116 | const rest = Math.max(0, Math.round((landed / usedPct) * (100 - usedPct))); | |
| 117 | return `${landed} landed for ${used}% so far; the rest buys about ${rest} at this pace.`; | |
| 118 | } |
src/crew/week_read.ts +10 −1
| @@ -9,7 +9,7 @@ import { measureFor } from "../core/measure.ts"; | ||
| 9 | 9 | import { getHeadroom, WINDOW_MS } from "../core/standing.ts"; |
| 10 | 10 | import { grantedAccounts, PLATFORM } from "../core/subscriptions.ts"; |
| 11 | 11 | import { belowCeiling, capacityCount, capacityValue, weekUse } from "../core/capacity.ts"; |
| 12 | import { efficiencyWords, weekDecision, type Efficiency, type WeekDecision, type WeekMeter } from "./week.ts"; | |
| 12 | import { allowanceWords, efficiencyWords, weekDecision, type Efficiency, type WeekDecision, type WeekMeter } from "./week.ts"; | |
| 13 | 13 | |
| 14 | 14 | /** The week as the provider's meter reads it: a meter, or null while no account's limit is known. */ |
| 15 | 15 | interface Week { |
| @@ -233,3 +233,12 @@ export function weekSpent(app: Reads, repo: Repo, now: Date): boolean { | ||
| 233 | 233 | const week = crewWeekFor(app, repo, now); |
| 234 | 234 | return week !== null && week.slots === 0; |
| 235 | 235 | } |
| 236 | ||
| 237 | /** | |
| 238 | * The allowance line for one repository: landings this window per point of the week used, and what | |
| 239 | * the rest should buy. | |
| 240 | */ | |
| 241 | export function allowanceLine(app: App, repo: Repo, now: Date = new Date()): string { | |
| 242 | const week = weekFor(app, repo, now); | |
| 243 | return allowanceWords(weekEfficiency(app, repo, now).landings, week === null ? null : week.usedPct); | |
| 244 | } |
src/http/mcp/tools/week.ts +5 −3
| @@ -1,11 +1,12 @@ | ||
| 1 | 1 | import { takeProposal } from "../../../actions/week.ts"; |
| 2 | 2 | import { polishSpend, polishWords } from "../../../crew/polish_share.ts"; |
| 3 | import { allowanceLine } from "../../../crew/week_read.ts"; | |
| 3 | 4 | import { latestRetro, takenOf } from "../../../flow/retro.ts"; |
| 4 | 5 | import { bad, good, type Tool, APP_ARG, planRepo, isAnswer } from "../shared.ts"; |
| 5 | 6 | |
| 6 | 7 | export const tool: Tool = { |
| 7 | 8 | name: "week", |
| 8 | doc: "an app's last weekly retrospective, the same page as /:owner/:name/week (`app`): `{ polish: { share, percent, polishUsd, totalUsd, under }, weekEnd, shipped, projected, accuracy, costly, stuck, proposals: [{ number, words, taken }] }`; with `take` (a proposal's index, 0 to 2) a writer who is a person (never an agent) takes that one proposal, which puts its issue behind the Later gate; nothing else ever changes", | |
| 9 | doc: "an app's last weekly retrospective, the same page as /:owner/:name/week (`app`): `{ allowance, polish: { share, percent, polishUsd, totalUsd, under }, weekEnd, shipped, projected, accuracy, costly, stuck, proposals: [{ number, words, taken }] }`; with `take` (a proposal's index, 0 to 2) a writer who is a person (never an agent) takes that one proposal, which puts its issue behind the Later gate; nothing else ever changes", | |
| 9 | 10 | description: "The week's retrospective; `take` accepts one proposal.", |
| 10 | 11 | inputSchema: { |
| 11 | 12 | type: "object", |
| @@ -20,7 +21,8 @@ export const tool: Tool = { | ||
| 20 | 21 | const retro = latestRetro(ctx.app.db, repo.id); |
| 21 | 22 | const spend = polishSpend(ctx.app.db, repo.id, repo.polishShare, new Date()); |
| 22 | 23 | const polish = { share: spend.share, percent: Math.round(spend.percent * 10) / 10, polishUsd: spend.polishUsd, totalUsd: spend.totalUsd, under: spend.under, says: polishWords(spend) }; |
| 23 | if (retro === undefined) return good({ polish, says: "No weekly window has closed yet." }); | |
| 24 | const allowance = allowanceLine(ctx.app, repo); | |
| 25 | if (retro === undefined) return good({ polish, allowance, says: "No weekly window has closed yet." }); | |
| 24 | 26 | if (args["take"] !== undefined) { |
| 25 | 27 | const idx = args["take"]; |
| 26 | 28 | if (typeof idx !== "number" || !Number.isInteger(idx) || idx < 0 || idx > 2) return bad("invalid_take", "take is a proposal's index, 0 to 2"); |
| @@ -28,6 +30,6 @@ export const tool: Tool = { | ||
| 28 | 30 | if (!took.ok) return bad(took.error.kind === "not_found" ? "no_such_proposal" : took.error.kind, took.error.message); |
| 29 | 31 | } |
| 30 | 32 | const taken = takenOf(ctx.app.db, repo.id, retro.weekEnd); |
| 31 | return good({ ...retro, polish, proposals: retro.proposals.map((p, i) => ({ number: p.number, words: p.words, taken: taken.has(i) })) }); | |
| 33 | return good({ ...retro, polish, allowance, proposals: retro.proposals.map((p, i) => ({ number: p.number, words: p.words, taken: taken.has(i) })) }); | |
| 32 | 34 | }, |
| 33 | 35 | }; |
src/web/pages/week.ts +2
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | * and nothing else on the page changes anything. |
| 6 | 6 | */ |
| 7 | 7 | import { takeProposal } from "../../actions/week.ts"; |
| 8 | import { allowanceLine } from "../../crew/week_read.ts"; | |
| 8 | 9 | import { polishSpend } from "../../crew/polish_share.ts"; |
| 9 | 10 | import { canWrite, type Repo } from "../../core/repos.ts"; |
| 10 | 11 | import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; |
| @@ -99,6 +100,7 @@ export function weekPage(ctx: Ctx): Response { | ||
| 99 | 100 | ? "The forge writes this when the weekly window closes. None has closed yet." |
| 100 | 101 | : `Written when the window ended ${retro.weekEnd.slice(0, 10)}, once, and never edited.`} <a href="${base}/next">Next up</a></p> |
| 101 | 102 | </div> |
| 103 | <p data-allowance>${allowanceLine(ctx.app, repo)}</p> | |
| 102 | 104 | ${polishBlock(ctx, repo)} |
| 103 | 105 | ${retro === undefined ? null : screen(base, retro, takenOf(ctx.app.db, repo.id, retro.weekEnd), writer)}`; |
| 104 | 106 | const response = page({ ctx, title: `The week · ${repo.name}`, crumbs: crumbsFor(repo), palette: repoPalette(ctx, repo), body }); |
test/week-doors.test.ts +33 −1
| @@ -18,7 +18,10 @@ import { nextUp } from "../src/core/next_up.ts"; | ||
| 18 | 18 | import type { Repo } from "../src/core/repos.ts"; |
| 19 | 19 | import { createRun } from "../src/core/runs.ts"; |
| 20 | 20 | import { noteLimit } from "../src/core/standing.ts"; |
| 21 | import { addSubscription } from "../src/core/subscriptions.ts"; | |
| 21 | import { addSubscription, PLATFORM } from "../src/core/subscriptions.ts"; | |
| 22 | import { noteMeasure } from "../src/core/measure.ts"; | |
| 23 | import { forgetWeek } from "../src/crew/week_read.ts"; | |
| 24 | import { allowanceWords } from "../src/crew/week.ts"; | |
| 22 | 25 | import { createToken } from "../src/core/tokens.ts"; |
| 23 | 26 | import { recordUsage } from "../src/core/usage.ts"; |
| 24 | 27 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; |
| @@ -349,4 +352,33 @@ describe("the week's doors", () => { | ||
| 349 | 352 | expect(takenOf(forge.db, repo.id, heard.weekEnd).has(1)).toBe(false); |
| 350 | 353 | app.stop(); |
| 351 | 354 | }); |
| 355 | ||
| 356 | test("the allowance line: unmeasured says so; landings and usage give the pace, on the page and in the tool", async () => { | |
| 357 | const line = async () => { | |
| 358 | const page = await (await fetch(`${base}/alice/plan/week`, { headers: { cookie: aliceCookie } })).text(); | |
| 359 | const onPage = /<p data-allowance>([^<]*)<\/p>/.exec(page)?.[1]; | |
| 360 | const viaTool = (await mcp(aliceTok, { app: "alice/plan" })).value.allowance; | |
| 361 | expect(onPage).toBe(viaTool); | |
| 362 | return viaTool as string; | |
| 363 | }; | |
| 364 | forgetWeek(); | |
| 365 | expect(await line()).toContain("not measured"); | |
| 366 | const now = new Date(); | |
| 367 | noteMeasure(forge.db, PLATFORM, { window: "week", usedPct: 40.6, resetsAt: new Date(now.getTime() + 3 * day).toISOString(), observedAt: now.toISOString() }); | |
| 368 | forgetWeek(); | |
| 369 | expect(await line()).toBe("Nothing has landed this week yet (41% of the allowance used), so there is no pace to project."); | |
| 370 | for (let k = 0; k < 4; k++) { | |
| 371 | const issueId = `allow${k}`; | |
| 372 | forge.db.query(`INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state, created_at) VALUES (?1, ?2, ?3, 'pull', 'P', '', ?4, 'open', ?5)`).run(issueId, repo.id, 900 + k, alice.id, now.toISOString()); | |
| 373 | forge.db.query(`INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id, merged_at, merged_oid, carried) VALUES (?1, ?2, ?3, 'base', '', ?4, ?5, ?6, 0)`).run(issueId, repo.defaultBranch, `allow/${k}`, repo.id, now.toISOString(), `new${k}`); | |
| 374 | } | |
| 375 | forgetWeek(); | |
| 376 | expect(await line()).toBe("4 landed for 41% so far; the rest buys about 6 at this pace."); | |
| 377 | }); | |
| 378 | ||
| 379 | test("allowanceWords never prints 0 or NaN for a missing pace", () => { | |
| 380 | expect(allowanceWords(23, 41)).toBe("23 landed for 41% so far; the rest buys about 33 at this pace."); | |
| 381 | for (const w of [allowanceWords(0, 30), allowanceWords(3, 0), allowanceWords(3, null), allowanceWords(3, Number.NaN)]) expect(w).not.toMatch(/NaN|about 0/); | |
| 382 | expect(allowanceWords(5, 100)).toContain("about 0"); | |
| 383 | }); | |
| 352 | 384 | }); |
new above
offline