Skip to content

fix(providers): live Token Plan quota + configurable fallback for xiaomi-mimo (#15753) - #15759

Merged
diegosouzapw merged 1 commit into
diegosouzapw:release/v3.8.52from
LazyGatto:fix/xiaomi-mimo-live-token-plan-quota
Oct 8, 2026
Merged

diegosouzapw merged 1 commit into
diegosouzapw:release/v3.8.52from
LazyGatto:fix/xiaomi-mimo-live-token-plan-quota

Conversation

@LazyGatto

Copy link
Copy Markdown
Contributor

Summary

Closes #15753.

For xiaomi-mimo, the quota adapter compared locally routed tokens against a hardcoded 4_100_000_000 — the Lite tier's monthly Credits — so after upgrading the plan (Lite → Pro) the connection stayed remainingPercentage: 0 / Exhausted until the calendar-month reset, while the real plan sits at 38%.

This PR adds the two layers proposed in the issue:

  1. Live console fetch (preferred): with a MiMo console session cookie on the connection, the fetcher reads the real plan usage and window from the console API.
  2. Configurable self-track fallback: the monthly budget becomes providerSpecificData.monthlyTokenLimit → XIAOMI_MIMO_MONTHLY_TOKEN_LIMIT → historical 4.1e9.

Changes

  • open-sse/services/usage/xiaomi-mimo.ts — live fetch of GET https://platform.xiaomimimo.com/api/v1/tokenPlan/usage + /tokenPlan/detail (8s timeout, shared throttleQuotaFetch): quotas.monthly from the month_total_token item (used/limit in Credits), plan label like Xiaomi MiMo Token Plan · Pro, resetAt from currentPeriodEnd (parsed deterministically as UTC — the value is a naive wall time). Session rejected (401/403, code !== 0, SSO loginUrl, non-JSON body) or cookie missing ⇒ a hint message plus the self-tracked numbers, so the dashboard keeps working instead of failing hard. Without a cookie the previous self-track behavior is unchanged except that the budget is now configurable.
  • open-sse/services/usage.ts — dispatch now forwards providerSpecificData to getXiaomiMimoUsage for xiaomi-mimo / xiaomi-mimo-token-plan.
  • Connection modal (quotaScrapingFieldValues.ts, QuotaScrapingFields.tsx, EditConnectionModal.tsx) — new xiaomiMimoConsoleCookie field mirroring the existing volc/alibaba/qwen console-cookie fields (blank values never overwrite an existing secret).
  • src/shared/validation/providerSpecificData.ts — validation for xiaomiMimoConsoleCookie (string ≤ 10k) and monthlyTokenLimit (positive number).
  • src/lib/providers/requestDefaults.ts, src/lib/compliance/providerAudit.ts — cookie stripped from API responses and audit summaries.
  • .env.example, docs/reference/ENVIRONMENT.md — XIAOMI_MIMO_CONSOLE_COOKIE and XIAOMI_MIMO_MONTHLY_TOKEN_LIMIT documented (with instructions for obtaining the cookie).
  • changelog.d/fixes/15753-xiaomi-mimo-live-token-plan-quota.md — changelog fragment.

Tests

  • tests/unit/xiaomi-mimo-live-quota-15753.test.ts — live payload → numbers/plan/resetAt and cookie forwarding; dispatch wiring; 401 + loginUrl ⇒ hint + fallback; cookie missing ⇒ hint + default 4.1B; providerSpecificData override; env override (psd wins); env cookie.
  • tests/unit/xiaomi-mimo-cookie-field.test.ts — assign/blank/partial + schema validation (mirrors the qwen cookie-field test).

Run with the repo runner (node --import tsx/esm --import ./open-sse/utils/setupPolyfill.ts --import ./tests/_setup/isolateDataDir.ts --test):

  • new + adjacent suites (live-quota, cookie-field, selftrack, split, console-cookie-sanitization, request-defaults, fix(providers): add xiaomi-mimo to NAMED_OPENAI_STYLE_PROVIDERS for live model discovery #15307): 34/34 pass
  • usage-service-hardening, qwen-token-plan-quota-fetcher, volc-cookie/request-sanitization, provider-specific-data-schema: 67 pass
  • xiaomi-registry/dispatcher-probe, quota-share-bugfixes: 39 pass
  • eslint on changed files, check:complexity, check:file-size, check:dead-code: green
  • typecheck:* / check:env-doc-sync: unchanged pre-existing failures on the base branch (verified identical on clean release/v3.8.52 in apiKeyRotator.ts, useModelImportHandlers.ts, engineConfigSave.ts, and 4 unrelated env vars); neither touches this PR's files.

Notes

  • Unit of truth is now the console API: used/limit are Credits, matching the MiMo dashboard exactly (verified against a live capture: 14,954,038,035 / 38,000,000,000, currentPeriodEnd 2026-11-05 23:59:59).
  • Prior art for the console-cookie flow: steipete/CodexBar (MiMoUsageFetcher.swift).
  • If a maintainer prefers splitting this into "configurable limit only" (small) + "live fetch" (feature), the commits are ready to be reworked — happy to adjust.

…omi-mimo (diegosouzapw#15753)

Xiaomi MiMo quota self-tracked routed tokens against a hardcoded 4.1B (the
Lite tier's Credits), so after a plan upgrade the connection stayed
Exhausted until the calendar-month reset. Two layers fix it:

1. Live console fetch: with a MiMo console session cookie on the connection
   (xiaomiMimoConsoleCookie / XIAOMI_MIMO_CONSOLE_COOKIE) the fetcher reads
   tokenPlan/usage + tokenPlan/detail and reports the real plan window —
   plan label like "Xiaomi MiMo Token Plan · Pro", used/limit in Credits,
   resetAt from currentPeriodEnd. A missing or expired session returns a
   hint message and falls back to the self-tracked numbers instead of
   failing hard.
2. Configurable fallback budget: providerSpecificData.monthlyTokenLimit, then
   XIAOMI_MIMO_MONTHLY_TOKEN_LIMIT, then the historical 4.1B default.

Also exposes the cookie in the connection modal (mirroring the qwen/volc
console-cookie fields), strips it from API responses and audit summaries,
validates the new providerSpecificData fields, and documents both env vars.
@LazyGatto

Copy link
Copy Markdown
Contributor Author

CI triage — every red check is pre-existing release/v3.8.52 drift, none is caused by this PR.

Base tip when branched and as of now: a2ed0704 (release/v3.8.52) — no rebase available, the failures are on the base commit itself.

Verified two ways:

  1. Local A/B on the same clone. Ran the failing Unit/Vitest suites on this branch (84da70d8b) and on clean base a2ed07043:
    • 10 Unit files (feature-flags-settings, issue-7793-env-doc-sync, providers-constants-split, ghe-copilot, server-owned-tool-loop-flag, internal-service-auth, hard-session-lease-bypass-inventory, probe-gate-autodisable, claude-web-sonnet5-registry, devin-cli-catalog): branch 106 pass / 12 fail — base 106 pass / 12 fail, identical failure sets.
    • open-sse/mcp-server/__tests__/audit.test.ts (Vitest): fails on base too (7 failed locally).
  2. Failure causes are all in untouched areas, none reference this PR's files:
Failing check Cause (base-red)
Unit Tests (2–8/8), Unit fast-path (1–4/4) feature-flag counts (82 ≠ 81), APIKEY_PROVIDERS 241 entries, GHE Copilot registry, Devin pricing, Codex 429 probe, cache-API loopback auth, hard-lease inventory, .env.example sync (issue #7793), browser-bundle/response.in_progress, etc. — identical on base
Vitest (MCP / autoCombo / UI components) open-sse/mcp-server/__tests__/audit.test.ts (MCP audit native-loader) — fails on base; the EditConnectionModal UI suite this PR touches passes (only React act(...) warnings)
API Route Typecheck open-sse/services/apiKeyRotator.ts TS2484 (baseline 0 → 1) — known base drift
Docs Sync (Strict) / Docs Gates (fast-path) stale counters: 193 migrations vs 199; 358 vs 359 providers; stale SVG diagrams
Merge integrity agent-skills generator drift
Lint orphan test discovery (cloudflared-object-error.node.test.ts)
Fast Quality Gates ToolsBuilder.tsx TS2339 ratchet drift
Quality Gates (Extended) workflow lint: 5 scheduled jobs without the upstream-only guard
Build Next.js module-not-found (node builtins in client bundle) across untouched files
Gate / CI, Gate / Quality aggregator gates over the above

The PR's own tests/gates are green (xiaomi-mimo-live-quota-15753, xiaomi-mimo-cookie-field, adjacent usage/quota suites, changelog-integrity, eslint on changed files, check:complexity/file-size/dead-code). Happy to rebase the moment release/v3.8.52 gets the base fixes.

@diegosouzapw
diegosouzapw merged commit d7070a5 into diegosouzapw:release/v3.8.52 Oct 8, 2026
29 of 51 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

2 participants