fix: fence ledger outbox claims by instance

This commit is contained in:
Hermes Agent
2026-09-13 09:41:51 +09:00
parent f801c53e0c
commit 762a953c95
15 changed files with 316 additions and 88 deletions
+8 -8
View File
@@ -39,7 +39,7 @@ def _store_with(pid="PF-1", cash=1_000_000.0, qty=0.0):
# --- write-back (REQ-019/030) ----------------------------------------------
def test_writeback_updates_snapshot_on_new():
store = _store_with()
outbox = InMemoryLedgerOutbox()
outbox = InMemoryLedgerOutbox(instance_key="test-instance")
ledger = InMemoryLedger(cash_after=300_000.0, quantity_after=10.0)
writer = LedgerWriter(outbox, ledger, snapshot_store=store)
outbox.enqueue(_trade())
@@ -50,7 +50,7 @@ def test_writeback_updates_snapshot_on_new():
def test_writeback_applies_on_duplicate_too():
store = _store_with(cash=1_000_000.0)
outbox = InMemoryLedgerOutbox()
outbox = InMemoryLedgerOutbox(instance_key="test-instance")
# execution already recorded once → second write is duplicate, *_after still authoritative
ledger = InMemoryLedger(cash_after=300_000.0, quantity_after=10.0)
ledger.record_trade(_trade(execution_id="dup")) # pre-record → next is duplicate
@@ -62,7 +62,7 @@ def test_writeback_applies_on_duplicate_too():
def test_writeback_skipped_when_after_missing():
store = _store_with(cash=1_000_000.0)
outbox = InMemoryLedgerOutbox()
outbox = InMemoryLedgerOutbox(instance_key="test-instance")
ledger = InMemoryLedger() # no cash_after/quantity_after → None
writer = LedgerWriter(outbox, ledger, snapshot_store=store)
outbox.enqueue(_trade())
@@ -74,7 +74,7 @@ def test_writeback_best_effort_does_not_block_completion():
class _BoomStore:
def update_cash_position(self, *a, **k):
raise RuntimeError("boom")
outbox = InMemoryLedgerOutbox()
outbox = InMemoryLedgerOutbox(instance_key="test-instance")
ledger = InMemoryLedger(cash_after=1.0, quantity_after=1.0)
writer = LedgerWriter(outbox, ledger, snapshot_store=_BoomStore())
outbox.enqueue(_trade())
@@ -83,7 +83,7 @@ def test_writeback_best_effort_does_not_block_completion():
def test_no_writeback_without_store():
outbox = InMemoryLedgerOutbox()
outbox = InMemoryLedgerOutbox(instance_key="test-instance")
ledger = InMemoryLedger(cash_after=1.0, quantity_after=1.0)
writer = LedgerWriter(outbox, ledger) # no snapshot_store
outbox.enqueue(_trade())
@@ -92,7 +92,7 @@ def test_no_writeback_without_store():
# --- estimate overlay (REQ-029 / OQ-4) — via make_slots_rpc_handler ----------
def test_pending_cash_delta_sums_by_asset():
outbox = InMemoryLedgerOutbox()
outbox = InMemoryLedgerOutbox(instance_key="test-instance")
outbox.enqueue(_trade(execution_id="b", side="buy", qty=10, price=1000.0, fee=1.0))
deltas = outbox.pending_cash_delta()
assert deltas["PF-1"] == trade_cash_delta(_trade(side="buy", qty=10, price=1000.0, fee=1.0))
@@ -101,7 +101,7 @@ def test_pending_cash_delta_sums_by_asset():
def test_apply_cash_estimate_overlays_pending_delta():
"""Cash-estimated overlay applied through slots handler (REQ-029 OQ-4)."""
outbox = InMemoryLedgerOutbox()
outbox = InMemoryLedgerOutbox(instance_key="test-instance")
outbox.enqueue(_trade(execution_id="b", side="buy", qty=10, price=1000.0, fee=1.0))
store = _store_with(pid="PF-1", cash=1_000_000.0, qty=0.0)
repo = InMemorySlotRepository()
@@ -117,7 +117,7 @@ def test_apply_cash_estimate_overlays_pending_delta():
def test_estimate_clears_after_writeback():
"""Once the ledger write completes (no longer pending), the estimate is gone."""
store = _store_with(cash=1_000_000.0)
outbox = InMemoryLedgerOutbox()
outbox = InMemoryLedgerOutbox(instance_key="test-instance")
ledger = InMemoryLedger(cash_after=989_999.0, quantity_after=10.0)
writer = LedgerWriter(outbox, ledger, snapshot_store=store)
outbox.enqueue(_trade(side="buy", qty=10, price=1000.0, fee=1.0))