Coverage for src/copytrading_engine/host/pipe/server.py: 81%

209 statements  

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

1"""Strict bounded JSON-line IPC over inherited private standard pipes.""" 

2 

3import asyncio 

4import json 

5import logging 

6from dataclasses import asdict 

7from typing import Protocol 

8 

9from pydantic import ValidationError 

10 

11from copytrading_engine.assistant.service import AssistantService 

12from copytrading_engine.backup.manifest import BackupManifestError 

13from copytrading_engine.backup.service import BackupRestoreService 

14from copytrading_engine.control.service import ControlService 

15from copytrading_engine.execution.application.restore_preflight import RestoreCandidatePreflight 

16from copytrading_engine.execution.domain.lifecycle import AccountControlConflict 

17from copytrading_engine.execution.domain.manual_commands import ManualRefusal, ManualRefusalReason 

18from copytrading_engine.host.errors import ( 

19 ForeignInstallation, 

20 IdentityConflict, 

21 InstallationAlreadyRunning, 

22 InvalidCommand, 

23 StoreClosed, 

24 StoreUnavailable, 

25 UnknownSchemaVersion, 

26 WorkflowNotFound, 

27) 

28from copytrading_engine.host.pipe.handlers.accounts import AccountHandlers 

29from copytrading_engine.host.pipe.handlers.assistant import AssistantHandlers 

30from copytrading_engine.host.pipe.handlers.control import ControlHandlers 

31from copytrading_engine.host.pipe.handlers.manual import ManualOrderHandlers 

32from copytrading_engine.host.pipe.handlers.profiles import ProfileHandlers 

33from copytrading_engine.host.pipe.handlers.restore import BackupRestoreHandlers 

34from copytrading_engine.host.pipe.handlers.trading import TradingLifecycleHandlers 

35from copytrading_engine.host.pipe.requests import ( 

36 REQUEST_ADAPTER, 

37 AbortRestoreCandidateRequest, 

38 AssistantAskRequest, 

39 AssistantCancelRequest, 

40 AssistantResetRequest, 

41 AssistantTurnRequest, 

42 CheckConnectionRequest, 

43 CompleteRestoreCandidateRequest, 

44 ConfirmManualOrdersRequest, 

45 ControlAccountRequest, 

46 CreateBackupRequest, 

47 EvaluateHistoricalProfileRequest, 

48 GetAccountFeedRequest, 

49 GetAccountsRequest, 

50 GetEquityHistoryRequest, 

51 GetGuruActivityRequest, 

52 GetGuruDayStatsRequest, 

53 GetManualCommandRequest, 

54 GetQuoteRequest, 

55 GetSourceActivityRequest, 

56 GetStatusRequest, 

57 GetTradingActivationRequest, 

58 GetTradingStatusRequest, 

59 GetWorkflowRequest, 

60 LearnGuruPlaybookRequest, 

61 ListManualCommandsRequest, 

62 PauseTradingRequest, 

63 PipeRequest, 

64 PrepareRestoreCandidateRequest, 

65 PreviewManualOrderRequest, 

66 PreviewRestoreRequest, 

67 RequestHandler, 

68 ResolveOwnershipRequest, 

69 RestoreCandidateStatusRequest, 

70 RestorePreflightRequest, 

71 SaveManualCorrectionRequest, 

72 StartTradingRequest, 

73 StopRequest, 

74 SubmitRequest, 

75 UpdateAccountLimitsRequest, 

76 ValidateTradingRequest, 

77) 

78from copytrading_engine.host.pipe.responses import ErrorCode, reply, workflow_payload 

79from copytrading_engine.host.pipe.services import TradingServices 

80from copytrading_engine.host.pipe.session import PipeSession 

81from copytrading_engine.host.self_test.model import SubmitSelfTest 

82from copytrading_engine.host.self_test.service import SelfTestService 

83from copytrading_engine.host.status import EngineQueries, EngineState 

84from copytrading_engine.shared.owner_facing import OwnerFacingError 

85 

86log = logging.getLogger(__name__) 

87 

88MAX_REQUEST_LINE_BYTES = 1024 * 1024 

89 

90# The by-hand steps: when one fails unexpectedly the reply still says which step it was. 

91MANUAL_FAILURES: dict[type, ManualRefusalReason] = { 

92 SaveManualCorrectionRequest: "correction_failed", 

93 PreviewManualOrderRequest: "preview_failed", 

94 ConfirmManualOrdersRequest: "confirm_failed", 

95 GetManualCommandRequest: "status_failed", 

96 ListManualCommandsRequest: "history_failed", 

97} 

98_READ_CHUNK_BYTES = 64 * 1024 

99_PROCESS_LIMIT = 100 

100# An assistant failure ends that one request as `unavailable`, never the pipe. 

101ASSISTANT_REQUESTS = ( 

102 AssistantAskRequest, 

103 AssistantTurnRequest, 

104 AssistantCancelRequest, 

105 AssistantResetRequest, 

106) 

107 

108 

109class _Output(Protocol): 

110 def write(self, data: bytes) -> None: ... 

111 

112 async def drain(self) -> None: ... 

113 

114 

115class PipeServer: 

116 """Serve one request per line while keeping database and pipe work separate.""" 

117 

118 def __init__( 

119 self, 

120 service: SelfTestService, 

121 queries: EngineQueries, 

122 trading: TradingServices | None = None, 

123 backup_restore: BackupRestoreService | None = None, 

124 restore_preflight: RestoreCandidatePreflight | None = None, 

125 *, 

126 restore_gated: bool = False, 

127 control: ControlService | None = None, 

128 assistant: AssistantService | None = None, 

129 ) -> None: 

130 self._service = service 

131 self._queries = queries 

132 self._trading = trading 

133 self._assistant = assistant 

134 self._restore_gated = restore_gated 

135 self._session = PipeSession() 

136 areas = ( 

137 TradingLifecycleHandlers(trading=trading), 

138 AccountHandlers(trading=trading), 

139 ManualOrderHandlers(trading=trading), 

140 ProfileHandlers(trading=trading), 

141 BackupRestoreHandlers( 

142 backup_restore=backup_restore, 

143 restore_preflight=restore_preflight, 

144 trading=trading, 

145 restore_gated=restore_gated, 

146 session=self._session, 

147 ), 

148 ControlHandlers(control=control, session=self._session), 

149 AssistantHandlers(assistant=assistant), 

150 ) 

151 self._handlers: dict[type[PipeRequest], RequestHandler] = { 

152 SubmitRequest: self._on_submit, 

153 GetStatusRequest: self._on_get_status, 

154 GetWorkflowRequest: self._on_get_workflow, 

155 StopRequest: self._on_stop, 

156 } 

157 for area in areas: 

158 self._handlers |= area.handlers() 

159 

160 async def handle_line(self, raw_line: bytes, *, overlong: bool = False) -> bytes: 

161 """Validate one request and return a single contract response line.""" 

162 if overlong or len(raw_line.rstrip(b"\r\n")) > MAX_REQUEST_LINE_BYTES: 

163 return reply(1, "", error="invalid_request") 

164 

165 payload: object 

166 try: 

167 payload = json.loads(raw_line) 

168 except UnicodeDecodeError, TypeError, ValueError, RecursionError: 

169 return reply(1, "", error="invalid_request") 

170 

171 version, request_id = _header(payload) 

172 if not isinstance(payload, dict) or type(payload.get("version")) is not int: 

173 return reply(version, request_id, error="invalid_request") 

174 try: 

175 request = REQUEST_ADAPTER.validate_json(raw_line) 

176 except ValidationError: 

177 return reply(version, request_id, error="invalid_request") 

178 

179 if self._restore_gated and not isinstance( 

180 request, 

181 ( 

182 GetStatusRequest, 

183 GetWorkflowRequest, 

184 GetTradingStatusRequest, 

185 GetTradingActivationRequest, 

186 RestoreCandidateStatusRequest, 

187 AbortRestoreCandidateRequest, 

188 RestorePreflightRequest, 

189 CompleteRestoreCandidateRequest, 

190 StopRequest, 

191 ), 

192 ): 

193 return reply(request.version, request.request_id, error="unavailable") 

194 

195 reason: ManualRefusalReason | None = None 

196 try: 

197 return await self._handlers[type(request)](request) 

198 except IdentityConflict: 

199 code: ErrorCode = "identity_conflict" 

200 except AccountControlConflict: 

201 code = "identity_conflict" 

202 except KeyError: 

203 code = "not_found" 

204 reason = MANUAL_FAILURES.get(type(request)) 

205 except WorkflowNotFound: 

206 code = "not_found" 

207 except InvalidCommand: 

208 code = "invalid_request" 

209 except ValueError as exc: 

210 # Only sentences written for the owner are recorded; other messages may carry input. 

211 owner_facing = isinstance(exc, OwnerFacingError | BackupManifestError) 

212 log.warning( 

213 "request_refused operation=%s error=%s reason=%s", 

214 type(request).__name__, 

215 type(exc).__name__, 

216 str(exc)[:160] if owner_facing else "", 

217 ) 

218 if isinstance(exc, OwnerFacingError): 

219 # A sentence written for the owner is said as written, whatever was asked. 

220 return reply( 

221 request.version, 

222 request.request_id, 

223 error="invalid_request", 

224 message=str(exc), 

225 ) 

226 if isinstance(exc, ManualRefusal): 

227 # A refusal the by-hand flow expects names its own reason for the app to word. 

228 refusal_code: ErrorCode = ( 

229 "identity_conflict" 

230 if exc.reason == "identity_conflict" 

231 else "not_found" 

232 if exc.reason == "command_not_found" 

233 else "unavailable" 

234 ) 

235 return reply( 

236 request.version, request.request_id, error=refusal_code, reason=exc.reason 

237 ) 

238 if type(request) in MANUAL_FAILURES: 

239 # A check the flow does not expect to fail still says which step it was. 

240 return reply( 

241 request.version, 

242 request.request_id, 

243 error="unavailable", 

244 reason=MANUAL_FAILURES[type(request)], 

245 ) 

246 if isinstance( 

247 request, 

248 ( 

249 EvaluateHistoricalProfileRequest, 

250 LearnGuruPlaybookRequest, 

251 CreateBackupRequest, 

252 PreviewRestoreRequest, 

253 PrepareRestoreCandidateRequest, 

254 RestorePreflightRequest, 

255 CompleteRestoreCandidateRequest, 

256 ), 

257 ): 

258 message = str(exc) 

259 if isinstance(exc, OwnerFacingError): 259 ↛ 260line 259 didn't jump to line 260 because the condition on line 259 was never true

260 return reply( 

261 request.version, 

262 request.request_id, 

263 error="invalid_request", 

264 message=message, 

265 ) 

266 if isinstance(exc, BackupManifestError): 266 ↛ 274line 266 didn't jump to line 274 because the condition on line 266 was always true

267 # Backup and restore reasons are fixed sentences without absolute paths. 

268 return reply( 

269 request.version, 

270 request.request_id, 

271 error="invalid_request", 

272 message=f"{message[:1].upper()}{message[1:]}.", 

273 ) 

274 if "identity conflicts" in message: 

275 code = "identity_conflict" 

276 elif ( 

277 "unavailable" in message 

278 or "not available" in message 

279 or "incomplete" in message 

280 ): 

281 code = "unavailable" 

282 else: 

283 code = "invalid_request" 

284 return reply(request.version, request.request_id, error=code) 

285 if isinstance(request, ASSISTANT_REQUESTS): 

286 code = "unavailable" 

287 elif isinstance( 287 ↛ 300line 287 didn't jump to line 300 because the condition on line 287 was always true

288 request, 

289 ( 

290 StartTradingRequest, 

291 ValidateTradingRequest, 

292 CheckConnectionRequest, 

293 UpdateAccountLimitsRequest, 

294 ControlAccountRequest, 

295 ResolveOwnershipRequest, 

296 ), 

297 ): 

298 code = "invalid_request" 

299 else: 

300 raise 

301 except ( 

302 ForeignInstallation, 

303 InstallationAlreadyRunning, 

304 StoreClosed, 

305 StoreUnavailable, 

306 UnknownSchemaVersion, 

307 ): 

308 code = "unavailable" 

309 except Exception as exc: 

310 # One request's failure is answered, never fatal: a correction or a manual order that 

311 # trips on bad local state must not take copying down with it. Messages may carry 

312 # input, so only the type is recorded. 

313 log.error( 

314 "request_failed operation=%s error=%s", type(request).__name__, type(exc).__name__ 

315 ) 

316 if not isinstance( 316 ↛ 349line 316 didn't jump to line 349 because the condition on line 316 was never true

317 request, 

318 ( 

319 SaveManualCorrectionRequest, 

320 PreviewManualOrderRequest, 

321 ConfirmManualOrdersRequest, 

322 GetManualCommandRequest, 

323 ListManualCommandsRequest, 

324 GetTradingStatusRequest, 

325 StartTradingRequest, 

326 ValidateTradingRequest, 

327 CheckConnectionRequest, 

328 PauseTradingRequest, 

329 UpdateAccountLimitsRequest, 

330 ControlAccountRequest, 

331 ResolveOwnershipRequest, 

332 GetAccountsRequest, 

333 GetSourceActivityRequest, 

334 GetGuruActivityRequest, 

335 GetGuruDayStatsRequest, 

336 EvaluateHistoricalProfileRequest, 

337 LearnGuruPlaybookRequest, 

338 GetAccountFeedRequest, 

339 GetEquityHistoryRequest, 

340 GetQuoteRequest, 

341 CreateBackupRequest, 

342 PreviewRestoreRequest, 

343 PrepareRestoreCandidateRequest, 

344 RestorePreflightRequest, 

345 CompleteRestoreCandidateRequest, 

346 *ASSISTANT_REQUESTS, 

347 ), 

348 ): 

349 raise 

350 code = "unavailable" 

351 reason = MANUAL_FAILURES.get(type(request)) 

352 return reply(request.version, request.request_id, error=code, reason=reason) 

353 

354 async def _on_submit(self, request: SubmitRequest) -> bytes: 

355 command = SubmitSelfTest( 

356 command_id=request.command.command_id, 

357 text=request.command.text, 

358 destination_ids=tuple(request.command.destination_ids), 

359 ) 

360 workflow = await self._service.submit(command) 

361 self._session.process_after_reply = True 

362 return reply( 

363 request.version, 

364 request.request_id, 

365 ok={"type": "workflow", "workflow": workflow_payload(workflow)}, 

366 ) 

367 

368 async def _on_get_status(self, request: GetStatusRequest) -> bytes: 

369 status = await self._queries.status(self._session.state) 

370 return reply( 

371 request.version, 

372 request.request_id, 

373 ok={"type": "status", "status": asdict(status)}, 

374 ) 

375 

376 async def _on_get_workflow(self, request: GetWorkflowRequest) -> bytes: 

377 workflow = await self._queries.workflow(request.command_id) 

378 return reply( 

379 request.version, 

380 request.request_id, 

381 ok={"type": "workflow", "workflow": workflow_payload(workflow)}, 

382 ) 

383 

384 async def _on_stop(self, request: StopRequest) -> bytes: 

385 self._session.stop() 

386 return reply( 

387 request.version, 

388 request.request_id, 

389 ok={"type": "stopping"}, 

390 ) 

391 

392 @property 

393 def engine_state(self) -> EngineState: 

394 """The session's lifecycle state, for the services that stamp it on what they do.""" 

395 return self._session.state 

396 

397 async def _end_assistant(self) -> None: 

398 if self._assistant is not None: 

399 await self._assistant.reset() 

400 

401 async def _wind_down(self) -> None: 

402 """Pause copying, then end any assistant turn, even if pausing fails.""" 

403 try: 

404 if self._trading is not None and not self._restore_gated: 

405 await self._trading.lifecycle.pause() 

406 finally: 

407 await self._end_assistant() 

408 

409 async def serve(self, reader: asyncio.StreamReader, writer: _Output) -> None: 

410 """Read bounded lines and drain every response asynchronously.""" 

411 lines = _BoundedLineReader(reader) 

412 while True: 

413 item = await lines.read_line() 

414 if item is None: 

415 await self._wind_down() 

416 if not self._restore_gated: 416 ↛ 418line 416 didn't jump to line 418 because the condition on line 416 was always true

417 await self._service.process_pending(max_items=_PROCESS_LIMIT) 

418 return 

419 

420 raw_line, overlong = item 

421 response = await self.handle_line(raw_line, overlong=overlong) 

422 writer.write(response) 

423 await writer.drain() 

424 

425 if self._session.process_after_reply: 425 ↛ 426line 425 didn't jump to line 426 because the condition on line 425 was never true

426 self._session.process_after_reply = False 

427 await self._service.process_pending(max_items=1) 

428 if self._session.stop_after_reply: 

429 self._session.stop_after_reply = False 

430 await self._wind_down() 

431 if not self._restore_gated: 431 ↛ 432line 431 didn't jump to line 432 because the condition on line 431 was never true

432 await self._service.process_pending(max_items=_PROCESS_LIMIT) 

433 return 

434 

435 

436class _BoundedLineReader: 

437 def __init__(self, reader: asyncio.StreamReader) -> None: 

438 self._reader = reader 

439 self._buffer = bytearray() 

440 

441 async def read_line(self) -> tuple[bytes, bool] | None: 

442 line = bytearray() 

443 overlong = False 

444 while True: 

445 newline = self._buffer.find(b"\n") 

446 if newline >= 0: 

447 segment = self._buffer[:newline] 

448 del self._buffer[: newline + 1] 

449 if not overlong and len(line) + len(segment) <= MAX_REQUEST_LINE_BYTES: 449 ↛ 454line 449 didn't jump to line 454 because the condition on line 449 was always true

450 line.extend(segment) 

451 if line.endswith(b"\r"): 451 ↛ 452line 451 didn't jump to line 452 because the condition on line 451 was never true

452 line.pop() 

453 return bytes(line), False 

454 return b"", True 

455 

456 if self._buffer: 456 ↛ 457line 456 didn't jump to line 457 because the condition on line 456 was never true

457 segment = bytes(self._buffer) 

458 self._buffer.clear() 

459 if not overlong: 

460 if len(line) + len(segment) > MAX_REQUEST_LINE_BYTES: 

461 line.clear() 

462 overlong = True 

463 else: 

464 line.extend(segment) 

465 

466 chunk = await self._reader.read(_READ_CHUNK_BYTES) 

467 if not chunk: 

468 if overlong: 468 ↛ 469line 468 didn't jump to line 469 because the condition on line 468 was never true

469 return b"", True 

470 if line: 470 ↛ 471line 470 didn't jump to line 471 because the condition on line 470 was never true

471 return bytes(line), False 

472 return None 

473 self._buffer.extend(chunk) 

474 

475 

476def _header(payload: object) -> tuple[int, str]: 

477 if not isinstance(payload, dict): 477 ↛ 478line 477 didn't jump to line 478 because the condition on line 477 was never true

478 return 1, "" 

479 version_value = payload.get("version") 

480 version = version_value if type(version_value) is int else 1 

481 request_value = payload.get("request_id") 

482 request_id = "" 

483 if isinstance(request_value, str) and len(request_value) <= 128: 483 ↛ 490line 483 didn't jump to line 490 because the condition on line 483 was always true

484 try: 

485 request_value.encode("utf-8") 

486 except UnicodeEncodeError: 

487 pass 

488 else: 

489 request_id = request_value 

490 return version, request_id