Skip to content

Find the Kimi Code 2.x scoped credential in the usage collector - #19241

Open
AnPod wants to merge 1 commit into
omacom:quattrofrom
AnPod:kimi-collector-scoped-credentials
Open

AnPod wants to merge 1 commit into
omacom:quattrofrom
AnPod:kimi-collector-scoped-credentials

Conversation

@AnPod

@AnPod AnPod commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Problem

On a machine signed in to Kimi Code 2.x through the global region (oauth host auth.kimi.ai), the agents panel shows no Kimi section and omarchy-agent-usage-kimi reports "Waiting for auth" despite valid credentials on disk (#19225).

Kimi Code 2.x persists a global-region (or other custom-environment) login under a scoped oauth key — credentials/kimi-code-env-<hash>.json, with the active key recorded in ~/.kimi-code/config.toml:

[providers."managed:kimi-code".oauth]
storage = "file"
key = "oauth/kimi-code-env-0e4f99c69cc27850"

The collector resolved only the default slot credentials/kimi-code.json, which is written for a mainland-China login, so it found no sign-in, providerHasSubscription() in Main.qml hid the section, and the quota was never probed.

Fix

bin/omarchy-agent-usage-kimi keeps the default slot as the fast path, and when it is missing:

  1. reads the provider oauth refs in config.toml (the managed kimi-code provider first) and uses credentials/<rest-of-key>.json for a key = "oauth/..." value,
  2. falls back to the single credentials/kimi-code-env-*.json when exactly one exists and no key is recorded,
  3. otherwise keeps today's behavior.

A token refresh is written back to the same file the CLI reads (scoped file when that is the resolved sign-in), and the limits cache stays keyed by the resolved path, so another sign-in or service never shows this account's quota. No network surface, trust check, or panel logic changes.

Test

Four new cases in test/shell.d/agent-usage-kimi-regression-test.sh, run against the real TLS stand-in for Kimi's services:

  • a scoped oauth key named in config.toml is read (red-before failed with exactly the issue's symptom: usageStatusText: "Waiting for auth", zero requests),
  • a lone scoped file is found without config.toml,
  • the default slot wins while it exists, and with it gone the recorded key disambiguates between several scoped files,
  • two scoped files with no key recorded stay "Waiting for auth" — no sign-in is guessed.

Full file passes (72 assertions); agent-usage-kimi-stats-test.sh, kimi-agent-test.sh, agent-usage-kimchi-test.sh and agent-usage-partition-test.sh pass unchanged.

Fixes #19225

A global-region (or other custom-environment) Kimi Code 2.x login keeps
its sign-in under a scoped oauth key recorded in config.toml, landed as
credentials/kimi-code-env-<hash>.json; the default kimi-code.json slot
is only written for a mainland-China login. The collector looked at the
default slot alone, so such machines showed "Waiting for auth" and the
panel hid the Kimi section despite valid credentials on disk.

The default slot stays the fast path; when it is missing, the collector
now reads the provider oauth key from config.toml and takes the file it
names, falling back to a lone credentials/kimi-code-env-*.json when no
key is recorded. A refreshed token is written back to the same scoped
file the CLI reads.

Fixes omacom#19225
@greptile-apps

greptile-apps Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The PR affects the Kimi usage collector’s sign-in and quota workflow.

Fix the fallback before merging so a missing configured credential cannot select another account.

Findings

  1. P1 Another account's quota appears ▶
  2. P2 Config lookup goes untested ▶

Summary

Adds scoped Kimi Code credential lookup while keeping the default file first.

  • Kimi usage finds scoped sign-ins when the default file is missing.

AnPod explicitly describes default-file precedence as intentional.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Resolve credential] --> B{Default file exists?}
  B -->|Yes| C[Use default file]
  B -->|No| D[Read configured OAuth keys]
  D --> E{Referenced file exists?}
  E -->|Yes| F[Use referenced file]
  E -->|No| G{Exactly one scoped file?}
  G -->|Yes| H[Use remaining scoped file]
  G -->|No| I[Wait for sign-in]
Loading

Reviews (1) · Last reviewed commit: "Find the Kimi Code 2.x scoped credential..." · Reviewed by Greptile

Comment on lines +131 to +133
env_files = sorted(CREDENTIALS_DIR.glob("kimi-code-env-*.json"))
if len(env_files) == 1:
return env_files[0]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Another account's quota appears

If the OAuth key recorded in config.toml points to a missing file while another environment's scoped credential remains, credentials_path() selects that remaining file. The panel then probes the other account instead of waiting for the configured sign-in. Only use the lone-file fallback when no key is recorded, as the PR description promises.

Comment on lines +489 to +491
(cred / "kimi-code.json").unlink()
expect("with the default gone, the key recorded in config.toml disambiguates",
load_collector("kimi").credentials_path().name, "kimi-code-env-aaa111.json")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Config lookup goes untested

The configured-key tests each create only one scoped file. After kimi-code.json is removed, scoped_precedence() therefore passes through the lone-file fallback even if all config parsing is deleted. Add a second scoped credential with a different token and check that the recorded key selects the right one. This would catch broken config lookup when several scoped credentials exist.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@omarchybot omarchybot added the bug Something isn't working label Nov 17, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed 30c4cba8d6fd857dd6889cb6ae333667e09b553b against quattro, the linked #19225 report, the existing review comments, and the older competing fix #15646. Both address scoped credential discovery. #15646 also derives the API/OAuth endpoints from the configured login and uses its slot-specific refresh lock; this diff keeps the default endpoints and oauth/kimi-code.lock.

The source also confirms the two gaps Greptile identified: a configured key whose file is missing can fall through to another scoped credential, and the configured-key fixtures contain only one scoped file, so they do not distinguish config lookup from the lone-file fallback. These were checked by reading, not reproduced here.

Codex High reviewed this item, with a second opinion from Opus 5.5 High and muse-wonder via pi, thinking high. Both favored continuing with the older #15646; that agrees with my comparison, and independence is not guaranteed. No worker tests ran for this head because the competing-fix comparison comes before verification. Nothing was pushed.

This PR additionally handles a lone scoped file when no key is recorded. I have handed that case and the ambiguity coverage to #15646's queue session for verification there, including the reviewers' concern that a missing configured credential must not select another account and that a file alone does not identify its regional endpoints. Any incorporated code must credit @AnPod.

Leaving this open for the maintainer to choose between the competing fixes. The bug label is applied; this head has not been marked verified.

omarchybot added a commit to kurenn/omarchy that referenced this pull request Nov 17, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

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)

2 participants