Skip to content

fix(rate-limits): resolve Kimi usage credentials from configured WSL runtime - #12372

Closed
cengiz-io wants to merge 1 commit into
stablyai:mainfrom
cengiz-io:fix/kimi-wsl-usage-target
Closed

cengiz-io wants to merge 1 commit into
stablyai:mainfrom
cengiz-io:fix/kimi-wsl-usage-target

Conversation

@cengiz-io

Copy link
Copy Markdown

Summary

Fixes #12370.

On Windows hosts where Kimi Code runs inside WSL, the status bar permanently showed "Run Kimi to refresh": fetchKimiRateLimits() read OAuth credentials only from the host's ~/.kimi-code/credentials/kimi-code.json, while the Kimi CLI inside WSL rotates the WSL-side copy every 15 minutes — so the host copy is always expired (delegated-refresh-required).

The Kimi fetcher now resolves its credentials home from the configured local-account runtime target, mirroring the existing Codex pattern:

  • kimi-home-path.ts (new): resolveKimiHomePath(settings) — host policy keeps the fetcher's KIMI_CODE_HOME ?? ~/.kimi-code default; a WSL policy resolves the distro's home via the existing cached getDefaultWslDistro() / getWslHome(distro) probe and reads it over its \\wsl.localhost\<distro> UNC path. Off Windows the target pins to host, matching getInitialCodexRateLimitTarget.
  • kimi-fetcher.ts: accepts { kimiHomePath? }; the credentials read goes through createAuthFilesystemOperation with per-path in-flight dedup and a 10s bound (same UNC-read pattern as codex-fetcher's backend auth read), so a disconnected distro degrades to an error instead of hanging the poll. UNC paths are joined with pathWin32. Expired-token copy now says "run kimi inside WSL distro <distro>" for WSL reads.
  • service.ts / index.ts: a setKimiHomePathResolver closure re-resolves the target from live settings on every poll (Kimi has no account switcher, so no kimiFetchTarget state tracking like Codex). An unresolvable WSL home yields an explicit WSL Kimi home unavailable for \<distro\> error instead of silently falling back to the stale host file — mirroring getMissingWslCodexHomeResult.

Failure semantics and the read-only guarantee are unchanged: Orca never refreshes or rewrites the credentials file; missing file → "Not signed in to Kimi Code", expired token → delegated-refresh-required.

Screenshots

No visual change. (With the fix, the Kimi status-bar usage populates on WSL-runtime hosts instead of showing "Run Kimi to refresh"; no renderer code was touched.)

Testing

  • pnpm lint — not run in full; oxlint on all 7 changed files: 0 warnings/errors, plus check-changed-code-quality.mjs (0 new findings) and check-max-lines-ratchet.mjs (OK)
  • pnpm typecheck — typecheck:node clean (change is main-process only)
  • pnpm test — scoped run: vitest run src/main/rate-limits → 415/415 passed (30 files), including 95 tests in the 3 touched/added test files
  • pnpm build — not run locally
  • Added or updated high-quality tests that would catch regressions, or explained why tests were not needed

New/updated tests cover: host default unchanged, KIMI_CODE_HOME honored, WSL UNC path resolution, missing file in WSL home, expired token in WSL home (asserts WSL-distro copy + delegated-refresh-required), all localAccountRuntime policies incl. off-Windows pinning, unresolvable WSL home, and service wiring (resolver value passed through, missing-home short-circuit, no-resolver host default).

AI Review Report

The diff was reviewed by an AI coding agent (Kimi Code) with focus on:

  • Cross-platform compatibility (macOS / Linux / Windows): the WSL branch is gated on platform === 'win32' (both in resolveKimiHomePath and via the pin-to-host guard matching getInitialCodexRateLimitTarget); on macOS/Linux the resolution always returns the host default and getCredentialsPath uses POSIX join. pathWin32.join is used only when parseWslUncPath confirms a UNC path, so host behavior on every platform is byte-identical to before. No shortcuts, labels, shell behavior, or Electron platform APIs were touched; no new wsl.exe invocations (file reads go over UNC, the distro/home probe reuses the existing cached src/main/wsl.ts helpers).
  • Regression risk: host-path resolution order (kimiHomePath ?? KIMI_CODE_HOME ?? ~/.kimi-code) verified to preserve the previous default exactly when no resolver option is passed (covered by tests); sync → async credentials read converted all call sites (single exported fetcher, single caller in service).
  • It flagged that the expired-token error copy was wrong for WSL ("on the computer running Orca") — fixed with a distro-specific message — and that an un-probed WSL home must not silently fall back to the stale host file — handled with an explicit error result.

Security Audit

  • Auth/secrets: the credentials file (contains access + refresh tokens) is still opened read-only; no token refresh, no writes, no logging of file contents. The access token is sent only as a Bearer header to the Kimi /usages HTTPS endpoint via electron net.fetch, exactly as before.
  • Path handling: the WSL home path comes from the existing local wsl.exe -- echo $HOME probe in src/main/wsl.ts (local machine trust boundary, same as Codex usage), not from remote/IPC input. No path traversal surface: segments are fixed (credentials/kimi-code.json).
  • Command execution: none added — no new spawned processes.
  • Input handling/DoS: UNC reads are deduped per path and bounded by AbortSignal.timeout(10s) so a stalled WSL distro cannot exhaust the fs thread pool or hang the fetch cycle; JSON parse of the credentials file is wrapped and reports only generic messages (no content leakage into errors).
  • Dependencies/IPC: no new dependencies, no IPC surface changes.

No follow-up required.

Notes

  • Windows + WSL only: behavior on macOS/Linux is unchanged by construction (pinned to host).
  • The Grok fetcher shares the delegated-refresh-required pattern and reads auth host-only — it likely has the same WSL blind spot but was deliberately left out of scope; happy to follow up if maintainers want the same treatment there.
  • Validated end-to-end reasoning against a live Win11 + WSL (Ubuntu) setup where the bug reproduced (host credentials expired, WSL credentials fresh); the fix makes the poll read the WSL copy that the running CLI actually rotates.

…runtime

The Kimi usage fetcher only read OAuth credentials from the host's
~/.kimi-code, so on Windows hosts where the Kimi CLI runs inside WSL the
status bar was stuck on "Run Kimi to refresh": the CLI rotates the
WSL-side credentials file, leaving the host-side token permanently
expired.

Mirror the Codex pattern: the service resolves the Kimi home for the
configured local-account runtime target (host keeps KIMI_CODE_HOME /
~/.kimi-code; a WSL target reads the distro home over its \\wsl$ UNC
path via the cached echo $HOME probe) and passes it to the fetcher,
which reads the credentials file with the shared UNC-read dedup/
serialization used by the Codex backend auth read. Reading stays
read-only; an unresolvable WSL home surfaces an explicit error instead
of silently falling back to the host file.
@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change adds Kimi runtime-home resolution for host and WSL targets. Credential loading now uses asynchronous, cached filesystem reads with timeout and structured error handling. RateLimitService resolves the Kimi home during full fetches, reports unavailable WSL homes, and passes resolved paths to the fetcher. Tests cover runtime selection, credential paths, missing files, token expiration, and service integration.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: resolving Kimi credentials from the configured WSL runtime.
Description check ✅ Passed The description covers the summary, testing, review, security, screenshots, platform behavior, and follow-up risks in the required sections.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d964a1b6-77f6-4ab3-91f1-496c0ae09342

📥 Commits

Reviewing files that changed from the base of the PR and between 637c7e9 and b683990.

📒 Files selected for processing (7)
  • src/main/index.ts
  • src/main/rate-limits/kimi-fetcher.test.ts
  • src/main/rate-limits/kimi-fetcher.ts
  • src/main/rate-limits/kimi-home-path.test.ts
  • src/main/rate-limits/kimi-home-path.ts
  • src/main/rate-limits/service.test.ts
  • src/main/rate-limits/service.ts

Comment thread src/main/rate-limits/service.ts Resolved
@nwparker

nwparker commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Thanks @cengiz-io — I independently reproduced #12370 and built a fix blind to this PR, then cross-referenced. Your createAuthFilesystemOperation read path is materially better than what I had (I was using sync existsSync/readFileSync over the \\wsl.localhost UNC path, which can freeze Electron main when the distro is down), so I adopted it wholesale along with your exact-read-path test vectors.

Merged result is at #12475 — it keeps your read mechanism, adds a src/main/kimi/ module placement with an exported getHostKimiHome() the sibling host-only resolutions can reuse, and switches the distro/home probe to the async wsl.exe helpers (both our versions used the sync ones, which block main for up to 5s per poll on a stopped distro).

Happy to close mine if you'd rather land it here — your diagnosis was correct and the credit is yours.

@cengiz-io

Copy link
Copy Markdown
Author

Thanks @nwparker — #12475 looks great. The async wsl.exe probe helpers are a real improvement over what we both had (blocking main for seconds on a stopped distro is exactly the kind of thing that's invisible until it isn't), and the src/main/kimi/ placement with a reusable getHostKimiHome() makes more sense for the codebase going forward.

Let's land yours — closing this in favor of #12475. Glad the read path and test vectors were useful. Thanks for the quick turnaround and the kind words.

@cengiz-io cengiz-io closed this Aug 4, 2026
OrcaWin pushed a commit that referenced this pull request Aug 8, 2026
… homes

Adopted from @cengiz-io's #12372: read credentials through
createAuthFilesystemOperation (async, per-path dedup, AbortSignal bound) so a
stopped distro degrades to an error instead of parking Electron main on a UNC
read. ENOTDIR joins ENOENT as "not signed in" to keep existsSync parity, and the
WSL runtime target is now probed with the async wsl.exe helpers.

Co-authored-by: Orca <[email protected]>
OrcaWin added a commit that referenced this pull request Aug 8, 2026
… runtime (#12475)

* fix(rate-limits): read Kimi usage credentials from the configured WSL runtime

Kimi's usage fetch always read the Windows host's ~/.kimi-code, so a Kimi CLI running inside WSL rotated only the WSL-side token and the status bar was stuck on 'Run Kimi to refresh'.

Resolve the Kimi home from the local-account runtime target (mirroring Codex's getDefaultWslDistro()/getWslHome() UNC pattern), pinned to host off Windows, and keep KIMI_CODE_HOME host-only.

Fixes #12370

Co-authored-by: Orca <[email protected]>

* fix(rate-limits): bound and offload the Kimi credentials read for WSL homes

Adopted from @cengiz-io's #12372: read credentials through
createAuthFilesystemOperation (async, per-path dedup, AbortSignal bound) so a
stopped distro degrades to an error instead of parking Electron main on a UNC
read. ENOTDIR joins ENOENT as "not signed in" to keep existsSync parity, and the
WSL runtime target is now probed with the async wsl.exe helpers.

Co-authored-by: Orca <[email protected]>

* test(rate-limits): build Kimi credential-path expectations with path.join

The host-home assertions hardcoded POSIX separators, so they only passed on
a POSIX runner — on a Windows dev machine `join` emits backslashes and all
four cases failed (three assertion mismatches plus a WSL-suite fixture whose
map key never matched the path the fetcher read).

---------

Co-authored-by: Orca <[email protected]>
Co-authored-by: OrcaWin <[email protected]>
@AmethystLiang

Copy link
Copy Markdown
Contributor

thanks for reporting. looking into it

enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
… runtime (stablyai#12475)

* fix(rate-limits): read Kimi usage credentials from the configured WSL runtime

Kimi's usage fetch always read the Windows host's ~/.kimi-code, so a Kimi CLI running inside WSL rotated only the WSL-side token and the status bar was stuck on 'Run Kimi to refresh'.

Resolve the Kimi home from the local-account runtime target (mirroring Codex's getDefaultWslDistro()/getWslHome() UNC pattern), pinned to host off Windows, and keep KIMI_CODE_HOME host-only.

Fixes stablyai#12370

Co-authored-by: Orca <[email protected]>

* fix(rate-limits): bound and offload the Kimi credentials read for WSL homes

Adopted from @cengiz-io's stablyai#12372: read credentials through
createAuthFilesystemOperation (async, per-path dedup, AbortSignal bound) so a
stopped distro degrades to an error instead of parking Electron main on a UNC
read. ENOTDIR joins ENOENT as "not signed in" to keep existsSync parity, and the
WSL runtime target is now probed with the async wsl.exe helpers.

Co-authored-by: Orca <[email protected]>

* test(rate-limits): build Kimi credential-path expectations with path.join

The host-home assertions hardcoded POSIX separators, so they only passed on
a POSIX runner — on a Windows dev machine `join` emits backslashes and all
four cases failed (three assertion mismatches plus a WSL-suite fixture whose
map key never matched the path the fetcher read).

---------

Co-authored-by: Orca <[email protected]>
Co-authored-by: OrcaWin <[email protected]>
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.

[Bug]: Kimi usage fetcher ignores WSL runtime — reads stale Windows credentials, status stuck on "Run Kimi to refresh"

4 participants