Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(memory): coerce frontmatter values to str in _scan_entries

Teerapat-Vatpitak16102825b418
diff --git a/agent/src/memory/persistent.py b/agent/src/memory/persistent.py
index d1e5d382b..57278e2f2 100644
--- a/agent/src/memory/persistent.py
+++ b/agent/src/memory/persistent.py
@@ -68,6 +68,24 @@ def _tokenize(text: str) -> set[str]:
return ascii_tokens | cjk_tokens
+def _coerce_str(value: object, default: str = "") -> str:
+ """Coerce frontmatter values to a display string.
+
+ ``parse_frontmatter`` returns lists for ``[a, b]`` syntax and bools for
+ ``true``/``false``. ``MemoryEntry`` annotates these fields as ``str`` so
+ callers (CLI rendering, recall scoring) can rely on string operations.
+ """
+ if value is None:
+ return default
+ if isinstance(value, str):
+ return value
+ if isinstance(value, bool):
+ return "true" if value else "false"
+ if isinstance(value, list):
+ return ", ".join(str(v) for v in value)
+ return str(value)
+
+
class PersistentMemory:
"""File-based persistent memory that survives across sessions.
@@ -124,9 +142,9 @@ class PersistentMemory:
meta, body = _parse_frontmatter(text)
entries.append(MemoryEntry(
path=path,
- title=meta.get("name", path.stem),
- description=meta.get("description", ""),
- memory_type=meta.get("type", "project"),
+ title=_coerce_str(meta.get("name"), default=path.stem),
+ description=_coerce_str(meta.get("description")),
+ memory_type=_coerce_str(meta.get("type"), default="project"),
body=body[:MAX_ENTRY_CHARS],
modified_at=path.stat().st_mtime,
))
diff --git a/agent/tests/test_cli_memory.py b/agent/tests/test_cli_memory.py
index 9b646c269..f2add3143 100644
--- a/agent/tests/test_cli_memory.py
+++ b/agent/tests/test_cli_memory.py
@@ -125,6 +125,22 @@ class TestCmdMemoryShow:
assert rc == cli.EXIT_SUCCESS
assert "evil[red]title" in out
+ def test_list_does_not_crash_on_bracketed_description(
+ self, tmp_path: Path, capsys: pytest.CaptureFixture[str]
+ ) -> None:
+ # Regression: descriptions that look like YAML list literals are parsed
+ # as lists. Without storage-layer coercion, rich.markup.escape() crashed
+ # with TypeError because it only accepts strings.
+ entry_path = tmp_path / "user_yaml-leak.md"
+ entry_path.write_text(
+ "---\nname: yaml-leak\ndescription: [red]inject[/red]\ntype: user\n---\n\nbody\n",
+ encoding="utf-8",
+ )
+ rc = cli.cmd_memory_list(memory_dir=tmp_path)
+ out = capsys.readouterr().out
+ assert rc == cli.EXIT_SUCCESS
+ assert "yaml-leak" in out
+
class TestCmdMemorySearch:
def test_finds_match(self, tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
diff --git a/agent/tests/test_persistent_memory.py b/agent/tests/test_persistent_memory.py
index 060ef7875..68d4699d6 100644
--- a/agent/tests/test_persistent_memory.py
+++ b/agent/tests/test_persistent_memory.py
@@ -6,7 +6,41 @@ from pathlib import Path
import pytest
-from src.memory.persistent import PersistentMemory, MemoryEntry, _tokenize
+from src.memory.persistent import PersistentMemory, MemoryEntry, _coerce_str, _tokenize
+
+
+class TestCoerceStr:
+ def test_passthrough_string(self) -> None:
+ assert _coerce_str("hello") == "hello"
+
+ def test_none_uses_default(self) -> None:
+ assert _coerce_str(None, default="fallback") == "fallback"
+
+ def test_list_joined_with_comma(self) -> None:
+ # `description: [red]inject[/red]` would parse to a single-element list
+ # because the frontmatter parser treats ``[...]`` as a list literal.
+ assert _coerce_str(["red]inject[/red"]) == "red]inject[/red"
+ assert _coerce_str(["a", "b"]) == "a, b"
+
+ def test_bool_lowercased(self) -> None:
+ assert _coerce_str(True) == "true"
+ assert _coerce_str(False) == "false"
+
+
+class TestScanEntriesCoercesFrontmatter:
+ def test_bracketed_description_renders_as_string(self, tmp_path) -> None:
+ # Regression: a description like ``[red]x[/red]`` parsed as a list used
+ # to leak through MemoryEntry.description and crash any downstream
+ # consumer that called string ops on it (e.g. rich.markup.escape).
+ entry_path = tmp_path / "user_bracket-desc.md"
+ entry_path.write_text(
+ "---\nname: bracket-desc\ndescription: [red]inject[/red]\ntype: user\n---\n\nbody\n",
+ encoding="utf-8",
+ )
+ pm = PersistentMemory(memory_dir=tmp_path)
+ entries = pm.list_entries()
+ assert len(entries) == 1
+ assert isinstance(entries[0].description, str)
# ---------------------------------------------------------------------------