Skip to content

"Vibe-Trading: Your Personal Trading Agent"

test: reproduce cache regression with existing public consumer

Rudy Mizrahi Celeklid918c5c89cd4
diff --git a/agent/tests/test_local_loader_cache_identity.py b/agent/tests/test_local_loader_cache_identity.py
index df90eb38c..91130d367 100644
--- a/agent/tests/test_local_loader_cache_identity.py
+++ b/agent/tests/test_local_loader_cache_identity.py
@@ -1,5 +1,6 @@
"""Local cache entries must identify the configured file and schema."""
+import json
from pathlib import Path
import pandas as pd
@@ -120,3 +121,61 @@ def test_ignored_yaml_metadata_does_not_break_local_fetch(
frame = local_loader.DataLoader().fetch(["AAA.US"], "2025-01-02", "2025-01-03")
assert frame["AAA.US"]["close"].tolist() == [10, 10]
+
+
+ "corruption",
+ ["null", "array", "index_names_length", "unhashable_index_column"],
+)
+def test_corrupt_local_cache_metadata_refetches_and_repairs(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, corruption: str
+) -> None:
+ """A corrupt optional cache must not hide an available local CSV source."""
+ monkeypatch.setattr(base, "loader_cache_enabled", lambda: True)
+ monkeypatch.setattr(base, "loader_cache_root", lambda: tmp_path / "cache")
+ path = tmp_path / "bars.csv"
+ _write(path, 10)
+ monkeypatch.setattr(
+ local_loader, "_load_config", lambda: {"sources": [_source(path)]}
+ )
+
+ def fetch() -> pd.DataFrame:
+ return local_loader.DataLoader().fetch(["AAA.US"], "2025-01-02", "2025-01-03")[
+ "AAA.US"
+ ]
+
+ assert fetch()["close"].tolist() == [10, 10]
+ metadata_paths = list((tmp_path / "cache").rglob("*.json"))
+ assert len(metadata_paths) == 1, "exercise one real persisted cache entry"
+ metadata_path = metadata_paths[0]
+ original = json.loads(metadata_path.read_text())
+ assert original["index_columns"], "exercise actual persisted index metadata"
+ malformed = dict(original)
+ if corruption == "null":
+ malformed = None
+ elif corruption == "array":
+ malformed = []
+ elif corruption == "index_names_length":
+ malformed["index_names"] = ["date", "unexpected_extra_level"]
+ else:
+ malformed["index_columns"] = [{"not": "a column name"}]
+ metadata_path.write_text(json.dumps(malformed))
+ _write(path, 20)
+
+ repaired = fetch()
+ assert repaired["close"].tolist() == [20, 20]
+ assert isinstance(repaired.index, pd.DatetimeIndex)
+ assert repaired.index.tolist() == [
+ pd.Timestamp("2025-01-02"),
+ pd.Timestamp("2025-01-03"),
+ ]
+ restored = json.loads(metadata_path.read_text())
+ assert isinstance(restored, dict)
+ assert restored["index_columns"] == original["index_columns"]
+ assert restored["index_names"] == original["index_names"]
+
+ def unexpected_read(*args: object) -> None:
+ raise AssertionError("repaired valid cache should avoid reading the CSV again")
+
+ monkeypatch.setitem(local_loader._READERS, "csv", unexpected_read)
+ pd.testing.assert_frame_equal(repaired, fetch())