The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
Backend metrics the scan can read from the log tail: implementation plan
This plan is followed task by task, test first, by the Engineer of task #2957 (project instructions, Engineer role), on PR #1880's own branch. There is no execution handoff.
Goal: The production scan measures an hour of backend load from what one argocd app logs call returns today,
even though each pod's logs keep only its last ~3 minutes (Ken's scan, 2026-11-04 21:30): requests per route, CPU per
request and DB bytes per request, with no tab, person or machine id anywhere.
Architecture: PR #1880 (10-28) adds prom-client counters and a /metrics listener; main has since added per-minute
log lines (route latency, db by shape, cpu by thread, cpu by kind) from the same hooks. After merging main, the
two are one: the events that feed the Prometheus counters also feed a ring of 60 one-minute buckets, and every minute
the process logs last hour, the ring's sums. The newest last hour line of each pod is in any log tail, so one read
gives the hour. /metrics serves the same counters, cumulative, for a scraper.
Tech Stack: Node 22, Fastify 5, Prisma 5 (MongoDB), prom-client 15, vitest.
Spec: Task #2957's brief (Ken's prod scan, 2026-11-04) and Ken's decision of 2026-11-04 (through River): node log size stays, the counters are how scans measure the backend, so they cover everything the scan reads from logs today.
Global Constraints
- Labels and log keys are bounded and carry no ids: a route is its Fastify pattern (
/api/super-tabs/:tabId/changes) orunrouted; a kind of work is the nameworkKind()gives (http GET /x,web-ws <frame>,bus <sub> <channel>,background); a Prisma model and operation; a status code. Each set is capped (300 values), past thatother. - Ships on, with no switch (ENGINEERING.md §1, Ken): the counters, the
last hourline and the/metricslistener run in every process. The port base and host are addresses, not switches. - One way to do a thing (ENGINEERING.md §1): one Prisma query extension, one slow-query log (main's
slow query, ≥ 200 ms, at most 20 a minute), one stack in a 500's log line (main'strace). - Measurement never blocks or slows the product (ENGINEERING.md §8): no work per request beyond a few map updates;
DB bytes come from
db by shape's existing sampled estimate, once a minute.
Review Focus
- A request to an unknown URL full of ids (
/wp-admin/6a7301…) is oneunroutedseries, in/metricsand in the line. - A pod that restarted 10 minutes ago logs an hour line covering 10 minutes and says so (
windowMs,minutes). - A 301st kind of work (a new bus channel, a new frame) lands in
other, in both views, never a new series. - A minute with no requests divides by nothing: per-request numbers are 0, not
NaNorInfinity. - A metrics port already taken (two local backends) is logged and the API keeps serving.
Task 1: One Prisma query hook times every query into the histogram
Files:
- Modify:
backend/src/lib/queryStats.ts(withQueryStats),backend/src/lib/metrics.ts(removetimeQuery,withQueryTiming,SLOW_QUERY_MS,QueryCall),backend/src/lib/prisma.ts - Test:
backend/src/lib/queryStats.test.ts,backend/src/lib/metrics.test.ts,backend/integration/prismaQueryTiming.test.ts
Interfaces:
Produces:
withQueryStats(client, stats?, warn?, observe?: (model: string, operation: string, seconds: number) => void);modelisrawfor a call without one.metrics.observeQuery(model, operation, seconds).Step 1: Failing test in
queryStats.test.ts:withQueryStatson a fake client whose$extendskeeps the hook; aUser.findFirstthat succeeds, one that throws, and a$runCommandRaweach callobserveonce with (User,findFirst) / (raw,$runCommandRaw) and the elapsed seconds; the thrown error passes unchanged.Step 2: Run
npx vitest run src/lib/queryStats.test.ts: FAIL (observe never called).Step 3: Add the
observeparameter; a throwingobserveis caught like a throwingstats.prisma.tswraps once:withQueryStats(client, undefined, undefined, metrics.observeQuery). DeletewithQueryTimingandtimeQuerywith their tests; the integration test uses the shared client andwithQueryStats.Step 4: Run the unit tests above and
npm run typecheck: PASS.Step 5: Commit.
Task 2: The /metrics listener is always on
Files: Modify backend/src/lib/metrics.ts (metricsAddress), backend/src/config/env.ts; test metrics.test.ts.
Interfaces: metricsAddress({ portBase = 9464, host = '127.0.0.1' }, workerIndex): { port, host } (never undefined).
- Step 1: Failing test:
metricsAddress({}, 0)is{ port: 9464, host: '127.0.0.1' }; worker 3 gets 9467. - Step 2: Run it: FAIL (
undefined). - Step 3: Default the base to 9464 in
env.ts(METRICS_PORT_BASEdefault'9464', no "empty means off"). - Step 4: PASS. Step 5: Commit.
Task 3: The last hour, from the same events as the counters
Files:
- Create:
backend/src/lib/lastHour.ts, testbackend/src/lib/lastHour.test.ts - Modify:
backend/src/lib/metrics.ts(counters,request,dbWindow,busyWindow, unmatched →unrouted),backend/src/lib/queryStats.ts(onWindowoption),backend/src/lib/selfProfile.ts(onWindowoption),backend/src/server.ts(wiring), testsmetrics.test.ts,queryStats.test.ts,selfProfile.test.ts
Interfaces:
createLastHour({ pod?, minutes = 60, top = 40, maxKeys = 300, cpuMs = () => process CPU ms, log? }): LastHourwithrequest(route: string, ms: number, failed: boolean),db(work: ReadonlyMap<string, { n: number; ms: number; bytes: number }>),busy(work: ReadonlyMap<string, number>),roll()(closes the minute and logslast hour).startLastHour(hour, everyMs = 60_000): () => void.createMetrics(hour = createLastHour())gains Countersdb_queries_total{work},db_result_bytes_total{work},work_busy_seconds_total{work}and methodsrequest(method, route, status, seconds),dbWindow(routes),busyWindow(kinds), each updating the Prometheus series and the hour.QueryStatsOptions.onWindow?(routes)gets every route's{ n, ms, bytes }of the minute (before the top-40 cut);SelfProfileOptions.onWindow?(busy)gets every kind's busy ms.The line:
last hour {pod, windowMs, minutes, requests, errors, cpuMs, cpuMsPerRequest, dbQueries, dbBytes, dbBytesPerRequest, routes: {"GET /x": {n, perMinute, errors, ms, dbQueries, dbBytes, dbBytesPerRequest, busyMs, busyMsPerRequest}}, work: {"web-ws x": {dbQueries, dbBytes, busyMs}}};routesjoinshttp GET /xwork to its route.Step 1: Failing tests in
lastHour.test.ts: sums across minutes and drops the 61st-oldest; per-request numbers;minutes/windowMsafter 10 minutes; zero requests gives 0; the 301st key isother; a route joins itshttpwork's DB bytes and busy ms; the line lists the top routes by time, by DB bytes and by busy ms.Step 2: Run: FAIL (module missing).
Step 3: Implement
lastHour.ts.Step 4: Failing tests in
metrics.test.ts: an unknown URL isunrouted;dbWindowof a bus channel kind addsdb_result_bytes_total{work="bus web termup"}with no id in the scrape; the 301st work isother; inqueryStats.test.ts,onWindowgets every route, not only the top 40; inselfProfile.test.ts,onWindowgets the minute's busy ms.Step 5: Run: FAIL. Step 6: Implement and wire in
server.ts. Step 7: PASS, typecheck. Step 8: Commit.
Task 4: The pull request says how the scan reads it
- PR body: first line
Requested by [email protected] through Harness.; the exactargocd app logscommand and how to sum pods;/metricsfor a scraper; the Design section; red/green evidence in a comment; backendnpm test.