From 3d7099b4218902f8e4fd974fb18d7bd85452cf2b Mon Sep 17 00:00:00 2001 From: Xubin Ren Date: Wed, 29 Apr 2026 08:54:51 +0000 Subject: [PATCH] fix(memory): clean atomic write test hygiene Made-with: Cursor --- nanobot/agent/memory.py | 2 +- tests/agent/test_memory_store.py | 25 ++++++++++--------------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/nanobot/agent/memory.py b/nanobot/agent/memory.py index 4cf687d4a..4bf79c356 100644 --- a/nanobot/agent/memory.py +++ b/nanobot/agent/memory.py @@ -369,7 +369,7 @@ class MemoryStore: 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 diff --git a/tests/agent/test_memory_store.py b/tests/agent/test_memory_store.py index a66274c28..9113437fd 100644 --- a/tests/agent/test_memory_store.py +++ b/tests/agent/test_memory_store.py @@ -148,14 +148,14 @@ class TestHistoryWithCursor: store.append_history("event 2") store.append_history("event 3") entries = store.read_unprocessed_history(since_cursor=0) - + # Monitor temp file existence tmp_path_obj = store.history_file.with_suffix(".jsonl.tmp") assert not tmp_path_obj.exists() # Should not exist initially - + # Call _write_entries store._write_entries(entries) - + # Temp file should be cleaned up assert not tmp_path_obj.exists() # Original file should exist @@ -166,26 +166,21 @@ class TestHistoryWithCursor: store = MemoryStore(tmp_path) store.append_history("event 1") entries = store.read_unprocessed_history(since_cursor=0) - + tmp_path_obj = store.history_file.with_suffix(".jsonl.tmp") - + # Mock os.replace to raise an exception - original_replace = __import__('os').replace - def failing_replace(*args, **kwargs): raise RuntimeError("Simulated failure") - + monkeypatch.setattr('os.replace', failing_replace) - - try: + + with pytest.raises(RuntimeError): store._write_entries(entries) - assert False, "Should have raised" - except RuntimeError: - pass - + # Temp file should be cleaned up assert not tmp_path_obj.exists() - + # Original file should still exist (because replace failed) assert store.history_file.exists()