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:
@@ -0,0 +1,3 @@
|
||||
from pyra.utils.paths import pyra_home
|
||||
|
||||
_MEMORY_ROOT = pyra_home() / "memory"
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user