Coverage for src/copytrading_engine/parsing/worker.py: 98%
78 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"""One durable extraction step; independent of transport and provider implementation."""
3import datetime as dt
4import logging
5from collections.abc import Callable, Mapping
6from contextlib import AbstractContextManager, nullcontext
8from copytrading_engine.parsing.application import outcome, transform, without_model
9from copytrading_engine.parsing.contracts import ExtractionStore, RequestReservation
10from copytrading_engine.parsing.contracts import RawMessage as _RawMessage
11from copytrading_engine.parsing.extraction import DecodeError, Decoder
12from copytrading_engine.parsing.history import recent_calls
13from copytrading_engine.parsing.readiness import ModelReadiness
14from copytrading_engine.parsing.retry_policy import next_retry_at
15from copytrading_engine.parsing.routes import Route, freeze_routes
16from copytrading_engine.shared.correlation import WorkflowAttempt, bind_workflow_attempt
17from copytrading_engine.shared.route_keys import choose_route
18from copytrading_engine.shared.signals import StockSignal
20log = logging.getLogger(__name__)
23class ParseWorker:
24 def __init__(
25 self,
26 store: ExtractionStore,
27 decoder: Decoder,
28 routes: Mapping[str, Route],
29 model: str,
30 daily_limit: int = 2000,
31 *,
32 observe: Callable[[str], AbstractContextManager] = lambda _: nullcontext(),
33 observe_workflow: Callable[[WorkflowAttempt], AbstractContextManager] = lambda _: (
34 nullcontext()
35 ),
36 on_decision: Callable[[str], None] = lambda _: None,
37 ) -> None:
38 self.store, self.decoder, self.routes = store, decoder, freeze_routes(routes)
39 self.model, self.daily_limit = model, daily_limit
40 self.model_health = ModelReadiness()
41 self.observe = observe
42 self.observe_workflow = observe_workflow
43 self.on_decision = on_decision
45 @property
46 def model_ready(self) -> bool:
47 return self.model_health.ready
49 async def process_next(self, now: dt.datetime) -> bool:
50 job = await self.store.next(now)
51 if job is None:
52 return False
53 key, raw, attempts = job.key, job.raw, job.attempts
54 chosen = choose_route(self.routes, raw.source, raw.channel_id, raw.author_id)
55 route, route_error = chosen.route, chosen.error
56 age = (now - raw.timestamp).total_seconds()
57 result = None
58 reason = None
59 if raw.image_count > 0:
60 reason = "attachments_require_human_review"
61 elif route is None:
62 reason = route_error or "source_profile_not_configured"
63 elif age < -5:
64 reason = "future_signal"
65 elif (result := without_model(raw, route, self.model)) is not None:
66 pass
67 elif attempts >= 3: 67 ↛ 68line 67 didn't jump to line 68 because the condition on line 67 was never true
68 reason = "decode_attempts_exhausted"
69 else:
70 reservation = RequestReservation(
71 key=key,
72 expected_attempts=attempts,
73 day=now.astimezone(dt.UTC).date(),
74 daily_limit=self.daily_limit,
75 retry_at=next_retry_at(now, attempts),
76 )
77 if await self.store.reserve(reservation):
78 correlation = WorkflowAttempt(job.workflow_id, job.trace_id, None, attempts + 1)
79 with bind_workflow_attempt(correlation):
80 with self.observe_workflow(correlation):
81 result = await self._decode(key, raw, route, attempts + 1, now)
82 else:
83 reason = "daily_model_request_budget_exhausted"
84 if reason:
85 if reason == "future_signal":
86 log.warning(
87 "signal_timing_rejected id=%s reason=%s source_at=%s processed_at=%s "
88 "age_seconds=%s",
89 key,
90 reason,
91 raw.timestamp.isoformat(),
92 now.isoformat(),
93 age,
94 )
95 result = self._review(raw, route, reason)
96 if result is not None:
97 await self.store.finish(key, result)
98 self.on_decision(result.decision)
99 log.info("decoded id=%s decision=%s model=%s", key, result.decision, result.model)
100 return True
102 async def _decode(
103 self, key: str, raw: _RawMessage, route: Route, attempt: int, now: dt.datetime
104 ) -> StockSignal | None:
105 try:
106 # The guru's book as the reader sees it beside the post (ADR-0010).
107 recent = recent_calls(await self.store.past_calls(raw.channel_id, key))
108 with self.observe(key):
109 result = await transform(raw, route, self.decoder, self.model, recent)
110 self.model_health.record()
111 return result
112 except DecodeError as exc:
113 # Provider internals are not durable business state or safe log fields.
114 self.model_health.record(exc.reason)
115 issues = exc.issues
116 await self.store.diagnose(key, attempt, now, exc.reason, issues)
117 log.warning(
118 "decode_failed id=%s reason=%s attempt=%s issues=%s",
119 key,
120 exc.reason,
121 attempt,
122 [(issue.path, issue.code) for issue in issues],
123 )
124 return None if exc.retryable else self._review(raw, route, exc.reason)
126 def _review(self, raw: _RawMessage, route: Route | None, reason: str) -> StockSignal:
127 """A post held for the owner keeps the guru and playbook revision its route names."""
128 return outcome(
129 raw,
130 "review",
131 reason,
132 model=self.model,
133 guru_id=route.guru_id if route is not None else None,
134 profile_revision=route.profile_revision if route is not None else None,
135 )