Coverage for src/copytrading_engine/headless/config.py: 100%
127 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-16 16:55 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-16 16:55 +0000
1"""Read copytrading.toml and the environment into the engine's own setup and keys.
3The file holds everything the app's setup screens hold, in plain names. Keys never live in
4the file: they come from environment variables, so the file can be shared or committed.
5"""
7import os
8import re
9import tomllib
10from collections.abc import Mapping
11from decimal import Decimal
12from pathlib import Path
13from typing import Literal
15from pydantic import BaseModel, ConfigDict, Field, SecretStr, ValidationError
17from copytrading_engine.execution.domain.sizing import RouteConnection
18from copytrading_engine.shared.model_providers import ModelProvider
19from copytrading_engine.shared.route_keys import guru_for_channel
20from copytrading_engine.trading.domain.config import (
21 AccountConfiguration,
22 AccountPolicy,
23 BrokerCredentials,
24 NotificationConfiguration,
25 ProviderConfiguration,
26 RouteConfiguration,
27 SourceConfiguration,
28 TradingConfiguration,
29 TradingSecrets,
30)
31from copytrading_engine.trading.domain.profiles import ProfileBuilder, ProfileDraft
33type AgentAccess = Literal["off", "read_pause", "propose"]
35DISCORD_TOKEN = "COPYTRADING_DISCORD_TOKEN"
36MODEL_API_KEY = "COPYTRADING_MODEL_API_KEY"
37TELEGRAM_TOKEN = "COPYTRADING_TELEGRAM_TOKEN"
38DISCORD_WEBHOOK_URL = "COPYTRADING_DISCORD_WEBHOOK_URL"
41DEFAULT_LIMITS = AccountPolicy()
44class ConfigError(Exception):
45 """The file or the environment cannot become a setup; each problem is one plain line."""
47 def __init__(self, problems: list[str]) -> None:
48 super().__init__("\n".join(problems))
49 self.problems = problems
52class _Strict(BaseModel):
53 model_config = ConfigDict(extra="forbid", frozen=True)
56class _Discord(_Strict):
57 channels: tuple[str, ...] = Field(min_length=1)
58 authors: tuple[str, ...] = ()
61class _Model(_Strict):
62 provider: ModelProvider
63 name: str
64 address: str | None = None
67class _Account(_Strict):
68 id: str
69 environment: Literal["paper", "live"]
70 limits: AccountPolicy = Field(default_factory=AccountPolicy)
73class _Guru(_Strict):
74 # The channel names the guru (one guru per channel), so a guru has no ID of its own here.
75 name: str
76 channel: str
77 author: str | None = None
78 playbook: str = ""
79 playbook_file: str | None = None
80 # One guru copies into one account; that account's max_symbol_usd is the guru's full position.
81 account: str
84class _Telegram(_Strict):
85 chat_id: str
88class _DiscordAlerts(_Strict):
89 """Alerts in a Discord channel; its webhook URL comes from the environment."""
92class _Agents(_Strict):
93 access: AgentAccess = "read_pause"
96class _File(_Strict):
97 discord: _Discord
98 model: _Model
99 accounts: tuple[_Account, ...] = Field(min_length=1)
100 gurus: tuple[_Guru, ...] = Field(min_length=1)
101 telegram: _Telegram | None = None
102 discord_alerts: _DiscordAlerts | None = None
103 agents: _Agents = Field(default_factory=_Agents)
106class ServerSetup(BaseModel):
107 """The engine's setup plus what only the headless server decides."""
109 model_config = ConfigDict(frozen=True)
111 configuration: TradingConfiguration
112 agent_access: AgentAccess
115def load_setup(path: Path) -> ServerSetup:
116 """Parse and check copytrading.toml; every problem is reported at once."""
117 try:
118 raw = tomllib.loads(path.read_text(encoding="utf-8"))
119 except FileNotFoundError:
120 raise ConfigError(
121 [f"{path} does not exist. Run `copytrading-server init` first."]
122 ) from None
123 except tomllib.TOMLDecodeError as exc:
124 raise ConfigError([f"{path} is not valid TOML: {exc}"]) from None
125 try:
126 parsed = _File.model_validate(_decimals(raw))
127 except ValidationError as exc:
128 raise ConfigError(_problems(exc)) from None
129 profiles = []
130 problems: list[str] = []
131 for index, guru in enumerate(parsed.gurus):
132 label = f"gurus[{index}] ({guru.name.strip() or guru.channel})"
133 playbook = guru.playbook
134 if guru.playbook_file is not None:
135 playbook_path = (path.parent / guru.playbook_file).expanduser()
136 try:
137 playbook = playbook_path.read_text(encoding="utf-8")
138 except OSError:
139 problems.append(f"{label}: cannot read {playbook_path}")
140 continue
141 try:
142 profiles.append(
143 ProfileBuilder().build(
144 ProfileDraft(
145 guru_id=guru_for_channel("discord", guru.channel),
146 display_name=guru.name,
147 playbook=playbook,
148 )
149 )
150 )
151 except ValidationError as exc:
152 problems.extend(f"{label}: {line}" for line in _problems(exc))
153 if parsed.telegram is not None and parsed.discord_alerts is not None:
154 problems.append("Choose one place for alerts: [telegram] or [discord_alerts], not both.")
155 if problems:
156 raise ConfigError(problems)
157 limits = {account.id: account.limits for account in parsed.accounts}
158 try:
159 configuration = TradingConfiguration(
160 source=SourceConfiguration(
161 channel_ids=parsed.discord.channels, author_ids=parsed.discord.authors
162 ),
163 provider=ProviderConfiguration(
164 name=parsed.model.provider, model=parsed.model.name, base_url=parsed.model.address
165 ),
166 accounts=tuple(
167 AccountConfiguration(
168 id=account.id, environment=account.environment, policy=account.limits
169 )
170 for account in parsed.accounts
171 ),
172 profiles=tuple(profiles),
173 routes=tuple(
174 RouteConfiguration(
175 channel_id=guru.channel,
176 author_id=guru.author,
177 guru_id=guru_for_channel("discord", guru.channel),
178 profile_revision=profile.profile_revision,
179 connections=(
180 RouteConnection(
181 account_id=guru.account,
182 full_position_usd=limits.get(
183 guru.account, AccountPolicy()
184 ).max_symbol_usd,
185 ),
186 ),
187 )
188 for guru, profile in zip(parsed.gurus, profiles, strict=True)
189 ),
190 notification=(
191 NotificationConfiguration(chat_id=parsed.telegram.chat_id)
192 if parsed.telegram is not None
193 else NotificationConfiguration(service="discord")
194 if parsed.discord_alerts is not None
195 else None
196 ),
197 )
198 except ValidationError as exc:
199 raise ConfigError(_problems(exc)) from None
200 return ServerSetup(configuration=configuration, agent_access=parsed.agents.access)
203def broker_key_names(account_id: str) -> tuple[str, str]:
204 """The environment variables holding one account's Alpaca key and secret."""
205 stem = re.sub(r"[^A-Z0-9]", "_", account_id.upper())
206 return f"COPYTRADING_ALPACA_{stem}_KEY", f"COPYTRADING_ALPACA_{stem}_SECRET"
209def load_secrets(
210 configuration: TradingConfiguration, environ: Mapping[str, str] = os.environ
211) -> TradingSecrets:
212 """Every key the setup needs, from the environment; all missing names are listed."""
213 missing: list[str] = []
215 def required(name: str) -> SecretStr:
216 value = environ.get(name, "").strip()
217 if not value:
218 missing.append(name)
219 return SecretStr(value)
221 discord = required(DISCORD_TOKEN)
222 if configuration.provider.name in {"ollama", "openai_compatible"}:
223 model_key = SecretStr(environ.get(MODEL_API_KEY, "").strip())
224 else:
225 model_key = required(MODEL_API_KEY)
226 brokers = []
227 for account in configuration.accounts:
228 key_name, secret_name = broker_key_names(account.id)
229 brokers.append(
230 BrokerCredentials(
231 account_id=account.id, key=required(key_name), secret=required(secret_name)
232 )
233 )
234 alerts = configuration.notification
235 alert_secret = (
236 None
237 if alerts is None
238 else required(DISCORD_WEBHOOK_URL if alerts.service == "discord" else TELEGRAM_TOKEN)
239 )
240 if missing:
241 raise ConfigError([f"Set {name} in the environment." for name in missing])
242 return TradingSecrets(
243 discord_token=discord,
244 provider_api_key=model_key,
245 brokers=tuple(brokers),
246 notification_token=alert_secret,
247 )
250def _decimals(value: object) -> object:
251 """TOML floats become exact decimals, so 0.1 stays 0.1 in money and fractions."""
252 if isinstance(value, float):
253 return Decimal(str(value))
254 if isinstance(value, dict):
255 return {key: _decimals(item) for key, item in value.items()}
256 if isinstance(value, list):
257 return [_decimals(item) for item in value]
258 return value
261def _problems(error: ValidationError) -> list[str]:
262 lines = []
263 for item in error.errors(include_input=False, include_url=False):
264 where = "".join(
265 f"[{part}]" if isinstance(part, int) else f".{part}" for part in item["loc"]
266 ).lstrip(".")
267 message = str(item["msg"]).removeprefix("Value error, ")
268 if item["type"] == "string_type" and _names_a_discord_id(item["loc"]):
269 message += " (put Discord IDs in quotes: they are longer than TOML numbers allow)"
270 lines.append(f"{where}: {message}" if where else message)
271 return lines
274def _names_a_discord_id(location: tuple[int | str, ...]) -> bool:
275 return any(part in {"channels", "authors", "channel", "author"} for part in location)