"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(live): reject malformed recovery counters
Miranda7170e5d1f542
diff --git a/agent/src/live/daily_count.py b/agent/src/live/daily_count.pyindex db5cfb339..5d02c1b14 100644--- a/agent/src/live/daily_count.py+++ b/agent/src/live/daily_count.py@@ -12,6 +12,7 @@ import jsonimport osimport threadingfrom contextlib import contextmanager+from datetime import date as calendar_datefrom datetime import datetime, timezonefrom typing import BinaryIO, Iterator@@ -136,6 +137,8 @@ def increment_daily_count(broker: str, action_id: str | None = None) -> int:raisecounter_date = Noneaction_ids = []+ if action_id is not None and counter_date is not None and counter_date > today:+ raise DailyCountError("daily order count date is in the future")count = read_daily_count(broker)if action_id is not None and action_id in action_ids:return count@@ -145,12 +148,12 @@ def increment_daily_count(broker: str, action_id: str | None = None) -> int:if action_id is not None:action_ids.append(action_id)path = _counter_path(broker)- path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)tmp = path.with_name(f".{path.name}.{os.getpid()}.{threading.get_ident()}.tmp")payload = json.dumps({"date": today, "count": count, "action_ids": action_ids}, ensure_ascii=False)try:+ path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)with tmp.open("x", encoding="utf-8", newline="\n") as handle:handle.write(payload + "\n")handle.flush()@@ -182,11 +185,21 @@ def _read_action_ids(broker: str) -> tuple[str | None, list[str]]:if not isinstance(raw, dict):raise DailyCountError("daily order count has an invalid schema")date, count, values = raw.get("date"), raw.get("count"), raw.get("action_ids", [])+ try:+ valid_date = (+ isinstance(date, str)+ and calendar_date.fromisoformat(date).isoformat() == date+ )+ except ValueError:+ valid_date = Falseif (- not isinstance(date, str) or not date+ not valid_dateor isinstance(count, bool) or not isinstance(count, int) or count < 0or not isinstance(values, list)- or any(not isinstance(value, str) or not value for value in values)+ or any(+ not isinstance(value, str) or not (1 <= len(value) <= 128)+ for value in values+ )or len(set(values)) != len(values)or len(values) > count):diff --git a/agent/tests/test_daily_count.py b/agent/tests/test_daily_count.pyindex 01fee6635..0503dd033 100644--- a/agent/tests/test_daily_count.py+++ b/agent/tests/test_daily_count.py@@ -12,7 +12,12 @@ import pytestimport src.live.paths as live_pathsfrom src.live import daily_count-from src.live.daily_count import daily_order_lock, increment_daily_count, read_daily_count+from src.live.daily_count import (+ DailyCountError,+ daily_order_lock,+ increment_daily_count,+ read_daily_count,+)pytestmark = pytest.mark.unit@@ -87,3 +92,36 @@ def test_action_id_deduplicates_after_utc_rollover(tmp_path, monkeypatch) -> Non"count": 1,"action_ids": ["act_new"],}+++ ("counter_date", "action_ids"),+ [+ ("not-a-date", ["act_recovered"]),+ ("9999-12-31", ["act_recovered"]),+ ("2026-08-25", ["x" * 129]),+ ],+)+def test_action_id_accounting_rejects_malformed_evidence(+ tmp_path, monkeypatch, counter_date, action_ids+) -> None:+ runtime_root = tmp_path / ".vibe-trading"+ monkeypatch.setattr(live_paths, "get_runtime_root", lambda: runtime_root)+ path = runtime_root / "live" / "alpaca" / "trade_counter.json"+ path.parent.mkdir(parents=True)+ original = json.dumps({"date": counter_date, "count": 1, "action_ids": action_ids})+ path.write_text(original, encoding="utf-8")++ with pytest.raises(DailyCountError):+ increment_daily_count("alpaca", action_id="act_recovered")++ assert path.read_text(encoding="utf-8") == original+++def test_action_id_accounting_wraps_directory_failure(tmp_path, monkeypatch) -> None:+ runtime_root = tmp_path / ".vibe-trading"+ runtime_root.write_text("not a directory", encoding="utf-8")+ monkeypatch.setattr(live_paths, "get_runtime_root", lambda: runtime_root)++ with pytest.raises(DailyCountError):+ increment_daily_count("alpaca", action_id="act_recovered")