mirror of
https://github.com/TauricResearch/TradingAgents.git
synced 2026-11-21 16:49:57 +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:
@@ -93,12 +93,12 @@ def _fake_msvcrt(results):
|
||||
|
||||
def locking(fd, mode, nbytes):
|
||||
calls.append(mode)
|
||||
if mode == 2 and results: # LK_LOCK; unlocking always succeeds
|
||||
if mode in (1, 2) and results: # LK_NBLCK, LK_LOCK; unlocking always succeeds
|
||||
error = results.pop(0)
|
||||
if error is not None:
|
||||
raise error
|
||||
|
||||
return types.SimpleNamespace(LK_LOCK=2, LK_UNLCK=0, locking=locking), calls
|
||||
return types.SimpleNamespace(LK_LOCK=2, LK_NBLCK=1, LK_UNLCK=0, locking=locking), calls
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@@ -126,3 +126,39 @@ def test_on_windows_a_lock_that_cannot_be_taken_is_reported_not_retried_forever(
|
||||
with pytest.raises(OSError, match="bad handle"), open(tmp_path / "log.md.lock", "a+b") as handle, \
|
||||
files._hold_windows(handle):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_a_lock_held_elsewhere_is_reported_when_not_waiting(tmp_path):
|
||||
import threading
|
||||
|
||||
path = tmp_path / "log.md"
|
||||
held, release = threading.Event(), threading.Event()
|
||||
|
||||
def holder():
|
||||
with files.locked(path):
|
||||
held.set()
|
||||
release.wait(5)
|
||||
|
||||
worker = threading.Thread(target=holder)
|
||||
worker.start()
|
||||
held.wait(5)
|
||||
with files.locked(path, wait=False) as acquired:
|
||||
assert acquired is False
|
||||
release.set()
|
||||
worker.join()
|
||||
with files.locked(path, wait=False) as acquired:
|
||||
assert acquired is True
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_on_windows_a_held_lock_is_reported_when_not_waiting(tmp_path, monkeypatch):
|
||||
import errno
|
||||
import sys
|
||||
|
||||
fake, calls = _fake_msvcrt([OSError(errno.EACCES, "held")])
|
||||
monkeypatch.setitem(sys.modules, "msvcrt", fake)
|
||||
|
||||
with open(tmp_path / "log.md.lock", "a+b") as handle, files._hold_windows(handle, wait=False) as acquired:
|
||||
assert acquired is False
|
||||
assert calls == [1] # one try, no unlock of a lock it never held
|
||||
|
||||
+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