rch 2.1.10

Remote Compilation Helper - PreToolUse hook CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//! Deployment planning and data structures.

use chrono::{DateTime, Utc};
use rch_common::WorkerConfig;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// A complete deployment plan.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeploymentPlan {
    /// Unique identifier for this deployment.
    pub id: Uuid,
    /// When the plan was created.
    pub created_at: DateTime<Utc>,
    /// Target version to deploy.
    pub target_version: String,
    /// Workers to deploy to.
    pub workers: Vec<WorkerDeployment>,
    /// Deployment strategy.
    pub strategy: DeploymentStrategy,
    /// Deployment options.
    pub options: DeployOptions,
}

impl DeploymentPlan {
    /// Create a new deployment plan.
    pub fn new(
        workers: &[&WorkerConfig],
        strategy: DeploymentStrategy,
        options: DeployOptions,
    ) -> anyhow::Result<Self> {
        let target_version = options
            .target_version
            .clone()
            .unwrap_or_else(|| env!("CARGO_PKG_VERSION").to_string());

        let worker_deployments = workers
            .iter()
            .map(|w| WorkerDeployment::new(&w.id.0, &target_version))
            .collect();

        Ok(Self {
            id: Uuid::new_v4(),
            created_at: Utc::now(),
            target_version,
            workers: worker_deployments,
            strategy,
            options,
        })
    }
}

/// Deployment strategy determining rollout behavior.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DeploymentStrategy {
    /// Deploy to all workers at once with parallelism limit.
    AllAtOnce { parallelism: usize },
    /// Canary deployment - deploy to subset first, then all.
    Canary {
        /// Percentage of workers for initial canary.
        percent: u8,
        /// Seconds to wait after canary before full rollout.
        wait_secs: u64,
        /// Automatically promote after successful canary.
        auto_promote: bool,
    },
    /// Rolling deployment - deploy in batches.
    Rolling {
        /// Number of workers per batch.
        batch_size: usize,
        /// Seconds to wait between batches.
        wait_between: u64,
    },
}

/// Options controlling deployment behavior.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DeployOptions {
    /// Force deployment even if version matches.
    pub force: bool,
    /// Run verification after deployment.
    pub verify: bool,
    /// Drain active builds before deployment.
    pub drain_first: bool,
    /// Timeout for draining in seconds.
    pub drain_timeout: u64,
    /// Skip toolchain synchronization.
    pub no_toolchain: bool,
    /// Resume from previous failed deployment.
    pub resume: bool,
    /// Target version to deploy (None = current local).
    pub target_version: Option<String>,
}

/// Deployment state for a single worker.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkerDeployment {
    /// Worker identifier.
    pub worker_id: String,
    /// Current installed version (if known).
    pub current_version: Option<String>,
    /// Target version to deploy.
    pub target_version: String,
    /// Current deployment status.
    pub status: DeploymentStatus,
    /// Deployment steps with their status.
    pub steps: Vec<DeployStep>,
    /// When deployment started.
    pub started_at: Option<DateTime<Utc>>,
    /// When deployment completed.
    pub completed_at: Option<DateTime<Utc>>,
    /// Error message if failed.
    pub error: Option<String>,
}

impl WorkerDeployment {
    /// Create a new worker deployment entry.
    pub fn new(worker_id: &str, target_version: &str) -> Self {
        Self {
            worker_id: worker_id.to_string(),
            current_version: None,
            target_version: target_version.to_string(),
            status: DeploymentStatus::Pending,
            steps: vec![
                DeployStep::new("preflight"),
                DeployStep::new("transfer"),
                DeployStep::new("install"),
                DeployStep::new("verify"),
            ],
            started_at: None,
            completed_at: None,
            error: None,
        }
    }

    /// Check if this deployment can transition to the given status.
    pub fn can_transition_to(&self, status: DeploymentStatus) -> bool {
        use DeploymentStatus::*;
        matches!(
            (&self.status, status),
            (Pending, Preflight)
                | (Preflight, Draining)
                | (Preflight, Transferring)
                | (Draining, Transferring)
                | (Transferring, Installing)
                | (Installing, Verifying)
                | (Verifying, Completed)
                | (_, Failed)
                | (_, Skipped)
                | (_, RolledBack)
        )
    }
}

/// Status of a worker deployment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DeploymentStatus {
    /// Not yet started.
    Pending,
    /// Running preflight checks.
    Preflight,
    /// Draining active builds.
    Draining,
    /// Transferring binaries.
    Transferring,
    /// Installing binaries.
    Installing,
    /// Running verification.
    Verifying,
    /// Successfully completed.
    Completed,
    /// Failed with error.
    Failed,
    /// Skipped (already at target version).
    Skipped,
    /// Rolled back after failure.
    RolledBack,
}

/// A single step in the deployment process.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeployStep {
    /// Step name.
    pub name: String,
    /// Step status.
    pub status: StepStatus,
    /// When step started.
    pub started_at: Option<DateTime<Utc>>,
    /// When step completed.
    pub completed_at: Option<DateTime<Utc>>,
    /// Step output or error message.
    pub output: Option<String>,
}

impl DeployStep {
    /// Create a new deployment step.
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            status: StepStatus::Pending,
            started_at: None,
            completed_at: None,
            output: None,
        }
    }

    /// Mark step as in progress.
    pub fn start(&mut self) {
        self.status = StepStatus::InProgress;
        self.started_at = Some(Utc::now());
    }

    /// Mark step as completed.
    pub fn complete(&mut self, output: Option<String>) {
        self.status = StepStatus::Completed;
        self.completed_at = Some(Utc::now());
        self.output = output;
    }

    /// Mark step as failed.
    pub fn fail(&mut self, error: String) {
        self.status = StepStatus::Failed;
        self.completed_at = Some(Utc::now());
        self.output = Some(error);
    }
}

/// Status of a deployment step.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum StepStatus {
    /// Not yet started.
    Pending,
    /// Currently running.
    InProgress,
    /// Successfully completed.
    Completed,
    /// Failed with error.
    Failed,
    /// Skipped.
    Skipped,
}

#[cfg(test)]
mod tests {
    use super::*;

    // ========================
    // DeployStep tests
    // ========================

    #[test]
    fn deploy_step_new_creates_pending_step() {
        let step = DeployStep::new("preflight");
        assert_eq!(step.name, "preflight");
        assert_eq!(step.status, StepStatus::Pending);
        assert!(step.started_at.is_none());
        assert!(step.completed_at.is_none());
        assert!(step.output.is_none());
    }

    #[test]
    fn deploy_step_start_sets_in_progress_and_timestamp() {
        let mut step = DeployStep::new("transfer");
        step.start();
        assert_eq!(step.status, StepStatus::InProgress);
        assert!(step.started_at.is_some());
        assert!(step.completed_at.is_none());
    }

    #[test]
    fn deploy_step_complete_sets_completed_and_timestamps() {
        let mut step = DeployStep::new("install");
        step.start();
        step.complete(Some("Installed successfully".to_string()));
        assert_eq!(step.status, StepStatus::Completed);
        assert!(step.started_at.is_some());
        assert!(step.completed_at.is_some());
        assert_eq!(step.output, Some("Installed successfully".to_string()));
    }

    #[test]
    fn deploy_step_complete_without_output() {
        let mut step = DeployStep::new("verify");
        step.start();
        step.complete(None);
        assert_eq!(step.status, StepStatus::Completed);
        assert!(step.output.is_none());
    }

    #[test]
    fn deploy_step_fail_sets_failed_and_error_message() {
        let mut step = DeployStep::new("transfer");
        step.start();
        step.fail("Connection refused".to_string());
        assert_eq!(step.status, StepStatus::Failed);
        assert!(step.completed_at.is_some());
        assert_eq!(step.output, Some("Connection refused".to_string()));
    }

    // ========================
    // WorkerDeployment tests
    // ========================

    #[test]
    fn worker_deployment_new_creates_pending_deployment() {
        let wd = WorkerDeployment::new("worker-1", "1.0.0");
        assert_eq!(wd.worker_id, "worker-1");
        assert!(wd.current_version.is_none());
        assert_eq!(wd.target_version, "1.0.0");
        assert_eq!(wd.status, DeploymentStatus::Pending);
        assert_eq!(wd.steps.len(), 4);
        assert!(wd.started_at.is_none());
        assert!(wd.completed_at.is_none());
        assert!(wd.error.is_none());
    }

    #[test]
    fn worker_deployment_new_creates_correct_steps() {
        let wd = WorkerDeployment::new("w1", "2.0.0");
        assert_eq!(wd.steps[0].name, "preflight");
        assert_eq!(wd.steps[1].name, "transfer");
        assert_eq!(wd.steps[2].name, "install");
        assert_eq!(wd.steps[3].name, "verify");
        for step in &wd.steps {
            assert_eq!(step.status, StepStatus::Pending);
        }
    }

    #[test]
    fn worker_deployment_can_transition_pending_to_preflight() {
        let wd = WorkerDeployment::new("w1", "1.0.0");
        assert!(wd.can_transition_to(DeploymentStatus::Preflight));
    }

    #[test]
    fn worker_deployment_can_transition_preflight_to_draining() {
        let mut wd = WorkerDeployment::new("w1", "1.0.0");
        wd.status = DeploymentStatus::Preflight;
        assert!(wd.can_transition_to(DeploymentStatus::Draining));
    }

    #[test]
    fn worker_deployment_can_transition_preflight_to_transferring() {
        let mut wd = WorkerDeployment::new("w1", "1.0.0");
        wd.status = DeploymentStatus::Preflight;
        assert!(wd.can_transition_to(DeploymentStatus::Transferring));
    }

    #[test]
    fn worker_deployment_can_transition_draining_to_transferring() {
        let mut wd = WorkerDeployment::new("w1", "1.0.0");
        wd.status = DeploymentStatus::Draining;
        assert!(wd.can_transition_to(DeploymentStatus::Transferring));
    }

    #[test]
    fn worker_deployment_can_transition_transferring_to_installing() {
        let mut wd = WorkerDeployment::new("w1", "1.0.0");
        wd.status = DeploymentStatus::Transferring;
        assert!(wd.can_transition_to(DeploymentStatus::Installing));
    }

    #[test]
    fn worker_deployment_can_transition_installing_to_verifying() {
        let mut wd = WorkerDeployment::new("w1", "1.0.0");
        wd.status = DeploymentStatus::Installing;
        assert!(wd.can_transition_to(DeploymentStatus::Verifying));
    }

    #[test]
    fn worker_deployment_can_transition_verifying_to_completed() {
        let mut wd = WorkerDeployment::new("w1", "1.0.0");
        wd.status = DeploymentStatus::Verifying;
        assert!(wd.can_transition_to(DeploymentStatus::Completed));
    }

    #[test]
    fn worker_deployment_can_transition_any_to_failed() {
        for status in [
            DeploymentStatus::Pending,
            DeploymentStatus::Preflight,
            DeploymentStatus::Draining,
            DeploymentStatus::Transferring,
            DeploymentStatus::Installing,
            DeploymentStatus::Verifying,
        ] {
            let mut wd = WorkerDeployment::new("w1", "1.0.0");
            wd.status = status;
            assert!(wd.can_transition_to(DeploymentStatus::Failed));
        }
    }

    #[test]
    fn worker_deployment_can_transition_any_to_skipped() {
        let mut wd = WorkerDeployment::new("w1", "1.0.0");
        wd.status = DeploymentStatus::Pending;
        assert!(wd.can_transition_to(DeploymentStatus::Skipped));
    }

    #[test]
    fn worker_deployment_can_transition_any_to_rolledback() {
        let mut wd = WorkerDeployment::new("w1", "1.0.0");
        wd.status = DeploymentStatus::Installing;
        assert!(wd.can_transition_to(DeploymentStatus::RolledBack));
    }

    #[test]
    fn worker_deployment_cannot_transition_pending_to_transferring() {
        let wd = WorkerDeployment::new("w1", "1.0.0");
        assert!(!wd.can_transition_to(DeploymentStatus::Transferring));
    }

    #[test]
    fn worker_deployment_cannot_transition_pending_to_completed() {
        let wd = WorkerDeployment::new("w1", "1.0.0");
        assert!(!wd.can_transition_to(DeploymentStatus::Completed));
    }

    // ========================
    // DeployOptions tests
    // ========================

    #[test]
    fn deploy_options_default_all_false() {
        let opts = DeployOptions::default();
        assert!(!opts.force);
        assert!(!opts.verify);
        assert!(!opts.drain_first);
        assert_eq!(opts.drain_timeout, 0);
        assert!(!opts.no_toolchain);
        assert!(!opts.resume);
        assert!(opts.target_version.is_none());
    }

    // ========================
    // DeploymentStrategy serialization tests
    // ========================

    #[test]
    fn deployment_strategy_all_at_once_serializes() {
        let strategy = DeploymentStrategy::AllAtOnce { parallelism: 4 };
        let json = serde_json::to_string(&strategy).unwrap();
        assert!(json.contains("AllAtOnce"));
        assert!(json.contains("4"));
    }

    #[test]
    fn deployment_strategy_canary_serializes() {
        let strategy = DeploymentStrategy::Canary {
            percent: 10,
            wait_secs: 60,
            auto_promote: true,
        };
        let json = serde_json::to_string(&strategy).unwrap();
        assert!(json.contains("Canary"));
        assert!(json.contains("10"));
        assert!(json.contains("60"));
        assert!(json.contains("true"));
    }

    #[test]
    fn deployment_strategy_rolling_serializes() {
        let strategy = DeploymentStrategy::Rolling {
            batch_size: 2,
            wait_between: 30,
        };
        let json = serde_json::to_string(&strategy).unwrap();
        assert!(json.contains("Rolling"));
        assert!(json.contains("2"));
        assert!(json.contains("30"));
    }

    #[test]
    fn deployment_strategy_deserializes_roundtrip() {
        let strategy = DeploymentStrategy::Canary {
            percent: 25,
            wait_secs: 120,
            auto_promote: false,
        };
        let json = serde_json::to_string(&strategy).unwrap();
        let restored: DeploymentStrategy = serde_json::from_str(&json).unwrap();
        match restored {
            DeploymentStrategy::Canary {
                percent,
                wait_secs,
                auto_promote,
            } => {
                assert_eq!(percent, 25);
                assert_eq!(wait_secs, 120);
                assert!(!auto_promote);
            }
            _ => panic!("Expected Canary strategy"),
        }
    }

    // ========================
    // DeploymentStatus tests
    // ========================

    #[test]
    fn deployment_status_variants_are_distinct() {
        let statuses = [
            DeploymentStatus::Pending,
            DeploymentStatus::Preflight,
            DeploymentStatus::Draining,
            DeploymentStatus::Transferring,
            DeploymentStatus::Installing,
            DeploymentStatus::Verifying,
            DeploymentStatus::Completed,
            DeploymentStatus::Failed,
            DeploymentStatus::Skipped,
            DeploymentStatus::RolledBack,
        ];
        for (i, s1) in statuses.iter().enumerate() {
            for (j, s2) in statuses.iter().enumerate() {
                if i == j {
                    assert_eq!(s1, s2);
                } else {
                    assert_ne!(s1, s2);
                }
            }
        }
    }

    #[test]
    fn deployment_status_serializes() {
        let status = DeploymentStatus::Completed;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, "\"Completed\"");
    }

    // ========================
    // StepStatus tests
    // ========================

    #[test]
    fn step_status_variants_are_distinct() {
        let statuses = [
            StepStatus::Pending,
            StepStatus::InProgress,
            StepStatus::Completed,
            StepStatus::Failed,
            StepStatus::Skipped,
        ];
        for (i, s1) in statuses.iter().enumerate() {
            for (j, s2) in statuses.iter().enumerate() {
                if i == j {
                    assert_eq!(s1, s2);
                } else {
                    assert_ne!(s1, s2);
                }
            }
        }
    }

    #[test]
    fn step_status_serializes() {
        let status = StepStatus::InProgress;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, "\"InProgress\"");
    }

    // ========================
    // WorkerDeployment serialization tests
    // ========================

    #[test]
    fn worker_deployment_serializes_roundtrip() {
        let wd = WorkerDeployment::new("worker-test", "3.0.0");
        let json = serde_json::to_string(&wd).unwrap();
        let restored: WorkerDeployment = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.worker_id, "worker-test");
        assert_eq!(restored.target_version, "3.0.0");
        assert_eq!(restored.status, DeploymentStatus::Pending);
    }

    // ========================
    // DeployOptions serialization tests
    // ========================

    #[test]
    fn deploy_options_serializes_roundtrip() {
        let opts = DeployOptions {
            force: true,
            verify: true,
            drain_first: false,
            drain_timeout: 30,
            no_toolchain: true,
            resume: false,
            target_version: Some("2.0.0".to_string()),
        };
        let json = serde_json::to_string(&opts).unwrap();
        let restored: DeployOptions = serde_json::from_str(&json).unwrap();
        assert!(restored.force);
        assert!(restored.verify);
        assert!(!restored.drain_first);
        assert_eq!(restored.drain_timeout, 30);
        assert!(restored.no_toolchain);
        assert!(!restored.resume);
        assert_eq!(restored.target_version, Some("2.0.0".to_string()));
    }

    // ========================
    // DeployStep serialization tests
    // ========================

    #[test]
    fn deploy_step_serializes_roundtrip() {
        let mut step = DeployStep::new("test-step");
        step.start();
        step.complete(Some("Done!".to_string()));

        let json = serde_json::to_string(&step).unwrap();
        let restored: DeployStep = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.name, "test-step");
        assert_eq!(restored.status, StepStatus::Completed);
        assert!(restored.started_at.is_some());
        assert!(restored.completed_at.is_some());
        assert_eq!(restored.output, Some("Done!".to_string()));
    }
}