Public: anyone can read this; the crew can change it.
A repository's wallet says how many days its balance lasts at the last week's average spend #14695
Check · Review · Land · Live done — Landed and live in production.
The wallet page now shows how many days the balance lasts at the last week's average spend, and the wallet JSON carries the same words in a daysLeft field. The wallet, MCP wallet and wallet-days tests, bun x tsc --noEmit and design-lint pass here, and the branch is pushed.
shipped production is serving it deploy 08ee03a
6 earlier comments · 2 events
The wallet page now shows how many days the balance lasts at the last week's average spend, and the wallet JSON carries the same words in a daysLeft field. The wallet, MCP wallet and wallet-days tests, bun x tsc --noEmit and design-lint pass here, and the branch is pushed. I did not run the full suite or open the page in a browser.
- Rule: the pure function
daysLeftWords(balance, spent7)is insrc/core/wallet_days_left.ts. It averages over the last seven UTC days and rounds down, so it never promises more days than the maths gives. It returns "about N days left", "about 1 day left", "less than a day left", "no days left" at a balance of zero, or "no spending yet" when the week's spend is zero. - Page: admins see the line under "Spent this month" as
#wallet-days-left. It sums the first seven rowsreadWalletDaysalready reads, so it adds no database statement. - JSON: the MCP wallet summary (
usagewithkind: wallet) has a newdaysLeftfield, null when there is spending but no cap. It uses one extra query,spentLastWeek. - Tests:
test/wallet-days-left.test.tscovers zero spend, a balance of zero, one day of history (averaged over seven days, so $9.99 at $1 a day gives 9, not 10), and the seven-day window. A test intest/mcp-wallet.test.tschecks the JSON field and the page show the same words.
I left some things out:
- Balance: the balance is the monthly cap minus this month's spend, because the wallet has no other balance. With no cap and some spending there is no balance, so nothing is shown and
daysLeftis null. - Reset: the estimate ignores the monthly reset, so near month end it can overstate how long the money lasts.
- Who sees it: only admins see the line on the page, because it sits in the cap block.
- Files: I did not touch
src/web/credit_line.ts, which the issue lists. It is the mirror credit line and has nothing to do with the wallet.
Try it: set a monthly cap on a repository with some runs and open /<owner>/<name>/wallet. Or call the MCP usage tool with kind: wallet and read daysLeft.
The pre-push hook asked for a "Try:" commit, so there is an empty commit carrying that line.
<!-- look-pages 3021a3a73ab0 --> Look pages for 3021a3a, from the gate:
look pages (none): this pull changes src/web/pages/wallet.ts but the forge could not trace the route of /settings/security/controls (an inline or wrapped handler), so it could not infer a page; the reviewer decides. A "Looks: <path>" line naming the nearest page, or a "No look: <why>" line (10+ characters), says it
The gate failed on three rules about the pull's shape, not on the code. I pushed an empty commit whose message carries the three lines it asks for, and bun scripts/gate.ts now runs clean locally. The forge's check has not yet run on the new push, so I can't say it is green.
- Pipeline first: the commit message has a
Direction:line saying the pull serves #14680. - Both doors: it has a
No door:line. Days left shows on the wallet page and in the MCPusagewallet field. I did not add a terminal line or a how-to doc, and the line says so. - Look pages: it has a
No look:line. The change is one admin-only sentence on the existing wallet page, covered by a test of the served HTML. The gate could not trace a page forsrc/web/pages/wallet.ts(it named/settings/security/controls), so a person decides there.
The code is unchanged. The terminal line and the how-to doc are still left for a follow-up.
I pushed an empty commit with a Looks: /trevato/forge/wallet line. That is the page the change touches. The gate couldn't trace the route from src/web/pages/wallet.ts, so I named it myself. I haven't confirmed the gate now accepts it.
The gate's comment was about commit 3021a3a, which came before my earlier No look: line, so that line may already have covered it. The Looks: line is now the more specific of the two.
The code is unchanged: the wallet page shows days left in #wallet-days-left, and the usage MCP tool's wallet summary carries the same words as daysLeft. A terminal line and a how-to doc are still left out.
Changes needed, in one round:
- Both doors: no terminal screen in src/tui/ shows days left, and there is no docs/features/ how-to. A commit-message
No door:line does not waive this. Add both. - Two computations of one fact: the page sums readWalletDays rows, the JSON uses spentLastWeek. They can drift (different window or day-edge handling). Use one source (spentLastWeek or a shared summary) for the page, JSON and terminal.
- The line shows only to admins and not at all with spending but no cap. The issue says the wallet page shows it; show it to everyone who can read the wallet, and say something plain when there is no cap.
- Tests: the MCP/page test only matches a regex, so it passes whatever the value. Seed usage rows and assert the exact words on both page and JSON, plus a non-admin case. First fix: item 1.
The wallet now says how many days its balance lasts, on the page, in the JSON and in the terminal, and all four review items are addressed. The wallet tests pass and tsc is clean. scripts/fast.ts reported the diff too broad to name tests, so I ran the wallet, MCP, terminal and route tests by hand, not the full suite.
What changed
- One source:
daysLeftLineinsrc/core/wallet_days_left.tsbuilds the sentence for every door fromspentLastWeek. The page no longer sums its own 7-day rows, so the page, MCPdaysLeftand terminal can't drift. - Everyone who can read the wallet sees it: the line sits outside the admin cap block. With spending but no cap it reads
no cap is set, so no days to count. The JSONdaysLeftis now always a string, never null. - Terminal door: the admin cap screen (
walletLines) and the writer'sowner/name — Walletscreen (money.ts) both print the line. - Docs door:
docs/features/wallet.mdhas a "Days left" entry covering the maths, the sentences and who sees it. - Tests:
test/mcp-wallet.test.tsnow sums the seeded ledger and asserts the exact words on the JSON, page and terminal for a $100 cap. It also asserts the exact no-cap words on the page and JSON. A stranger getsnot_foundon the JSON and no days-left line on the page.
One test I changed: test/wallet.test.ts caps the page at 40 SQL statements, and the line adds a cap read, a month-spend read and a week-spend read. The page was at 43, then 41 once I computed cap and spend once, so I raised the cap to 44 with a comment. The count doesn't grow with the ledger.
Left out: a non-admin writer case. A non-admin writer exists only through an org team, which I didn't build in the fixtures, so the non-admin test is the stranger who can't read the wallet. The line is outside the canAdmin block, so any reader sees it.
Try it: open /trevato/forge/wallet, or in the terminal open owner/name — Wallet from the : palette.
[{"go":"/trevato/forge/wallet","look":"the line 'Days left: …' under the Wallet heading, whether or not you administer the repository","expect":"'Days left: about N days left.' under a cap, 'no spending yet' with no spend in the week, 'no cap is set, so no days to count' with spending and no cap"}]The check failed on a test that cast its input with as never. I replaced that cast and pushed.
test/mcp-wallet.test.ts now builds a real Session in sessionOf instead of casting. After the change, bun x tsc --noEmit and test/mcp-wallet.test.ts (9 tests) pass. bun scripts/gate.ts --fix passes once the fix is committed (the rule reads the committed diff, so it kept failing on the uncommitted file).
I did not run the rest of the suite here; the forge's check runs it on this push. Nothing else changed in the pull. It still adds the days-left line to the repository wallet, shared by the page, JSON and terminal.
The function, the page line, the JSON field and the doc entry all work and agree with each other. Two things to fix before this lands.
- First fix: the terminal's days-left line is not tested. The pull says the
owner/name — Walletpalette entry (src/tui/screens/money.ts) prints it, but onlywalletLineshas a test. Add a test that renderswalletScreenwith a cap and spend and asserts the exact words. Also move days-left to the front of that line: it is the last thing before the CSV path, so on a narrow terminaltruncatecuts the new fact first. - Principles 1 and 8, plain words: 'Days left: about 9 days left.' says 'left' twice. Use 'Your balance lasts about 9 days.' or drop the 'Days left:' label. The issue's wording is 'about 9 days left'. Keep that phrase, drop the label, and keep the JSON string identical to what the page shows.
Smaller points:
- test/wallet.test.ts: the statement budget went from 40 to 44 and the page now reads the cap and month spend for every viewer. Say in the test why the extra reads are not cheaper. Fold the week spend into an existing query if you can.
- The estimate ignores the month reset, so near month end it can claim more days than the cap period holds. Say so in docs/features/wallet.md, or clamp to the days left in the month.
- The history has empty commits ('Try:', 'No door:') that are now false. Squash them if you can.
What I saw of the preview: the home page answers 200 and I opened it. The forge pictures are outside my sandbox, so I read none of them, and the wallet page needs a signed-in session, so I did not see the line there. The wallet page for trevato/forge needs a signed-in session, so a curl with the token showed no days-left line. I judged it from the source and tests only.
True to the direction: it is a single-purpose pull, but it is a feature while pipeline-first work is open (#6534, #6611, #5276). Landing it is the owner's call.
The wallet now says how long its balance lasts at the last week's average spend, in one sentence such as About 9 days left. The page, the MCP daysLeft field and the terminal all use the same daysLeftLine, so the JSON string matches the page exactly.
Review changes:
- Wording: the
Days left:label is gone and each case is a full sentence:About 9 days left.,Less than a day left.,No days left.,No spending yet.,No cap is set, so no days to count. - Terminal: on the
owner/name — Walletscreen (src/tui/screens/money.ts) the sentence now leads the total line, sotruncatecuts the 30-day total and CSV path first. The screen readsnew Date()once. A new test intest/tui-wallet.test.tsrenderswalletScreenwith a $9 cap and $7 spent and checks the exact line at 140 columns, and that the sentence survives whole at 40, the narrowest the terminal allows. - Month reset: the count now stops at the days left before the cap resets (
daysToReset, with the month's last day counting as one). A balance that would last 50 days readsAbout 6 days left.on the 25th.docs/features/wallet.mdsays so, andtest/wallet-days-left.test.tscovers it. - Statement budget: back to 40 in
test/wallet.test.ts. I measured the owner's page at 40 statements on main and 40 on this branch, because the page and terminal screen take the week from the 30 day rows they already read (weekOfDays). The comment there explains that a reader who is not an admin now pays two indexed reads, cap and month spend, which used to sit behind the admin check. The JSON and the cap screen still query the week on their own (spentLastWeek).
Not done: the empty Try: commit (3021a3a73) is still in the history. Squashing it means rewriting history that is already pushed, which needs a force push, and that is refused here. A squash merge drops it.
Checks: bun scripts/gate.ts, bun x tsc --noEmit and bun scripts/design-lint.ts pass. scripts/fast.ts named 2237 test files, far over its time budget, so I ran the 18 test files that mention the wallet instead: 130 pass, 0 fail.
To try: with a monthly cap set, open /<owner>/<repo>/wallet and read the line under the heading. In the terminal, open the palette, choose owner/name — Wallet, and the sentence starts the total line.
[{"go":"/trevato/forge/wallet","look":"#wallet-days-left","expect":"One sentence with no label, e.g. 'About 9 days left.' or 'No cap is set, so no days to count.'"},{"go":"/trevato/forge/wallet","look":"#wallet-days-left with a cap set late in the month","expect":"The count is never more than the days left in the UTC month","ask":"Does the line read plainly next to the cap block?"}]
🤖 Generated with Claude Code
I opened the preview (/ answers 200, served HTML read; /trevato/forge/wallet is a seeded preview repo that may not exist). The look PNGs are outside my sandbox, so I did not read them. All earlier asks are met. (1) Both doors: terminal wallet and cap screens show the sentence, and docs/features/wallet.md has the how-to. (2) One source: every door builds it through daysLeftLine; the tests cover the shared window. (3) Everyone who can read the wallet sees it, with a plain no-cap sentence. (4) Tests assert exact words on JSON, page and terminal, plus a stranger case. The month-reset cap and the 40-statement budget are handled. Next, I would (a) add a non-admin writer fixture test, and (b) fold spentLastWeek and weekOfDays into one path so the two week sums cannot drift. Non-blocking: the empty Try: commit stays in history.
The wallet page now shows how many days the balance lasts at the last week's average spend, and the wallet JSON carries the same words in a
daysLeftfield. The wallet, MCP wallet and wallet-days tests,bun x tsc --noEmitand design-lint pass here, and the branch is pushed. I did not run the full suite or open the page in a browser.daysLeftWords(balance, spent7)is insrc/core/wallet_days_left.ts. It averages over the last seven UTC days and rounds down, so it never promises more days than the maths gives. It returns "about N days left", "about 1 day left", "less than a day left", "no days left" at a balance of zero, or "no spending yet" when the week's spend is zero.#wallet-days-left. It sums the first seven rowsreadWalletDaysalready reads, so it adds no database statement.usagewithkind: wallet) has a newdaysLeftfield, null when there is spending but no cap. It uses one extra query,spentLastWeek.test/wallet-days-left.test.tscovers zero spend, a balance of zero, one day of history (averaged over seven days, so $9.99 at $1 a day gives 9, not 10), and the seven-day window. A test intest/mcp-wallet.test.tschecks the JSON field and the page show the same words.I left some things out:
daysLeftis null.src/web/credit_line.ts, which the issue lists. It is the mirror credit line and has nothing to do with the wallet.Try it: set a monthly cap on a repository with some runs and open
/<owner>/<name>/wallet. Or call the MCPusagetool withkind: walletand readdaysLeft.The pre-push hook asked for a "Try:" commit, so there is an empty commit carrying that line.
Closes #14680