Coverage for src/copytrading_engine/execution/domain/lifecycle.py: 90%

19 statements  

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

1"""Durable operator permission, separate from broker and risk readiness.""" 

2 

3from typing import Literal, Self 

4 

5from pydantic import AwareDatetime, Field, model_validator 

6 

7from copytrading_engine.execution.domain.values import Identifier, Value 

8 

9type EntryPermission = Literal["disabled", "enabled", "paused"] 

10type RecoveryPreference = Literal["manual", "automatic"] 

11 

12 

13class AccountOwnerClosed(RuntimeError): 

14 """The account's executor is stopping or stopped; its retained ledger answers instead.""" 

15 

16 

17class AccountControlConflict(ValueError): 

18 """A stable command identity was reused with different content.""" 

19 

20 

21class AccountControlCommand(Value): 

22 """An owner's pause or resume, kept in the account's record. `set_recovery` survives only 

23 because records keep the commands they took before recovery moved into the setup.""" 

24 

25 command_id: Identifier 

26 account_id: Identifier 

27 action: Literal["pause", "resume", "set_recovery", "restore_manual"] 

28 recovery_preference: RecoveryPreference | None = None 

29 

30 @model_validator(mode="after") 

31 def valid_action(self) -> Self: 

32 if (self.action == "set_recovery") != (self.recovery_preference is not None): 32 ↛ 33line 32 didn't jump to line 33 because the condition on line 32 was never true

33 raise ValueError("Recovery preference belongs only to set_recovery") 

34 return self 

35 

36 

37class AccountControlResult(Value): 

38 command: AccountControlCommand 

39 applied_at: AwareDatetime 

40 entry_permission: EntryPermission 

41 recovery_preference: RecoveryPreference 

42 

43 

44class AccountControl(Value): 

45 entry_permission: EntryPermission = "disabled" 

46 # What the record last ran with; the setup's choice is the one that applies. 

47 recovery_preference: RecoveryPreference = "manual" 

48 commands: dict[str, AccountControlResult] = Field(default_factory=dict)