Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(memory): recover entries orphaned by the missing-suffix write bug

Haozhe Wu5b638b2bc99e
diff --git a/agent/src/memory/hierarchy.py b/agent/src/memory/hierarchy.py
index 7f8c4b685..2f82b51b8 100644
--- a/agent/src/memory/hierarchy.py
+++ b/agent/src/memory/hierarchy.py
@@ -89,15 +89,71 @@ class MemoryHierarchy:
)
return self._base_dir / filename
+ def recover_extensionless_entries(self) -> List[Path]:
+ """Rename category entries that were written without the ``.md`` suffix.
+
+ ``route_entry`` documents its second argument as "the .md filename",
+ but a caller passed a bare slug, so entries landed as
+ ``<category>/<slug>`` with no suffix. Every scan here filters on
+ ``suffix == ".md"``, which means those entries are not merely
+ mis-named — they are invisible to ``list_entries`` and ``find``, and
+ stay invisible after the write path is corrected, because nothing
+ goes back for them.
+
+ Only a suffix-less file whose content opens with a frontmatter fence
+ is touched, so an unrelated file dropped in a category directory is
+ left alone. An existing ``<slug>.md`` wins; the orphan is left in
+ place rather than silently overwriting the newer entry.
+
+ Returns:
+ Paths of the entries after renaming, in the order recovered.
+ """
+ recovered: List[Path] = []
+ for category in CATEGORIES:
+ cat_dir = self._base_dir / category
+ if not cat_dir.is_dir():
+ continue
+ for item in sorted(cat_dir.iterdir()):
+ if item.name in _SKIP_NAMES or not item.is_file():
+ continue
+ if item.suffix:
+ continue
+ target = item.with_suffix(".md")
+ if target.exists():
+ logger.warning(
+ "Extension-less memory entry %s shadowed by %s; leaving "
+ "it in place rather than overwriting the newer entry",
+ item,
+ target,
+ )
+ continue
+ try:
+ head = item.read_text(encoding="utf-8")[:3]
+ except OSError:
+ continue
+ if head.strip() != "---":
+ continue
+ try:
+ item.rename(target)
+ except OSError:
+ logger.warning("Could not recover memory entry %s", item)
+ continue
+ logger.info("Recovered memory entry %s -> %s", item.name, target.name)
+ recovered.append(target)
+ return recovered
+
def scan_all(self) -> List[Path]:
"""Scan base dir + all category subdirs for *.md files.
Skips entries listed in _SKIP_NAMES and the archive/ directory.
Includes flat-stored entries in base dir for backward compatibility.
+ Entries orphaned by the missing-suffix write bug are recovered first,
+ so they become visible without the user having to run anything.
Returns:
Sorted list of .md file paths.
"""
+ self.recover_extensionless_entries()
results: List[Path] = []
# Scan base directory (flat legacy entries)
diff --git a/agent/tests/test_memory_orphan_recovery.py b/agent/tests/test_memory_orphan_recovery.py
new file mode 100644
index 000000000..437b12987
--- /dev/null
+++ b/agent/tests/test_memory_orphan_recovery.py
@@ -0,0 +1,55 @@
+"""Recovery of memory entries orphaned by the missing-suffix write bug."""
+from pathlib import Path
+import pytest
+from src.memory.hierarchy import MemoryHierarchy
+
+_ENTRY = "---\nname: alpha-note\ndescription: d\nmetadata:\n type: project\n---\n\nbody\n"
+
+
+def test_extensionless_entry_is_recovered_and_becomes_visible(tmp_path: Path) -> None:
+ (tmp_path / "project").mkdir()
+ orphan = tmp_path / "project" / "alpha-note"
+ orphan.write_text(_ENTRY, encoding="utf-8")
+
+ found = MemoryHierarchy(tmp_path).scan_all()
+
+ assert not orphan.exists()
+ assert (tmp_path / "project" / "alpha-note.md").is_file()
+ assert (tmp_path / "project" / "alpha-note.md") in found
+
+
+def test_recovery_never_overwrites_a_newer_entry(tmp_path: Path) -> None:
+ (tmp_path / "project").mkdir()
+ (tmp_path / "project" / "dup").write_text(_ENTRY, encoding="utf-8")
+ keep = tmp_path / "project" / "dup.md"
+ keep.write_text(_ENTRY.replace("body", "newer"), encoding="utf-8")
+
+ MemoryHierarchy(tmp_path).recover_extensionless_entries()
+
+ assert "newer" in keep.read_text(encoding="utf-8")
+ assert (tmp_path / "project" / "dup").is_file()
+
+
+ "name,content",
+ [("notes.txt", _ENTRY), ("scratch", "just some text, no frontmatter\n")],
+ ids=["has_suffix", "no_frontmatter"],
+)
+def test_unrelated_files_are_left_alone(tmp_path: Path, name, content) -> None:
+ (tmp_path / "project").mkdir()
+ other = tmp_path / "project" / name
+ other.write_text(content, encoding="utf-8")
+
+ MemoryHierarchy(tmp_path).recover_extensionless_entries()
+
+ assert other.is_file()
+ assert not (tmp_path / "project" / "scratch.md").exists()
+
+
+def test_recovery_is_idempotent(tmp_path: Path) -> None:
+ (tmp_path / "feedback").mkdir()
+ (tmp_path / "feedback" / "lesson").write_text(_ENTRY, encoding="utf-8")
+ h = MemoryHierarchy(tmp_path)
+
+ assert len(h.recover_extensionless_entries()) == 1
+ assert h.recover_extensionless_entries() == []