Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(models): find Claude's account config in the Windows home, and fix the test main fails on (#4178)

Milind Soni785be84a5893
diff --git a/server/drivers/claude-model-access.test.ts b/server/drivers/claude-model-access.test.ts
index 108172df67..576dc6a8dd 100644
--- a/server/drivers/claude-model-access.test.ts
+++ b/server/drivers/claude-model-access.test.ts
@@ -78,8 +78,34 @@ describe("what the organization turned off", () => {
describe("reading the account config", () => {
it("looks where the CLI keeps it", () => {
- expect(claudeAccountFiles({ HOME: "/h" })).toEqual(["/h/.claude/.config.json", "/h/.claude.json"]);
- expect(claudeAccountFiles({ HOME: "/h", CLAUDE_CONFIG_DIR: "/c" })).toEqual(["/c/.config.json", "/c/.claude.json"]);
+ expect(claudeAccountFiles({ HOME: "/h" })).toEqual([join("/h", ".claude", ".config.json"), join("/h", ".claude.json")]);
+ expect(claudeAccountFiles({ HOME: "/h", CLAUDE_CONFIG_DIR: "/c" })).toEqual([join("/c", ".config.json"), join("/c", ".claude.json")]);
+ });
+
+ it("on Windows takes USERPROFILE as the home, as the CLI's os.homedir() does, not a HOME a shell left behind", () => {
+ vi.spyOn(process, "platform", "get").mockReturnValue("win32");
+ try {
+ const shell = { HOME: "/c/shell-home", USERPROFILE: "/Users/ada" };
+ expect(claudeAccountFiles(shell)).toEqual([join("/Users/ada", ".claude", ".config.json"), join("/Users/ada", ".claude.json")]);
+ expect(claudeAccountFiles({ ...shell, CLAUDE_CONFIG_DIR: "/c" })).toEqual([join("/c", ".config.json"), join("/c", ".claude.json")]);
+ } finally {
+ vi.restoreAllMocks();
+ }
+ });
+
+ it("keeps what it knew when the file cannot be read for a moment, and reads it again next time", async () => {
+ const home = tempDir();
+ const file = join(home, ".claude.json");
+ writeFileSync(file, access({ apiName: "claude-fable-5-1", entitled: false }));
+ const seen: string[][] = [];
+ const reads = [() => Promise.resolve(access({ apiName: "claude-fable-5-1", entitled: false })), () => Promise.reject(new Error("EPERM")), () => Promise.resolve(access())];
+ const blocks = createOrgModelBlocks(() => [{ HOME: home, USERPROFILE: home }], (blocked) => seen.push([...blocked]), () => reads.shift()!());
+ await blocks.refresh();
+ utimesSync(file, new Date(), new Date(Date.now() + 5_000));
+ await blocks.refresh();
+ expect(seen).toEqual([["claude-fable-5-1"]]);
+ await blocks.refresh();
+ expect(seen).toEqual([["claude-fable-5-1"], []]);
});
const watch = (env: Record<string, string | undefined>[]) => {
@@ -91,7 +117,7 @@ describe("reading the account config", () => {
const home = tempDir();
const file = join(home, ".claude.json");
writeFileSync(file, access({ apiName: "claude-fable-5-1", entitled: false }));
- const { seen, blocks } = watch([{ HOME: home }]);
+ const { seen, blocks } = watch([{ HOME: home, USERPROFILE: home }]);
await blocks.refresh();
await blocks.refresh();
expect(seen).toEqual([["claude-fable-5-1"]]);
@@ -104,7 +130,7 @@ describe("reading the account config", () => {
it("on a cloud provider's backend ignores a list an earlier sign-in left", async () => {
const home = tempDir();
writeFileSync(join(home, ".claude.json"), access({ apiName: "claude-fable-5-1", entitled: false }));
- const { seen, blocks } = watch([{ HOME: home, CLAUDE_CODE_USE_BEDROCK: "1" }]);
+ const { seen, blocks } = watch([{ HOME: home, USERPROFILE: home, CLAUDE_CODE_USE_BEDROCK: "1" }]);
await blocks.refresh();
expect(seen).toEqual([[]]);
});
@@ -114,7 +140,7 @@ describe("reading the account config", () => {
writeFileSync(join(older, ".claude.json"), access({ apiName: "claude-opus-5", entitled: false }));
utimesSync(join(older, ".claude.json"), new Date(1_000), new Date(1_000));
writeFileSync(join(newer, ".claude.json"), access({ apiName: "claude-fable-5", entitled: false }));
- const { seen, blocks } = watch([{ HOME: older }, { HOME: newer }]);
+ const { seen, blocks } = watch([{ HOME: older, USERPROFILE: older }, { HOME: newer, USERPROFILE: newer }]);
await blocks.refresh();
expect(seen).toEqual([["claude-fable-5"]]);
});
diff --git a/server/drivers/claude-model-access.ts b/server/drivers/claude-model-access.ts
index dc506e6a9e..d75dd5d8d9 100644
--- a/server/drivers/claude-model-access.ts
+++ b/server/drivers/claude-model-access.ts
@@ -1,10 +1,10 @@
// Claude Code keeps what an organization allows in its account config: `modelAccessCache`, `[{ apiName, entitled }]`, as
// it stood at the CLI's last start (2.1.312). The CLI answers a turn on a denied model with the organization's default.
import { readFile, stat } from "node:fs/promises";
-import { homedir } from "node:os";
import { join } from "node:path";
import type { ModelCatalog } from "../contracts.ts";
+import { userHome } from "../env-path.ts";
import { conversationLanguage, serverLine } from "../server-lines.ts";
import { topLevelJsonMember } from "../setup-import.ts";
@@ -40,9 +40,10 @@ export function parseOrgBlockedModels(text: string): Set<string> {
}
/** Where 2.1.312 keeps its account config: `.config.json` in its config folder when that has content (older installs), else
- * `.claude.json` in `CLAUDE_CONFIG_DIR` or the home folder. */
+ * `.claude.json` in `CLAUDE_CONFIG_DIR` or the home folder. Its home folder is `os.homedir()`: on Windows that is USERPROFILE,
+ * never a HOME a POSIX-flavoured shell left behind. */
export function claudeAccountFiles(env: Record<string, string | undefined>): string[] {
- const home = env.HOME || env.USERPROFILE || homedir();
+ const home = userHome(env);
const folder = env.CLAUDE_CONFIG_DIR?.trim();
return [join(folder || join(home, ".claude"), ".config.json"), join(folder || home, ".claude.json")];
}
@@ -61,7 +62,11 @@ type AccountEnv = Record<string, string | undefined>;
/** Keeps the catalog in step with the account config. `environments` names every place the CLI may have written it: on a
* Team Cloud each audience's engine home holds its own copy of the same organization's answer, and the newest speaks. */
-export function createOrgModelBlocks(environments: () => AccountEnv[], apply: (blocked: ReadonlySet<string>) => void) {
+export function createOrgModelBlocks(
+ environments: () => AccountEnv[],
+ apply: (blocked: ReadonlySet<string>) => void,
+ read: (path: string) => Promise<string> = (path) => readFile(path, "utf8"),
+) {
let lastRead = "";
return {
async refresh(force = false): Promise<void> {
@@ -75,11 +80,17 @@ export function createOrgModelBlocks(environments: () => AccountEnv[], apply: (b
break;
}
}
- const read = newest ? `${newest.path}:${newest.mtimeMs}` : "none";
- if (!force && read === lastRead) return;
- lastRead = read;
- if (!newest || newest.size > ACCOUNT_FILE_MAX_BYTES) return apply(new Set());
- apply(parseOrgBlockedModels(await readFile(newest.path, "utf8").catch(() => "")));
+ const version = newest ? `${newest.path}:${newest.mtimeMs}` : "none";
+ if (!force && version === lastRead) return;
+ if (!newest || newest.size > ACCOUNT_FILE_MAX_BYTES) {
+ lastRead = version;
+ return apply(new Set());
+ }
+ // Windows refuses a read while the CLI is renaming this file into place: what was known stands until a read succeeds.
+ const text = await read(newest.path).catch(() => null);
+ if (text === null) return;
+ lastRead = version;
+ apply(parseOrgBlockedModels(text));
},
};
}