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:
Yijia-Xiao
2026-10-03 21:27:43 +00:00
parent 460b82e20d
commit 11dab3b0fe
6 changed files with 270 additions and 106 deletions
+38 -2
View File
@@ -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