diff --git a/src/agents/models-config.file-mode.test.ts b/src/agents/models-config.file-mode.test.ts index e01e6485302..1166f498024 100644 --- a/src/agents/models-config.file-mode.test.ts +++ b/src/agents/models-config.file-mode.test.ts @@ -1,18 +1,16 @@ import fs from "node:fs/promises"; import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; -import { resolveOpenClawAgentDir } from "./agent-paths.js"; +import { cleanupTempDirs, makeTempDir } from "../../test/helpers/temp-dir.js"; import { - CUSTOM_PROXY_MODELS_CONFIG, - installModelsConfigTestHooks, - withModelsTempHome as withTempHome, -} from "./models-config.e2e-harness.js"; -import { ensureOpenClawModelsJson, resetModelsJsonReadyCacheForTest } from "./models-config.js"; + ensureModelsFileModeForModelsJson, + writeModelsFileAtomicForModelsJson, +} from "./models-config.js"; -installModelsConfigTestHooks(); +const tempDirs = new Set(); afterEach(() => { - resetModelsJsonReadyCacheForTest(); + cleanupTempDirs(tempDirs); }); describe("models-config file mode", () => { @@ -20,28 +18,25 @@ describe("models-config file mode", () => { if (process.platform === "win32") { return; } - await withTempHome(async () => { - await ensureOpenClawModelsJson(CUSTOM_PROXY_MODELS_CONFIG); - const modelsPath = path.join(resolveOpenClawAgentDir(), "models.json"); - const stat = await fs.stat(modelsPath); - expect(stat.mode & 0o777).toBe(0o600); - }); + const dir = makeTempDir(tempDirs, "models-json-mode-"); + const modelsPath = path.join(dir, "models.json"); + await writeModelsFileAtomicForModelsJson(modelsPath, '{"providers":{}}\n'); + const stat = await fs.stat(modelsPath); + expect(stat.mode & 0o777).toBe(0o600); }); it("repairs models.json mode to 0600 on no-content-change paths", async () => { if (process.platform === "win32") { return; } - await withTempHome(async () => { - await ensureOpenClawModelsJson(CUSTOM_PROXY_MODELS_CONFIG); - const modelsPath = path.join(resolveOpenClawAgentDir(), "models.json"); - await fs.chmod(modelsPath, 0o644); + const dir = makeTempDir(tempDirs, "models-json-mode-"); + const modelsPath = path.join(dir, "models.json"); + await writeModelsFileAtomicForModelsJson(modelsPath, '{"providers":{}}\n'); + await fs.chmod(modelsPath, 0o644); - const result = await ensureOpenClawModelsJson(CUSTOM_PROXY_MODELS_CONFIG); - expect(result.wrote).toBe(false); + await ensureModelsFileModeForModelsJson(modelsPath); - const stat = await fs.stat(modelsPath); - expect(stat.mode & 0o777).toBe(0o600); - }); + const stat = await fs.stat(modelsPath); + expect(stat.mode & 0o777).toBe(0o600); }); }); diff --git a/src/agents/models-config.ts b/src/agents/models-config.ts index 1b1a34b2a78..0b2bbbbfaf4 100644 --- a/src/agents/models-config.ts +++ b/src/agents/models-config.ts @@ -74,13 +74,16 @@ async function readExistingModelsFile(pathname: string): Promise<{ } } -async function ensureModelsFileMode(pathname: string): Promise { +export async function ensureModelsFileModeForModelsJson(pathname: string): Promise { await fs.chmod(pathname, 0o600).catch(() => { // best-effort }); } -async function writeModelsFileAtomic(targetPath: string, contents: string): Promise { +export async function writeModelsFileAtomicForModelsJson( + targetPath: string, + contents: string, +): Promise { const tempPath = `${targetPath}.${process.pid}.${Date.now()}.tmp`; await fs.writeFile(tempPath, contents, { mode: 0o600 }); await fs.rename(tempPath, targetPath); @@ -149,7 +152,7 @@ export async function ensureOpenClawModelsJson( if (cached) { const settled = await cached; if (settled.fingerprint === fingerprint) { - await ensureModelsFileMode(targetPath); + await ensureModelsFileModeForModelsJson(targetPath); return settled.result; } } @@ -173,13 +176,13 @@ export async function ensureOpenClawModelsJson( } if (plan.action === "noop") { - await ensureModelsFileMode(targetPath); + await ensureModelsFileModeForModelsJson(targetPath); return { fingerprint, result: { agentDir, wrote: false } }; } await fs.mkdir(agentDir, { recursive: true, mode: 0o700 }); - await writeModelsFileAtomic(targetPath, plan.contents); - await ensureModelsFileMode(targetPath); + await writeModelsFileAtomicForModelsJson(targetPath, plan.contents); + await ensureModelsFileModeForModelsJson(targetPath); return { fingerprint, result: { agentDir, wrote: true } }; }); MODELS_JSON_STATE.readyCache.set(targetPath, pending);