Skip to content

Find Kimi credentials for global (kimi.ai) sign-ins - #15646

Open
kurenn wants to merge 3 commits into
omacom:quattrofrom
kurenn:add-kimi-agent-usage
Open

kurenn wants to merge 3 commits into
omacom:quattrofrom
kurenn:add-kimi-agent-usage

Conversation

@kurenn

@kurenn kurenn commented Oct 16, 2026 •

Copy link
Copy Markdown

The Kimi collector from #7274 only reads credentials/kimi-code.json. That is where Kimi Code stores a mainland (kimi.com) login, but a global (kimi.ai) or custom-environment login is filed under a kimi-code-env-<hash> slot. Only the oauth key in config.toml names that slot:

[providers."managed:kimi-code".oauth]
key = "oauth/kimi-code-env-0e4f99c69cc27850"
oauth_host = "https://auth.kimi.ai"

For those users the collector never finds the token and the panel reports "Kimi unavailable".

Changes

  • Read config.toml to find the credential slot (restricted to kimi-code or kimi-code-env-<hex>, so the config can't point outside credentials/). Only Kimi Code's own home uses slots; a legacy kimi-cli sign-in keeps kimi-code.json and the kimi.com hosts.
  • Lock oauth/<slot>.lock, the same lock Kimi Code takes for that slot, so a refresh can't race the CLI.
  • Take the OAuth host and API base URL from the same login. The KIMI_CODE_*, KIMI_OAUTH_HOST and KIMI_USAGE_URL environment overrides still win, and ALLOWED_DOMAINS still decides whether a token is sent.
  • Send a User-Agent: api.kimi.ai returns 403 to urllib's default one (api.kimi.com accepts it).
  • config.toml reading is shared with code_routes() through load_config(), so a config with invalid UTF-8 can't crash the collector at import.

Testing

  • New scoped_slot case in agent-usage-kimi-regression-test.sh loads a fresh collector after writing a scoped config.toml. It checks CREDENTIALS_PATH, USAGE_URL and TOKEN_URL, then runs a real refresh through the TLS stub with a stale oauth/<slot>.lock. The lock is taken over, held during the token request and released, and the rotated pair lands in the slot file. It also covers both sets of env overrides, a config pointing at a non-Kimi host (no token sent), a path-traversal key, and a legacy sign-in staying on the default slot. The refresh case now checks the User-Agent on both requests. The new checks fail against the collector on quattro.
  • agent-usage-kimi-regression-test.sh, agent-usage-kimi-stats-test.sh, kimi-agent-test.sh, agent-usage-update-test.sh, agents-panel-test.sh and ./test/cli pass.
  • On a real kimi.ai account: limits load (5-hour session, monthly total, monthly code). An expired token refreshes against auth.kimi.ai under the lock, and the rotated pair is persisted at mode 600.

🤖 Generated with Claude Code

Fixes #19225

@greptile-apps

greptile-apps Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge; no new actionable issues were found.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Summary

The PR lets the Kimi collector find global sign-ins through config.toml.

  • The Kimi collector uses the configured sign-in slot and hosts.

The earlier test-coverage concern is addressed: scoped_slot() loads a fresh collector, checks its paths and URLs, and refreshes through the scoped lock. No new actionable issues were found.

Reviews (4) · Last reviewed commit: "Keep Kimi host overrides on their matchi..." · Reviewed by Greptile

Comment thread test/shell.d/agent-usage-kimi-test.sh Resolved
@omarchybot

Copy link
Copy Markdown
Collaborator

Looked at this today and read the diff: it makes the Kimi collector follow the oauth key in config.toml to a kimi-code-env-<hash> credential slot, takes the login and API hosts from that login, and sends a User-Agent. I read it as a bug fix. Nothing has been run on a worker yet, and no label has gone on.

The reason is the second opinion. Every item gets a classification from two models, and the reviewer (Codex Medium) is reachable, but omabot had used up its daily review budget by the time this came up. With only one model's reading the classification is unsettled, so I stopped there rather than verify a fix whose class nobody has checked.

Next: this goes round again once the budget resets. That pass classifies it with the second opinion, then reproduces the kimi.ai sign-in failure on a worker against quattro and runs the Kimi tests on this head. Nothing is needed from you in the meantime.

Kimi Code files a global or custom-environment login under a
kimi-code-env-<hash> credential slot that only the oauth key in
config.toml names, so the collector looked for kimi-code.json, found
nothing, and reported Kimi unavailable. Follow config.toml to the slot,
lock the matching oauth/<slot>.lock, and take the OAuth and API hosts
from the same login; the legacy kimi-cli home keeps the default slot
and hosts. kimi.ai rejects urllib's default User-Agent with a 403, so
both requests now send their own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kurenn
kurenn force-pushed the add-kimi-agent-usage branch from e49bc5b to 50a3517 Compare October 26, 2026 14:22
@volodymyrkatkalov

Copy link
Copy Markdown

Tested PR head 50a35178 on aarch64 Omarchy (omarchy-dev 4.0.0.r7368.g600ca88-1) with Kimi Code 2.4.1 and a global kimi.ai login using credential slot kimi-code-env-0e4f99c69cc27850.

Before the patch, the stock collector found local session statistics but returned Waiting for auth and no limits. With this collector, a live --force run successfully returned fresh Session (5-hour), Monthly total, and Monthly code limits with reset times and no authentication error.

I also verified the panel integration by installing the collector as a user-owned override and having a cloned Agents widget use it. After restarting the shell, omarchy-shell omarchy.agents refresh populated kimi.json with those limits through the widget-driven refresh path.

This confirms credential discovery and live quota collection for my global sign-in. My token was still valid, so this test did not exercise the expired-token refresh path.

@volodymyrkatkalov volodymyrkatkalov 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.

Approving after reviewing the full change at 50a35178 and verifying it against a real global Kimi sign-in.

The credential-slot validation, endpoint precedence and trust checks, legacy fallback, per-slot refresh locking, and private credential writes look sound. Live quota collection and the widget-driven refresh path both work with the environment-specific credential slot.

Focused checks passed:

  • agent-usage-kimi-regression-test.sh (76 assertions)
  • agent-usage-kimi-stats-test.sh (24 assertions)
  • kimi-agent-test.sh
  • agent-usage-update-test.sh
  • agents-panel-test.sh
  • ./test/cli (125 assertions)
  • git diff --check quattro...HEAD

The real account had a valid access token; expired-token rotation and the per-slot lock were exercised through the new TLS regression coverage rather than against the live account.

@volodymyrkatkalov

Copy link
Copy Markdown

Follow-up to my review: live OAuth token rotation now passes too. I invoked the collector's own refresh path against the real global Kimi service, without an HTTP fixture or changing the stored expiry.

Both the access token and refresh token rotated, the new pair was persisted and reread successfully with file mode 0600, and the per-slot lock was released. The new access token was then accepted by the live quota endpoint, which returned the Session (5-hour), Monthly total, and Monthly code windows. No token values were logged.

@omarchybot omarchybot added bug Something isn't working verified Omarchy Triage has verified that this issue is ready for final review labels Nov 2, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed and run at 50a35178. Nothing found to fix.

What was checked, on a disposable Omarchy worker against quattro (175e63b6):

  • The bug reproduces: with quattro's collector, the new scoped_slot case fails (it reads credentials/kimi-code.json and asks api.kimi.com and auth.kimi.com instead of the kimi-code-env-<hash> slot and the kimi.ai hosts config.toml names), and the refresh case sends Python-urllib/3.14 as its User-Agent. Both pass on this head.
  • agent-usage-kimi-regression-test.sh (76 passed), agent-usage-kimi-stats-test.sh, kimi-agent-test.sh, agent-usage-update-test.sh, agents-panel-test.sh and ./test/cli all pass on this head.
  • Read: the slot regex keeps a hostile or malformed config.toml inside credentials/, hosts taken from it still go through trusted_url() before a token is sent, the legacy ~/.kimi sign-in keeps the default slot and kimi.com hosts, and the limits cache still keys on the credential path, usage URL and token.

The second opinion came from Codex Medium and from muse-wonder (via pi, thinking high). Neither found a defect; both read the change as proportionate and the tests as exercising the real collector rather than a stub. They agreed with what I had found, and their independence isn't guaranteed. @volodymyrkatkalov's test on a real kimi.ai login at this head, including a live token rotation, covers what a worker can't: the real service.

#16431 (Pi credentials for Kimi) also edits bin/omarchy-agent-usage-kimi. It adds a different credential source, so it doesn't compete with this one, but whichever merges second may need a rebase.

This is now waiting on the maintainer. Nothing is needed from you.

omarchybot and others added 2 commits November 18, 2026 00:46
Adapt the credential-selection cases from omacom#19241 without inferring an active account or regional endpoints from orphaned files. The configured slot must win over leftover sign-ins; a missing slot or absent configuration must not borrow another scoped credential.

Co-Authored-By: AnPod <drejc83@gmail.com>
Co-Authored-By: GPT-6 (Codex) <noreply@openai.com>
Co-Authored-By: Opus 5.5 High <noreply@anthropic.com>
Co-Authored-By: muse-wonder High <noreply@meta.com>
A host override must select the same sign-in as Kimi Code. Keeping config.toml's global slot while overriding the API host sends that account's token to the mainland service, and a base-only override also kept the wrong OAuth host. Derive the slot from the normalized endpoint pair using Kimi Code's SHA-256 identity, and retain its default OAuth host when an override does not name one. Extend the account-selection checks to cover a missing slot beside a leftover default credential.

Co-Authored-By: GPT-6 (Codex) <noreply@openai.com>
Co-Authored-By: Opus 5.5 High <noreply@anthropic.com>
@omarchybot omarchybot removed verified Omarchy Triage has verified that this issue is ready for final review bug Something isn't working labels Nov 17, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed 23d46452025894977045bcbe1eec0f1105e1e11e and reproduced #19225 on a disposable Omarchy worker: the base collector returns Waiting for auth for a configured global sign-in; this head refreshes it under the selected slot's lock, persists the rotated token and returns quota. Reverting the collector reproduces the failure again.

Added account-selection tests adapted from #19241, crediting @AnPod. The configured slot wins over leftover sign-ins, and a missing configured file or an unconfigured scoped file does not borrow another account. A file alone does not identify the account's regional endpoints, so the lone-file fallback was excluded.

Fixed a host-override mismatch: KIMI_CODE_BASE_URL, KIMI_CODE_OAUTH_HOST and KIMI_OAUTH_HOST now select the corresponding credential slot using Kimi Code's endpoint identity. A mainland override previously sent the configured global account's token. That regression fails with the fix reverted and passes on this head.

On the worker, ./test/cli and the Kimi regression, Kimi stats, Kimi agent, usage updater and agents panel tests passed. The new selection checks also failed when deliberately changed to prefer a leftover default credential or guess a lone scoped file. Kimi Code 2.6.2's published source was checked for slot derivation and lock naming.

The captures show the actual updater and panel using synthetic credentials and a local quota service; they establish the panel change, without claiming a live account test at this head.

Before: the base collector leaves the Kimi subscription hidden

After: this head shows the synthetic Kimi account's Session and Monthly meters

GPT-6 in Codex (driver effort not exposed) reviewed this, with Opus 5.5 High and muse-wonder via pi, thinking high. Opus raised the host-override mismatch; both reviewers found no blocking defects in the final head. Their final agreement is not a guarantee of independence. The global slot used as the hash oracle also appears in the real-account reports above and in #19225.

The new head has a classification disagreement: I classified the reproduced failure as a bug, while deepseek-v4p1-flash classified the change as an enhancement (classification effort and rationale were not recorded). Under the queue rules that stops verification labels and readiness assessment, despite the passing worker checks. The previous labels were withdrawn; this is waiting on the maintainer to settle the classification. No contributor change is requested.

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.

agents panel: kimi collector misses Kimi Code 2.x global-region logins (kimi-code-env-*.json)

3 participants