Skip to main content

rch_common/
types.rs

1//! Common types used across RCH components.
2
3use crate::{CompilationKind, toolchain::ToolchainInfo};
4use rand::RngExt;
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7use std::path::PathBuf;
8
9/// Unique identifier for a worker in the fleet.
10#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
11pub struct WorkerId(pub String);
12
13impl WorkerId {
14    pub fn new(id: impl Into<String>) -> Self {
15        Self(id.into())
16    }
17
18    pub fn as_str(&self) -> &str {
19        &self.0
20    }
21}
22
23impl std::fmt::Display for WorkerId {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        write!(f, "{}", self.0)
26    }
27}
28
29/// Status of a worker in the fleet.
30///
31/// State transitions:
32/// ```text
33/// HEALTHY ←→ DEGRADED (automatic based on response times)
34///    ↓           ↓
35///    ↓       UNREACHABLE (automatic on heartbeat failure)
36///    ↓
37/// DRAINING (via `rch workers drain`) - finishing current jobs
38///    ↓
39/// DRAINED (automatic when all jobs complete) - idle, ready to disable
40///    ↓
41/// DISABLED (via `rch workers disable`) - completely offline
42///
43/// Use `rch workers enable` to return from DRAINING/DRAINED/DISABLED → HEALTHY
44/// ```
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
46#[serde(rename_all = "snake_case")]
47pub enum WorkerStatus {
48    /// Worker is healthy and accepting jobs.
49    #[default]
50    Healthy,
51    /// Worker is responding slowly.
52    Degraded,
53    /// Worker failed to respond to heartbeat.
54    Unreachable,
55    /// Worker is not accepting new jobs (finishing current).
56    Draining,
57    /// Worker has finished draining (no active jobs, idle).
58    Drained,
59    /// Worker is manually disabled.
60    Disabled,
61}
62
63/// Circuit breaker state for a worker.
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
65#[serde(rename_all = "snake_case")]
66pub enum CircuitState {
67    /// Normal operation.
68    #[default]
69    Closed,
70    /// Circuit is open (short-circuit).
71    Open,
72    /// Circuit is half-open (probing).
73    HalfOpen,
74}
75
76/// Required runtime for command execution.
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, JsonSchema)]
78#[serde(rename_all = "lowercase")]
79pub enum RequiredRuntime {
80    /// No specific runtime required (default).
81    #[default]
82    None,
83    /// Requires Rust toolchain.
84    Rust,
85    /// Requires Bun runtime.
86    Bun,
87    /// Requires Node.js runtime.
88    Node,
89    /// Requires the Nix package manager (nix binary + populated `/nix/store`).
90    Nix,
91    /// Requires the Go toolchain (`go` binary).
92    Go,
93    /// Requires the zig cross-compilation toolchain (`cargo-zigbuild` + `zig`).
94    Zig,
95}
96
97/// Per-command priority hint for worker selection.
98///
99/// This is an *input hint* from the caller (typically the hook) and should not
100/// change default behavior when omitted.
101#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, JsonSchema)]
102#[serde(rename_all = "lowercase")]
103pub enum CommandPriority {
104    Low,
105    #[default]
106    Normal,
107    High,
108}
109
110impl std::fmt::Display for CommandPriority {
111    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
112        let value = match self {
113            Self::Low => "low",
114            Self::Normal => "normal",
115            Self::High => "high",
116        };
117        write!(f, "{}", value)
118    }
119}
120
121impl std::str::FromStr for CommandPriority {
122    type Err = ();
123
124    fn from_str(s: &str) -> Result<Self, Self::Err> {
125        match s.trim().to_lowercase().as_str() {
126            "low" => Ok(Self::Low),
127            "normal" => Ok(Self::Normal),
128            "high" => Ok(Self::High),
129            _ => Err(()),
130        }
131    }
132}
133
134/// Worker selection request sent from hook to daemon.
135#[derive(Debug, Clone, Serialize, Deserialize)]
136pub struct SelectionRequest {
137    /// Project identifier (usually directory name or hash).
138    pub project: String,
139    /// Full command being executed (optional, for active build tracking).
140    #[serde(default, skip_serializing_if = "Option::is_none")]
141    pub command: Option<String>,
142    /// Priority hint for this request.
143    #[serde(default)]
144    pub command_priority: CommandPriority,
145    /// Estimated CPU cores needed for this compilation.
146    pub estimated_cores: u32,
147    /// Additional free GiB required on the worker's reported build filesystem.
148    /// A positive value is a hard admission budget, retained by the daemon for
149    /// this build's lifetime; zero leaves disk admission to the ordinary policy.
150    #[serde(default, skip_serializing_if = "disk_headroom_disabled")]
151    pub disk_headroom_gib: u32,
152    /// Preferred worker IDs (e.g., from project config).
153    #[serde(default)]
154    pub preferred_workers: Vec<WorkerId>,
155    /// Rust toolchain information for the project.
156    #[serde(default)]
157    pub toolchain: Option<ToolchainInfo>,
158    /// Required runtime for command execution.
159    #[serde(default)]
160    pub required_runtime: RequiredRuntime,
161    /// Classification decision latency in microseconds (for AGENTS.md compliance).
162    /// This tracks how long the 5-tier classification took on the hook side.
163    #[serde(default, skip_serializing_if = "Option::is_none")]
164    pub classification_duration_us: Option<u64>,
165    /// Job-mode admission (bd-g7rpy): set by `rch exec --job` only. When the
166    /// one-active-job-per-project-per-worker guard excludes every otherwise-
167    /// eligible worker, a job request may QUEUE on that exclusion instead of
168    /// failing over to local execution. Compilation requests never set it,
169    /// preserving their immediate NoAdmissibleWorkers behavior.
170    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
171    pub job_mode: bool,
172    /// Operator-declared named tools this work requires VERIFIED on the worker
173    /// (`rch exec --job --require-tool NAME`, or a project's
174    /// `jobs.required_tools`).
175    ///
176    /// Job mode only — the compilation classifier never sets it, so ordinary
177    /// offloaded builds keep their existing admission behavior exactly. Empty
178    /// means no gate; a name no worker has verified excludes every worker,
179    /// because the caller asked for the gate precisely because the job cannot
180    /// run without the tool.
181    #[serde(default, skip_serializing_if = "Vec::is_empty")]
182    pub required_tools: Vec<String>,
183    /// Process ID of the hook (for active build tracking).
184    #[serde(default, skip_serializing_if = "Option::is_none")]
185    pub hook_pid: Option<u32>,
186}
187
188fn disk_headroom_disabled(gib: &u32) -> bool {
189    *gib == 0
190}
191
192/// Reason for worker selection result.
193///
194/// Provides context when no worker is available, enabling informative
195/// fallback messages in the hook.
196#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
197#[serde(rename_all = "snake_case")]
198pub enum SelectionReason {
199    /// Worker assigned successfully.
200    Success,
201    /// No workers configured in workers.toml.
202    NoWorkersConfigured,
203    /// All workers are unreachable (failed health checks).
204    AllWorkersUnreachable,
205    /// All workers have circuits open (after repeated failures).
206    AllCircuitsOpen,
207    /// All workers are at capacity (no available slots).
208    AllWorkersBusy,
209    /// Workers were present but none passed selection health thresholds.
210    NoWorkersPassedHealth,
211    /// All candidate workers failed hard preflight checks.
212    AllWorkersFailedPreflight,
213    /// All candidate workers failed repo convergence checks (repos missing/stale/failed).
214    AllWorkersFailedConvergence,
215    /// Workers exist but admission was blocked by concrete capacity/preflight reasons.
216    NoAdmissibleWorkers(String),
217    /// No workers match required tags or preferences.
218    NoMatchingWorkers,
219    /// No workers have the required runtime (e.g., Bun, Node).
220    NoWorkersWithRuntime(String),
221    /// Internal error during selection.
222    SelectionError(String),
223    /// Worker assigned via affinity pinning (recent successful build).
224    AffinityPinned,
225    /// Worker assigned via last-success fallback (all others unavailable).
226    AffinityFallback,
227}
228
229/// Wire protocol version for daemon `/select-worker` responses.
230///
231/// This is intentionally separate from the broader CLI API envelope version:
232/// selection responses are emitted as a raw [`SelectionResponse`] on the hook
233/// hot path, so the client needs a small explicit compatibility marker.
234pub const SELECTION_RESPONSE_PROTOCOL_VERSION: u64 = 1;
235
236impl std::fmt::Display for SelectionReason {
237    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
238        match self {
239            Self::Success => write!(f, "worker assigned successfully"),
240            Self::NoWorkersConfigured => write!(f, "no workers configured"),
241            Self::AllWorkersUnreachable => write!(f, "all workers unreachable"),
242            Self::AllCircuitsOpen => write!(f, "all worker circuits open"),
243            Self::AllWorkersBusy => write!(f, "all workers at capacity"),
244            Self::NoWorkersPassedHealth => write!(f, "no workers passed health thresholds"),
245            Self::AllWorkersFailedPreflight => write!(f, "all workers failed preflight checks"),
246            Self::AllWorkersFailedConvergence => {
247                write!(f, "all workers failed repo convergence checks")
248            }
249            Self::NoAdmissibleWorkers(summary) => {
250                write!(f, "no admissible workers: {}", summary)
251            }
252            Self::NoMatchingWorkers => write!(f, "no matching workers found"),
253            Self::NoWorkersWithRuntime(rt) => write!(f, "no workers with {} installed", rt),
254            Self::SelectionError(e) => write!(f, "selection error: {}", e),
255            Self::AffinityPinned => write!(f, "worker assigned via affinity pinning"),
256            Self::AffinityFallback => write!(f, "worker assigned via last-success fallback"),
257        }
258    }
259}
260
261// ============================================================================
262// Worker Selection Strategy
263// ============================================================================
264
265/// Worker selection strategy determining how workers are chosen for jobs.
266#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
267#[serde(rename_all = "snake_case")]
268pub enum SelectionStrategy {
269    /// Legacy/manual control: prioritize worker.priority first, then break ties
270    /// using command-aware cache/speed hints.
271    Priority,
272    /// Select worker with highest SpeedScore.
273    /// Best for performance-critical builds with homogeneous workers.
274    Fastest,
275    /// Balance all factors: SpeedScore, load, health, cache affinity.
276    /// Default for general use across diverse worker pools.
277    #[default]
278    Balanced,
279    /// Prefer workers with warm caches for the project.
280    /// Best for incremental builds on large codebases.
281    CacheAffinity,
282    /// Weighted random selection favoring fast workers but ensuring fairness.
283    /// Prevents hot-spotting when many workers are available.
284    FairFastest,
285}
286
287impl std::fmt::Display for SelectionStrategy {
288    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
289        let name = match self {
290            Self::Priority => "priority",
291            Self::Fastest => "fastest",
292            Self::Balanced => "balanced",
293            Self::CacheAffinity => "cache_affinity",
294            Self::FairFastest => "fair_fastest",
295        };
296        write!(f, "{}", name)
297    }
298}
299
300impl std::str::FromStr for SelectionStrategy {
301    type Err = String;
302
303    fn from_str(s: &str) -> Result<Self, Self::Err> {
304        match s.trim().to_lowercase().as_str() {
305            "priority" => Ok(Self::Priority),
306            "fastest" => Ok(Self::Fastest),
307            "balanced" => Ok(Self::Balanced),
308            "cache_affinity" | "cache-affinity" | "cacheaffinity" => Ok(Self::CacheAffinity),
309            "fair_fastest" | "fair-fastest" | "fairfastest" => Ok(Self::FairFastest),
310            _ => Err(format!(
311                "unknown selection strategy '{}', expected one of: priority, fastest, balanced, cache_affinity, fair_fastest",
312                s
313            )),
314        }
315    }
316}
317
318/// Configuration for the worker selection algorithm.
319#[derive(Debug, Clone, Serialize, Deserialize)]
320pub struct SelectionConfig {
321    /// Selection strategy to use.
322    #[serde(default)]
323    pub strategy: SelectionStrategy,
324    /// Minimum success rate (0.0-1.0) for a worker to be considered.
325    #[serde(default = "default_min_success_rate")]
326    pub min_success_rate: f64,
327    /// Factor weights for the balanced strategy.
328    #[serde(default)]
329    pub weights: SelectionWeightConfig,
330    /// Fairness settings for fair_fastest strategy.
331    #[serde(default)]
332    pub fairness: FairnessConfig,
333    /// Cache affinity settings for project-to-worker pinning.
334    #[serde(default)]
335    pub affinity: AffinityConfig,
336
337    // Preflight health guards (bd-3eaa)
338    /// Maximum load-per-core threshold. Workers exceeding this are skipped.
339    /// Set to None to disable load-based filtering.
340    #[serde(default = "default_max_load_per_core")]
341    pub max_load_per_core: Option<f64>,
342    /// Minimum free disk space in GB. Workers below this are skipped.
343    /// Set to None to disable the free-space floor; per-slot disk derating still applies.
344    #[serde(default = "default_min_free_gb")]
345    pub min_free_gb: Option<f64>,
346    /// Disk budget in GiB per concurrent slot, after reserving `min_free_gb`.
347    /// Must be finite and positive. Unknown disk telemetry preserves configured slots.
348    #[serde(
349        default = "default_disk_gb_per_slot",
350        deserialize_with = "deserialize_disk_gb_per_slot"
351    )]
352    pub disk_gb_per_slot: f64,
353}
354
355impl Default for SelectionConfig {
356    fn default() -> Self {
357        Self {
358            strategy: SelectionStrategy::default(),
359            min_success_rate: default_min_success_rate(),
360            weights: SelectionWeightConfig::default(),
361            fairness: FairnessConfig::default(),
362            affinity: AffinityConfig::default(),
363            max_load_per_core: default_max_load_per_core(),
364            min_free_gb: default_min_free_gb(),
365            disk_gb_per_slot: default_disk_gb_per_slot(),
366        }
367    }
368}
369
370fn default_min_success_rate() -> f64 {
371    0.8
372}
373
374fn default_max_load_per_core() -> Option<f64> {
375    Some(2.0) // Skip workers with load/core > 2.0
376}
377
378fn default_min_free_gb() -> Option<f64> {
379    Some(10.0) // Skip workers with < 10 GB free
380}
381
382fn default_disk_gb_per_slot() -> f64 {
383    10.0
384}
385
386fn deserialize_disk_gb_per_slot<'de, D>(deserializer: D) -> Result<f64, D::Error>
387where
388    D: serde::Deserializer<'de>,
389{
390    let budget = f64::deserialize(deserializer)?;
391    if budget.is_finite() && budget > 0.0 {
392        Ok(budget)
393    } else {
394        Err(serde::de::Error::custom(
395            "disk_gb_per_slot must be finite and greater than zero",
396        ))
397    }
398}
399
400/// Weight configuration for the balanced selection strategy.
401#[derive(Debug, Clone, Serialize, Deserialize)]
402pub struct SelectionWeightConfig {
403    /// Weight for SpeedScore (0.0-1.0).
404    #[serde(default = "default_weight_speedscore")]
405    pub speedscore: f64,
406    /// Weight for available slots (0.0-1.0).
407    #[serde(default = "default_weight_slots")]
408    pub slots: f64,
409    /// Weight for health/success rate (0.0-1.0).
410    #[serde(default = "default_weight_health")]
411    pub health: f64,
412    /// Weight for cache affinity (0.0-1.0).
413    #[serde(default = "default_weight_cache")]
414    pub cache: f64,
415    /// Weight for network latency (0.0-1.0).
416    #[serde(default = "default_weight_network")]
417    pub network: f64,
418    /// Weight for worker priority (0.0-1.0).
419    #[serde(default = "default_weight_priority")]
420    pub priority: f64,
421    /// Weight for disk headroom (0.0-1.0); zero disables disk ranking.
422    #[serde(
423        default = "default_weight_disk",
424        deserialize_with = "deserialize_weight_disk"
425    )]
426    pub disk: f64,
427    /// Penalty multiplier for half-open circuit workers (0.0-1.0).
428    #[serde(default = "default_half_open_penalty")]
429    pub half_open_penalty: f64,
430}
431
432impl Default for SelectionWeightConfig {
433    fn default() -> Self {
434        Self {
435            speedscore: default_weight_speedscore(),
436            slots: default_weight_slots(),
437            health: default_weight_health(),
438            cache: default_weight_cache(),
439            network: default_weight_network(),
440            priority: default_weight_priority(),
441            disk: default_weight_disk(),
442            half_open_penalty: default_half_open_penalty(),
443        }
444    }
445}
446
447fn default_weight_speedscore() -> f64 {
448    0.5
449}
450fn default_weight_slots() -> f64 {
451    0.4
452}
453fn default_weight_health() -> f64 {
454    0.3
455}
456fn default_weight_cache() -> f64 {
457    0.2
458}
459fn default_weight_network() -> f64 {
460    0.1
461}
462fn default_weight_priority() -> f64 {
463    // Worker priority is a first-class selection factor: operators set high
464    // priority on preferred boxes (e.g. fast dedicated workers) and expect them
465    // to actually receive work. At the old 0.1 a top-priority worker gained at
466    // most +0.1 to its balanced score — too weak to overcome a warm-cache or
467    // lower-latency competitor, so the highest-priority workers could sit idle
468    // indefinitely. Load/slots still throttle a high-priority worker as it fills.
469    0.5
470}
471fn default_half_open_penalty() -> f64 {
472    0.5
473}
474
475fn default_weight_disk() -> f64 {
476    0.2
477}
478
479fn deserialize_weight_disk<'de, D>(deserializer: D) -> Result<f64, D::Error>
480where
481    D: serde::Deserializer<'de>,
482{
483    let weight = f64::deserialize(deserializer)?;
484    if weight.is_finite() && (0.0..=1.0).contains(&weight) {
485        Ok(weight)
486    } else {
487        Err(serde::de::Error::custom(
488            "disk weight must be finite and between zero and one",
489        ))
490    }
491}
492
493/// Fairness settings for the fair_fastest selection strategy.
494#[derive(Debug, Clone, Serialize, Deserialize)]
495pub struct FairnessConfig {
496    /// Lookback window in seconds for tracking recent selections.
497    #[serde(default = "default_fairness_lookback_secs")]
498    pub lookback_secs: u64,
499    /// Maximum consecutive selections for a single worker before penalty.
500    #[serde(default = "default_max_consecutive_selections")]
501    pub max_consecutive_selections: u32,
502}
503
504impl Default for FairnessConfig {
505    fn default() -> Self {
506        Self {
507            lookback_secs: default_fairness_lookback_secs(),
508            max_consecutive_selections: default_max_consecutive_selections(),
509        }
510    }
511}
512
513fn default_fairness_lookback_secs() -> u64 {
514    300 // 5 minutes
515}
516
517fn default_max_consecutive_selections() -> u32 {
518    3
519}
520
521/// Cache affinity settings for project-to-worker pinning.
522#[derive(Debug, Clone, Serialize, Deserialize)]
523pub struct AffinityConfig {
524    /// Enable affinity pinning (pin projects to last successful worker).
525    #[serde(default = "default_affinity_enabled")]
526    pub enabled: bool,
527    /// Time window in minutes to pin a project to its last successful worker.
528    /// After this window expires, normal selection resumes.
529    #[serde(default = "default_affinity_pin_minutes")]
530    pub pin_minutes: u64,
531    /// Enable last-success fallback when no workers match selection criteria.
532    /// If enabled, the daemon will attempt the last successful worker for a project
533    /// when all other workers are unavailable or fail selection.
534    #[serde(default = "default_last_success_fallback")]
535    pub enable_last_success_fallback: bool,
536    /// Minimum success rate for the last-success worker to be used as fallback.
537    /// Must be lower than min_success_rate to be meaningful as a fallback.
538    #[serde(default = "default_fallback_min_success_rate")]
539    pub fallback_min_success_rate: f64,
540}
541
542impl Default for AffinityConfig {
543    fn default() -> Self {
544        Self {
545            enabled: default_affinity_enabled(),
546            pin_minutes: default_affinity_pin_minutes(),
547            enable_last_success_fallback: default_last_success_fallback(),
548            fallback_min_success_rate: default_fallback_min_success_rate(),
549        }
550    }
551}
552
553fn default_affinity_enabled() -> bool {
554    true
555}
556
557fn default_affinity_pin_minutes() -> u64 {
558    60 // 1 hour default
559}
560
561fn default_last_success_fallback() -> bool {
562    true
563}
564
565fn default_fallback_min_success_rate() -> f64 {
566    0.5 // More lenient than normal min_success_rate (0.8)
567}
568
569/// Details about a selected worker for remote compilation.
570#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
571pub struct SelectedWorker {
572    /// Selected worker ID.
573    pub id: WorkerId,
574    /// Host address for SSH.
575    pub host: String,
576    /// SSH user.
577    pub user: String,
578    /// Path to SSH identity file.
579    pub identity_file: String,
580    /// Number of slots available on this worker after reservation.
581    pub slots_available: u32,
582    /// Worker's speed score (0-100).
583    pub speed_score: f64,
584    /// Operator-declared host OS (e.g. `windows`), if any. Propagated so the
585    /// hook can pick the right transport (Windows workers use `C:/rch` +
586    /// tar-over-ssh instead of the Unix rsync path). `None` for the historical
587    /// unlabelled workers. `#[serde(default)]` keeps old daemons/hooks
588    /// wire-compatible.
589    #[serde(default)]
590    pub declared_os: Option<String>,
591}
592
593/// Worker selection response from daemon to hook.
594#[derive(Debug, Clone, Serialize, Deserialize)]
595pub struct SelectionResponse {
596    /// Selected worker details, if available.
597    pub worker: Option<SelectedWorker>,
598    /// Reason for the selection result.
599    pub reason: SelectionReason,
600    /// Optional build ID assigned by the daemon (for active build tracking).
601    #[serde(default, skip_serializing_if = "Option::is_none")]
602    pub build_id: Option<u64>,
603    /// Optional per-worker selector diagnostics for machine-readable triage.
604    #[serde(default, skip_serializing_if = "Option::is_none")]
605    pub diagnostics: Option<SelectionDiagnostics>,
606}
607
608/// Final selector decision for a worker considered during routing.
609#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
610#[serde(rename_all = "snake_case")]
611pub enum WorkerSelectionDiagnosticDecision {
612    /// Worker passed hard gates and normal health threshold.
613    Allow,
614    /// Worker failed normal health threshold but passed fallback threshold.
615    FallbackCandidate,
616    /// Worker was denied by a hard gate or both health thresholds.
617    Deny,
618}
619
620/// Per-worker selector facts captured at worker-selection time.
621#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
622pub struct WorkerSelectionDiagnostic {
623    /// Worker ID.
624    pub worker_id: WorkerId,
625    /// Worker lifecycle/status label.
626    pub status: String,
627    /// Circuit breaker state label.
628    pub circuit_state: String,
629    /// Current normalized pressure state.
630    pub pressure_state: String,
631    /// Stable pressure reason code.
632    pub pressure_reason_code: String,
633    /// Rolling selector success rate, where available.
634    #[serde(skip_serializing_if = "Option::is_none")]
635    pub success_rate: Option<f64>,
636    /// Normal selection success-rate threshold.
637    pub min_success_rate: f64,
638    /// Last-success fallback success-rate threshold.
639    pub fallback_min_success_rate: f64,
640    /// Required runtime for this command.
641    pub required_runtime: RequiredRuntime,
642    /// Whether this worker has the required runtime.
643    pub runtime_available: bool,
644    /// Available slots at selection time.
645    pub available_slots: u32,
646    /// Total configured slots.
647    pub total_slots: u32,
648    /// Estimated slots requested by this command.
649    pub estimated_cores: u32,
650    /// Whether this worker was excluded because it already runs this project.
651    pub active_project_excluded: bool,
652    /// Final per-worker selector decision.
653    pub final_decision: WorkerSelectionDiagnosticDecision,
654    /// Human-readable final reason.
655    pub final_reason: String,
656    /// Stable machine-readable reasons collected for this worker.
657    pub reason_codes: Vec<String>,
658}
659
660/// Machine-readable worker selector diagnostics for a selection request.
661#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
662pub struct SelectionDiagnostics {
663    /// Required runtime for this command.
664    pub required_runtime: RequiredRuntime,
665    /// Estimated slots requested by this command.
666    pub estimated_cores: u32,
667    /// Normal success-rate threshold.
668    pub min_success_rate: f64,
669    /// Last-success fallback success-rate threshold.
670    pub fallback_min_success_rate: f64,
671    /// Count of workers excluded because they already run this project.
672    pub active_project_exclusion_count: usize,
673    /// One diagnostic entry per configured worker.
674    pub workers: Vec<WorkerSelectionDiagnostic>,
675}
676
677/// Request to release reserved worker slots.
678#[derive(Debug, Clone, Serialize, Deserialize)]
679pub struct ReleaseRequest {
680    /// ID of the worker to release slots on.
681    pub worker_id: WorkerId,
682    /// Number of slots to release.
683    pub slots: u32,
684    /// Optional build ID to mark complete.
685    #[serde(default, skip_serializing_if = "Option::is_none")]
686    pub build_id: Option<u64>,
687    /// Exact client wrapper identity associated with the admitted build.
688    #[serde(default, skip_serializing_if = "Option::is_none")]
689    pub local_wrapper_id: Option<String>,
690    /// Optional exit code for the build (used to finalize active build tracking).
691    #[serde(default, skip_serializing_if = "Option::is_none")]
692    pub exit_code: Option<i32>,
693    /// Optional total duration in milliseconds (pipeline or execution).
694    ///
695    /// If omitted, the daemon will compute duration from the active build's start time.
696    #[serde(default, skip_serializing_if = "Option::is_none")]
697    pub duration_ms: Option<u64>,
698    /// Optional bytes transferred during the pipeline (upload + artifact download).
699    #[serde(default, skip_serializing_if = "Option::is_none")]
700    pub bytes_transferred: Option<u64>,
701    /// Optional per-phase timing breakdown for the build pipeline.
702    #[serde(default, skip_serializing_if = "Option::is_none")]
703    pub timing: Option<CommandTimingBreakdown>,
704    /// The hook classified this non-zero exit as a worker fault (missing
705    /// toolchain or system library, CPU-capability signal, full disk), so the
706    /// daemon records no cache warmth for it (review of GH #81).
707    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
708    pub worker_fault: bool,
709    /// The failed remote command reported exhausted disk space or quota.
710    /// After matching the durable build owner, the daemon temporarily bypasses
711    /// this worker until fresh recovery probes establish disk headroom.
712    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
713    pub worker_disk_full: bool,
714    /// Actual remote source and output roots used by the failed attempt.
715    /// Recovery must measure these filesystems as well as configured roots.
716    #[serde(default, skip_serializing_if = "Vec::is_empty")]
717    pub worker_disk_roots: Vec<String>,
718}
719
720/// Build execution phase for daemon heartbeat tracking.
721#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
722#[serde(rename_all = "snake_case")]
723pub enum BuildHeartbeatPhase {
724    /// Uploading sources/dependencies to the worker.
725    SyncUp,
726    /// Running the remote compilation/test command.
727    Execute,
728    /// Downloading build artifacts from the worker.
729    SyncDown,
730    /// Build is completing or finalizing.
731    Finalize,
732}
733
734/// Request to update liveness/progress for an active build.
735#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
736pub struct BuildHeartbeatRequest {
737    /// Active build identifier assigned by the daemon.
738    pub build_id: u64,
739    /// Worker executing the build.
740    pub worker_id: WorkerId,
741    /// Hook process ID sending the heartbeat.
742    #[serde(default, skip_serializing_if = "Option::is_none")]
743    pub hook_pid: Option<u32>,
744    /// Client-minted wrapper identity for local durable-lease correlation.
745    #[serde(default, skip_serializing_if = "Option::is_none")]
746    pub local_wrapper_id: Option<String>,
747    /// Remote file containing the process-group leader PID for cancellation.
748    #[serde(default, skip_serializing_if = "Option::is_none")]
749    pub remote_pgid_file: Option<String>,
750    /// Current build execution phase.
751    pub phase: BuildHeartbeatPhase,
752    /// Optional human-readable progress detail.
753    #[serde(default, skip_serializing_if = "Option::is_none")]
754    pub detail: Option<String>,
755    /// Optional monotonic progress counter.
756    ///
757    /// The hook increments this when it observes meaningful progress
758    /// (phase transitions, new output, or richer progress signals).
759    #[serde(default, skip_serializing_if = "Option::is_none")]
760    pub progress_counter: Option<u64>,
761    /// Optional progress estimate in [0,100].
762    #[serde(default, skip_serializing_if = "Option::is_none")]
763    pub progress_percent: Option<f64>,
764}
765
766/// Configuration for a remote worker.
767#[derive(Debug, Clone, Serialize, Deserialize)]
768pub struct WorkerConfig {
769    /// Unique identifier for this worker.
770    pub id: WorkerId,
771    /// SSH hostname or IP address.
772    pub host: String,
773    /// SSH username.
774    pub user: String,
775    /// Path to SSH private key.
776    pub identity_file: String,
777    /// Total CPU slots available on this worker.
778    pub total_slots: u32,
779    /// Priority for worker selection (higher = preferred).
780    #[serde(default = "default_priority")]
781    pub priority: u32,
782    /// Optional tags for filtering.
783    ///
784    /// Mostly descriptive, with one reserved form: `os:<name>` (see
785    /// [`crate::declared_os`]) is a hard admission gate, written in
786    /// `workers.toml` as `os = "<name>"`.
787    #[serde(default)]
788    pub tags: Vec<String>,
789    /// Operator-declared named tool probes for this worker, written in
790    /// `workers.toml` as
791    /// `tools = [{ name = "clang", command = ["clang", "--version"] }]`.
792    ///
793    /// Unlike a tag, which is an unverified naming convention, each entry is
794    /// VERIFIED by running its fixed argv on the worker as the configured user;
795    /// only a zero exit makes the tool present. That is what lets
796    /// `--require-tool` gate selection on evidence instead of on a promise.
797    #[serde(default, skip_serializing_if = "Vec::is_empty")]
798    pub tools: Vec<WorkerToolProbe>,
799}
800
801/// One operator-declared tool probe as it appears in `workers.toml`.
802///
803/// Kept as plain configuration data; [`crate::capability_probe::NamedToolProbe`]
804/// is the validated form, and the conversion is where a malformed name is
805/// refused.
806#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
807pub struct WorkerToolProbe {
808    /// The name `--require-tool` and `jobs.required_tools` refer to.
809    pub name: String,
810    /// The exact argv to run. Zero exit = present; anything else = absent.
811    pub command: Vec<String>,
812}
813
814fn default_priority() -> u32 {
815    100
816}
817
818/// Tag prefix reserved for the operator-declared host OS.
819pub const OS_TAG_PREFIX: &str = "os:";
820
821/// The host OS an operator declared for a worker, from its tags.
822///
823/// Written in `workers.toml` as `os = "windows"` and normalized into the
824/// reserved `os:windows` tag on load, so it rides the existing tag vector
825/// through every path that already carries a [`WorkerConfig`].
826///
827/// Declaring it makes the worker **exclusive**: selection admits it only for
828/// commands that require that OS, so a Windows box cannot quietly absorb a
829/// `cargo check` dispatched from Linux and return wrong-platform artifacts.
830/// Workers without the tag — every historical worker — keep taking anything.
831#[must_use]
832pub fn declared_os(tags: &[String]) -> Option<String> {
833    // `find` over the mapped values, not `find_map` over the tags: a malformed
834    // `os:` entry must not mask a valid declaration later in the list, because
835    // failing open here is precisely the "Windows worker silently takes Linux
836    // jobs" outcome the gate exists to prevent.
837    tags.iter()
838        .filter_map(|tag| tag.strip_prefix(OS_TAG_PREFIX))
839        .map(|os| os.trim().to_ascii_lowercase())
840        .find(|os| !os.is_empty())
841}
842
843/// Build the reserved tag for a declared OS.
844#[must_use]
845pub fn os_tag(os: &str) -> String {
846    format!("{OS_TAG_PREFIX}{}", os.trim().to_ascii_lowercase())
847}
848
849impl Default for WorkerConfig {
850    fn default() -> Self {
851        Self {
852            id: WorkerId::new("default-worker"),
853            host: "localhost".to_string(),
854            user: "user".to_string(),
855            identity_file: "~/.ssh/id_rsa".to_string(),
856            total_slots: 4,
857            priority: default_priority(),
858            tags: Vec::new(),
859            tools: Vec::new(),
860        }
861    }
862}
863
864/// Runtime capabilities detected on a worker.
865///
866/// These are probed during health checks and cached for routing decisions.
867/// Commands requiring specific runtimes (e.g., `bun test`) can be routed
868/// only to workers with the corresponding capability.
869#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
870pub struct WorkerCapabilities {
871    /// Rust compiler version (from `rustc --version`).
872    #[serde(default, skip_serializing_if = "Option::is_none")]
873    pub rustc_version: Option<String>,
874    /// Installed rustup toolchains, including their host triple when rustup
875    /// reports one (for example `nightly-2026-07-05-x86_64-unknown-linux-gnu`).
876    #[serde(default, skip_serializing_if = "Vec::is_empty")]
877    pub rustup_toolchains: Vec<String>,
878    /// Installed rustup components as `<toolchain>:<component>` facts. Component
879    /// names are normalized by removing only the probed host-triple suffix, so
880    /// `clippy-preview` can never satisfy a `clippy` requirement.
881    #[serde(default, skip_serializing_if = "Vec::is_empty")]
882    pub rustup_components: Vec<String>,
883    /// Non-fatal diagnostics gathered while probing (for example a tool binary
884    /// that had to be resolved from a well-known fallback location because the
885    /// caller's PATH was minimal). Empty in the common case; clients may
886    /// surface these so "no facts" is distinguishable from "facts show
887    /// absence" (bd-deft5).
888    #[serde(default, skip_serializing_if = "Vec::is_empty")]
889    pub probe_warnings: Vec<String>,
890    /// Bun runtime version (from `bun --version`).
891    #[serde(default, skip_serializing_if = "Option::is_none")]
892    pub bun_version: Option<String>,
893    /// Node.js version (from `node --version`).
894    #[serde(default, skip_serializing_if = "Option::is_none")]
895    pub node_version: Option<String>,
896    /// npm version (from `npm --version`).
897    #[serde(default, skip_serializing_if = "Option::is_none")]
898    pub npm_version: Option<String>,
899    /// Nix version (from `nix --version`). Presence implies a usable nix binary
900    /// with an accompanying `/nix/store`, gating `nix build`/`nix develop -c`
901    /// routing to this worker.
902    #[serde(default, skip_serializing_if = "Option::is_none")]
903    pub nix_version: Option<String>,
904    /// Go toolchain version (from `go version`). Presence gates `go build`/
905    /// `go test`/`go vet` routing to this worker.
906    #[serde(default, skip_serializing_if = "Option::is_none")]
907    pub go_version: Option<String>,
908    /// Zig version (from `zig version`). `cargo zigbuild` shells out to `zig`
909    /// as the linker/C toolchain, so the bare `zig` binary is necessary but not
910    /// sufficient — see [`WorkerCapabilities::has_zig`].
911    #[serde(default, skip_serializing_if = "Option::is_none")]
912    pub zig_version: Option<String>,
913    /// cargo-zigbuild version (from `cargo-zigbuild --version`). Probed as the
914    /// hyphenated binary rather than `cargo zigbuild --version`, which the
915    /// subcommand's own parser rejects.
916    #[serde(default, skip_serializing_if = "Option::is_none")]
917    pub cargo_zigbuild_version: Option<String>,
918    /// x86-64 microarchitecture level (1..=4) parsed from `/proc/cpuinfo`
919    /// flags on the worker (bd-6qchz / bd-68hon item 4). `Some(2)` marks a
920    /// pre-AVX2 box (e.g. ovh-b's Ivy Bridge) whose build scripts SIGILL on
921    /// x86-64-v3 codegen; selection deprioritizes such workers proactively
922    /// while the reactive SIGILL quarantine remains the primary mechanism.
923    /// `None` on non-x86 workers or when the probe cannot read cpuinfo.
924    #[serde(default, skip_serializing_if = "Option::is_none")]
925    pub cpu_microarch_level: Option<u8>,
926    /// Operator-declared named tools whose probe exited ZERO on this worker.
927    ///
928    /// The list is evidence, not configuration: a name appears here only
929    /// because the worker ran the declared argv and it succeeded. Selection
930    /// gates `--require-tool` on this, which is the difference between a
931    /// verified capability and a tag somebody wrote by hand.
932    #[serde(default, skip_serializing_if = "Vec::is_empty")]
933    pub tools_present: Vec<String>,
934    /// Operator-declared named tools whose probe RAN and did not exit zero.
935    ///
936    /// Kept separate from "never declared" so an operator can tell a worker
937    /// that is missing a tool from a fleet that never probed for it — the two
938    /// have different fixes.
939    #[serde(default, skip_serializing_if = "Vec::is_empty")]
940    pub tools_absent: Vec<String>,
941
942    // Health metrics (bd-3eaa)
943    /// Number of CPU cores on the worker.
944    #[serde(default, skip_serializing_if = "Option::is_none")]
945    pub num_cpus: Option<u32>,
946    /// 1-minute load average.
947    #[serde(default, skip_serializing_if = "Option::is_none")]
948    pub load_avg_1: Option<f64>,
949    /// 5-minute load average.
950    #[serde(default, skip_serializing_if = "Option::is_none")]
951    pub load_avg_5: Option<f64>,
952    /// 15-minute load average.
953    #[serde(default, skip_serializing_if = "Option::is_none")]
954    pub load_avg_15: Option<f64>,
955    /// Free space in GB on the tightest mount a build touches: whichever of
956    /// the projects root, its alias and `/tmp` has the lowest free ratio.
957    /// Drives pressure classification (a full `/tmp` breaks scp/mktemp).
958    #[serde(default, skip_serializing_if = "Option::is_none")]
959    pub disk_free_gb: Option<f64>,
960    /// Total size in GB of the mount reported in `disk_free_gb`.
961    #[serde(default, skip_serializing_if = "Option::is_none")]
962    pub disk_total_gb: Option<f64>,
963    /// Free space in GB on the filesystem that holds build trees (the fuller
964    /// of the projects root and its alias; `/tmp` excluded). Sizes capacity:
965    /// slot derating, disk headroom and the free-space floor (GH #78).
966    /// `None` from an `rch-wkr` that predates the field.
967    #[serde(default, skip_serializing_if = "Option::is_none")]
968    pub build_disk_free_gb: Option<f64>,
969    /// Total size in GB of the mount reported in `build_disk_free_gb`.
970    #[serde(default, skip_serializing_if = "Option::is_none")]
971    pub build_disk_total_gb: Option<f64>,
972    /// Canonical path-topology preflight status (`/data/projects` + `/dp` alias).
973    ///
974    /// `Some(false)` indicates a hard preflight failure that should exclude this
975    /// worker from remote scheduling until a subsequent successful revalidation.
976    #[serde(default, skip_serializing_if = "Option::is_none")]
977    pub projects_root_ok: Option<bool>,
978    /// Machine-readable failure reason for path-topology preflight.
979    #[serde(default, skip_serializing_if = "Option::is_none")]
980    pub projects_root_issue: Option<String>,
981    /// Unix timestamp (ms) when topology preflight last executed.
982    #[serde(default, skip_serializing_if = "Option::is_none")]
983    pub projects_root_checked_at_unix_ms: Option<i64>,
984}
985
986impl WorkerCapabilities {
987    /// Create new empty capabilities.
988    pub fn new() -> Self {
989        Self::default()
990    }
991
992    /// Create mock capabilities with Rust installed.
993    ///
994    /// Used by mock transport to simulate a worker with Rust toolchain.
995    pub fn mock_with_rust() -> Self {
996        Self {
997            rustc_version: Some("1.85.0-nightly (mock)".to_string()),
998            projects_root_ok: Some(true),
999            ..Self::default()
1000        }
1001    }
1002
1003    /// Whether this is a known pre-x86-64-v3 (no AVX2) x86 worker — the class
1004    /// whose build-script/proc-macro binaries SIGILL on v3 codegen (bd-68hon).
1005    /// `None` (unknown/non-x86) is NOT pre-v3: only affirmative evidence
1006    /// deprioritizes a worker.
1007    pub fn is_pre_v3_x86(&self) -> bool {
1008        matches!(self.cpu_microarch_level, Some(1 | 2))
1009    }
1010
1011    /// Check if this worker has Bun installed.
1012    pub fn has_bun(&self) -> bool {
1013        self.bun_version.is_some()
1014    }
1015
1016    /// Check if this worker has Node.js installed.
1017    pub fn has_node(&self) -> bool {
1018        self.node_version.is_some()
1019    }
1020
1021    /// Check if this worker has Rust installed.
1022    pub fn has_rust(&self) -> bool {
1023        self.rustc_version.is_some()
1024    }
1025
1026    /// Check whether `component` is installed for `toolchain`.
1027    ///
1028    /// Rustup reports pinned toolchains with a host-triple suffix. The request
1029    /// carries the project pin without that suffix, so the owner half uses the
1030    /// same exact-or-`-`-boundary prefix rule as toolchain admission. The
1031    /// component half is exact.
1032    pub fn has_rustup_component(&self, toolchain: &str, component: &str) -> bool {
1033        let owner_prefix = format!("{toolchain}-");
1034        self.rustup_components.iter().any(|fact| {
1035            fact.split_once(':').is_some_and(|(owner, name)| {
1036                name == component && (owner == toolchain || owner.starts_with(&owner_prefix))
1037            })
1038        })
1039    }
1040
1041    /// Check if this worker has Nix installed (nix binary + `/nix/store`).
1042    pub fn has_nix(&self) -> bool {
1043        self.nix_version.is_some()
1044    }
1045
1046    /// Check if this worker has the Go toolchain installed.
1047    pub fn has_go(&self) -> bool {
1048        self.go_version.is_some()
1049    }
1050
1051    /// Check if this worker can run `cargo zigbuild` cross-compiles.
1052    ///
1053    /// Requires BOTH halves of the toolchain: the `cargo-zigbuild` cargo
1054    /// subcommand and the `zig` binary it drives as the linker. Either one
1055    /// alone fails at build time with an error the remote-result heuristics do
1056    /// not recognize as infrastructure (`error: no such command: 'zigbuild'` is
1057    /// not a rustup message), so the exit code would be surfaced verbatim
1058    /// instead of falling open to local. Gating both here keeps that failure
1059    /// out of the user's build.
1060    pub fn has_zig(&self) -> bool {
1061        self.zig_version.is_some() && self.cargo_zigbuild_version.is_some()
1062    }
1063
1064    /// Calculate load per core (1-minute load average / num_cpus).
1065    /// Returns None if metrics are unavailable.
1066    pub fn load_per_core(&self) -> Option<f64> {
1067        match (self.load_avg_1, self.num_cpus) {
1068            (Some(load), Some(cpus)) if cpus > 0 => Some(load / cpus as f64),
1069            _ => None,
1070        }
1071    }
1072
1073    /// Check if worker has high load (exceeds threshold).
1074    /// Returns None if metrics unavailable (fail-open).
1075    pub fn is_high_load(&self, max_load_per_core: f64) -> Option<bool> {
1076        self.load_per_core().map(|lpc| lpc > max_load_per_core)
1077    }
1078
1079    /// `(free_gb, total_gb)` of the filesystem that holds build trees.
1080    ///
1081    /// Capacity questions (how many builds fit, is the free-space floor met)
1082    /// are about that filesystem, not about a small tmpfs `/tmp` that happens
1083    /// to be the tightest mount. Falls back to the tightest-mount sample when
1084    /// the worker does not report a build-disk sample (older `rch-wkr`), which
1085    /// is the conservative pre-GH-#78 behaviour.
1086    pub fn build_disk_gb(&self) -> (Option<f64>, Option<f64>) {
1087        if self.build_disk_free_gb.is_some() {
1088            (self.build_disk_free_gb, self.build_disk_total_gb)
1089        } else {
1090            (self.disk_free_gb, self.disk_total_gb)
1091        }
1092    }
1093
1094    /// Check if the build disk is below the free-space threshold.
1095    /// Returns None if metrics unavailable (fail-open).
1096    pub fn is_low_disk(&self, min_free_gb: f64) -> Option<bool> {
1097        self.build_disk_gb().0.map(|free| free < min_free_gb)
1098    }
1099
1100    /// Check if canonical path-topology preflight is healthy.
1101    ///
1102    /// Returns `None` when probe data is unavailable (fail-open behavior).
1103    pub fn is_topology_healthy(&self) -> Option<bool> {
1104        self.projects_root_ok
1105    }
1106}
1107
1108/// Classify an x86-64 microarchitecture level (1..=4) from a `/proc/cpuinfo`
1109/// `flags` line (bd-6qchz).
1110///
1111/// Levels follow the psABI x86-64-v2/v3/v4 feature baselines, restricted to
1112/// flags as they appear in Linux cpuinfo. A CPU missing any flag of a level is
1113/// classified at the level below — e.g. ovh-b's Ivy Bridge E3-1245 V2 has AVX
1114/// but no `avx2`/`bmi2`/`fma`, so it classifies as 2 and its v3-codegen build
1115/// scripts SIGILL (the bd-68hon incident class).
1116#[must_use]
1117pub fn x86_64_microarch_level_from_flags(flags: &str) -> u8 {
1118    let set: std::collections::HashSet<&str> = flags.split_whitespace().collect();
1119    let has_all = |required: &[&str]| required.iter().all(|flag| set.contains(flag));
1120    // x86-64-v2: CMPXCHG16B, LAHF/SAHF, POPCNT, SSE3/SSSE3/SSE4.x.
1121    const V2: &[&str] = &["cx16", "lahf_lm", "popcnt", "sse4_1", "sse4_2", "ssse3"];
1122    // x86-64-v3: AVX/AVX2, BMI1/2, F16C, FMA, MOVBE, XSAVE (LZCNT is `abm`).
1123    const V3: &[&str] = &[
1124        "abm", "avx", "avx2", "bmi1", "bmi2", "f16c", "fma", "movbe", "xsave",
1125    ];
1126    // x86-64-v4: the AVX-512 F/BW/CD/DQ/VL baseline.
1127    const V4: &[&str] = &["avx512f", "avx512bw", "avx512cd", "avx512dq", "avx512vl"];
1128    if !has_all(V2) {
1129        return 1;
1130    }
1131    if !has_all(V3) {
1132        return 2;
1133    }
1134    if !has_all(V4) {
1135        return 3;
1136    }
1137    4
1138}
1139
1140/// Path topology configuration.
1141///
1142/// Overrides the default canonical and alias project roots used by
1143/// [`crate::path_topology::PathTopologyPolicy`].  This is useful on platforms
1144/// such as macOS where SIP prevents using `/data/projects` and the user must
1145/// choose a different root (e.g. `~/Projects`).
1146///
1147/// ```toml
1148/// [path_topology]
1149/// canonical_root = "/Users/me/Projects"
1150/// alias_root = "/Users/me/p"
1151/// ```
1152#[derive(Debug, Clone, Default, Serialize, Deserialize)]
1153pub struct PathTopologyConfig {
1154    /// Canonical project root directory.
1155    /// Defaults to `/data/projects` when absent.
1156    #[serde(default)]
1157    pub canonical_root: Option<String>,
1158
1159    /// Alias (symlink) root that points at the canonical root.
1160    /// Defaults to `/dp` when absent.
1161    #[serde(default)]
1162    pub alias_root: Option<String>,
1163}
1164
1165impl PathTopologyConfig {
1166    /// Build a [`crate::path_topology::PathTopologyPolicy`] from this config,
1167    /// falling back to the compiled-in defaults for any field left unset.
1168    ///
1169    /// Empty strings are treated as unset (fall back to defaults).
1170    /// Tilde prefixes (`~/...`) are expanded to the user's home directory.
1171    pub fn to_policy(&self) -> crate::path_topology::PathTopologyPolicy {
1172        let canonical = self
1173            .canonical_root
1174            .as_deref()
1175            .filter(|s| !s.is_empty())
1176            .map(|s| shellexpand::tilde(s).into_owned())
1177            .unwrap_or_else(|| crate::path_topology::DEFAULT_CANONICAL_PROJECT_ROOT.to_string());
1178        let alias = self
1179            .alias_root
1180            .as_deref()
1181            .filter(|s| !s.is_empty())
1182            .map(|s| shellexpand::tilde(s).into_owned())
1183            .unwrap_or_else(|| crate::path_topology::DEFAULT_ALIAS_PROJECT_ROOT.to_string());
1184        crate::path_topology::PathTopologyPolicy::new(
1185            PathBuf::from(canonical),
1186            PathBuf::from(alias),
1187        )
1188    }
1189}
1190
1191/// RCH configuration.
1192#[derive(Debug, Clone, Default, Serialize, Deserialize)]
1193pub struct RchConfig {
1194    #[serde(default)]
1195    pub general: GeneralConfig,
1196    #[serde(default)]
1197    pub compilation: CompilationConfig,
1198    #[serde(default)]
1199    pub transfer: TransferConfig,
1200    #[serde(default)]
1201    pub environment: EnvironmentConfig,
1202    #[serde(default)]
1203    pub circuit: CircuitBreakerConfig,
1204    #[serde(default)]
1205    pub output: OutputConfig,
1206    #[serde(default)]
1207    pub self_healing: SelfHealingConfig,
1208    #[serde(default)]
1209    pub self_test: SelfTestConfig,
1210    /// Worker selection algorithm configuration.
1211    #[serde(default)]
1212    pub selection: SelectionConfig,
1213    /// Execution allowlist configuration (bd-785w).
1214    #[serde(default)]
1215    pub execution: ExecutionConfig,
1216    /// Worker health alerting configuration (daemon).
1217    #[serde(default)]
1218    pub alerts: AlertsConfig,
1219    /// Fleet operation configuration (bd-rs7w.2).
1220    #[serde(default)]
1221    pub fleet: FleetConfig,
1222    /// Path topology overrides for project root directories.
1223    #[serde(default)]
1224    pub path_topology: PathTopologyConfig,
1225    /// Doctor / reliability subsystem configuration (verdict webhooks).
1226    #[serde(default)]
1227    pub doctor: DoctorConfig,
1228    /// Session-history remediation knobs: the central schema and default policy
1229    /// for temporary bypass, auto-rejoin, reconciliation, proof, incident
1230    /// ledger, build-root, pooled targets, telemetry freshness, log retention,
1231    /// disk pressure, and smoke defaults (bd-...remediation-ocv9i.17.1).
1232    #[serde(default)]
1233    pub remediation: crate::remediation_config::RemediationConfig,
1234    /// Layer 0 configuration pack knobs (bd-bqu38 / M-1 §149). Defaults OFF;
1235    /// injected as CARGO_PROFILE_* env vars into remote cargo builds.
1236    #[serde(default)]
1237    pub layer0: Layer0Config,
1238    /// Daemon status API over TCP for other machines on the tailnet
1239    /// (`[api]`, bd-2f5ms). Off unless `bind` is set.
1240    #[serde(default)]
1241    pub api: ApiConfig,
1242    /// Where the fleet dashboard lives (`[dashboard]`), for `rch web`.
1243    #[serde(default)]
1244    pub dashboard: DashboardConfig,
1245    /// Job-mode defaults (`[jobs]`), for `rch exec --job`.
1246    #[serde(default)]
1247    pub jobs: JobsConfig,
1248}
1249
1250/// Job-mode configuration (`[jobs]`).
1251#[derive(Debug, Clone, Default, Serialize, Deserialize)]
1252pub struct JobsConfig {
1253    /// Named tools every job from this project requires a worker to have
1254    /// VERIFIED, as though each were passed with `--require-tool`.
1255    ///
1256    /// This is the project saying "my jobs cannot run on a worker without
1257    /// these", so it applies on top of any `--require-tool` given on the
1258    /// command line rather than being replaced by it. It affects job mode
1259    /// ONLY: ordinary offloaded compilation keeps its existing admission
1260    /// behavior, because a project-wide requirement that silently narrowed
1261    /// every build's worker pool would be a surprising way to lose the fleet.
1262    #[serde(default, skip_serializing_if = "Vec::is_empty")]
1263    pub required_tools: Vec<String>,
1264}
1265
1266/// Daemon status API over TCP (`[api]`).
1267///
1268/// rchd's rich state (`/status`, `/workers/capabilities`) lives on a `0600`
1269/// Unix socket, which only the local user can read. This section opens the
1270/// same JSON on a TCP listener so agents and the fleet dashboard on OTHER
1271/// machines can ask a dispatcher "why are you local-only right now?" without
1272/// ssh. It is meant for a tailnet: the bind address must be loopback or a
1273/// Tailscale address (100.64.0.0/10, fd7a:115c:a1e0::/48) unless
1274/// `allow_any_addr` is set, and every status route requires the bearer token
1275/// unless `token` is unset AND `no_token` is explicitly true.
1276///
1277/// ```toml
1278/// [api]
1279/// bind = "tailscale"       # or "tailscale:9101", "100.68.51.94:9101", "127.0.0.1:9101"
1280/// token = "…"              # or token_file = "~/.config/rch/api.token"
1281/// ```
1282#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
1283#[serde(default)]
1284pub struct ApiConfig {
1285    /// Where to listen. `"tailscale"` resolves this machine's Tailscale IPv4
1286    /// at startup (default port 9101); `"tailscale:PORT"`, `"IP:PORT"` or
1287    /// `"[v6]:PORT"` are taken literally. Unset (empty) = API off.
1288    pub bind: String,
1289    /// Bearer token required on `/status` and `/workers/capabilities`.
1290    pub token: Option<String>,
1291    /// File holding the bearer token (trimmed). Takes precedence over `token`.
1292    pub token_file: Option<String>,
1293    /// Serve the status routes with NO token. Only honoured when no token is
1294    /// configured; must be set explicitly so an empty token is never an
1295    /// accidental open door.
1296    pub no_token: bool,
1297    /// Permit a bind address outside loopback and the Tailscale ranges.
1298    pub allow_any_addr: bool,
1299}
1300
1301/// Where the fleet dashboard is served (`[dashboard]`).
1302#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
1303#[serde(default)]
1304pub struct DashboardConfig {
1305    /// URL `rch web` opens, e.g. `https://rch-fleet.vercel.app`.
1306    pub url: Option<String>,
1307}
1308
1309/// Doctor reliability subsystem configuration.
1310///
1311/// Currently carries the verdict-transition webhook surface. Kept as its own
1312/// section (`[doctor]`) so future doctor knobs land here rather than bloating
1313/// the top-level config.
1314#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
1315pub struct DoctorConfig {
1316    /// Verdict-transition webhook destinations.
1317    #[serde(default)]
1318    pub webhooks: DoctorWebhooksConfig,
1319}
1320
1321/// Webhook notification surface for reliability verdict transitions.
1322///
1323/// Fires when `rch doctor --reliability --watch` observes a verdict change
1324/// (`Healthy → Degraded`, `Degraded → Failing`, etc.). Each endpoint selects
1325/// which transitions it cares about and how the payload is formatted.
1326#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
1327pub struct DoctorWebhooksConfig {
1328    /// Named webhook destinations. Empty = no webhook dispatch.
1329    #[serde(default)]
1330    pub endpoints: Vec<DoctorWebhookEndpoint>,
1331    /// Maximum number of in-flight webhook deliveries. Bounds memory and
1332    /// outbound connection pressure: when the queue is full, a transition's
1333    /// delivery is dropped (and journaled) rather than growing unboundedly.
1334    /// Per-process, shared across all endpoints.
1335    #[serde(default = "default_webhook_max_inflight")]
1336    pub max_inflight: usize,
1337}
1338
1339// Manual `Default` so the in-code default matches the serde default
1340// (`#[derive(Default)]` would give `max_inflight = 0`, diverging from a
1341// from-TOML load that fills in 16).
1342impl Default for DoctorWebhooksConfig {
1343    fn default() -> Self {
1344        Self {
1345            endpoints: Vec::new(),
1346            max_inflight: default_webhook_max_inflight(),
1347        }
1348    }
1349}
1350
1351/// A single webhook destination for verdict transitions.
1352///
1353/// SECRETS ARE NEVER STORED HERE. Routing keys, bearer tokens, and signing
1354/// secrets are referenced by the *name* of an environment variable
1355/// (`*_env` fields) read at dispatch time. There is deliberately no
1356/// `secret` / `routing_key` / `bearer_token` / `password` field on this
1357/// struct (enforced by `test_no_inline_secret_fields`).
1358#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
1359pub struct DoctorWebhookEndpoint {
1360    /// Operator-facing name (appears in logs and the audit trail).
1361    pub name: String,
1362    /// Destination URL.
1363    pub url: String,
1364    /// Payload format.
1365    #[serde(default)]
1366    pub format: DoctorWebhookFormat,
1367    /// Transition predicates that arm this endpoint. Empty = never fire.
1368    /// Supported: `healthy_to_degraded`, `degraded_to_failing`,
1369    /// `any_to_failing`, `any_to_degraded`, `failing_to_healthy`,
1370    /// `degraded_to_healthy`, `any_to_healthy`, `any_to_any`.
1371    #[serde(default)]
1372    pub on_transitions: Vec<String>,
1373    /// Maximum retry attempts after the first try fails.
1374    #[serde(default = "default_webhook_retry_max")]
1375    pub retry_max: u32,
1376    /// Base backoff between retries (milliseconds); doubles each attempt.
1377    #[serde(default = "default_webhook_retry_backoff_ms")]
1378    pub retry_backoff_ms: u64,
1379    /// Per-attempt request timeout (milliseconds).
1380    #[serde(default = "default_webhook_timeout_ms")]
1381    pub timeout_ms: u64,
1382    /// Per-endpoint kill switch.
1383    #[serde(default = "default_true")]
1384    pub enabled: bool,
1385    /// Name of the env var holding the PagerDuty Events API routing key.
1386    /// Required for `format = "pagerduty"`. Read at runtime, never stored.
1387    #[serde(default)]
1388    pub routing_key_env: Option<String>,
1389    /// Name of the env var holding a bearer token sent as
1390    /// `Authorization: Bearer <token>`. Read at runtime, never stored.
1391    #[serde(default)]
1392    pub bearer_token_env: Option<String>,
1393    /// Name of the env var holding an HMAC-SHA256 signing secret. When set,
1394    /// the body is signed and the signature sent as `X-RCH-Signature:
1395    /// sha256=<hex>`. Read at runtime, never stored.
1396    #[serde(default)]
1397    pub signing_secret_env: Option<String>,
1398}
1399
1400/// Built-in webhook payload formats.
1401#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
1402#[serde(rename_all = "snake_case")]
1403pub enum DoctorWebhookFormat {
1404    /// Opinionated RCH JSON schema (default).
1405    #[default]
1406    GenericJson,
1407    /// Slack Block Kit payload.
1408    Slack,
1409    /// PagerDuty Events API v2 payload.
1410    Pagerduty,
1411}
1412
1413fn default_webhook_max_inflight() -> usize {
1414    16
1415}
1416
1417fn default_webhook_retry_max() -> u32 {
1418    3
1419}
1420
1421fn default_webhook_retry_backoff_ms() -> u64 {
1422    200
1423}
1424
1425fn default_webhook_timeout_ms() -> u64 {
1426    5000
1427}
1428
1429/// Worker health alerting configuration.
1430#[derive(Debug, Clone, Serialize, Deserialize)]
1431pub struct AlertsConfig {
1432    /// Enable or disable alert generation in the daemon.
1433    #[serde(default = "default_true")]
1434    pub enabled: bool,
1435    /// Suppress duplicate alerts for this many seconds.
1436    #[serde(default = "default_alert_suppress_duplicates_secs")]
1437    pub suppress_duplicates_secs: u64,
1438    /// How long to keep a cleared alert visible (with state
1439    /// `cleared_pending_clean`) before removing it entirely. Prevents
1440    /// transient, self-healing circuit-breaker warnings from lingering on
1441    /// `rch status` long after the fleet returned to healthy (bd-3ogaz).
1442    #[serde(default = "default_alert_cleared_retention_secs")]
1443    pub cleared_retention_secs: u64,
1444    /// Webhook configuration for external notifications.
1445    #[serde(default)]
1446    pub webhook: Option<WebhookConfig>,
1447}
1448
1449/// Webhook configuration for alert dispatch.
1450#[derive(Debug, Clone, Default, Serialize, Deserialize)]
1451pub struct WebhookConfig {
1452    /// Webhook endpoint URL (e.g., Slack/Discord webhook URL).
1453    pub url: Option<String>,
1454    /// Secret for HMAC-SHA256 signing (optional).
1455    #[serde(default)]
1456    pub secret: Option<String>,
1457    /// Timeout in seconds for webhook requests.
1458    #[serde(default = "default_webhook_timeout_secs")]
1459    pub timeout_secs: u64,
1460    /// Number of retries on failure.
1461    #[serde(default = "default_webhook_retry_count")]
1462    pub retry_count: u32,
1463    /// Events to send (empty = all events).
1464    /// Supported: worker_offline, worker_degraded, circuit_open, all_workers_offline
1465    #[serde(default)]
1466    pub events: Vec<String>,
1467}
1468
1469fn default_webhook_timeout_secs() -> u64 {
1470    5
1471}
1472
1473fn default_webhook_retry_count() -> u32 {
1474    3
1475}
1476
1477fn default_alert_suppress_duplicates_secs() -> u64 {
1478    300
1479}
1480
1481fn default_alert_cleared_retention_secs() -> u64 {
1482    300
1483}
1484
1485impl Default for AlertsConfig {
1486    fn default() -> Self {
1487        Self {
1488            enabled: true,
1489            suppress_duplicates_secs: default_alert_suppress_duplicates_secs(),
1490            cleared_retention_secs: default_alert_cleared_retention_secs(),
1491            webhook: None,
1492        }
1493    }
1494}
1495
1496/// Fleet operation configuration.
1497///
1498/// Controls timeouts, thresholds, and behavior for fleet management commands
1499/// like `rch fleet preflight`, `rch fleet status`, and `rch fleet deploy`.
1500#[derive(Debug, Clone, Serialize, Deserialize)]
1501pub struct FleetConfig {
1502    /// SSH connection timeout in seconds.
1503    #[serde(default = "default_fleet_ssh_connect_timeout_secs")]
1504    pub ssh_connect_timeout_secs: u64,
1505    /// SSH command execution timeout in seconds.
1506    #[serde(default = "default_fleet_ssh_command_timeout_secs")]
1507    pub ssh_command_timeout_secs: u64,
1508    /// Minimum required disk space in MB for worker health.
1509    #[serde(default = "default_fleet_min_disk_space_mb")]
1510    pub min_disk_space_mb: u64,
1511    /// Maximum acceptable load average for worker health.
1512    #[serde(default = "default_fleet_max_load_average")]
1513    pub max_load_average: f64,
1514    /// Maximum number of concurrent SSH connections.
1515    #[serde(default = "default_fleet_max_concurrent_workers")]
1516    pub max_concurrent_workers: usize,
1517    /// Number of retry attempts for transient failures.
1518    #[serde(default = "default_fleet_retry_count")]
1519    pub retry_count: u32,
1520    /// Delay between retries in milliseconds.
1521    #[serde(default = "default_fleet_retry_delay_ms")]
1522    pub retry_delay_ms: u64,
1523}
1524
1525fn default_fleet_ssh_connect_timeout_secs() -> u64 {
1526    10
1527}
1528
1529fn default_fleet_ssh_command_timeout_secs() -> u64 {
1530    30
1531}
1532
1533fn default_fleet_min_disk_space_mb() -> u64 {
1534    500
1535}
1536
1537fn default_fleet_max_load_average() -> f64 {
1538    10.0
1539}
1540
1541fn default_fleet_max_concurrent_workers() -> usize {
1542    10
1543}
1544
1545fn default_fleet_retry_count() -> u32 {
1546    2
1547}
1548
1549fn default_fleet_retry_delay_ms() -> u64 {
1550    1000
1551}
1552
1553impl Default for FleetConfig {
1554    fn default() -> Self {
1555        Self {
1556            ssh_connect_timeout_secs: default_fleet_ssh_connect_timeout_secs(),
1557            ssh_command_timeout_secs: default_fleet_ssh_command_timeout_secs(),
1558            min_disk_space_mb: default_fleet_min_disk_space_mb(),
1559            max_load_average: default_fleet_max_load_average(),
1560            max_concurrent_workers: default_fleet_max_concurrent_workers(),
1561            retry_count: default_fleet_retry_count(),
1562            retry_delay_ms: default_fleet_retry_delay_ms(),
1563        }
1564    }
1565}
1566
1567impl FleetConfig {
1568    /// Get SSH connect timeout as Duration.
1569    pub fn ssh_connect_timeout(&self) -> std::time::Duration {
1570        std::time::Duration::from_secs(self.ssh_connect_timeout_secs)
1571    }
1572
1573    /// Get SSH command timeout as Duration.
1574    pub fn ssh_command_timeout(&self) -> std::time::Duration {
1575        std::time::Duration::from_secs(self.ssh_command_timeout_secs)
1576    }
1577
1578    /// Get retry delay as Duration.
1579    pub fn retry_delay(&self) -> std::time::Duration {
1580        std::time::Duration::from_millis(self.retry_delay_ms)
1581    }
1582}
1583
1584/// Box-level role (bd-wywsj): what this MACHINE is for, so "never
1585/// build locally" is configuration rather than a per-call env var.
1586#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
1587#[serde(rename_all = "lowercase")]
1588pub enum BoxRole {
1589    /// Orchestrator box: offloadable builds default FAIL-CLOSED +
1590    /// queue (RCH_REQUIRE_REMOTE semantics) — a saturated fleet waits
1591    /// for a worker instead of repopulating local rustc. Explicit
1592    /// per-call env still overrides.
1593    Dispatcher,
1594    /// The box IS the compute: fail-open/local; `rch shim install`
1595    /// refuses here.
1596    Worker,
1597    /// Today's behavior (fully backward compatible).
1598    #[default]
1599    Hybrid,
1600}
1601
1602impl BoxRole {
1603    /// Stable lowercase token.
1604    #[must_use]
1605    pub fn as_str(self) -> &'static str {
1606        match self {
1607            BoxRole::Dispatcher => "dispatcher",
1608            BoxRole::Worker => "worker",
1609            BoxRole::Hybrid => "hybrid",
1610        }
1611    }
1612}
1613
1614#[derive(Debug, Clone, Serialize, Deserialize)]
1615pub struct GeneralConfig {
1616    /// Whether RCH is enabled.
1617    #[serde(default = "default_true")]
1618    pub enabled: bool,
1619    /// Box-level role (bd-wywsj): dispatcher boxes default fail-closed
1620    /// + queue for offloadable builds; worker boxes are the compute.
1621    #[serde(default)]
1622    pub role: BoxRole,
1623    /// Force local execution for this project (never offload), even if the command is classified.
1624    ///
1625    /// Intended for sensitive projects or cases where local state must be authoritative.
1626    #[serde(default)]
1627    pub force_local: bool,
1628    /// Force remote execution for this project (always attempt offload) when safe.
1629    ///
1630    /// This bypasses heuristic gating (e.g. confidence threshold overrides) but still respects
1631    /// structural safety checks and NEVER_INTERCEPT patterns.
1632    #[serde(default)]
1633    pub force_remote: bool,
1634    /// Log level (trace, debug, info, warn, error).
1635    #[serde(default = "default_log_level")]
1636    pub log_level: String,
1637    /// Path to Unix socket for daemon communication.
1638    #[serde(default = "default_socket_path")]
1639    pub socket_path: String,
1640}
1641
1642/// Environment variable passthrough configuration.
1643#[derive(Debug, Clone, Default, Serialize, Deserialize)]
1644pub struct EnvironmentConfig {
1645    /// Allowlist of environment variables to forward to remote workers.
1646    #[serde(default)]
1647    pub allowlist: Vec<String>,
1648}
1649
1650impl Default for GeneralConfig {
1651    fn default() -> Self {
1652        Self {
1653            enabled: true,
1654            role: BoxRole::default(),
1655            force_local: false,
1656            force_remote: false,
1657            log_level: "info".to_string(),
1658            socket_path: default_socket_path(),
1659        }
1660    }
1661}
1662
1663/// Visibility level for hook output.
1664#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, JsonSchema)]
1665#[serde(rename_all = "lowercase")]
1666pub enum OutputVisibility {
1667    #[default]
1668    None,
1669    Summary,
1670    Verbose,
1671}
1672
1673impl std::fmt::Display for OutputVisibility {
1674    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1675        let value = match self {
1676            OutputVisibility::None => "none",
1677            OutputVisibility::Summary => "summary",
1678            OutputVisibility::Verbose => "verbose",
1679        };
1680        write!(f, "{}", value)
1681    }
1682}
1683
1684impl std::str::FromStr for OutputVisibility {
1685    type Err = ();
1686
1687    fn from_str(s: &str) -> Result<Self, Self::Err> {
1688        match s.trim().to_lowercase().as_str() {
1689            "none" | "silent" | "quiet" => Ok(Self::None),
1690            "summary" | "short" => Ok(Self::Summary),
1691            "verbose" | "debug" => Ok(Self::Verbose),
1692            _ => Err(()),
1693        }
1694    }
1695}
1696
1697/// Color mode for remote command output.
1698///
1699/// Controls whether ANSI color codes are preserved when streaming
1700/// output from remote compilation commands.
1701#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
1702#[serde(rename_all = "lowercase")]
1703pub enum ColorMode {
1704    /// Force color output regardless of terminal detection.
1705    /// Sets CARGO_TERM_COLOR=always and similar environment variables.
1706    #[default]
1707    Always,
1708    /// Let the remote command detect terminal capabilities (may lose colors).
1709    Auto,
1710    /// Disable color output entirely.
1711    Never,
1712}
1713
1714impl std::fmt::Display for ColorMode {
1715    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1716        let value = match self {
1717            ColorMode::Always => "always",
1718            ColorMode::Auto => "auto",
1719            ColorMode::Never => "never",
1720        };
1721        write!(f, "{}", value)
1722    }
1723}
1724
1725impl std::str::FromStr for ColorMode {
1726    type Err = ();
1727
1728    fn from_str(s: &str) -> Result<Self, Self::Err> {
1729        match s.trim().to_lowercase().as_str() {
1730            "always" | "force" | "yes" | "true" => Ok(Self::Always),
1731            "auto" | "detect" => Ok(Self::Auto),
1732            "never" | "none" | "no" | "false" => Ok(Self::Never),
1733            _ => Err(()),
1734        }
1735    }
1736}
1737
1738fn default_color_mode() -> ColorMode {
1739    ColorMode::default()
1740}
1741
1742/// Hook output configuration.
1743#[derive(Debug, Clone, Serialize, Deserialize)]
1744pub struct OutputConfig {
1745    /// Output visibility level for hook messages.
1746    #[serde(default = "default_output_visibility")]
1747    pub visibility: OutputVisibility,
1748    /// Whether the first-run success message has been shown.
1749    #[serde(default)]
1750    pub first_run_complete: bool,
1751    /// Color mode for remote command output.
1752    /// Controls whether ANSI color codes are preserved in remote output.
1753    #[serde(default = "default_color_mode")]
1754    pub color_mode: ColorMode,
1755}
1756
1757impl Default for OutputConfig {
1758    fn default() -> Self {
1759        Self {
1760            visibility: OutputVisibility::None,
1761            first_run_complete: false,
1762            color_mode: ColorMode::default(),
1763        }
1764    }
1765}
1766
1767// ============================================================================
1768// Self-Healing Configuration
1769// ============================================================================
1770
1771fn default_autostart_cooldown_secs() -> u64 {
1772    30
1773}
1774
1775fn default_autostart_timeout_secs() -> u64 {
1776    3
1777}
1778
1779/// Log level for self-healing diagnostic output.
1780///
1781/// Used to filter `tracing` events emitted by the self-healing subsystem
1782/// (hook auto-start, daemon hook-install). Read from
1783/// `RCH_SELF_HEALING_LOG_LEVEL=debug|info|warn|error` (case-insensitive).
1784#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
1785#[serde(rename_all = "lowercase")]
1786pub enum SelfHealingLogLevel {
1787    Debug,
1788    #[default]
1789    Info,
1790    Warn,
1791    Error,
1792}
1793
1794impl SelfHealingLogLevel {
1795    /// Parse a human-friendly level name. Returns `None` for unknown input.
1796    pub fn from_env_str(s: &str) -> Option<Self> {
1797        match s.trim().to_ascii_lowercase().as_str() {
1798            "debug" => Some(Self::Debug),
1799            "info" => Some(Self::Info),
1800            "warn" | "warning" => Some(Self::Warn),
1801            "error" => Some(Self::Error),
1802            _ => None,
1803        }
1804    }
1805}
1806
1807/// Self-healing behaviors for the hook and daemon.
1808#[derive(Debug, Clone, Serialize, Deserialize)]
1809pub struct SelfHealingConfig {
1810    /// Whether the hook should attempt to auto-start the daemon.
1811    #[serde(default = "default_true")]
1812    pub hook_starts_daemon: bool,
1813    /// Whether the daemon should auto-install Claude Code hooks on startup.
1814    #[serde(default = "default_true")]
1815    pub daemon_installs_hooks: bool,
1816    /// Cooldown between auto-start attempts in seconds.
1817    #[serde(default = "default_autostart_cooldown_secs")]
1818    pub auto_start_cooldown_secs: u64,
1819    /// Time to wait for the daemon socket after spawning in seconds.
1820    /// Also aliased as `daemon_start_timeout` for backwards compatibility.
1821    #[serde(
1822        default = "default_autostart_timeout_secs",
1823        alias = "daemon_start_timeout"
1824    )]
1825    pub auto_start_timeout_secs: u64,
1826    /// Log level for self-healing diagnostic events. Default: info.
1827    /// Override via `RCH_SELF_HEALING_LOG_LEVEL=debug|info|warn|error`.
1828    #[serde(default)]
1829    pub self_healing_log_level: SelfHealingLogLevel,
1830}
1831
1832impl Default for SelfHealingConfig {
1833    fn default() -> Self {
1834        Self {
1835            hook_starts_daemon: default_true(),
1836            daemon_installs_hooks: default_true(),
1837            auto_start_cooldown_secs: default_autostart_cooldown_secs(),
1838            auto_start_timeout_secs: default_autostart_timeout_secs(),
1839            self_healing_log_level: SelfHealingLogLevel::default(),
1840        }
1841    }
1842}
1843
1844impl SelfHealingConfig {
1845    /// Apply environment variable overrides to this config.
1846    ///
1847    /// Supported environment variables:
1848    /// - `RCH_NO_SELF_HEALING=1` - Master switch to disable all self-healing
1849    /// - `RCH_HOOK_STARTS_DAEMON=0|1` - Control hook auto-starting daemon
1850    /// - `RCH_DAEMON_INSTALLS_HOOKS=0|1` - Control daemon auto-installing hooks
1851    /// - `RCH_AUTO_START_TIMEOUT_SECS=<seconds>` - Max wait for daemon start
1852    /// - `RCH_AUTO_START_COOLDOWN_SECS=<seconds>` - Min time between auto-starts
1853    pub fn with_env_overrides(mut self) -> Self {
1854        // Master disable switch
1855        if let Ok(val) = std::env::var("RCH_NO_SELF_HEALING")
1856            && (val == "1" || val.eq_ignore_ascii_case("true"))
1857        {
1858            self.hook_starts_daemon = false;
1859            self.daemon_installs_hooks = false;
1860            return self;
1861        }
1862
1863        // Individual toggles
1864        if let Ok(val) = std::env::var("RCH_HOOK_STARTS_DAEMON") {
1865            self.hook_starts_daemon = val != "0" && !val.eq_ignore_ascii_case("false");
1866        }
1867        if let Ok(val) = std::env::var("RCH_DAEMON_INSTALLS_HOOKS") {
1868            self.daemon_installs_hooks = val != "0" && !val.eq_ignore_ascii_case("false");
1869        }
1870
1871        // Numeric settings
1872        if let Ok(val) = std::env::var("RCH_AUTO_START_TIMEOUT_SECS")
1873            && let Ok(secs) = val.parse()
1874        {
1875            self.auto_start_timeout_secs = secs;
1876        }
1877        if let Ok(val) = std::env::var("RCH_AUTO_START_COOLDOWN_SECS")
1878            && let Ok(secs) = val.parse()
1879        {
1880            self.auto_start_cooldown_secs = secs;
1881        }
1882        // br-4zf3p: log-level override.
1883        if let Ok(val) = std::env::var("RCH_SELF_HEALING_LOG_LEVEL") {
1884            if let Some(level) = SelfHealingLogLevel::from_env_str(&val) {
1885                self.self_healing_log_level = level;
1886            } else {
1887                tracing::warn!(
1888                    target: "rch::self_healing",
1889                    invalid = %val,
1890                    "RCH_SELF_HEALING_LOG_LEVEL has invalid value; using default=info"
1891                );
1892            }
1893        }
1894
1895        self
1896    }
1897}
1898
1899// ============================================================================
1900// Self-Test Configuration
1901// ============================================================================
1902
1903/// Action to take when a scheduled self-test fails.
1904#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
1905#[serde(rename_all = "snake_case")]
1906pub enum SelfTestFailureAction {
1907    /// Only emit an alert/log entry.
1908    #[default]
1909    Alert,
1910    /// Disable the failing worker automatically.
1911    DisableWorker,
1912    /// Alert and disable the worker.
1913    AlertAndDisable,
1914}
1915
1916/// Which workers to include in self-test runs.
1917#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1918#[serde(untagged)]
1919pub enum SelfTestWorkers {
1920    /// Special value "all" (or a single worker id string).
1921    All(String),
1922    /// Explicit worker list.
1923    List(Vec<WorkerId>),
1924}
1925
1926impl Default for SelfTestWorkers {
1927    fn default() -> Self {
1928        Self::All("all".to_string())
1929    }
1930}
1931
1932impl SelfTestWorkers {
1933    /// Resolve to an explicit list if configured, or None to indicate "all".
1934    pub fn resolve(&self) -> Option<Vec<WorkerId>> {
1935        match self {
1936            SelfTestWorkers::All(value) => {
1937                if value.eq_ignore_ascii_case("all") {
1938                    None
1939                } else {
1940                    Some(vec![WorkerId::new(value.clone())])
1941                }
1942            }
1943            SelfTestWorkers::List(list) => Some(list.clone()),
1944        }
1945    }
1946}
1947
1948fn default_self_test_retry_count() -> u32 {
1949    3
1950}
1951
1952fn default_self_test_retry_delay() -> String {
1953    "5m".to_string()
1954}
1955
1956fn default_self_test_enabled() -> bool {
1957    false
1958}
1959
1960/// Self-test scheduling and behavior configuration.
1961#[derive(Debug, Clone, Serialize, Deserialize)]
1962pub struct SelfTestConfig {
1963    /// Enable scheduled self-tests.
1964    #[serde(default = "default_self_test_enabled")]
1965    pub enabled: bool,
1966    /// Cron schedule (e.g., "0 3 * * *").
1967    #[serde(default)]
1968    pub schedule: Option<String>,
1969    /// Interval duration (e.g., "24h").
1970    #[serde(default)]
1971    pub interval: Option<String>,
1972    /// Which workers to test (default: "all").
1973    #[serde(default)]
1974    pub workers: SelfTestWorkers,
1975    /// Action on failure.
1976    #[serde(default)]
1977    pub on_failure: SelfTestFailureAction,
1978    /// Retry count for failed tests.
1979    #[serde(default = "default_self_test_retry_count")]
1980    pub retry_count: u32,
1981    /// Delay between retries.
1982    #[serde(default = "default_self_test_retry_delay")]
1983    pub retry_delay: String,
1984}
1985
1986impl Default for SelfTestConfig {
1987    fn default() -> Self {
1988        Self {
1989            enabled: default_self_test_enabled(),
1990            schedule: None,
1991            interval: None,
1992            workers: SelfTestWorkers::default(),
1993            on_failure: SelfTestFailureAction::default(),
1994            retry_count: default_self_test_retry_count(),
1995            retry_delay: default_self_test_retry_delay(),
1996        }
1997    }
1998}
1999
2000/// Circuit breaker configuration.
2001#[derive(Debug, Clone, Serialize, Deserialize)]
2002pub struct CircuitBreakerConfig {
2003    /// Consecutive failures to open circuit.
2004    #[serde(default = "default_circuit_failure_threshold")]
2005    pub failure_threshold: u32,
2006    /// Consecutive successes to close circuit.
2007    #[serde(default = "default_circuit_success_threshold")]
2008    pub success_threshold: u32,
2009    /// Error rate threshold (0.0-1.0) to open circuit.
2010    #[serde(default = "default_circuit_error_rate_threshold")]
2011    pub error_rate_threshold: f64,
2012    /// Rolling window size in seconds.
2013    #[serde(default = "default_circuit_window_secs")]
2014    pub window_secs: u64,
2015    /// Cooldown duration before half-open (seconds).
2016    #[serde(default = "default_circuit_open_cooldown_secs")]
2017    pub open_cooldown_secs: u64,
2018    /// Max concurrent probes in half-open.
2019    #[serde(default = "default_circuit_half_open_max_probes")]
2020    pub half_open_max_probes: u32,
2021    /// Consecutive *command-class* failures to open the circuit.
2022    ///
2023    /// Command-class evidence is an operation of the same shape as dispatched
2024    /// work — a remote binary that must run to completion (the telemetry poll,
2025    /// or a health probe whose session connected but whose command never
2026    /// finished). It is tracked separately from the plain liveness probe
2027    /// because a host under fork exhaustion or swap thrash keeps answering
2028    /// `echo` over a warm connection while every real command times out;
2029    /// interleaved liveness successes would otherwise keep resetting
2030    /// `failure_threshold` and the breaker would never trip.
2031    #[serde(default = "default_circuit_command_failure_threshold")]
2032    pub command_failure_threshold: u32,
2033    /// Upper bound (seconds) for the open cooldown after repeated reopens.
2034    ///
2035    /// Each Open -> HalfOpen -> Open cycle without an intervening Closed
2036    /// doubles the cooldown (`open_cooldown_secs`, x2, x4, ...) up to this
2037    /// cap, so a flapping worker converges to Open instead of being
2038    /// re-admitted every cooldown. Set equal to `open_cooldown_secs` to
2039    /// disable the backoff.
2040    #[serde(default = "default_circuit_open_cooldown_max_secs")]
2041    pub open_cooldown_max_secs: u64,
2042}
2043
2044impl Default for CircuitBreakerConfig {
2045    fn default() -> Self {
2046        Self {
2047            failure_threshold: default_circuit_failure_threshold(),
2048            success_threshold: default_circuit_success_threshold(),
2049            error_rate_threshold: default_circuit_error_rate_threshold(),
2050            window_secs: default_circuit_window_secs(),
2051            open_cooldown_secs: default_circuit_open_cooldown_secs(),
2052            half_open_max_probes: default_circuit_half_open_max_probes(),
2053            command_failure_threshold: default_circuit_command_failure_threshold(),
2054            open_cooldown_max_secs: default_circuit_open_cooldown_max_secs(),
2055        }
2056    }
2057}
2058
2059impl CircuitBreakerConfig {
2060    /// Effective cooldown (ms) for the given reopen streak, with exponential
2061    /// backoff capped at `open_cooldown_max_secs`. `reopen_streak` counts
2062    /// Open transitions since the circuit last closed (1 = first open).
2063    pub fn cooldown_ms_for_streak(&self, reopen_streak: u32) -> u64 {
2064        let base_ms = self.open_cooldown_secs.saturating_mul(1000);
2065        let cap_ms = self
2066            .open_cooldown_max_secs
2067            .saturating_mul(1000)
2068            .max(base_ms);
2069        // Exponent bounded so the shift cannot overflow; the cap clamps anyway.
2070        let exponent = reopen_streak.saturating_sub(1).min(16);
2071        base_ms.saturating_mul(1u64 << exponent).min(cap_ms)
2072    }
2073}
2074
2075/// Circuit breaker statistics for tracking state transitions.
2076///
2077/// Maintains a rolling window of successes and failures to determine
2078/// when the circuit should open, close, or enter half-open state.
2079#[derive(Debug, Clone, Default)]
2080pub struct CircuitStats {
2081    /// Current circuit state.
2082    state: CircuitState,
2083    /// Consecutive failure count (reset on success).
2084    consecutive_failures: u32,
2085    /// Consecutive success count in half-open state (reset on failure).
2086    consecutive_successes: u32,
2087    /// Total successes in the current rolling window.
2088    window_successes: u32,
2089    /// Total failures in the current rolling window.
2090    window_failures: u32,
2091    /// Timestamp when circuit entered Open state (epoch millis).
2092    opened_at: Option<u64>,
2093    /// Timestamp of last state change (epoch millis).
2094    last_state_change: u64,
2095    /// Current active probes in half-open state.
2096    active_probes: u32,
2097    /// Consecutive command-class failures (telemetry poll exhaustion, probe
2098    /// sessions that connected but never completed). Reset only by a
2099    /// command-class success or a circuit close — NOT by a liveness success,
2100    /// which is the whole point (see `CircuitBreakerConfig::command_failure_threshold`).
2101    consecutive_command_failures: u32,
2102    /// Number of Open transitions since the circuit last closed. Drives the
2103    /// cooldown backoff; 0 while Closed-since-recovery.
2104    reopen_streak: u32,
2105    /// Recent health check results (true=success, false=failure).
2106    /// Used for history visualization in status display.
2107    recent_results: Vec<bool>,
2108}
2109
2110/// Maximum number of recent results to keep for history visualization.
2111const CIRCUIT_HISTORY_SIZE: usize = 10;
2112
2113impl CircuitStats {
2114    /// Create new circuit stats in the closed state.
2115    pub fn new() -> Self {
2116        Self {
2117            state: CircuitState::Closed,
2118            consecutive_failures: 0,
2119            consecutive_successes: 0,
2120            window_successes: 0,
2121            window_failures: 0,
2122            opened_at: None,
2123            last_state_change: Self::now_millis(),
2124            active_probes: 0,
2125            consecutive_command_failures: 0,
2126            reopen_streak: 0,
2127            recent_results: Vec::with_capacity(CIRCUIT_HISTORY_SIZE),
2128        }
2129    }
2130
2131    /// Consecutive command-class failures observed since the last
2132    /// command-class success (or circuit close).
2133    pub fn consecutive_command_failures(&self) -> u32 {
2134        self.consecutive_command_failures
2135    }
2136
2137    /// Open transitions since the circuit last closed (0 when it has not
2138    /// opened since recovery). Exposed for diagnostics.
2139    pub fn reopen_streak(&self) -> u32 {
2140        self.reopen_streak
2141    }
2142
2143    /// Cooldown currently in force (ms), including reopen backoff.
2144    fn effective_cooldown_ms(&self, config: &CircuitBreakerConfig) -> u64 {
2145        config.cooldown_ms_for_streak(self.reopen_streak.max(1))
2146    }
2147
2148    /// Get the current circuit state.
2149    pub fn state(&self) -> CircuitState {
2150        self.state
2151    }
2152
2153    /// Get the timestamp when the circuit opened (if open).
2154    pub fn opened_at(&self) -> Option<u64> {
2155        self.opened_at
2156    }
2157
2158    /// Get the timestamp of the last state change.
2159    pub fn last_state_change(&self) -> u64 {
2160        self.last_state_change
2161    }
2162
2163    /// Get the consecutive failure count.
2164    pub fn consecutive_failures(&self) -> u32 {
2165        self.consecutive_failures
2166    }
2167
2168    /// Get the error rate in the current window (0.0-1.0).
2169    ///
2170    /// Computed over the bounded recent-results window (the last
2171    /// `CIRCUIT_HISTORY_SIZE` health checks) rather than the unbounded lifetime
2172    /// `window_successes`/`window_failures` counters. A long-lived daemon polling
2173    /// distant workers accumulates occasional transient failures over days; with a
2174    /// lifetime ratio a currently-healthy worker could stay gated (circuit kept
2175    /// open, depressed health score) until the daemon was restarted. A recency
2176    /// window lets the rate self-heal as fresh successes age out old failures.
2177    pub fn error_rate(&self) -> f64 {
2178        if self.recent_results.is_empty() {
2179            return 0.0;
2180        }
2181        let failures = self.recent_results.iter().filter(|&&ok| !ok).count();
2182        failures as f64 / self.recent_results.len() as f64
2183    }
2184
2185    /// Record a successful operation.
2186    ///
2187    /// In closed state: resets consecutive failures, increments window successes.
2188    /// In half-open state: increments consecutive successes.
2189    pub fn record_success(&mut self) {
2190        self.window_successes += 1;
2191        self.consecutive_failures = 0;
2192        self.push_result(true);
2193
2194        if self.state == CircuitState::HalfOpen {
2195            self.consecutive_successes += 1;
2196            if self.active_probes > 0 {
2197                self.active_probes -= 1;
2198            }
2199        }
2200    }
2201
2202    /// Record a failed operation.
2203    ///
2204    /// Increments consecutive failures and window failures.
2205    /// In half-open state, resets consecutive successes.
2206    pub fn record_failure(&mut self) {
2207        self.window_failures += 1;
2208        self.consecutive_failures += 1;
2209        self.push_result(false);
2210
2211        if self.state == CircuitState::HalfOpen {
2212            self.consecutive_successes = 0;
2213            if self.active_probes > 0 {
2214                self.active_probes -= 1;
2215            }
2216        }
2217    }
2218
2219    /// Check if the circuit should open based on config thresholds.
2220    ///
2221    /// Returns true if:
2222    /// - Consecutive failures exceed threshold, OR
2223    /// - Error rate exceeds threshold (with minimum sample size)
2224    pub fn should_open(&self, config: &CircuitBreakerConfig) -> bool {
2225        if self.state != CircuitState::Closed {
2226            return false;
2227        }
2228
2229        // Open if consecutive failures exceed threshold
2230        if self.consecutive_failures >= config.failure_threshold {
2231            return true;
2232        }
2233
2234        // Open if error rate exceeds threshold (with a minimum sample size).
2235        // Counts samples in the bounded recent-results window so the gate
2236        // reflects recent behavior, matching error_rate() above.
2237        if self.recent_results.len() >= 5 && self.error_rate() >= config.error_rate_threshold {
2238            return true;
2239        }
2240
2241        false
2242    }
2243
2244    /// Check if the circuit should transition to half-open.
2245    ///
2246    /// Returns true if the circuit is open and the cooldown period has elapsed.
2247    pub fn should_half_open(&self, config: &CircuitBreakerConfig) -> bool {
2248        if self.state != CircuitState::Open {
2249            return false;
2250        }
2251
2252        let now = Self::now_millis();
2253        let cooldown_ms = self.effective_cooldown_ms(config);
2254
2255        if let Some(opened_at) = self.opened_at {
2256            now.saturating_sub(opened_at) >= cooldown_ms
2257        } else {
2258            false
2259        }
2260    }
2261
2262    /// Check if the circuit should close.
2263    ///
2264    /// Returns true if in half-open state, consecutive successes meet or
2265    /// exceed the success threshold, AND no command-class failure is
2266    /// outstanding. A worker whose last real command timed out is not
2267    /// recovered no matter how many liveness probes it answers; it stays
2268    /// HalfOpen (degraded, probe-limited) until a command-class success
2269    /// clears the evidence.
2270    pub fn should_close(&self, config: &CircuitBreakerConfig) -> bool {
2271        if self.state != CircuitState::HalfOpen {
2272            return false;
2273        }
2274
2275        self.consecutive_successes >= config.success_threshold
2276            && self.consecutive_command_failures == 0
2277    }
2278
2279    /// Check if a probe request can be made in half-open state.
2280    ///
2281    /// Returns true if active probes are below the maximum allowed.
2282    pub fn can_probe(&self, config: &CircuitBreakerConfig) -> bool {
2283        if self.state != CircuitState::HalfOpen {
2284            return false;
2285        }
2286
2287        self.active_probes < config.half_open_max_probes
2288    }
2289
2290    /// Start a probe request in half-open state.
2291    ///
2292    /// Returns true if the probe was started, false if already at max probes.
2293    pub fn start_probe(&mut self, config: &CircuitBreakerConfig) -> bool {
2294        if !self.can_probe(config) {
2295            return false;
2296        }
2297        self.active_probes += 1;
2298        true
2299    }
2300
2301    /// Transition the circuit to open state.
2302    pub fn open(&mut self) {
2303        if self.state != CircuitState::Open {
2304            self.state = CircuitState::Open;
2305            self.opened_at = Some(Self::now_millis());
2306            self.last_state_change = Self::now_millis();
2307            self.consecutive_successes = 0;
2308            self.active_probes = 0;
2309            // Every open without an intervening close lengthens the next
2310            // cooldown (see `CircuitBreakerConfig::cooldown_ms_for_streak`).
2311            self.reopen_streak = self.reopen_streak.saturating_add(1);
2312        }
2313    }
2314
2315    /// Transition the circuit to half-open state.
2316    pub fn half_open(&mut self) {
2317        if self.state != CircuitState::HalfOpen {
2318            self.state = CircuitState::HalfOpen;
2319            self.last_state_change = Self::now_millis();
2320            self.consecutive_successes = 0;
2321            self.active_probes = 0;
2322        }
2323    }
2324
2325    /// Transition the circuit to closed state.
2326    pub fn close(&mut self) {
2327        if self.state != CircuitState::Closed {
2328            self.state = CircuitState::Closed;
2329            self.last_state_change = Self::now_millis();
2330            self.opened_at = None;
2331            self.consecutive_failures = 0;
2332            self.consecutive_successes = 0;
2333            self.active_probes = 0;
2334            self.consecutive_command_failures = 0;
2335            self.reopen_streak = 0;
2336            // Reset the window on close, INCLUDING the recent-results history
2337            // that error_rate() now reads. Otherwise a freshly-recovered circuit
2338            // would still compute a high error rate from the pre-failure samples
2339            // and immediately re-open (flapping). Matches the original intent of
2340            // resetting the error-rate inputs when the circuit recovers.
2341            self.window_successes = 0;
2342            self.window_failures = 0;
2343            self.recent_results.clear();
2344        }
2345    }
2346
2347    /// Reset the rolling window counters.
2348    ///
2349    /// Called periodically to ensure the window reflects recent activity.
2350    /// Also clears the recent-results history that `error_rate()` is computed
2351    /// from, so a reset truly zeroes the observed error rate.
2352    pub fn reset_window(&mut self) {
2353        self.window_successes = 0;
2354        self.window_failures = 0;
2355        self.recent_results.clear();
2356    }
2357
2358    /// Get recent health check results for history visualization.
2359    ///
2360    /// Returns a slice of recent results (true=success, false=failure),
2361    /// with the most recent result at the end.
2362    pub fn recent_results(&self) -> &[bool] {
2363        &self.recent_results
2364    }
2365
2366    /// Calculate seconds remaining until circuit auto-transitions to half-open.
2367    ///
2368    /// Returns None if circuit is not open or cooldown has already elapsed.
2369    pub fn recovery_remaining_secs(&self, config: &CircuitBreakerConfig) -> Option<u64> {
2370        if self.state != CircuitState::Open {
2371            return None;
2372        }
2373
2374        let now = Self::now_millis();
2375        let cooldown_ms = self.effective_cooldown_ms(config);
2376
2377        if let Some(opened_at) = self.opened_at {
2378            let elapsed_ms = now.saturating_sub(opened_at);
2379            if elapsed_ms < cooldown_ms {
2380                return Some((cooldown_ms - elapsed_ms) / 1000);
2381            }
2382        }
2383        None
2384    }
2385
2386    /// Push a result to the history, maintaining the maximum size.
2387    fn push_result(&mut self, success: bool) {
2388        if self.recent_results.len() >= CIRCUIT_HISTORY_SIZE {
2389            self.recent_results.remove(0);
2390        }
2391        self.recent_results.push(success);
2392    }
2393
2394    fn now_millis() -> u64 {
2395        std::time::SystemTime::now()
2396            .duration_since(std::time::UNIX_EPOCH)
2397            .map(|d| d.as_millis() as u64)
2398            .unwrap_or(0)
2399    }
2400
2401    /// Drive the circuit through a single health-check outcome and return the
2402    /// resulting [`CircuitState`].
2403    ///
2404    /// This is the single, self-contained transition engine shared by every
2405    /// circuit consumer (the daemon health monitor via
2406    /// `WorkerState::record_health_check`, and the diagnostic `WorkerHealth`
2407    /// panel via `WorkerHealth::observe`). Keeping the transition logic in ONE
2408    /// place is what makes `WorkerState.circuit` (which selection reads) and the
2409    /// diagnostic `WorkerHealth.circuit` agree.
2410    ///
2411    /// Behaviour:
2412    /// - On a **healthy** outcome: if the circuit is Open and its cooldown has
2413    ///   elapsed it first transitions Open → HalfOpen *before* crediting the
2414    ///   success, so recovery needs exactly `success_threshold` clean probes
2415    ///   (crediting a success against an Open circuit would be lost). Then the
2416    ///   success is recorded and the circuit closes once the threshold is met.
2417    /// - On an **unhealthy** outcome: the failure is recorded, the circuit opens
2418    ///   if the closed-state open condition is met, and a HalfOpen probe failure
2419    ///   reopens the circuit — UNLESS the failure was `transient` (a retryable
2420    ///   transport blip that survived the in-check retries). A transient
2421    ///   half-open failure neither reopens nor resets the cooldown, so a single
2422    ///   network hiccup during recovery cannot bounce a worker back to Open.
2423    /// - After either branch, an Open circuit past its cooldown is nudged to
2424    ///   HalfOpen so probing resumes on the next poll even for a still-failing
2425    ///   worker.
2426    pub fn apply_health_outcome(
2427        &mut self,
2428        healthy: bool,
2429        transient: bool,
2430        config: &CircuitBreakerConfig,
2431    ) -> CircuitState {
2432        if healthy {
2433            // Transition to half-open BEFORE crediting the success so the probe
2434            // counts toward recovery. record_success() only increments the
2435            // consecutive-success counter while in HalfOpen.
2436            if self.state == CircuitState::Open && self.should_half_open(config) {
2437                self.half_open();
2438            }
2439            self.record_success();
2440            if self.should_close(config) {
2441                self.close();
2442            }
2443        } else {
2444            let was_half_open = self.state == CircuitState::HalfOpen;
2445            self.record_failure();
2446            if self.should_open(config) {
2447                self.open();
2448            } else if was_half_open && !transient {
2449                // A real (non-transient) probe failure in half-open reopens the
2450                // circuit and restarts the cooldown. A transient failure is
2451                // ignored so a network blip can't undo an in-progress recovery.
2452                self.open();
2453            }
2454        }
2455
2456        // Even for a still-failing worker, resume probing once the cooldown
2457        // elapses so a recovered worker is retried promptly.
2458        if self.state == CircuitState::Open && self.should_half_open(config) {
2459            self.half_open();
2460        }
2461
2462        self.state
2463    }
2464
2465    /// Drive the circuit through a single *command-class* outcome and return
2466    /// the resulting [`CircuitState`].
2467    ///
2468    /// Command-class evidence comes from an operation of the same shape as
2469    /// dispatched work: a remote binary that must run to completion (the
2470    /// telemetry poll, or a health probe whose SSH session connected but whose
2471    /// command never finished). It is the signal the plain liveness probe
2472    /// cannot provide — a host under fork exhaustion or swap thrash keeps
2473    /// answering `echo` over a warm connection while every real command hangs,
2474    /// and on that evidence alone the breaker would happily re-admit it.
2475    ///
2476    /// Behaviour:
2477    /// - **Success**: clears the outstanding command-failure count and then
2478    ///   counts as an ordinary healthy outcome (may advance Open -> HalfOpen ->
2479    ///   Closed exactly like a liveness success).
2480    /// - **Failure**: increments the command-failure count and records a plain
2481    ///   failure. The circuit opens when either the ordinary open condition or
2482    ///   `command_failure_threshold` is met. In HalfOpen a command failure
2483    ///   ALWAYS reopens — there is no transient exemption, because "connected
2484    ///   but the command did not complete" is precisely the condition the
2485    ///   half-open trial exists to detect. While any command failure is
2486    ///   outstanding, [`Self::should_close`] refuses to close on liveness
2487    ///   successes alone.
2488    pub fn apply_command_outcome(
2489        &mut self,
2490        success: bool,
2491        config: &CircuitBreakerConfig,
2492    ) -> CircuitState {
2493        if success {
2494            self.consecutive_command_failures = 0;
2495            return self.apply_health_outcome(true, false, config);
2496        }
2497
2498        self.consecutive_command_failures = self.consecutive_command_failures.saturating_add(1);
2499        let was_half_open = self.state == CircuitState::HalfOpen;
2500        self.record_failure();
2501        let threshold = config.command_failure_threshold.max(1);
2502        if was_half_open
2503            || self.should_open(config)
2504            || (self.state == CircuitState::Closed
2505                && self.consecutive_command_failures >= threshold)
2506        {
2507            self.open();
2508        }
2509
2510        if self.state == CircuitState::Open && self.should_half_open(config) {
2511            self.half_open();
2512        }
2513
2514        self.state
2515    }
2516}
2517
2518#[derive(Debug, Clone, Serialize, Deserialize)]
2519pub struct CompilationConfig {
2520    /// Minimum confidence score to intercept (0.0-1.0).
2521    #[serde(default = "default_confidence")]
2522    pub confidence_threshold: f64,
2523    /// Run locally when this build's measured local median is below this (ms).
2524    /// Consulted only by the speed gate, i.e. when `local_slots > 0`.
2525    #[serde(default = "default_min_local_time")]
2526    pub min_local_time_ms: u64,
2527    /// Minimum measured speedup ratio (local_time / remote_time) to offload.
2528    /// If the measured speedup < threshold, run locally. Default: 1.2 (20% faster).
2529    /// Consulted only by the speed gate, i.e. when `local_slots > 0`.
2530    #[serde(default = "default_speedup_threshold")]
2531    pub remote_speedup_threshold: f64,
2532    /// Speed gate (bd-2spi4): how many offloadable builds may run concurrently
2533    /// on THIS machine because its own measured build history says remote is
2534    /// not worth it. Zero (default) disables the gate, so every offloadable
2535    /// build is offloaded as before. Set it only on a dispatcher that is faster
2536    /// than its workers; local runs also need free memory (see the hook).
2537    #[serde(default)]
2538    pub local_slots: u32,
2539    /// Default slot estimate for build commands (cargo build, gcc, etc.).
2540    #[serde(default = "default_build_slots")]
2541    pub build_slots: u32,
2542    /// Default slot estimate for test commands (cargo test, nextest).
2543    /// Tests typically use more parallelism than builds.
2544    #[serde(default = "default_test_slots")]
2545    pub test_slots: u32,
2546    /// Default slot estimate for check/lint commands (cargo check, clippy).
2547    /// These are typically faster and use fewer resources.
2548    #[serde(default = "default_check_slots")]
2549    pub check_slots: u32,
2550    /// Per-build additional free-space budget on the worker's reported build
2551    /// filesystem, in GiB. Include expected output growth and a safety margin.
2552    /// Zero (default) disables this explicit budget. Positive budgets require
2553    /// a fresh disk observation and are held in durable daemon ownership until
2554    /// completion. They are admission accounting, not filesystem quotas.
2555    #[serde(default)]
2556    pub disk_headroom_gib: u32,
2557    /// Timeout in seconds for build commands.
2558    /// Build commands (cargo build, gcc, etc.) typically complete faster than tests.
2559    #[serde(default = "default_build_timeout")]
2560    pub build_timeout_sec: u64,
2561    /// Timeout in seconds for test commands.
2562    /// Test commands often need longer timeouts due to test suite execution time.
2563    #[serde(default = "default_test_timeout")]
2564    pub test_timeout_sec: u64,
2565    /// Timeout in seconds for bun commands (bun test, bun typecheck).
2566    /// Bun has known issues where its internal --timeout flag doesn't work for
2567    /// CPU-bound hangs, so we use a shorter external timeout by default.
2568    #[serde(default = "default_bun_timeout")]
2569    pub bun_timeout_sec: u64,
2570    /// Whether to wrap remote commands with external timeout protection.
2571    /// This prevents runaway/stuck processes from consuming worker slots indefinitely.
2572    /// Default: true. Set to false to disable timeout wrapping entirely.
2573    #[serde(default = "default_external_timeout_enabled")]
2574    pub external_timeout_enabled: bool,
2575    /// Whether a remote build FAILURE may fall back to LOCAL execution on the
2576    /// orchestrator once every eligible remote worker has been exhausted.
2577    ///
2578    /// On a remote failure the hook first retries the build on a *different,
2579    /// higher-capacity* worker (see `run_exec`'s worker-fallback loop). Local
2580    /// execution runs the heavy compile on the orchestrator that coordinates the
2581    /// whole fleet, so it is a genuine last resort and can flood that box. This
2582    /// flag gates it. Default `true` preserves the historical fail-open behavior
2583    /// (local only when no worker can serve); set to `false` on an orchestrator
2584    /// to refuse local fallback and fail the build closed instead. `force_remote`
2585    /// / proof mode / `RCH_REQUIRE_REMOTE` still override this by refusing local
2586    /// unconditionally.
2587    #[serde(default = "default_allow_local_fallback")]
2588    pub allow_local_fallback: bool,
2589    /// Per-job rustc parallelism cap applied on the WORKER (issue #49).
2590    ///
2591    /// `total_slots` bounds concurrent *jobs* per worker, but cargo defaults
2592    /// `-j` to the worker's `nproc`, so one offloaded `cargo test --workspace`
2593    /// forks `nproc` rustc regardless of slots and can swap-thrash a
2594    /// RAM-light box. This policy injects `CARGO_BUILD_JOBS` into the remote
2595    /// build environment; see [`RemoteBuildJobs`] for the modes. It never
2596    /// overrides a `CARGO_BUILD_JOBS` the worker session already carries, an
2597    /// inline `CARGO_BUILD_JOBS=N` or `-j N` in the command, or a project
2598    /// `.cargo/config.toml` `[build] jobs` — explicit intent always wins.
2599    #[serde(default)]
2600    pub remote_build_jobs: RemoteBuildJobs,
2601}
2602
2603/// How rch derives the `CARGO_BUILD_JOBS` it injects into a remote build.
2604///
2605/// - `auto` (default): computed on the worker from its live `nproc` and
2606///   total RAM as `clamp(ram_gib / 8, 2, min(nproc, 8))` — budgeting roughly
2607///   8 GiB of RAM per concurrent rustc leaves headroom for two to three
2608///   concurrent jobs at a realistic per-rustc peak of ~3 GiB.
2609/// - `off`: inject nothing (historical behavior; cargo uses `nproc`).
2610/// - `<n>`: inject a fixed job count fleet-wide.
2611///
2612/// In every mode an explicitly set `CARGO_BUILD_JOBS` on the worker (for
2613/// example from `/etc/environment`) or in the command is left untouched.
2614#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
2615pub enum RemoteBuildJobs {
2616    /// Derive from the worker's cores and RAM at execution time.
2617    #[default]
2618    Auto,
2619    /// Do not inject `CARGO_BUILD_JOBS`.
2620    Off,
2621    /// Inject a fixed job count (must be >= 1).
2622    Fixed(u32),
2623}
2624
2625impl RemoteBuildJobs {
2626    /// Parse the config/CLI spelling: `auto`, `off`, or a positive integer.
2627    pub fn parse(value: &str) -> Result<Self, String> {
2628        let trimmed = value.trim();
2629        match trimmed.to_ascii_lowercase().as_str() {
2630            "auto" => Ok(Self::Auto),
2631            "off" | "none" | "disabled" => Ok(Self::Off),
2632            _ => match trimmed.parse::<u32>() {
2633                Ok(0) => Err(
2634                    "remote_build_jobs must be >= 1 (cargo rejects `-j 0`); use `off` to disable"
2635                        .to_string(),
2636                ),
2637                Ok(n) => Ok(Self::Fixed(n)),
2638                Err(_) => Err(format!(
2639                    "'{trimmed}' is not a valid remote_build_jobs value (expected `auto`, `off`, or a positive integer)"
2640                )),
2641            },
2642        }
2643    }
2644}
2645
2646impl std::fmt::Display for RemoteBuildJobs {
2647    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2648        match self {
2649            Self::Auto => f.write_str("auto"),
2650            Self::Off => f.write_str("off"),
2651            Self::Fixed(n) => write!(f, "{n}"),
2652        }
2653    }
2654}
2655
2656impl std::str::FromStr for RemoteBuildJobs {
2657    type Err = String;
2658
2659    fn from_str(value: &str) -> Result<Self, Self::Err> {
2660        Self::parse(value)
2661    }
2662}
2663
2664impl Serialize for RemoteBuildJobs {
2665    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2666        match self {
2667            Self::Auto => serializer.serialize_str("auto"),
2668            Self::Off => serializer.serialize_str("off"),
2669            Self::Fixed(n) => serializer.serialize_u32(*n),
2670        }
2671    }
2672}
2673
2674impl<'de> Deserialize<'de> for RemoteBuildJobs {
2675    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
2676        #[derive(Deserialize)]
2677        #[serde(untagged)]
2678        enum Raw {
2679            Count(u64),
2680            Spelling(String),
2681        }
2682        match Raw::deserialize(deserializer)? {
2683            Raw::Count(n) => {
2684                let n = u32::try_from(n).map_err(|_| {
2685                    serde::de::Error::custom(format!("remote_build_jobs {n} is out of range"))
2686                })?;
2687                Self::parse(&n.to_string()).map_err(serde::de::Error::custom)
2688            }
2689            Raw::Spelling(s) => Self::parse(&s).map_err(serde::de::Error::custom),
2690        }
2691    }
2692}
2693
2694impl Default for CompilationConfig {
2695    fn default() -> Self {
2696        Self {
2697            confidence_threshold: 0.85,
2698            min_local_time_ms: 2000,
2699            remote_speedup_threshold: default_speedup_threshold(),
2700            local_slots: 0,
2701            build_slots: default_build_slots(),
2702            test_slots: default_test_slots(),
2703            check_slots: default_check_slots(),
2704            disk_headroom_gib: 0,
2705            build_timeout_sec: default_build_timeout(),
2706            test_timeout_sec: default_test_timeout(),
2707            bun_timeout_sec: default_bun_timeout(),
2708            external_timeout_enabled: default_external_timeout_enabled(),
2709            allow_local_fallback: default_allow_local_fallback(),
2710            remote_build_jobs: RemoteBuildJobs::default(),
2711        }
2712    }
2713}
2714
2715fn default_build_slots() -> u32 {
2716    4
2717}
2718
2719fn default_test_slots() -> u32 {
2720    8
2721}
2722
2723fn default_check_slots() -> u32 {
2724    2
2725}
2726
2727/// Default build timeout: 5 minutes (300 seconds).
2728/// Most builds complete well within this time.
2729fn default_build_timeout() -> u64 {
2730    300
2731}
2732
2733/// Default test timeout: 30 minutes (1800 seconds).
2734/// Test suites often take significantly longer than builds.
2735fn default_test_timeout() -> u64 {
2736    1800
2737}
2738
2739/// Default bun timeout: 10 minutes (600 seconds).
2740/// Bun's internal --timeout flag doesn't work for CPU-bound hangs,
2741/// so we use a shorter external timeout to prevent zombies.
2742fn default_bun_timeout() -> u64 {
2743    600
2744}
2745
2746/// Default: external timeout protection is enabled.
2747fn default_external_timeout_enabled() -> bool {
2748    true
2749}
2750
2751/// Default: permit last-resort local fallback after remote workers are
2752/// exhausted. Set `compilation.allow_local_fallback = false` to fail closed and
2753/// keep heavy compiles off the orchestrator entirely.
2754fn default_allow_local_fallback() -> bool {
2755    true
2756}
2757
2758impl CompilationConfig {
2759    /// Returns the appropriate external timeout for the given compilation kind.
2760    ///
2761    /// - Bun commands get the shorter bun_timeout (to protect against known hang issues)
2762    /// - Test commands get the longer test_timeout
2763    /// - All other commands (builds, checks, clippy) get the build_timeout
2764    pub fn timeout_for_kind(&self, kind: Option<CompilationKind>) -> std::time::Duration {
2765        let secs = match kind {
2766            // Bun has known issues where internal timeout doesn't work for CPU hangs
2767            Some(CompilationKind::BunTest) | Some(CompilationKind::BunTypecheck) => {
2768                self.bun_timeout_sec
2769            }
2770            // Other test commands use the standard test timeout
2771            Some(CompilationKind::CargoTest) | Some(CompilationKind::CargoNextest) => {
2772                self.test_timeout_sec
2773            }
2774            // Nix derivation builds can take a long time (fetch + compile large
2775            // closures); give them the generous test-timeout bucket rather than
2776            // the short build timeout so legitimate builds are not killed early.
2777            Some(CompilationKind::NixBuild) => self.test_timeout_sec,
2778            // Go test suites are long-running like cargo test; give them the
2779            // test bucket rather than the short build timeout.
2780            Some(CompilationKind::GoTest) => self.test_timeout_sec,
2781            // Builds, checks, clippy, and unknown use build timeout
2782            _ => self.build_timeout_sec,
2783        };
2784        std::time::Duration::from_secs(secs)
2785    }
2786
2787    /// Returns whether external timeout wrapping is enabled.
2788    pub fn external_timeout_enabled(&self) -> bool {
2789        self.external_timeout_enabled
2790    }
2791}
2792
2793/// Version of the Layer 0 configuration pack schema (bd-bqu38 / M-1 §149).
2794///
2795/// Bump when a knob is added, removed, or its meaning changes; consumers
2796/// record the version alongside emitted baselines so regressions are
2797/// attributable to a specific pack revision.
2798pub const LAYER0_PACK_VERSION: &str = "1";
2799
2800/// Layer 0 configuration pack knobs (bd-bqu38).
2801///
2802/// Every knob defaults to OFF and toggles independently — the kill/rollback
2803/// contract from the master plan (§149: removed from defaults if it regresses
2804/// representative p95, output equivalence, debugger behavior, or
2805/// compatibility). Knobs are injected into remote cargo builds as
2806/// `CARGO_PROFILE_*` environment variables, so enabling one never mutates any
2807/// user-owned `.cargo/config.toml`.
2808#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
2809#[serde(default)]
2810pub struct Layer0Config {
2811    /// Master switch: nothing is injected unless this is on.
2812    pub enabled: bool,
2813    /// `profile.release.lto = "thin"` via `CARGO_PROFILE_RELEASE_LTO=thin`.
2814    pub release_lto_thin: bool,
2815    /// `profile.release.codegen-units = 1` via
2816    /// `CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1`.
2817    pub release_codegen_units_1: bool,
2818}
2819
2820impl Layer0Config {
2821    /// Environment pairs to inject into a remote cargo build.
2822    ///
2823    /// Empty unless [`Self::enabled`] — the master switch gates every knob so
2824    /// an operator can disable the whole pack with one key. Order is stable
2825    /// (declaration order) for deterministic emission and test assertions.
2826    #[must_use]
2827    pub fn active_env(&self) -> Vec<(&'static str, &'static str)> {
2828        if !self.enabled {
2829            return Vec::new();
2830        }
2831        let mut env = Vec::new();
2832        if self.release_lto_thin {
2833            env.push(("CARGO_PROFILE_RELEASE_LTO", "thin"));
2834        }
2835        if self.release_codegen_units_1 {
2836            env.push(("CARGO_PROFILE_RELEASE_CODEGEN_UNITS", "1"));
2837        }
2838        env
2839    }
2840}
2841
2842#[derive(Debug, Clone, Serialize, Deserialize)]
2843pub struct TransferConfig {
2844    /// zstd compression level (1-19).
2845    #[serde(default = "default_compression")]
2846    pub compression_level: u32,
2847    /// Patterns to exclude from transfer.
2848    #[serde(default = "default_excludes")]
2849    pub exclude_patterns: Vec<String>,
2850    /// SSH keepalive interval in seconds (`ssh -o ServerAliveInterval=<N>`).
2851    ///
2852    /// When unset, build transfers use 15 seconds. Zero defers to OpenSSH configuration.
2853    #[serde(default, skip_serializing_if = "Option::is_none")]
2854    pub ssh_server_alive_interval_secs: Option<u64>,
2855    /// SSH ControlPersist idle timeout in seconds (`ssh -o ControlPersist=<N>s`).
2856    ///
2857    /// - Unset preserves OpenSSH defaults (ControlPersist enabled by default via mux).
2858    /// - `0` disables persistence (`ControlPersist=no`).
2859    #[serde(default, skip_serializing_if = "Option::is_none")]
2860    pub ssh_control_persist_secs: Option<u64>,
2861    /// Staging base on the worker for the transfer paths that do NOT mirror
2862    /// the client's project layout: `--clean-overlay` roots, Windows workers,
2863    /// and `rch cache warm`. Must be an absolute path; defaults to
2864    /// [`default_remote_base`] (`/data/tmp/rch`).
2865    ///
2866    /// An ordinary `rch exec` does not use this: it mirrors the project under
2867    /// the worker's canonical root (`[path_topology] canonical_root`), and the
2868    /// pooled Cargo target store rides along inside that mirror. To move
2869    /// pooled stores onto another filesystem, set
2870    /// `[remediation.pooled_target] store_base` (issue #64).
2871    #[serde(default = "default_remote_base")]
2872    pub remote_base: String,
2873    /// Per-attempt timeout for source synchronization, in milliseconds.
2874    ///
2875    /// This is deliberately separate from remote compilation and artifact-return
2876    /// timeouts. When unset, RCH derives a conservative timeout from the source
2877    /// payload size (30 seconds plus one second per MiB, capped at one hour).
2878    #[serde(default, skip_serializing_if = "Option::is_none")]
2879    pub sync_timeout_ms: Option<u64>,
2880    /// Silence-based stall timeout for source synchronization, in seconds
2881    /// (issue #59).
2882    ///
2883    /// Aborts a source sync when rsync emits NO output at all — no progress
2884    /// refresh, no stats, no itemized line — for this long. This is deliberately
2885    /// far tighter than the wall-clock `sync_timeout_ms` cap (which scales with
2886    /// payload size up to an hour): a large first-time sync keeps refreshing its
2887    /// progress line and is never killed, while a dead channel or wedged rsync
2888    /// is detected within this window instead of holding the reservation until
2889    /// the wall-clock cap. On a stall the hook releases the worker and re-enters
2890    /// selection excluding it. `0` disables silence detection.
2891    #[serde(default = "default_source_sync_silence_timeout_secs")]
2892    pub source_sync_silence_timeout_secs: u64,
2893    /// Retry policy for transient network errors during transfer.
2894    #[serde(default)]
2895    pub retry: RetryConfig,
2896    /// Verify artifact integrity using blake3 hashes after transfer (bd-377q).
2897    ///
2898    /// When enabled, computes blake3 hashes of key artifacts (binaries, test outputs)
2899    /// on the worker after build, then verifies them locally after download.
2900    /// Disabled by default to avoid extra overhead.
2901    #[serde(default)]
2902    pub verify_artifacts: bool,
2903    /// Maximum file size (bytes) for artifact verification (bd-377q).
2904    ///
2905    /// Files larger than this limit are skipped during verification to avoid
2906    /// excessive I/O. Defaults to 100MB.
2907    #[serde(default = "default_verify_max_size")]
2908    pub verify_max_size_bytes: u64,
2909
2910    // =========================================================================
2911    // Transfer Optimization (bd-3hho)
2912    // =========================================================================
2913    /// Maximum transfer size in MB before skipping remote execution.
2914    ///
2915    /// When set, runs `rsync --dry-run --stats` to estimate transfer size.
2916    /// If the estimated size exceeds this threshold, remote offload is skipped
2917    /// and the command runs locally. Set to `None` (default) to disable.
2918    #[serde(default, skip_serializing_if = "Option::is_none")]
2919    pub max_transfer_mb: Option<u64>,
2920
2921    /// Maximum estimated transfer time in milliseconds before skipping remote.
2922    ///
2923    /// Uses `estimated_bandwidth_bps` (or measured link speed) to calculate
2924    /// expected transfer time. If it exceeds this threshold, remote offload
2925    /// is skipped. Set to `None` (default) to disable.
2926    #[serde(default, skip_serializing_if = "Option::is_none")]
2927    pub max_transfer_time_ms: Option<u64>,
2928
2929    /// Bandwidth limit for rsync in KB/s.
2930    ///
2931    /// Passed as `rsync --bwlimit=<N>` to limit transfer speed and prevent
2932    /// network saturation. Set to `None` or `0` (default) for unlimited.
2933    #[serde(default, skip_serializing_if = "Option::is_none")]
2934    pub bwlimit_kbps: Option<u64>,
2935
2936    /// Explicit rsync binary for every local transfer (issue #66).
2937    ///
2938    /// Unset (the default) lets rch pick: the PATH `rsync` when it is a
2939    /// modern (3.1+) rsync, otherwise a modern rsync from the well-known
2940    /// Homebrew/MacPorts locations, otherwise the PATH binary driven with the
2941    /// openrsync/2.6.9-compatible argv. Set this to force one binary
2942    /// (`~` is expanded; a bare name is looked up on PATH). The
2943    /// `RCH_RSYNC_BIN` environment variable overrides this setting.
2944    #[serde(default, skip_serializing_if = "Option::is_none")]
2945    pub rsync_bin: Option<String>,
2946
2947    /// Estimated link bandwidth in bytes per second.
2948    ///
2949    /// Used for transfer time estimation when `max_transfer_time_ms` is set.
2950    /// If not provided, defaults to 10 MB/s (10485760 bytes/sec).
2951    #[serde(default, skip_serializing_if = "Option::is_none")]
2952    pub estimated_bandwidth_bps: Option<u64>,
2953
2954    // =========================================================================
2955    // Adaptive Compression (bd-243w)
2956    // =========================================================================
2957    /// Enable adaptive zstd compression based on transfer size (bd-243w).
2958    ///
2959    /// When enabled, automatically selects compression level based on estimated
2960    /// payload size:
2961    /// - < 10MB: level 1 (fast, minimal compression)
2962    /// - 10-200MB: level 3 (balanced, default)
2963    /// - > 200MB: level 7 (slower, better compression)
2964    ///
2965    /// When disabled (default), uses the fixed `compression_level` setting.
2966    #[serde(default)]
2967    pub adaptive_compression: bool,
2968
2969    /// Minimum compression level for adaptive mode (bd-243w).
2970    ///
2971    /// Even in adaptive mode, compression level won't go below this.
2972    /// Defaults to 1.
2973    #[serde(default = "default_min_compression")]
2974    pub min_compression_level: u32,
2975
2976    /// Maximum compression level for adaptive mode (bd-243w).
2977    ///
2978    /// Even in adaptive mode, compression level won't exceed this.
2979    /// Defaults to 9 (avoids CPU-intensive levels 10+).
2980    #[serde(default = "default_max_compression")]
2981    pub max_compression_level: u32,
2982}
2983
2984impl Default for TransferConfig {
2985    fn default() -> Self {
2986        Self {
2987            compression_level: 3,
2988            exclude_patterns: default_excludes(),
2989            ssh_server_alive_interval_secs: None,
2990            ssh_control_persist_secs: None,
2991            remote_base: default_remote_base(),
2992            sync_timeout_ms: None,
2993            source_sync_silence_timeout_secs: default_source_sync_silence_timeout_secs(),
2994            retry: RetryConfig::default(),
2995            verify_artifacts: false,
2996            verify_max_size_bytes: default_verify_max_size(),
2997            // Transfer optimization (bd-3hho)
2998            max_transfer_mb: None,
2999            max_transfer_time_ms: None,
3000            bwlimit_kbps: None,
3001            rsync_bin: None,
3002            estimated_bandwidth_bps: None,
3003            // Adaptive compression (bd-243w)
3004            adaptive_compression: false,
3005            min_compression_level: default_min_compression(),
3006            max_compression_level: default_max_compression(),
3007        }
3008    }
3009}
3010
3011impl TransferConfig {
3012    /// Smallest accepted explicit source-sync timeout (one second).
3013    pub const MIN_SYNC_TIMEOUT_MS: u64 = 1_000;
3014    /// Largest accepted explicit or derived source-sync timeout (one hour).
3015    pub const MAX_SYNC_TIMEOUT_MS: u64 = 3_600_000;
3016    /// Fixed floor used by the payload-aware default policy.
3017    pub const DEFAULT_SYNC_TIMEOUT_FLOOR_MS: u64 = 30_000;
3018    const DEFAULT_SYNC_BYTES_PER_SECOND: u64 = 1024 * 1024;
3019
3020    /// Whether an explicit source-sync timeout is within the supported range.
3021    #[must_use]
3022    pub fn valid_sync_timeout_ms(value: u64) -> bool {
3023        (Self::MIN_SYNC_TIMEOUT_MS..=Self::MAX_SYNC_TIMEOUT_MS).contains(&value)
3024    }
3025
3026    /// Resolve the per-attempt source-sync timeout for an exact payload size.
3027    ///
3028    /// Explicit configuration wins. Otherwise the default grants 30 seconds of
3029    /// setup time plus one second per MiB at a conservative 1 MiB/s floor. This
3030    /// prevents a cold, large pinned tree from inheriting the old fixed 30-second
3031    /// ceiling while still bounding every individual attempt.
3032    #[must_use]
3033    pub fn sync_timeout_for_payload(&self, payload_bytes: u64) -> std::time::Duration {
3034        let timeout_ms = self
3035            .sync_timeout_ms
3036            .filter(|value| Self::valid_sync_timeout_ms(*value))
3037            .unwrap_or_else(|| {
3038                let transfer_seconds = payload_bytes
3039                    .saturating_add(Self::DEFAULT_SYNC_BYTES_PER_SECOND - 1)
3040                    / Self::DEFAULT_SYNC_BYTES_PER_SECOND;
3041                Self::DEFAULT_SYNC_TIMEOUT_FLOOR_MS
3042                    .saturating_add(transfer_seconds.saturating_mul(1_000))
3043                    .min(Self::MAX_SYNC_TIMEOUT_MS)
3044            });
3045        std::time::Duration::from_millis(timeout_ms)
3046    }
3047
3048    /// Select compression level based on estimated transfer size (bd-243w).
3049    ///
3050    /// When adaptive compression is enabled, selects an appropriate level
3051    /// based on payload size:
3052    /// - < 10MB: level 1 (fast, minimal compression)
3053    /// - 10-200MB: level 3 (balanced)
3054    /// - > 200MB: level 7 (better compression)
3055    ///
3056    /// The result is clamped between `min_compression_level` and `max_compression_level`.
3057    ///
3058    /// When adaptive compression is disabled, returns the fixed `compression_level`.
3059    pub fn select_compression_level(&self, estimated_bytes: Option<u64>) -> u32 {
3060        if !self.adaptive_compression {
3061            return self.compression_level;
3062        }
3063
3064        let Some(bytes) = estimated_bytes else {
3065            // No estimate available, use default
3066            return self.compression_level;
3067        };
3068
3069        // Size thresholds
3070        const SMALL_THRESHOLD: u64 = 10_000_000; // 10 MB (decimal)
3071        const LARGE_THRESHOLD: u64 = 200_000_000; // 200 MB (decimal)
3072
3073        let level = if bytes < SMALL_THRESHOLD {
3074            1 // Fast compression for small payloads
3075        } else if bytes < LARGE_THRESHOLD {
3076            3 // Balanced for medium payloads
3077        } else {
3078            7 // Better compression for large payloads
3079        };
3080
3081        // Clamp to configured bounds
3082        level.clamp(self.min_compression_level, self.max_compression_level)
3083    }
3084}
3085
3086// =============================================================================
3087// Execution Configuration (bd-785w)
3088// =============================================================================
3089
3090/// Default allowlist of commands that can be executed remotely.
3091/// Aligned with the classifier's supported CompilationKind values.
3092/// Command bases of every compilation kind rch knows how to offload.
3093///
3094/// This is the built-in `execution.allowlist`. It is also what a persisted,
3095/// explicit allowlist is compared against: a config file written before a tool
3096/// family was added (Go, Nix, TypeScript, ...) silently keeps that family local,
3097/// and the only symptom is a `[RCH] local (... not in execution.allowlist)` line
3098/// (issue #51). `ExecutionConfig::missing_builtin_bases` surfaces the gap.
3099#[rustfmt::skip]
3100pub const BUILTIN_EXECUTION_BASES: &[&str] = &[
3101    // Rust
3102    "cargo",
3103    "rustc",
3104    // cargo-nextest
3105    "nextest",
3106    // C/C++
3107    "gcc",
3108    "g++",
3109    "clang",
3110    "clang++",
3111    "cc",
3112    "c++",
3113    // Build systems
3114    "make",
3115    "cmake",
3116    "ninja",
3117    "meson",
3118    // Bun
3119    "bun",
3120    // Nix (covers both `nix build` and the legacy `nix-build`)
3121    "nix",
3122    // Go
3123    "go",
3124    // TypeScript (covers both `tsc` and `npx tsc`; command_base is "tsc")
3125    "tsc",
3126];
3127
3128fn default_execution_allowlist() -> Vec<String> {
3129    BUILTIN_EXECUTION_BASES
3130        .iter()
3131        .map(|base| (*base).to_string())
3132        .collect()
3133}
3134
3135/// Execution allowlist configuration for remote builds (bd-785w).
3136///
3137/// Controls which command base names are permitted for remote execution.
3138/// Commands not in the allowlist will fail-open to local execution.
3139#[derive(Debug, Clone, Serialize, Deserialize)]
3140pub struct ExecutionConfig {
3141    /// Allowlist of command base names permitted for remote execution.
3142    ///
3143    /// Examples: "cargo", "rustc", "gcc", "bun"
3144    ///
3145    /// If a classified compilation command's base name is not in this list,
3146    /// RCH will fail-open to local execution with a clear reason.
3147    ///
3148    /// An empty allowlist disables all remote execution (local only).
3149    #[serde(default = "default_execution_allowlist")]
3150    pub allowlist: Vec<String>,
3151}
3152
3153impl Default for ExecutionConfig {
3154    fn default() -> Self {
3155        Self {
3156            allowlist: default_execution_allowlist(),
3157        }
3158    }
3159}
3160
3161impl ExecutionConfig {
3162    /// Check if a command base name is allowed for remote execution.
3163    pub fn is_allowed(&self, command_base: &str) -> bool {
3164        self.allowlist
3165            .iter()
3166            .any(|allowed| allowed.eq_ignore_ascii_case(command_base))
3167    }
3168
3169    /// Built-in compilation tool bases that this allowlist does NOT admit.
3170    ///
3171    /// Empty for the default allowlist. Non-empty means some command family rch
3172    /// classifies as compilation will always fall open to local execution on
3173    /// this box — usually because the config file predates that family.
3174    pub fn missing_builtin_bases(&self) -> Vec<&'static str> {
3175        BUILTIN_EXECUTION_BASES
3176            .iter()
3177            .copied()
3178            .filter(|base| !self.is_allowed(base))
3179            .collect()
3180    }
3181}
3182
3183// =============================================================================
3184// Retry Configuration (bd-x1ek)
3185// =============================================================================
3186
3187fn default_retry_max_attempts() -> u32 {
3188    3
3189}
3190
3191fn default_retry_base_delay_ms() -> u64 {
3192    100
3193}
3194
3195fn default_retry_max_delay_ms() -> u64 {
3196    5000
3197}
3198
3199fn default_retry_jitter_factor() -> f64 {
3200    0.1
3201}
3202
3203fn default_retry_total_timeout_ms() -> u64 {
3204    30000
3205}
3206
3207/// Configuration for retry behavior on transient errors.
3208///
3209/// Used by rsync and SSH operations to recover from transient network failures
3210/// without blocking too long or retrying non-recoverable errors.
3211#[derive(Debug, Clone, Serialize, Deserialize)]
3212pub struct RetryConfig {
3213    /// Maximum number of retry attempts (1 = no retries, just initial attempt).
3214    #[serde(default = "default_retry_max_attempts")]
3215    pub max_attempts: u32,
3216
3217    /// Initial delay between retries in milliseconds.
3218    #[serde(default = "default_retry_base_delay_ms")]
3219    pub base_delay_ms: u64,
3220
3221    /// Maximum delay between retries in milliseconds (caps exponential backoff).
3222    #[serde(default = "default_retry_max_delay_ms")]
3223    pub max_delay_ms: u64,
3224
3225    /// Jitter factor (0.0 - 1.0) to randomize delay.
3226    /// A value of 0.1 means ±10% randomization.
3227    #[serde(default = "default_retry_jitter_factor")]
3228    pub jitter_factor: f64,
3229
3230    /// Total timeout for all retry attempts in milliseconds.
3231    /// After this, retries stop even if max_attempts not reached.
3232    #[serde(default = "default_retry_total_timeout_ms")]
3233    pub total_timeout_ms: u64,
3234}
3235
3236impl Default for RetryConfig {
3237    fn default() -> Self {
3238        Self {
3239            max_attempts: default_retry_max_attempts(),
3240            base_delay_ms: default_retry_base_delay_ms(),
3241            max_delay_ms: default_retry_max_delay_ms(),
3242            jitter_factor: default_retry_jitter_factor(),
3243            total_timeout_ms: default_retry_total_timeout_ms(),
3244        }
3245    }
3246}
3247
3248impl RetryConfig {
3249    /// Create a config that disables retries (single attempt only).
3250    pub fn no_retry() -> Self {
3251        Self {
3252            max_attempts: 1,
3253            ..Default::default()
3254        }
3255    }
3256
3257    /// Calculate delay for a given attempt number (0-indexed).
3258    ///
3259    /// Uses exponential backoff with optional jitter.
3260    pub fn delay_for_attempt(&self, attempt: u32) -> std::time::Duration {
3261        if attempt == 0 {
3262            return std::time::Duration::ZERO;
3263        }
3264
3265        // Exponential backoff: base * 2^attempt
3266        let base = self.base_delay_ms as f64;
3267        let delay = base * (2.0_f64.powi(attempt as i32 - 1));
3268        let capped = delay.min(self.max_delay_ms as f64);
3269
3270        // Apply jitter: delay ± (delay * jitter_factor)
3271        let jittered = if self.jitter_factor > 0.0 {
3272            let jitter_range = capped * self.jitter_factor;
3273            let jitter = (rand::rng().random::<f64>() * 2.0 - 1.0) * jitter_range;
3274            (capped + jitter).max(0.0)
3275        } else {
3276            capped
3277        };
3278
3279        std::time::Duration::from_millis(jittered as u64)
3280    }
3281
3282    /// Check if we should retry given elapsed time since start.
3283    pub fn should_retry(&self, attempt: u32, elapsed: std::time::Duration) -> bool {
3284        if attempt >= self.max_attempts {
3285            return false;
3286        }
3287        elapsed.as_millis() < self.total_timeout_ms as u128
3288    }
3289}
3290
3291/// Default source-sync silence timeout (issue #59): two minutes without a
3292/// single byte of rsync output means a dead channel or wedged rsync, not a
3293/// large transfer (progress2 refreshes its line continuously while bytes
3294/// flow, and each refresh counts as forward progress).
3295fn default_source_sync_silence_timeout_secs() -> u64 {
3296    120
3297}
3298
3299/// Default remote base path for project transfers.
3300///
3301/// Rooted under `/data/tmp` (the fleet-standard managed ephemeral-build zone)
3302/// rather than `/tmp`: on the Linux workers `/tmp` is a RAM-backed tmpfs, so
3303/// dispatched build artifacts placed there consume RAM and evade the disk-GC
3304/// (sbh / datatmp-janitor) that watches `/data/tmp`. Keeping the remote base in
3305/// the managed zone lets those reapers reclaim orphaned build trees. Must agree
3306/// with the rchd cache-cleanup and reclaim `remote_base` defaults.
3307pub fn default_remote_base() -> String {
3308    "/data/tmp/rch".to_string()
3309}
3310
3311/// Validate and normalize a remote base path.
3312/// Returns an error if the path is not absolute or contains traversal sequences.
3313pub fn validate_remote_base(path: &str) -> Result<String, String> {
3314    // Expand tilde for user convenience
3315    let expanded = shellexpand::tilde(path).into_owned();
3316
3317    // Check for absolute path
3318    if !expanded.starts_with('/') {
3319        return Err(format!(
3320            "remote_base must be an absolute path, got: {}",
3321            path
3322        ));
3323    }
3324
3325    // Check for path traversal attempts
3326    if expanded.contains("..") {
3327        return Err(format!(
3328            "remote_base must not contain path traversal (..): {}",
3329            path
3330        ));
3331    }
3332
3333    // Normalize: remove trailing slashes
3334    let normalized = expanded.trim_end_matches('/').to_string();
3335
3336    // Safety checks
3337    if normalized.is_empty() || normalized == "/" {
3338        return Err("remote_base cannot be the root directory (safety restriction)".to_string());
3339    }
3340
3341    // Enforce at least 2 levels deep (e.g. /tmp/rch, /home/user/rch)
3342    // /tmp -> depth 1 (unsafe for recursive delete)
3343    // /tmp/rch -> depth 2 (safe)
3344    let components: Vec<&str> = normalized.split('/').filter(|c| !c.is_empty()).collect();
3345    if components.len() < 2 {
3346        return Err(format!(
3347            "remote_base must be at least 2 levels deep (e.g. /tmp/rch), got: {}",
3348            normalized
3349        ));
3350    }
3351
3352    Ok(normalized)
3353}
3354
3355fn default_circuit_failure_threshold() -> u32 {
3356    3
3357}
3358
3359fn default_circuit_success_threshold() -> u32 {
3360    2
3361}
3362
3363fn default_circuit_error_rate_threshold() -> f64 {
3364    0.5
3365}
3366
3367fn default_circuit_window_secs() -> u64 {
3368    60
3369}
3370
3371fn default_circuit_open_cooldown_secs() -> u64 {
3372    30
3373}
3374
3375fn default_circuit_half_open_max_probes() -> u32 {
3376    1
3377}
3378
3379fn default_circuit_command_failure_threshold() -> u32 {
3380    2
3381}
3382
3383fn default_circuit_open_cooldown_max_secs() -> u64 {
3384    // 30s base doubled four times: 30, 60, 120, 240, 480.
3385    480
3386}
3387
3388fn default_true() -> bool {
3389    true
3390}
3391
3392fn default_log_level() -> String {
3393    "info".to_string()
3394}
3395
3396pub fn default_socket_path() -> String {
3397    // Prefer XDG_RUNTIME_DIR if available (per-user runtime directory).
3398    if let Ok(runtime_dir) = std::env::var("XDG_RUNTIME_DIR")
3399        && !runtime_dir.trim().is_empty()
3400    {
3401        let path = PathBuf::from(runtime_dir).join("rch.sock");
3402        return path.to_string_lossy().to_string();
3403    }
3404
3405    // Fallback to ~/.cache/rch/rch.sock (persistent across reboots).
3406    if let Some(cache_dir) = dirs::cache_dir() {
3407        let rch_cache = cache_dir.join("rch");
3408        let _ = std::fs::create_dir_all(&rch_cache);
3409        return rch_cache.join("rch.sock").to_string_lossy().to_string();
3410    }
3411
3412    // Last resort: /tmp/rch.sock
3413    "/tmp/rch.sock".to_string()
3414}
3415
3416#[allow(dead_code)]
3417fn default_output_visibility() -> OutputVisibility {
3418    OutputVisibility::None
3419}
3420
3421fn default_confidence() -> f64 {
3422    0.85
3423}
3424
3425fn default_min_local_time() -> u64 {
3426    2000
3427}
3428
3429/// Default speedup threshold: 1.2 (require 20% speedup to offload).
3430/// This is conservative to avoid offloading builds that would be only
3431/// marginally faster remotely when accounting for transfer overhead.
3432fn default_speedup_threshold() -> f64 {
3433    1.2
3434}
3435
3436fn default_compression() -> u32 {
3437    3
3438}
3439
3440/// Default maximum file size for artifact verification (100MB).
3441fn default_verify_max_size() -> u64 {
3442    100 * 1024 * 1024 // 100MB
3443}
3444
3445/// Default minimum compression level for adaptive mode.
3446fn default_min_compression() -> u32 {
3447    1
3448}
3449
3450/// Default maximum compression level for adaptive mode.
3451fn default_max_compression() -> u32 {
3452    9
3453}
3454
3455fn default_excludes() -> Vec<String> {
3456    vec![
3457        // Rust build artifacts
3458        "target/".to_string(),
3459        "*.rlib".to_string(),
3460        "*.rmeta".to_string(),
3461        // Git metadata is large, often permission-sensitive on workers, and
3462        // a partial `.git/` tree is not reliable for build scripts anyway.
3463        ".git/".to_string(),
3464        // Node.js / Bun dependencies (massive, reinstalled on worker)
3465        "node_modules/".to_string(),
3466        // Bun cache and runtime files
3467        ".bun/".to_string(),
3468        // npm / pnpm caches
3469        ".npm/".to_string(),
3470        ".pnpm-store/".to_string(),
3471        // Common build output directories
3472        "dist/".to_string(),
3473        "build/".to_string(),
3474        // Framework-specific caches
3475        ".next/".to_string(),
3476        ".nuxt/".to_string(),
3477        ".turbo/".to_string(),
3478        ".parcel-cache/".to_string(),
3479        // Beads runtime state (local issue DB + lock/temp files); not needed remotely.
3480        ".beads/".to_string(),
3481        // Local diagnostic runs may contain host-specific symlinks and are not build inputs.
3482        ".doctor/".to_string(),
3483        // Coverage reports (generated during tests)
3484        "coverage/".to_string(),
3485        ".nyc_output/".to_string(),
3486        // Credentials and secrets (must never be transferred to workers)
3487        ".cargo/credentials".to_string(),
3488        ".cargo/credentials.toml".to_string(),
3489        ".env".to_string(),
3490        ".env.*".to_string(),
3491        "*.pem".to_string(),
3492        "*.key".to_string(),
3493        "credentials.json".to_string(),
3494        "secrets.json".to_string(),
3495        "secrets.yaml".to_string(),
3496        "secrets.yml".to_string(),
3497        "secrets.toml".to_string(),
3498        "secrets.env".to_string(),
3499        "secrets.txt".to_string(),
3500        "secrets.conf".to_string(),
3501        "secrets.cfg".to_string(),
3502        "secrets.properties".to_string(),
3503        "secrets.xml".to_string(),
3504        // The classes above were already covered. These are the ones
3505        // RABS's E027 source-capture seed set denies
3506        // (rabs-sandbox/src/source_capture.rs) that this list did not,
3507        // so the two halves of the product now agree about what a
3508        // secret is. Two components disagreeing on that is the kind of
3509        // gap nobody notices until one of them is the path that ran.
3510        //
3511        // rsync patterns without a `/` match a basename at any depth,
3512        // so these cover nested copies too — a key checked into a
3513        // fixtures directory is excluded exactly like one at the root.
3514        //
3515        // SSH private keys. The public halves (.pub) are deliberately
3516        // not excluded: they are not secret and some builds read them.
3517        "id_rsa".to_string(),
3518        "id_ed25519".to_string(),
3519        "id_ecdsa".to_string(),
3520        "id_dsa".to_string(),
3521        // Key/certificate container formats (*.pem and *.key are above).
3522        "*.p12".to_string(),
3523        "*.pfx".to_string(),
3524        "*.keystore".to_string(),
3525        "*.jks".to_string(),
3526        // Machine credentials and cloud/container config that carries
3527        // long-lived tokens.
3528        ".netrc".to_string(),
3529        "_netrc".to_string(),
3530        ".aws/credentials".to_string(),
3531        ".docker/config.json".to_string(),
3532        // Package-registry auth tokens. These sit in project roots far
3533        // more often than the key formats above and are the likeliest
3534        // real leak in a normal repo.
3535        ".npmrc".to_string(),
3536        ".pypirc".to_string(),
3537        // GnuPG private keyring.
3538        "secring.*".to_string(),
3539    ]
3540}
3541
3542// ============================================================================
3543// Build History Types
3544// ============================================================================
3545
3546/// Location where a build was executed.
3547#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
3548#[serde(rename_all = "snake_case")]
3549pub enum BuildLocation {
3550    /// Build executed locally.
3551    #[default]
3552    Local,
3553    /// Build executed on a remote worker.
3554    Remote,
3555}
3556
3557/// Worker health snapshot captured when a cancellation completes.
3558#[derive(Debug, Clone, Serialize, Deserialize)]
3559pub struct BuildCancellationWorkerHealth {
3560    /// Worker status at cancellation completion.
3561    pub status: String,
3562    /// Worker speed score at cancellation completion.
3563    pub speed_score: f64,
3564    /// Slots currently used on the worker.
3565    pub used_slots: u32,
3566    /// Slots currently available on the worker.
3567    pub available_slots: u32,
3568    /// Normalized pressure state.
3569    pub pressure_state: String,
3570    /// Stable pressure reason code.
3571    pub pressure_reason_code: String,
3572}
3573
3574/// Structured cancellation metadata attached to cancelled build records.
3575#[derive(Debug, Clone, Serialize, Deserialize)]
3576pub struct BuildCancellationMetadata {
3577    /// Stable cancellation operation identifier.
3578    pub operation_id: String,
3579    /// Cancellation origin (for example `user`, `timeout`).
3580    pub origin: String,
3581    /// Stable reason code for cancellation.
3582    pub reason_code: String,
3583    /// Ordered cancellation state path.
3584    #[serde(default)]
3585    pub decision_path: Vec<String>,
3586    /// Highest escalation stage reached (`term`, `remote_kill`, `sigkill`).
3587    pub escalation_stage: String,
3588    /// Number of escalation transitions taken.
3589    pub escalation_count: u32,
3590    /// Whether remote kill was attempted.
3591    pub remote_kill_attempted: bool,
3592    /// Whether cleanup completed successfully.
3593    pub cleanup_ok: bool,
3594    /// Whether active build history was successfully claimed/cancelled.
3595    pub history_cancelled: bool,
3596    /// Final cancellation state.
3597    pub final_state: String,
3598    /// Worker health snapshot at completion.
3599    #[serde(default, skip_serializing_if = "Option::is_none")]
3600    pub worker_health: Option<BuildCancellationWorkerHealth>,
3601}
3602
3603/// Record of a completed build.
3604#[derive(Debug, Clone, Serialize, Deserialize)]
3605pub struct BuildRecord {
3606    /// Unique build identifier.
3607    pub id: u64,
3608    /// When the build started (ISO 8601 timestamp).
3609    pub started_at: String,
3610    /// When the build completed (ISO 8601 timestamp).
3611    pub completed_at: String,
3612    /// Project identifier (usually directory name or hash).
3613    pub project_id: String,
3614    /// Worker that executed the build (None if local).
3615    pub worker_id: Option<String>,
3616    /// Full command executed.
3617    pub command: String,
3618    /// Exit code (0 = success).
3619    pub exit_code: i32,
3620    /// Duration in milliseconds.
3621    pub duration_ms: u64,
3622    /// Build location (local or remote).
3623    pub location: BuildLocation,
3624    /// Bytes transferred (if remote).
3625    pub bytes_transferred: Option<u64>,
3626    /// Optional timing breakdown for the pipeline.
3627    #[serde(default)]
3628    pub timing: Option<CommandTimingBreakdown>,
3629    /// Structured cancellation metadata for cancelled builds.
3630    #[serde(default, skip_serializing_if = "Option::is_none")]
3631    pub cancellation: Option<BuildCancellationMetadata>,
3632}
3633
3634/// Input payload for recording a completed build.
3635#[derive(Debug, Clone, Serialize, Deserialize)]
3636pub struct BuildRecordInput {
3637    /// When the build started (ISO 8601 timestamp).
3638    pub started_at: String,
3639    /// When the build completed (ISO 8601 timestamp).
3640    pub completed_at: String,
3641    /// Project identifier (usually directory name or hash).
3642    pub project_id: String,
3643    /// Worker that executed the build (None if local).
3644    pub worker_id: Option<String>,
3645    /// Full command executed.
3646    pub command: String,
3647    /// Exit code (0 = success).
3648    pub exit_code: i32,
3649    /// Duration in milliseconds.
3650    pub duration_ms: u64,
3651    /// Build location (local or remote).
3652    pub location: BuildLocation,
3653    /// Bytes transferred (if remote).
3654    pub bytes_transferred: Option<u64>,
3655    /// Optional timing breakdown for the pipeline.
3656    #[serde(default)]
3657    pub timing: Option<CommandTimingBreakdown>,
3658    /// Structured cancellation metadata for cancelled builds.
3659    #[serde(default, skip_serializing_if = "Option::is_none")]
3660    pub cancellation: Option<BuildCancellationMetadata>,
3661}
3662
3663impl BuildRecordInput {
3664    /// Convert input payload into a build record with a supplied ID.
3665    pub fn into_record(self, id: u64) -> BuildRecord {
3666        BuildRecord {
3667            id,
3668            started_at: self.started_at,
3669            completed_at: self.completed_at,
3670            project_id: self.project_id,
3671            worker_id: self.worker_id,
3672            command: self.command,
3673            exit_code: self.exit_code,
3674            duration_ms: self.duration_ms,
3675            location: self.location,
3676            bytes_transferred: self.bytes_transferred,
3677            timing: self.timing,
3678            cancellation: self.cancellation,
3679        }
3680    }
3681}
3682
3683/// Aggregate terminal-command statistics for build history.
3684///
3685/// These counts do not infer whether compilation completed before a failing
3686/// test or interrupted command. The denominator includes every stored record.
3687#[derive(Debug, Clone, Default, Serialize, Deserialize)]
3688pub struct BuildStats {
3689    /// Total number of builds in history.
3690    pub total_builds: usize,
3691    /// Number of successfully completed commands (exit_code == 0).
3692    pub success_count: usize,
3693    /// Number of unsuccessful commands, including interruption (exit_code != 0).
3694    pub failure_count: usize,
3695    /// Number of remote builds.
3696    pub remote_count: usize,
3697    /// Number of local builds.
3698    pub local_count: usize,
3699    /// Average build duration in milliseconds.
3700    pub avg_duration_ms: u64,
3701}
3702
3703/// Saved time statistics from remote builds.
3704///
3705/// Tracks estimated time savings from offloading builds to remote workers.
3706/// Estimates are only produced from an observed local baseline; without one the
3707/// stats carry measured remote durations with `estimate_basis = "none"` and
3708/// zero estimated savings rather than a fabricated speedup factor.
3709#[derive(Debug, Clone, Default, Serialize, Deserialize)]
3710pub struct SavedTimeStats {
3711    /// Total time spent on remote builds (milliseconds).
3712    pub total_remote_duration_ms: u64,
3713    /// Estimated total time if those builds ran locally (milliseconds).
3714    /// Zero when no local baseline exists.
3715    pub estimated_local_duration_ms: u64,
3716    /// Time saved by offloading (milliseconds).
3717    /// Computed as max(0, estimated_local - remote); zero without a baseline.
3718    pub time_saved_ms: u64,
3719    /// Number of remote builds included in calculation.
3720    pub builds_counted: usize,
3721    /// Average speedup factor (local_estimate / remote_duration).
3722    /// A value of 2.0 means remote builds are ~2x faster than local.
3723    /// Zero when the estimate basis is "none".
3724    pub avg_speedup: f64,
3725    /// Total time saved today (milliseconds).
3726    pub today_saved_ms: u64,
3727    /// Total time saved this week (milliseconds).
3728    pub week_saved_ms: u64,
3729    /// How the local-time estimate was derived: "observed_local_mean" when
3730    /// successful local builds back the baseline, "none" otherwise.
3731    #[serde(default)]
3732    pub estimate_basis: String,
3733    /// Number of successful local builds backing the baseline (0 for "none").
3734    #[serde(default)]
3735    pub local_baseline_builds: usize,
3736}
3737
3738// ============================================================================
3739// Compilation Timing and Metrics
3740// ============================================================================
3741
3742use std::collections::VecDeque;
3743use std::time::{Duration, Instant};
3744
3745/// Breakdown of timing for each compilation phase.
3746#[derive(Debug, Clone, Default, Serialize, Deserialize)]
3747pub struct CompilationTimingBreakdown {
3748    /// Time to sync source files to worker.
3749    #[serde(with = "duration_millis")]
3750    pub rsync_up: Duration,
3751    /// Time for cargo build on worker.
3752    #[serde(with = "duration_millis")]
3753    pub remote_build: Duration,
3754    /// Time to sync artifacts back.
3755    #[serde(with = "duration_millis")]
3756    pub rsync_down: Duration,
3757    /// Total end-to-end latency.
3758    #[serde(with = "duration_millis")]
3759    pub total: Duration,
3760}
3761
3762/// Timing breakdown for a single command pipeline.
3763///
3764/// Uses optional durations so missing phases serialize as `null`.
3765#[derive(Debug, Clone, Default, Serialize, Deserialize)]
3766pub struct CommandTimingBreakdown {
3767    /// Time spent classifying the command.
3768    #[serde(with = "option_duration_millis")]
3769    pub classify: Option<Duration>,
3770    /// Time spent selecting a worker.
3771    #[serde(with = "option_duration_millis")]
3772    pub select: Option<Duration>,
3773    /// Time spent syncing source files to the worker.
3774    #[serde(with = "option_duration_millis")]
3775    pub sync_up: Option<Duration>,
3776    /// Time spent executing the remote command.
3777    #[serde(with = "option_duration_millis")]
3778    pub exec: Option<Duration>,
3779    /// Time spent syncing artifacts back.
3780    #[serde(with = "option_duration_millis")]
3781    pub sync_down: Option<Duration>,
3782    /// Time spent on cleanup (slot release, bookkeeping).
3783    #[serde(with = "option_duration_millis")]
3784    pub cleanup: Option<Duration>,
3785    /// Total end-to-end time for the pipeline.
3786    #[serde(with = "option_duration_millis")]
3787    pub total: Option<Duration>,
3788}
3789
3790mod duration_millis {
3791    use serde::{Deserialize, Deserializer, Serialize, Serializer};
3792    use std::time::Duration;
3793
3794    pub fn serialize<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
3795    where
3796        S: Serializer,
3797    {
3798        duration.as_millis().serialize(serializer)
3799    }
3800
3801    pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error>
3802    where
3803        D: Deserializer<'de>,
3804    {
3805        let millis = u64::deserialize(deserializer)?;
3806        Ok(Duration::from_millis(millis))
3807    }
3808}
3809
3810/// Comprehensive metrics for a single compilation.
3811#[derive(Debug, Clone, Serialize, Deserialize)]
3812pub struct CompilationMetrics {
3813    /// Project identifier (usually directory name or hash).
3814    pub project_id: String,
3815    /// Worker that performed the compilation.
3816    pub worker_id: String,
3817    /// When the compilation occurred.
3818    pub timestamp: chrono::DateTime<chrono::Utc>,
3819    /// Timing breakdown for each phase.
3820    pub timing: CompilationTimingBreakdown,
3821    /// Local build time for comparison (if available).
3822    #[serde(with = "option_duration_millis")]
3823    pub local_build_time: Option<Duration>,
3824    /// Speedup ratio (local_time / remote_time).
3825    pub speedup: Option<f64>,
3826    /// Number of files synced to worker.
3827    pub files_synced: u64,
3828    /// Total bytes transferred.
3829    pub bytes_transferred: u64,
3830    /// Exit code from compilation.
3831    pub exit_code: i32,
3832    /// Whether compilation succeeded.
3833    pub success: bool,
3834}
3835
3836mod option_duration_millis {
3837    use serde::{Deserialize, Deserializer, Serialize, Serializer};
3838    use std::time::Duration;
3839
3840    pub fn serialize<S>(duration: &Option<Duration>, serializer: S) -> Result<S::Ok, S::Error>
3841    where
3842        S: Serializer,
3843    {
3844        match duration {
3845            Some(d) => Some(d.as_millis() as u64).serialize(serializer),
3846            None => Option::<u64>::None.serialize(serializer),
3847        }
3848    }
3849
3850    pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Duration>, D::Error>
3851    where
3852        D: Deserializer<'de>,
3853    {
3854        let opt: Option<u64> = Option::deserialize(deserializer)?;
3855        Ok(opt.map(Duration::from_millis))
3856    }
3857}
3858
3859impl CompilationMetrics {
3860    /// Calculate speedup based on local vs remote build time.
3861    pub fn calculate_speedup(&mut self) {
3862        if let Some(local) = self.local_build_time
3863            && self.timing.total.as_millis() > 0
3864        {
3865            self.speedup = Some(local.as_secs_f64() / self.timing.total.as_secs_f64());
3866        }
3867    }
3868
3869    /// Check if remote compilation was beneficial (faster than local).
3870    pub fn is_beneficial(&self) -> bool {
3871        self.speedup.map(|s| s > 1.0).unwrap_or(false)
3872    }
3873}
3874
3875impl Default for CompilationMetrics {
3876    fn default() -> Self {
3877        Self {
3878            project_id: String::new(),
3879            worker_id: String::new(),
3880            timestamp: chrono::Utc::now(),
3881            timing: CompilationTimingBreakdown::default(),
3882            local_build_time: None,
3883            speedup: None,
3884            files_synced: 0,
3885            bytes_transferred: 0,
3886            exit_code: 0,
3887            success: true,
3888        }
3889    }
3890}
3891
3892/// Timer for tracking compilation phases.
3893///
3894/// Use this to measure the duration of each phase of remote compilation.
3895///
3896/// # Example
3897/// ```ignore
3898/// let mut timer = CompilationTimer::new("my-project", "worker-1");
3899/// // ... rsync source files ...
3900/// timer.end_rsync_up();
3901/// // ... run cargo build ...
3902/// timer.end_remote_build();
3903/// // ... rsync artifacts back ...
3904/// timer.end_rsync_down();
3905/// let metrics = timer.finish(0, 100, 1_000_000);
3906/// ```
3907#[derive(Debug)]
3908pub struct CompilationTimer {
3909    project_id: String,
3910    worker_id: String,
3911    start: Instant,
3912    phase_start: Instant,
3913    rsync_up: Option<Duration>,
3914    remote_build: Option<Duration>,
3915    rsync_down: Option<Duration>,
3916}
3917
3918impl CompilationTimer {
3919    /// Create a new timer for a compilation.
3920    pub fn new(project_id: &str, worker_id: &str) -> Self {
3921        let now = Instant::now();
3922        Self {
3923            project_id: project_id.to_string(),
3924            worker_id: worker_id.to_string(),
3925            start: now,
3926            phase_start: now,
3927            rsync_up: None,
3928            remote_build: None,
3929            rsync_down: None,
3930        }
3931    }
3932
3933    /// Mark the end of the rsync upload phase.
3934    pub fn end_rsync_up(&mut self) {
3935        self.rsync_up = Some(self.phase_start.elapsed());
3936        self.phase_start = Instant::now();
3937        tracing::info!(
3938            rsync_up_ms = %self.rsync_up.unwrap().as_millis(),
3939            "TIMING: rsync_up completed"
3940        );
3941    }
3942
3943    /// Mark the end of the remote build phase.
3944    pub fn end_remote_build(&mut self) {
3945        self.remote_build = Some(self.phase_start.elapsed());
3946        self.phase_start = Instant::now();
3947        tracing::info!(
3948            remote_build_ms = %self.remote_build.unwrap().as_millis(),
3949            "TIMING: remote_build completed"
3950        );
3951    }
3952
3953    /// Mark the end of the rsync download phase.
3954    pub fn end_rsync_down(&mut self) {
3955        self.rsync_down = Some(self.phase_start.elapsed());
3956        tracing::info!(
3957            rsync_down_ms = %self.rsync_down.unwrap().as_millis(),
3958            "TIMING: rsync_down completed"
3959        );
3960    }
3961
3962    /// Finish timing and produce metrics.
3963    pub fn finish(self, exit_code: i32, files: u64, bytes: u64) -> CompilationMetrics {
3964        let total = self.start.elapsed();
3965        tracing::info!(
3966            total_ms = %total.as_millis(),
3967            exit_code = %exit_code,
3968            "TIMING: compilation completed"
3969        );
3970
3971        CompilationMetrics {
3972            project_id: self.project_id,
3973            worker_id: self.worker_id,
3974            timestamp: chrono::Utc::now(),
3975            timing: CompilationTimingBreakdown {
3976                rsync_up: self.rsync_up.unwrap_or_default(),
3977                remote_build: self.remote_build.unwrap_or_default(),
3978                rsync_down: self.rsync_down.unwrap_or_default(),
3979                total,
3980            },
3981            local_build_time: None,
3982            speedup: None,
3983            files_synced: files,
3984            bytes_transferred: bytes,
3985            exit_code,
3986            success: exit_code == 0,
3987        }
3988    }
3989}
3990
3991/// Aggregator for compilation metrics with statistics.
3992#[derive(Debug)]
3993pub struct MetricsAggregator {
3994    history: VecDeque<CompilationMetrics>,
3995    max_history: usize,
3996}
3997
3998impl MetricsAggregator {
3999    /// Create a new aggregator with a maximum history size.
4000    pub fn new(max_history: usize) -> Self {
4001        Self {
4002            history: VecDeque::with_capacity(max_history),
4003            max_history,
4004        }
4005    }
4006
4007    /// Record a new compilation's metrics.
4008    pub fn record(&mut self, metrics: CompilationMetrics) {
4009        if self.history.len() >= self.max_history {
4010            self.history.pop_front();
4011        }
4012        self.history.push_back(metrics);
4013    }
4014
4015    /// Get the average speedup across all recorded compilations.
4016    pub fn average_speedup(&self) -> Option<f64> {
4017        let speedups: Vec<f64> = self.history.iter().filter_map(|m| m.speedup).collect();
4018
4019        if speedups.is_empty() {
4020            None
4021        } else {
4022            Some(speedups.iter().sum::<f64>() / speedups.len() as f64)
4023        }
4024    }
4025
4026    /// Get the p50 (median) total compilation time.
4027    pub fn p50_total_time(&self) -> Option<Duration> {
4028        self.percentile_total_time(0.50)
4029    }
4030
4031    /// Get the p95 total compilation time.
4032    pub fn p95_total_time(&self) -> Option<Duration> {
4033        self.percentile_total_time(0.95)
4034    }
4035
4036    /// Get the p99 total compilation time.
4037    pub fn p99_total_time(&self) -> Option<Duration> {
4038        self.percentile_total_time(0.99)
4039    }
4040
4041    /// Get a percentile of total compilation times.
4042    ///
4043    /// Uses the "inclusive" percentile method (matching numpy.percentile, Excel PERCENTILE.INC):
4044    /// index = (n - 1) * percentile, where n is the number of samples.
4045    fn percentile_total_time(&self, percentile: f64) -> Option<Duration> {
4046        let mut times: Vec<_> = self.history.iter().map(|m| m.timing.total).collect();
4047
4048        if times.is_empty() {
4049            return None;
4050        }
4051
4052        times.sort();
4053        // Use inclusive percentile: index = (n - 1) * p
4054        // For p50 with 100 items: (99 * 0.50) = 49.5 -> 49 (true median position)
4055        // For p99 with 100 items: (99 * 0.99) = 98.01 -> 98
4056        let idx = (((times.len() - 1) as f64 * percentile).round() as usize).min(times.len() - 1);
4057        Some(times[idx])
4058    }
4059
4060    /// Get the success rate as a percentage.
4061    pub fn success_rate(&self) -> f64 {
4062        if self.history.is_empty() {
4063            return 100.0;
4064        }
4065        let successes = self.history.iter().filter(|m| m.success).count();
4066        (successes as f64 / self.history.len() as f64) * 100.0
4067    }
4068
4069    /// Get the number of recorded compilations.
4070    pub fn count(&self) -> usize {
4071        self.history.len()
4072    }
4073
4074    /// Get all recorded metrics.
4075    pub fn metrics(&self) -> &VecDeque<CompilationMetrics> {
4076        &self.history
4077    }
4078
4079    /// Clear all recorded metrics.
4080    pub fn clear(&mut self) {
4081        self.history.clear();
4082    }
4083}
4084
4085impl Default for MetricsAggregator {
4086    fn default() -> Self {
4087        Self::new(1000)
4088    }
4089}
4090
4091#[cfg(test)]
4092mod tests {
4093    use super::*;
4094    use crate::test_guard;
4095
4096    #[test]
4097    fn test_declared_os_reads_reserved_tag() {
4098        assert_eq!(
4099            declared_os(&["rust".to_string(), "os:windows".to_string()]),
4100            Some("windows".to_string())
4101        );
4102        assert_eq!(
4103            declared_os(&["os:WINDOWS".to_string()]),
4104            Some("windows".to_string())
4105        );
4106    }
4107
4108    #[test]
4109    fn test_declared_os_absent_for_ordinary_tags() {
4110        // Every historical worker: no reserved tag, so no exclusivity and the
4111        // worker stays a candidate for any job.
4112        assert_eq!(declared_os(&[]), None);
4113        assert_eq!(
4114            declared_os(&["rust".to_string(), "bun".to_string(), "go".to_string()]),
4115            None
4116        );
4117        // A malformed declaration must not silently fence the worker off.
4118        assert_eq!(declared_os(&["os:".to_string()]), None);
4119        assert_eq!(declared_os(&["os:  ".to_string()]), None);
4120    }
4121
4122    #[test]
4123    fn test_os_tag_roundtrips_through_declared_os() {
4124        for os in ["windows", "Darwin", " linux "] {
4125            let tag = os_tag(os);
4126            assert_eq!(
4127                declared_os(std::slice::from_ref(&tag)).as_deref(),
4128                Some(os.trim().to_ascii_lowercase().as_str())
4129            );
4130        }
4131    }
4132
4133    /// Verdict-webhook config must NEVER carry an inline secret. Operators
4134    /// reference secrets by env-var name (`*_env`); the resolved value is
4135    /// read at dispatch time and never persisted to the cache or config TOML.
4136    /// This guards against a future careless `pub secret: String` addition.
4137    #[test]
4138    fn test_no_inline_secret_fields() {
4139        let endpoint = DoctorWebhookEndpoint {
4140            name: "ops".to_string(),
4141            url: "https://example.com/hook".to_string(),
4142            format: DoctorWebhookFormat::Pagerduty,
4143            on_transitions: vec!["any_to_failing".to_string()],
4144            retry_max: 3,
4145            retry_backoff_ms: 200,
4146            timeout_ms: 5000,
4147            enabled: true,
4148            routing_key_env: Some("RCH_PAGERDUTY_ROUTING_KEY".to_string()),
4149            bearer_token_env: Some("RCH_WEBHOOK_BEARER".to_string()),
4150            signing_secret_env: Some("RCH_WEBHOOK_HMAC".to_string()),
4151        };
4152        // Serialize to a JSON object and inspect the *keys*. The forbidden
4153        // field names must be entirely absent; only `*_env` references survive.
4154        let value = serde_json::to_value(&endpoint).unwrap();
4155        let obj = value.as_object().expect("endpoint serializes to an object");
4156        for forbidden in ["secret", "routing_key", "bearer_token", "password", "token"] {
4157            assert!(
4158                !obj.contains_key(forbidden),
4159                "endpoint config must not expose an inline `{forbidden}` field"
4160            );
4161        }
4162        assert!(obj.contains_key("routing_key_env"));
4163        assert!(obj.contains_key("bearer_token_env"));
4164        assert!(obj.contains_key("signing_secret_env"));
4165    }
4166
4167    #[test]
4168    fn test_webhook_format_toml_spelling() {
4169        // The config TOML uses snake_case spellings. TOML documents are tables
4170        // at the root, so a bare enum cannot be (de)serialized standalone —
4171        // verify the wire spelling through a `format = "..."` table field, the
4172        // shape it actually appears in within the config.
4173        #[derive(serde::Serialize, serde::Deserialize, PartialEq, Debug)]
4174        struct FormatWrap {
4175            format: DoctorWebhookFormat,
4176        }
4177        let rendered = toml::to_string(&FormatWrap {
4178            format: DoctorWebhookFormat::GenericJson,
4179        })
4180        .unwrap();
4181        assert!(
4182            rendered.contains("format = \"generic_json\""),
4183            "unexpected TOML: {rendered}"
4184        );
4185        let slack: FormatWrap = toml::from_str("format = \"slack\"").unwrap();
4186        assert_eq!(slack.format, DoctorWebhookFormat::Slack);
4187        let pd: FormatWrap = toml::from_str("format = \"pagerduty\"").unwrap();
4188        assert_eq!(pd.format, DoctorWebhookFormat::Pagerduty);
4189    }
4190
4191    #[test]
4192    fn test_doctor_webhooks_defaults() {
4193        let cfg = DoctorWebhooksConfig::default();
4194        assert!(cfg.endpoints.is_empty());
4195        assert_eq!(cfg.max_inflight, 16);
4196        // RchConfig wires the section in with serde(default), so an empty
4197        // config still yields an empty (never-firing) webhook surface.
4198        let rch: RchConfig = toml::from_str("").unwrap();
4199        assert!(rch.doctor.webhooks.endpoints.is_empty());
4200    }
4201
4202    #[test]
4203    fn test_doctor_webhook_endpoint_parses_from_toml() {
4204        let toml_src = r#"
4205[doctor.webhooks]
4206max_inflight = 8
4207
4208[[doctor.webhooks.endpoints]]
4209name = "ops-slack"
4210url = "https://hooks.slack.com/services/T0/B0/x"
4211format = "slack"
4212on_transitions = ["healthy_to_degraded", "any_to_failing"]
4213retry_max = 2
4214"#;
4215        let cfg: RchConfig = toml::from_str(toml_src).unwrap();
4216        assert_eq!(cfg.doctor.webhooks.max_inflight, 8);
4217        assert_eq!(cfg.doctor.webhooks.endpoints.len(), 1);
4218        let ep = &cfg.doctor.webhooks.endpoints[0];
4219        assert_eq!(ep.name, "ops-slack");
4220        assert_eq!(ep.format, DoctorWebhookFormat::Slack);
4221        assert_eq!(ep.on_transitions.len(), 2);
4222        assert_eq!(ep.retry_max, 2);
4223        // Defaults filled in for omitted fields.
4224        assert_eq!(ep.retry_backoff_ms, 200);
4225        assert_eq!(ep.timeout_ms, 5000);
4226        assert!(ep.enabled);
4227    }
4228
4229    fn assert_golden_json(
4230        actual: serde_json::Value,
4231        expected: &serde_json::Value,
4232        fixture_path: &str,
4233    ) {
4234        if &actual == expected {
4235            return;
4236        }
4237
4238        let actual_json = serde_json::to_string_pretty(&actual).expect("actual JSON renders");
4239        let expected_json = serde_json::to_string_pretty(expected).expect("expected JSON renders");
4240        assert_eq!(
4241            &actual,
4242            expected,
4243            "golden mismatch in {fixture_path}\n\
4244             expected_blake3={}\n\
4245             actual_blake3={}\n\
4246             bless path: review the semantic diff, update the fixture expected_* field, \
4247             and record both hashes in the Beads closeout.\n\
4248             expected:\n{expected_json}\n\
4249             actual:\n{actual_json}",
4250            blake3::hash(expected_json.as_bytes()),
4251            blake3::hash(actual_json.as_bytes())
4252        );
4253    }
4254
4255    #[test]
4256    fn test_circuit_state_default() {
4257        let _guard = test_guard!();
4258        assert_eq!(CircuitState::default(), CircuitState::Closed);
4259    }
4260
4261    #[test]
4262    fn test_circuit_config_defaults() {
4263        let _guard = test_guard!();
4264        let config = CircuitBreakerConfig::default();
4265        assert_eq!(config.failure_threshold, 3);
4266        assert_eq!(config.success_threshold, 2);
4267        assert_eq!(config.error_rate_threshold, 0.5);
4268        assert_eq!(config.window_secs, 60);
4269        assert_eq!(config.open_cooldown_secs, 30);
4270        assert_eq!(config.half_open_max_probes, 1);
4271    }
4272
4273    #[test]
4274    fn test_rch_config_has_circuit_defaults() {
4275        let _guard = test_guard!();
4276        let config = RchConfig::default();
4277        assert_eq!(config.circuit.failure_threshold, 3);
4278    }
4279
4280    #[test]
4281    fn test_circuit_config_serde_roundtrip() {
4282        let _guard = test_guard!();
4283        let config = CircuitBreakerConfig {
4284            failure_threshold: 5,
4285            success_threshold: 3,
4286            error_rate_threshold: 0.75,
4287            window_secs: 120,
4288            open_cooldown_secs: 45,
4289            half_open_max_probes: 2,
4290            command_failure_threshold: 4,
4291            open_cooldown_max_secs: 900,
4292        };
4293        let json = serde_json::to_string(&config).unwrap();
4294        let parsed: CircuitBreakerConfig = serde_json::from_str(&json).unwrap();
4295        assert_eq!(parsed.command_failure_threshold, 4);
4296        assert_eq!(parsed.open_cooldown_max_secs, 900);
4297        assert_eq!(parsed.failure_threshold, 5);
4298        assert_eq!(parsed.success_threshold, 3);
4299        assert_eq!(parsed.error_rate_threshold, 0.75);
4300        assert_eq!(parsed.window_secs, 120);
4301        assert_eq!(parsed.open_cooldown_secs, 45);
4302        assert_eq!(parsed.half_open_max_probes, 2);
4303    }
4304
4305    // ========================================================================
4306    // WorkerCapabilities Tests
4307    // ========================================================================
4308
4309    #[test]
4310    fn test_worker_capabilities_default() {
4311        let _guard = test_guard!();
4312        let caps = WorkerCapabilities::default();
4313        assert!(caps.rustc_version.is_none());
4314        assert!(caps.rustup_toolchains.is_empty());
4315        assert!(caps.rustup_components.is_empty());
4316        assert!(caps.bun_version.is_none());
4317        assert!(caps.node_version.is_none());
4318        assert!(caps.npm_version.is_none());
4319        assert!(caps.projects_root_ok.is_none());
4320        assert!(caps.projects_root_issue.is_none());
4321        assert!(caps.projects_root_checked_at_unix_ms.is_none());
4322    }
4323
4324    #[test]
4325    fn test_worker_capabilities_new() {
4326        let _guard = test_guard!();
4327        let caps = WorkerCapabilities::new();
4328        assert!(caps.rustc_version.is_none());
4329        assert!(!caps.has_rust());
4330        assert!(!caps.has_bun());
4331        assert!(!caps.has_node());
4332        assert!(caps.is_topology_healthy().is_none());
4333    }
4334
4335    #[test]
4336    fn test_worker_capabilities_has_rust() {
4337        let _guard = test_guard!();
4338        let mut caps = WorkerCapabilities::new();
4339        assert!(!caps.has_rust());
4340
4341        caps.rustc_version = Some("rustc 1.76.0 (07dca489a 2024-02-04)".to_string());
4342        assert!(caps.has_rust());
4343    }
4344
4345    #[test]
4346    fn test_worker_capabilities_components_are_toolchain_scoped_and_exact() {
4347        let _guard = test_guard!();
4348        let caps = WorkerCapabilities {
4349            rustup_components: vec![
4350                "stable-x86_64-unknown-linux-gnu:clippy".to_string(),
4351                "nightly-2026-07-05-x86_64-unknown-linux-gnu:rustfmt".to_string(),
4352                "beta-x86_64-unknown-linux-gnu:clippy-preview".to_string(),
4353            ],
4354            ..Default::default()
4355        };
4356
4357        assert!(caps.has_rustup_component("stable", "clippy"));
4358        assert!(caps.has_rustup_component("nightly-2026-07-05", "rustfmt"));
4359        assert!(!caps.has_rustup_component("nightly-2026-07-05", "clippy"));
4360        assert!(!caps.has_rustup_component("beta", "clippy"));
4361        assert!(!caps.has_rustup_component("nightly-2026-07-0", "rustfmt"));
4362    }
4363
4364    #[test]
4365    fn test_worker_capabilities_has_bun() {
4366        let _guard = test_guard!();
4367        let mut caps = WorkerCapabilities::new();
4368        assert!(!caps.has_bun());
4369
4370        caps.bun_version = Some("1.0.25".to_string());
4371        assert!(caps.has_bun());
4372    }
4373
4374    #[test]
4375    fn test_worker_capabilities_has_nix() {
4376        let _guard = test_guard!();
4377        let mut caps = WorkerCapabilities::new();
4378        assert!(!caps.has_nix());
4379
4380        caps.nix_version = Some("nix (Nix) 2.24.9".to_string());
4381        assert!(caps.has_nix());
4382    }
4383
4384    #[test]
4385    fn test_worker_capabilities_has_zig_requires_both_halves() {
4386        let _guard = test_guard!();
4387        let mut caps = WorkerCapabilities::new();
4388        assert!(!caps.has_zig());
4389
4390        // `zig` alone cannot run `cargo zigbuild` — cargo would report
4391        // `no such command: 'zigbuild'`.
4392        caps.zig_version = Some("0.14.1".to_string());
4393        assert!(!caps.has_zig());
4394
4395        // cargo-zigbuild alone cannot link — it shells out to `zig cc`.
4396        caps.zig_version = None;
4397        caps.cargo_zigbuild_version = Some("cargo-zigbuild 0.23.0".to_string());
4398        assert!(!caps.has_zig());
4399
4400        caps.zig_version = Some("0.14.1".to_string());
4401        assert!(caps.has_zig());
4402    }
4403
4404    #[test]
4405    fn test_nix_build_uses_generous_timeout() {
4406        let _guard = test_guard!();
4407        let cfg = CompilationConfig::default();
4408        // Nix builds get the long test-timeout bucket, not the short build timeout.
4409        assert_eq!(
4410            cfg.timeout_for_kind(Some(CompilationKind::NixBuild)),
4411            std::time::Duration::from_secs(cfg.test_timeout_sec)
4412        );
4413    }
4414
4415    #[test]
4416    fn test_job_uses_build_timeout_bucket() {
4417        let _guard = test_guard!();
4418        // Jobs (explicit `rch exec --job`) reuse the standard build-timeout
4419        // bucket, so `RCH_BUILD_TIMEOUT_SEC` governs them without new config.
4420        let cfg = CompilationConfig::default();
4421        assert_eq!(
4422            cfg.timeout_for_kind(Some(CompilationKind::Job)),
4423            std::time::Duration::from_secs(cfg.build_timeout_sec)
4424        );
4425    }
4426
4427    #[test]
4428    fn test_layer0_disabled_by_default_and_env_gated() {
4429        let _guard = test_guard!();
4430        let cfg = Layer0Config::default();
4431        assert!(!cfg.enabled);
4432        // Master switch off ⇒ nothing injects even with knobs on.
4433        let knobs_on = Layer0Config {
4434            enabled: false,
4435            release_lto_thin: true,
4436            release_codegen_units_1: true,
4437        };
4438        assert!(cfg.active_env().is_empty());
4439        assert!(knobs_on.active_env().is_empty());
4440    }
4441
4442    #[test]
4443    fn test_layer0_knobs_emit_stable_env_pairs() {
4444        let _guard = test_guard!();
4445        let lto_only = Layer0Config {
4446            enabled: true,
4447            release_lto_thin: true,
4448            release_codegen_units_1: false,
4449        };
4450        assert_eq!(
4451            lto_only.active_env(),
4452            vec![("CARGO_PROFILE_RELEASE_LTO", "thin")]
4453        );
4454        let both = Layer0Config {
4455            enabled: true,
4456            release_lto_thin: true,
4457            release_codegen_units_1: true,
4458        };
4459        assert_eq!(
4460            both.active_env(),
4461            vec![
4462                ("CARGO_PROFILE_RELEASE_LTO", "thin"),
4463                ("CARGO_PROFILE_RELEASE_CODEGEN_UNITS", "1"),
4464            ],
4465            "emission order must stay stable for deterministic baselines"
4466        );
4467    }
4468
4469    #[test]
4470    fn test_layer0_pack_version_is_recorded() {
4471        let _guard = test_guard!();
4472        // Baselines record the pack version; it must exist and be non-empty.
4473        assert!(!LAYER0_PACK_VERSION.is_empty());
4474    }
4475    #[test]
4476    fn test_default_allowlist_includes_nix() {
4477        let _guard = test_guard!();
4478        let cfg = ExecutionConfig::default();
4479        assert!(cfg.is_allowed("nix"));
4480    }
4481
4482    #[test]
4483    fn test_worker_capabilities_has_node() {
4484        let _guard = test_guard!();
4485        let mut caps = WorkerCapabilities::new();
4486        assert!(!caps.has_node());
4487
4488        caps.node_version = Some("v20.11.0".to_string());
4489        assert!(caps.has_node());
4490    }
4491
4492    #[test]
4493    fn test_worker_capabilities_multiple_runtimes() {
4494        let _guard = test_guard!();
4495        let caps = WorkerCapabilities {
4496            rustc_version: Some("rustc 1.76.0".to_string()),
4497            bun_version: Some("1.0.25".to_string()),
4498            node_version: Some("v20.11.0".to_string()),
4499            npm_version: Some("10.2.4".to_string()),
4500            ..Default::default()
4501        };
4502
4503        assert!(caps.has_rust());
4504        assert!(caps.has_bun());
4505        assert!(caps.has_node());
4506    }
4507
4508    #[test]
4509    fn test_worker_capabilities_serialization_empty() {
4510        let _guard = test_guard!();
4511        let caps = WorkerCapabilities::new();
4512        let json = serde_json::to_string(&caps).unwrap();
4513        // Empty capabilities should serialize to {}
4514        assert_eq!(json, "{}");
4515
4516        // And deserialize back correctly
4517        let parsed: WorkerCapabilities = serde_json::from_str(&json).unwrap();
4518        assert!(!parsed.has_rust());
4519        assert!(!parsed.has_bun());
4520        assert!(!parsed.has_node());
4521    }
4522
4523    #[test]
4524    fn test_worker_capabilities_serialization_with_versions() {
4525        let _guard = test_guard!();
4526        let caps = WorkerCapabilities {
4527            rustc_version: Some("rustc 1.76.0-nightly".to_string()),
4528            bun_version: Some("1.0.25".to_string()),
4529            node_version: None,
4530            npm_version: None,
4531            ..Default::default()
4532        };
4533
4534        let json = serde_json::to_string(&caps).unwrap();
4535        assert!(json.contains("rustc_version"));
4536        assert!(json.contains("rustc 1.76.0-nightly"));
4537        assert!(json.contains("bun_version"));
4538        assert!(json.contains("1.0.25"));
4539        // None fields should be skipped
4540        assert!(!json.contains("node_version"));
4541        assert!(!json.contains("npm_version"));
4542
4543        let parsed: WorkerCapabilities = serde_json::from_str(&json).unwrap();
4544        assert!(parsed.has_rust());
4545        assert!(parsed.has_bun());
4546        assert!(!parsed.has_node());
4547    }
4548
4549    #[test]
4550    fn test_worker_capabilities_deserialization_partial() {
4551        let _guard = test_guard!();
4552        // Deserialize JSON with only some fields
4553        let json = r#"{"bun_version": "1.0.0"}"#;
4554        let caps: WorkerCapabilities = serde_json::from_str(json).unwrap();
4555
4556        assert!(!caps.has_rust());
4557        assert!(caps.has_bun());
4558        assert!(!caps.has_node());
4559        assert_eq!(caps.bun_version, Some("1.0.0".to_string()));
4560    }
4561
4562    #[test]
4563    fn test_worker_capabilities_clone() {
4564        let _guard = test_guard!();
4565        let caps = WorkerCapabilities {
4566            rustc_version: Some("1.76.0".to_string()),
4567            bun_version: None,
4568            node_version: Some("v20".to_string()),
4569            npm_version: None,
4570            projects_root_ok: Some(false),
4571            projects_root_issue: Some("alias_missing".to_string()),
4572            projects_root_checked_at_unix_ms: Some(123),
4573            ..Default::default()
4574        };
4575
4576        let cloned = caps.clone();
4577        assert_eq!(cloned.rustc_version, caps.rustc_version);
4578        assert_eq!(cloned.bun_version, caps.bun_version);
4579        assert_eq!(cloned.node_version, caps.node_version);
4580        assert_eq!(cloned.npm_version, caps.npm_version);
4581        assert_eq!(cloned.projects_root_ok, caps.projects_root_ok);
4582        assert_eq!(cloned.projects_root_issue, caps.projects_root_issue);
4583        assert_eq!(
4584            cloned.projects_root_checked_at_unix_ms,
4585            caps.projects_root_checked_at_unix_ms
4586        );
4587    }
4588
4589    #[test]
4590    fn test_worker_capabilities_topology_health_helper() {
4591        let _guard = test_guard!();
4592        let mut caps = WorkerCapabilities::new();
4593        assert_eq!(caps.is_topology_healthy(), None);
4594
4595        caps.projects_root_ok = Some(true);
4596        assert_eq!(caps.is_topology_healthy(), Some(true));
4597
4598        caps.projects_root_ok = Some(false);
4599        assert_eq!(caps.is_topology_healthy(), Some(false));
4600    }
4601
4602    #[test]
4603    fn test_selection_reason_serialization() {
4604        let _guard = test_guard!();
4605        // Test that all variants serialize to snake_case
4606        assert_eq!(
4607            serde_json::to_string(&SelectionReason::Success).unwrap(),
4608            "\"success\""
4609        );
4610        assert_eq!(
4611            serde_json::to_string(&SelectionReason::NoWorkersConfigured).unwrap(),
4612            "\"no_workers_configured\""
4613        );
4614        assert_eq!(
4615            serde_json::to_string(&SelectionReason::AllWorkersUnreachable).unwrap(),
4616            "\"all_workers_unreachable\""
4617        );
4618        assert_eq!(
4619            serde_json::to_string(&SelectionReason::AllCircuitsOpen).unwrap(),
4620            "\"all_circuits_open\""
4621        );
4622        assert_eq!(
4623            serde_json::to_string(&SelectionReason::AllWorkersBusy).unwrap(),
4624            "\"all_workers_busy\""
4625        );
4626        assert_eq!(
4627            serde_json::to_string(&SelectionReason::NoWorkersPassedHealth).unwrap(),
4628            "\"no_workers_passed_health\""
4629        );
4630        assert_eq!(
4631            serde_json::to_string(&SelectionReason::AllWorkersFailedPreflight).unwrap(),
4632            "\"all_workers_failed_preflight\""
4633        );
4634        assert_eq!(
4635            serde_json::to_string(&SelectionReason::AllWorkersFailedConvergence).unwrap(),
4636            "\"all_workers_failed_convergence\""
4637        );
4638        assert_eq!(
4639            serde_json::to_string(&SelectionReason::NoAdmissibleWorkers(
4640                "critical_pressure=1,insufficient_slots=1".to_string()
4641            ))
4642            .unwrap(),
4643            "{\"no_admissible_workers\":\"critical_pressure=1,insufficient_slots=1\"}"
4644        );
4645        assert_eq!(
4646            serde_json::to_string(&SelectionReason::NoMatchingWorkers).unwrap(),
4647            "\"no_matching_workers\""
4648        );
4649    }
4650
4651    #[test]
4652    fn test_selection_reason_with_error() {
4653        let _guard = test_guard!();
4654        let reason = SelectionReason::SelectionError("test error".to_string());
4655        let json = serde_json::to_string(&reason).unwrap();
4656        assert!(json.contains("selection_error"));
4657        assert!(json.contains("test error"));
4658    }
4659
4660    #[test]
4661    fn test_selection_reason_deserialization() {
4662        let _guard = test_guard!();
4663        assert_eq!(
4664            serde_json::from_str::<SelectionReason>("\"success\"").unwrap(),
4665            SelectionReason::Success
4666        );
4667        assert_eq!(
4668            serde_json::from_str::<SelectionReason>("\"all_workers_busy\"").unwrap(),
4669            SelectionReason::AllWorkersBusy
4670        );
4671        assert_eq!(
4672            serde_json::from_str::<SelectionReason>("\"no_workers_passed_health\"").unwrap(),
4673            SelectionReason::NoWorkersPassedHealth
4674        );
4675        assert_eq!(
4676            serde_json::from_str::<SelectionReason>("\"all_workers_failed_preflight\"").unwrap(),
4677            SelectionReason::AllWorkersFailedPreflight
4678        );
4679        assert_eq!(
4680            serde_json::from_str::<SelectionReason>("\"all_workers_failed_convergence\"").unwrap(),
4681            SelectionReason::AllWorkersFailedConvergence
4682        );
4683        assert_eq!(
4684            serde_json::from_str::<SelectionReason>(
4685                "{\"no_admissible_workers\":\"critical_pressure=1\"}"
4686            )
4687            .unwrap(),
4688            SelectionReason::NoAdmissibleWorkers("critical_pressure=1".to_string())
4689        );
4690    }
4691
4692    #[test]
4693    fn test_selection_reason_display() {
4694        let _guard = test_guard!();
4695        assert_eq!(
4696            SelectionReason::Success.to_string(),
4697            "worker assigned successfully"
4698        );
4699        assert_eq!(
4700            SelectionReason::NoWorkersConfigured.to_string(),
4701            "no workers configured"
4702        );
4703        assert_eq!(
4704            SelectionReason::AllWorkersUnreachable.to_string(),
4705            "all workers unreachable"
4706        );
4707        assert_eq!(
4708            SelectionReason::AllWorkersBusy.to_string(),
4709            "all workers at capacity"
4710        );
4711        assert_eq!(
4712            SelectionReason::NoWorkersPassedHealth.to_string(),
4713            "no workers passed health thresholds"
4714        );
4715        assert_eq!(
4716            SelectionReason::AllWorkersFailedPreflight.to_string(),
4717            "all workers failed preflight checks"
4718        );
4719        assert_eq!(
4720            SelectionReason::AllWorkersFailedConvergence.to_string(),
4721            "all workers failed repo convergence checks"
4722        );
4723        assert_eq!(
4724            SelectionReason::NoAdmissibleWorkers(
4725                "critical_pressure=1,insufficient_slots=1".to_string()
4726            )
4727            .to_string(),
4728            "no admissible workers: critical_pressure=1,insufficient_slots=1"
4729        );
4730        assert_eq!(
4731            SelectionReason::SelectionError("oops".to_string()).to_string(),
4732            "selection error: oops"
4733        );
4734    }
4735
4736    #[test]
4737    fn test_selection_response_with_worker() {
4738        let _guard = test_guard!();
4739        let response = SelectionResponse {
4740            worker: Some(SelectedWorker {
4741                id: WorkerId::new("test"),
4742                host: "localhost".to_string(),
4743                user: "user".to_string(),
4744                identity_file: "~/.ssh/id_rsa".to_string(),
4745                slots_available: 8,
4746                speed_score: 75.0,
4747                declared_os: None,
4748            }),
4749            reason: SelectionReason::Success,
4750            build_id: None,
4751            diagnostics: None,
4752        };
4753
4754        let json = serde_json::to_string(&response).unwrap();
4755        assert!(json.contains("\"reason\":\"success\""));
4756        assert!(json.contains("\"id\":\"test\""));
4757    }
4758
4759    #[test]
4760    fn test_selection_response_without_worker() {
4761        let _guard = test_guard!();
4762        let response = SelectionResponse {
4763            worker: None,
4764            reason: SelectionReason::AllWorkersBusy,
4765            build_id: None,
4766            diagnostics: None,
4767        };
4768
4769        let json = serde_json::to_string(&response).unwrap();
4770        assert!(json.contains("\"worker\":null"));
4771        assert!(json.contains("\"reason\":\"all_workers_busy\""));
4772    }
4773
4774    #[test]
4775    fn golden_selection_response_with_no_workers_passed_health_diagnostics() {
4776        let _guard = test_guard!();
4777        const FIXTURE_PATH: &str =
4778            "../../tests/goldens/ft_4tp7g/selection_diagnostics_no_workers_passed_health.json";
4779        let fixture: serde_json::Value = serde_json::from_str(include_str!(
4780            "../../tests/goldens/ft_4tp7g/selection_diagnostics_no_workers_passed_health.json"
4781        ))
4782        .expect("selection diagnostics golden fixture parses");
4783        assert_eq!(
4784            fixture["schema_version"],
4785            "rch.golden.selection_diagnostics.v1"
4786        );
4787
4788        let response = SelectionResponse {
4789            worker: None,
4790            reason: SelectionReason::NoWorkersPassedHealth,
4791            build_id: None,
4792            diagnostics: Some(SelectionDiagnostics {
4793                required_runtime: RequiredRuntime::Rust,
4794                estimated_cores: 2,
4795                min_success_rate: 0.8,
4796                fallback_min_success_rate: 0.5,
4797                active_project_exclusion_count: 0,
4798                workers: vec![WorkerSelectionDiagnostic {
4799                    worker_id: WorkerId::new("fixture-worker"),
4800                    status: "healthy".to_string(),
4801                    circuit_state: "closed".to_string(),
4802                    pressure_state: "healthy".to_string(),
4803                    pressure_reason_code: "pressure.ok".to_string(),
4804                    success_rate: Some(0.2),
4805                    min_success_rate: 0.8,
4806                    fallback_min_success_rate: 0.5,
4807                    required_runtime: RequiredRuntime::Rust,
4808                    runtime_available: true,
4809                    available_slots: 8,
4810                    total_slots: 8,
4811                    estimated_cores: 2,
4812                    active_project_excluded: false,
4813                    final_decision: WorkerSelectionDiagnosticDecision::Deny,
4814                    final_reason: "success_rate 0.20 < fallback 0.50".to_string(),
4815                    reason_codes: vec![
4816                        "health.below_min_success_rate".to_string(),
4817                        "health.below_fallback_min_success_rate".to_string(),
4818                    ],
4819                }],
4820            }),
4821        };
4822
4823        assert_golden_json(
4824            serde_json::to_value(&response).expect("response serializes"),
4825            &fixture["expected_response"],
4826            FIXTURE_PATH,
4827        );
4828    }
4829
4830    #[test]
4831    fn test_selection_response_roundtrip() {
4832        let _guard = test_guard!();
4833        let original = SelectionResponse {
4834            worker: Some(SelectedWorker {
4835                id: WorkerId::new("worker-1"),
4836                host: "192.168.1.100".to_string(),
4837                user: "ubuntu".to_string(),
4838                identity_file: "/path/to/key".to_string(),
4839                slots_available: 16,
4840                speed_score: 90.5,
4841                declared_os: None,
4842            }),
4843            reason: SelectionReason::Success,
4844            build_id: None,
4845            diagnostics: None,
4846        };
4847
4848        let json = serde_json::to_string(&original).unwrap();
4849        let parsed: SelectionResponse = serde_json::from_str(&json).unwrap();
4850
4851        assert!(parsed.worker.is_some());
4852        let worker = parsed.worker.unwrap();
4853        assert_eq!(worker.id.as_str(), "worker-1");
4854        assert_eq!(worker.host, "192.168.1.100");
4855        assert_eq!(worker.slots_available, 16);
4856        assert_eq!(parsed.reason, SelectionReason::Success);
4857    }
4858
4859    #[test]
4860    fn test_build_heartbeat_phase_roundtrip() {
4861        let _guard = test_guard!();
4862        let json = serde_json::to_string(&BuildHeartbeatPhase::SyncUp).unwrap();
4863        assert_eq!(json, "\"sync_up\"");
4864        let parsed: BuildHeartbeatPhase = serde_json::from_str(&json).unwrap();
4865        assert_eq!(parsed, BuildHeartbeatPhase::SyncUp);
4866    }
4867
4868    #[test]
4869    fn test_build_heartbeat_request_roundtrip() {
4870        let _guard = test_guard!();
4871        let request = BuildHeartbeatRequest {
4872            build_id: 42,
4873            worker_id: WorkerId::new("worker-a"),
4874            hook_pid: Some(12345),
4875            local_wrapper_id: Some("wrapper-abc123".to_string()),
4876            remote_pgid_file: Some("/tmp/rch/project/hash/.rch-run/42.pgid".to_string()),
4877            phase: BuildHeartbeatPhase::Execute,
4878            detail: Some("Compiling crates".to_string()),
4879            progress_counter: Some(7),
4880            progress_percent: Some(42.5),
4881        };
4882
4883        let json = serde_json::to_string(&request).unwrap();
4884        let parsed: BuildHeartbeatRequest = serde_json::from_str(&json).unwrap();
4885        assert_eq!(parsed.build_id, 42);
4886        assert_eq!(parsed.worker_id.as_str(), "worker-a");
4887        assert_eq!(
4888            parsed.remote_pgid_file,
4889            Some("/tmp/rch/project/hash/.rch-run/42.pgid".to_string())
4890        );
4891        assert_eq!(parsed.phase, BuildHeartbeatPhase::Execute);
4892        assert_eq!(parsed.progress_counter, Some(7));
4893        assert_eq!(parsed.progress_percent, Some(42.5));
4894    }
4895
4896    // CircuitStats tests
4897
4898    #[test]
4899    fn test_circuit_stats_new() {
4900        let _guard = test_guard!();
4901        let stats = CircuitStats::new();
4902        assert_eq!(stats.state(), CircuitState::Closed);
4903        assert_eq!(stats.consecutive_failures(), 0);
4904        assert_eq!(stats.error_rate(), 0.0);
4905        assert!(stats.opened_at().is_none());
4906    }
4907
4908    #[test]
4909    fn test_circuit_stats_record_success() {
4910        let _guard = test_guard!();
4911        let mut stats = CircuitStats::new();
4912        stats.record_success();
4913        stats.record_success();
4914        assert_eq!(stats.error_rate(), 0.0);
4915        assert_eq!(stats.consecutive_failures(), 0);
4916    }
4917
4918    #[test]
4919    fn test_circuit_stats_record_failure() {
4920        let _guard = test_guard!();
4921        let mut stats = CircuitStats::new();
4922        stats.record_failure();
4923        stats.record_failure();
4924        assert_eq!(stats.consecutive_failures(), 2);
4925        assert_eq!(stats.error_rate(), 1.0); // 2 failures, 0 successes
4926    }
4927
4928    #[test]
4929    fn test_circuit_stats_error_rate() {
4930        let _guard = test_guard!();
4931        let mut stats = CircuitStats::new();
4932        stats.record_success();
4933        stats.record_success();
4934        stats.record_failure();
4935        // 1 failure / 3 total = 0.333...
4936        assert!((stats.error_rate() - 0.333).abs() < 0.01);
4937    }
4938
4939    #[test]
4940    fn test_circuit_stats_should_open_consecutive_failures() {
4941        let _guard = test_guard!();
4942        let mut stats = CircuitStats::new();
4943        let config = CircuitBreakerConfig::default(); // threshold = 3
4944
4945        stats.record_failure();
4946        assert!(!stats.should_open(&config));
4947
4948        stats.record_failure();
4949        assert!(!stats.should_open(&config));
4950
4951        stats.record_failure();
4952        assert!(stats.should_open(&config)); // 3 consecutive failures
4953    }
4954
4955    #[test]
4956    fn test_circuit_stats_should_open_error_rate() {
4957        let _guard = test_guard!();
4958        let mut stats = CircuitStats::new();
4959        let config = CircuitBreakerConfig {
4960            error_rate_threshold: 0.5,
4961            ..Default::default()
4962        };
4963
4964        // Need at least 5 samples
4965        stats.record_success();
4966        stats.record_success();
4967        stats.record_failure();
4968        stats.record_failure();
4969        assert!(!stats.should_open(&config)); // Only 4 samples
4970
4971        stats.record_failure(); // 5 samples: 2 success, 3 failures = 60% error rate
4972        assert!(stats.should_open(&config));
4973    }
4974
4975    #[test]
4976    fn test_circuit_close_clears_recent_results_no_flap() {
4977        let _guard = test_guard!();
4978        // Regression: error_rate() is computed over recent_results, so close()
4979        // must clear that history or a recovered circuit immediately re-opens
4980        // on stale pre-failure samples (flapping).
4981        let mut stats = CircuitStats::new();
4982        let config = CircuitBreakerConfig {
4983            error_rate_threshold: 0.5,
4984            ..Default::default()
4985        };
4986        for _ in 0..6 {
4987            stats.record_failure();
4988        }
4989        assert!(stats.should_open(&config));
4990        stats.open();
4991        // Recover via half-open probes, then close.
4992        stats.half_open();
4993        stats.record_success();
4994        stats.record_success();
4995        stats.close();
4996        // Error rate must be reset and the circuit must NOT immediately re-open.
4997        assert_eq!(stats.error_rate(), 0.0);
4998        assert!(!stats.should_open(&config));
4999    }
5000
5001    #[test]
5002    fn test_apply_health_outcome_opens_on_failure_threshold() {
5003        let _guard = test_guard!();
5004        // failure_threshold=3: three unhealthy outcomes open the circuit.
5005        let config = CircuitBreakerConfig {
5006            failure_threshold: 3,
5007            open_cooldown_secs: 300,
5008            ..Default::default()
5009        };
5010        let mut stats = CircuitStats::new();
5011        assert_eq!(
5012            stats.apply_health_outcome(false, false, &config),
5013            CircuitState::Closed
5014        );
5015        assert_eq!(
5016            stats.apply_health_outcome(false, false, &config),
5017            CircuitState::Closed
5018        );
5019        assert_eq!(
5020            stats.apply_health_outcome(false, false, &config),
5021            CircuitState::Open
5022        );
5023    }
5024
5025    #[test]
5026    fn test_apply_health_outcome_half_open_two_probe_recovery() {
5027        let _guard = test_guard!();
5028        // success_threshold=2: recovery from Open needs exactly two clean probes.
5029        // open_cooldown_secs=0 makes should_half_open() true immediately, so the
5030        // engine's trailing nudge leaves an opened circuit already in HalfOpen —
5031        // exactly the state a real daemon would be in on the next poll once the
5032        // cooldown had elapsed. From HalfOpen the engine credits successes.
5033        let config = CircuitBreakerConfig {
5034            failure_threshold: 3,
5035            success_threshold: 2,
5036            open_cooldown_secs: 0,
5037            ..Default::default()
5038        };
5039        let mut stats = CircuitStats::new();
5040        for _ in 0..3 {
5041            stats.apply_health_outcome(false, false, &config);
5042        }
5043        // With a 0s cooldown the engine nudges Open -> HalfOpen immediately.
5044        assert_eq!(stats.state(), CircuitState::HalfOpen);
5045
5046        // First healthy probe in half-open: one success credited, not yet closed.
5047        let after_first = stats.apply_health_outcome(true, false, &config);
5048        assert_eq!(after_first, CircuitState::HalfOpen);
5049
5050        // Second healthy probe: threshold met -> Closed.
5051        let after_second = stats.apply_health_outcome(true, false, &config);
5052        assert_eq!(after_second, CircuitState::Closed);
5053    }
5054
5055    #[test]
5056    fn test_apply_health_outcome_transient_half_open_does_not_reopen() {
5057        let _guard = test_guard!();
5058        // A transient failure while half-open must NOT reset the recovery streak
5059        // — a network blip can't undo an in-progress recovery.
5060        let config = CircuitBreakerConfig {
5061            failure_threshold: 3,
5062            success_threshold: 2,
5063            open_cooldown_secs: 0,
5064            ..Default::default()
5065        };
5066        let mut stats = CircuitStats::new();
5067        for _ in 0..3 {
5068            stats.apply_health_outcome(false, false, &config);
5069        }
5070        // 0s cooldown -> already nudged to HalfOpen.
5071        assert_eq!(stats.state(), CircuitState::HalfOpen);
5072
5073        // One clean probe (1 of 2 needed).
5074        assert_eq!(
5075            stats.apply_health_outcome(true, false, &config),
5076            CircuitState::HalfOpen
5077        );
5078
5079        // Transient failure in half-open: stays HalfOpen (does NOT reopen the
5080        // circuit or restart the cooldown). The key contrast with a real failure
5081        // is that the circuit does not drop back to Open — probing continues in
5082        // place, so recovery resumes immediately on the next clean probes rather
5083        // than waiting out a fresh cooldown.
5084        let after_transient = stats.apply_health_outcome(false, true, &config);
5085        assert_eq!(after_transient, CircuitState::HalfOpen);
5086
5087        // Recovery still completes from half-open with the required clean probes.
5088        assert_eq!(
5089            stats.apply_health_outcome(true, false, &config),
5090            CircuitState::HalfOpen
5091        );
5092        assert_eq!(
5093            stats.apply_health_outcome(true, false, &config),
5094            CircuitState::Closed
5095        );
5096    }
5097
5098    #[test]
5099    fn test_apply_health_outcome_real_vs_transient_half_open_failure() {
5100        let _guard = test_guard!();
5101        // With a positive cooldown the difference is directly observable: a REAL
5102        // half-open failure reopens the circuit (state -> Open, must wait out the
5103        // cooldown again), whereas a TRANSIENT half-open failure keeps it in
5104        // HalfOpen so recovery continues without a fresh cooldown.
5105        //
5106        // Manually drive to HalfOpen (open() + half_open()) so the positive
5107        // cooldown doesn't force a real wallclock wait to reach half-open.
5108        let config = CircuitBreakerConfig {
5109            failure_threshold: 3,
5110            success_threshold: 2,
5111            open_cooldown_secs: 300,
5112            ..Default::default()
5113        };
5114
5115        // Real failure path.
5116        let mut real = CircuitStats::new();
5117        real.open();
5118        real.half_open();
5119        assert_eq!(real.state(), CircuitState::HalfOpen);
5120        let after_real = real.apply_health_outcome(false, false, &config);
5121        assert_eq!(
5122            after_real,
5123            CircuitState::Open,
5124            "a real half-open failure must reopen the circuit"
5125        );
5126
5127        // Transient failure path (same starting state).
5128        let mut transient = CircuitStats::new();
5129        transient.open();
5130        transient.half_open();
5131        assert_eq!(transient.state(), CircuitState::HalfOpen);
5132        let after_transient = transient.apply_health_outcome(false, true, &config);
5133        assert_eq!(
5134            after_transient,
5135            CircuitState::HalfOpen,
5136            "a transient half-open failure must NOT reopen the circuit"
5137        );
5138    }
5139
5140    /// Issue #48 regression: a worker whose liveness probe (`echo` over a warm
5141    /// session) keeps succeeding while every real command times out must NOT
5142    /// be re-admitted. Liveness successes alone cannot close a half-open
5143    /// circuit while command-class evidence is outstanding.
5144    #[test]
5145    fn test_command_failure_blocks_half_open_close_on_liveness_successes() {
5146        let _guard = test_guard!();
5147        let config = CircuitBreakerConfig {
5148            failure_threshold: 3,
5149            success_threshold: 2,
5150            open_cooldown_secs: 0,
5151            command_failure_threshold: 2,
5152            ..Default::default()
5153        };
5154        let mut stats = CircuitStats::new();
5155        stats.open();
5156        stats.half_open();
5157
5158        // The half-open trial: a real command hangs on the worker.
5159        assert_eq!(
5160            stats.apply_command_outcome(false, &config),
5161            CircuitState::HalfOpen,
5162            "cooldown 0: reopen then immediately half-open again"
5163        );
5164        assert_eq!(stats.consecutive_command_failures(), 1);
5165
5166        // Liveness keeps answering — many times over.
5167        for _ in 0..10 {
5168            assert_eq!(
5169                stats.apply_health_outcome(true, false, &config),
5170                CircuitState::HalfOpen,
5171                "liveness successes must not close the circuit while a command failure is outstanding"
5172            );
5173        }
5174
5175        // Only a command-class success clears the evidence and recovers.
5176        assert_eq!(
5177            stats.apply_command_outcome(true, &config),
5178            CircuitState::Closed
5179        );
5180        assert_eq!(stats.consecutive_command_failures(), 0);
5181        assert_eq!(stats.reopen_streak(), 0);
5182    }
5183
5184    /// Issue #48: interleaved liveness successes reset `failure_threshold`, so
5185    /// command failures need their own threshold to open a Closed circuit.
5186    #[test]
5187    fn test_command_failures_open_closed_circuit_despite_liveness_successes() {
5188        let _guard = test_guard!();
5189        let config = CircuitBreakerConfig {
5190            failure_threshold: 3,
5191            command_failure_threshold: 2,
5192            open_cooldown_secs: 300,
5193            ..Default::default()
5194        };
5195        let mut stats = CircuitStats::new();
5196
5197        assert_eq!(
5198            stats.apply_command_outcome(false, &config),
5199            CircuitState::Closed
5200        );
5201        // Two liveness successes in between: consecutive_failures is reset...
5202        stats.apply_health_outcome(true, false, &config);
5203        stats.apply_health_outcome(true, false, &config);
5204        assert_eq!(stats.consecutive_failures(), 0);
5205        // ...but the command-failure count is not, and the second one opens.
5206        assert_eq!(
5207            stats.apply_command_outcome(false, &config),
5208            CircuitState::Open
5209        );
5210        assert_eq!(stats.reopen_streak(), 1);
5211    }
5212
5213    /// A command failure in HalfOpen always reopens: "connected but the
5214    /// command never completed" is exactly what the trial exists to detect,
5215    /// so there is no transient exemption on this channel.
5216    #[test]
5217    fn test_command_failure_in_half_open_always_reopens() {
5218        let _guard = test_guard!();
5219        let config = CircuitBreakerConfig {
5220            open_cooldown_secs: 300,
5221            ..Default::default()
5222        };
5223        let mut stats = CircuitStats::new();
5224        stats.open();
5225        stats.half_open();
5226        assert_eq!(
5227            stats.apply_command_outcome(false, &config),
5228            CircuitState::Open
5229        );
5230        assert_eq!(stats.reopen_streak(), 2);
5231    }
5232
5233    /// Repeated Open -> HalfOpen -> Open cycles lengthen the cooldown
5234    /// (exponential, capped) so a flapping worker converges to Open instead of
5235    /// being re-admitted on every base cooldown.
5236    #[test]
5237    fn test_reopen_streak_backs_off_cooldown_until_cap() {
5238        let _guard = test_guard!();
5239        let config = CircuitBreakerConfig {
5240            open_cooldown_secs: 30,
5241            open_cooldown_max_secs: 100,
5242            ..Default::default()
5243        };
5244        assert_eq!(config.cooldown_ms_for_streak(0), 30_000);
5245        assert_eq!(config.cooldown_ms_for_streak(1), 30_000);
5246        assert_eq!(config.cooldown_ms_for_streak(2), 60_000);
5247        assert_eq!(config.cooldown_ms_for_streak(3), 100_000, "capped");
5248        assert_eq!(config.cooldown_ms_for_streak(40), 100_000, "no overflow");
5249
5250        // A cap below the base never shortens the base cooldown.
5251        let inverted = CircuitBreakerConfig {
5252            open_cooldown_secs: 30,
5253            open_cooldown_max_secs: 5,
5254            ..Default::default()
5255        };
5256        assert_eq!(inverted.cooldown_ms_for_streak(3), 30_000);
5257
5258        // The streak is observable through recovery_remaining_secs: after two
5259        // opens the remaining cooldown exceeds the base.
5260        let mut stats = CircuitStats::new();
5261        stats.open();
5262        stats.half_open();
5263        stats.open();
5264        assert_eq!(stats.reopen_streak(), 2);
5265        let remaining = stats.recovery_remaining_secs(&config).unwrap();
5266        assert!(
5267            remaining > 30 && remaining <= 60,
5268            "second open should wait ~60s, got {remaining}"
5269        );
5270
5271        // Closing resets the streak, so the next open starts from the base.
5272        stats.close();
5273        assert_eq!(stats.reopen_streak(), 0);
5274        stats.open();
5275        assert!(stats.recovery_remaining_secs(&config).unwrap() <= 30);
5276    }
5277
5278    #[test]
5279    fn test_circuit_stats_success_resets_consecutive_failures() {
5280        let _guard = test_guard!();
5281        let mut stats = CircuitStats::new();
5282        stats.record_failure();
5283        stats.record_failure();
5284        assert_eq!(stats.consecutive_failures(), 2);
5285
5286        stats.record_success();
5287        assert_eq!(stats.consecutive_failures(), 0);
5288    }
5289
5290    #[test]
5291    fn test_circuit_stats_open_transition() {
5292        let _guard = test_guard!();
5293        let mut stats = CircuitStats::new();
5294        stats.open();
5295
5296        assert_eq!(stats.state(), CircuitState::Open);
5297        assert!(stats.opened_at().is_some());
5298    }
5299
5300    #[test]
5301    fn test_circuit_stats_half_open_transition() {
5302        let _guard = test_guard!();
5303        let mut stats = CircuitStats::new();
5304        stats.open();
5305        stats.half_open();
5306
5307        assert_eq!(stats.state(), CircuitState::HalfOpen);
5308    }
5309
5310    #[test]
5311    fn test_circuit_stats_close_transition() {
5312        let _guard = test_guard!();
5313        let mut stats = CircuitStats::new();
5314        stats.open();
5315        stats.half_open();
5316        stats.close();
5317
5318        assert_eq!(stats.state(), CircuitState::Closed);
5319        assert!(stats.opened_at().is_none());
5320        assert_eq!(stats.consecutive_failures(), 0);
5321    }
5322
5323    #[test]
5324    fn test_circuit_stats_should_close() {
5325        let _guard = test_guard!();
5326        let mut stats = CircuitStats::new();
5327        let config = CircuitBreakerConfig {
5328            success_threshold: 2,
5329            ..Default::default()
5330        };
5331
5332        stats.open();
5333        stats.half_open();
5334
5335        assert!(!stats.should_close(&config)); // 0 successes
5336
5337        stats.record_success();
5338        assert!(!stats.should_close(&config)); // 1 success
5339
5340        stats.record_success();
5341        assert!(stats.should_close(&config)); // 2 successes
5342    }
5343
5344    #[test]
5345    fn test_circuit_stats_can_probe() {
5346        let _guard = test_guard!();
5347        let mut stats = CircuitStats::new();
5348        let config = CircuitBreakerConfig {
5349            half_open_max_probes: 1,
5350            ..Default::default()
5351        };
5352
5353        // Can't probe when closed
5354        assert!(!stats.can_probe(&config));
5355
5356        stats.open();
5357        // Can't probe when open
5358        assert!(!stats.can_probe(&config));
5359
5360        stats.half_open();
5361        // Can probe when half-open
5362        assert!(stats.can_probe(&config));
5363
5364        // Start a probe
5365        assert!(stats.start_probe(&config));
5366        // Can't start another probe
5367        assert!(!stats.can_probe(&config));
5368        assert!(!stats.start_probe(&config));
5369    }
5370
5371    #[test]
5372    fn test_circuit_stats_probe_completion() {
5373        let _guard = test_guard!();
5374        let mut stats = CircuitStats::new();
5375        let config = CircuitBreakerConfig {
5376            half_open_max_probes: 1,
5377            ..Default::default()
5378        };
5379
5380        stats.open();
5381        stats.half_open();
5382        stats.start_probe(&config);
5383
5384        // Probe completes with success
5385        stats.record_success();
5386
5387        // Can start another probe
5388        assert!(stats.can_probe(&config));
5389    }
5390
5391    #[test]
5392    fn test_circuit_stats_failure_in_half_open() {
5393        let _guard = test_guard!();
5394        let mut stats = CircuitStats::new();
5395        let config = CircuitBreakerConfig {
5396            success_threshold: 2,
5397            ..Default::default()
5398        };
5399
5400        stats.open();
5401        stats.half_open();
5402
5403        stats.record_success();
5404        assert_eq!(stats.consecutive_failures(), 0);
5405
5406        // Failure resets consecutive successes
5407        stats.record_failure();
5408        assert!(!stats.should_close(&config));
5409
5410        // Need 2 more successes
5411        stats.record_success();
5412        stats.record_success();
5413        assert!(stats.should_close(&config));
5414    }
5415
5416    #[test]
5417    fn test_circuit_stats_reset_window() {
5418        let _guard = test_guard!();
5419        let mut stats = CircuitStats::new();
5420        stats.record_success();
5421        stats.record_failure();
5422
5423        assert!(stats.error_rate() > 0.0);
5424
5425        stats.reset_window();
5426        assert_eq!(stats.error_rate(), 0.0);
5427    }
5428
5429    #[test]
5430    fn test_circuit_state_transitions_are_deterministic() {
5431        let _guard = test_guard!();
5432        let config = CircuitBreakerConfig::default();
5433        let mut stats = CircuitStats::new();
5434
5435        // Start closed
5436        assert_eq!(stats.state(), CircuitState::Closed);
5437
5438        // Cause failures to open
5439        for _ in 0..3 {
5440            stats.record_failure();
5441        }
5442        assert!(stats.should_open(&config));
5443        stats.open();
5444        assert_eq!(stats.state(), CircuitState::Open);
5445
5446        // Transition to half-open after cooldown would be checked
5447        stats.half_open();
5448        assert_eq!(stats.state(), CircuitState::HalfOpen);
5449
5450        // Successes to close
5451        for _ in 0..2 {
5452            stats.record_success();
5453        }
5454        assert!(stats.should_close(&config));
5455        stats.close();
5456        assert_eq!(stats.state(), CircuitState::Closed);
5457    }
5458
5459    // ========================================================================
5460    // Compilation Timing Tests
5461    // ========================================================================
5462
5463    fn make_test_metrics(
5464        speedup: Option<f64>,
5465        total_secs: u64,
5466        success: bool,
5467    ) -> CompilationMetrics {
5468        CompilationMetrics {
5469            project_id: "test-project".to_string(),
5470            worker_id: "worker-1".to_string(),
5471            timestamp: chrono::Utc::now(),
5472            timing: CompilationTimingBreakdown {
5473                rsync_up: Duration::from_millis(100),
5474                remote_build: Duration::from_millis(800),
5475                rsync_down: Duration::from_millis(100),
5476                total: Duration::from_secs(total_secs),
5477            },
5478            local_build_time: speedup.map(|s| Duration::from_secs_f64(total_secs as f64 * s)),
5479            speedup,
5480            files_synced: 100,
5481            bytes_transferred: 1_000_000,
5482            exit_code: if success { 0 } else { 1 },
5483            success,
5484        }
5485    }
5486
5487    #[test]
5488    fn test_compilation_timing_breakdown_default() {
5489        let _guard = test_guard!();
5490        let timing = CompilationTimingBreakdown::default();
5491        assert_eq!(timing.rsync_up, Duration::ZERO);
5492        assert_eq!(timing.remote_build, Duration::ZERO);
5493        assert_eq!(timing.rsync_down, Duration::ZERO);
5494        assert_eq!(timing.total, Duration::ZERO);
5495    }
5496
5497    #[test]
5498    fn test_compilation_timing_breakdown_serialization() {
5499        let _guard = test_guard!();
5500        let timing = CompilationTimingBreakdown {
5501            rsync_up: Duration::from_millis(100),
5502            remote_build: Duration::from_millis(2000),
5503            rsync_down: Duration::from_millis(50),
5504            total: Duration::from_millis(2150),
5505        };
5506
5507        let json = serde_json::to_string(&timing).unwrap();
5508        assert!(json.contains("100")); // rsync_up
5509        assert!(json.contains("2000")); // remote_build
5510        assert!(json.contains("50")); // rsync_down
5511        assert!(json.contains("2150")); // total
5512
5513        let parsed: CompilationTimingBreakdown = serde_json::from_str(&json).unwrap();
5514        assert_eq!(parsed.rsync_up, timing.rsync_up);
5515        assert_eq!(parsed.remote_build, timing.remote_build);
5516        assert_eq!(parsed.rsync_down, timing.rsync_down);
5517        assert_eq!(parsed.total, timing.total);
5518    }
5519
5520    #[test]
5521    fn test_command_timing_breakdown_serialization() {
5522        let _guard = test_guard!();
5523        let timing = CommandTimingBreakdown {
5524            classify: Some(Duration::from_millis(2)),
5525            select: None,
5526            sync_up: Some(Duration::from_millis(120)),
5527            exec: Some(Duration::from_millis(900)),
5528            sync_down: None,
5529            cleanup: Some(Duration::from_millis(5)),
5530            total: Some(Duration::from_millis(1027)),
5531        };
5532
5533        let json = serde_json::to_string(&timing).unwrap();
5534        assert!(json.contains("\"classify\":2"));
5535        assert!(json.contains("\"select\":null"));
5536        assert!(json.contains("\"sync_up\":120"));
5537        assert!(json.contains("\"exec\":900"));
5538        assert!(json.contains("\"sync_down\":null"));
5539        assert!(json.contains("\"cleanup\":5"));
5540        assert!(json.contains("\"total\":1027"));
5541
5542        let parsed: CommandTimingBreakdown = serde_json::from_str(&json).unwrap();
5543        assert_eq!(parsed.classify, timing.classify);
5544        assert_eq!(parsed.select, timing.select);
5545        assert_eq!(parsed.sync_up, timing.sync_up);
5546        assert_eq!(parsed.exec, timing.exec);
5547        assert_eq!(parsed.sync_down, timing.sync_down);
5548        assert_eq!(parsed.cleanup, timing.cleanup);
5549        assert_eq!(parsed.total, timing.total);
5550    }
5551
5552    #[test]
5553    fn test_compilation_metrics_calculate_speedup() {
5554        let _guard = test_guard!();
5555        let mut metrics = CompilationMetrics {
5556            timing: CompilationTimingBreakdown {
5557                total: Duration::from_secs(10),
5558                ..Default::default()
5559            },
5560            local_build_time: Some(Duration::from_secs(30)),
5561            ..Default::default()
5562        };
5563
5564        metrics.calculate_speedup();
5565
5566        assert!(metrics.speedup.is_some());
5567        let speedup = metrics.speedup.unwrap();
5568        assert!(
5569            (speedup - 3.0).abs() < 0.01,
5570            "Expected 3.0x speedup, got {}",
5571            speedup
5572        );
5573    }
5574
5575    #[test]
5576    fn test_compilation_metrics_is_beneficial() {
5577        let _guard = test_guard!();
5578        let mut metrics = CompilationMetrics::default();
5579
5580        // No speedup calculated
5581        assert!(!metrics.is_beneficial());
5582
5583        // Speedup > 1 is beneficial
5584        metrics.speedup = Some(1.5);
5585        assert!(metrics.is_beneficial());
5586
5587        // Speedup < 1 is not beneficial
5588        metrics.speedup = Some(0.8);
5589        assert!(!metrics.is_beneficial());
5590
5591        // Speedup = 1 is not beneficial (exactly same speed)
5592        metrics.speedup = Some(1.0);
5593        assert!(!metrics.is_beneficial());
5594    }
5595
5596    #[test]
5597    fn test_compilation_metrics_serialization() {
5598        let _guard = test_guard!();
5599        let metrics = make_test_metrics(Some(2.5), 10, true);
5600        let json = serde_json::to_string(&metrics).unwrap();
5601
5602        assert!(json.contains("test-project"));
5603        assert!(json.contains("worker-1"));
5604        assert!(json.contains("2.5")); // speedup
5605
5606        let parsed: CompilationMetrics = serde_json::from_str(&json).unwrap();
5607        assert_eq!(parsed.project_id, "test-project");
5608        assert_eq!(parsed.worker_id, "worker-1");
5609        assert!(parsed.success);
5610    }
5611
5612    #[test]
5613    fn test_compilation_timer_new() {
5614        let _guard = test_guard!();
5615        let timer = CompilationTimer::new("my-project", "my-worker");
5616        assert_eq!(timer.project_id, "my-project");
5617        assert_eq!(timer.worker_id, "my-worker");
5618        assert!(timer.rsync_up.is_none());
5619        assert!(timer.remote_build.is_none());
5620        assert!(timer.rsync_down.is_none());
5621    }
5622
5623    #[test]
5624    fn test_compilation_timer_phases() {
5625        let _guard = test_guard!();
5626        let mut timer = CompilationTimer::new("test", "worker");
5627
5628        // Simulate rsync up
5629        std::thread::sleep(Duration::from_millis(5));
5630        timer.end_rsync_up();
5631        assert!(timer.rsync_up.is_some());
5632        assert!(timer.rsync_up.unwrap() >= Duration::from_millis(5));
5633
5634        // Simulate remote build
5635        std::thread::sleep(Duration::from_millis(10));
5636        timer.end_remote_build();
5637        assert!(timer.remote_build.is_some());
5638        assert!(timer.remote_build.unwrap() >= Duration::from_millis(10));
5639
5640        // Simulate rsync down
5641        std::thread::sleep(Duration::from_millis(5));
5642        timer.end_rsync_down();
5643        assert!(timer.rsync_down.is_some());
5644        assert!(timer.rsync_down.unwrap() >= Duration::from_millis(5));
5645
5646        // Finish
5647        let metrics = timer.finish(0, 50, 500_000);
5648        assert_eq!(metrics.project_id, "test");
5649        assert_eq!(metrics.worker_id, "worker");
5650        assert!(metrics.success);
5651        assert!(metrics.timing.total >= Duration::from_millis(20));
5652        assert_eq!(metrics.files_synced, 50);
5653        assert_eq!(metrics.bytes_transferred, 500_000);
5654    }
5655
5656    #[test]
5657    fn test_metrics_aggregator_new() {
5658        let _guard = test_guard!();
5659        let agg = MetricsAggregator::new(100);
5660        assert_eq!(agg.count(), 0);
5661        assert!(agg.average_speedup().is_none());
5662    }
5663
5664    #[test]
5665    fn test_metrics_aggregator_record() {
5666        let _guard = test_guard!();
5667        let mut agg = MetricsAggregator::new(3);
5668
5669        // Record first metric
5670        agg.record(make_test_metrics(Some(2.0), 10, true));
5671        assert_eq!(agg.count(), 1);
5672
5673        // Record up to max
5674        agg.record(make_test_metrics(Some(3.0), 20, true));
5675        agg.record(make_test_metrics(Some(4.0), 30, true));
5676        assert_eq!(agg.count(), 3);
5677
5678        // Exceed max should drop oldest
5679        agg.record(make_test_metrics(Some(5.0), 40, true));
5680        assert_eq!(agg.count(), 3);
5681    }
5682
5683    #[test]
5684    fn test_metrics_aggregator_average_speedup() {
5685        let _guard = test_guard!();
5686        let mut agg = MetricsAggregator::new(100);
5687
5688        // Empty case
5689        assert!(agg.average_speedup().is_none());
5690
5691        // Add metrics with speedups 1, 2, 3, 4, 5 -> average = 3
5692        for i in 1..=5 {
5693            agg.record(make_test_metrics(Some(i as f64), 10, true));
5694        }
5695
5696        let avg = agg.average_speedup().unwrap();
5697        assert!((avg - 3.0).abs() < 0.01, "Expected 3.0, got {}", avg);
5698    }
5699
5700    #[test]
5701    fn test_metrics_aggregator_average_speedup_with_none() {
5702        let _guard = test_guard!();
5703        let mut agg = MetricsAggregator::new(100);
5704
5705        // Mix of Some and None speedups
5706        agg.record(make_test_metrics(Some(2.0), 10, true));
5707        agg.record(make_test_metrics(None, 10, true)); // No speedup
5708        agg.record(make_test_metrics(Some(4.0), 10, true));
5709
5710        // Average should only consider metrics with speedup
5711        let avg = agg.average_speedup().unwrap();
5712        assert!((avg - 3.0).abs() < 0.01, "Expected 3.0, got {}", avg);
5713    }
5714
5715    #[test]
5716    fn test_metrics_aggregator_percentiles() {
5717        let _guard = test_guard!();
5718        let mut agg = MetricsAggregator::new(100);
5719
5720        // Add metrics with total times 1s, 2s, 3s, ..., 10s
5721        for i in 1..=10 {
5722            agg.record(make_test_metrics(Some(1.0), i, true));
5723        }
5724
5725        // p50 (median) should be around 5s
5726        let p50 = agg.p50_total_time().unwrap();
5727        assert!(p50 >= Duration::from_secs(5) && p50 <= Duration::from_secs(6));
5728
5729        // p95 should be around 9s or 10s
5730        let p95 = agg.p95_total_time().unwrap();
5731        assert!(p95 >= Duration::from_secs(9) && p95 <= Duration::from_secs(10));
5732
5733        // p99 should be 10s
5734        let p99 = agg.p99_total_time().unwrap();
5735        assert!(p99 >= Duration::from_secs(9));
5736    }
5737
5738    #[test]
5739    fn test_metrics_aggregator_success_rate() {
5740        let _guard = test_guard!();
5741        let mut agg = MetricsAggregator::new(100);
5742
5743        // Empty case
5744        assert_eq!(agg.success_rate(), 100.0);
5745
5746        // All successful
5747        agg.record(make_test_metrics(Some(1.0), 10, true));
5748        agg.record(make_test_metrics(Some(1.0), 10, true));
5749        assert_eq!(agg.success_rate(), 100.0);
5750
5751        // Add a failure (2 success, 1 failure = 66.67%)
5752        agg.record(make_test_metrics(Some(1.0), 10, false));
5753        let rate = agg.success_rate();
5754        assert!((rate - 66.67).abs() < 1.0, "Expected ~66.67%, got {}", rate);
5755    }
5756
5757    #[test]
5758    fn test_metrics_aggregator_clear() {
5759        let _guard = test_guard!();
5760        let mut agg = MetricsAggregator::new(100);
5761        agg.record(make_test_metrics(Some(1.0), 10, true));
5762        agg.record(make_test_metrics(Some(2.0), 20, true));
5763
5764        assert_eq!(agg.count(), 2);
5765
5766        agg.clear();
5767        assert_eq!(agg.count(), 0);
5768        assert!(agg.average_speedup().is_none());
5769    }
5770
5771    // CompilationConfig timeout tests
5772
5773    #[test]
5774    fn test_remote_build_jobs_parse_display_and_serde() {
5775        let _guard = test_guard!();
5776        assert_eq!(RemoteBuildJobs::default(), RemoteBuildJobs::Auto);
5777        for (spelling, expected) in [
5778            ("auto", RemoteBuildJobs::Auto),
5779            (" AUTO ", RemoteBuildJobs::Auto),
5780            ("off", RemoteBuildJobs::Off),
5781            ("none", RemoteBuildJobs::Off),
5782            ("4", RemoteBuildJobs::Fixed(4)),
5783        ] {
5784            assert_eq!(
5785                RemoteBuildJobs::parse(spelling).unwrap(),
5786                expected,
5787                "{spelling}"
5788            );
5789        }
5790        assert!(RemoteBuildJobs::parse("0").is_err(), "cargo rejects -j 0");
5791        assert!(RemoteBuildJobs::parse("many").is_err());
5792        assert_eq!(RemoteBuildJobs::Fixed(4).to_string(), "4");
5793        assert_eq!(RemoteBuildJobs::Off.to_string(), "off");
5794
5795        // TOML accepts both the string spellings and a bare integer, and an
5796        // absent key keeps the default.
5797        #[derive(Deserialize)]
5798        struct Wrap {
5799            compilation: CompilationConfig,
5800        }
5801        let parsed: Wrap = toml::from_str("[compilation]\nremote_build_jobs = \"off\"\n").unwrap();
5802        assert_eq!(parsed.compilation.remote_build_jobs, RemoteBuildJobs::Off);
5803        let parsed: Wrap = toml::from_str("[compilation]\nremote_build_jobs = 6\n").unwrap();
5804        assert_eq!(
5805            parsed.compilation.remote_build_jobs,
5806            RemoteBuildJobs::Fixed(6)
5807        );
5808        let parsed: Wrap = toml::from_str("[compilation]\nbuild_slots = 4\n").unwrap();
5809        assert_eq!(parsed.compilation.remote_build_jobs, RemoteBuildJobs::Auto);
5810        assert!(toml::from_str::<Wrap>("[compilation]\nremote_build_jobs = 0\n").is_err());
5811
5812        // Serialization round-trips through the same spellings.
5813        let json = serde_json::to_string(&RemoteBuildJobs::Fixed(3)).unwrap();
5814        assert_eq!(json, "3");
5815        assert_eq!(
5816            serde_json::to_string(&RemoteBuildJobs::Auto).unwrap(),
5817            "\"auto\""
5818        );
5819        let back: RemoteBuildJobs = serde_json::from_str("\"auto\"").unwrap();
5820        assert_eq!(back, RemoteBuildJobs::Auto);
5821    }
5822
5823    #[test]
5824    fn test_compilation_config_default_timeouts() {
5825        let _guard = test_guard!();
5826        let config = CompilationConfig::default();
5827        // Default build timeout: 5 minutes
5828        assert_eq!(config.build_timeout_sec, 300);
5829        // Default test timeout: 30 minutes
5830        assert_eq!(config.test_timeout_sec, 1800);
5831    }
5832
5833    #[test]
5834    fn test_compilation_config_timeout_for_test_kinds() {
5835        let _guard = test_guard!();
5836        let config = CompilationConfig::default();
5837
5838        // Test commands should get test_timeout_sec
5839        assert_eq!(
5840            config.timeout_for_kind(Some(crate::CompilationKind::CargoTest)),
5841            std::time::Duration::from_secs(1800)
5842        );
5843        assert_eq!(
5844            config.timeout_for_kind(Some(crate::CompilationKind::CargoNextest)),
5845            std::time::Duration::from_secs(1800)
5846        );
5847        assert_eq!(
5848            config.timeout_for_kind(Some(crate::CompilationKind::BunTest)),
5849            std::time::Duration::from_secs(config.bun_timeout_sec)
5850        );
5851    }
5852
5853    #[test]
5854    fn test_compilation_config_timeout_for_build_kinds() {
5855        let _guard = test_guard!();
5856        let config = CompilationConfig::default();
5857
5858        // Build/check commands should get build_timeout_sec
5859        assert_eq!(
5860            config.timeout_for_kind(Some(crate::CompilationKind::CargoBuild)),
5861            std::time::Duration::from_secs(300)
5862        );
5863        assert_eq!(
5864            config.timeout_for_kind(Some(crate::CompilationKind::CargoCheck)),
5865            std::time::Duration::from_secs(300)
5866        );
5867        assert_eq!(
5868            config.timeout_for_kind(Some(crate::CompilationKind::CargoClippy)),
5869            std::time::Duration::from_secs(300)
5870        );
5871        assert_eq!(
5872            config.timeout_for_kind(None),
5873            std::time::Duration::from_secs(300)
5874        );
5875    }
5876
5877    #[test]
5878    fn test_compilation_config_custom_timeouts() {
5879        let _guard = test_guard!();
5880        let config = CompilationConfig {
5881            build_timeout_sec: 600, // 10 minutes
5882            test_timeout_sec: 3600, // 1 hour
5883            ..Default::default()
5884        };
5885
5886        assert_eq!(
5887            config.timeout_for_kind(Some(crate::CompilationKind::CargoBuild)),
5888            std::time::Duration::from_secs(600)
5889        );
5890        assert_eq!(
5891            config.timeout_for_kind(Some(crate::CompilationKind::CargoTest)),
5892            std::time::Duration::from_secs(3600)
5893        );
5894    }
5895
5896    #[test]
5897    fn test_compilation_config_speedup_threshold_default() {
5898        let _guard = test_guard!();
5899        let config = CompilationConfig::default();
5900        // Default speedup threshold: 1.2 (20% faster required)
5901        assert!((config.remote_speedup_threshold - 1.2).abs() < 0.001);
5902    }
5903
5904    #[test]
5905    fn test_compilation_config_speedup_threshold_custom() {
5906        let _guard = test_guard!();
5907        let config = CompilationConfig {
5908            remote_speedup_threshold: 1.5, // Require 50% faster
5909            ..Default::default()
5910        };
5911        assert!((config.remote_speedup_threshold - 1.5).abs() < 0.001);
5912    }
5913
5914    #[test]
5915    fn test_compilation_config_speedup_threshold_no_minimum() {
5916        let _guard = test_guard!();
5917        // Setting to 1.0 means "always offload when other criteria met"
5918        let config = CompilationConfig {
5919            remote_speedup_threshold: 1.0,
5920            ..Default::default()
5921        };
5922        assert!((config.remote_speedup_threshold - 1.0).abs() < 0.001);
5923    }
5924
5925    #[test]
5926    fn test_compilation_config_min_local_time_ms_default() {
5927        let _guard = test_guard!();
5928        let config = CompilationConfig::default();
5929        // Default min_local_time_ms: 2000ms
5930        assert_eq!(config.min_local_time_ms, 2000);
5931    }
5932
5933    #[test]
5934    fn test_compilation_config_serde_roundtrip_speedup_threshold() {
5935        let _guard = test_guard!();
5936        let config = CompilationConfig {
5937            remote_speedup_threshold: 2.5,
5938            min_local_time_ms: 5000,
5939            ..Default::default()
5940        };
5941        let json = serde_json::to_string(&config).unwrap();
5942        let parsed: CompilationConfig = serde_json::from_str(&json).unwrap();
5943        assert!((parsed.remote_speedup_threshold - 2.5).abs() < 0.001);
5944        assert_eq!(parsed.min_local_time_ms, 5000);
5945    }
5946
5947    // ========================================================================
5948    // validate_remote_base Tests
5949    // ========================================================================
5950
5951    #[test]
5952    fn test_validate_remote_base_absolute_path() {
5953        let _guard = test_guard!();
5954        assert_eq!(validate_remote_base("/tmp/rch").unwrap(), "/tmp/rch");
5955        assert_eq!(
5956            validate_remote_base("/var/rch-builds").unwrap(),
5957            "/var/rch-builds"
5958        );
5959        assert_eq!(
5960            validate_remote_base("/home/builder/.rch").unwrap(),
5961            "/home/builder/.rch"
5962        );
5963    }
5964
5965    #[test]
5966    fn test_validate_remote_base_tilde_expansion() {
5967        let _guard = test_guard!();
5968        // Tilde expansion should work
5969        let result = validate_remote_base("~/rch");
5970        assert!(result.is_ok());
5971        let path = result.unwrap();
5972        assert!(
5973            path.starts_with('/'),
5974            "Path should be absolute after expansion: {}",
5975            path
5976        );
5977        assert!(!path.contains('~'), "Tilde should be expanded: {}", path);
5978    }
5979
5980    #[test]
5981    fn test_validate_remote_base_rejects_relative_path() {
5982        let _guard = test_guard!();
5983        let result = validate_remote_base("tmp/rch");
5984        assert!(result.is_err());
5985        assert!(result.unwrap_err().contains("absolute path"));
5986    }
5987
5988    #[test]
5989    fn test_validate_remote_base_rejects_path_traversal() {
5990        let _guard = test_guard!();
5991        let result = validate_remote_base("/tmp/../etc/rch");
5992        assert!(result.is_err());
5993        assert!(result.unwrap_err().contains("path traversal"));
5994
5995        let result = validate_remote_base("/tmp/rch/../other");
5996        assert!(result.is_err());
5997        assert!(result.unwrap_err().contains("path traversal"));
5998    }
5999
6000    #[test]
6001    fn test_validate_remote_base_normalizes_trailing_slash() {
6002        let _guard = test_guard!();
6003        assert_eq!(validate_remote_base("/tmp/rch/").unwrap(), "/tmp/rch");
6004        assert_eq!(validate_remote_base("/tmp/rch///").unwrap(), "/tmp/rch");
6005    }
6006
6007    #[test]
6008    fn test_validate_remote_base_root_path() {
6009        let _guard = test_guard!();
6010        // Root path should be rejected
6011        let result = validate_remote_base("/");
6012        assert!(result.is_err());
6013        assert!(result.unwrap_err().contains("root directory"));
6014    }
6015
6016    #[test]
6017    fn test_validate_remote_base_rejects_top_level() {
6018        let _guard = test_guard!();
6019        // Top-level directories should be rejected (depth 1)
6020        let result = validate_remote_base("/tmp");
6021        assert!(result.is_err());
6022        assert!(result.unwrap_err().contains("at least 2 levels deep"));
6023
6024        let result = validate_remote_base("/home");
6025        assert!(result.is_err());
6026
6027        // Depth 2 should be allowed
6028        assert_eq!(validate_remote_base("/tmp/rch").unwrap(), "/tmp/rch");
6029    }
6030
6031    #[test]
6032    fn test_default_remote_base() {
6033        let _guard = test_guard!();
6034        assert_eq!(default_remote_base(), "/data/tmp/rch");
6035    }
6036
6037    #[test]
6038    fn test_transfer_config_default_has_remote_base() {
6039        let _guard = test_guard!();
6040        let config = TransferConfig::default();
6041        assert_eq!(config.remote_base, "/data/tmp/rch");
6042    }
6043
6044    #[test]
6045    fn test_source_sync_timeout_validation_boundaries() {
6046        let _guard = test_guard!();
6047        assert!(!TransferConfig::valid_sync_timeout_ms(0));
6048        assert!(!TransferConfig::valid_sync_timeout_ms(999));
6049        assert!(TransferConfig::valid_sync_timeout_ms(1_000));
6050        assert!(TransferConfig::valid_sync_timeout_ms(3_600_000));
6051        assert!(!TransferConfig::valid_sync_timeout_ms(3_600_001));
6052    }
6053
6054    #[test]
6055    fn test_source_sync_timeout_is_explicit_or_payload_aware() {
6056        let _guard = test_guard!();
6057        let default = TransferConfig::default();
6058        assert_eq!(
6059            default.sync_timeout_for_payload(0),
6060            std::time::Duration::from_millis(30_000)
6061        );
6062        assert_eq!(
6063            default.sync_timeout_for_payload(10 * 1024 * 1024),
6064            std::time::Duration::from_millis(40_000)
6065        );
6066        assert_eq!(
6067            default.sync_timeout_for_payload(u64::MAX),
6068            std::time::Duration::from_millis(3_600_000)
6069        );
6070
6071        let explicit = TransferConfig {
6072            sync_timeout_ms: Some(120_000),
6073            ..TransferConfig::default()
6074        };
6075        assert_eq!(
6076            explicit.sync_timeout_for_payload(u64::MAX),
6077            std::time::Duration::from_millis(120_000)
6078        );
6079    }
6080
6081    // ========================================================================
6082    // RetryConfig Tests (bd-x1ek)
6083    // ========================================================================
6084
6085    #[test]
6086    fn test_retry_config_default() {
6087        let _guard = test_guard!();
6088        let config = RetryConfig::default();
6089        assert_eq!(config.max_attempts, 3);
6090        assert_eq!(config.base_delay_ms, 100);
6091        assert_eq!(config.max_delay_ms, 5000);
6092        assert_eq!(config.jitter_factor, 0.1);
6093        assert_eq!(config.total_timeout_ms, 30000);
6094    }
6095
6096    #[test]
6097    fn test_retry_config_no_retry() {
6098        let _guard = test_guard!();
6099        let config = RetryConfig::no_retry();
6100        assert_eq!(config.max_attempts, 1);
6101    }
6102
6103    #[test]
6104    fn test_retry_config_delay_for_attempt_zero() {
6105        let _guard = test_guard!();
6106        let config = RetryConfig::default();
6107        assert_eq!(config.delay_for_attempt(0), std::time::Duration::ZERO);
6108    }
6109
6110    #[test]
6111    fn test_retry_config_delay_for_attempt_exponential() {
6112        let _guard = test_guard!();
6113        let config = RetryConfig {
6114            base_delay_ms: 100,
6115            max_delay_ms: 10000,
6116            jitter_factor: 0.0, // No jitter for deterministic test
6117            ..Default::default()
6118        };
6119
6120        // attempt 1: 100 * 2^0 = 100ms
6121        assert_eq!(
6122            config.delay_for_attempt(1),
6123            std::time::Duration::from_millis(100)
6124        );
6125        // attempt 2: 100 * 2^1 = 200ms
6126        assert_eq!(
6127            config.delay_for_attempt(2),
6128            std::time::Duration::from_millis(200)
6129        );
6130        // attempt 3: 100 * 2^2 = 400ms
6131        assert_eq!(
6132            config.delay_for_attempt(3),
6133            std::time::Duration::from_millis(400)
6134        );
6135    }
6136
6137    #[test]
6138    fn test_retry_config_delay_capped_at_max() {
6139        let _guard = test_guard!();
6140        let config = RetryConfig {
6141            base_delay_ms: 1000,
6142            max_delay_ms: 2000,
6143            jitter_factor: 0.0,
6144            ..Default::default()
6145        };
6146
6147        // attempt 2: 1000 * 2^1 = 2000ms (at cap)
6148        assert_eq!(
6149            config.delay_for_attempt(2),
6150            std::time::Duration::from_millis(2000)
6151        );
6152        // attempt 3: 1000 * 2^2 = 4000ms, capped to 2000ms
6153        assert_eq!(
6154            config.delay_for_attempt(3),
6155            std::time::Duration::from_millis(2000)
6156        );
6157    }
6158
6159    #[test]
6160    fn test_retry_config_random_jitter_stays_within_bounds() {
6161        let _guard = test_guard!();
6162        let config = RetryConfig {
6163            base_delay_ms: 100,
6164            max_delay_ms: 200,
6165            jitter_factor: 0.25,
6166            ..Default::default()
6167        };
6168
6169        // Jitter applies after the exponential delay is capped. Exercise the
6170        // real RNG without requiring any particular random value or sequence.
6171        for (attempt, minimum_ms, maximum_ms) in [(1, 75, 125), (3, 150, 250)] {
6172            for _ in 0..128 {
6173                let delay = config.delay_for_attempt(attempt);
6174                assert!(delay >= std::time::Duration::from_millis(minimum_ms));
6175                assert!(delay <= std::time::Duration::from_millis(maximum_ms));
6176            }
6177        }
6178    }
6179
6180    #[test]
6181    fn test_retry_config_should_retry() {
6182        let _guard = test_guard!();
6183        let config = RetryConfig {
6184            max_attempts: 3,
6185            total_timeout_ms: 1000,
6186            ..Default::default()
6187        };
6188
6189        // Before max attempts and within timeout
6190        assert!(config.should_retry(0, std::time::Duration::from_millis(0)));
6191        assert!(config.should_retry(1, std::time::Duration::from_millis(500)));
6192        assert!(config.should_retry(2, std::time::Duration::from_millis(900)));
6193
6194        // At max attempts
6195        assert!(!config.should_retry(3, std::time::Duration::from_millis(0)));
6196
6197        // Past timeout
6198        assert!(!config.should_retry(1, std::time::Duration::from_millis(1001)));
6199    }
6200
6201    #[test]
6202    fn test_retry_config_serde_roundtrip() {
6203        let _guard = test_guard!();
6204        let config = RetryConfig {
6205            max_attempts: 5,
6206            base_delay_ms: 200,
6207            max_delay_ms: 8000,
6208            jitter_factor: 0.2,
6209            total_timeout_ms: 60000,
6210        };
6211        let json = serde_json::to_string(&config).unwrap();
6212        let parsed: RetryConfig = serde_json::from_str(&json).unwrap();
6213        assert_eq!(parsed.max_attempts, 5);
6214        assert_eq!(parsed.base_delay_ms, 200);
6215        assert_eq!(parsed.max_delay_ms, 8000);
6216        assert_eq!(parsed.jitter_factor, 0.2);
6217        assert_eq!(parsed.total_timeout_ms, 60000);
6218    }
6219
6220    #[test]
6221    fn test_transfer_config_includes_retry() {
6222        let _guard = test_guard!();
6223        let config = TransferConfig::default();
6224        assert_eq!(config.retry.max_attempts, 3);
6225    }
6226
6227    // =========================================================================
6228    // ExecutionConfig Tests (bd-785w)
6229    // =========================================================================
6230
6231    #[test]
6232    fn test_execution_config_default_allowlist() {
6233        let _guard = test_guard!();
6234        let config = ExecutionConfig::default();
6235        // All supported compilers should be in the default allowlist
6236        assert!(config.is_allowed("cargo"));
6237        assert!(config.is_allowed("rustc"));
6238        assert!(config.is_allowed("gcc"));
6239        assert!(config.is_allowed("g++"));
6240        assert!(config.is_allowed("clang"));
6241        assert!(config.is_allowed("clang++"));
6242        assert!(config.is_allowed("make"));
6243        assert!(config.is_allowed("cmake"));
6244        assert!(config.is_allowed("ninja"));
6245        assert!(config.is_allowed("meson"));
6246        assert!(config.is_allowed("bun"));
6247        assert!(config.is_allowed("nextest"));
6248        assert!(config.is_allowed("cc"));
6249        assert!(config.is_allowed("c++"));
6250    }
6251
6252    #[test]
6253    fn test_execution_config_is_allowed_case_insensitive() {
6254        let _guard = test_guard!();
6255        let config = ExecutionConfig::default();
6256        // Should be case-insensitive
6257        assert!(config.is_allowed("CARGO"));
6258        assert!(config.is_allowed("Cargo"));
6259        assert!(config.is_allowed("GCC"));
6260        assert!(config.is_allowed("Gcc"));
6261    }
6262
6263    #[test]
6264    fn test_execution_config_is_not_allowed() {
6265        let _guard = test_guard!();
6266        let config = ExecutionConfig::default();
6267        // Unknown commands should not be allowed
6268        assert!(!config.is_allowed("python"));
6269        assert!(!config.is_allowed("npm"));
6270        assert!(!config.is_allowed(""));
6271        // `go` and `tsc` ARE allowed now that Go/TypeScript are offload targets.
6272        // If they were missing from the allowlist the hook would fail open to
6273        // LOCAL execution — classification would look correct while every build
6274        // silently ran on the orchestrator.
6275        assert!(config.is_allowed("go"));
6276        assert!(config.is_allowed("tsc"));
6277    }
6278
6279    #[test]
6280    fn test_execution_config_empty_allowlist() {
6281        let _guard = test_guard!();
6282        let config = ExecutionConfig { allowlist: vec![] };
6283        // Empty allowlist should block everything
6284        assert!(!config.is_allowed("cargo"));
6285        assert!(!config.is_allowed("gcc"));
6286    }
6287
6288    #[test]
6289    fn test_execution_config_custom_allowlist() {
6290        let _guard = test_guard!();
6291        let config = ExecutionConfig {
6292            allowlist: vec!["cargo".to_string(), "custom_tool".to_string()],
6293        };
6294        assert!(config.is_allowed("cargo"));
6295        assert!(config.is_allowed("custom_tool"));
6296        assert!(!config.is_allowed("gcc"));
6297    }
6298
6299    #[test]
6300    fn test_execution_config_serde() {
6301        let _guard = test_guard!();
6302        let config = ExecutionConfig {
6303            allowlist: vec!["cargo".to_string(), "rustc".to_string()],
6304        };
6305        let json = serde_json::to_string(&config).unwrap();
6306        let parsed: ExecutionConfig = serde_json::from_str(&json).unwrap();
6307        assert_eq!(parsed.allowlist, config.allowlist);
6308    }
6309
6310    #[test]
6311    fn test_rch_config_includes_execution() {
6312        let _guard = test_guard!();
6313        let config = RchConfig::default();
6314        // RchConfig should include ExecutionConfig with default allowlist
6315        assert!(config.execution.is_allowed("cargo"));
6316    }
6317
6318    // ========================================================================
6319    // Adaptive Compression Tests (bd-243w)
6320    // ========================================================================
6321
6322    #[test]
6323    fn test_adaptive_compression_disabled_by_default() {
6324        let _guard = test_guard!();
6325        let config = TransferConfig::default();
6326        assert!(!config.adaptive_compression);
6327        // Should use fixed level when disabled
6328        assert_eq!(config.select_compression_level(Some(1_000_000)), 3);
6329        assert_eq!(config.select_compression_level(Some(500_000_000)), 3);
6330    }
6331
6332    #[test]
6333    fn test_adaptive_compression_small_payload() {
6334        let _guard = test_guard!();
6335        let config = TransferConfig {
6336            adaptive_compression: true,
6337            ..Default::default()
6338        };
6339        // < 10MB should use level 1
6340        assert_eq!(config.select_compression_level(Some(0)), 1);
6341        assert_eq!(config.select_compression_level(Some(1_000_000)), 1); // 1MB
6342        assert_eq!(config.select_compression_level(Some(9_999_999)), 1); // ~10MB
6343    }
6344
6345    #[test]
6346    fn test_adaptive_compression_medium_payload() {
6347        let _guard = test_guard!();
6348        let config = TransferConfig {
6349            adaptive_compression: true,
6350            ..Default::default()
6351        };
6352        // 10-200MB should use level 3
6353        assert_eq!(config.select_compression_level(Some(10_000_000)), 3); // 10MB
6354        assert_eq!(config.select_compression_level(Some(100_000_000)), 3); // 100MB
6355        assert_eq!(config.select_compression_level(Some(199_999_999)), 3); // ~200MB
6356    }
6357
6358    #[test]
6359    fn test_adaptive_compression_large_payload() {
6360        let _guard = test_guard!();
6361        let config = TransferConfig {
6362            adaptive_compression: true,
6363            ..Default::default()
6364        };
6365        // > 200MB should use level 7
6366        assert_eq!(config.select_compression_level(Some(200_000_000)), 7); // 200MB
6367        assert_eq!(config.select_compression_level(Some(500_000_000)), 7); // 500MB
6368        assert_eq!(config.select_compression_level(Some(1_000_000_000)), 7); // 1GB
6369    }
6370
6371    #[test]
6372    fn test_adaptive_compression_no_estimate() {
6373        let _guard = test_guard!();
6374        let config = TransferConfig {
6375            adaptive_compression: true,
6376            compression_level: 5,
6377            ..Default::default()
6378        };
6379        // No estimate should fallback to default level
6380        assert_eq!(config.select_compression_level(None), 5);
6381    }
6382
6383    #[test]
6384    fn test_adaptive_compression_respects_min_level() {
6385        let _guard = test_guard!();
6386        let config = TransferConfig {
6387            adaptive_compression: true,
6388            min_compression_level: 3,
6389            ..Default::default()
6390        };
6391        // Small payload would want level 1, but min is 3
6392        assert_eq!(config.select_compression_level(Some(1_000_000)), 3);
6393    }
6394
6395    #[test]
6396    fn test_adaptive_compression_respects_max_level() {
6397        let _guard = test_guard!();
6398        let config = TransferConfig {
6399            adaptive_compression: true,
6400            max_compression_level: 5,
6401            ..Default::default()
6402        };
6403        // Large payload would want level 7, but max is 5
6404        assert_eq!(config.select_compression_level(Some(500_000_000)), 5);
6405    }
6406
6407    #[test]
6408    fn test_adaptive_compression_serde_roundtrip() {
6409        let _guard = test_guard!();
6410        let config = TransferConfig {
6411            adaptive_compression: true,
6412            min_compression_level: 2,
6413            max_compression_level: 8,
6414            ..Default::default()
6415        };
6416        let json = serde_json::to_string(&config).unwrap();
6417        let parsed: TransferConfig = serde_json::from_str(&json).unwrap();
6418        assert!(parsed.adaptive_compression);
6419        assert_eq!(parsed.min_compression_level, 2);
6420        assert_eq!(parsed.max_compression_level, 8);
6421    }
6422
6423    // -------------------------------------------------------------------------
6424    // Self-Healing Config Tests (bd-59kg)
6425    // -------------------------------------------------------------------------
6426
6427    #[test]
6428    fn test_self_healing_config_defaults() {
6429        let _guard = test_guard!();
6430        // TEST START: SelfHealingConfig::default() returns expected values
6431        let config = SelfHealingConfig::default();
6432
6433        // Both self-healing behaviors enabled by default
6434        assert!(
6435            config.hook_starts_daemon,
6436            "hook_starts_daemon should be true by default"
6437        );
6438        assert!(
6439            config.daemon_installs_hooks,
6440            "daemon_installs_hooks should be true by default"
6441        );
6442
6443        // Timing defaults
6444        assert_eq!(
6445            config.auto_start_cooldown_secs, 30,
6446            "auto_start_cooldown_secs should default to 30"
6447        );
6448        assert_eq!(
6449            config.auto_start_timeout_secs, 3,
6450            "auto_start_timeout_secs should default to 3"
6451        );
6452        // TEST PASS: SelfHealingConfig defaults
6453    }
6454
6455    #[test]
6456    fn test_self_healing_config_serde_full() {
6457        let _guard = test_guard!();
6458        // TEST START: Full SelfHealingConfig serialization/deserialization
6459        let config = SelfHealingConfig {
6460            hook_starts_daemon: false,
6461            daemon_installs_hooks: false,
6462            auto_start_cooldown_secs: 60,
6463            auto_start_timeout_secs: 10,
6464            self_healing_log_level: SelfHealingLogLevel::Debug,
6465        };
6466
6467        let json = serde_json::to_string(&config).unwrap();
6468        assert!(json.contains("\"hook_starts_daemon\":false"));
6469        assert!(json.contains("\"daemon_installs_hooks\":false"));
6470        assert!(json.contains("\"auto_start_cooldown_secs\":60"));
6471        assert!(json.contains("\"auto_start_timeout_secs\":10"));
6472        assert!(json.contains("\"self_healing_log_level\":\"debug\""));
6473
6474        let parsed: SelfHealingConfig = serde_json::from_str(&json).unwrap();
6475        assert!(!parsed.hook_starts_daemon);
6476        assert!(!parsed.daemon_installs_hooks);
6477        assert_eq!(parsed.auto_start_cooldown_secs, 60);
6478        assert_eq!(parsed.auto_start_timeout_secs, 10);
6479        assert_eq!(parsed.self_healing_log_level, SelfHealingLogLevel::Debug);
6480        // TEST PASS: Full SelfHealingConfig serde
6481    }
6482
6483    // br-4zf3p: Tests for the self_healing_log_level field.
6484
6485    #[test]
6486    fn test_self_healing_log_level_default_is_info() {
6487        // TEST START: SelfHealingLogLevel::default() == Info
6488        let level = SelfHealingLogLevel::default();
6489        assert_eq!(level, SelfHealingLogLevel::Info);
6490        // Also verify the SelfHealingConfig default surfaces it.
6491        let config = SelfHealingConfig::default();
6492        assert_eq!(
6493            config.self_healing_log_level,
6494            SelfHealingLogLevel::Info,
6495            "config default should be Info"
6496        );
6497        // TEST PASS
6498    }
6499
6500    #[test]
6501    fn test_self_healing_log_level_serializes_lowercase() {
6502        // TEST START: serde uses lowercase variants
6503        let json = serde_json::to_string(&SelfHealingLogLevel::Warn).unwrap();
6504        assert_eq!(json, "\"warn\"");
6505        let parsed: SelfHealingLogLevel = serde_json::from_str("\"error\"").unwrap();
6506        assert_eq!(parsed, SelfHealingLogLevel::Error);
6507        // TEST PASS
6508    }
6509
6510    #[test]
6511    fn test_self_healing_log_level_from_env_str_accepts_known() {
6512        // TEST START: from_env_str parses each level (case-insensitive) and aliases
6513        assert_eq!(
6514            SelfHealingLogLevel::from_env_str("debug"),
6515            Some(SelfHealingLogLevel::Debug)
6516        );
6517        assert_eq!(
6518            SelfHealingLogLevel::from_env_str("DEBUG"),
6519            Some(SelfHealingLogLevel::Debug)
6520        );
6521        assert_eq!(
6522            SelfHealingLogLevel::from_env_str("info"),
6523            Some(SelfHealingLogLevel::Info)
6524        );
6525        assert_eq!(
6526            SelfHealingLogLevel::from_env_str("warn"),
6527            Some(SelfHealingLogLevel::Warn)
6528        );
6529        assert_eq!(
6530            SelfHealingLogLevel::from_env_str("warning"),
6531            Some(SelfHealingLogLevel::Warn),
6532            "warning should map to Warn"
6533        );
6534        assert_eq!(
6535            SelfHealingLogLevel::from_env_str("error"),
6536            Some(SelfHealingLogLevel::Error)
6537        );
6538        // Whitespace tolerance
6539        assert_eq!(
6540            SelfHealingLogLevel::from_env_str("  debug  "),
6541            Some(SelfHealingLogLevel::Debug)
6542        );
6543        // TEST PASS
6544    }
6545
6546    #[test]
6547    fn test_self_healing_log_level_from_env_str_rejects_unknown() {
6548        // TEST START: unknown values return None (caller falls back to default)
6549        assert!(SelfHealingLogLevel::from_env_str("banana").is_none());
6550        assert!(SelfHealingLogLevel::from_env_str("").is_none());
6551        assert!(SelfHealingLogLevel::from_env_str("trace").is_none());
6552        // TEST PASS
6553    }
6554
6555    #[test]
6556    fn test_self_healing_config_serde_partial_uses_defaults() {
6557        let _guard = test_guard!();
6558        // TEST START: Partial TOML/JSON uses defaults for missing fields
6559        let json = r#"{"hook_starts_daemon": false}"#;
6560        let config: SelfHealingConfig = serde_json::from_str(json).unwrap();
6561
6562        assert!(
6563            !config.hook_starts_daemon,
6564            "Explicit false should be parsed"
6565        );
6566        assert!(
6567            config.daemon_installs_hooks,
6568            "Missing field should use default (true)"
6569        );
6570        assert_eq!(
6571            config.auto_start_cooldown_secs, 30,
6572            "Missing field should use default (30)"
6573        );
6574        assert_eq!(
6575            config.auto_start_timeout_secs, 3,
6576            "Missing field should use default (3)"
6577        );
6578        // TEST PASS: Partial SelfHealingConfig uses defaults
6579    }
6580
6581    #[test]
6582    fn test_self_healing_config_toml_with_alias() {
6583        let _guard = test_guard!();
6584        // TEST START: daemon_start_timeout alias works for auto_start_timeout_secs
6585        let toml_str = r#"
6586            hook_starts_daemon = true
6587            daemon_installs_hooks = false
6588            auto_start_cooldown_secs = 45
6589            daemon_start_timeout = 7
6590        "#;
6591
6592        let config: SelfHealingConfig = toml::from_str(toml_str).unwrap();
6593        assert!(config.hook_starts_daemon);
6594        assert!(!config.daemon_installs_hooks);
6595        assert_eq!(config.auto_start_cooldown_secs, 45);
6596        assert_eq!(
6597            config.auto_start_timeout_secs, 7,
6598            "daemon_start_timeout alias should set auto_start_timeout_secs"
6599        );
6600        // TEST PASS: TOML alias works
6601    }
6602
6603    #[allow(unsafe_code)]
6604    mod self_healing_env_override_tests {
6605        use super::*;
6606        use crate::config::env_test_lock;
6607
6608        fn env_guard() -> std::sync::MutexGuard<'static, ()> {
6609            env_test_lock()
6610        }
6611
6612        fn set_env(key: &str, value: &str) {
6613            // SAFETY: Tests are serialized with env_guard().
6614            unsafe { std::env::set_var(key, value) };
6615        }
6616
6617        fn remove_env(key: &str) {
6618            // SAFETY: Tests are serialized with env_guard().
6619            unsafe { std::env::remove_var(key) };
6620        }
6621
6622        struct EnvVarGuard {
6623            key: &'static str,
6624            old: Option<String>,
6625        }
6626
6627        impl EnvVarGuard {
6628            fn set(key: &'static str, value: &str) -> Self {
6629                let old = std::env::var(key).ok();
6630                set_env(key, value);
6631                Self { key, old }
6632            }
6633        }
6634
6635        impl Drop for EnvVarGuard {
6636            fn drop(&mut self) {
6637                if let Some(old) = &self.old {
6638                    set_env(self.key, old);
6639                } else {
6640                    remove_env(self.key);
6641                }
6642            }
6643        }
6644
6645        #[test]
6646        fn test_self_healing_config_with_env_overrides_master_disable() {
6647            let _guard = test_guard!();
6648            let _guard = env_guard();
6649
6650            let _no_self_healing = EnvVarGuard::set("RCH_NO_SELF_HEALING", "1");
6651            let _hook_starts_daemon = EnvVarGuard::set("RCH_HOOK_STARTS_DAEMON", "1");
6652            let _daemon_installs_hooks = EnvVarGuard::set("RCH_DAEMON_INSTALLS_HOOKS", "1");
6653            let _cooldown = EnvVarGuard::set("RCH_AUTO_START_COOLDOWN_SECS", "99");
6654            let _timeout = EnvVarGuard::set("RCH_AUTO_START_TIMEOUT_SECS", "99");
6655
6656            let config = SelfHealingConfig::default().with_env_overrides();
6657            assert!(
6658                !config.hook_starts_daemon,
6659                "RCH_NO_SELF_HEALING should disable hook auto-start"
6660            );
6661            assert!(
6662                !config.daemon_installs_hooks,
6663                "RCH_NO_SELF_HEALING should disable daemon hook installation"
6664            );
6665
6666            // Master disable returns early; numeric overrides should not apply.
6667            assert_eq!(config.auto_start_cooldown_secs, 30);
6668            assert_eq!(config.auto_start_timeout_secs, 3);
6669        }
6670
6671        #[test]
6672        fn test_self_healing_config_with_env_overrides_toggles_and_numbers() {
6673            let _guard = test_guard!();
6674            let _guard = env_guard();
6675
6676            let _hook_starts_daemon = EnvVarGuard::set("RCH_HOOK_STARTS_DAEMON", "0");
6677            let _daemon_installs_hooks = EnvVarGuard::set("RCH_DAEMON_INSTALLS_HOOKS", "false");
6678            let _cooldown = EnvVarGuard::set("RCH_AUTO_START_COOLDOWN_SECS", "45");
6679            let _timeout = EnvVarGuard::set("RCH_AUTO_START_TIMEOUT_SECS", "7");
6680
6681            let config = SelfHealingConfig::default().with_env_overrides();
6682            assert!(!config.hook_starts_daemon);
6683            assert!(!config.daemon_installs_hooks);
6684            assert_eq!(config.auto_start_cooldown_secs, 45);
6685            assert_eq!(config.auto_start_timeout_secs, 7);
6686        }
6687
6688        #[test]
6689        fn test_self_healing_config_with_env_overrides_invalid_numbers_ignored() {
6690            let _guard = test_guard!();
6691            let _guard = env_guard();
6692
6693            let _cooldown = EnvVarGuard::set("RCH_AUTO_START_COOLDOWN_SECS", "not-a-number");
6694            let _timeout = EnvVarGuard::set("RCH_AUTO_START_TIMEOUT_SECS", "nope");
6695
6696            let config = SelfHealingConfig::default().with_env_overrides();
6697            assert_eq!(config.auto_start_cooldown_secs, 30);
6698            assert_eq!(config.auto_start_timeout_secs, 3);
6699        }
6700    }
6701
6702    // -------------------------------------------------------------------------
6703    // Preflight Health Guard Tests (bd-3eaa)
6704    // -------------------------------------------------------------------------
6705
6706    #[test]
6707    fn test_load_per_core_calculation() {
6708        let _guard = test_guard!();
6709        let mut caps = WorkerCapabilities::new();
6710        // No metrics -> None
6711        assert!(caps.load_per_core().is_none());
6712
6713        // Only load, no cpus -> None
6714        caps.load_avg_1 = Some(4.0);
6715        assert!(caps.load_per_core().is_none());
6716
6717        // Both available
6718        caps.num_cpus = Some(4);
6719        assert_eq!(caps.load_per_core(), Some(1.0)); // 4.0 / 4 = 1.0
6720
6721        // High load
6722        caps.load_avg_1 = Some(16.0);
6723        assert_eq!(caps.load_per_core(), Some(4.0)); // 16.0 / 4 = 4.0
6724    }
6725
6726    #[test]
6727    fn test_is_high_load() {
6728        let _guard = test_guard!();
6729        let mut caps = WorkerCapabilities::new();
6730        caps.load_avg_1 = Some(8.0);
6731        caps.num_cpus = Some(4);
6732        // load_per_core = 2.0
6733
6734        // Below threshold
6735        assert_eq!(caps.is_high_load(3.0), Some(false));
6736        // At threshold
6737        assert_eq!(caps.is_high_load(2.0), Some(false)); // equal is OK
6738        // Above threshold
6739        assert_eq!(caps.is_high_load(1.5), Some(true));
6740
6741        // No metrics -> None (fail-open)
6742        caps.load_avg_1 = None;
6743        assert!(caps.is_high_load(1.0).is_none());
6744    }
6745
6746    #[test]
6747    fn test_is_low_disk() {
6748        let _guard = test_guard!();
6749        let mut caps = WorkerCapabilities::new();
6750        caps.disk_free_gb = Some(15.0);
6751
6752        // Above threshold
6753        assert_eq!(caps.is_low_disk(10.0), Some(false));
6754        // At threshold
6755        assert_eq!(caps.is_low_disk(15.0), Some(false)); // equal is OK
6756        // Below threshold
6757        assert_eq!(caps.is_low_disk(20.0), Some(true));
6758
6759        // No metrics -> None (fail-open)
6760        caps.disk_free_gb = None;
6761        assert!(caps.is_low_disk(10.0).is_none());
6762    }
6763
6764    #[test]
6765    fn low_disk_judges_the_build_disk_not_a_small_tmpfs() {
6766        let _guard = test_guard!();
6767        // GH #78: a 16 GB tmpfs /tmp is the tightest mount by ratio, but the
6768        // build trees live on a roomy data disk.
6769        let mut caps = WorkerCapabilities::new();
6770        caps.disk_free_gb = Some(8.0);
6771        caps.disk_total_gb = Some(15.5);
6772        caps.build_disk_free_gb = Some(1700.0);
6773        caps.build_disk_total_gb = Some(1900.0);
6774        assert_eq!(caps.build_disk_gb(), (Some(1700.0), Some(1900.0)));
6775        assert_eq!(caps.is_low_disk(10.0), Some(false));
6776
6777        // A full build disk is low even when /tmp is roomy.
6778        caps.build_disk_free_gb = Some(4.0);
6779        assert_eq!(caps.is_low_disk(10.0), Some(true));
6780
6781        // An older rch-wkr without the build sample keeps the old behaviour.
6782        caps.build_disk_free_gb = None;
6783        caps.build_disk_total_gb = None;
6784        assert_eq!(caps.build_disk_gb(), (Some(8.0), Some(15.5)));
6785        assert_eq!(caps.is_low_disk(10.0), Some(true));
6786    }
6787
6788    #[test]
6789    fn build_disk_sample_is_optional_on_the_wire() {
6790        let _guard = test_guard!();
6791        let old: WorkerCapabilities =
6792            serde_json::from_str(r#"{"disk_free_gb":13.9,"disk_total_gb":15.5}"#).unwrap();
6793        assert_eq!(old.build_disk_free_gb, None);
6794        assert_eq!(old.build_disk_gb(), (Some(13.9), Some(15.5)));
6795
6796        let new: WorkerCapabilities = serde_json::from_str(
6797            r#"{"disk_free_gb":13.9,"disk_total_gb":15.5,"build_disk_free_gb":1700.0,"build_disk_total_gb":1900.0}"#,
6798        )
6799        .unwrap();
6800        assert_eq!(new.build_disk_gb(), (Some(1700.0), Some(1900.0)));
6801        let json = serde_json::to_string(&new).unwrap();
6802        assert!(json.contains("\"build_disk_free_gb\":1700.0"), "{json}");
6803    }
6804
6805    #[test]
6806    fn test_selection_config_preflight_defaults() {
6807        let _guard = test_guard!();
6808        let config = SelectionConfig::default();
6809        // Default thresholds
6810        assert_eq!(config.max_load_per_core, Some(2.0));
6811        assert_eq!(config.min_free_gb, Some(10.0));
6812        assert_eq!(config.disk_gb_per_slot, 10.0);
6813    }
6814
6815    #[test]
6816    fn test_selection_config_preflight_serde() {
6817        let _guard = test_guard!();
6818        let config = SelectionConfig {
6819            max_load_per_core: Some(3.5),
6820            min_free_gb: Some(25.0),
6821            disk_gb_per_slot: 0.83,
6822            ..Default::default()
6823        };
6824        let json = serde_json::to_string(&config).unwrap();
6825        let parsed: SelectionConfig = serde_json::from_str(&json).unwrap();
6826        assert_eq!(parsed.max_load_per_core, Some(3.5));
6827        assert_eq!(parsed.min_free_gb, Some(25.0));
6828        assert_eq!(parsed.disk_gb_per_slot, 0.83);
6829    }
6830
6831    #[test]
6832    fn test_selection_disk_slot_budget_validation() {
6833        let _guard = test_guard!();
6834        for value in ["0.0", "-1.0", "nan", "inf", "-inf"] {
6835            let result = toml::from_str::<SelectionConfig>(&format!("disk_gb_per_slot = {value}"));
6836            assert!(result.is_err(), "invalid disk slot budget {value}");
6837        }
6838        let default: SelectionConfig = toml::from_str("").unwrap();
6839        assert_eq!(default.disk_gb_per_slot, 10.0);
6840    }
6841
6842    #[test]
6843    fn test_selection_disk_weight_validation_and_roundtrip() {
6844        for value in ["-0.1", "1.1", "nan", "inf", "-inf"] {
6845            assert!(toml::from_str::<SelectionWeightConfig>(&format!("disk = {value}")).is_err());
6846        }
6847        for value in [0.0, 0.2, 1.0] {
6848            let weights: SelectionWeightConfig =
6849                toml::from_str(&format!("disk = {value}")).unwrap();
6850            assert_eq!(weights.disk, value);
6851            let roundtrip: SelectionWeightConfig =
6852                serde_json::from_str(&serde_json::to_string(&weights).unwrap()).unwrap();
6853            assert_eq!(roundtrip.disk, value);
6854        }
6855        let defaults: SelectionWeightConfig = toml::from_str("").unwrap();
6856        assert_eq!(defaults.disk, 0.2);
6857    }
6858
6859    #[test]
6860    fn test_worker_capabilities_health_metrics_serde() {
6861        let _guard = test_guard!();
6862        let caps = WorkerCapabilities {
6863            num_cpus: Some(8),
6864            load_avg_1: Some(1.5),
6865            load_avg_5: Some(2.0),
6866            load_avg_15: Some(1.8),
6867            disk_free_gb: Some(50.5),
6868            disk_total_gb: Some(100.0),
6869            ..Default::default()
6870        };
6871        let json = serde_json::to_string(&caps).unwrap();
6872        let parsed: WorkerCapabilities = serde_json::from_str(&json).unwrap();
6873        assert_eq!(parsed.num_cpus, Some(8));
6874        assert_eq!(parsed.load_avg_1, Some(1.5));
6875        assert_eq!(parsed.disk_free_gb, Some(50.5));
6876    }
6877
6878    #[test]
6879    fn microarch_level_classifies_ivy_bridge_as_v2() {
6880        // Abbreviated real flag set from ovh-b (Xeon E3-1245 V2, Ivy Bridge):
6881        // AVX present, but no avx2/bmi/fma — the bd-68hon SIGILL class.
6882        let ivy = "fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov \
6883                   pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx rdtscp lm \
6884                   constant_tsc pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 x2apic \
6885                   popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm";
6886        assert_eq!(x86_64_microarch_level_from_flags(ivy), 2);
6887    }
6888
6889    #[test]
6890    fn microarch_level_classifies_v3_and_v4() {
6891        let v3 = "cx16 lahf_lm popcnt sse4_1 sse4_2 ssse3 abm avx avx2 bmi1 bmi2 \
6892                  f16c fma movbe xsave";
6893        assert_eq!(x86_64_microarch_level_from_flags(v3), 3);
6894
6895        let v4 = format!("{v3} avx512f avx512bw avx512cd avx512dq avx512vl");
6896        assert_eq!(x86_64_microarch_level_from_flags(&v4), 4);
6897    }
6898
6899    #[test]
6900    fn microarch_level_v1_for_ancient_or_empty_flags() {
6901        assert_eq!(x86_64_microarch_level_from_flags(""), 1);
6902        assert_eq!(x86_64_microarch_level_from_flags("fpu mmx sse sse2"), 1);
6903    }
6904
6905    #[test]
6906    fn pre_v3_predicate_only_fires_on_affirmative_evidence() {
6907        let mut caps = WorkerCapabilities::new();
6908        assert!(!caps.is_pre_v3_x86(), "unknown must not deprioritize");
6909        caps.cpu_microarch_level = Some(2);
6910        assert!(caps.is_pre_v3_x86());
6911        caps.cpu_microarch_level = Some(3);
6912        assert!(!caps.is_pre_v3_x86());
6913    }
6914}