refactor(usage): split usage.ts + proxy hardening (Theme X2)#33
Merged
Conversation
…ing (X2) Theme X2 from the authmux improvement protocol. The 660-line usage.ts co-resident with three subsystems (public API client, localhost proxy with auth dance, local rollout walker) plus pure scoring math is split into focused modules: usage/api-client.ts — fetchUsageFromApi(parsed) usage/proxy-client.ts — fetchUsageFromProxy() + dashboard session usage/local-rollout.ts — fetchUsageFromLocal(codexDir) usage/math.ts — pure remainingPercent/usageScore/... usage/index.ts — barrel usage/_internal/snapshot-parsers.ts — shared parser helpers Proxy hardening: by default the proxy client now refuses to send dashboard credentials to non-loopback URLs (literal 127.0.0.0/8, ::1, localhost). Behind AUTHMUX_PROXY_INSECURE=1 the legacy permissive behavior is preserved for one minor release, with a process.emitWarning on every call. Refusal raises ProxyInsecureUrlError (code E_PROXY_INSECURE_URL) extending AuthmuxError per the N3 taxonomy. Env vars CODEX_LB_DASHBOARD_URL / CODEX_LB_URL are resolved lazily per call per N4 conventions. Tests: src/tests/usage-math.test.ts adds 41 cases covering remainingPercent, resolveRateWindow, usageScore, shouldSwitchCurrent edge cases (0%, 100%, NaN, negative, reset-expiry, fallback windows, threshold boundaries). All 170 tests pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Theme X2 —
usage.tssplit + proxy hardeningImplements Theme X2 from
docs/future/17-ROADMAP.mdand the 5-file split sketched indocs/future/01-ARCHITECTURE.md§2.2.Exit criteria
src/lib/accounts/usage/directory exists withapi-client.ts,proxy-client.ts,local-rollout.ts,math.ts,index.ts.usage.tsis removed (no shim — only internal consumers existed;from "../usage"resolves tousage/index.ts).CODEX_LB_DASHBOARD_*env vars are set.src/tests/usage-math.test.tscoversremainingPercent,usageScore,shouldSwitchCurrentexhaustively (41 cases).New files (LOC)
src/lib/accounts/usage/math.tsremainingPercent,resolveRateWindow,usageScore,shouldSwitchCurrentsrc/lib/accounts/usage/local-rollout.tsfetchUsageFromLocal(codexDir)— walks~/.codex/sessions/jsonlsrc/lib/accounts/usage/api-client.tsfetchUsageFromApi(parsed)— single ChatGPT backend-api fetch with 5s timeoutsrc/lib/accounts/usage/proxy-client.tsfetchUsageFromProxy()— dashboard session, password env, TOTP, non-loopback refusalsrc/lib/accounts/usage/index.tssrc/lib/accounts/usage/_internal/snapshot-parsers.tssrc/tests/usage-math.test.ts(N2-introduced
usage/adapter.tsis unchanged; itsfrom "../usage"import now resolves tousage/index.ts.)Proxy hardening details
ProxyInsecureUrlErrorextendsAuthmuxErrorwithcode = "E_PROXY_INSECURE_URL", slotted into the N3 taxonomy.127.0.0.0/8,::1,localhost, and IPv4-mapped IPv6 loopback. Deceptive hostnames (127.example.com,127.0.0.1.evil.com) are refused.CODEX_LB_DASHBOARD_URLtakes precedence overCODEX_LB_URL; default falls back tohttp://127.0.0.1:2455.AUTHMUX_PROXY_INSECURE=1opts back into pre-X2 permissive behavior with aprocess.emitWarningon every call. Will be removed in the next minor release.Verification
Edge cases additionally verified locally:
http://localhost:2455accepted,http://[::1]:2455accepted,http://127.0.0.5:2455accepted,https://127.example.comrefused,http://127.0.0.1.evil.example.comrefused.Test plan
npm run build— cleannpm test— 170/170 pass (was 129/129; +41 usage-math cases)🤖 Generated with Claude Code