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

Every answered request is counted by route pattern, surface and viewer role #1873

Parked opened this
Closed without merging

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.

Notes

stopped parked by the crew deploy 9e693a1

opened this

The forge now counts every answered request in a new usage_count 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. 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 --noEmit is clean. usage-count, check-list, schema-migrations, schema-additive, reserved-names, server, db, router, access-log and app tests pass. I did not run the whole suite.

What changed

  • Module name: The issue names src/core/usage.ts, but that file is already the agent-run spend ledger. The counter lives in src/core/usage_count.ts instead.
  • Counting: It happens in one place, answer() in src/http/server.ts, which also covers hosted children. Security headers are untouched.
  • Router: Router.match now also returns the route's pattern. That pattern is the key, so three ids on one route are one row.
  • Role and surface: Role is anon, user, admin or agent. Surface is api, mcp or web. An agent's API call is counted as agent/api.
  • Batching: A request costs one in-memory increment. Counts are written in one transaction after about a second or 500 keys, and on server stop, so no query is added per request.
  • Gate: test/usage-count.test.ts is named in GATE in scripts/gate.ts.

Left out

  • There is no page or API to read the counts yet; readUsage is the only reader.
  • /healthz, /readyz, asset traffic and app-subdomain proxy traffic aren't counted.
  • Paths no route claims are counted under a single (unmatched) row.
  • Rows older than 90 days are deleted the first time each process flushes.
  • There's no new top-level page, so nothing was added to the reserved names.
  • The test doesn't check security headers, because they are added at the Bun.serve edge 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

2 earlier comments · 1 event
Changes requested — before the latest push

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 anon even 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.

commented

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() in src/http/server.ts wraps the call to answerGet. If it throws, the request is counted as 500 and the error is rethrown, so error() still answers 500 or 403 as before. The new test in test/usage-count.test.ts renames the user table, calls /api/v1/users/:username, and asserts the call throws and leaves exactly one 5xx row.
  • The 90-day prune now runs every hour. The prune in src/core/usage_count.ts runs 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 kept anon for a path no route claims, whoever asked, because resolving the viewer on a miss would cost a session read. The module doc in src/core/usage_count.ts now says so.

Not changed: probes (/healthz, /readyz) are still not counted.

commented

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.

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