Keep the mode of ~/.claude.json and the models file when loop-claude rewrites them

This commit is contained in:
2026-09-27 15:07:09 +02:00
parent 4b2ebc95a5
commit 82f1a208bf
4 changed files with 35 additions and 20 deletions
+17 -14
View File
@@ -227,6 +227,21 @@ def make_private(folder):
os.umask(mask)
def replace_json(path, data, **dump):
staged = f"{path}.{os.getpid()}"
try:
mode = os.stat(path).st_mode & 0o7777 if os.path.exists(path) else 0o600
with open(os.open(staged, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600), "w", encoding="utf-8") as handle:
os.fchmod(handle.fileno(), mode)
json.dump(data, handle, **dump)
os.replace(staged, path)
except OSError as failure:
if os.path.exists(staged):
os.remove(staged)
return failure
return None
def model(variable, chosen):
return ["--model", os.environ.get(variable) or chosen]
@@ -248,14 +263,7 @@ def project_model(root):
chosen = ask_model(name, saved)
# Reread: another loop may have saved its project while this one waited on the human.
models = {**read_models(saved), name: chosen}
staged = f"{saved}.{os.getpid()}"
try:
with open(staged, "w", encoding="utf-8") as handle:
json.dump(models, handle, indent="\t", sort_keys=True)
os.replace(staged, saved)
except OSError as failure:
if os.path.exists(staged):
os.remove(staged)
if failure := replace_json(saved, models, indent="\t", sort_keys=True):
sys.exit(f"could not save the model in {saved}: {failure.strerror}")
print(f"{name} runs on {chosen}", file=sys.stderr)
return chosen
@@ -511,12 +519,7 @@ def trust(paths):
return
for path in granted:
projects.setdefault(path, {})["hasTrustDialogAccepted"] = True
staged = f"{config}.loop-claude"
try:
with open(staged, "w", encoding="utf-8") as handle:
json.dump(data, handle, indent=2)
os.replace(staged, config)
except OSError as failure:
if failure := replace_json(config, data, indent=2):
print(f"could not write {config}: {failure.strerror}", file=sys.stderr)
return
print("trusted for the interrupt: " + ", ".join(granted), file=sys.stderr)