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

Every answered request is counted once, in the server's answer, by pattern, role and surface #2170

opened this

Merged 50d 23h ago, live in production

Every response the forge answers is now counted once in usagecount. bun x tsc --noEmit is clean and the usage, access-log, app, router, server and check-list tests pass. I did not run the full suite.

Notes

opened this

Every response the forge answers is now counted once in usage_count. bun x tsc --noEmit is clean and the usage, access-log, app, router, server and check-list tests pass. I did not run the full suite.

  • Where it's counted: the count is taken in handle in src/http/server.ts, right after securityFloor, for both the forge's own answers and hosted children's. It's a map increment, so the common request adds no query. A refused event is logged as a warning and never turns into a 500.
  • What it records: the route pattern, role and surface. Pattern is the matched route string, so /issues/1, /issues/2 and /issues/3 land on one row. A path that matches no route is counted as (unmatched). Role is anon, user, admin or agent, from the viewer that resolveAuth returns. Surface is api for /api/, mcp for /mcp, and web otherwise.
  • Health checks: /healthz and /readyz are counted under their own patterns, as anon.
  • Hosted children: each child's requests are counted in its own database, and each database has its own counter.
  • Shutdown: Servers.stop() flushes every counter first, before the children's databases close.
  • New file: src/http/usage_tag.ts carries the pattern and role from the router to the response door. It also holds the role and surface helpers. It adds no dependency, and the gate picks up new test files by itself.
  • Test: test/usage-count-server.test.ts checks that three issue ids give one row of 3 with agent/api/GET. It also checks that random stranger paths collapse into (unmatched) and that responses keep the security floor.

What I left out:

  • Apps proxied through proxyApp: an app's own traffic is not the forge's answer, so it isn't counted.
  • Reading the data: no page or query reads the table yet.

To try it, boot the app, hit a few routes, wait about a second or stop it, then read usage_count with readUsage.

🤖 Generated with Claude Code

Closes #1918

commented

Every answered request is counted once in usage_count, by route pattern, role and surface, in the server's single response door. bun x tsc --noEmit is clean and the usage, check-list and spawn-host tests pass. I did not run the full suite.

  • Counting: the count is taken in handle in src/http/server.ts right after securityFloor, for the forge's own answers and for hosted children.
  • Role and surface: role is anon, user, admin or agent, taken from the request's viewer. Surface is api for /api/, mcp for /mcp, and web otherwise.
  • Unmatched paths: a path that matches no route goes to one (unmatched) row.
  • Health routes: /healthz and /readyz are counted under their own patterns, as anon.
  • Shutdown: Servers.stop() flushes every counter before hosted children close. The count is a map increment, so the common request adds no query.
  • Log amplification fix: a stranger's HTTP method (for example PROPFIND) used to make count throw, so each such request wrote a log warning. OTHER is now one of the allowed methods in core/usage_count.ts, and methodOf in src/http/usage_tag.ts maps any method outside the list to OTHER before counting. The table stays bounded and nothing is logged for these requests.
  • Tests:
    • test/usage-count-server.test.ts checks that three ids on one route give one row of 3 under agent/api, and that stranger paths collapse into (unmatched).
    • The same file checks both health patterns, /mcp on surface mcp, / on web, and an admin and a user each counted under their own role.
    • It checks that PROPFIND and MKCOL land in OTHER with app.log.warn never called, and has unit checks for roleOf, surfaceOf and methodOf.
    • test/usage-count-child.test.ts boots a hosted child with the spawn-host fixture. It checks that the child's requests land in the child's database and the parent's in the parent's.

I left out two things:

  • Proxied apps: an app's own traffic through proxyApp is not the forge's answer, so it isn't counted.
  • Reading the data: no page or query reads the table yet.

I used MKCOL rather than FOO in the test because Bun's fetch turns an unknown method like FOO into GET before it is sent.

🤖 Generated with Claude Code

commented

A request whose handler throws is now answered and counted. Before, it skipped the count and went straight to Bun's error() hook, so 5xx was undercounted. bun x tsc --noEmit is clean and the usage, check-list, spawn-host and app tests pass. I did not run the full suite.

  • Throwing handler: failure(app, error) in src/http/server.ts builds the answer for a throw. That is a 403 carrying the guard's own words when a database trigger refused, otherwise a 500 that says nothing of the fault. answered(owner, run) wraps both answer and a hosted child's http.handle, applies the floor and turns a throw into failure(...). handle then counts that response like any other, under the matched route's pattern, and a hosted child's throw is counted in the child's database. Bun's error() hook now calls the same failure as a last resort.
  • Tests:
    • test/usage-count-server.test.ts makes a repo query throw and checks for a 500 with the security floor and one 5xx row under /api/v1/repos/:owner/:name/issues/:number as admin. It fails without the fix.
    • test/usage-count-child.test.ts does the same inside a hosted child and checks the row lands in the child's usage_count.
  • Left uncounted on purpose: the early 404s for an unknown hosted child or app host sit before notePeer and are not counted. They are not answers from a route, and a stranger can send them with any hostname. That keeps the table out of reach of what strangers type. Proxied apps' own traffic is also uncounted, as before.

Nothing reads the table yet.

🤖 Generated with Claude Code

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs