refactor(utils): dedupe atomic JSONL write helper
Extract atomic_write_lines so history.jsonl, session JSONL saves, and WebUI transcript rewrites share unique temp files, optional fsync, and BaseException cleanup. Closes #5290
This commit is contained in:
1 parent
27b3e81434
commit
23de1f1e34
7 files changed
+287
-89
No files matched your search
+5
-23
@@ -9,7 +9,6 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import threading
|
||||
import weakref
|
||||
@@ -31,6 +30,7 @@ from nanobot.session.manager import Session, SessionManager
|
||||
from nanobot.session.summary import is_summary_checkpoint, session_summary_from_metadata
|
||||
from nanobot.utils.gitstore import GitStore
|
||||
from nanobot.utils.helpers import (
|
||||
atomic_write_lines,
|
||||
build_assistant_message,
|
||||
content_with_media_breadcrumbs,
|
||||
ensure_dir,
|
||||
@@ -473,28 +473,10 @@ class MemoryStore:
|
||||
|
||||
def _write_entries(self, entries: list[dict[str, Any]]) -> None:
|
||||
"""Overwrite history.jsonl with the given entries (atomic write)."""
|
||||
tmp_path = self.history_file.with_suffix(self.history_file.suffix + ".tmp")
|
||||
try:
|
||||
with open(tmp_path, "w", encoding="utf-8") as f:
|
||||
for entry in entries:
|
||||
f.write(json.dumps(entry, ensure_ascii=False) + "\n")
|
||||
f.flush()
|
||||
os.fsync(f.fileno())
|
||||
os.replace(tmp_path, self.history_file)
|
||||
|
||||
# fsync the directory so the rename is durable.
|
||||
# On Windows, opening a directory with O_RDONLY raises
|
||||
# PermissionError — skip the dir sync there (NTFS
|
||||
# journals metadata synchronously).
|
||||
with suppress(PermissionError):
|
||||
fd = os.open(str(self.history_file.parent), os.O_RDONLY)
|
||||
try:
|
||||
os.fsync(fd)
|
||||
finally:
|
||||
os.close(fd)
|
||||
except BaseException:
|
||||
tmp_path.unlink(missing_ok=True)
|
||||
raise
|
||||
atomic_write_lines(
|
||||
self.history_file,
|
||||
(json.dumps(entry, ensure_ascii=False) for entry in entries),
|
||||
)
|
||||
|
||||
# -- dream cursor --------------------------------------------------------
|
||||
|
||||
|
||||
Reference in new issue
Block a user