GeezerVison is showing github.com — lab-ops-hub: /api/budget hardcodes estimated_overage_usd to 0 and undercounts D1 writes (PR #436) · Issue #442 · developing-today/code
lab-ops-hub: /api/budget hardcodes estimated_overage_usd to 0 and undercounts D1 writes (PR #436) #442DescriptionFinding
PR #436 (#436, head a772fc2f391234cbdd4ed169d9174b13e8994801, initial commit 98381153702ef50135230e1d941040e4bbb4f4db) makes /api/budget the centrepiece of pkgs/lab-ops-hub — the README says: "The budget endpoint is the point … so the demo continuously proves it is costing $0 over the $5 plan." The implementation in pkgs/lab-ops-hub/src/index.ts cannot prove that:
- The overage figure is a literal. The budget handler (:140–:152) returns
estimated_overage_usd: 0 unconditionally. Each resource row in the same response carries used, included, and an overage rate, but no code ever compares used to included or computes an estimate — the endpoint would report $0 even after a limit is passed.
d1_rows_written is badly undercounted. It reports used: by.d1_write ?? 0, but logUsage(env, "d1_write", …) is only called after a machine insert (:169) and a note insert (:182). D1 writes that never emit that event include: every logUsage ledger insert itself (:36 — at least one per request, for worker_request), the tasks insert (:208), the files-metadata insert (:225), the summary UPDATE (:197), the crawls insert (:264), and the queue consumer's files UPDATE (~:288). Actual D1 rows written are therefore many times the reported number (the worker_request ledger alone guarantees writes ≥ requests).
Verified against the PR head code above; the package is not on main yet. Deduped: issue searches for lab-ops-hub, budget, overage return no existing issue. Cross-repo: the package exists only in this repo/PR.
Why it matters
A self-check whose answer is hard-coded to "$0, under budget" is worse than no self-check: the first time usage actually exceeds an included amount (see the unauthenticated-endpoint exposure in the sibling issue), the dashboard will still claim $0 and the overage will be discovered on the Cloudflare bill instead.
Acceptance criteria
-
estimated_overage_usd is computed from the counted usage: for each resource with an included amount and a rate, max(0, used - included) * rate, summed (resources without a rate are excluded from the sum but still shown)
- The
d1_write counter covers every D1 write path (or the row is renamed/documented as "counted application writes" and the ledger's own writes are accounted for explicitly) — a request sequence of known writes (e.g. 1 task POST + 1 file POST + 1 crawl POST) moves the counter by the true number of rows written
- A test or scripted check demonstrates the endpoint reports a non-zero estimate when recorded usage exceeds an included amount (e.g. by seeding
usage_events past a threshold)
Activityadded a commit that references this issue on Oct 21, 2026This issue will close once commit f776001 is merged into the 'main' branch.f776001 dezren39 commented on Oct 21, 2026 Fixed on PR #436's branch in commit f776001 (same commit as the #441 fix):
estimated_overage_usd is now computed by computeBudget (src/lib.ts): per resource with an included amount and a rate, max(0, used - included) * rate, summed and also reported per-resource.
d1_rows_written covers every D1 write path — d1_write events now also fire for the tasks insert, files-metadata insert, summary UPDATE, crawls insert, queue-consumer files UPDATE and cron ledger trim (machines/notes already had them) — plus the usage ledger's own writes, accounted for explicitly: every usage_events row is itself one D1 write, so the month total of all event kinds is added on top and the response/README say so.
- Tests (
npm test, Node, 6/6) demonstrate the non-zero case: 20M counted worker requests → estimated_overage_usd = 3; 30M d1_write events → 60M counted rows → $10 overage. The handler-level check (stubbed D1 returning 20M worker_request rows) returns estimated_overage_usd 3 with d1_rows_written.used = 20,000,000.
This issue stays open until PR #436 merges.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|