Coverage for src/copytrading_engine/headless/owner.py: 90%

52 statements  

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

1"""The owner socket's contract: what the owner may ask a running server, and its answers. 

2 

3Requests and answers are typed and checked where they cross the socket, so the server never 

4acts on a malformed line and the command line never reads the engine's internal JSON. 

5Proposals reuse the agent contract's own view, so both sides describe one approval the same way. 

6""" 

7 

8import json 

9from typing import Annotated, Literal 

10 

11from pydantic import BaseModel, ConfigDict, Field, StrictBool, TypeAdapter 

12 

13from copytrading_engine.control.wire import ProposalView 

14 

15 

16class _Message(BaseModel): 

17 model_config = ConfigDict(frozen=True, extra="forbid", hide_input_in_errors=True) 

18 

19 

20# Requests 

21 

22 

23class AskStatus(_Message): 

24 op: Literal["status"] = "status" 

25 

26 

27class SetEntries(_Message): 

28 op: Literal["entries"] = "entries" 

29 account: str = Field(pattern=r"^[a-zA-Z0-9_-]{1,64}$") 

30 enabled: StrictBool 

31 

32 

33class ListProposals(_Message): 

34 op: Literal["proposals"] = "proposals" 

35 

36 

37class Approve(_Message): 

38 """Approve exactly the proposal the owner was shown: its id and its digest.""" 

39 

40 op: Literal["approve"] = "approve" 

41 proposal_id: str = Field(pattern=r"^p-[0-9a-f]{12}$") 

42 digest: str = Field(pattern=r"^[0-9a-f]{64}$") 

43 

44 

45class Reject(_Message): 

46 op: Literal["reject"] = "reject" 

47 proposal_id: str = Field(pattern=r"^p-[0-9a-f]{12}$") 

48 

49 

50class Pause(_Message): 

51 op: Literal["pause"] = "pause" 

52 

53 

54class Resume(_Message): 

55 op: Literal["resume"] = "resume" 

56 

57 

58type OwnerRequest = Annotated[ 

59 AskStatus | SetEntries | ListProposals | Approve | Reject | Pause | Resume, 

60 Field(discriminator="op"), 

61] 

62OWNER_REQUEST: TypeAdapter[OwnerRequest] = TypeAdapter(OwnerRequest) 

63 

64 

65# Answers 

66 

67 

68class AccountLine(BaseModel): 

69 """One account as the owner needs it; the engine's other status fields are ignored.""" 

70 

71 model_config = ConfigDict(frozen=True, extra="ignore") 

72 

73 id: str 

74 state: str 

75 entry_permission: str 

76 error_code: str | None = None 

77 

78 

79class CopyingStatus(BaseModel): 

80 model_config = ConfigDict(frozen=True, extra="ignore") 

81 

82 type: Literal["copying_status"] = "copying_status" 

83 state: str 

84 source_connected: bool 

85 model_ready: bool 

86 processed_signals: int 

87 error_code: str | None = None 

88 accounts: tuple[AccountLine, ...] = () 

89 

90 def summary(self) -> str: 

91 """One line for a log: copying's state, work done, and each account.""" 

92 accounts = ", ".join( 

93 f"{account.id} {account.state}" 

94 + (" (entries on)" if account.entry_permission == "enabled" else "") 

95 for account in self.accounts 

96 ) 

97 problem = f"; problem: {self.error_code.replace('_', ' ')}" if self.error_code else "" 

98 return ( 

99 f"Copying {self.state}; {self.processed_signals} post(s) handled; {accounts}{problem}" 

100 ) 

101 

102 

103class EntriesChanged(_Message): 

104 type: Literal["entries_changed"] = "entries_changed" 

105 account_id: str 

106 entry_permission: str 

107 

108 

109class Proposals(_Message): 

110 type: Literal["proposals"] = "proposals" 

111 items: tuple[ProposalView, ...] 

112 

113 

114class ProposalChanged(_Message): 

115 type: Literal["proposal_changed"] = "proposal_changed" 

116 proposal: ProposalView 

117 

118 

119class Refused(_Message): 

120 """The server could not do what was asked; the reason is plain text.""" 

121 

122 type: Literal["refused"] = "refused" 

123 reason: str 

124 

125 

126type OwnerAnswer = Annotated[ 

127 CopyingStatus | EntriesChanged | Proposals | ProposalChanged | Refused, 

128 Field(discriminator="type"), 

129] 

130OWNER_ANSWER: TypeAdapter[OwnerAnswer] = TypeAdapter(OwnerAnswer) 

131 

132 

133def encode(message: BaseModel) -> bytes: 

134 return message.model_dump_json().encode() + b"\n" 

135 

136 

137def proposal_from_engine(value: object) -> ProposalView: 

138 """The engine's proposal JSON, checked against the published agent contract.""" 

139 return ProposalView.model_validate_json(json.dumps(value))