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 <noreply@anthropic.com>
This commit is contained in:
curo1305
2026-05-17 18:09:45 +02:00
parent 18b2b94194
commit bbe9bcfe0a
4 changed files with 9 additions and 9 deletions
+3 -4
View File
@@ -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