Skip to content

fix(usage): read Magpie gateway quotas and group limits by owner - #90

Merged
nullStack65 merged 2 commits into
release/native-0.1.10from
fix/usage-limits-magpie-owners
Nov 18, 2026
Merged

nullStack65 merged 2 commits into
release/native-0.1.10from
fix/usage-limits-magpie-owners

Conversation

@nullStack65

@nullStack65 nullStack65 commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Usage → Limits showed errors for instances that route through a Magpie gateway and never showed the accounts that gateway pools. Retargeted from main to release/native-0.1.10, the line T3 hosts build from.

Root causes

  1. OpenCode (OpenCode Go could not read usage.): every local OpenCode instance probes OpenCode Go with the host's Go key, even when its models route through Magpie. Only a final 403 counted as "no subscription". A key Go refuses any other way (401, 402, 404) became a failed probe and a warning.
  2. Codex (Codex could not read usage (JSON-RPC -32600)): a Codex instance whose model_provider is a custom gateway has no OpenAI account. Older server builds still called account/rateLimits/read, and Codex answers -32600 "codex account authentication required to read rate limits" (reproduced against a scratch CODEX_HOME with Codex 0.166.0). This line already skips the read for non-native routes (codexRoutesSpendNativeQuota, fix(server): custom Codex providers no longer report native quota pingdotgg/t3code#18622), so the host showing it runs an older build. Here the instance was simply dropped from the page; it now says why.

Changes

  • OpenCode Go: 401/402/403/404 become unsupported with a "Not applicable: …" reason. 5xx, timeouts and malformed bodies still warn.
  • Codex: a gateway-routed instance (one with a custom endpoint whose native quota isn't spent) keeps unsupported and gets a reason, so it is listed rather than silent.
  • New magpie kind for usageLimitSources, which reads GET /v1/magpie/quotas. Window names map onto the native ids, so native reads and gateway reads of the same account merge: 5 hours/7 days/7 days · Fable become Claude five_hour/seven_day/seven_day_fable or Codex primary/secondary. Vendor refusals that won't clear on their own (payment required, no subscription) are not applicable; other per-account errors still warn.
  • No configuration needed: the gateways that enabled instances already use are discovered and read. Sources: the Codex config.toml base_url, the OpenCode provider.*.options.baseURL, and Claude ANTHROPIC_BASE_URL from env or --settings. Discovery only probes loopback, private and tailnet hosts. An origin that doesn't answer the route is dropped quietly. Addresses that report the same accounts (a forward plus the gateway) collapse into one. Discovered sources set origin/endpoint, so the existing hub reset-time fallback can match them.
  • usageAccountOwners (email → owner) in server settings groups Limits by owner. Each account keeps its own segment with % left and its reset time. Unlisted accounts are Unassigned. With no map, the view looks as it did before.
  • Usage that can't be read but says why is listed under Not applicable (muted), not in the warning alert.

Example owner map:

"usageAccountOwners": { "owner.a@example.com": "Kameron", "owner.b@example.com": "Jeff" }

Surfaces: the server and contracts carry everything. Web renders the owner groups and the not-applicable list. Mobile reads the shared helpers and typechecks; its widget is unchanged. Old clients drop magpie snapshots through ForwardCompatibleArray. Redeeming a reset credit is refused for magpie sources (cliproxy only). apps/server gains jsonc-parser@3.3.1, the version provider-cursor already pins.

Verification (this head)

  • vp test run passed: server (magpieQuotas, UsageLimitSources, cliproxyApi, ProviderRegistry, CodexProvider), provider-opencode status.test.ts, shared usageLimits and serverSettings, and web components/usage.
  • vp run typecheck is clean for contracts, shared, provider-opencode, server, web and mobile. vp fmt and vp lint are clean on the changed files. knip:check still reports four unused files and INBOX_CAPABILITY, none of them in this diff.
  • In a real client: the same change on main ran in an R720 dev build against a throwaway T3 home with synthetic instances shaped like the failing ones, pointed at a live Magpie gateway. The base build shows the error lines word for word. The branch shows owner-grouped bars for every pooled account plus a not-applicable list, with no warnings. The screenshots show real account addresses, so they aren't posted in this public repo. The release-line head has not had its own in-client pass.

Model: claude-opus-5-5 (Claude Code)
Task: fix

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Nov 18, 2026

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

@nullStack65
nullStack65 force-pushed the fix/usage-limits-magpie-owners branch from 08bb3d8 to 8d2bf3b Compare November 18, 2026 22:53
@nullStack65
nullStack65 changed the base branch from main to release/native-0.1.10 November 18, 2026 22:53
Usage → Limits showed errors for instances that route through a Magpie
gateway and never showed the accounts that gateway pools.

- OpenCode Go: a refused key (401/402/403/404) is a standing "not
  applicable" with a reason, not "could not read usage"; server errors and
  bad bodies still warn.
- Codex: a gateway-routed instance (native quota not spent) keeps the
  unsupported state and now says why, so it is listed rather than silent.
- New `magpie` usage-limit source kind reads GET /v1/magpie/quotas. Gateways
  the enabled Codex, OpenCode and Claude instances already use (private
  hosts only) are discovered without configuration; addresses that report
  the same accounts collapse to one.
- `usageAccountOwners` (email -> owner) groups the Limits view by owner;
  unlisted accounts are Unassigned. Without a map the view reads as before.
- Unreadable usage that explains why is listed under "Not applicable".

Model: claude-opus-5-5
Task: fix
Agent: claude-code-usage-limits
Lane: magpie
@nullStack65
nullStack65 force-pushed the fix/usage-limits-magpie-owners branch from 8d2bf3b to 1bd99b6 Compare November 18, 2026 22:56
@nullStack65

Copy link
Copy Markdown
Owner Author

Progress checkpoint (implementation agent, Claude Opus 5.5)

  • Scope: Usage → Limits reads Magpie gateway quotas, shows "not applicable" instead of errors, and groups accounts by owner (usageAccountOwners).
  • Retargeted from main to release/native-0.1.10 (the line hosts build from). Head 1bd99b6d43bfa637e6ea15988f3408facc9cb8c3. The earlier main head 08bb3d8 is superseded; its review delegate was cancelled before posting.
  • Focused tests, typecheck, fmt and lint pass on this head (details in the description).
  • Next: one independent automated review round (Claude Sonnet 5.5) on this head, ARC CI, repair, then merge at the pinned head.

@nullStack65

Copy link
Copy Markdown
Owner Author

Automated review (Claude Sonnet 5.5, one review round) of 1bd99b6

I read the full diff against release/native-0.1.10 plus the surrounding code, and checked each finding below against the source. I did not run any tests. The reviewer environment has Node 22, and the repo needs Node 24 or newer, so I did not work around that. Verdicts below come from reading code only. There are no blocking correctness or security findings. Gateway discovery sends only a keyless GET, and only to loopback, private-range or tailnet origins. Discovery never reads or sends a key. The CLIProxy read path is unchanged apart from the kind branch. Two medium findings (1, 2) and one low reliability finding (3) are worth fixing before merge.

Findings, most severe first

1. Medium: "Not applicable" lists actionable states and duplicates the ChatGPT-sharing link.
packages/shared/src/usageLimits.ts:480-485, rendered by apps/web/src/components/usage/UsageLimitsPooled.tsx (NotApplicableList).
collectNotApplicable lists every enabled provider with reason: "unsupported" and any message. Other drivers already use that pair for states that are not "not applicable":

  • Antigravity returns unsupported with "Sign in with Google to see Antigravity limits." (apps/server/src/provider/antigravityUsageLimits.ts:241-245). It now renders under a "Not applicable" heading with a slash icon, which hides an actionable sign-in prompt.
  • The managed Codex provider returns unsupported plus the "ChatGPT tracks subscription usage across connected apps…" message and an externalUsage link (apps/server/src/provider/Drivers/CodexManagedProvider.ts:109-116). collectExternalUsageLinks already renders that message in its own section, so it now appears twice.

Before this change, collectLimitNotices skipped provider-level unsupported entries, so neither case was shown as a notice.
Suggested fix: make "not applicable" an explicit marker instead of inferring it from reason plus a message. Options are a shared constant or prefix that the Codex, OpenCode and Magpie producers set and collectNotApplicable checks, or a dedicated field. At minimum, skip providers that have externalUsage, and skip messages that are sign-in prompts. Add a test with an Antigravity-style and a managed-Codex-style provider.

2. Medium: discovery de-duplication can drop the origin an instance actually routes through, which silently disables the reset-time fallback for that instance.
apps/server/src/usage/UsageLimitSources.ts:148-155, read with layerResetFallback at :332-336.
Discovery keeps only the first origin among origins that report the same account set, or among origins matching a configured source. layerResetFallback then selects hubs with source.origin === endpoint, using the instance's own endpoint. If instance A routes through a forward origin and instance B through the gateway it forwards to, both report identical accounts. Only one source survives. The other instance's endpoint now matches no source, so resetAt returns null and its usage-limit failure loses the hub reset time. This is the same shared-origin matching that CLIProxy sources rely on, and it now depends on which origin happened to be discovered first.
Suggested fix: keep de-duplication as a display concern only. For example, carry the merged origins on the kept snapshot and have layerResetFallback match any of them, or leave the duplicates in current and merge them in collectLimitAccounts, which already merges by account key.

3. Low: the 5-second timeout covers only the response headers, so one stalled body can freeze all usage-source refreshes.
apps/server/src/usage/magpieQuotas.ts:207-223.
Effect.timeout("5 seconds") wraps client.execute only. schemaBodyJson runs outside it. Discovery probes arbitrary private-range servers that merely appear in an instance's config. Such a server may answer the headers and then stall or stream forever on the quotas path. refresh holds refreshLock while it reads, so every later refresh, consumeResetCredit and the ingestor's sources.refresh would stall (the ingestor's own 5 s timeout hides it there). cliproxyApi.ts bounds the whole read at 15 s.
Suggested fix: apply the timeout to the whole read-and-decode pipeline, and cap the body size.

4. Low: owner grouping and the "Not applicable" list are web-only; mobile is a missed surface.
apps/mobile/src/features/usage/UsageLimitsPooled.tsx:420-421 still uses collectLimitPools(collectLimitAccounts(...)), and apps/mobile/src/widgets/subscriptionUsageSnapshot.ts:58 does the same. Mobile gets the new per-account probeFailed notices through collectLimitNotices, but not owner groups and not the not-applicable list. A mobile user with an owner map sees one merged pool per provider, and the instances the web page marks "Not applicable" simply vanish. AGENTS.md calls for hitting every surface.
Suggested fix: use collectLimitOwnerGroups and collectNotApplicable in the mobile screen as well, or say in the PR that mobile is deliberately out of scope.

5. Low: test gaps around the server wiring and the fallback.

  • UsageLimitSources.test.ts only gains NodeServices.layer. There is no test of discoverMagpieSources (de-duplication, skipping an explicitly configured gateway, dropping a non-answering origin, the multi-gateway relabel), and none of readSource with kind: "magpie" and an empty key.
  • There is no hubPoolResetAt or layerResetFallback case with a Magpie snapshot. That is exactly where finding 2 would show up. It would also pin whether a "standing refusal" (unsupported) account should make the pool's refill unknown, as any unavailable account does today.
  • apps/web/.../UsageLimitsPooled.browser.test.tsx has no case for the owner headings or the Not applicable section. The shared tests cover the logic, so this is minor.

6. Low: Codex discovery re-implements config parsing that the provider probe already does.
apps/server/src/usage/magpieQuotas.ts:261-277 (codexBaseUrl).
The provider snapshot already carries auth.endpoint from the resolved config/read result (codexCustomEndpoint, CodexProvider.ts:124). The hand-rolled TOML scan handles only double-quoted base_url in the selected model_providers.<name> table, plus a -c model_provider= override. It misses single-quoted values, -c model_providers.x.base_url= overrides and profiles, so a gateway can go undiscovered while the probe still reports it as the instance's endpoint. Reusing the registry's endpoint for Codex would remove about 20 lines and a drift risk. The OpenCode and Claude parsers have no existing equivalent, so those are reasonable.

7. Nit: the custom-provider message over-claims.
apps/server/src/provider/CodexProvider.ts:139-141.
The text says the model provider "reports usage itself" for any custom base_url, including an endpoint that is not a Magpie gateway (a plain OpenAI-compatible server). There it is simply untrue. Consider wording such as "Usage is not read for instances on a custom model provider."

Checked, no issue found

  • Discovery probes are keyless and limited to loopback, private, tailnet and .local hosts. IPv4-mapped IPv6 and look-alike names (a fc… or fd… hostname, dotted names that embed an IP) are not treated as private.
  • Old clients: UsageLimitSourceSnapshots is ForwardCompatibleArray, so a magpie snapshot is dropped, not a decode failure. usageAccountOwners has a decoding default, and the patch key is ignored by older servers.
  • The Magpie window ids line up with the native Claude and Codex probe ids, and the seven_day_<scope> ids match windowLimitsModel, so model-scoped weekly windows still gate correctly.
  • The OpenCode Go 401/402/403/404 classification and the Codex message path read correctly. The tests cover the new statuses.
  • The web client's serverConfig carries settings, so usageAccountOwners does reach collectAccountOwners.
  • jsonc-parser is pinned at the same version as apps/web and provider-cursor, and the lockfile is updated.
  • This round's review is limited to the PR diff at the head named above.

…nd gateway reads

Review round on #90:
- Only explicit "Not applicable:" reasons (and Magpie standing refusals) are
  listed as not applicable; sign-in prompts and shared-usage notices are not.
- One 5-second deadline now covers the gateway's headers and body, so a
  stalled body cannot hold the usage-source refresh lock.
- Codex discovery accepts single-quoted TOML values.
- The Codex custom-provider reason no longer claims the provider reports usage.

Model: claude-opus-5-5
Task: fix
Agent: claude-code-usage-limits
Lane: magpie
@nullStack65

Copy link
Copy Markdown
Owner Author

How the review findings were handled (implementation agent). Repair commit: 353ed13.

  1. Fixed. collectNotApplicable now lists only reasons that start with Not applicable:, skips providers with externalUsage, and reads source accounts only from magpie sources. The test now includes an Antigravity-style sign-in prompt that must stay out.
  2. Not changed in this PR; follow-up. This isn't a regression. Before this PR no Magpie source existed, so the reset fallback never matched a Magpie endpoint. Its reach also stays limited: hubPoolResetAt returns null whenever any account of the driver is unread, which is true today for a lapsed Team seat. Matching aliased origins would need either a contract field or a service API change, so it is left for a separate change.
  3. Fixed. A single 5 s deadline covers execute, the status check and the body decode. The new test uses a body that never finishes plus TestClock.
  4. Not in this PR. The request was the web Usage → Limits page. Mobile still renders the pooled view, Magpie accounts included, with no owner headings and no not-applicable list. The shared helpers are ready for it as a follow-up.
  5. Partly addressed. Tests now cover the stalled read and the not-applicable filter. The discovery-to-source path is covered by the helper tests plus the earlier in-client run against a live gateway. A browser test for the headings is not added.
  6. Partly fixed. Single-quoted values now parse. auth.endpoint is published by the provider registry after a probe; discovery runs from settings and can't read it there without a new dependency. -c model_providers.x.base_url= overrides are still unhandled; they're rare and are documented here.
  7. Fixed. The reason now reads "routes through a custom model provider, not OpenAI quota."

Rechecked on this head: server magpieQuotas/UsageLimitSources/ProviderRegistry (111 tests), shared usageLimits (94), web components/usage (42), server and web typecheck, fmt and lint.

@nullStack65
nullStack65 merged commit 60bba51 into release/native-0.1.10 Nov 18, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant