"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(memory): the compression pass skips a file that is gone, not an id that was acted on (#1450)
Haozhe Wu451c554aa1ec
diff --git a/agent/src/memory/lifecycle.py b/agent/src/memory/lifecycle.pyindex 417a42049..327771316 100644--- a/agent/src/memory/lifecycle.py+++ b/agent/src/memory/lifecycle.py@@ -205,7 +205,6 @@ class MemoryLifecycle:now = time.time()actions: list[dict] = []- gc_acted_ids: set[str] = set()for entry in entries:age_days = (now - entry.created_at) / 86400.0@@ -238,17 +237,17 @@ class MemoryLifecycle:# Tier 1: force archive even if classified as deleteeffective = "archive" if not self.ENABLE_DELETE else actionself._execute_gc_action(entry, effective)- gc_acted_ids.add(entry.id)self._append_gc_log(actions, dry_run)# Tier 2: Trigger compression for aged entries. Skipped on dry_run:# compression rewrites entry bodies and frontmatter in place, and a- # dry run must not mutate the files it is only auditing. Entries- # already archived/deleted above are skipped too: _execute_gc_action- # already renamed or unlinked entry.path on disk, so re-using this- # same pre-Tier-1 snapshot for them would try to compress a file- # that no longer exists at that location.+ # dry run must not mutate the files it is only auditing. An entry+ # Tier 1 archived or deleted is skipped too: _execute_gc_action renamed+ # or unlinked entry.path, and this is the pre-Tier-1 snapshot. The+ # check is on the path, not on entry.id: ids are a six-hex hash two+ # files can share, and an archive step that returned early (lock+ # timeout, OSError) leaves its file in place to be compressed (#1450).from src.config.accessor import get_env_configif not dry_run and get_env_config().memory.compression_enabled:try:@@ -256,7 +255,7 @@ class MemoryLifecycle:pipeline = CompressionPipeline(self._memory._dir)now_ts = time.time()for entry in entries:- if entry.id in gc_acted_ids:+ if not entry.path.exists():continuetarget = pipeline.should_compress(compression_level=entry.compression_level,diff --git a/agent/tests/test_memory_gc.py b/agent/tests/test_memory_gc.pyindex 441ed0a0c..8c7bfb17b 100644--- a/agent/tests/test_memory_gc.py+++ b/agent/tests/test_memory_gc.py@@ -260,6 +260,51 @@ class TestRunGC:assert "Cannot archive non-existent file" not in caplog.textassert "_write_compressed" not in caplog.text+ def test_gc_compresses_a_healthy_entry_that_shares_an_archived_entrys_id(+ self, tmp_path: Path, monkeypatch+ ) -> None:+ """The Tier 2 skip is keyed on the file, not on the entry id (#1450).++ Ids are a six-hex hash (``sha256(...)[:6]`` when the frontmatter has+ none), so two files can carry the same one. Skipping by id would leave+ the healthy file uncompressed because its twin was archived.+ """+ monkeypatch.setenv("VT_MEMORY_GC", "1")+ monkeypatch.setenv("VT_MEMORY_DECAY", "1")+ monkeypatch.setenv("VT_MEMORY_COMPRESSION", "1")+ now = time.time()+ old_iso = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(now - 30 * 86400))+ _create_memory_file(+ tmp_path,+ "old-and-stale",+ quality_score=0.01,+ access_count=0,+ created_at=old_iso,+ last_accessed=old_iso,+ entry_id="ab12cd",+ )+ healthy = _create_memory_file(+ tmp_path,+ "young-but-unread",+ content=" ".join(+ f"Sentence number {i} discusses trading topic {i} in detail." for i in range(12)+ ),+ keywords=["alpha", "momentum"],+ created_at=time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(now - 2 * 86400)),+ last_accessed=time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(now - 9 * 86400)),+ entry_id="ab12cd",+ )+ before = healthy.read_text(encoding="utf-8")+ lc = MemoryLifecycle(PersistentMemory(memory_dir=tmp_path))++ actions = lc.run_gc(dry_run=False)++ assert [a["action"] for a in actions] == ["archive"]+ assert (tmp_path / "archive" / "project_old-and-stale.md").exists()+ after = healthy.read_text(encoding="utf-8")+ assert after != before, "the healthy twin was skipped because it shares the archived id"+ assert "compression_level: daily" in after+# ---------------------------------------------------------------------------# 2. find_relevant importance weighting