Coverage for src/copytrading_engine/trading/application/operator_service.py: 86%

272 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-16 16:55 +0000

1"""Operator read models over live owners, retained ledgers, and source evidence.""" 

2 

3import asyncio 

4import datetime as dt 

5import logging 

6from dataclasses import dataclass, field 

7from decimal import Decimal 

8from pathlib import Path 

9 

10from copytrading_engine.execution.application.manual_commands import ( 

11 manual_command_page_from_snapshot, 

12) 

13from copytrading_engine.execution.application.ports import ACCOUNT_FAILURES 

14from copytrading_engine.execution.domain.ledger_state import ( 

15 HELD_FOR_OWNER, 

16 EarlierLedgerVersion, 

17 LedgerSnapshot, 

18) 

19from copytrading_engine.execution.domain.manual_commands import ( 

20 ManualCommandPage, 

21 ManualCommandPageRequest, 

22 ManualRefusal, 

23) 

24from copytrading_engine.execution.domain.market import ( 

25 Bar, 

26 DailyBar, 

27 DailyRange, 

28 EquityHistory, 

29 HistoryWindow, 

30 Quote, 

31) 

32from copytrading_engine.execution.domain.sessions import ET, trade_date 

33from copytrading_engine.execution.presentation.account_feed import AccountFeedPage 

34from copytrading_engine.execution.presentation.guru_performance import ( 

35 GuruPerformance, 

36 guru_performance, 

37) 

38from copytrading_engine.execution.presentation.operator_views import ( 

39 AccountEventPage, 

40 AccountOverview, 

41 AccountOverviewPage, 

42 AccountUnavailable, 

43 DestinationView, 

44 UnavailableReason, 

45 destination_views, 

46 with_live_facts, 

47) 

48from copytrading_engine.shared.route_keys import choose_route, guru_for_channel, route_key 

49from copytrading_engine.trading.application.account_access import ( 

50 AccountAccess, 

51 OwnerReads, 

52 OwnerUnavailable, 

53) 

54from copytrading_engine.trading.application.paused_reader import ( 

55 PausedAccountReader, 

56 ReadKeys, 

57 ReadOnlyBroker, 

58) 

59from copytrading_engine.trading.presentation.operator_models import ( 

60 GuruDayStats, 

61 GuruDayStatsPage, 

62 SourceActivity, 

63 SourceActivityPage, 

64) 

65 

66log = logging.getLogger(__name__) 

67 

68# Orders the owner placed by hand for a post; a call they entered themselves no longer waits. 

69_OWNER_ENTRY = "manual-" 

70 

71 

72@dataclass 

73class _GuruTally: 

74 posts: int = 0 

75 traded: int = 0 

76 reads: list[float] = field(default_factory=list) 

77 waiting: set[str] = field(default_factory=set) 

78 

79 

80def _traded(view: DestinationView) -> bool: 

81 """The account bought or sold on the post: an order of it filled, in whole or in part.""" 

82 return any(order.status == "filled" or order.filled_quantity > 0 for order in view.orders) 

83 

84 

85def _waits(views: list[DestinationView]) -> bool: 

86 """A call of the post waits for the owner (ADR-0007): an account holds it for them, and they 

87 have not entered the trade by hand.""" 

88 held = any( 

89 view.status == "review_required" 

90 or any(outcome in HELD_FOR_OWNER for outcome in view.instruction_outcomes) 

91 for view in views 

92 ) 

93 entered = any( 

94 order.client_id.startswith(_OWNER_ENTRY) for view in views for order in view.orders 

95 ) 

96 return held and not entered 

97 

98 

99def _no_broker(keys: ReadKeys) -> ReadOnlyBroker: 

100 raise RuntimeError("This engine reads no broker while copying is paused") 

101 

102 

103class OperatorQueryService: 

104 """Answer the native app's paged account, event, source, and command-history reads.""" 

105 

106 def __init__(self, access: AccountAccess, reader: PausedAccountReader | None = None) -> None: 

107 self._access = access 

108 # What each running account last said of itself: a worker busy placing orders may not 

109 # answer within a read's time, and is still copying. 

110 self._readiness: dict[str, str] = {} 

111 self._owner_reads = OwnerReads() 

112 # With no reader given, paused accounts are read from their ledgers alone. 

113 self._reader = reader if reader is not None else PausedAccountReader(_no_broker) 

114 

115 def attach_readers(self, keys: tuple[ReadKeys, ...]) -> None: 

116 """Read paused accounts live with these keys until `detach_readers`.""" 

117 self._reader.attach(keys) 

118 

119 def detach_readers(self) -> None: 

120 self._reader.detach() 

121 

122 async def _live(self, account_id: str, overview: AccountOverview) -> AccountOverview: 

123 """A retained overview with the broker's balance and valuations, when they can be read.""" 

124 facts = await self._reader.facts(account_id) 

125 if facts is None: 

126 return overview 

127 return with_live_facts(overview, facts.account, facts.positions, facts.observed_at) 

128 

129 def _unavailable_readiness(self, account_id: str) -> str: 

130 """Why an account is not running, when its owner can act on it; else just unavailable.""" 

131 supervisor = self._access.supervisors().get(account_id) 

132 code = ( 

133 supervisor.error_code 

134 if supervisor is not None 

135 else self._access.account_error(account_id) 

136 ) 

137 return code if code in ACCOUNT_FAILURES else "account_unavailable" 

138 

139 async def account_overviews( 

140 self, before_account_id: str | None = None, limit: int = 50 

141 ) -> AccountOverviewPage: 

142 """Return a bounded descending keyset page; no cross-page snapshot is implied.""" 

143 if type(limit) is not int or not 1 <= limit <= 100: 

144 raise ValueError("Invalid account overview page") 

145 if before_account_id is not None and ( 

146 not isinstance(before_account_id, str) or not 1 <= len(before_account_id) <= 64 

147 ): 

148 raise ValueError("Invalid account overview page") 

149 configuration = self._access.configuration() 

150 configured = {item.id: item for item in configuration.accounts} if configuration else {} 

151 eligible_configured = { 

152 account_id 

153 for account_id in configured 

154 if before_account_id is None or account_id < before_account_id 

155 } 

156 paths = self._access.retained_page_paths( 

157 before_account_id, limit + 1 + len(eligible_configured) 

158 ) 

159 

160 async def read_one( 

161 account_id: str, 

162 ) -> tuple[AccountOverview | None, AccountUnavailable | None]: 

163 failure: UnavailableReason | None = None 

164 database = paths.get(account_id) 

165 supervisor = self._access.supervisors().get(account_id) 

166 if ( 

167 supervisor is not None 

168 and supervisor.owner is not None 

169 and supervisor.state != "failed" 

170 ): 

171 owner = supervisor.owner 

172 live = await self._owner_reads.read( 

173 account_id, owner, "overview", owner.operator_overview 

174 ) 

175 if not isinstance(live, OwnerUnavailable): 

176 self._readiness[account_id] = live.readiness 

177 return live, None 

178 failure = live.reason 

179 if database is not None: 

180 try: 

181 overview, _, _ = await self._access.retained_account(database, timeout=2) 

182 except TimeoutError: 

183 failure = "timeout" 

184 except EarlierLedgerVersion: 

185 failure = "earlier_version" 

186 except Exception as exc: # noqa: BLE001 - one unreadable ledger must not hide others 

187 log.warning( 

188 "retained_account_read_failed id=%s type=%s error=%s", 

189 account_id, 

190 type(exc).__name__, 

191 exc, 

192 ) 

193 failure = "read_failed" 

194 else: 

195 overview = await self._live(account_id, overview) 

196 if account_id not in configured: 

197 return overview, None 

198 if self._access.runtime_state() in {"paused", "pausing"}: 198 ↛ 199line 198 didn't jump to line 199 because the condition on line 198 was never true

199 readiness = "processing_stopped" 

200 elif failure == "timeout" and account_id in self._readiness: 200 ↛ 203line 200 didn't jump to line 203 because the condition on line 200 was never true

201 # Its worker is running but busy, so the saved ledger stands in for the 

202 # holdings and the account's own last word for whether it copies. 

203 readiness = self._readiness[account_id] 

204 else: 

205 readiness = self._unavailable_readiness(account_id) 

206 return overview.model_copy( 

207 update={ 

208 "active_configuration": True, 

209 "readiness": readiness, 

210 "recovery_preference": configured[ 

211 account_id 

212 ].policy.recovery_preference, 

213 } 

214 ), None 

215 unavailable = ( 

216 AccountUnavailable(account_id=account_id, reason=failure) if failure else None 

217 ) 

218 if account_id not in configured: 

219 return None, unavailable 

220 account = configured[account_id] 

221 return AccountOverview( 

222 account_id=account_id, 

223 environment=account.environment, 

224 active_configuration=True, 

225 broker_identity="unverified", 

226 entry_permission="unknown", 

227 recovery_preference=account.policy.recovery_preference, 

228 readiness=( 

229 self._readiness[account_id] 

230 if failure == "timeout" and account_id in self._readiness 

231 else self._unavailable_readiness(account_id) 

232 ), 

233 account_risk_status="unavailable", 

234 account_risk_reason="account_unavailable", 

235 account_activity_status="unavailable", 

236 account_activity_reason="account_unavailable", 

237 total_exposure_usd=None, 

238 app_cost_basis_usd=None, 

239 positions=(), 

240 unresolved_incidents=(), 

241 ownership_incidents=(), 

242 pending_orders=None, 

243 balance=None, 

244 ), unavailable 

245 

246 candidates = sorted(set(paths) | eligible_configured, reverse=True) 

247 page_ids = candidates[: limit + 1] 

248 results = await asyncio.gather(*(read_one(account_id) for account_id in page_ids[:limit])) 

249 return AccountOverviewPage( 

250 items=tuple(item for item, _ in results if item is not None), 

251 next_before_account_id=page_ids[limit - 1] if len(page_ids) > limit else None, 

252 unavailable_accounts=tuple(gap for _, gap in results if gap is not None), 

253 ) 

254 

255 async def account_events( 

256 self, account_id: str, before_seq: int | None, limit: int 

257 ) -> AccountEventPage: 

258 if not 1 <= limit <= 100 or (before_seq is not None and before_seq < 1): 258 ↛ 259line 258 didn't jump to line 259 because the condition on line 258 was never true

259 raise ValueError("Invalid account event page") 

260 supervisor = self._access.supervisors().get(account_id) 

261 if supervisor is not None and supervisor.owner is not None and supervisor.state != "failed": 261 ↛ 262line 261 didn't jump to line 262 because the condition on line 261 was never true

262 owner = supervisor.owner 

263 live = await self._owner_reads.read( 

264 account_id, 

265 owner, 

266 ("events", before_seq, limit), 

267 lambda: owner.event_page(before_seq, limit), 

268 ) 

269 if not isinstance(live, OwnerUnavailable): 

270 return live 

271 database = self._access.retained_paths().get(account_id) 

272 if database is None: 272 ↛ 273line 272 didn't jump to line 273 because the condition on line 272 was never true

273 raise KeyError(account_id) 

274 _, _, page = await self._access.retained_account( 

275 database, before_seq=before_seq, limit=limit, timeout=2 

276 ) 

277 return page 

278 

279 async def account_feed( 

280 self, account_id: str, before_seq: int | None, limit: int 

281 ) -> AccountFeedPage: 

282 """The account's feed: from its running owner, or else from its retained ledger.""" 

283 if not 1 <= limit <= 100 or (before_seq is not None and before_seq < 1): 283 ↛ 284line 283 didn't jump to line 284 because the condition on line 283 was never true

284 raise ValueError("Invalid account feed page") 

285 supervisor = self._access.supervisors().get(account_id) 

286 if supervisor is not None and supervisor.owner is not None and supervisor.state != "failed": 286 ↛ 287line 286 didn't jump to line 287 because the condition on line 286 was never true

287 owner = supervisor.owner 

288 live = await self._owner_reads.read( 

289 account_id, 

290 owner, 

291 ("feed", before_seq, limit), 

292 lambda: owner.feed_page(before_seq, limit), 

293 ) 

294 if not isinstance(live, OwnerUnavailable): 

295 return live 

296 database = self._access.retained_paths().get(account_id) 

297 if database is None: 297 ↛ 298line 297 didn't jump to line 298 because the condition on line 297 was never true

298 raise KeyError(account_id) 

299 return await self._access.retained_feed( 

300 database, before_seq=before_seq, limit=limit, timeout=2 

301 ) 

302 

303 async def equity_history(self, account_id: str, window: HistoryWindow) -> EquityHistory | None: 

304 """The account's broker curve: from its running owner, else (paused, or an owner that 

305 cannot answer) from the read keys the app attached; None when neither can ask.""" 

306 configuration = self._access.configuration() 

307 configured = configuration is not None and account_id in { 

308 item.id for item in configuration.accounts 

309 } 

310 # An account the app lists from its record is known even before its keys are attached or 

311 # copying has started in this engine: it has no curve to show yet, which is not "not found". 

312 if ( 

313 not configured 

314 and not self._reader.holds(account_id) 

315 and account_id not in self._access.retained_paths() 

316 ): 

317 raise KeyError(account_id) 

318 supervisor = self._access.supervisors().get(account_id) 

319 if supervisor is not None and supervisor.owner is not None and supervisor.state != "failed": 

320 owner = supervisor.owner 

321 history = await self._owner_reads.read( 

322 account_id, 

323 owner, 

324 ("history", window), 

325 lambda: owner.equity_history(window, dt.datetime.now(dt.UTC)), 

326 ) 

327 if not isinstance(history, OwnerUnavailable): 327 ↛ 328line 327 didn't jump to line 328 because the condition on line 327 was never true

328 return history 

329 return await self._reader.equity_history(account_id, window) 

330 

331 async def quote(self, account_id: str, symbol: str) -> Quote | None: 

332 """A stock's latest quote through the account's read keys, for the trade sheet.""" 

333 if not self._reader.holds(account_id): 

334 raise KeyError(account_id) 

335 return await self._reader.quote(account_id, symbol) 

336 

337 async def bars(self, account_id: str, symbols: tuple[str, ...]) -> dict[str, tuple[Bar, ...]]: 

338 """Held stocks' five-minute bars through the account's read keys, for the Positions.""" 

339 if not self._reader.holds(account_id): 

340 raise KeyError(account_id) 

341 return await self._reader.bars(account_id, symbols) 

342 

343 async def daily_bars( 

344 self, account_id: str, symbol: str, period: DailyRange 

345 ) -> tuple[DailyBar, ...]: 

346 """A stock's daily bars through the account's read keys, for its page.""" 

347 if not self._reader.holds(account_id): 

348 raise KeyError(account_id) 

349 return await self._reader.daily_bars(account_id, symbol, period) 

350 

351 async def guru_performance(self, account_id: str, guru_id: str) -> GuruPerformance: 

352 """What one guru's copied calls made in one account: realized from its retained ledger 

353 and journal, unrealized from the open lots at the broker's price when it can be read.""" 

354 database = self._access.retained_paths().get(account_id) 

355 if database is None: 

356 # An account the setup lists has made nothing yet, which is not "not found". 

357 configuration = self._access.configuration() 

358 if self._reader.holds(account_id) or ( 

359 configuration is not None 

360 and account_id in {item.id for item in configuration.accounts} 

361 ): 

362 return guru_performance( 

363 LedgerSnapshot(), 

364 (), 

365 guru_id=guru_id, 

366 account_id=account_id, 

367 unrealized_usd=Decimal(0), 

368 ) 

369 raise KeyError(account_id) 

370 snapshot, events = await asyncio.wait_for( 

371 asyncio.to_thread(self._access.evidence.retained_fills, database), timeout=5 

372 ) 

373 overview, _, _ = await self._access.retained_account(database, timeout=2) 

374 lots = [ 

375 lot 

376 for position in (await self._live(account_id, overview)).positions 

377 for lot in position.lots 

378 if lot.guru_id == guru_id and lot.remaining_qty > 0 

379 ] 

380 unrealized = ( 

381 sum((lot.unrealized_pl or Decimal(0) for lot in lots), Decimal(0)) 

382 if all(lot.unrealized_pl is not None for lot in lots) 

383 else None 

384 ) 

385 return guru_performance( 

386 snapshot, events, guru_id=guru_id, account_id=account_id, unrealized_usd=unrealized 

387 ) 

388 

389 async def source_activity(self, before_seq: int | None, limit: int) -> SourceActivityPage: 

390 """A page of the posts of the channels the setup follows, newest first; with no setup 

391 yet, every post.""" 

392 configuration = self._access.configuration() 

393 channels = ( 

394 None 

395 if configuration is None 

396 else tuple(dict.fromkeys((r.source, r.channel_id) for r in configuration.routes)) 

397 ) 

398 page = await asyncio.wait_for( 

399 asyncio.to_thread( 

400 self._access.evidence.source_page, 

401 self._access.application_database, 

402 before_seq=before_seq, 

403 limit=limit, 

404 destinations={}, 

405 channels=channels, 

406 ), 

407 timeout=2, 

408 ) 

409 by_source, unavailable = await self._destinations({item.source_id for item in page.items}) 

410 return self._with_destinations(page, by_source, unavailable) 

411 

412 async def guru_activity( 

413 self, guru_id: str, before_seq: int | None, limit: int 

414 ) -> SourceActivityPage: 

415 """A page of one guru's posts, newest first, read from their channel in the setup; a guru 

416 the setup no longer has has none.""" 

417 configuration = self._access.configuration() 

418 channel = next( 

419 ( 

420 (route.source, route.channel_id) 

421 for route in (configuration.routes if configuration is not None else ()) 

422 if guru_for_channel(route.source, route.channel_id) == guru_id 

423 ), 

424 None, 

425 ) 

426 if channel is None: 

427 return SourceActivityPage(items=(), rejected_items=()) 

428 page = await asyncio.wait_for( 

429 asyncio.to_thread( 

430 self._access.evidence.source_page, 

431 self._access.application_database, 

432 before_seq=before_seq, 

433 limit=limit, 

434 destinations={}, 

435 channel=channel, 

436 ), 

437 timeout=2, 

438 ) 

439 by_source, unavailable = await self._destinations({item.source_id for item in page.items}) 

440 return self._with_destinations(page, by_source, unavailable) 

441 

442 def _with_destinations( 

443 self, 

444 page: SourceActivityPage, 

445 views_by_source: dict[str, list[DestinationView]], 

446 unavailable: tuple[AccountUnavailable, ...], 

447 ) -> SourceActivityPage: 

448 """The page's posts, each with what every account did with it, and a pending delivery for 

449 the accounts a route will still send it to.""" 

450 by_source = { 

451 item.source_id: list(views_by_source.get(item.source_id, [])) for item in page.items 

452 } 

453 configuration = self._access.configuration() 

454 if configuration is not None: 

455 accounts = {account.id: account for account in configuration.accounts} 

456 routes_by_key = { 

457 route_key(candidate.source, candidate.channel_id, candidate.author_id): candidate 

458 for candidate in configuration.routes 

459 } 

460 for item in page.items: 

461 if item.delivery_status != "pending": 

462 continue 

463 parts = item.source_id.split(":", 2) 

464 route = None 

465 if len(parts) == 3: 465 ↛ 467line 465 didn't jump to line 467 because the condition on line 465 was always true

466 route = choose_route(routes_by_key, "discord", parts[1], item.author_id).route 

467 if route is None: 467 ↛ 468line 467 didn't jump to line 468 because the condition on line 467 was never true

468 continue 

469 present = {view.account_id for view in by_source[item.source_id]} 

470 for connection in route.connections: 

471 if connection.account_id in present: 

472 continue 

473 by_source[item.source_id].append( 

474 DestinationView( 

475 account_id=connection.account_id, 

476 environment=accounts[connection.account_id].environment, 

477 status="pending_delivery", 

478 instruction_outcomes=(), 

479 limits_hit=(), 

480 orders=(), 

481 ) 

482 ) 

483 return page.model_copy( 

484 update={ 

485 "unavailable_accounts": unavailable, 

486 "items": tuple( 

487 item.model_copy( 

488 update={ 

489 "destinations": tuple( 

490 sorted( 

491 by_source[item.source_id], key=lambda value: value.account_id 

492 ) 

493 ) 

494 } 

495 ) 

496 for item in page.items 

497 ), 

498 } 

499 ) 

500 

501 async def _destinations( 

502 self, ids: set[str] 

503 ) -> tuple[dict[str, list[DestinationView]], tuple[AccountUnavailable, ...]]: 

504 """What each account did with each of these posts, from its owner or its ledger.""" 

505 by_source: dict[str, list[DestinationView]] = {key: [] for key in ids} 

506 paths = self._access.retained_paths() 

507 

508 async def read_destinations( 

509 account_id: str, database: Path 

510 ) -> dict[str, DestinationView] | AccountUnavailable: 

511 supervisor = self._access.supervisors().get(account_id) 

512 if ( 

513 supervisor is not None 

514 and supervisor.owner is not None 

515 and supervisor.state != "failed" 

516 ): 

517 owner = supervisor.owner 

518 live = await self._owner_reads.read( 

519 account_id, 

520 owner, 

521 ("destinations", frozenset(ids)), 

522 lambda: owner.destination_views(ids), 

523 ) 

524 if not isinstance(live, OwnerUnavailable): 524 ↛ 526line 524 didn't jump to line 526 because the condition on line 524 was always true

525 return live 

526 try: 

527 _, snapshot, _ = await self._access.retained_account(database, timeout=2) 

528 except TimeoutError: 

529 return AccountUnavailable(account_id=account_id, reason="timeout") 

530 except EarlierLedgerVersion: 

531 return AccountUnavailable(account_id=account_id, reason="earlier_version") 

532 except Exception as exc: # noqa: BLE001 - one unreadable ledger must not hide others 

533 log.warning("destination_read_failed id=%s type=%s", account_id, type(exc).__name__) 

534 return AccountUnavailable(account_id=account_id, reason="read_failed") 

535 try: 

536 events = await asyncio.wait_for( 

537 asyncio.to_thread(self._access.evidence.retained_message_events, database, ids), 

538 timeout=2, 

539 ) 

540 except Exception as exc: # noqa: BLE001 - the timeline is extra; the outcome stands 

541 log.warning("timeline_read_failed id=%s type=%s", account_id, type(exc).__name__) 

542 events = () 

543 return destination_views(snapshot, ids, events) 

544 

545 results = await asyncio.gather( 

546 *( 

547 read_destinations(account_id, database) 

548 for account_id, database in sorted(paths.items()) 

549 ) 

550 ) 

551 unavailable = tuple(result for result in results if isinstance(result, AccountUnavailable)) 

552 for views in results: 

553 if isinstance(views, AccountUnavailable): 

554 continue 

555 for source_id, view in views.items(): 

556 if source_id in by_source: 556 ↛ 555line 556 didn't jump to line 555 because the condition on line 556 was always true

557 by_source[source_id].append(view) 

558 return by_source, unavailable 

559 

560 async def guru_day_stats( 

561 self, day_start: dt.datetime, day_end: dt.datetime, now: dt.datetime | None = None 

562 ) -> GuruDayStatsPage: 

563 """Each guru's posts, trades and read time from `day_start` up to `day_end`, and the 

564 posts of the current trading day still waiting for the owner, whole, counted from every 

565 post the engine holds: the app lists exactly these, so what it counts is what it shows.""" 

566 now = now or dt.datetime.now(dt.UTC) 

567 trading_day = trade_date(now) 

568 # The trading day began at 20:00 New York time the evening before. 

569 trading_start = dt.datetime.combine( 

570 trading_day - dt.timedelta(days=1), dt.time(20), tzinfo=ET 

571 ) 

572 posts = await asyncio.wait_for( 

573 asyncio.to_thread( 

574 self._access.evidence.guru_posts, 

575 self._access.application_database, 

576 since=min(day_start, trading_start), 

577 ), 

578 timeout=2, 

579 ) 

580 by_source, unavailable = await self._destinations({post.source_id for post in posts}) 

581 tallies: dict[str, _GuruTally] = {} 

582 for post in posts: 

583 tally = tallies.setdefault(post.guru_id, _GuruTally()) 

584 views = by_source.get(post.source_id, []) 

585 if day_start <= post.source_at < day_end: 

586 tally.posts += 1 

587 if any(_traded(view) for view in views): 

588 tally.traded += 1 

589 if post.read_started_at is not None and post.read_at is not None: 589 ↛ 593line 589 didn't jump to line 593 because the condition on line 589 was always true

590 tally.reads.append( 

591 max(0.0, (post.read_at - post.read_started_at).total_seconds()) 

592 ) 

593 if trade_date(post.source_at) == trading_day and _waits(views): 

594 tally.waiting.add(post.source_id) 

595 waiting = {source_id for tally in tallies.values() for source_id in tally.waiting} 

596 held: dict[str, SourceActivity] = {} 

597 if waiting: 

598 page = await asyncio.wait_for( 

599 asyncio.to_thread( 

600 self._access.evidence.source_page, 

601 self._access.application_database, 

602 before_seq=None, 

603 limit=len(waiting), 

604 destinations={}, 

605 source_ids=tuple(sorted(waiting)), 

606 ), 

607 timeout=2, 

608 ) 

609 held = { 

610 item.source_id: item 

611 for item in self._with_destinations(page, by_source, unavailable).items 

612 } 

613 return GuruDayStatsPage( 

614 day_start=day_start, 

615 day_end=day_end, 

616 gurus=tuple( 

617 GuruDayStats( 

618 guru_id=guru_id, 

619 posts=tally.posts, 

620 traded=tally.traded, 

621 read_seconds=sum(tally.reads) / len(tally.reads) if tally.reads else None, 

622 waiting=tuple( 

623 sorted( 

624 (held[source_id] for source_id in tally.waiting if source_id in held), 

625 key=lambda item: item.sequence, 

626 reverse=True, 

627 ) 

628 ), 

629 ) 

630 for guru_id, tally in sorted(tallies.items()) 

631 ), 

632 unavailable_accounts=unavailable, 

633 ) 

634 

635 async def list_manual_commands(self, request: ManualCommandPageRequest) -> ManualCommandPage: 

636 request = ManualCommandPageRequest.model_validate(request.model_dump()) 

637 supervisor = self._access.supervisors().get(request.account_id) 

638 if ( 

639 supervisor is not None 

640 and supervisor.owner is not None 

641 and supervisor.state not in {"failed", "paused"} 

642 ): 

643 return await supervisor.owner.manual_command_page( 

644 request.source_id, 

645 before_command_id=request.before_command_id, 

646 limit=request.limit, 

647 ) 

648 return await self._retained_manual_command_page(request) 

649 

650 async def _retained_manual_command_page( 

651 self, request: ManualCommandPageRequest 

652 ) -> ManualCommandPage: 

653 try: 

654 database = self._access.manual_command_ledger_path(request.account_id) 

655 _, snapshot, _ = await self._access.retained_account(database, timeout=3) 

656 return manual_command_page_from_snapshot( 

657 snapshot, 

658 account_id=request.account_id, 

659 source_id=request.source_id, 

660 before_command_id=request.before_command_id, 

661 limit=request.limit, 

662 ) 

663 except Exception as exc: 

664 log.warning( 

665 "retained_manual_command_read_failed account=%s type=%s", 

666 request.account_id, 

667 type(exc).__name__, 

668 ) 

669 raise ManualRefusal("history_failed") from exc