Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(memory): stop hierarchy mode from collapsing same-title index rows

Lanre Shittu52486f16cc72
diff --git a/agent/src/memory/persistent.py b/agent/src/memory/persistent.py
index f04cdcf24..4652b77f1 100644
--- a/agent/src/memory/persistent.py
+++ b/agent/src/memory/persistent.py
@@ -551,7 +551,15 @@ class PersistentMemory:
"add(%s): lock timeout, best-effort write", stripped_name
)
path.write_text(frontmatter, encoding="utf-8")
- self._update_index(stripped_name, path.name, description or stripped_name)
+ # path.relative_to(self._dir), not path.name: under
+ # VT_MEMORY_HIERARCHY the file lives at
+ # "{memory_type}/{slug}.md", so path.name alone is just
+ # "{slug}.md" -- identical for two entries sharing a title but
+ # different memory_type. That collapsed _update_index's match
+ # key, so the second add() silently overwrote the first
+ # entry's row instead of keeping both, per #1525's intent.
+ index_key = path.relative_to(self._dir).as_posix()
+ self._update_index(stripped_name, index_key, description or stripped_name)
if get_env_config().memory.links_enabled:
try:
@@ -676,7 +684,10 @@ class PersistentMemory:
def _rebuild_index(self) -> None:
"""Rebuild MEMORY.md from all existing entry files."""
entries = self._scan_entries()
- lines = [f"- [{e.title}]({e.path.name}) — {e.description}" for e in entries]
+ lines = [
+ f"- [{e.title}]({e.path.relative_to(self._dir).as_posix()}) — {e.description}"
+ for e in entries
+ ]
self._index_path.write_text(
"\n".join(lines[:MAX_INDEX_LINES]), encoding="utf-8"
)
diff --git a/agent/tests/test_persistent_memory.py b/agent/tests/test_persistent_memory.py
index d1b81b8e2..48ce1be6a 100644
--- a/agent/tests/test_persistent_memory.py
+++ b/agent/tests/test_persistent_memory.py
@@ -293,6 +293,30 @@ class TestAdd:
assert entries[0].title == "routed-mem"
assert pm.find("routed-mem") is not None
+ def test_hierarchy_same_title_different_types_keep_separate_index_rows(
+ self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
+ ) -> None:
+ # Sibling of test_same_title_different_types_keep_separate_index_rows,
+ # but under VT_MEMORY_HIERARCHY. There, route_entry() drops the
+ # memory_type prefix from the filename (it lives in the category
+ # subdirectory instead), so both entries shared the same bare
+ # "{slug}.md" and the second add() clobbered the first entry's
+ # index row.
+ monkeypatch.setenv("VT_MEMORY_HIERARCHY", "1")
+ pm = PersistentMemory(memory_dir=tmp_path)
+ pm.add(
+ "kb-entry", "raw material notes", "reference", description="reference doc"
+ )
+ pm.add("kb-entry", "user preference notes", "user", description="user pref")
+
+ assert (tmp_path / "reference" / "kb-entry.md").exists()
+ assert (tmp_path / "user" / "kb-entry.md").exists()
+
+ index = (tmp_path / "MEMORY.md").read_text(encoding="utf-8")
+ assert index.count("[kb-entry]") == 2
+ assert "reference/kb-entry.md" in index
+ assert "user/kb-entry.md" in index
+
def test_recovered_orphan_and_new_write_agree_on_the_same_path(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
) -> None: