Skip to content

feat(mobile): show all usage providers on mobile with a per-provider visibility filter - #8571

Closed
audichuang wants to merge 3 commits into
stablyai:mainfrom
audichuang:feat/mobile-provider-usage
Closed

audichuang wants to merge 3 commits into
stablyai:mainfrom
audichuang:feat/mobile-provider-usage

Conversation

@audichuang

Copy link
Copy Markdown
Contributor

Summary

Mobile only showed Claude and Codex usage, even though the host already streams the full RateLimitState for every provider in the accounts.list / accounts.subscribe snapshot. This PR surfaces the rest — Gemini, OpenCode Go, Kimi, MiniMax, Grok — on mobile, plus a per-provider visibility filter under Settings → Account usage (defaults to Claude + Codex only; the others are opt-in so the phone isn't flooded).

Pure client change — no backend or protocol change. Highlights:

  • Splits ManagedAccountProviderKey (switchable: Claude/Codex) from UsageProviderKey (display-only), so a display-only provider can never route into account-switching selectors.
  • A canonical provider descriptor maps the stable wire id to its snapshot field and label (resolving the opencode-go / opencodeGo mismatch); new snapshot fields are optional and normalized with ?? null, so an old cached snapshot stays valid.
  • getProviderUsageWindows renders each provider's real windows (Gemini named buckets, OpenCode Go monthly, others session/weekly) instead of two hardcoded 5h/7d bars.

Bug fixed along the way: visibility toggle could drop an opted-in provider

Problem — On the settings screen, a toggle fired before the stored visibility set finished loading persisted the whole set against the default {claude, codex} base, silently dropping any provider that lived only in the stored set (e.g. an opted-in Grok).

Root cause — The screen started on the default set while the stored set loaded asynchronously; the pre-load toggle wrote that stale default plus the one change.

Fix

  • Gate the switches until the stored set loads, so no toggle can persist against the default base.
  • Move persistence to a serialized read-modify-write in preferences.ts (setUsageProviderVisible): each toggle re-reads the latest stored set and changes only its own provider, so concurrent toggles can't clobber each other and the write survives an unmount.
  • Load once on mount via a settled read (the screen is the sole writer), so a focus-reload can't overwrite the optimistic UI with a pre-write snapshot.
  • Distinguish an I/O read failure (abort the write — don't clobber the stored set with the default) from corrupt content (missing / malformed JSON / non-array → re-base on the default and self-heal).

Follow-up review fixes: the Home usage card now keeps a display-only provider visible while fetching/on error (matching the accounts screen) instead of hiding it; each visibility Switch gets an accessibilityLabel; UsageBar treats a non-finite percent as no data so it never renders width: "NaN%".

Screenshots

Mobile UI change (new Settings → Account usage filter screen; Home card and per-host accounts screen now list opted-in providers). Before/after captures to be added from a device/simulator — no desktop-renderable surface.

Testing

  • pnpm typecheck — clean on all changed files (tsc --noEmit -p mobile/tsconfig.json)
  • pnpm lint — changed files pass oxlint + oxfmt via the husky pre-commit hook; full-repo lint not run
  • pnpm test — ran the focused suites below (49 passed); full suite not run
  • pnpm build — not run locally; relying on CI
  • Added regression tests that fail without the fix (verified by reverting each fix)

Focused tests (from mobile/):

pnpm exec vitest run src/storage/preferences.test.ts src/components/account-usage-state.test.ts

New tests cover: a stored-only provider (Grok) survives a pre-load toggle; concurrent toggles serialize without loss; an I/O read failure aborts the write and leaves the stored set intact; malformed stored JSON self-heals. Each was confirmed to fail before its fix.

AI Review Report

Reviewed with Codex (GPT-5.6) across several independent adversarial passes:

  • Data flow — Verified the "pure client" premise end to end: the host's getAccountsSnapshot() returns rateLimits.getState() whole (Grok included) and accounts.subscribe broadcasts it unmodified, so no host/protocol change is needed.
  • Concurrency / lifecycle — Iterated on the settings-screen load/toggle race until the load-gate + serialized read-modify-write closed every flagged path (pre-load overwrite, unmount loss, focus late-load overwrite, I/O-failure clobber, malformed-JSON lockout).
  • UI consistency & a11y — Flagged and fixed a Home-vs-accounts display divergence for fetching/error display-only providers, a missing switch accessibilityLabel, and a NaN% bar-width edge case.
  • Cross-platform — This is React Native mobile code with no OS-specific branches, shortcuts, labels, or path handling; nothing Electron/macOS/Linux/Windows-specific is touched.

Security Audit

No new input parsing from untrusted sources, no command execution, no path handling, no secrets, and no new IPC/RPC surface. Visibility preferences are stored locally in AsyncStorage; the usage data already arrives in the existing accounts snapshot. Stored preferences are defensively validated (unknown ids filtered, corrupt content normalized).

Notes

  • Related to feat(mobile): show usage reset countdown on accounts screen #7954 (open — adds a usage reset countdown on the accounts screen). Different goal (that adds a countdown row; this adds providers + a visibility filter), but both touch AccountUsage.tsx / account-usage-state.ts, so expect a small rebase if one lands first. No functional overlap.
  • No existing mobile issue to Closes; the desktop provider-usage work that populated the host RateLimitState is already merged, so this is purely the mobile client catching up.
  • Known trade-off: the optimistic toggle swallows a storage-write failure (best-effort, matching the app's existing preference-save convention) rather than rolling back the UI.

桌面已支援 Claude/Codex/Gemini/OpenCode Go/Kimi/MiniMax/Grok 的用量顯示,
行動端卻只顯示 Claude+Codex。此變更讓行動端顯示全部 provider,並於
Settings → Account usage 提供每個 provider 的開關(預設只開 Claude+Codex,
其餘 opt-in,避免手機一次塞滿)。

- 純 client 變更:host 已在 accounts.list / accounts.subscribe 的 snapshot
  送出完整 RateLimitState,零後端 / 協定改動。
- 拆 ManagedAccountProviderKey(可切換)vs UsageProviderKey(唯讀顯示);
  display-only provider 走不可點路徑,不會觸發帳號切換。
- 以 canonical descriptor 對應 wire id 與 snapshot 欄位(解 opencode-go /
  opencodeGo 不一致);新欄位為 optional 並以 ?? null 正規化舊 v1 快取。
- getProviderUsageWindows 依實際視窗渲染:Gemini 具名 buckets、OpenCode Go
  monthly、其餘 session/weekly,不再硬寫 5h/7d。
- 可見性偏好存於本地 AsyncStorage,沿用既有 focus-reload 慣例。
- home 卡片抽成 HomeAccountUsageCard 以符合 max-lines。
… load

The Account-usage settings screen started on the default set {claude,codex}
while the stored visibility set loaded asynchronously. A toggle fired before
that load resolved persisted the whole set against the stale default, silently
dropping any provider that lived only in the stored set (e.g. an opted-in
Grok).

- Gate the switches until the stored set loads, so no toggle can persist
  against the default base.
- Move persistence to a serialized read-modify-write in preferences
  (`setUsageProviderVisible`): each toggle re-reads the latest stored set and
  changes only its own provider, so concurrent toggles can't clobber each
  other and the write survives an unmount.
- Load once on mount via a settled read (the screen is the sole writer), so a
  focus-reload can't overwrite the optimistic UI with a pre-write snapshot.
- Distinguish an I/O read failure (abort the write) from corrupt content
  (missing / malformed JSON / non-array → re-base on the default and self-heal).

Tests cover: stored-only provider survives a pre-load toggle, concurrent
toggles serialize, I/O read failure aborts the write, malformed JSON self-heals.
…11y, guard NaN

Review follow-ups on the multi-provider usage feature:
- The Home usage card now keeps a display-only provider visible while it is
  fetching or in error (spinner / last bars) instead of dropping it, matching
  the accounts screen and making the previously-unreachable fetching branch
  reachable.
- Each visibility Switch gets an accessibilityLabel so a screen reader names
  the provider being toggled.
- UsageBar treats a non-finite percent (NaN/Infinity) as no data so it never
  renders `width: "NaN%"`.
@audichuang

Copy link
Copy Markdown
Contributor Author

Superseded by #9274 — thanks @bbingz for picking this up and carrying it forward with the upstream rebase. Closing this in favour of that PR.

@audichuang audichuang closed this Jul 18, 2026
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.

2 participants