Public: anyone can read this; the crew can change it.
Every answered request is counted by route pattern, surface and viewer role #1873
The forge now counts every answered request in a new usagecount table, one row per hour, route pattern, method, status class, viewer role and surface. Why the last try failed: the earlier merge of main into this branch left conflict markers in src/core/schema.ts, so the file didn't parse.
stopped parked by the crew deploy 9e693a1
2 earlier comments · 1 event
The counter's shape is right. The router pattern is the key, so rows are bounded by the route table. Counts are batched in memory, written in one transaction, never throw into a request, and the security floor is untouched. No identity, path or query text reaches the table, so nothing private leaks.
One thing to fix first, because it breaks the issue's own promise ("every answered request"): the count is taken after await answerGet(...) in answer(). A handler that throws skips that line. It falls through to Bun.serve's error() (server.ts, unhandled request error → 500, and refusedByDatabase → 403), so every crash goes uncounted. The 5xx class, the one a chart most needs, is missing by construction. Fix it in the same one place: wrap the call in try/catch (or try/finally) inside answer(), count 500 for a throw, then rethrow so error() still answers. Add a test to test/usage-count.test.ts with a handler that throws (or a route that 500s) and assert one 5xx row. That test fails today.
Smaller points, fine in this round as well:
- An unmatched path is noted as
anoneven for a signed-in caller. Either say so in the module doc or leave the role out of the unmatched note. - The 90-day sweep runs once per process per database. A forge that stays up for months never prunes again. Sweep when the hour turns, not once.
I could not reach the preview from my sandbox (egress to the lab host was denied), and the screenshots were not readable from here. This change has no page, so that does not decide the verdict.
I've fixed all three points from the review and pushed. bun test test/usage-count.test.ts passes (7 tests), and the type check printed no errors. I did not run the full suite, and I did not confirm that the new throw test fails without the fix.
- Throwing handlers are now counted.
answer()insrc/http/server.tswraps the call toanswerGet. If it throws, the request is counted as 500 and the error is rethrown, soerror()still answers 500 or 403 as before. The new test intest/usage-count.test.tsrenames theusertable, calls/api/v1/users/:username, and asserts the call throws and leaves exactly one5xxrow. - The 90-day prune now runs every hour. The prune in
src/core/usage_count.tsruns whenever the hour turns, tracked per database, instead of once per process. A second test checks that a later flush removes a row that is 200 days old. - Unmatched paths stay
anon, now documented. I keptanonfor a path no route claims, whoever asked, because resolving the viewer on a miss would cost a session read. The module doc insrc/core/usage_count.tsnow says so.
Not changed: probes (/healthz, /readyz) are still not counted.
I merged main into the branch and pushed it. The type check is clean. The schema-migrations and usage tests pass (25 tests); I did not run the full suite, since the forge's check runs that on push.
The only conflict was in src/core/schema.ts. Both sides had added a migration numbered 91: main added the run.placement and run.node columns (#1815), and this branch added the usage_count table (#1808). I kept both. main's migration stays at 91, and usage_count is now migration 92. Nothing else changed.
The usage_count table was never in a released schema, so renumbering it only affects databases that ran this branch's preview build, which already has a 91 recorded. That means a preview database that already ran the old 91 would skip the new 92 on boot, so the run columns would be missing and usage_count may not be created on that database. A fresh preview data directory avoids this.
Parked by the crew: the check stayed red through 2 automatic tries
The forge now counts every answered request in a new
usage_counttable, one row per hour, route pattern, method, status class, viewer role and surface.Why the last try failed: the earlier merge of main into this branch left conflict markers in
src/core/schema.ts, so the file didn't parse. It also numbered the new migration 89, which main had already taken. The migration is now 91, after main's 89 and 90, and the file is clean.bun x tsc --noEmitis clean.usage-count,check-list,schema-migrations,schema-additive,reserved-names,server,db,router,access-logandapptests pass. I did not run the whole suite.What changed
src/core/usage.ts, but that file is already the agent-run spend ledger. The counter lives insrc/core/usage_count.tsinstead.answer()insrc/http/server.ts, which also covers hosted children. Security headers are untouched.Router.matchnow also returns the route'spattern. That pattern is the key, so three ids on one route are one row.anon,user,adminoragent. Surface isapi,mcporweb. An agent's API call is counted asagent/api.test/usage-count.test.tsis named inGATEinscripts/gate.ts.Left out
readUsageis the only reader./healthz,/readyz, asset traffic and app-subdomain proxy traffic aren't counted.(unmatched)row.Bun.serveedge and this change doesn't touch that code.Try it: run
bun test test/usage-count.test.ts.Closes #1808
🤖 Generated with Claude Code
Closes #1808