mirror of
https://github.com/TauricResearch/TradingAgents.git
synced 2026-11-22 17:19:56 +02:00
fix(memory): settle each decision once, and keep each outcome as soon as it is known
- a settlement pass holds the log's settlement lock, so runs sharing a log never pay for the same reflection twice; only outcomes actually written count as settled, and a run's Memory Log step does not wait for another run's pass - each outcome is written when its reflection is done, so an interrupted pass keeps what it finished - any failure to price a decision is reported as a failed settlement, not taken for a window that has yet to trade
This commit is contained in:
+111
-1
@@ -1183,8 +1183,118 @@ def test_the_graph_settles_every_ticker_under_its_own_config(tmp_path, monkeypat
|
||||
graph.memory_log, graph.reflector = make_log(tmp_path), object()
|
||||
seen = []
|
||||
monkeypatch.setattr(settlement, "settle_all_pending",
|
||||
lambda log, reflector, config: seen.append(get_config()["output_language"])
|
||||
lambda log, reflector, config, wait=True: seen.append(get_config()["output_language"])
|
||||
or settlement.Settlement(settled=[("AAPL", "2026-01-05")]))
|
||||
|
||||
assert graph.settle_all_pending().settled == [("AAPL", "2026-01-05")]
|
||||
assert seen == ["Deutsch"]
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestSettlementOnceAndDurably:
|
||||
"""Settlement is paid for in reflection calls, so it runs once per decision and
|
||||
keeps what it finished; what it could not do is reported (#1428, #1445)."""
|
||||
|
||||
def _log(self, tmp_path, monkeypatch, *dates):
|
||||
log = make_log(tmp_path)
|
||||
for date in dates:
|
||||
log.store_decision("AAPL", date, DECISION_BUY)
|
||||
series = pd.Series([100.0 + i for i in range(40)], index=pd.bdate_range("2026-01-02", periods=40))
|
||||
monkeypatch.setattr(settlement, "get_closes", _closes({"AAPL": series, "SPY": series}))
|
||||
return log
|
||||
|
||||
def test_two_runs_settling_at_once_reflect_once(self, tmp_path, monkeypatch):
|
||||
import threading
|
||||
import time
|
||||
|
||||
self._log(tmp_path, monkeypatch, "2026-01-05")
|
||||
calls = []
|
||||
|
||||
class _Slow:
|
||||
def reflect_on_final_decision(self, **kwargs):
|
||||
calls.append(1)
|
||||
time.sleep(0.2)
|
||||
return "Held up."
|
||||
|
||||
results = []
|
||||
threads = [threading.Thread(target=lambda: results.append(
|
||||
settlement.settle_pending("AAPL", make_log(tmp_path), _Slow(), {"holding_period_days": 5})))
|
||||
for _ in range(2)]
|
||||
[t.start() for t in threads]
|
||||
[t.join() for t in threads]
|
||||
|
||||
assert len(calls) == 1
|
||||
assert sorted(len(r.settled) for r in results) == [0, 1]
|
||||
|
||||
def test_an_interrupted_pass_keeps_the_outcomes_it_finished(self, tmp_path, monkeypatch):
|
||||
log = self._log(tmp_path, monkeypatch, "2026-01-05", "2026-01-12")
|
||||
|
||||
class _Interrupted:
|
||||
calls = 0
|
||||
|
||||
def reflect_on_final_decision(self, **kwargs):
|
||||
_Interrupted.calls += 1
|
||||
if _Interrupted.calls == 2:
|
||||
raise KeyboardInterrupt
|
||||
return "Held up."
|
||||
|
||||
with pytest.raises(KeyboardInterrupt):
|
||||
settlement.settle_pending("AAPL", log, _Interrupted(), {"holding_period_days": 5})
|
||||
|
||||
assert [e["date"] for e in log.get_pending_entries()] == ["2026-01-12"]
|
||||
|
||||
def test_a_price_request_that_fails_is_reported_not_taken_for_not_due(self, tmp_path, monkeypatch):
|
||||
from tradingagents.dataflows.errors import VendorUnavailableError
|
||||
|
||||
log = self._log(tmp_path, monkeypatch, "2026-01-05")
|
||||
|
||||
def down(*args, **kwargs):
|
||||
raise VendorUnavailableError("Yahoo Finance request failed: TimeoutError")
|
||||
|
||||
monkeypatch.setattr(settlement, "get_closes", down)
|
||||
|
||||
result = settlement.settle_pending("AAPL", log, object(), {"holding_period_days": 5})
|
||||
|
||||
assert result.settled == [] and "prices unavailable" in result.failed[0][2]
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_any_failure_to_price_a_decision_is_reported(tmp_path, monkeypatch):
|
||||
log = make_log(tmp_path)
|
||||
log.store_decision("AAPL", "2026-01-05", DECISION_BUY)
|
||||
|
||||
def broken(*args, **kwargs):
|
||||
raise RuntimeError("vendor outage")
|
||||
|
||||
monkeypatch.setattr(settlement, "get_closes", broken)
|
||||
|
||||
result = settlement.settle_pending("AAPL", log, object(), {"holding_period_days": 5})
|
||||
|
||||
assert result.settled == [] and "vendor outage" in result.failed[0][2]
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_a_run_does_not_wait_on_another_settlement_pass(tmp_path):
|
||||
"""The other pass settles the same log; a run's step goes on without waiting."""
|
||||
import threading
|
||||
import time
|
||||
|
||||
log = make_log(tmp_path)
|
||||
log.store_decision("AAPL", "2026-01-05", DECISION_BUY)
|
||||
held, release = threading.Event(), threading.Event()
|
||||
|
||||
def other_pass():
|
||||
with make_log(tmp_path).settling():
|
||||
held.set()
|
||||
release.wait(5)
|
||||
|
||||
worker = threading.Thread(target=other_pass)
|
||||
worker.start()
|
||||
held.wait(5)
|
||||
started = time.monotonic()
|
||||
result = settlement.settle_all_pending(log, object(), {"holding_period_days": 5}, wait=False)
|
||||
elapsed = time.monotonic() - started
|
||||
release.set()
|
||||
worker.join()
|
||||
|
||||
assert elapsed < 1 and result.settled == [] and result.failed == []
|
||||
|
||||
Reference in New Issue
Block a user