From 74b22b65221e59eaa4a82ca2eae962812fc24172 Mon Sep 17 00:00:00 2001 From: chengyongru Date: Wed, 14 Oct 2026 13:51:39 +0800 Subject: [PATCH] refactor: deduplicate durable writes and file-edit path resolution --- nanobot/cron/service.py | 39 +++------------------------- nanobot/triggers/local_store.py | 25 ++---------------- nanobot/utils/file_edit_events.py | 31 ++++++++-------------- nanobot/utils/helpers.py | 16 ++++++++++++ nanobot/utils/run_records.py | 31 +++------------------- tests/utils/test_helpers.py | 43 +++++++++++++++++++++++++------ 6 files changed, 70 insertions(+), 115 deletions(-) diff --git a/nanobot/cron/service.py b/nanobot/cron/service.py index 71d4a2420..14f04c15d 100644 --- a/nanobot/cron/service.py +++ b/nanobot/cron/service.py @@ -1,9 +1,7 @@ """Cron service for scheduling agent tasks.""" import asyncio -import errno import json -import os import time import uuid from contextlib import suppress @@ -29,6 +27,7 @@ from nanobot.cron.types import ( CronStore, ) from nanobot.runtime_context import RUNTIME_CONTEXT_INPUT_META +from nanobot.utils.helpers import atomic_write_text from nanobot.utils.run_records import ( write_run_record as write_automation_run_record, ) @@ -447,40 +446,8 @@ class CronService: @staticmethod def _atomic_write(path: Path, content: str) -> None: - """Write *content* to *path* atomically with fsync. - - Uses a temp-file + ``os.replace`` + ``fsync`` pattern so a crash or - SIGKILL mid-write cannot leave the destination truncated or invalid. - Mirrors ``nanobot.session.manager.SessionManager.save`` (see - commit 512bf59, ``fix(session): fsync sessions on graceful shutdown - to prevent data loss``). Without this, ``jobs.json`` could be - corrupted on container shutdown and silently re-created empty on - next start, wiping every scheduled job. - """ - path.parent.mkdir(parents=True, exist_ok=True) - tmp_path = path.with_suffix(path.suffix + ".tmp") - try: - with open(tmp_path, "w", encoding="utf-8") as f: - f.write(content) - f.flush() - os.fsync(f.fileno()) - os.replace(tmp_path, path) - # fsync the parent directory so the rename itself is durable. - # Skip on Windows where opening a directory raises PermissionError; - # some shared filesystems reject directory fsync with EINVAL. - with suppress(PermissionError): - fd = os.open(str(path.parent), os.O_RDONLY) - try: - try: - os.fsync(fd) - except OSError as exc: - if exc.errno != errno.EINVAL: - raise - finally: - os.close(fd) - except BaseException: - tmp_path.unlink(missing_ok=True) - raise + """Durably replace the job store without exposing a partial write.""" + atomic_write_text(path, content) def write_run_record(self, run_id: str, record: dict[str, Any]) -> None: """Write an internal audit record for one cron execution.""" diff --git a/nanobot/triggers/local_store.py b/nanobot/triggers/local_store.py index 3e5e93d8b..5f347a6c9 100644 --- a/nanobot/triggers/local_store.py +++ b/nanobot/triggers/local_store.py @@ -2,7 +2,6 @@ from __future__ import annotations -import errno import json import os import secrets @@ -16,7 +15,7 @@ from filelock import FileLock from loguru import logger from nanobot.triggers.local_types import LocalTrigger, TriggerDelivery, TriggerRunRecord -from nanobot.utils.helpers import truncate_text +from nanobot.utils.helpers import atomic_write_text, truncate_text from nanobot.utils.run_records import write_run_record as write_automation_run_record _TRIGGER_ID_ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789" @@ -409,27 +408,7 @@ class LocalTriggerStore: @staticmethod def _atomic_write(path: Path, content: str) -> None: - path.parent.mkdir(parents=True, exist_ok=True) - tmp_path = path.with_name(f".{path.name}.{uuid.uuid4().hex}.tmp") - try: - with open(tmp_path, "w", encoding="utf-8") as f: - f.write(content) - f.flush() - os.fsync(f.fileno()) - os.replace(tmp_path, path) - with suppress(PermissionError): - fd = os.open(str(path.parent), os.O_RDONLY) - try: - try: - os.fsync(fd) - except OSError as exc: - if exc.errno != errno.EINVAL: - raise - finally: - os.close(fd) - except BaseException: - tmp_path.unlink(missing_ok=True) - raise + atomic_write_text(path, content) def _new_trigger_id(existing_ids: set[str]) -> str: diff --git a/nanobot/utils/file_edit_events.py b/nanobot/utils/file_edit_events.py index 1db2135b5..e351ffd07 100644 --- a/nanobot/utils/file_edit_events.py +++ b/nanobot/utils/file_edit_events.py @@ -410,26 +410,17 @@ def _resolve_apply_patch_paths( def _resolve_single_path(tool: Any, workspace: Path | None, raw_path: Any) -> Path | None: if not isinstance(raw_path, str) or not raw_path.strip(): return None - resolver = getattr(tool, "_resolve_write", None) - if callable(resolver): - try: - resolved = resolver(raw_path) - if isinstance(resolved, Path): - return resolved - if resolved: - return Path(cast(str, resolved)) - except Exception: - return None - resolver = getattr(tool, "_resolve", None) - if callable(resolver): - try: - resolved = resolver(raw_path) - if isinstance(resolved, Path): - return resolved - if resolved: - return Path(cast(str, resolved)) - except Exception: - return None + for resolver_name in ("_resolve_write", "_resolve"): + resolver = getattr(tool, resolver_name, None) + if callable(resolver): + try: + resolved = resolver(raw_path) + if isinstance(resolved, Path): + return resolved + if resolved: + return Path(cast(str, resolved)) + except Exception: + return None if workspace is None: return Path(raw_path).expanduser().resolve() return (workspace / raw_path).expanduser().resolve() diff --git a/nanobot/utils/helpers.py b/nanobot/utils/helpers.py index 829bfeb90..e5c91ec2b 100644 --- a/nanobot/utils/helpers.py +++ b/nanobot/utils/helpers.py @@ -547,6 +547,22 @@ def _cleanup_tool_result_buckets(root: Path, current_bucket: Path) -> None: shutil.rmtree(path, ignore_errors=True) +def atomic_write_text(path: Path, content: str) -> None: + """Create parent directories and durably replace *path* with UTF-8 text.""" + path.parent.mkdir(parents=True, exist_ok=True) + tmp_path = path.with_name(f".{path.name}.{uuid.uuid4().hex}.tmp") + try: + with open(tmp_path, "w", encoding="utf-8") as handle: + handle.write(content) + handle.flush() + os.fsync(handle.fileno()) + os.replace(tmp_path, path) + _fsync_directory_after_replace(path.parent) + except BaseException: + tmp_path.unlink(missing_ok=True) + raise + + def atomic_write_lines(path: Path, lines: Iterable[str], *, fsync: bool = True) -> None: """Atomically replace *path* with already-serialized record lines. diff --git a/nanobot/utils/run_records.py b/nanobot/utils/run_records.py index e952c02d3..a7d761624 100644 --- a/nanobot/utils/run_records.py +++ b/nanobot/utils/run_records.py @@ -2,15 +2,14 @@ from __future__ import annotations -import errno import json -import os import time import uuid -from contextlib import suppress from pathlib import Path from typing import Any +from nanobot.utils.helpers import atomic_write_text + def safe_run_record_name(run_id: str) -> str: """Return a filesystem-safe filename stem for a run ID.""" @@ -26,33 +25,9 @@ def write_run_record(runs_dir: Path, run_id: str, record: dict[str, Any]) -> Pat "run_id": run_id, "updated_at_ms": _now_ms(), } - _atomic_write(path, json.dumps(payload, indent=2, ensure_ascii=False)) + atomic_write_text(path, json.dumps(payload, indent=2, ensure_ascii=False)) return path def _now_ms() -> int: return int(time.time() * 1000) - - -def _atomic_write(path: Path, content: str) -> None: - path.parent.mkdir(parents=True, exist_ok=True) - tmp_path = path.with_name(f".{path.name}.{uuid.uuid4().hex}.tmp") - try: - with open(tmp_path, "w", encoding="utf-8") as f: - f.write(content) - f.flush() - os.fsync(f.fileno()) - os.replace(tmp_path, path) - with suppress(PermissionError): - fd = os.open(str(path.parent), os.O_RDONLY) - try: - try: - os.fsync(fd) - except OSError as exc: - if exc.errno != errno.EINVAL: - raise - finally: - os.close(fd) - except BaseException: - tmp_path.unlink(missing_ok=True) - raise diff --git a/tests/utils/test_helpers.py b/tests/utils/test_helpers.py index 0afe01bd5..d649f9390 100644 --- a/tests/utils/test_helpers.py +++ b/tests/utils/test_helpers.py @@ -1,5 +1,6 @@ import errno import os +from collections.abc import Callable from pathlib import Path import pytest @@ -8,6 +9,7 @@ from nanobot.utils import helpers, token_encoding from nanobot.utils.helpers import ( _write_text_atomic, atomic_write_lines, + atomic_write_text, content_with_media_breadcrumbs, split_message, truncate_text_to_tokens, @@ -190,6 +192,19 @@ def test_write_text_atomic_keeps_file_when_directory_fsync_is_unsupported( assert len(fsync_calls) == 1 +def test_atomic_write_text_creates_parent_and_replaces_exact_content(tmp_path: Path) -> None: + target = tmp_path / "runs" / "record.json" + atomic_write_text(target, "old") + assert target.read_text(encoding="utf-8") == "old" + + content = '{"message": "café"}' + atomic_write_text(target, content) + assert target.read_text(encoding="utf-8") == content + + atomic_write_text(target, "") + assert target.read_text(encoding="utf-8") == "" + + def test_atomic_write_lines_round_trip_replaces_target(tmp_path: Path) -> None: """Missing newline insertion or a non-atomic partial write drops records.""" target = tmp_path / "history.jsonl" @@ -212,8 +227,14 @@ def test_atomic_write_lines_empty_input_replaces_file_with_empty(tmp_path: Path) @pytest.mark.parametrize("error_type", [RuntimeError, KeyboardInterrupt]) -def test_atomic_write_lines_cleans_temp_when_replace_fails( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch, error_type: type[BaseException] +@pytest.mark.parametrize( + "write", + [atomic_write_text, lambda path, text: atomic_write_lines(path, [text])], + ids=["text", "lines"], +) +def test_atomic_writes_clean_temp_when_replace_fails( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, error_type: type[BaseException], + write: Callable[[Path, str], None], ) -> None: """A failed replace must not leave a temp file or change the target.""" target = tmp_path / "history.jsonl" @@ -225,7 +246,7 @@ def test_atomic_write_lines_cleans_temp_when_replace_fails( monkeypatch.setattr(helpers.os, "replace", fail_replace) with pytest.raises(error_type, match="replace failed"): - atomic_write_lines(target, ["new"]) + write(target, "new") assert target.read_text(encoding="utf-8") == "kept\n" assert list(tmp_path.glob("*.tmp")) == [] @@ -279,8 +300,14 @@ def test_atomic_write_lines_suppresses_directory_permission_error( @pytest.mark.parametrize("error_number", [errno.EINVAL, errno.EIO], ids=["unsupported", "io-error"]) -def test_atomic_write_lines_directory_fsync_errors( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch, error_number: int +@pytest.mark.parametrize( + ("write", "expected_content"), + [(atomic_write_text, "ok"), (lambda path, text: atomic_write_lines(path, [text]), "ok\n")], + ids=["text", "lines"], +) +def test_atomic_writes_directory_fsync_errors( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, error_number: int, + write: Callable[[Path, str], None], expected_content: str, ) -> None: """Only unsupported directory fsync errors are ignored; descriptors always close. @@ -320,13 +347,13 @@ def test_atomic_write_lines_directory_fsync_errors( monkeypatch.setattr(helpers.os, "fsync", fsync_then_error) if error_number == errno.EINVAL: - atomic_write_lines(target, ["ok"]) + write(target, "ok") else: with pytest.raises(OSError) as exc_info: - atomic_write_lines(target, ["ok"]) + write(target, "ok") assert exc_info.value.errno == error_number - assert target.read_text(encoding="utf-8") == "ok\n" + assert target.read_text(encoding="utf-8") == expected_content assert fsync_calls == 2 assert opened == [(str(tmp_path), directory_fd)] assert closed == [directory_fd]