Coverage for src/copytrading_engine/trading/application/account_access.py: 89%

129 statements  

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

1"""Reach a trading account through its live owner, or its retained ledger, in bounded time.""" 

2 

3import asyncio 

4import heapq 

5import logging 

6from collections.abc import Awaitable, Callable, Hashable, Mapping 

7from dataclasses import dataclass 

8from pathlib import Path 

9from typing import cast 

10 

11from copytrading_engine.execution.domain.ledger_state import LedgerSnapshot 

12from copytrading_engine.execution.domain.lifecycle import AccountOwnerClosed 

13from copytrading_engine.execution.presentation.account_feed import AccountFeedPage 

14from copytrading_engine.execution.presentation.operator_views import ( 

15 AccountEventPage, 

16 AccountOverview, 

17 UnavailableReason, 

18) 

19from copytrading_engine.trading.application.accounts import AccountSupervisor 

20from copytrading_engine.trading.application.ports import OperatorEvidence 

21from copytrading_engine.trading.domain.config import TradingConfiguration 

22 

23log = logging.getLogger(__name__) 

24 

25 

26_OWNER_READ_TIMEOUT_SECONDS = 2 

27# Each page of posts asks for its own set; only the newest few answers are kept. 

28_REMEMBERED_READS = 64 

29 

30 

31@dataclass(frozen=True, slots=True) 

32class OwnerUnavailable: 

33 """A live owner read that timed out or failed; callers fall back or report it.""" 

34 

35 reason: UnavailableReason 

36 

37 

38async def bounded_owner_read[T](operation: Awaitable[T]) -> T | OwnerUnavailable: 

39 """A stalled broker worker cannot stall all native account/source reads.""" 

40 task = asyncio.ensure_future(operation) 

41 done, _ = await asyncio.wait({task}, timeout=_OWNER_READ_TIMEOUT_SECONDS) 

42 if not done: 

43 task.add_done_callback( 

44 lambda completed: completed.exception() if not completed.cancelled() else None 

45 ) 

46 return OwnerUnavailable("timeout") 

47 try: 

48 return task.result() 

49 except AccountOwnerClosed: 

50 # Copying is pausing or stopped: the retained ledger answers; nothing went wrong. 

51 return OwnerUnavailable("read_failed") 

52 except Exception as exc: # noqa: BLE001 - one owner's failure is reported, not raised 

53 log.warning("operator_account_read_failed type=%s error=%s", type(exc).__name__, exc) 

54 return OwnerUnavailable("read_failed") 

55 

56 

57class OwnerReads: 

58 """Reads of a running account's worker, the same thread that places its orders. A busy worker 

59 is not a stopped one: while an earlier read still waits on it, that read's last answer stands 

60 in at once, and no second read queues behind the first to make the worker busier.""" 

61 

62 def __init__(self) -> None: 

63 self._owners: dict[str, object] = {} 

64 self._waiting: dict[str, asyncio.Future[object]] = {} 

65 self._last: dict[tuple[str, Hashable], object] = {} 

66 

67 async def read[T]( 

68 self, account_id: str, owner: object, key: Hashable, operation: Callable[[], Awaitable[T]] 

69 ) -> T | OwnerUnavailable: 

70 """`operation` reads `owner`, the account's worker now; `key` names what it reads, so an 

71 answer stands in only for the same read of the same worker.""" 

72 if self._owners.get(account_id) is not owner: 

73 self._forget(account_id) 

74 self._owners[account_id] = owner 

75 remembered = (account_id, key) 

76 waiting = self._waiting.get(account_id) 

77 if waiting is not None and not waiting.done(): 

78 return cast(T, self._last.get(remembered, OwnerUnavailable("timeout"))) 

79 task = asyncio.ensure_future(operation()) 

80 self._waiting[account_id] = cast(asyncio.Future[object], task) 

81 

82 def keep(done: asyncio.Future[T]) -> None: 

83 # A read that outlived its wait still brings the newest answer once it lands. 

84 if self._owners.get(account_id) is owner and not done.cancelled(): 

85 if done.exception() is None: 

86 self._last.pop(remembered, None) 

87 self._last[remembered] = done.result() 

88 while len(self._last) > _REMEMBERED_READS: 88 ↛ 89line 88 didn't jump to line 89 because the condition on line 88 was never true

89 del self._last[next(iter(self._last))] 

90 

91 task.add_done_callback(keep) 

92 result = await bounded_owner_read(task) 

93 if result == OwnerUnavailable("timeout") and remembered in self._last: 

94 return cast(T, self._last[remembered]) 

95 return result 

96 

97 def _forget(self, account_id: str) -> None: 

98 self._waiting.pop(account_id, None) 

99 for remembered in [item for item in self._last if item[0] == account_id]: 

100 del self._last[remembered] 

101 

102 

103class AccountAccess: 

104 """Shared by operator reads and manual intervention; the runtime owns the supervisors.""" 

105 

106 def __init__( 

107 self, 

108 data_dir: Path, 

109 supervisors: Callable[[], Mapping[str, AccountSupervisor]], 

110 configuration: Callable[[], TradingConfiguration | None], 

111 runtime_state: Callable[[], str], 

112 evidence: OperatorEvidence, 

113 account_error: Callable[[str], str | None] = lambda _: None, 

114 ) -> None: 

115 """`account_error` is the last reason an account stopped, kept after its supervisor ends.""" 

116 self.data_dir = data_dir 

117 self.supervisors = supervisors 

118 self.configuration = configuration 

119 self.runtime_state = runtime_state 

120 self.evidence = evidence 

121 self.account_error = account_error 

122 

123 @property 

124 def application_database(self) -> Path: 

125 return self.data_dir / "application.db" 

126 

127 async def retained_account( 

128 self, 

129 database: Path, 

130 *, 

131 timeout: float, 

132 before_seq: int | None = None, 

133 limit: int = 50, 

134 ) -> tuple[AccountOverview, LedgerSnapshot, AccountEventPage]: 

135 """Read a retained ledger off the event loop; a stalled read raises TimeoutError.""" 

136 return await asyncio.wait_for( 

137 asyncio.to_thread( 

138 self.evidence.retained_account, database, before_seq=before_seq, limit=limit 

139 ), 

140 timeout=timeout, 

141 ) 

142 

143 async def retained_feed( 

144 self, database: Path, *, before_seq: int | None, limit: int, timeout: float 

145 ) -> AccountFeedPage: 

146 """Read a retained account's feed off the event loop.""" 

147 return await asyncio.wait_for( 

148 asyncio.to_thread( 

149 self.evidence.retained_feed, database, before_seq=before_seq, limit=limit 

150 ), 

151 timeout=timeout, 

152 ) 

153 

154 def retained_paths(self) -> dict[str, Path]: 

155 root = self.data_dir / "accounts" 

156 if not root.exists(): 

157 return {} 

158 if root.is_symlink(): 158 ↛ 159line 158 didn't jump to line 159 because the condition on line 158 was never true

159 raise RuntimeError("Account state directory is invalid") 

160 paths: dict[str, Path] = {} 

161 for account_dir in root.iterdir(): 

162 if account_dir.name.startswith("."): 162 ↛ 163line 162 didn't jump to line 163 because the condition on line 162 was never true

163 continue # not an account: a folder staged mid-move (account_records) 

164 if account_dir.is_symlink() or not account_dir.is_dir(): 164 ↛ 165line 164 didn't jump to line 165 because the condition on line 164 was never true

165 raise RuntimeError("Account evidence path is invalid") 

166 database = account_dir / "execution.sqlite3" 

167 if database.is_symlink(): 167 ↛ 168line 167 didn't jump to line 168 because the condition on line 167 was never true

168 raise RuntimeError("Account evidence path is invalid") 

169 if database.exists(): 

170 paths[account_dir.name] = database 

171 return paths 

172 

173 def retained_page_paths(self, before_account_id: str | None, capacity: int) -> dict[str, Path]: 

174 root = self.data_dir / "accounts" 

175 if not root.exists(): 175 ↛ 176line 175 didn't jump to line 176 because the condition on line 175 was never true

176 return {} 

177 if root.is_symlink(): 177 ↛ 178line 177 didn't jump to line 178 because the condition on line 177 was never true

178 raise RuntimeError("Account state directory is invalid") 

179 paths: list[tuple[str, Path]] = [] 

180 for account_dir in root.iterdir(): 

181 if account_dir.name.startswith("."): 

182 continue # not an account: a folder staged mid-move (account_records) 

183 if account_dir.is_symlink() or not account_dir.is_dir(): 183 ↛ 184line 183 didn't jump to line 184 because the condition on line 183 was never true

184 raise RuntimeError("Account evidence path is invalid") 

185 database = account_dir / "execution.sqlite3" 

186 if database.is_symlink(): 186 ↛ 187line 186 didn't jump to line 187 because the condition on line 186 was never true

187 raise RuntimeError("Account evidence path is invalid") 

188 if not database.exists(): 

189 continue 

190 account_id = account_dir.name 

191 if before_account_id is not None and account_id >= before_account_id: 

192 continue 

193 heapq.heappush(paths, (account_id, database)) 

194 if len(paths) > capacity: 

195 heapq.heappop(paths) 

196 return dict(paths) 

197 

198 def manual_command_ledger_path(self, account_id: str) -> Path: 

199 if ( 

200 not account_id 

201 or len(account_id) > 64 

202 or not account_id.isascii() 

203 or not all(character.isalnum() or character in "_-" for character in account_id) 

204 ): 

205 raise ValueError("Manual account history is unavailable") 

206 accounts_root = self.data_dir / "accounts" 

207 account_dir = accounts_root / account_id 

208 database = account_dir / "execution.sqlite3" 

209 if ( 209 ↛ 217line 209 didn't jump to line 217 because the condition on line 209 was never true

210 accounts_root.is_symlink() 

211 or not accounts_root.is_dir() 

212 or account_dir.is_symlink() 

213 or not account_dir.is_dir() 

214 or database.is_symlink() 

215 or not database.is_file() 

216 ): 

217 raise ValueError("Manual account history is unavailable") 

218 return database