fix(pairing): restore durable atomic writes

This commit is contained in:
yorkhellen
2026-07-04 21:19:19 +08:00
committed by Xubin Ren
parent 28011413bc
commit 8b645135bc
2 changed files with 56 additions and 2 deletions
+11 -1
View File
@@ -2,6 +2,7 @@
import base64
import json
import os
import re
import shutil
import time
@@ -447,8 +448,17 @@ def _cleanup_tool_result_buckets(root: Path, current_bucket: Path) -> None:
def _write_text_atomic(path: Path, content: str) -> None:
tmp = path.with_name(f".{path.name}.{uuid.uuid4().hex}.tmp")
try:
tmp.write_text(content, encoding="utf-8")
with open(tmp, "w", encoding="utf-8") as f:
f.write(content)
f.flush()
os.fsync(f.fileno())
tmp.replace(path)
with suppress(OSError, NotImplementedError):
dfd = os.open(path.parent, os.O_RDONLY)
try:
os.fsync(dfd)
finally:
os.close(dfd)
finally:
if tmp.exists():
tmp.unlink(missing_ok=True)