Coverage for src/copytrading_engine/host/status.py: 100%

27 statements  

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

1"""Typed status and workflow queries exposed by the engine boundary.""" 

2 

3from dataclasses import dataclass 

4from typing import Literal 

5 

6from copytrading_engine.host.self_test.model import WorkflowView 

7from copytrading_engine.host.self_test.ports import WorkflowStore 

8 

9EngineState = Literal["starting", "running", "stopping", "failed"] 

10TelemetryState = Literal["healthy", "degraded"] 

11 

12 

13@dataclass(frozen=True) 

14class DiagnosticCaptureProjection: 

15 source_events: int = 0 

16 source_event_gaps: int = 0 

17 model_requests: int = 0 

18 model_request_gaps: int = 0 

19 model_responses: int = 0 

20 model_response_gaps: int = 0 

21 

22 

23@dataclass(frozen=True) 

24class EngineStatus: 

25 instance_id: str 

26 state: EngineState 

27 accepted: int 

28 pending: int 

29 completed: int 

30 telemetry_state: TelemetryState 

31 telemetry_dropped: int 

32 telemetry_error_code: str | None = None 

33 diagnostic_capture: DiagnosticCaptureProjection = DiagnosticCaptureProjection() 

34 

35 

36class EngineQueries: 

37 """Read status and workflow views without exposing adapter details.""" 

38 

39 def __init__(self, store: WorkflowStore, instance_id: str) -> None: 

40 self._store = store 

41 self._instance_id = instance_id 

42 

43 async def status( 

44 self, 

45 state: EngineState, 

46 *, 

47 telemetry_state: TelemetryState = "degraded", 

48 telemetry_dropped: int = 0, 

49 telemetry_error_code: str | None = None, 

50 diagnostic_capture: DiagnosticCaptureProjection | None = None, 

51 ) -> EngineStatus: 

52 accepted, pending, completed = await self._store.counts_async() 

53 return EngineStatus( 

54 instance_id=self._instance_id, 

55 state=state, 

56 accepted=accepted, 

57 pending=pending, 

58 completed=completed, 

59 telemetry_state=telemetry_state, 

60 telemetry_dropped=telemetry_dropped, 

61 telemetry_error_code=telemetry_error_code, 

62 diagnostic_capture=diagnostic_capture or DiagnosticCaptureProjection(), 

63 ) 

64 

65 async def workflow(self, command_id: str) -> WorkflowView: 

66 return await self._store.get_async(command_id)