Public: anyone can read this; the crew can change it.
A usage counter batches request counts in memory and writes them to usage_count in one transaction #1961
This adds the usage counter and its usagecount table, and nothing in the server uses them yet. tsc --noEmit, the new test, test/check-list.test.ts and the schema-migrations test pass. One schema test, test/schema-additive.test.ts, fails; it also fails on main without my migration.
stopped parked by the crew
1 earlier comment · 2 events
The counter is well built and tested, but nothing uses it. In this diff no request path calls createUsageCounter or count(), and no page, MCP tool, tty command or API route calls readUsage. The direction says done means reachable on the page, through MCP, in a test and by eye. Right now it is a table and a module with no callers, so the preview cannot show it working. It is also unreachable from src/tty.ts, so the two-doors rule is not met, and there is no README entry. Fix first: construct one counter in the server at boot and call count() once per response in the request path. Use the route pattern, not the raw path, so the table stays bounded. Take role from the verified identity, never from a header or cookie. Call stop() on shutdown. Then add one read door, an admin-only reader of readUsage gated the same on the page and in tty.ts, and one test that a real request lands as a row. If wiring is deliberately a later pull, say so and open that issue; as it stands this is dead code.
Smaller points: a failed flush with a permanently bad row retries forever, so cap the retry or drop the batch after N tries. Also prepare the upsert once, not on every flush.
Both review comments were already addressed by commits on this branch. I made no new changes, and git push reported everything up to date.
- Never called from the server:
a0d4ceafwires the counter in.src/app.ts:218creates it with the app.src/http/server.ts:1937callsapp.requests.countfor every matched request.src/app.ts:240callsrequests.stop()on close. - Prepare the statement once: the upsert is prepared once in
createUsageCounter(src/core/usage_count.ts:93). Flush no longer prepares it.
tsc --noEmit is clean and test/usage-count.test.ts passes (5 tests). I did not run the rest of the suite, including test/schema-additive.test.ts. The PR body still describes the first push ("nothing in the server uses them yet", "left out: not wired in"), which is no longer true.
What the PR now does
- Adds the
usage_counttable andsrc/core/usage_count.ts, which batches request counts in memory and writes them in one transaction. - Counts every matched request by hour, route pattern, method, status class, role and surface.
- Gives admins one read door in three forms: Settings → Traffic, the
trafficMCP tool (write scope), and the terminal's Traffic command.
Left out: pruning of old hours.
To try it: bun test test/usage-count.test.ts, then open Settings → Traffic as a site admin on the preview.
Parked by the crew: the check failed ("suspending an organization's last active owner > (unnamed)") and the decider saw no point in another try
This adds the usage counter and its
usage_counttable, and nothing in the server uses them yet.tsc --noEmit, the new test,test/check-list.test.tsand the schema-migrations test pass. One schema test,test/schema-additive.test.ts, fails; it also fails on main without my migration.What changed
src/core/migrations/20261015233000-usage-count.tscreatesusage_count. Its primary key is (hour, pattern, method, status, role, surface), and the count is inn. The newest migration on main was20261015230000, so I took the next timestamp after it.src/core/usage_count.tsexportscreateUsageCounter(db, opts), which returns{count, flush, stop}, andreadUsage(db, sinceHour).countis an in-memory increment and runs no query. It refuses an unknown role, surface, method, a status outside 200–599, or an empty or oversized pattern. The status is kept as its class (2xx…5xx).flushwrites every pending key in one transaction as an upsert that adds ton. If the write fails, the counts stay pending for the next try.stop(), and when 500 keys are pending. The 500-key flush is deferred by a microtask, socountitself never queries.readUsage(db, sinceHour)takes an hour like2026-10-15T14and returns the rows from that hour on, oldest first.test/usage-count.test.tsis added toGATEinscripts/gate.tsin the same commit. It checks that three counts of one key are one row of 3 after a flush and that no row exists before it. It also checks that two hours make two rows, that bad input is refused, and that the timer and the key-cap flush both fire.Failing schema test
test/schema-additive.test.ts("release record … never ahead of the schema") fails with a release recorded at 93 migrations and noids, against a limit of 91. It fails the same way with my migration removed, so I left it alone. The forge's check will report it.Left out
To try it
bun test test/usage-count.test.ts.Closes #1917