Public: anyone can read this; the crew can change it.
The terminal's week screen shows the same allowance line as /week #15876
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15853-the-terminal-s-week-screen-shows-the-sam → main 0 commits fork 7 threads · 7 unresolved
docs/features/week-pace.md +4
| @@ -16,3 +16,7 @@ The settings are the plain values in `WEEK` (`src/crew/week.ts`); the decision i | ||
| 16 | 16 | ## Bets (#8713) |
| 17 | 17 | |
| 18 | 18 | Bets are not in the forge yet, so the release above falls back to the lowest-numbered gate-held issue. When bets land, "behind" releases the next scope of a backed bet, or offers the next bet to back, in place of that fallback; the change is the one candidate read in `releaseForPace` (`src/crew/autonomy.ts`). Appetites then sit inside the week's capacity the decision already works out (`capUsd` less the reserve), so no bet's share can exceed it. |
| 19 | ||
| 20 | ## In the terminal (#15853) | |
| 21 | ||
| 22 | The terminal's week screen ends its facts with the allowance line: pulls landed in the last seven days, the percent of the weekly allowance used, and what the rest buys at that pace. The words come from `allowanceLine` in `src/actions/usage.ts`, written there for the `/week` page and MCP tool of #15818 to import; today the terminal is its only caller. The percent is the crew's own, `crewWeekFor(...).usedPct` (the figure the handover reports), so no surface computes a second one. Spend is the owner's fact: `allowanceLine` returns null for anyone `canSeeOwnerFacts` refuses, so a guest on a public repo sees the retrospective without the line, whichever surface asks. Tested in `test/tui-week-line.test.ts`. | |
src/actions/usage.ts +34
| @@ -13,6 +13,7 @@ | ||
| 13 | 13 | |
| 14 | 14 | import type { App } from "../app.ts"; |
| 15 | 15 | import { getIssuesByIds } from "../core/issues.ts"; |
| 16 | import { mergedPullCount } from "../core/pulls.ts"; | |
| 16 | 17 | import { |
| 17 | 18 | canSeeOwnerFacts, |
| 18 | 19 | listReposVisibleTo, |
| @@ -31,6 +32,7 @@ import { | ||
| 31 | 32 | } from "../core/subscriptions.ts"; |
| 32 | 33 | import { readSpend, type Spend } from "./spend.ts"; |
| 33 | 34 | import { guardCounts, type GuardCounts } from "../crew/guard.ts"; |
| 35 | import { crewWeekFor } from "../crew/week_read.ts"; | |
| 34 | 36 | import { |
| 35 | 37 | recentUsage, |
| 36 | 38 | usageTotals, |
| @@ -299,3 +301,35 @@ export function readForgeUsage( | ||
| 299 | 301 | subscriptions, |
| 300 | 302 | }; |
| 301 | 303 | } |
| 304 | ||
| 305 | /** | |
| 306 | * The allowance line (#15853), written here for the `/week` page and the MCP | |
outdated
Second definition of the week's used percent; reuse |
||
| 307 | * tool of #15818 to import; the terminal's week screen is its one caller | |
| 308 | * today. Pulls landed in the last seven days per point of the weekly | |
| 309 | * allowance used, and what the rest buys at that pace. The percent is the | |
| 310 | * crew's own (`crewWeekFor(...).usedPct`, what the handover reports), so no | |
| 311 | * surface computes a second one. Spend is the owner's fact: null for anyone | |
| 312 | * `canSeeOwnerFacts` refuses, so no caller can leak it. | |
| 313 | */ | |
| 314 | export function allowanceLine( | |
| 315 | app: Pick<App, "db">, | |
| 316 | repo: Repo, | |
| 317 | user: User | undefined, | |
| 318 | now: Date, | |
| 319 | ): string | null { | |
| 320 | if (!canSeeOwnerFacts(app.db, repo, user)) return null; | |
| 321 | const week = crewWeekFor(app, repo, now); | |
| 322 | if (week === null) return "The weekly allowance is not known yet, so there is no pace to quote."; | |
| 323 | const used = week.usedPct; | |
| 324 | const since = new Date(now.getTime() - 7 * 86_400_000).toISOString(); | |
| 325 | const landed = mergedPullCount(app.db, repo.id, since); | |
outdated
JSDoc claims page and MCP tool share this; neither does yet. Also gate by user here (return null without canSeeOwnerFacts) so callers cannot leak the account-wide percent. |
||
| 326 | if (landed === 0) { | |
| 327 | const of = `${used}% of the allowance`; | |
| 328 | return `Nothing has landed this week for ${of}, so there is no pace to quote.`; | |
| 329 | } | |
outdated
Reads all forge credentials with no check on who is asking. Gate by canSeeOwnerFacts at the caller or pass the user in; otherwise a guest sees account-wide usage. |
||
| 330 | if (used < 1) { | |
| 331 | return `${landed} landed this week; too little of the allowance is used to quote a pace.`; | |
| 332 | } | |
| 333 | const rest = Math.round((landed / used) * Math.max(0, 100 - used)); | |
| 334 | return `${landed} landed for ${used}% so far; the rest buys about ${rest} at this pace.`; | |
| 335 | } |
src/tui/screens/week.ts +3
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | * updates the screen here. |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | import { allowanceLine } from "../../actions/usage.ts"; | |
| 12 | 13 | import { takeProposal } from "../../actions/week.ts"; |
| 13 | 14 | import { canRead, canWrite, getRepoById, type Repo } from "../../core/repos.ts"; |
| 14 | 15 | import { polishSpend, polishWords } from "../../crew/polish_share.ts"; |
| @@ -103,6 +104,8 @@ export function weekScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 103 | 104 | ? ["The forge writes this when the weekly window closes. None has closed yet."] |
| 104 | 105 | : summaryLines(retro)), |
| 105 | 106 | polishLine(f, repo), |
| 107 | // Null for a reader who may not see the owner's spend (`allowanceLine` decides). | |
outdated
Add this line only when canSeeOwnerFacts(f.db, repo, f.user). Currently any reader gets it. |
||
| 108 | ...[allowanceLine(f, repo, f.user, new Date())].filter((l) => l !== null), | |
Non-blocking: the spread-and-filter works but is a roundabout way to add one optional line. Compute |
||
| 106 | 109 | ]; |
| 107 | 110 | const [head, list] = split(area, "v", [len(lines.length + 1), fill(1)]); |
| 108 | 111 | lines.forEach((line, i) => |
test/explore.test.ts +5 −17
| @@ -24,6 +24,7 @@ import { proveAll } from "./helpers/verified.ts"; | ||
| 24 | 24 | import { promote } from "./helpers/members.ts"; |
| 25 | 25 | import { exploreParams, readExploreLine } from "../src/actions/explore.ts"; |
| 26 | 26 | import { seenFetch } from "./helpers/explore_seen.ts"; |
| 27 | import { landPulls } from "./helpers/tui-text.ts"; | |
| 27 | 28 | /** The href the page gives a line: separate parameters, `&` escaped as in markup. */ |
| 28 | 29 | const linkOf = (line: string): string => `/explore?${exploreParams(readExploreLine(line).query)}`.replace(/&/g, "&"); |
| 29 | 30 | |
| @@ -124,23 +125,10 @@ describe("explore", () => { | ||
| 124 | 125 | if (!record.ok) throw new Error(record.error); |
| 125 | 126 | return made.value; |
| 126 | 127 | }; |
| 127 | const merged = (repo: Repo, author: User, n: number) => { | |
| 128 | for (let i = 0; i < n; i++) { | |
| 129 | const made = createIssue(app.db, { | |
| 130 | repoId: repo.id, | |
| 131 | authorId: author.id, | |
| 132 | title: `change ${i}`, | |
| 133 | kind: "pull", | |
| 134 | }); | |
| 135 | if (!made.ok) throw new Error(made.error); | |
| 136 | app.db | |
| 137 | .query( | |
| 138 | `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at) | |
| 139 | VALUES (?1, 'main', ?2, '', '', ?3)`, | |
| 140 | ) | |
| 141 | .run(made.value.id, `b${i}`, new Date(Date.now() - (i + 1) * 3_600_000).toISOString()); | |
| 142 | } | |
| 143 | }; | |
| 128 | const merged = (repo: Repo, author: User, n: number) => | |
| 129 | landPulls(app.db, repo.id, author.id, n, (i) => | |
| 130 | new Date(Date.now() - (i + 1) * 3_600_000).toISOString(), | |
| 131 | ); | |
| 144 | 132 | const forkOf = async (parent: Repo, owner: User, name: string) => { |
| 145 | 133 | const made = await createRepo(app, owner, { name, visibility: "public" }); |
| 146 | 134 | if (!made.ok) throw new Error(String(made.error)); |
test/helpers/tui-text.ts added +57
| @@ -0,0 +1,57 @@ | ||
| 1 | /** | |
| 2 | * Test helpers for the terminal's screens: a context that does nothing, a | |
| 3 | * session for one user, and a screen drawn to text; and pulls landed on a | |
| 4 | * repository at a time, for the counts the week and explore read. | |
| 5 | */ | |
| 6 | import type { Database } from "bun:sqlite"; | |
| 7 | import type { App } from "../../src/app.ts"; | |
| 8 | import { createIssue } from "../../src/core/issues.ts"; | |
| 9 | import type { User } from "../../src/core/users.ts"; | |
| 10 | import type { Ctx, Screen } from "../../src/tui/app/app.ts"; | |
| 11 | import type { Session } from "../../src/tui/session.ts"; | |
| 12 | import { Screen as Surface } from "../../src/tui/term/screen.ts"; | |
| 13 | import { theme } from "../../src/tui/ui/theme.ts"; | |
| 14 | ||
| 15 | /** A context whose effects are no-ops, for rendering a screen once. */ | |
| 16 | export function quietCtx(): Ctx { | |
| 17 | const ctx = { | |
| 18 | theme: theme("dark"), | |
| 19 | invalidate() {}, | |
| 20 | toast() {}, | |
| 21 | push() {}, | |
| 22 | pop() {}, | |
| 23 | copy() {}, | |
| 24 | hit() {}, | |
| 25 | }; | |
| 26 | return ctx as unknown as Ctx; | |
| 27 | } | |
| 28 | ||
| 29 | /** A terminal session for `user` over `app`. */ | |
| 30 | export function sessionFor(app: App, user: User): Session { | |
| 31 | return { app, db: app.db, user } as unknown as Session; | |
| 32 | } | |
| 33 | ||
| 34 | /** `screen` drawn once on a `w` by `h` surface, as text. */ | |
| 35 | export function screenText(screen: Screen, w: number, h: number): string { | |
| 36 | const s = new Surface(w, h); | |
| 37 | screen.render({ x: 0, y: 0, w, h }, s); | |
| 38 | return s.toText(); | |
| 39 | } | |
| 40 | ||
| 41 | /** `n` pulls by `authorId` on `repoId`, merged at `at(i)`. */ | |
| 42 | export function landPulls( | |
| 43 | db: Database, | |
| 44 | repoId: string, | |
| 45 | authorId: string, | |
| 46 | n: number, | |
| 47 | at: (i: number) => string, | |
| 48 | ): void { | |
| 49 | for (let i = 0; i < n; i++) { | |
| 50 | const made = createIssue(db, { repoId, authorId, title: `change ${i}`, kind: "pull" }); | |
| 51 | if (!made.ok) throw new Error(String(made.error)); | |
| 52 | db.query( | |
| 53 | `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at) | |
| 54 | VALUES (?1, 'main', ?2, '', '', ?3)`, | |
| 55 | ).run(made.value.id, `b${i}`, at(i)); | |
| 56 | } | |
| 57 | } |
test/tui-week-line.test.ts added +120
| @@ -0,0 +1,120 @@ | ||
| 1 | /** | |
| 2 | * The terminal's week screen shows the allowance line (#15853): one function, | |
| 3 | * `allowanceLine`, over the crew's own week percent (`crewWeekFor`), for the | |
| 4 | * known and unknown cases, and only to those who may see the owner's facts. | |
| 5 | * Each test builds its own forge. | |
| 6 | */ | |
| 7 | import { afterEach, beforeEach, 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 { allowanceLine } from "../src/actions/usage.ts"; | |
| 13 | import { noteMeasure } from "../src/core/measure.ts"; | |
| 14 | import { createRepo, getRepoById, type Repo } from "../src/core/repos.ts"; | |
| 15 | import { addSubscription, setGrants } from "../src/core/subscriptions.ts"; | |
| 16 | import { createUser, type User } from "../src/core/users.ts"; | |
| 17 | import { crewWeekFor, forgetWeek } from "../src/crew/week_read.ts"; | |
| 18 | import { weekScreen } from "../src/tui/screens/week.ts"; | |
| 19 | import { quietLogs } from "./helpers/globals.ts"; | |
| 20 | import { landPulls, quietCtx, screenText, sessionFor } from "./helpers/tui-text.ts"; | |
| 21 | ||
| 22 | quietLogs(); | |
| 23 | ||
| 24 | let dir: string; | |
| 25 | let app: App; | |
| 26 | let owner: User; | |
| 27 | let guest: User; | |
| 28 | let repo: Repo; | |
| 29 | let sub: string; | |
| 30 | ||
| 31 | const text = (as: User = owner): string => { | |
| 32 | const screen = weekScreen(sessionFor(app, as), quietCtx(), getRepoById(app.db, repo.id)!); | |
| 33 | void screen.load!(new AbortController().signal); | |
| 34 | return screenText(screen, 140, 30); | |
outdated
Over 100 columns; wrap, and share the |
||
| 35 | }; | |
| 36 | ||
| 37 | const land = (n: number) => | |
| 38 | landPulls(app.db, repo.id, owner.id, n, () => new Date(Date.now() - 3600_000).toISOString()); | |
| 39 | ||
| 40 | const measure = (usedPct: number) => { | |
| 41 | noteMeasure(app.db, sub, { | |
| 42 | window: "week", | |
| 43 | usedPct, | |
| 44 | resetsAt: new Date(Date.now() + 86_400_000).toISOString(), | |
| 45 | observedAt: new Date().toISOString(), | |
| 46 | }); | |
| 47 | forgetWeek(); | |
| 48 | }; | |
| 49 | ||
| 50 | const person = async (username: string, adminIfFirst: boolean): Promise<User> => { | |
| 51 | const u = await createUser(app.db, { | |
| 52 | origin: "seed", | |
| 53 | trusted: true, | |
| 54 | username, | |
| 55 | email: `${username}@forge.local`, | |
| 56 | password: "a-good-passphrase", | |
| 57 | adminIfFirst, | |
| 58 | }); | |
| 59 | if (!u.ok) throw new Error(String(u.error)); | |
| 60 | return u.value; | |
| 61 | }; | |
| 62 | ||
| 63 | beforeEach(async () => { | |
| 64 | forgetWeek(); | |
| 65 | dir = await mkdtemp(join(tmpdir(), "forge-tui-week-line-")); | |
| 66 | app = createApp({ dataDir: dir, port: 0 }); | |
| 67 | owner = await person("founding", true); | |
| 68 | guest = await person("guest", false); | |
| 69 | const r = await createRepo(app.db, app.reposRoot, { | |
| 70 | ownerId: owner.id, | |
| 71 | name: "site", | |
| 72 | visibility: "public", | |
| 73 | }); | |
| 74 | if (!r.ok) throw new Error(String(r.error)); | |
| 75 | repo = r.value; | |
| 76 | const token = "sk-ant-oat01-" + "a".repeat(40); | |
| 77 | const added = addSubscription(app.db, dir, { name: "acct", token }); | |
| 78 | if (!added.ok) throw new Error(String(added.error)); | |
| 79 | sub = added.value.id; | |
| 80 | setGrants(app.db, repo.id, "", [sub]); | |
| 81 | }, 30_000); | |
| 82 | ||
| 83 | afterEach(async () => { | |
| 84 | app?.close(); | |
| 85 | await rm(dir, { recursive: true, force: true }); | |
| 86 | }); | |
| 87 | ||
| 88 | test("no allowance known: the screen says so, not 0 or NaN", () => { | |
| 89 | const out = text(); | |
| 90 | expect(out).toContain("The weekly allowance is not known yet"); | |
| 91 | expect(out).not.toContain("NaN"); | |
| 92 | }); | |
| 93 | ||
| 94 | test("known allowance, nothing landed: says so plainly", () => { | |
| 95 | measure(41); | |
| 96 | const out = text(); | |
| 97 | expect(out).toContain("Nothing has landed this week for 41% of the allowance"); | |
| 98 | expect(out).not.toContain("NaN"); | |
| 99 | }); | |
| 100 | ||
| 101 | test("known allowance and landings: same words as allowanceLine, the crew's percent", () => { | |
| 102 | measure(41); | |
| 103 | land(23); | |
| 104 | const now = new Date(); | |
| 105 | expect(crewWeekFor(app, repo, now)?.usedPct).toBe(41); | |
| 106 | const line = allowanceLine(app, repo, owner, now); | |
| 107 | expect(line).toBe("23 landed for 41% so far; the rest buys about 33 at this pace."); | |
| 108 | expect(text()).toContain(line!); | |
| 109 | }); | |
| 110 | ||
| 111 | test("a guest on a public repo sees no allowance line, and the function says null", () => { | |
| 112 | measure(41); | |
| 113 | land(23); | |
| 114 | expect(allowanceLine(app, repo, guest, new Date())).toBeNull(); | |
| 115 | expect(allowanceLine(app, repo, undefined, new Date())).toBeNull(); | |
| 116 | const out = text(guest); | |
| 117 | expect(out).not.toContain("landed for"); | |
| 118 | expect(out).not.toContain("allowance"); | |
| 119 | expect(text()).toContain("23 landed for 41%"); | |
| 120 | }); |
test/visibility-words.test.ts +5 −18
| @@ -17,11 +17,9 @@ import { createToken } from "../src/core/tokens.ts"; | ||
| 17 | 17 | import { createUser, type User } from "../src/core/users.ts"; |
| 18 | 18 | import { createOrg } from "../src/core/orgs.ts"; |
| 19 | 19 | import { getRepoById, type Repo } from "../src/core/repos.ts"; |
| 20 | import type { Ctx, Screen } from "../src/tui/app/app.ts"; | |
| 21 | import type { Session } from "../src/tui/session.ts"; | |
| 20 | import type { Screen } from "../src/tui/app/app.ts"; | |
| 22 | 21 | import { aboutScreen } from "../src/tui/screens/about.ts"; |
| 23 | import { Screen as Surface } from "../src/tui/term/screen.ts"; | |
| 24 | import { theme } from "../src/tui/ui/theme.ts"; | |
| 22 | import { quietCtx, screenText, sessionFor } from "./helpers/tui-text.ts"; | |
| 25 | 23 | import { localSource } from "../src/view/local.ts"; |
| 26 | 24 | import { serve, type Servers } from "../src/http/server.ts"; |
| 27 | 25 | |
| @@ -101,21 +99,10 @@ describe("the page head, the terminal and the MCP say it once, the same way", () | ||
| 101 | 99 | }; |
| 102 | 100 | /** The terminal's About card, as text. */ |
| 103 | 101 | const aboutText = (repo: Repo, who: User): string => { |
| 104 | const ctx = { | |
| 105 | theme: theme("dark"), | |
| 106 | invalidate() {}, | |
| 107 | toast() {}, | |
| 108 | push() {}, | |
| 109 | pop() {}, | |
| 110 | copy() {}, | |
| 111 | hit() {}, | |
| 112 | } as unknown as Ctx; | |
| 113 | const f = { app, db: app.db, user: who } as unknown as Session; | |
| 114 | 102 | const src = localSource(app, getRepoById(app.db, repo.id)!, who); |
| 115 | const screen = aboutScreen(f, ctx, { render() {} } as unknown as Screen, src, undefined, "main"); | |
| 116 | const s = new Surface(160, 30); | |
| 117 | screen.render({ x: 0, y: 0, w: 160, h: 30 }, s); | |
| 118 | return s.toText(); | |
| 103 | const back = { render() {} } as unknown as Screen; | |
| 104 | const screen = aboutScreen(sessionFor(app, who), quietCtx(), back, src, undefined, "main"); | |
| 105 | return screenText(screen, 160, 30); | |
| 119 | 106 | }; |
| 120 | 107 | const mcpAppLine = async (repo: string, who: User): Promise<string> => { |
| 121 | 108 | const res = await fetch(`${servers.url}/mcp`, { |
/weekdoes not show this line on this head; say the function is written here for #15818 to import.