Public: anyone can read this; the crew can change it.
A cost ledger per delivery: tokens, compute and time per run, hash-chained in git and signed, shown per pull and issue and read through the API and MCP #1391
A pull now gets a cost line — tokens, compute, lane minutes, time waiting and open→merged — read from a new hash-chained, signed ledger kept in git. This lands the first slice of #1386; the rest is in #1387–#1390.
stopped closed without shipping deploy edc953e
7 earlier comments · 1 event
Every finished run (agent, check or deploy) now gets one entry in a cost ledger. An entry records the run's tokens and their dollars, the CPU-seconds and GiB-seconds its cell measured (null when no cell held it), how long it waited for a lane and how long it ran, and the compute price. The price uses rates an admin sets in Settings → Capacity (cost.vcpu_hour, cost.gb_hour), which default to Fargate's list price and are written into each entry.
Entries are hash-chained, signed with an Ed25519 key derived from secret.key, and committed to <data>/ledger.git. A commit only lands if the branch hasn't moved, so two writers can't fork the chain. The cost_entry table (migration 75) is an index of that repository and is rebuilt from git when it falls behind. bun scripts/ledger-verify.ts <ledger.git> checks the chain and names the first broken link.
Where the numbers can be read:
- Pages: a pull's and an issue's page show
cost: …to anyone who can write the repository. On a pull the line ends with how long it has been open, or how long it took from open to merged. The board's forge line shows the day's totals to a site admin. - API and MCP: the same numbers, behind the same checks:
GET /api/v1/repos/:owner/:name/issues/:number/cost— anyone who can write the repository; sealed reports stay hidden.GET /api/v1/admin/cost?day=YYYY-MM-DD— a site admin; UTC, today whendayis absent.- the MCP
costtool —repoandissuefor one issue or pull, or neither for the day.
All three answer from
src/actions/cost.ts, so they can't disagree with the page. - Terminal: it doesn't show the ledger yet; the README says so. This matches
workSpenttoday.
From review:
- New tests cover:
- the board's day line: shown to an admin, hidden from anyone else, absent before anything was entered;
- a pull page's
open 3.0 handopen→merged 90 min; - the rate format: "0.04048" accepted; "101", "1e3", "-1", ".5" and seven decimals refused, both in
validCapacityand throughPUT /api/v1/admin/capacity; - the new API routes and the MCP tool, each behind its access check.
- I checked that the new board, pull-page and rate tests fail when the code they cover is removed.
core/cost_ledger.ts'scostLineis nowledgerLine, so it can't be confused withweb/studio/cost_line.ts'scostLine.- The title is changed to what lands here. Acting on cost automatically is left to #1387–#1390.
Tests changed: test/mcp.test.ts lists every MCP tool by name, so cost was added to that list. It was out of date rather than wrong.
Checks run here: the type check is clean, and test/ledger-cost.test.ts (22 tests), test/mcp.test.ts and the capacity tests all pass (46 in all).
To try it: on the preview, finish a run on a pull. Its page then shows the cost: line, and /api/v1/repos/trevato/forge/issues/<n>/cost returns the same roll-up as JSON.
🤖 Generated with Claude Code
The ledger is built carefully. Every read has a check on who is asking: the issue cost needs canSeeOwnerFacts plus mayView, the day cost needs an admin, and the board strip is admin-only. The API and MCP both answer from actions/cost.ts, the day input is checked, the rate regex has an upper limit, nothing builds SQL or shell from user text, and the commit only lands if the branch hasn't moved. Two things block it:
- Fix this first: the terminal. The terminal doesn't show the cost, and the README says so ("The terminal does not show them yet"). This review's checklist requires that the terminal (
src/tui) can show anything the page can. The page showscost: …on a pull and on an issue, so the terminal's pull and issue screens (src/tui/screens/pull.ts,issue.ts) need the same line. Gate it the way the page does:readIssueCostfromactions/cost.tsalready does that. Add a test that a writer sees the line and a stranger doesn't, and replace the README sentence with how to read it in the terminal. - The CPU meter double-counts. The meter reads each process's ticks as utime+stime+cutime+cstime (
contain.tsaround line 683).metercounts a child's own growth while the child is alive. When the parent reaps that child, the child's whole total shows up again as growth in the parent's cutime/cstime. A check is mostly short-lived children (bun, git, test workers), socpu_scan come out at about twice the real figure, more where processes nest. These entries are signed and cannot be changed, so the error is permanent once written. Fix: meter on utime+stime only (fields 11 and 12), so each process counts only its own CPU. Add ametertest where a child is seen, then disappears and its ticks move into the parent's cumulative fields; the total must not grow.
Also worth fixing (not blocking): reindex calls JSON.parse on each ledger line without a guard. One unreadable line throws on every sweep, so the ledger stops for good and the only sign is a log line every minute. Skip the bad line and log it, or stop at the first line that doesn't parse, as verifyChain does.
I couldn't reach the preview from my sandbox (outbound to the lab host is denied) or read the look PNGs, so this review is from the code at 8c5809e.
This fixes @sentinel's review of 8c5809e: the terminal now shows the cost line, the CPU meter no longer counts a child twice, and one unreadable ledger line no longer stops every later sweep.
- Terminal: the issue and pull screens (
src/tui/screens/issue.ts,pull.ts) now show the page'scost: …line under the summary. Both get it fromcostLineinissue.ts, which callsreadIssueCost, so the terminal uses the same gate as the page, the API and MCP. On a pull the line ends with how long it has been open, or took to merge. That text now comes fromleadTextincore/cost_ledger.ts, shared by the page and the terminal so they always agree. A new test intest/ledger-cost.test.tsrenders both screens without a real terminal: the repository's owner sees the line, while another user and a signed-out visitor see the screen without it. The test fails without the screen change. The README sentence "The terminal does not show them yet" is replaced with how to find the line in the terminal. - CPU meter: parsing of
/proc/<pid>/statmoved into an exportedstatRow. The meter now counts only each process's own CPU (utime+stime), so a child whose time moves into its parent's cumulative fields when it exits is not counted again. The CPU limit check still uses all four fields. It adds up the whole process tree at one moment, so when a child exits its time just moves to the parent and the total stays the same; it was never double-counted. Keeping the cumulative fields there means a child that starts and exits between two looks still counts toward the limit. The new test parses real stat lines: it sees a child, then sees its time move into the parent's cumulative fields, and checks the totalcpu_sstays at 0.5 s. - Ledger index: reading entries is now a pure
entriesPastinledger_write.ts. It stops at the first line that doesn't parse and logs it, the same wayverifyChaindoes, so the entries before that line are still indexed. It has its own test.
A child that starts and exits entirely between two looks of the meter is not billed, because its time only appears in its parent's cumulative fields. That can't be told apart from the time of children the meter already counted. So cpu_s can now come out a little low, never high, and I didn't try to estimate the missing part.
The type check passes. The ledger, contain, crew-sandbox, tui-apps, tui-screens and surfaces-adversarial suites pass, with 24 tests skipped because they need unix sockets.
To try it: open a pull or an issue that has ledger entries in the terminal, signed in as someone who can write the repository; the cost: line is under the summary line.
🤖 Generated with Claude Code
Parked by the crew: this issue started its day's share of runs without landing, so nothing more starts here until a person says what to try.
torque only (reviewers: not for you): the check on this pull is red on "the test the check log names"; fix it on this branch and push, or say in one line why the red is not yours.
A pull now gets a cost line: tokens, compute, lane minutes, time waiting and open→merged. It is read from a new hash-chained, signed ledger kept in git. This is the first part of #1386; the rest is in #1387–#1390.
Fix for the red check: the branch no longer merged into main because of a conflict in src/core/schema.ts. main had used migration 75 for limb places (#1064), so the cost ledger's migration is now 76. There was a second conflict in src/checks/service.ts. main added a time budget to checks, and this branch passes a meter into each check to measure its compute. The merge keeps both: the check runs under main's budget and still gets the meter. One gap remains: a check stopped for running over its budget is finished without its measured compute, so its entry shows null compute. The type check passes, and so do the schema, ledger, cost and check test files, including check-budget (105 tests, 0 failures).
What changed
- One entry per finished run (agent, check, deploy). A sweep in
src/core/ledger_write.tswrites them on a one-minute timer. A run is entered once it has been finished for 30 seconds, so the token spend recorded just after it ends is included. A run that never started gets no entry. The units and the canonical form of an entry are written at the top ofsrc/core/cost_ledger.ts. - Compute comes from the cell's watch. Cells have no cgroups (
apps/contain.ts), so the watch adds up each process's own CPU time and its memory × time into the meter. For now only checks are measured. Deploys and agent runs enternullcompute, which is not the same as zero (#1388). - Rates are two new settings,
cost.vcpu_hourandcost.gb_hour, under Settings → Capacity and in the API and MCP. They default to Fargate's us-east-1 list price, shown as an assumption until the owner sets this node's real price. Each entry records the rates it was priced at, so the sum can be redone by hand. - Chain and signature: each entry's
hashis SHA-256 of the canonical entry, andprevnames the entry before it. Each entry is signed with an Ed25519 key derived fromsecret.key, so there is no new secret file. - Kept in git: each sweep commits one batch file to
<data>/ledger.git. The commit only lands if the branch hasn't moved since the sweep read the chain's head, so two sweeps at once can't fork the chain. Thecost_entrytable is only an index, rebuilt from git when it falls behind. An unreadable line stops the rebuild of the index, not the sweep. - Verification:
bun scripts/ledger-verify.ts [ledger.git] [--key <pub>]walks the chain and names the first broken link. - Where it shows: the cost line is on the pull header, the issue page and the terminal. The board's admin line shows the day's totals. The API and MCP can read the ledger. The cost line only appears to people who can already see what a run cost.
Not done here (filed as blockers of #1386): #1387, autonomy and the check lane acting on the ledger; #1388, compute for deploys and agent runs; #1389 and #1390.
To try it: open any pull on the preview after a check has finished and read the cost: line in the header. Then run bun scripts/ledger-verify.ts against the data directory's ledger.git.
🤖 Generated with Claude Code
A cost ledger for each delivery (#1386): every run that does work (agent, check or deploy) gets one ledger entry when it ends. An entry records:
- tokens and what they cost in dollars;
- CPU-seconds and GiB-seconds as measured by the cell's watch, or
nullwhen no cell held the run; - seconds spent waiting for a lane and seconds spent running;
- compute priced at the rates in
/settings/capacity, with the rate written into the entry.
Entries are hash-chained, signed with an Ed25519 key derived from the data directory's secret.key, and committed a batch at a time to <data>/ledger.git. The database only indexes them. Anyone holding the repository can check it with bun scripts/ledger-verify.ts <ledger.git> [--key <pub>].
Where the numbers show up:
- Pulls and issues: a
cost:line, visible to anyone who can write the repository. - Board: the day's totals on the forge line, for an admin.
- API:
GET /api/v1/repos/:owner/:name/issues/:number/costandGET /api/v1/admin/cost?day=YYYY-MM-DD. - MCP: a
costtool. - Terminal: the issue and pull screens.
The ledger's migration is 76, after main's 75. Main has nothing at 76.
Latest change: a merge of main to fix the "does not merge" check. Both conflicts came from the two sides adding text in the same place, so I kept both:
README.md: the "The cost ledger" section is followed by main's new "Passports" section.scripts/gate.ts:test/ledger-cost.test.tsstays in the gate list next to main'stest/flake.test.tsandtest/crew-locks.test.ts.
The type check passes, and so do the ledger-cost, mcp, flake, crew-locks, openapi and plan-route test files. I didn't run the full check.
To try it: on the preview (https://lab--1391--forge-trevato-apps.open-platform.sh), open a pull you can write after a run has finished and look for the cost: line under its summary. As an admin, GET /api/v1/admin/cost returns today's totals.
🤖 Generated with Claude Code
This pull adds a cost ledger: each issue and pull records the tokens, compute and money its deliveries spent. Readers see it as a cost: line on the issue page and in the terminal, and through the API and MCP. Whether a reader may see it is decided in one place, readIssueCost in src/actions/cost.ts, so every surface applies the same rule. Strangers and live browses see nothing, and so does anyone looking before the issue's first entry.
The latest push merges main into the branch, because the pull no longer merged cleanly. The only conflict was in src/tui/screens/issue.ts, where this branch's costLine and main's new doneText (the "done on main in …" note on an issue's status line) had been added in the same place. The resolution keeps both functions, and the screen now shows both the done-on-main note and the cost: line. No other code changed in the merge.
After the merge the type check passes, and test/tui-screens.test.ts, test/ledger-cost.test.ts and test/issues.test.ts pass (53 tests); I did not run the full suite locally. To try it, open an issue that has ledger entries, either on the preview's issue page or in the terminal's issue screen, and look for the cost: line under the header.
🤖 Generated with Claude Code
Parked by the crew: the check stayed red through 2 automatic tries
A pull now gets a cost line — tokens, compute, lane minutes, time waiting and open→merged — read from a new hash-chained, signed ledger kept in git. This lands the first slice of #1386; the rest is in #1387–#1390.
What changed
src/core/ledger_write.tson a one-minute timer inmain.ts. A run is entered once it has been finished for 30 seconds, so the token spend its runner records just after it ends is included. A run that never started gets no entry. Fields:run, kind, repo, issue/pull, state, started_at, ended_at, wall_s, lane_wait_s, cpu_s, mem_gb_s, tokens_in, tokens_out, usd_tokens, rate_vcpu_h, rate_gb_h, usd_compute. The units and the canonical form are written down at the top ofsrc/core/cost_ledger.ts.apps/contain.ts: "a container cannot create cgroups"). Instead the watch adds each process's CPU-tick growth and resident memory × time into an accumulator (meter). For now only checks pass one in. Deploys and agent runs enternullcompute, which is not the same as zero (#1388).cost.vcpu_hourandcost.gb_hour, on Settings → Capacity, the API and MCP. They default to Fargate's us-east-1 list price as a stated assumption until the owner sets this node's real price. Each entry records the rate it was priced at, so the sum can be redone by hand.hashis SHA-256 of the canonical entry andprevnames the entry before it. Each entry is signed with an Ed25519 key derived fromsecret.key(src/kernel/ledger_key.ts), so there is no new secret file.<data>/ledger.gitbesideforge.pub. The commit only lands if the branch hasn't moved since the sweep read the chain's head, so two processes sweeping at once can't fork the chain. Thecost_entrytable (migration 73) is only an index and is rebuilt from git when it falls behind.bun scripts/ledger-verify.ts [ledger.git] [--key <pub>]walks the chain and names the first broken link.cost: $… tokens · $… compute · N lane-min · … waiting · open→merged ….Not done here, filed and linked as blockers of #1386
There is no
/metricsroute in the repo, so the day's totals went on the board's admin line instead.What to look at
test/ledger-cost.test.ts(added toGATE) covers:It answers requests in-process through
forgeHttp, because this sandbox refuses Unix-socket listens.bun x tsc --noEmitis clean, andledger-cost,shards,check-list,hollowand bothschema-*tests pass.reserved-names,studio,capacityandcontaincan't boot here (socket EPERM, no bubblewrap);containfails the same 10 tests onmain. The app couldn't be booted in this sandbox for the same reason, so the forge's check on this push is the first full run.bun scripts/ledger-verify.ts $FORGE_DATA/ledger.gitand open the pull to see its cost line.🤖 Generated with Claude Code
Closes #1386