Coverage for src/copytrading_engine/parsing/providers/transport.py: 82%

149 statements  

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

1"""Bounded capture at the SDK's serialized HTTP request/response stream boundary.""" 

2 

3import json 

4from collections.abc import AsyncIterator, Callable 

5from typing import cast 

6 

7import httpx2 as httpx 

8 

9from copytrading_engine.shared.correlation import WorkflowAttempt, current_workflow_attempt 

10from copytrading_engine.shared.payload_capture import ( 

11 CaptureKind, 

12 CaptureStatus, 

13 JsonValue, 

14 PayloadCapture, 

15 ProviderName, 

16) 

17 

18 

19class _BoundedBytes: 

20 def __init__(self, maximum: int) -> None: 

21 self.maximum = maximum 

22 self.total = 0 

23 self.parts: list[bytes] = [] 

24 self.retained = 0 

25 self.complete = False 

26 

27 def append(self, chunk: bytes) -> None: 

28 self.total += len(chunk) 

29 remaining = self.maximum - self.retained 

30 if remaining > 0: 30 ↛ exitline 30 didn't return from function 'append' because the condition on line 30 was always true

31 retained = chunk[:remaining] 

32 self.parts.append(retained) 

33 self.retained += len(retained) 

34 

35 def finish(self) -> None: 

36 self.complete = True 

37 

38 def value(self) -> bytes | None: 

39 if self.total > self.maximum: 39 ↛ 40line 39 didn't jump to line 40 because the condition on line 39 was never true

40 return None 

41 return b"".join(self.parts) 

42 

43 

44class _TeeStream(httpx.AsyncByteStream): 

45 def __init__(self, source: httpx.AsyncByteStream, capture: _BoundedBytes) -> None: 

46 self._source = source 

47 self._capture = capture 

48 self._closed = False 

49 

50 async def __aiter__(self) -> AsyncIterator[bytes]: 

51 try: 

52 async for chunk in self._source: 

53 self._capture.append(chunk) 

54 yield chunk 

55 except BaseException: 

56 await self.aclose() 

57 raise 

58 else: 

59 self._capture.finish() 

60 

61 async def aclose(self) -> None: 

62 if self._closed: 

63 return 

64 self._closed = True 

65 await self._source.aclose() 

66 

67 

68class DiagnosticHTTPTransport(httpx.AsyncBaseTransport): 

69 """Tee SDK wire bodies into redacted diagnostics without retaining unbounded bytes.""" 

70 

71 def __init__( 

72 self, 

73 inner: httpx.AsyncBaseTransport, 

74 sink: PayloadCapture | None, 

75 *, 

76 provider: ProviderName, 

77 maximum_bytes: int = 48 * 1024, 

78 ) -> None: 

79 if not 1 <= maximum_bytes <= 48 * 1024: 79 ↛ 80line 79 didn't jump to line 80 because the condition on line 79 was never true

80 raise ValueError("provider payload capture limit is outside supported bounds") 

81 self._inner = inner 

82 self._sink = sink 

83 self._provider = provider 

84 self._maximum_bytes = maximum_bytes 

85 

86 async def handle_async_request(self, request: httpx.Request) -> httpx.Response: 

87 attempt = current_workflow_attempt() 

88 request_bytes = _BoundedBytes(self._maximum_bytes) 

89 try: 

90 available_request_body = request.content 

91 except httpx.RequestNotRead: 

92 available_request_body = None 

93 if available_request_body is not None: 93 ↛ 97line 93 didn't jump to line 97 because the condition on line 93 was always true

94 request_bytes.append(available_request_body) 

95 request_bytes.finish() 

96 else: 

97 request.stream = _TeeStream(cast(httpx.AsyncByteStream, request.stream), request_bytes) 

98 try: 

99 response = await self._inner.handle_async_request(request) 

100 except BaseException: 

101 self._emit( 

102 "model_request", 

103 request_bytes, 

104 attempt, 

105 status=_stream_status(request_bytes), 

106 ) 

107 self._emit("model_response", None, attempt, status="missing") 

108 raise 

109 self._emit( 

110 "model_request", 

111 request_bytes, 

112 attempt, 

113 status=_stream_status(request_bytes), 

114 method=request.method, 

115 path=request.url.path, 

116 ) 

117 response_bytes = _BoundedBytes(self._maximum_bytes) 

118 # The wire carries the body as the server compressed it; the journal keeps it readable. 

119 encoding = response.headers.get("content-encoding") 

120 if response.is_stream_consumed: 

121 # Already read, so already decoded. 

122 response_bytes.append(response.content) 

123 response_bytes.finish() 

124 self._emit( 

125 "model_response", 

126 response_bytes, 

127 attempt, 

128 status=_stream_status(response_bytes), 

129 method=request.method, 

130 path=request.url.path, 

131 status_code=response.status_code, 

132 ) 

133 return response 

134 response.stream = _ResponseTeeStream( 

135 cast(httpx.AsyncByteStream, response.stream), 

136 response_bytes, 

137 lambda: self._emit( 

138 "model_response", 

139 response_bytes, 

140 attempt, 

141 status=_stream_status(response_bytes), 

142 method=request.method, 

143 path=request.url.path, 

144 status_code=response.status_code, 

145 encoding=encoding, 

146 ), 

147 lambda: self._emit( 

148 "model_response", 

149 response_bytes, 

150 attempt, 

151 status="incomplete" if response_bytes.total else "missing", 

152 method=request.method, 

153 path=request.url.path, 

154 status_code=response.status_code, 

155 encoding=encoding, 

156 ), 

157 ) 

158 return response 

159 

160 async def aclose(self) -> None: 

161 await self._inner.aclose() 

162 

163 def _emit( 

164 self, 

165 kind: CaptureKind, 

166 capture: _BoundedBytes | None, 

167 attempt: WorkflowAttempt | None, 

168 *, 

169 status: CaptureStatus, 

170 method: str | None = None, 

171 path: str | None = None, 

172 status_code: int | None = None, 

173 encoding: str | None = None, 

174 ) -> None: 

175 if self._sink is None: 175 ↛ 176line 175 didn't jump to line 176 because the condition on line 175 was never true

176 return 

177 payload = ( 

178 None 

179 if capture is None 

180 else _wire_payload( 

181 _decoded(capture.value(), encoding), status_code, method=method, path=path 

182 ) 

183 ) 

184 source_bytes = 0 if capture is None else capture.total 

185 try: 

186 self._sink.record_payload( 

187 capture_kind=kind, 

188 workflow_id=attempt.workflow_id if attempt else None, 

189 trace_id=attempt.trace_id if attempt else None, 

190 destination_id=attempt.destination_id if attempt else None, 

191 attempt=attempt.attempt if attempt else None, 

192 provider=self._provider, 

193 payload=payload, 

194 source_bytes=source_bytes, 

195 max_bytes=self._maximum_bytes, 

196 status=status, 

197 ) 

198 except Exception: # noqa: BLE001 - instrumentation must not alter provider behavior 

199 # Instrumentation is a best-effort side effect and cannot alter provider behavior. 

200 return 

201 

202 

203class _ResponseTeeStream(_TeeStream): 

204 def __init__( 

205 self, 

206 source: httpx.AsyncByteStream, 

207 capture: _BoundedBytes, 

208 on_complete: Callable[[], None], 

209 on_incomplete: Callable[[], None], 

210 ) -> None: 

211 super().__init__(source, capture) 

212 self._on_complete = on_complete 

213 self._on_incomplete = on_incomplete 

214 self._reported = False 

215 

216 async def __aiter__(self) -> AsyncIterator[bytes]: 

217 try: 

218 async for chunk in self._source: 

219 self._capture.append(chunk) 

220 yield chunk 

221 except BaseException: 

222 self._report_incomplete() 

223 await super().aclose() 

224 raise 

225 else: 

226 self._capture.finish() 

227 self._report_complete() 

228 

229 async def aclose(self) -> None: 

230 if not self._reported: 230 ↛ 231line 230 didn't jump to line 231 because the condition on line 230 was never true

231 self._report_incomplete() 

232 await super().aclose() 

233 

234 def _report_complete(self) -> None: 

235 if not self._reported: 235 ↛ exitline 235 didn't return from function '_report_complete' because the condition on line 235 was always true

236 self._reported = True 

237 self._on_complete() 

238 

239 def _report_incomplete(self) -> None: 

240 if not self._reported: 240 ↛ exitline 240 didn't return from function '_report_incomplete' because the condition on line 240 was always true

241 self._reported = True 

242 self._on_incomplete() 

243 

244 

245def _stream_status(capture: _BoundedBytes) -> CaptureStatus: 

246 if capture.total > capture.maximum: 246 ↛ 247line 246 didn't jump to line 247 because the condition on line 246 was never true

247 return "oversize" 

248 if capture.complete: 248 ↛ 250line 248 didn't jump to line 250 because the condition on line 248 was always true

249 return "complete" 

250 return "incomplete" if capture.total else "missing" 

251 

252 

253def _decoded(body: bytes | None, encoding: str | None) -> bytes | None: 

254 """The body as the client read it: the content encoding the server applied (gzip, br, zstd) 

255 undone by the HTTP client's own decoders, so secrets in it can be found and redacted. A body 

256 that will not decode is kept as it came.""" 

257 if body is None or not encoding or encoding.strip().lower() == "identity": 

258 return body 

259 try: 

260 return httpx.Response(200, headers={"content-encoding": encoding}, content=body).read() 

261 except httpx.DecodingError: 

262 return body 

263 

264 

265def _wire_payload( 

266 body: bytes | None, 

267 status_code: int | None, 

268 *, 

269 method: str | None, 

270 path: str | None, 

271) -> dict[str, JsonValue] | None: 

272 if body is None: 272 ↛ 273line 272 didn't jump to line 273 because the condition on line 272 was never true

273 return None 

274 text = body.decode("utf-8", errors="replace") 

275 try: 

276 serialized: JsonValue = json.loads(text) 

277 except json.JSONDecodeError: 

278 serialized = text 

279 result: dict[str, JsonValue] = {"body": serialized} 

280 if method is not None: 

281 result["method"] = method 

282 if path is not None: 

283 result["path"] = path 

284 if status_code is not None: 

285 result["status_code"] = status_code 

286 return result