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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/15818-week-says-in-one-line-what-the-allowance → main 0 commits fork 1 thread · 1 unresolved

  • Merged. scout/15818-week-says-in-one-line-what-the-allowance was merged into main as 2f3542d.

Review

Sign in to review this change.
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 {
102102 const runs = Math.round((e.runs / e.landings) * 10) / 10;
103103 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).`;
104104 }
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";
99 import { getHeadroom, WINDOW_MS } from "../core/standing.ts";
1010 import { grantedAccounts, PLATFORM } from "../core/subscriptions.ts";
1111 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";
1313
1414 /** The week as the provider's meter reads it: a meter, or null while no account's limit is known. */
1515 interface Week {
@@ -233,3 +233,12 @@ export function weekSpent(app: Reads, repo: Repo, now: Date): boolean {
233233 const week = crewWeekFor(app, repo, now);
234234 return week !== null && week.slots === 0;
235235 }
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 @@
11 import { takeProposal } from "../../../actions/week.ts";
22 import { polishSpend, polishWords } from "../../../crew/polish_share.ts";
3 import { allowanceLine } from "../../../crew/week_read.ts";
34 import { latestRetro, takenOf } from "../../../flow/retro.ts";
45 import { bad, good, type Tool, APP_ARG, planRepo, isAnswer } from "../shared.ts";
56
67 export const tool: Tool = {
78 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",
910 description: "The week's retrospective; `take` accepts one proposal.",
1011 inputSchema: {
1112 type: "object",
@@ -20,7 +21,8 @@ export const tool: Tool = {
2021 const retro = latestRetro(ctx.app.db, repo.id);
2122 const spend = polishSpend(ctx.app.db, repo.id, repo.polishShare, new Date());
2223 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." });
2426 if (args["take"] !== undefined) {
2527 const idx = args["take"];
2628 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 = {
2830 if (!took.ok) return bad(took.error.kind === "not_found" ? "no_such_proposal" : took.error.kind, took.error.message);
2931 }
3032 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) })) });
3234 },
3335 };
src/web/pages/week.ts +2
@@ -5,6 +5,7 @@
55 * and nothing else on the page changes anything.
66 */
77 import { takeProposal } from "../../actions/week.ts";
8 import { allowanceLine } from "../../crew/week_read.ts";
89 import { polishSpend } from "../../crew/polish_share.ts";
910 import { canWrite, type Repo } from "../../core/repos.ts";
1011 import { notePhase, takeSessionTime } from "../../core/route_timing.ts";
@@ -99,6 +100,7 @@ export function weekPage(ctx: Ctx): Response {
99100 ? "The forge writes this when the weekly window closes. None has closed yet."
100101 : `Written when the window ended ${retro.weekEnd.slice(0, 10)}, once, and never edited.`} <a href="${base}/next">Next up</a></p>
101102 </div>
103 <p data-allowance>${allowanceLine(ctx.app, repo)}</p>
102104 ${polishBlock(ctx, repo)}
103105 ${retro === undefined ? null : screen(base, retro, takenOf(ctx.app.db, repo.id, retro.weekEnd), writer)}`;
104106 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";
1818 import type { Repo } from "../src/core/repos.ts";
1919 import { createRun } from "../src/core/runs.ts";
2020 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";
2225 import { createToken } from "../src/core/tokens.ts";
2326 import { recordUsage } from "../src/core/usage.ts";
2427 import { createUser, getUserByName, type User } from "../src/core/users.ts";
@@ -349,4 +352,33 @@ describe("the week's doors", () => {
349352 expect(takenOf(forge.db, repo.id, heard.weekEnd).has(1)).toBe(false);
350353 app.stop();
351354 });
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 });
352384 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs