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
351
352
353
//! Starting the `trusty-analyze` daemon the audit's report is built from (#5670).
//!
//! Why: `tga audit` always renders with `--analyze` (see [`super::review`]), and
//! DOC-67 §8 sources the findings table, the complexity distribution and the
//! health factors from that daemon alone. Nothing started it. The renderer's
//! client only ever issues GETs, and its fail-open contract (DOC-67 §9) turns a
//! refused connection into one gap line inside the artifact — so an unattended
//! audit on a machine with no daemon delivered a report whose three
//! analyze-derived sections were empty, and exited 0.
//!
//! This module is the decision that closes it: `tga audit` starts the daemon
//! itself, as a whole-run preflight, and refuses the run when it cannot. It sits
//! beside the two preflights already in `crate::commands::audit` — the inference
//! credential and the renderer version — for the same DOC-67 §2 reason: the
//! sweep gets one non-interactive shot, and a precondition knowable in
//! milliseconds must not be discovered after minutes of collection.
//!
//! What: [`ensure_analyze_daemon`], the binary/socket resolution rules it
//! applies, and [`AnalyzeDaemonUnavailable`].
//!
//! #6287 (ADR-0032) moved the daemon onto a Unix socket, which changes the
//! probe rather than the decision. `trusty_common::daemon_guard`'s
//! `spin_until_ready` is built around a health URL, so the poll loop is local
//! here now; `spawn_detached` from that module still starts the process. The
//! readiness verdict itself got STRICTER in one respect and stayed identical in
//! another: it still refuses a degraded daemon (see [`daemon_is_healthy`]), and
//! it now reads that verdict off a typed field rather than off an HTTP status
//! code the daemon no longer emits.
//!
//! Nothing here is fail-open. Both failure arms — the binary would not spawn,
//! and the daemon never answered — return `Err`, because a report missing those
//! sections reads as a clean pass rather than as an outage.
//! Test: `super::tests` against stubs; `super::real_binary_tests` (`#[ignore]`d)
//! against the real `trusty-analyze` binary.
//!
//! # Spec References
//! - [`SPEC-TGAUDIT-06~draft`](../../../../docs/specs/DOC-67-tga-audit-mode.md#SPEC-TGAUDIT-06~draft)
//! - [`SPEC-TGAUDIT-09~draft`](../../../../docs/specs/DOC-67-tga-audit-mode.md#SPEC-TGAUDIT-09~draft)
use ;
use ;
use ;
/// Environment variable that overrides the `trusty-analyze` binary path.
///
/// Why: `trusty-audit` already sets this on every `tga audit` child it spawns
/// (`crates/trusty-audit/src/run.rs`) so the sweep runs the engagement's pinned
/// copy rather than whatever is on the operator's PATH. Until now nothing on
/// this path read it — #5663 set it believing it pinned the analyze step, and it
/// reached only `trusty-review review`'s subprocess client. Reading it here is
/// what makes that belief true.
pub const ENV_ANALYZE_BIN: &str = "TRUSTY_ANALYZE_BIN";
/// Default binary name searched on PATH.
pub const DEFAULT_ANALYZE_BIN: &str = "trusty-analyze";
/// Environment variable naming the analyze daemon's socket.
///
/// Why: `trusty-review` resolves the same variable for the socket it dials, so
/// the daemon this starts and the daemon the renderer queries are the same
/// process by construction — two spellings would let them disagree silently.
///
/// #6287 renamed it from `PR_INTELLIGENCE_ANALYZER_URL`. The value it carries
/// changed from a URL to a filesystem path, and a variable that still said URL
/// while holding a path would leave an operator setting `http://…` and getting
/// a dial failure that names a socket they never configured. Both crates read
/// the new name in the same change, so there is no window where they disagree.
pub const ENV_ANALYZE_SOCKET: &str = "PR_INTELLIGENCE_ANALYZER_SOCKET";
/// Default analyze daemon socket, as the daemon itself resolves it.
///
/// Why: the daemon binds `trusty_common::daemon_socket_path("trusty-analyze")`
/// and every consumer dials the same call, so there is nothing to keep in sync
/// by hand — which is what the retired `DEFAULT_ANALYZE_URL` constant needed,
/// against `trusty_review::config::DEFAULT_ANALYZER_URL`, and what a derived
/// path removes.
///
/// # Errors
///
/// When the data directory cannot be resolved or created.
/// The audit cannot proceed without the analyze daemon.
///
/// Why: this is refused before the sweep rather than reported after it, so the
/// message is the operator's whole remedy — and the remedy is almost never "run
/// the binary again". `trusty-analyze serve` exits immediately when
/// `trusty-search` is unreachable, which is the usual reason a spawn produces no
/// daemon, so the message names that ordering rather than the symptom.
/// What: the socket probed, the binary tried, and the underlying cause.
/// Test: `super::tests::an_analyze_daemon_that_never_comes_up_refuses_the_audit`.
/// Where and how [`ensure_analyze_daemon`] looks for the daemon.
///
/// Why: the two values come from the environment and the two budgets are fixed,
/// which makes the whole guard untestable if it reads them itself. Taking them
/// as a value is what lets a test drive the spawn-and-poll path against a stub
/// executable on a temp-dir socket — the same split
/// [`super::review::binary_from_override`] uses for its rule.
/// What: the daemon socket, the binary to spawn, and the readiness budget.
/// Test: `super::tests::a_reachable_analyze_daemon_is_not_restarted`.
/// The binary resolution rule: an override wins unless it is empty.
///
/// Why/What/Test: see [`AnalyzeGuard::from_env`]. `None` and `Some("")` both
/// fall back to [`DEFAULT_ANALYZE_BIN`].
pub
/// The socket resolution rule: an override wins unless it is empty.
///
/// Why/What: see [`AnalyzeGuard::from_env`]. `None` and `Some("")` both fall
/// back to [`default_analyze_socket`].
///
/// # Errors
///
/// Only on the fallback path, when the data directory cannot be resolved.
///
/// Test: `super::tests::analyze_resolution_prefers_the_env_overrides`.
pub
/// Is the daemon at `socket` up AND is its own dependency reachable?
///
/// Why: a daemon that is up but degraded — trusty-search unreachable — is not a
/// pass. DOC-67 §8 sources three sections from this daemon's analysis, all of
/// which need the search corpus, so a degraded daemon produces the same empty
/// sections a missing daemon does. Before #6287 that verdict came free: the
/// daemon answered HTTP 503 while degraded and `probe_once` counted only a 2xx.
/// A JSON-RPC health call answers with a RESULT frame either way, so the check
/// has to read `status` — and doing so is what keeps the audit's trusty-search
/// dependency hard on every run rather than only on a fresh spawn.
///
/// What: one `analyze.health` frame; `true` only for a result whose `status` is
/// `"ok"`. Every failure — dial, error frame, missing field, anything else — is
/// `false`, because none of them is evidence the daemon can serve the report.
///
/// Test: `super::tests::a_degraded_analyze_daemon_refuses_the_audit`.
async
/// The method `trusty-analyze` answers a health probe on.
///
/// Duplicated as a literal rather than imported: `tga` has no Cargo edge on
/// `trusty-analyze`. `trusty_analyze::service::METHOD_HEALTH` is the definition;
/// `trusty-analyze/tests/uds_consumer_contract.rs` is what keeps them equal.
const ANALYZE_HEALTH_METHOD: &str = "analyze.health";
/// How long one health dial may take.
const PROBE_TIMEOUT: Duration = from_secs;
/// The exact argument vector the guard hands `trusty-analyze`.
///
/// Why: this list IS the tga→trusty-analyze contract. Building it in a pure
/// function is what lets a test assert its contents without spawning anything —
/// the same reason [`super::review::report_args`] exists on the renderer side.
/// What: `serve`, with no socket argument. #6287: the daemon derives its socket
/// path from the data directory, and passing `--socket` would start a daemon on
/// a path the renderer does not dial. An operator who moved the socket with
/// [`ENV_ANALYZE_SOCKET`] is pointing at a daemon they started themselves; this
/// guard's spawn only ever produces one at the default path.
/// Test: `super::tests::the_spawn_arguments_are_a_bare_serve`.
pub
/// Ensure the analyze daemon is up before the sweep starts.
///
/// Why/What: see the module docs. Resolves the guard from the environment and
/// delegates to [`ensure_analyze_daemon_with`], so the environment is read
/// exactly once, at the public entry point.
///
/// # Errors
///
/// [`AnalyzeDaemonUnavailable`] when the daemon is absent and cannot be started.
///
/// Test: `super::tests::a_reachable_analyze_daemon_is_not_restarted`.
pub async
/// [`ensure_analyze_daemon`] with the address, binary and budgets already fixed.
///
/// Why: taking them as a value is what lets a test drive the whole spawn-and-poll
/// path against a stub executable on an ephemeral port, without touching the
/// process environment or leaving a daemon behind.
/// What: calls `analyze.health` on the socket; on a healthy answer, returns
/// without spawning anything. On a miss, spawns `<binary> serve` detached and
/// polls until it answers healthy. Neither failure is downgraded — a spawn that
/// fails and a daemon that never answers both return `Err`.
///
/// A spawn that succeeds proves only that a process started. `trusty-analyze
/// serve` exits 1 when trusty-search is unreachable, so the readiness poll — not
/// the PID — is what decides the verdict.
///
/// A daemon that is up but degraded — which is what `trusty-analyze` reports
/// while trusty-search is unreachable — is not a pass either; see
/// [`daemon_is_healthy`] for why that check has to be explicit since #6287.
/// That is what makes the audit's trusty-search dependency hard on every run
/// rather than only on a fresh spawn.
///
/// Concurrent calls are safe and independent: the guard is read-only shared
/// input, and nothing is cached between calls, so each reaches its own verdict
/// from its own probe. There is no cross-call spawn deduplication — see
/// `super::tests::two_concurrent_guards_both_resolve_against_one_slow_daemon`
/// for why none is owed.
///
/// # Errors
///
/// [`AnalyzeDaemonUnavailable`] carrying the spawn error, or the readiness
/// timeout.
///
/// Test: `super::tests::{a_reachable_analyze_daemon_is_not_restarted,
/// an_unspawnable_analyze_binary_refuses_the_audit,
/// an_analyze_daemon_that_never_comes_up_refuses_the_audit,
/// a_degraded_analyze_daemon_refuses_the_audit,
/// two_concurrent_guards_both_resolve_against_one_slow_daemon}`, and
/// `super::real_binary_tests` end to end.
pub async