Public: anyone can read this; the crew can change it.

Cost ledger: an issue sums its pulls, and the board shows the median ask→live #1392

Closed opened this
Closed without merging

An issue's cost line now includes the pulls that close it, and once its change serves in production the line ends with its ask→live time. The board's forge line shows the day's median ask→live next to the dollars. Closes #1389.

Notes

stopped closed without shipping deploy 0fabe74

opened this

An issue's cost line now includes the pulls that close it, and once its change serves in production the line ends with its ask→live time. The board's forge line shows the day's median ask→live next to the dollars. Closes #1389.

This branch is stacked on #1391 (the #1386 ledger), which hasn't merged. Commit cb46b409 is that pull's commit. Only 4710aefa is new here, so review that commit, and land #1391 first.

What changed

  • An issue's cost line (issueLedgerCost in src/web/pages/runs.ts, shown on the issue page). It adds the issue's own entries to the entries of every pull whose description closes it. That includes pulls that never merged, because an abandoned attempt still cost money. The pulls are found with the forge's one reader, closesFromBody, through a new pullsClosing in src/core/pulls.ts. A body that says #12 is not counted for issue #1. The sum is costOf(db, ids) in src/core/cost_ledger.ts, and issueCost now calls it with a single id. The line reads like cost: $0.75 tokens · … · with 1 pull · ask→live 3.0 h, and like before only the owner sees it.
  • Ask→live (new src/core/lead_time.ts, which states the rules at the top):
    • The ask is when the issue was opened.
    • A merged pull is live when the first production deploy queued at or after its merge starts serving (serving_at). Rollbacks are not followed: any deploy queued after the merge is taken to contain it.
    • An issue is live when the first of its pulls is.
    • The day's median covers the issues that went live that UTC day, counting pulls merged in the 30 days before.
    • Two kinds of issue are left out: those whose repository never deploys to production, and those opened after their change was already live.
  • Board: the admin's forge line now reads today: … · median ask→live 3.0 h (1 live).
  • Migration 74 adds two partial indexes, run_deploy_served and pull_merged. They keep the board's two reads, which run on every page load, from scanning the whole table; I checked with EXPLAIN QUERY PLAN that both are used.

Tests: test/ledger-rollup.test.ts is new and added to GATE.

  • An issue's roll-up equals its own entries plus its pulls', checked against a hand sum. A pull that closes #1x is not counted for #1.
  • The median helper is checked against hand values for odd, even and empty sets.
  • dayAskLive over a fixed set gives the median of {1, 3, 5, 10} h = 4 h. The set includes a deploy queued before the merge, a preview deploy, a repository that never deploys, and a change that went live the day before; none of these is counted.
  • The issue page and the board both show the new text.

No existing test was changed.

tsc is clean, and the new file, ledger-cost, shards, check-list, hollow and the two schema tests pass. reserved-names and studio could not run in this sandbox: it refuses to let them listen on the hook socket (EPERM, tracked in #1163), so the forge's check is the first real run of those.

Not done here

  • A pull's own line still shows open→merged rather than ask→live.
  • Nothing was added to /metrics, because that route doesn't exist.

🤖 Generated with Claude Code

Closes #1389

5 earlier comments · 2 events
commented

An issue's cost line now adds up the ledger entries of every pull whose description closes it, merged or not. Once one of those pulls is serving in production, the line also shows the issue's ask→live time. That time runs from the issue opening until the first production deploy queued after that pull merged starts to serve. The board's forge line shows the day's median ask→live next to the dollars. core/lead_time.ts states what counts.

This builds on the cost ledger (#1386). Each finished run gets one hash-chained, Ed25519-signed entry, committed in batches to <data>/ledger.git. The cost_entry table is its index and is rebuilt from git when it falls behind. scripts/ledger-verify.ts reports the first broken link in the chain.

Merge with main: main took migration 73 for the MCP's OAuth oauth_code table (#971). This branch's migrations are renumbered after it:

  • 74: the run compute columns and cost_entry.
  • 75: the two partial indexes on pull(merged_at) and on served deploys in run(serving_at). They keep the board's per-render reads off a table scan.

No SQL changed, only the order. Nothing has ever applied this branch's migrations under their old numbers, so nothing needs repairing. Worth checking in src/core/schema.ts: the order is main's 73, then these two.

Tests: the type check passes. The schema, ledger, cost roll-up and OAuth tests pass (141 across 13 files). They were run with FORGE_TEST_SOCKET=port because the sandbox here refuses to bind Unix sockets. The full suite was not run here.

To try it: on the preview, open an issue that a merged pull closes and read its cost line, then look at the board's forge line for the median ask→live.

🤖 Generated with Claude Code

commented

A cost ledger records what each finished run cost. It is a hash-chained list of entries whose source of truth is a ledger repository in git (core/cost_ledger.ts). An issue's cost is rolled up from those entries. The board also gets an ask→live lead time (core/lead_time.ts), read from the day's merged pulls and the production deploys that served.

This push merges main again. The only conflict was at the end of the migration list in src/core/schema.ts: both sides had added a migration numbered 74.

  • main's migration (look.attached_by, look.note for #1066) stays at 74.
  • This branch's two migrations are now 75 (the run.cpu_s/run.mem_gb_s columns and the cost_entry table) and 76 (the partial indexes on run(serving_at) and pull(merged_at)).

Only the number comments changed; the migration text is the same on both sides. kernel/migrate.ts identifies a migration by a hash of its text, so a preview database that already ran the earlier numbering sees these as already applied and skips them.

Checks: bun run typecheck is clean. These suites pass: migrate, schema-migrations, schema-additive, ledger-cost, ledger-rollup, board, board-windows and attach-look. In looks.test.ts, one test failed: "a real capture through the grant door, with /usr/bin/chromium". Chromium couldn't start because the local sandbox refused it a socket (socket() failed: Operation not permitted); it is not caused by the merge, and the forge's check runs that test without the sandbox.

🤖 Generated with Claude Code

Changes requested — before the latest push

The ledger itself looks sound. Identity on every surface is gated: the board's day line is admin-only through forgeStrip, and the issue and pull cost lines go through canSeeOwnerFacts. The new capacity keys are validated strictly (^\d{1,3}(\.\d{1,6})?$, ≤ 100). There are no new write endpoints, no SQL built from user text, and the key is derived from secret.key with no new file or secret. bun test test/ledger-cost.test.ts test/ledger-rollup.test.ts gives 21 pass, 0 fail.

One item on the checklist is missed:

Fix first: there is only one door. The issue cost line (its own entries plus its pulls', with N pulls, ask→live …) exists only on the web issue page (issueLedgerCost in src/web/studio/tell.ts). Nothing in src/tui/screens/issue.ts, pull.ts or dashboard.ts shows the cost or ask→live, and no MCP tool or JSON API returns it. The direction says done means reachable on the page, through MCP and in a test. Concretely:

  1. Return the issue and pull roll-up (costOf plus issueAskLive) from the issue and pull JSON and MCP reads, behind the same canSeeOwnerFacts gate.
  2. Render the same costLine text on the TUI issue and pull screens, and the day's line on the dashboard for an admin.
  3. Add a test that a non-owner gets neither.

Smaller:

  • src/core/pulls.ts lost a space in an unrelated doc comment (answers:the).

I couldn't reach the preview or the look PNGs from my sandbox (both refused). This verdict rests on the diff and the tests.

the crew's run was interrupted: restart, resumes on the next pass — cancelled
commented

An issue's cost line now adds up the issue's own runs and every pull whose description closes it, including pulls that never merged. Once the change serves in production, the line also shows its ask→live time. The board's forge line shows the day's median ask→live next to the dollars. Closes #1389.

What changed

  • Issue roll-up: costOf(db, ids) in src/core/cost_ledger.ts sums the issue's runs and the runs of every pull returned by pullsClosing (src/core/pulls.ts). pullsClosing uses the forge's one reader, closesFromBody, so a pull that says #12 is not counted for issue #1.
  • Ask→live: src/core/lead_time.ts measures from when the issue was opened to the first production deploy that serves the merge. Its rules are written at the top of the file. The board shows the median, and leaves it out when nothing has gone live that day.
  • The roll-up appears on every door. It and its line live in src/actions/work_cost.ts, behind canSeeOwnerFacts. The places that read it are:
    • the web issue and change pages
    • a cost key on the issue and pull JSON
    • a new MCP cost tool (listed in docs/MCP.md)
    • the terminal's issue and pull screens
    • for an admin, the terminal dashboard

    Someone who can't write to the repository sees none of it.

Review feedback

  • The cost line was only on the web issue page; the terminal and MCP/JSON issue reads now show the same roll-up behind the same gate. test/ledger-doors.test.ts checks every door both as the owner and as a reader who cannot write.
  • The stray answers:the in src/core/pulls.ts is back to answers: the.

What I chose not to do: no per-run breakdown on the issue page. It shows the total and how many pulls it includes, and the change page already lists each run.

How to try it: on the preview, sign in as the owner and open an issue that one of the pulls closes. The cost line reads like cost: $0.75 tokens · … · with 1 pull · ask→live 3.0 h. The same numbers are in GET /api/v1/repos/<owner>/<repo>/issues/N, under cost, and on the terminal's issue screen. The type check passes, and the covering tests pass (99 across ledger*, mcp, board*, tui-screens, read-api and lead*). I did not run the full suite here; the forge's check does that on the push.

🤖 Generated with Claude Code

commented

The cost ledger now shows an issue's and a pull's cost through every door, not only the web page. An issue's cost is the sum of its pulls' costs, read from the cost_entry index of the ledger chain. Site admins also get the day's figures as a "today:" line at the top of the TUI dashboard. test/ledger-doors.test.ts drives each door twice: once as the owner, and once as a reader who cannot write.

This branch is now merged with main. Two files conflicted:

  • src/core/schema.ts: main's limb_place table (#1064) keeps migration 75. This branch's migrations come after it: the cost ledger's run columns and cost_entry table become 76, and the ask→live indexes become 77. Neither of those has reached production yet, so moving them is safe. Check the new order before merging.
  • src/tui/screens/dashboard.ts: the dashboard keeps both top strips. main's forge-health strip comes first, then the ledger's "today:" line, then the two panes.

To try it, open an issue or pull with finished runs on the preview, or as a site admin, open the dashboard in the TUI.

The type check is clean, and the schema, migration, limb, board, ledger and MCP tests pass locally (124 tests). The full suite will run in the forge's check on this push.

🤖 Generated with Claude Code

commented

I merged main into this branch again, which clears the merge conflict in two files that made the check fail. Type check is clean. These ran and passed: test/checks.test.ts (24 tests), plus the ledger and terminal-UI suites (291 tests). I didn't run the whole check; the forge runs it on this push.

  • src/checks/service.ts: main added a time budget to the full gate. The gate now aborts when it runs over and goes back on the queue once, and a second overrun fails it. This pull's cell meter now goes into that budgeted check(…) call, so a check's resource use is still recorded.
  • src/tui/screens/dashboard.ts: I kept both changes. Site admins still get the cost ledger's "today: …" line. Autonomous repositories still show main's planner-hold text.

The overrun paths return before any resource use is recorded, so a check that runs out of time is not metered. Recording it there would be a small follow-up.

🤖 Generated with Claude Code

commented

Parked by the crew: the check stayed red through 2 automatic tries

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs