From bbe9bcfe0a5da0f346fdee2a03725265b957b617 Mon Sep 17 00:00:00 2001 From: curo1305 Date: Sun, 17 May 2026 18:09:45 +0200 Subject: [PATCH] refactor(memory): centralize _MEMORY_ROOT; fix mkdir order in append_memory _MEMORY_ROOT was defined independently in reader.py, writer.py, and index.py. Moved to memory/__init__.py; all three import from there. Also fixes a bug in append_memory where path.write_text() was called before path.parent.mkdir(), which would crash when creating a file in a new subdirectory. Co-Authored-By: Claude Sonnet 4.6 --- src/pyra/memory/__init__.py | 3 +++ src/pyra/memory/index.py | 4 ++-- src/pyra/memory/reader.py | 4 +--- src/pyra/memory/writer.py | 7 +++---- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/pyra/memory/__init__.py b/src/pyra/memory/__init__.py index e69de29..6e8b5f8 100644 --- a/src/pyra/memory/__init__.py +++ b/src/pyra/memory/__init__.py @@ -0,0 +1,3 @@ +from pyra.utils.paths import pyra_home + +_MEMORY_ROOT = pyra_home() / "memory" diff --git a/src/pyra/memory/index.py b/src/pyra/memory/index.py index 6d18069..470a2ca 100644 --- a/src/pyra/memory/index.py +++ b/src/pyra/memory/index.py @@ -1,9 +1,9 @@ import datetime from pathlib import Path -from pyra.utils.paths import pyra_home, safe_chmod +from pyra.memory import _MEMORY_ROOT +from pyra.utils.paths import safe_chmod -_MEMORY_ROOT = pyra_home() / "memory" _INDEX_FILE = _MEMORY_ROOT / "MEMORY_INDEX.md" diff --git a/src/pyra/memory/reader.py b/src/pyra/memory/reader.py index 1a0be35..a0d78c0 100644 --- a/src/pyra/memory/reader.py +++ b/src/pyra/memory/reader.py @@ -2,10 +2,8 @@ import datetime from dataclasses import dataclass from pathlib import Path +from pyra.memory import _MEMORY_ROOT from pyra.security.boundaries import assert_safe_path -from pyra.utils.paths import pyra_home - -_MEMORY_ROOT = pyra_home() / "memory" @dataclass diff --git a/src/pyra/memory/writer.py b/src/pyra/memory/writer.py index 3df61c9..6c2739a 100644 --- a/src/pyra/memory/writer.py +++ b/src/pyra/memory/writer.py @@ -1,10 +1,9 @@ from pathlib import Path +from pyra.memory import _MEMORY_ROOT from pyra.memory.index import update_index from pyra.security.boundaries import assert_safe_path -from pyra.utils.paths import pyra_home, safe_chmod - -_MEMORY_ROOT = pyra_home() / "memory" +from pyra.utils.paths import safe_chmod def _resolve_and_validate(name: str) -> Path: @@ -32,12 +31,12 @@ def write_memory(name: str, content: str) -> Path: def append_memory(name: str, content: str) -> Path: path = _resolve_and_validate(name) + path.parent.mkdir(parents=True, exist_ok=True) if path.exists(): existing = path.read_text() path.write_text(existing.rstrip() + "\n\n" + content) else: path.write_text(content) - path.parent.mkdir(parents=True, exist_ok=True) safe_chmod(path, 0o600) update_index() return path