From 53ca2836e7bf52f35c387f68b9fa43897249d534 Mon Sep 17 00:00:00 2001 From: yorkhellen Date: Tue, 28 Apr 2026 19:57:15 +0800 Subject: [PATCH] fix(memory): also fsync directory for rename durability --- nanobot/agent/memory.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/nanobot/agent/memory.py b/nanobot/agent/memory.py index 3b34f68d..4cf687d4 100644 --- a/nanobot/agent/memory.py +++ b/nanobot/agent/memory.py @@ -369,6 +369,19 @@ 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 + # journals metadata synchronously). + try: + fd = os.open(str(self.history_file.parent), os.O_RDONLY) + try: + os.fsync(fd) + finally: + os.close(fd) + except PermissionError: + pass # Windows — directory fsync not supported except BaseException: tmp_path.unlink(missing_ok=True) raise