From bce9d93fb573028ce6f195ef05beac774885f057 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 8 Mar 2026 16:19:17 +0000 Subject: [PATCH] fix: publish models.json atomically --- src/agents/models-config.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/agents/models-config.ts b/src/agents/models-config.ts index ef56cc8e728..9145c5089b9 100644 --- a/src/agents/models-config.ts +++ b/src/agents/models-config.ts @@ -105,6 +105,12 @@ async function ensureModelsFileMode(pathname: string): Promise { }); } +async function writeModelsFileAtomic(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); +} + function resolveModelsConfigInput(config?: OpenClawConfig): OpenClawConfig { const runtimeSource = getRuntimeConfigSourceSnapshot(); if (!runtimeSource) { @@ -195,7 +201,7 @@ export async function ensureOpenClawModelsJson( } await fs.mkdir(agentDir, { recursive: true, mode: 0o700 }); - await fs.writeFile(targetPath, next, { mode: 0o600 }); + await writeModelsFileAtomic(targetPath, next); await ensureModelsFileMode(targetPath); return { agentDir, wrote: true }; });