Skip to content
This repository was archived by the owner on Jul 30, 2026. It is now read-only.

fix(usage): separate subscription refresh from local refresh - #81

Merged
makoMakoGo merged 5 commits into
personal/local-clientsfrom
fix/subscription-usage-explicit-refresh
Jun 26, 2026
Merged

fix(usage): separate subscription refresh from local refresh#81
makoMakoGo merged 5 commits into
personal/local-clientsfrom
fix/subscription-usage-explicit-refresh

Conversation

@makoMakoGo

@makoMakoGo makoMakoGo commented Jun 25, 2026

Copy link
Copy Markdown
Owner

What changed

  • Added explicit TUI subscription provider selection via usageProviders.
  • Kept an empty usageProviders list as cache-display mode: the Usage tab may show cached data, but it will not send remote subscription requests.
  • Kept global r scoped to local report refreshes; subscription refresh is u in the Usage tab only.
  • Stopped subscription auto-polling; each TUI session may fetch once on first Usage-tab entry when enabled providers are configured.
  • Made explicit TUI provider selections report missing credentials instead of silently collapsing into "no data".
  • Removed the generic MiniMax usage provider; MiniMax subscription usage is only minimax-token-plan-cn or minimax-token-plan-global.
  • Added Kimi Code API key support via TOKSCALE_USAGE_KIMI_CODING_PLAN_API_KEY, while retaining Kimi Code OAuth credentials.
  • Updated the Usage footer/status text so Usage no longer reuses local-report refresh time.
  • Updated README variants and added ADR 0014 for the local-report vs remote-subscription boundary.

Provider credential policy

Purpose-specific subscription credentials only:

  • Z.ai/Zhipu GLM Coding Plan: TOKSCALE_USAGE_ZAI_CODING_PLAN_API_KEY
  • Kimi Code: TOKSCALE_USAGE_KIMI_CODING_PLAN_API_KEY or Kimi Code OAuth credentials
  • MiniMax CN Token Plan: TOKSCALE_USAGE_MINIMAX_TOKEN_PLAN_CN_KEY
  • MiniMax Global Token Plan: TOKSCALE_USAGE_MINIMAX_TOKEN_PLAN_GLOBAL_KEY

General provider API keys such as ZAI_API_KEY, GLM_API_KEY, KIMI_API_KEY, MINIMAX_API_KEY, and MINIMAX_API_TOKEN are intentionally ignored for subscription quota lookups.

Validation

  • cargo fmt --all -- --check
  • cargo test -p tokscale-cli usage -- --nocapture
  • cargo test -p tokscale-cli footer -- --nocapture
  • cargo test -p tokscale-cli app::tests::test_usage -- --nocapture
  • cargo test -p tokscale-cli
  • cargo test

Closes #80

Summary by CodeRabbit

  • 新功能

    • 新增订阅使用量(Usage)相关说明与配置选项,支持启用 Usage 标签并选择可用提供方。
    • 补充 TUI 快捷键说明,明确 urR 在 Usage 与本地报告中的不同刷新行为。
  • 文档

    • 更新多语言 README,完善订阅额度、支持的 provider、环境变量与示例配置说明。
    • 新增一份 ADR,说明订阅使用量远程请求的边界与使用规则。

@coderabbitai

coderabbitai Bot commented Jun 25, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@makoMakoGo, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 38 minutes and 6 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 492ef8d8-57e5-4735-aa03-5e6ff446e310

📥 Commits

Reviewing files that changed from the base of the PR and between 1808a3b and 9e8072a.

📒 Files selected for processing (5)
  • crates/tokscale-cli/src/commands/usage/kimi.rs
  • crates/tokscale-cli/src/commands/usage/mod.rs
  • crates/tokscale-cli/src/tui/app.rs
  • crates/tokscale-cli/src/tui/ui/footer.rs
  • crates/tokscale-cli/src/tui/ui/usage.rs

Walkthrough

本 PR 将订阅额度获取从本地报表刷新中拆分出来,新增 Usage Tab 的 provider 白名单、专用环境变量与刷新语义,并同步更新 Kimi、Z.ai、MiniMax Token Plan 的凭据读取、TUI 状态/帮助行、以及多语言 README 与 ADR。

Changes

订阅额度边界与提供方流程

Layer / File(s) Summary
Provider IDs and credentials
crates/tokscale-cli/src/commands/usage/{helpers.rs,mod.rs,zai.rs,minimax_tokenplan.rs}, crates/tokscale-cli/src/tui/settings.rs
新增 provider ID 归一化、usage_providers 设置和 read_first_env;Z.ai 与 MiniMax Token Plan 的订阅凭据改为读取 TOKSCALE_USAGE_* 环境变量,并补了相关默认值与解析测试。
Kimi Code provider
crates/tokscale-cli/src/commands/usage/kimi.rs
Kimi Code 的 API key、OAuth 设备头、usage 解析、配额标签生成和 fetch/retry 流程都改为新的订阅额度形态,并更新了对应测试。
Provider fan-out
crates/tokscale-cli/src/commands/usage/mod.rs
重构 provider 列表为 UsageProvider,按启用列表筛选后并发抓取,并把 outputs 与错误汇总进 UsageFetchBatch
App state and wiring
crates/tokscale-cli/src/tui/app.rs
App 记录启用的订阅 provider 和订阅检查时间,并把初始化、切 tab、r/u 和自动刷新路径改为区分本地报表刷新与订阅 usage 入口。
Subscription fetch lifecycle
crates/tokscale-cli/src/tui/app.rs
fetch_subscription_usageon_tick 和初次进入 Usage 的门控逻辑改为按 provider 启用状态执行,并补了完成、并发和无 provider 场景的测试。
Footer help and status
crates/tokscale-cli/src/tui/ui/footer.rs
Usage 页脚改为按 provider 启用状态、自动刷新状态和最近检查时间生成帮助行与状态行,并更新相关测试。
Usage panel messages
crates/tokscale-cli/src/tui/ui/usage.rs
Usage 面板把 loading/empty/cached 文案抽成辅助函数,并在没有启用 provider 且仍有缓存时插入缓存提示行。
Docs and ADR
README*.md, docs/adr/0014-explicit-subscription-usage-boundary.md
各语言 README 和 ADR 记录了 Subscription Usage 的 provider ID、专用环境变量、配置项、按键语义和边界说明。

Sequence Diagram(s)

sequenceDiagram
  participant App
  participant FetchEnabled as fetch_enabled
  participant FetchProviders as fetch_providers
  participant UsageProvider
  App->>FetchEnabled: 启用的 provider IDs
  FetchEnabled->>FetchProviders: 选中的 providers
  FetchProviders->>UsageProvider: fetch()
  UsageProvider-->>FetchProviders: outputs / error
  FetchProviders-->>App: UsageFetchBatch
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • makoMakoGo/tokscale#57: 同样修改了 crates/tokscale-cli/src/tui/app.rs 的 tab 处理与切换逻辑,和这次 Usage tab 的门控改动直接相关。

Poem

我蹦进 Usage 的草坡边,
u 一点,星光就亮在指尖。
r 只刷本地叶,不惊云烟,
provider 小门牌,乖乖排成线。
啾啾,额度与缓存都在春天。

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 42.06% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了本次将订阅刷新与本地刷新分离的核心改动。
Linked Issues check ✅ Passed 实现了显式 provider 白名单、r/u 语义分离、禁用自动轮询,并拒绝通用环境变量,符合 #80 要求。
Out of Scope Changes check ✅ Passed 未见明显与 #80 目标无关的代码变更,改动主要集中在 usage 逻辑、配置和文档。
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/subscription-usage-explicit-refresh

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

Comment @coderabbitai help to get the list of available commands.

@codspeed-hq

codspeed-hq Bot commented Jun 25, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 18 untouched benchmarks


Comparing fix/subscription-usage-explicit-refresh (9e8072a) with personal/local-clients (366d21b)1

Open in CodSpeed

Footnotes

  1. No successful run was found on personal/local-clients (f23c2be) during the generation of this report, so 366d21b was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@makoMakoGo
makoMakoGo force-pushed the fix/subscription-usage-explicit-refresh branch 2 times, most recently from 942ba1e to 7aa540e Compare June 25, 2026 17:17
Require the TUI Usage tab to opt into subscription providers before fetching remote quota data. Keep global r scoped to local report refreshes and use u for Usage-tab subscription refreshes.

Closes #80
@makoMakoGo
makoMakoGo force-pushed the fix/subscription-usage-explicit-refresh branch from 7aa540e to 1a9af1b Compare June 25, 2026 23:07
@makoMakoGo
makoMakoGo marked this pull request as ready for review June 26, 2026 00:12

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/tokscale-cli/src/commands/usage/kimi.rs`:
- Around line 389-394: The refresh flow in kimi.rs only persists credentials
when refreshed.refresh_token is present, so an access token returned with
expires_in but no new refresh token is never saved. Update the refresh handling
around the stored_refresh_token and save_credentials logic to fall back to the
existing stored_refresh_token when refreshed.refresh_token is None, and still
persist the new access_token and expires_in. Ensure the code path that handles
token refresh keeps the old refresh token instead of requiring a new one every
time.

In `@crates/tokscale-cli/src/commands/usage/mod.rs`:
- Around line 89-90: `UsageProviderId` is missing the explicit `kimi-code` alias
for the `Kimi` provider, so configs using that name are not recognized. Update
the provider matching in `UsageProviderId`’s parsing logic to accept both `kimi`
and `kimi-code` as `Self::Kimi`, keeping the existing alias handling style used
for the other provider IDs.

In `@crates/tokscale-cli/src/tui/app.rs`:
- Around line 527-533: `App::new_with_cached_data()` is still loading
`Settings::load()` and then immediately calling
`maybe_fetch_subscription_usage_on_usage_entry()`, so Usage-tab tests can pick
up developer-local `usageProviders` and trigger real fetch behavior. Update the
`App` construction path to use isolated test settings for `make_usage_app()` and
`make_app_on()` in `usage.rs` and `footer.rs`, or guard the initial auto-fetch
inside `App::new_with_cached_data()` with `#[cfg(test)]` so tests do not depend
on local configuration or spawn subscription fetch threads.

In `@crates/tokscale-cli/src/tui/ui/footer.rs`:
- Around line 477-488: Replace the substring-based filtering in
subscription_status_message with an explicit subscription-only check instead of
matching any status containing “usage” or “subscription.” Update the App status
handling so usage-related messages like “Jumped to today’s usage” and “Returned
to daily usage” are not reused by usage_status_row_line; ideally add a dedicated
subscription status field or a strict whitelist of known subscription messages.
Use the subscription_status_message helper and the usage_status_row_line call
site to locate and narrow this logic.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 02b05219-6cd1-46cb-bd25-e2e00ddd7373

📥 Commits

Reviewing files that changed from the base of the PR and between f23c2be and 1808a3b.

📒 Files selected for processing (15)
  • README.ja.md
  • README.ko.md
  • README.md
  • README.zh-cn.md
  • crates/tokscale-cli/src/commands/usage/helpers.rs
  • crates/tokscale-cli/src/commands/usage/kimi.rs
  • crates/tokscale-cli/src/commands/usage/minimax.rs
  • crates/tokscale-cli/src/commands/usage/minimax_tokenplan.rs
  • crates/tokscale-cli/src/commands/usage/mod.rs
  • crates/tokscale-cli/src/commands/usage/zai.rs
  • crates/tokscale-cli/src/tui/app.rs
  • crates/tokscale-cli/src/tui/settings.rs
  • crates/tokscale-cli/src/tui/ui/footer.rs
  • crates/tokscale-cli/src/tui/ui/usage.rs
  • docs/adr/0014-explicit-subscription-usage-boundary.md
💤 Files with no reviewable changes (1)
  • crates/tokscale-cli/src/commands/usage/minimax.rs

Comment thread crates/tokscale-cli/src/commands/usage/kimi.rs Outdated
Comment thread crates/tokscale-cli/src/commands/usage/mod.rs Outdated
Comment thread crates/tokscale-cli/src/tui/app.rs
Comment thread crates/tokscale-cli/src/tui/ui/footer.rs

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

7 issues found across 15 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="crates/tokscale-cli/src/tui/ui/footer.rs">

<violation number="1" location="crates/tokscale-cli/src/tui/ui/footer.rs:237">
P3: Usage help row omits the `e` export shortcut even though the key remains active. Footer guidance is inconsistent with actual keybindings.</violation>
</file>

<file name="crates/tokscale-cli/src/tui/ui/usage.rs">

<violation number="1" location="crates/tokscale-cli/src/tui/ui/usage.rs:96">
P2: Usage loading text is keyed off local-report loading, so it can falsely claim subscription fetch is in progress. This breaks the new local-vs-subscription separation and misleads users in cache-display mode.</violation>
</file>

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Re-trigger cubic

Comment thread crates/tokscale-cli/src/tui/ui/footer.rs
Comment thread crates/tokscale-cli/src/tui/ui/usage.rs Outdated
Comment thread crates/tokscale-cli/src/tui/ui/usage.rs Outdated
Comment thread crates/tokscale-cli/src/commands/usage/kimi.rs Outdated
Comment thread crates/tokscale-cli/src/commands/usage/mod.rs Outdated
Comment thread crates/tokscale-cli/src/tui/ui/footer.rs Outdated
Comment thread crates/tokscale-cli/src/tui/ui/footer.rs Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 5 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread crates/tokscale-cli/src/tui/app.rs Outdated
@makoMakoGo
makoMakoGo merged commit f616afc into personal/local-clients Jun 26, 2026
9 checks passed
@makoMakoGo
makoMakoGo deleted the fix/subscription-usage-explicit-refresh branch June 26, 2026 08:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Separate subscription quota refresh from local report refresh

1 participant