Coverage for src/copytrading_engine/parsing/relay.py: 91%
34 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-16 16:55 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-16 16:55 +0000
1"""Replay-safe boundaries between capture, parsing, and downstream owners."""
3from collections.abc import Awaitable, Callable
4from typing import Protocol
6from copytrading_engine.parsing.contracts import PendingSignal
7from copytrading_engine.shared.notification_models import NotificationIntent
8from copytrading_engine.shared.raw_message import RawMessage
9from copytrading_engine.shared.signals import StockSignal
10from copytrading_engine.sources.application import Delivery
13class CapturedSources(Protocol):
14 async def claim_batch(self, limit: int) -> tuple[Delivery, ...]: ...
16 async def confirm(self, key: str) -> None: ...
19class ParserInbox(Protocol):
20 async def add(self, event: RawMessage) -> None: ...
23class SignalOutbox(Protocol):
24 async def claim_pending_signals(self, limit: int) -> tuple[PendingSignal, ...]: ...
26 async def confirm_signal(self, key: str) -> None: ...
29class NotificationOutbox(Protocol):
30 async def claim_pending_notifications(self, limit: int) -> tuple[NotificationIntent, ...]: ...
32 async def confirm_notification(self, key: str) -> None: ...
35async def accept_live_sources(
36 source: CapturedSources, parser: ParserInbox, *, limit: int = 100
37) -> int:
38 """Acknowledge capture only after the parser has durably accepted its identity."""
39 deliveries = await source.claim_batch(limit)
40 for delivery in deliveries:
41 if delivery.mode != "live": 41 ↛ 42line 41 didn't jump to line 42 because the condition on line 41 was never true
42 raise RuntimeError("Historical capture reached the live parser relay")
43 event = RawMessage.model_validate_json(delivery.payload)
44 if event.identity != delivery.key: 44 ↛ 45line 44 didn't jump to line 45 because the condition on line 44 was never true
45 raise ValueError("Source delivery identity does not match its payload")
46 await parser.add(event)
47 await source.confirm(delivery.key)
48 return len(deliveries)
51async def deliver_signals(
52 parser: SignalOutbox,
53 receive: Callable[[StockSignal], Awaitable[None]],
54 *,
55 limit: int = 100,
56) -> int:
57 """Confirm only after a downstream owner has durably accepted the result."""
58 deliveries = await parser.claim_pending_signals(limit)
59 for pending in deliveries:
60 await receive(pending.signal)
61 await parser.confirm_signal(pending.key)
62 return len(deliveries)
65async def deliver_notifications(
66 parser: NotificationOutbox,
67 send: Callable[[NotificationIntent], Awaitable[None]],
68 *,
69 limit: int = 100,
70) -> int:
71 """A transport may resend after a crash; the key remains stable for dedupe."""
72 deliveries = await parser.claim_pending_notifications(limit)
73 for notification in deliveries:
74 await send(notification)
75 await parser.confirm_notification(notification.key)
76 return len(deliveries)