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

Every answered request is counted by route pattern, surface and viewer role into a usage table #1654

Closed opened this
Closed without merging

Every answered request is now counted into a new requestcount table, one row per hour, route pattern, method, status class, viewer role and surface. The new test and the neighbouring suites I ran pass, and tsc is clean. I did not run the full check.

Notes

stopped closed without shipping deploy e1d9c3d

opened this

Every answered request is now counted into a new request_count table, one row per hour, route pattern, method, status class, viewer role and surface. The new test and the neighbouring suites I ran pass, and tsc is clean. I did not run the full check.

  • One place: answer() in src/http/server.ts counts each response, so the security headers on every response are untouched. The router now returns the pattern it matched, so a path with ids counts as /:owner/:name/issues/:number. A path matching no route counts as (no route). The /healthz and /readyz probes count under their own names.
  • Batched writes: src/core/usage_count.ts holds counts in memory and writes them in one transaction every 2 seconds, or when 500 distinct keys have gathered. A request only updates a map. Server stop flushes. A crash can lose up to the last 2 seconds of counts.
  • Roles:
    • An agent account is agent, and a forge admin is admin.
    • On repository routes a person is owner, writer or reader, by what they may do on that repository.
    • Elsewhere a signed-in person is reader, and anyone signed out is stranger.
  • Surfaces: /api is api, /mcp is mcp, a request with ?fragment= is fragment, and anything else is page.
  • Tests: test/usage-count.test.ts is added to GATE. It checks that three issue requests land on one row with n=3, that an agent's API call is counted as agent/api, and that unmatched paths never appear raw.

Changes from the issue as written:

  • File name: src/core/usage.ts already exists as the agent-run spend ledger, so the counter is src/core/usage_count.ts.
  • Route name: the issue's :n is really :number in the route table.
  • Schema: this adds migration 78.

Left out:

  • SSH counts: ssh is in the surface type, but nothing records it yet because SSH doesn't pass through HTTP.
  • Reading the counts: there is no page or API for them yet; listCounts reads the table.
  • Hosted children: I did not check whether requests answered for hosted children count into the child's own database.

To try it, request any page, wait 2 seconds, then select * from request_count in the forge database.

Closes #1564

2 earlier comments
commented

The failing check came from my branch being behind main, not from anything in the request-counting change. test/crew-sandbox-real.test.ts landed on main after I branched, and main already lists it in EXCLUDED in scripts/gate.ts. My branch didn't have that entry, so the sharding step rejected the file.

I merged origin/main into the branch and pushed. No code changed on my side, so the counting behaviour is the same as before.

I did not confirm the fix by running the tests. tsc --noEmit printed no errors. The bun test filters I gave matched no test files, so they ran nothing. I did not re-run bun scripts/test-shards.ts, which is the step that failed. Please check the forge's re-run of the check on the new push.

commented

The pull now merges into main again. main added a schema migration (78, variant groups) while my branch added its own migration 78 for the request_count table, so src/core/schema.ts conflicted.

I kept both and renumbered mine. Variants stay 78 and request_count is now 79. I found no other place that hard-codes a migration number.

I haven't re-run the full check. tsc --noEmit printed no errors, and the 25 tests I ran under test/schema and test/usage passed. The forge's check on this push will run the rest.

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