Repository navigation
feat(usage): serve a read-only live meter on GET /v1/usage/live - #8735
Closed
andrew05060414 wants to merge 6 commits into
Closed
andrew05060414 wants to merge 6 commits into
andrew05060414 wants to merge 6 commits into
Conversation
…ext fill) for one request row
… desktop renderer
…ve-meter server fixture
… test room to start
Contributor
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
Contributor
|
Contributor
⏳ DRAFT
What to do
Review readiness checklist
✅ 4/4 boxes ticked. This pull request was already a draft. Its draft status will be preserved after every issue above is resolved. Hygiene
|
3 of 7 tasks
Contributor
Author
|
Superseded by #8741, which carries this change together with the related live routing PRs as one commit each on current |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Problem. Users who want generation speed, cache hit and context fill next to the Codex desktop composer currently tail
usage.jsonlwith a separate reader process and recompute everything from raw ledger rows. OpenCodex already records the exact generation window (genStartMs/lastOutputMs, #6309) and the usage counters, but exposes no small read of "how did the last request go".After this PR.
GET /v1/usage/livereturns the newest completed conversation request, overall and per client family, as numbers only:{ "latest": { "speed": { "value": 90, "reason": null }, "outputTokens": 540, "generationMs": 6000, "cache": { "readTokens": 184000, "inputTokens": 200000, "percent": 92, "reason": null }, "context": { "usedTokens": 200540, "limitTokens": null, "percent": null, "reason": "limit_unknown" }, "model": "gpt-6.1-sol", "provider": "native", "client": "codex_desktop", "timestamp": 1794612826094 }, "byClient": { "codex": { "...": "same shape" } }, "generatedAt": 1794613105280 }nullwith a reason code when the request failed, was cancelled or incomplete, usage is missing or estimated, output tokens are missing, or the window is missing, under 1 s or inconsistent. Cache and context percentages arenullwith a reason when their inputs are not known; the context limit comes from the persisted Codex catalog'scontext_window(cached by mtime and size), never a default.client=codex|claude|other,conversation=<raw id | logged digest | codex://threads/<id>>(reusesmatchesLogConversationId). The filter is never echoed.codex-auto-reviewrows are never reported./v1/models(resolveApiAuth): no credential on a loopback bind, a data key otherwise, never the admin token. Additionally the Codex desktop renderer originapp://-may read this one route and its preflight; the loopback Host gate still applies when no key is required, and every other data-plane route keeps its origin rules (/v1/modelsfromapp://-still returns 403). Not reachable over link ingress. No dashboard change.Details.
src/usage/live-meter.tsis the pure projection;src/server/live-meter-handler.tsthe route, catalog cache and preflight.serve-options.tsgains two short blocks (now ~1962 of its 2000-line cap).AUTH_MATRIXgains a row. No new imports insrc/router.ts,src/server/lifecycle.tsorsrc/server/responses/core.ts; nothing fromsrc/lab.Access-Control-Allow-Methodslists the generic method set; the route itself only serves GET (POST returns 404).reference/management-api.md(en, es, zh-cn, zh-tw) andstructure/usage-client-attribution.md.Verification
tests/usage/live-meter.test.ts(ported generation-window cases plus cache, context and selection) andtests/server/live-meter-handler.test.ts(query validation, client and conversation filters, no id echo,app://-allowed on this route only, non-loopback without key rejected, and a realstartServercase): 92 pass across these,core-lab-boundary,management-route-registry,test-layoutand the file-size ratchet.ocx startwith synthetic ledger rows:Origin: app://-GET returns 200 withAccess-Control-Allow-Origin: app://-; a 6 s window with 540 output tokens reads 90 tok/s; a 0.3 s window readsnull/generation_window_too_short; a newercodex-auto-reviewrow in the same conversation is skipped; an unknown conversation returnslatest: null;Origin: https://evil.examplereturns 403.bun run typecheck,bun run structure:check,bun run privacy:scan, Spanish docs localization tests: pass.AUTH_MATRIX is true of the running servertimes out on this machine with and without this branch (it also fails on a checkout without these commits), so it is left to CI. The real-server case got a 60 s timeout because server start-up under the shared machine's load exceeded the default 5 s.bun run testwas not run (shared Windows machine; the preload git-config guard rejectsGIT_CONFIG_GLOBAL=NULhere). Coverage beyond the files above is left to CI.Readiness (2026-11-13)
GET /v1/usage/livewas not on the unauthenticated loopback listener's allowlist. On a host whose public listener binds a non-loopback address, the Codex++ footer's127.0.0.1:<port>read reaches that socket and got a 404. It is now admitted there as GET only.structure/runtime.mdandstructure/usage-client-attribution.mdare updated, and a shape test pins the entry./api/*is still 404 there.tests/server/live-meter-handler.test.tsandtests/server/loopback-listener-admission.test.ts: 44 pass.dev(873ec9b), inside the gate's window; no rebase for review.bun run typecheck,structure:check,skill:surface:check,privacy:scan, the layout and file-size guards and this PR's focused tests; after the deploy one real request each through Claude, GPT, Gemini, DeepSeek and Grok returned 200 and the error rate did not rise.maintainer-sponsoredlabel: the hygiene gate flagssrc/server/auth-cors.ts. Everything else is ready for review.Checklist
/v1/models; one extra allowed origin (app://-) scoped to this route; body holds no prompt text, account labels, keys or conversation ids.Review readiness checklist
This PR stays in draft until every box below is ticked. Tick all four boxes once the requirements are met:
Required local validation passed; commands, results, and any full-suite exception are documented.
I pushed my PR to a recent dev commit (at most 10 behind; a maintainer may still ask for the exact tip before merge).
I resolved all correct Codex and CodeRabbit findings.
My PR is ready for review.