Coverage for src/copytrading_engine/sources/session.py: 46%
157 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"""Own one Discord connection, its callbacks, recovery barrier, and shutdown."""
3import asyncio
4import datetime as dt
5import logging
6import sys
7from collections.abc import Awaitable, Callable, Set
8from functools import wraps
10import discord
11from pydantic import JsonValue
13from copytrading_engine.sources.application import (
14 AttachmentReference,
15 ForwardBatch,
16 RecoveryCapture,
17 SourceCapture,
18)
19from copytrading_engine.sources.recovery import RejectedCapture
20from copytrading_engine.sources.source import (
21 InvalidSourceMessage,
22 adapter_event,
23 attachment_references,
24 envelope,
25 require_history_channel,
26)
28log = logging.getLogger(__name__)
31def _allowed_author(message: discord.Message, authors: Set[int] | None) -> bool:
32 return authors is None or getattr(getattr(message, "author", None), "id", None) in authors
35async def capture_message(
36 message: discord.Message, box: SourceCapture, *, authors: Set[int] | None = None
37) -> None:
38 original_event = adapter_event(message)
39 references = attachment_references(message)
40 try:
41 event = envelope(message)
42 except InvalidSourceMessage as error:
43 await box.reject(
44 error.event, error.reason, source_event=original_event, attachments=references
45 )
46 log.error(
47 "source_message_rejected channel=%s id=%s reason=%s",
48 message.channel.id,
49 message.id,
50 error.reason,
51 )
52 return
53 if _allowed_author(message, authors):
54 await box.add(event, source_event=original_event, attachments=references)
55 else:
56 payload = event.model_dump(mode="json")
57 await box.reject(
58 payload, "author_not_configured", source_event=original_event, attachments=references
59 )
62async def recover_recent_messages(
63 client: discord.Client,
64 channels: Set[int],
65 box: RecoveryCapture,
66 *,
67 now: dt.datetime | None = None,
68 authors: Set[int] | None = None,
69) -> None:
70 """Page every accessible message after each durable channel cursor."""
71 observed_at = now if now is not None else dt.datetime.now(dt.UTC)
72 # Bound this pass, but commit only IDs returned by Discord. A clock-derived
73 # fence is not proof that messages up to that ID have been captured.
74 fence_id = discord.utils.time_snowflake(observed_at)
75 fallback_id = discord.utils.time_snowflake(observed_at - dt.timedelta(seconds=120))
76 for channel_id in sorted(channels):
77 channel = client.get_channel(channel_id)
78 if channel is None: 78 ↛ 79line 78 didn't jump to line 79 because the condition on line 78 was never true
79 channel = await client.fetch_channel(channel_id)
80 source = require_history_channel(channel)
81 start = await box.recovery_start(channel_id, fallback_id)
82 last_id, bootstrap = start.last_id, start.bootstrap
83 if last_id > fence_id: 83 ↛ 84line 83 didn't jump to line 84 because the condition on line 83 was never true
84 raise RuntimeError("Recovery cursor is ahead of the source clock")
85 log.info("source_recovery_started channel=%s bootstrap=%s", channel_id, bootstrap)
86 while last_id < fence_id: 86 ↛ 138line 86 didn't jump to line 138 because the condition on line 86 was always true
87 events = []
88 rejected: list[RejectedCapture] = []
89 source_events: dict[str, JsonValue] = {}
90 attachments: dict[str, tuple[AttachmentReference, ...]] = {}
91 page_last = last_id
92 async for message in source.history(
93 after=discord.Object(id=last_id),
94 before=discord.Object(id=fence_id),
95 oldest_first=True,
96 limit=100,
97 ):
98 if not last_id < message.id < fence_id or message.id <= page_last: 98 ↛ 99line 98 didn't jump to line 99 because the condition on line 98 was never true
99 raise RuntimeError("Discord history page is unordered or outside its cursor")
100 page_last = message.id
101 original_event = adapter_event(message)
102 references = attachment_references(message)
103 try:
104 event = envelope(message)
105 source_events[event.identity] = original_event
106 if _allowed_author(message, authors): 106 ↛ 107line 106 didn't jump to line 107 because the condition on line 106 was never true
107 events.append(event)
108 attachments[event.identity] = references
109 else:
110 attachments[event.identity] = references
111 rejected.append(
112 RejectedCapture(event.model_dump(mode="json"), "author_not_configured")
113 )
114 except InvalidSourceMessage as error:
115 identity = (
116 f"{error.event['source']}:{error.event['channel_id']}:{error.event['id']}"
117 )
118 source_events[identity] = original_event
119 attachments[identity] = references
120 rejected.append(RejectedCapture(error.event, error.reason))
121 log.error(
122 "source_message_rejected channel=%s id=%s reason=%s",
123 channel_id,
124 message.id,
125 error.reason,
126 )
127 if page_last == last_id:
128 break
129 await box.capture_recovery_page(
130 channel_id,
131 events,
132 rejected,
133 page_last,
134 source_events=source_events,
135 attachments=attachments,
136 )
137 last_id = page_last
138 log.info("source_recovery_complete channel=%s cursor=%s", channel_id, last_id)
141def token_rejected(error: BaseException | None) -> bool:
142 """Discord refused the token itself, so trying again cannot help until a new one is pasted."""
143 return isinstance(error, discord.LoginFailure)
146class DiscordSession:
147 """Expose lifecycle operations while keeping recovery and task state private."""
149 def __init__(
150 self,
151 client: discord.Client,
152 channels: Set[int],
153 box: RecoveryCapture,
154 stop: asyncio.Event,
155 *,
156 report_failure: Callable[[str, BaseException | None], None],
157 authors: Set[int] | None = None,
158 ) -> None:
159 self._client = client
160 self._channels = frozenset(channels)
161 self._authors = frozenset(authors) if authors is not None else None
162 self._box = box
163 self._stop = stop
164 self._report_failure = report_failure
165 self._ready = asyncio.Event()
166 self._publication_lock = asyncio.Lock()
167 self._generation = 0
168 self._closing = False
169 self._callbacks: set[asyncio.Task] = set()
170 self._connection: asyncio.Task[None] | None = None
171 self._register("on_message", self._on_message)
172 self._register("on_error", self._on_error)
173 self._register("on_ready", self._recover)
174 self._register("on_resumed", self._recover)
175 self._register("on_disconnect", self._on_disconnect)
177 @property
178 def ready(self) -> bool:
179 return not self._closing and self._client.is_ready() and self._ready.is_set()
181 def start(self, token: str) -> None:
182 """Start once; the caller must register close() before invoking this."""
183 if self._connection is not None or self._closing:
184 raise RuntimeError("Discord session cannot be started again")
185 self._connection = asyncio.create_task(self._client.start(token))
187 async def ensure_running(self) -> None:
188 if self._connection is None:
189 raise RuntimeError("Discord session has not started")
190 if self._connection.done():
191 await self._connection
192 raise RuntimeError("Discord connection stopped")
194 async def forward_if_ready(self, forwarder: ForwardBatch) -> None:
195 """Hold the recovery barrier for the complete assigned delivery batch."""
196 if not self.ready or self._stop.is_set():
197 return
198 async with self._publication_lock:
199 if self.ready and not self._stop.is_set():
200 await forwarder.flush()
202 async def close(self) -> None:
203 self._closing = True
204 self._pause()
205 try:
206 await self._client.close()
207 finally:
208 tasks = list(self._callbacks)
209 if self._connection is not None:
210 tasks.append(self._connection)
211 for task in tasks:
212 task.cancel()
213 await asyncio.gather(*tasks, return_exceptions=True)
215 def _pause(self) -> int:
216 self._generation += 1
217 self._ready.clear()
218 return self._generation
220 def _register[**P](self, name: str, callback: Callable[P, Awaitable[None]]) -> None:
221 @wraps(callback)
222 async def tracked(*args: P.args, **kwargs: P.kwargs) -> None:
223 if self._closing:
224 return
225 task = asyncio.current_task()
226 if task is None:
227 raise RuntimeError("Source callback has no owning task")
228 self._callbacks.add(task)
229 try:
230 await callback(*args, **kwargs)
231 finally:
232 self._callbacks.discard(task)
234 # Discord dispatches by the callback's name; methods remain private to this owner.
235 tracked.__name__ = name
236 self._client.event(tracked)
238 async def _on_message(self, message: discord.Message) -> None:
239 if message.channel.id in self._channels:
240 await capture_message(message, self._box, authors=self._authors)
242 async def _on_error(self, event: str, *args: object, **kwargs: object) -> None:
243 del args, kwargs # Discord callback values may contain private source data.
244 self._report_failure(f"source_callback_failed callback={event}", sys.exception())
245 self._stop.set()
247 async def _recover(self) -> None:
248 generation = self._pause()
249 async with self._publication_lock:
250 log.info("Discord connected; subscribed channels=%d", len(self._channels))
251 await recover_recent_messages(
252 self._client, self._channels, self._box, authors=self._authors
253 )
254 if (
255 generation == self._generation
256 and self._client.is_ready()
257 and not self._stop.is_set()
258 and not self._closing
259 ):
260 self._ready.set()
262 async def _on_disconnect(self) -> None:
263 self._pause()