Coverage for src/copytrading_engine/bootstrap.py: 58%
173 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"""Local engine composition root and inherited-pipe lifecycle."""
3import argparse
4import asyncio
5import os
6import sys
7import uuid
8from collections.abc import AsyncGenerator
9from contextlib import asynccontextmanager
10from pathlib import Path
11from typing import cast
13from copytrading_engine.assistant.service import AssistantService
14from copytrading_engine.backup.restore.gate import (
15 restore_manual_disabled,
16 restore_manual_disabled_path,
17)
18from copytrading_engine.backup.restore.inspector import SQLiteRestoreCandidateInspector
19from copytrading_engine.backup.schema_catalog import (
20 create_application_schema,
21 current_operational_schema_catalog,
22)
23from copytrading_engine.backup.service import BackupRestoreService
24from copytrading_engine.control.service import ControlService
25from copytrading_engine.control.sqlite import SQLiteControlAudit
26from copytrading_engine.diagnostics.capture import DiagnosticEngineQueries, DiagnosticWorkflowStore
27from copytrading_engine.diagnostics.telemetry.config import TelemetryConfig
28from copytrading_engine.diagnostics.telemetry.local import LocalTelemetry
29from copytrading_engine.execution.adapters.alpaca.broker import AlpacaBroker, AlpacaCredentials
30from copytrading_engine.execution.adapters.restore_preparer import prepare_restored_account
31from copytrading_engine.execution.adapters.sqlite_ledger import current_execution_snapshot_schema
32from copytrading_engine.execution.application.restore_preflight import (
33 RestoreBrokerCredential,
34 RestoreCandidatePreflight,
35)
36from copytrading_engine.host.errors import (
37 ForeignInstallation,
38 IdentityConflict,
39 InstallationAlreadyRunning,
40 InvalidCommand,
41 StoreUnavailable,
42 UnknownSchemaVersion,
43 WorkflowError,
44)
45from copytrading_engine.host.installation import Installation
46from copytrading_engine.host.pipe.server import PipeServer
47from copytrading_engine.host.pipe.services import TradingServices
48from copytrading_engine.host.self_test.parser import SelfTestParser
49from copytrading_engine.host.self_test.service import SelfTestService
50from copytrading_engine.host.self_test.store import SQLiteSelfTestStore
51from copytrading_engine.trading.adapters.telemetry import TradingTelemetry
52from copytrading_engine.trading.entrypoints.runtime import TradingRuntime
54_STARTUP_RECOVERY_LIMIT = 100
57class _PipeOutput(asyncio.Protocol):
58 def __init__(self) -> None:
59 self._transport: asyncio.WriteTransport | None = None
60 self._ready = asyncio.Event()
61 self._ready.set()
62 self._failure: Exception | None = None
64 def connection_made(self, transport: asyncio.BaseTransport) -> None:
65 self._transport = cast(asyncio.WriteTransport, transport)
67 def pause_writing(self) -> None:
68 self._ready.clear()
70 def resume_writing(self) -> None:
71 self._ready.set()
73 def connection_lost(self, exc: Exception | None) -> None:
74 self._failure = exc
75 self._ready.set()
77 def write(self, data: bytes) -> None:
78 if self._transport is None:
79 raise StoreUnavailable("standard output pipe is unavailable")
80 self._transport.write(data)
82 async def drain(self) -> None:
83 await self._ready.wait()
84 if self._failure is not None:
85 raise StoreUnavailable("standard output pipe is unavailable") from self._failure
88def main() -> int:
89 parser = argparse.ArgumentParser(prog="copytrading-engine")
90 parser.add_argument("--data-dir", required=True, type=Path)
91 parser.add_argument("--instance-id", required=True)
92 args = parser.parse_args()
93 try:
94 instance_id = _canonical_instance_id(args.instance_id)
95 data_dir = args.data_dir.expanduser().resolve()
96 owner_support_directory = owner_support_root()
97 persist_installation_id(owner_support_directory, instance_id)
98 asyncio.run(_run(data_dir, instance_id))
99 except ForeignInstallation, IdentityConflict:
100 print("engine startup failed code=identity_conflict", file=sys.stderr)
101 return 1
102 except InstallationAlreadyRunning, UnknownSchemaVersion:
103 print("engine startup failed code=unavailable", file=sys.stderr)
104 return 1
105 except InvalidCommand, ValueError:
106 print("engine startup failed code=invalid_request", file=sys.stderr)
107 return 2
108 except OSError, StoreUnavailable, WorkflowError:
109 print("engine startup failed code=unavailable", file=sys.stderr)
110 return 1
111 except Exception: # noqa: BLE001 - last-resort exit code; details stay off stderr
112 print("engine failed code=internal_error", file=sys.stderr)
113 return 1
114 return 0
117async def _run(data_dir: Path, instance_id: str) -> None:
118 async with compose_engine(
119 data_dir,
120 instance_id,
121 diagnostics_directory=diagnostics_state_root(),
122 owner_support_directory=owner_support_root(),
123 ) as server:
124 await _serve_stdio(server)
127@asynccontextmanager
128async def compose_engine(
129 data_dir: Path,
130 instance_id: str,
131 *,
132 diagnostics_directory: Path,
133 owner_support_directory: Path,
134) -> AsyncGenerator[PipeServer]:
135 """The whole engine behind one request server, shut down cleanly on exit.
137 The app drives it over an inherited pipe; the headless server drives the same requests
138 from its sockets.
139 """
140 restore_gate = restore_manual_disabled_path(owner_support_directory)
141 telemetry = LocalTelemetry(TelemetryConfig.from_environment(diagnostics_directory))
142 trading_telemetry = TradingTelemetry(sink=telemetry)
143 trading = TradingRuntime(
144 data_dir,
145 telemetry=trading_telemetry,
146 restore_gate_path=restore_gate,
147 )
148 restore_gated = restore_manual_disabled(restore_gate)
149 backup_restore = BackupRestoreService(
150 data_dir,
151 trading.maintenance_fence,
152 schema_catalog=current_operational_schema_catalog,
153 snapshot_schema=current_execution_snapshot_schema,
154 owner_support_directory=owner_support_directory,
155 account_preparer=prepare_restored_account,
156 )
157 restore_preflight = RestoreCandidatePreflight(
158 SQLiteRestoreCandidateInspector(backup_restore),
159 _restore_evidence_broker,
160 )
161 installation = Installation(
162 data_dir / "application.db",
163 instance_id=instance_id,
164 lock_path=owner_support_directory / ".engine.lock",
165 read_only=restore_gated,
166 )
167 try:
168 with installation, SQLiteSelfTestStore(installation) as store:
169 if not restore_gated: 169 ↛ 171line 169 didn't jump to line 171 because the condition on line 169 was always true
170 create_application_schema(data_dir / "application.db")
171 diagnostic_store = DiagnosticWorkflowStore(store, telemetry)
172 service = SelfTestService(diagnostic_store, SelfTestParser())
173 queries = DiagnosticEngineQueries(store, installation.instance_id, telemetry)
174 await _process_pending_if_allowed(
175 service,
176 restore_gate,
177 max_items=_STARTUP_RECOVERY_LIMIT,
178 )
179 control_audit = (
180 None
181 if restore_gated
182 else await SQLiteControlAudit.open(data_dir / "application.db")
183 )
184 control = (
185 None
186 if control_audit is None
187 else ControlService(trading, trading.operator, trading.manual, control_audit)
188 )
189 assistant = (
190 None
191 if control is None
192 else AssistantService(
193 control,
194 trading.operator,
195 engine_state=lambda: server.engine_state,
196 register_secrets=telemetry.register_secrets,
197 )
198 )
199 server = PipeServer(
200 service,
201 queries,
202 TradingServices.of(trading),
203 backup_restore,
204 restore_preflight,
205 restore_gated=restore_gated,
206 control=control,
207 assistant=assistant,
208 )
209 try:
210 yield server
211 finally:
212 await trading.shutdown()
213 if control_audit is not None: 213 ↛ 216line 213 didn't jump to line 216
214 await control_audit.close()
215 finally:
216 telemetry.close(timeout_seconds=5.0)
219def _restore_evidence_broker(
220 credential: RestoreBrokerCredential,
221) -> AlpacaBroker:
222 return AlpacaBroker(
223 AlpacaCredentials(key=credential.key, secret=credential.secret),
224 credential.environment,
225 )
228def diagnostics_state_root() -> Path:
229 return _required_directory("COPYTRADING_DESKTOP_DIAGNOSTICS_STATE_DIR")
232def owner_support_root() -> Path:
233 return _required_directory("COPYTRADING_DESKTOP_OWNER_SUPPORT_DIR")
236def _required_directory(variable: str) -> Path:
237 """The app names every state root; the engine never derives one from --data-dir."""
238 value = os.environ.get(variable)
239 if not value:
240 raise ValueError(f"{variable} is required")
241 return Path(value)
244async def _process_pending_if_allowed(
245 service: SelfTestService,
246 restore_gate: Path,
247 *,
248 max_items: int = _STARTUP_RECOVERY_LIMIT,
249) -> None:
250 if restore_manual_disabled(restore_gate):
251 return
252 await service.process_pending(max_items=max_items)
255async def _serve_stdio(server: PipeServer) -> None:
256 loop = asyncio.get_running_loop()
257 reader = asyncio.StreamReader(limit=64 * 1024)
258 reader_protocol = asyncio.StreamReaderProtocol(reader)
259 read_transport, _ = await loop.connect_read_pipe(lambda: reader_protocol, sys.stdin.buffer)
260 output = _PipeOutput()
261 write_transport, _ = await loop.connect_write_pipe(lambda: output, sys.stdout.buffer)
262 try:
263 await server.serve(reader, output)
264 finally:
265 read_transport.close()
266 write_transport.close()
269def _canonical_instance_id(value: str) -> str:
270 try:
271 canonical = str(uuid.UUID(value))
272 except (ValueError, AttributeError) as exc:
273 raise InvalidCommand("instance ID must be a UUID") from exc
274 if value.lower() != canonical:
275 raise InvalidCommand("instance ID must use canonical UUID formatting")
276 return canonical
279def persist_installation_id(owner_support_directory: Path, instance_id: str) -> None:
280 try:
281 owner_support_directory.mkdir(mode=0o700, parents=True, exist_ok=True)
282 os.chmod(owner_support_directory, 0o700)
283 identity_path = owner_support_directory / "installation-id"
284 try:
285 descriptor = os.open(
286 identity_path,
287 os.O_CREAT | os.O_EXCL | os.O_WRONLY,
288 0o600,
289 )
290 except FileExistsError:
291 existing = identity_path.read_text(encoding="ascii").strip()
292 if _canonical_instance_id(existing) != instance_id:
293 raise ForeignInstallation(
294 "data directory belongs to a different installation"
295 ) from None
296 return
298 with os.fdopen(descriptor, "w", encoding="ascii") as identity_file:
299 identity_file.write(instance_id + "\n")
300 identity_file.flush()
301 os.fsync(identity_file.fileno())
302 os.chmod(identity_path, 0o600)
303 directory_fd = os.open(owner_support_directory, os.O_RDONLY)
304 try:
305 os.fsync(directory_fd)
306 finally:
307 os.close(directory_fd)
308 except ForeignInstallation, InvalidCommand:
309 raise
310 except OSError as exc:
311 raise StoreUnavailable("installation identity is unavailable") from exc