fix(usage): preserve the selected Kimi credential home

Compare deployment credentials within each home so a newer legacy-home
login cannot override KIMI_CODE_HOME. Cover the expired-default plus live
global workflow through discovery and usage fetching.

Co-authored-by: benthecarman <benthecarman@live.com>
This commit is contained in:
paseo-bot[bot]
2026-11-02 00:42:16 +00:00
co-authored by benthecarman
parent 521afd9f33
commit 6d98544251
2 changed files with 51 additions and 3 deletions
+5 -3
View File
@@ -331,9 +331,10 @@ export async function discover(): Promise<UsageAccount[]> {
join(homedir(), ".kimi"),
];
// A login to another deployment leaves the old slot behind with a dead token.
// The CLI keeps refreshing the slot it uses, so the latest expiry wins.
let found: { input: UsageInput; expiresAt: number } | undefined;
// Within each home, prefer the latest expiry. Keep the configured home ahead
// of the legacy fallback, even if that fallback holds a newer token.
for (const home of homes) {
let found: { input: UsageInput; expiresAt: number } | undefined;
for (const deployment of KIMI_CODE_DEPLOYMENTS) {
const input: UsageInput = {
store: "file",
@@ -345,6 +346,7 @@ export async function discover(): Promise<UsageAccount[]> {
const expiresAt = credentials.expires_at ?? Number.POSITIVE_INFINITY;
if (!found || expiresAt > found.expiresAt) found = { input, expiresAt };
}
if (found) return [{ key: "default", input: found.input }];
}
return found ? [{ key: "default", input: found.input }] : [];
return [];
}