1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//! Inference-reachability probe for the `review_health` endpoint.
//!
//! Why: MPM and other orchestrators need to distinguish "service up AND
//! inference working" from "service up but creds expired / endpoint
//! unreachable" before paying the cost of a full `review_pr` call.
//! A cheap, cached liveness probe lets callers gate on a single JSON field
//! (`inference`) rather than attempting a real review and handling the failure.
//!
//! What: `InferenceProbe` wraps a short-TTL cache (default 10 s) around a
//! minimal real LLM call (max_tokens=1). The cache means repeated `/health`
//! polls don't hammer the provider. The per-probe timeout is configurable via
//! `TRUSTY_REVIEW_HEALTH_TIMEOUT_SECS` (default 10 s, see #739); a timed-out
//! probe returns `Unknown` rather than `Unreachable` so a slow Bedrock cold-start
//! does not falsely degrade health — real review calls have a ~300 s budget, so
//! a probe timeout should not be treated as a hard unreachability signal.
//! `InferenceStatus` maps provider errors to the four states: `ok`,
//! `unreachable`, `auth_error`, `unknown`.
//!
//! Consecutive-unknown degradation (#820): a permanently hung or misconfigured
//! endpoint would otherwise report `inference: "unknown"` indefinitely while the
//! top-level `status` stays `"ok"` (because `Unknown` is non-degrading per #739).
//! To surface stuck endpoints, `InferenceProbe` tracks a consecutive-unknown
//! counter. Once `CONSECUTIVE_UNKNOWN_DEGRADATION_THRESHOLD` (default 3)
//! consecutive probes all return `Unknown`, `effective_status()` returns
//! `Unreachable` instead — which `compute_status` then maps to `"degraded"`.
//! The counter resets on any non-Unknown probe result.
//!
//! Test: `probe_status_*` unit tests in this module inject stub providers and
//! verify each status transition. Live credential tests are separate (ignored).
use ;
use ;
use ;
use ;
// ─── Configurable timeout helper ─────────────────────────────────────────────
/// After this many consecutive `Unknown` probe results, `InferenceProbe` reports
/// `Unreachable` to signal a stuck or misconfigured endpoint (#820).
///
/// Why: a single `Unknown` (probe timed out) must not degrade health — it may be
/// a normal Bedrock cold-start (#739). But N consecutive `Unknown` results
/// indicate a permanently hung endpoint that would otherwise stay invisible.
/// What: used by `InferenceProbe::effective_status` to decide when to escalate.
/// Test: `consecutive_unknown_degrades_after_threshold` in the tests module.
pub const CONSECUTIVE_UNKNOWN_DEGRADATION_THRESHOLD: u32 = 3;
/// Return the per-probe hard timeout, consulting `TRUSTY_REVIEW_HEALTH_TIMEOUT_SECS`.
///
/// Why: AWS Bedrock cold-starts were measured at ~7.4 s (#739), which exceeded
/// the previous hard-coded 3 s timeout and caused spurious `unreachable` /
/// `degraded` health results. Making the timeout configurable lets operators
/// tune it without recompiling; 10 s comfortably covers observed cold-start
/// latency while still bounding health-check latency.
/// What: reads `TRUSTY_REVIEW_HEALTH_TIMEOUT_SECS` from the environment; parses
/// it as a `u64`; falls back to `DEFAULT_HEALTH_TIMEOUT_SECS` (10) on any
/// parse failure or if the variable is unset. A value of 0 is treated as
/// "use default" to prevent an accidentally zero timeout from hanging forever.
/// Test: `health_probe_timeout_default`, `health_probe_timeout_env_override`,
/// `health_probe_timeout_env_invalid_falls_back`,
/// `health_probe_timeout_env_zero_falls_back` in the `tests` module.
pub
use crateLlmResponse;
use crate;
// ─── Status enum ─────────────────────────────────────────────────────────────
/// Inference-reachability status produced by the lightweight probe.
///
/// Why: callers need to distinguish four distinct states so they can decide
/// the appropriate remediation (retry vs. fix creds vs. check network).
/// What: serialises as lowercase string (`"ok"`, `"unreachable"`, etc.).
/// Test: `probe_status_serialises_lowercase`.
// ─── Error-to-status mapping ──────────────────────────────────────────────────
/// Map an `LlmError` to the appropriate `InferenceStatus`.
///
/// Why: the four `InferenceStatus` variants each correspond to a subset of
/// `LlmError` variants; centralising the mapping keeps it consistent across
/// HTTP and MCP paths.
/// What: auth/validation errors → `AuthError`; transport/rate/5xx → `Unreachable`.
/// Test: `error_mapping_*` tests cover each `LlmError` variant.
// ─── Cached probe ─────────────────────────────────────────────────────────────
/// Cached inference-reachability probe with consecutive-unknown degradation (#820).
///
/// Why: running a live LLM call on every `/health` hit is expensive and slow.
/// The cache amortises the probe cost across a configurable TTL window (default
/// 10 s) so repeated health polls don't hammer the provider.
/// A single `Unknown` result (probe timed out) is non-degrading (#739) to
/// accommodate Bedrock cold-starts, but a permanently hung endpoint would stay
/// invisible indefinitely. The consecutive-unknown counter catches that case:
/// after `CONSECUTIVE_UNKNOWN_DEGRADATION_THRESHOLD` consecutive `Unknown` results
/// `effective_status()` returns `Unreachable`, which `compute_status` maps to
/// `"degraded"`. The counter resets on any non-Unknown result.
/// What: holds a `Mutex`-guarded `Option<(InferenceStatus, Instant)>` for the
/// cache, and an `AtomicU32` for the consecutive-unknown streak. `probe` returns
/// the cached value (or runs a fresh probe); `effective_status` applies the
/// degradation rule before returning to callers.
/// Test: `probe_cache_ttl_*`, `consecutive_unknown_degrades_after_threshold`,
/// `consecutive_unknown_resets_on_ok`.
// ─── Low-level probe ──────────────────────────────────────────────────────────
/// Issue the smallest possible real request to the LLM provider.
///
/// Why: a real (not mocked) request exercises both connectivity AND auth,
/// so we can distinguish `unreachable` from `auth_error` — a purely
/// credential-check API (if one existed) would not verify connectivity.
/// What: sends a 1-token completion with a trivial prompt through the provider;
/// maps any error to `InferenceStatus` via `map_llm_error`. The call is
/// wrapped in `tokio::time::timeout` so a hung endpoint never stalls /health.
/// A timeout returns `Unknown` rather than `Unreachable` (#739): the probe
/// budget is much shorter than the real review budget (~300 s), so a slow
/// cold-start should not be reported as "endpoint unreachable" — it is simply
/// "could not confirm reachability within the probe window".
/// Test: `probe_returns_ok_on_success`, `probe_returns_auth_error_on_access_denied`,
/// `probe_returns_unreachable_on_transport`, `probe_timeout_returns_unknown`.
async
// ─── Unit tests ───────────────────────────────────────────────────────────────
// Split into a sibling file to keep this file under the 500-line cap (#610).
// The sibling file is included as the module body via `#[path = ...]` so it
// has full access to private items (`run_probe`, etc.) just as inline tests would.