repolith-core 0.0.9

Declarative orchestrator for Rust toolchains spread across multiple sibling git repositories.
Documentation
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
//! Integration tests for the [`repolith_core::plan`] module.

use async_trait::async_trait;
use repolith_core::action::Action;
use repolith_core::cache::{Cache, Result as CacheResult};
use repolith_core::plan::{ChangeReason, Plan, PlanError};
use repolith_core::types::{ActionId, BuildError, BuildEvent, BuildOutput, Ctx, Sha256};
use std::collections::HashMap;
use std::path::PathBuf;
use tokio_util::sync::CancellationToken;

/// Minimal in-memory mock cache for plan tests.
struct MockCache {
    events: HashMap<ActionId, BuildEvent>,
}

impl MockCache {
    fn new() -> Self {
        Self {
            events: HashMap::new(),
        }
    }

    fn with_event(mut self, event: BuildEvent) -> Self {
        let id = match &event {
            BuildEvent::Success { id, .. } | BuildEvent::Failed { id, .. } => id.clone(),
        };
        self.events.insert(id, event);
        self
    }
}

#[async_trait]
impl Cache for MockCache {
    async fn last_build(&self, id: &ActionId) -> Option<BuildEvent> {
        self.events.get(id).cloned()
    }

    async fn record(&mut self, event: BuildEvent) -> CacheResult<()> {
        let id = match &event {
            BuildEvent::Success { id, .. } | BuildEvent::Failed { id, .. } => id.clone(),
        };
        self.events.insert(id, event);
        Ok(())
    }
}

/// Stub action whose id, deps, and `input_hash` are all configurable up front.
struct StubAction {
    id: ActionId,
    deps: Vec<ActionId>,
    hash: Sha256,
    /// What `output_present` reports. `true` matches the trait default.
    present: bool,
}

impl StubAction {
    fn new(name: &str, deps: &[&str], hash_byte: u8) -> Self {
        Self {
            id: ActionId(name.to_string()),
            deps: deps.iter().map(|d| ActionId((*d).to_string())).collect(),
            hash: Sha256([hash_byte; 32]),
            present: true,
        }
    }

    /// Simulate an artifact that is gone from this machine — deleted by
    /// hand, or never built here because a peer wrote the cache entry.
    fn without_artifact(mut self) -> Self {
        self.present = false;
        self
    }
}

#[async_trait]
impl Action for StubAction {
    fn id(&self) -> ActionId {
        self.id.clone()
    }

    fn deps(&self) -> Vec<ActionId> {
        self.deps.clone()
    }

    async fn input_hash(&self, _ctx: &Ctx) -> std::result::Result<Sha256, BuildError> {
        Ok(self.hash)
    }

    async fn execute(&self, _ctx: &Ctx) -> std::result::Result<BuildOutput, BuildError> {
        Ok(BuildOutput {
            output_hash: self.hash,
            stdout: String::new(),
        })
    }

    async fn output_present(&self, _ctx: &Ctx) -> bool {
        self.present
    }
}

fn ctx() -> Ctx {
    Ctx {
        cancel: CancellationToken::new(),
        workdir: PathBuf::from("/tmp"),
        env: HashMap::new(),
    }
}

fn aid(s: &str) -> ActionId {
    ActionId(s.to_string())
}

fn boxed(actions: Vec<StubAction>) -> Vec<Box<dyn Action>> {
    actions
        .into_iter()
        .map(|a| Box::new(a) as Box<dyn Action>)
        .collect()
}

#[tokio::test]
async fn test_diamond_layers() {
    // A → {B, C} → D ; expected layers: [[A], [B,C], [D]]
    let actions = boxed(vec![
        StubAction::new("A", &[], 0),
        StubAction::new("B", &["A"], 0),
        StubAction::new("C", &["A"], 0),
        StubAction::new("D", &["B", "C"], 0),
    ]);
    let cache = MockCache::new();
    let plan = Plan::compute(&actions, &cache, &ctx()).await.unwrap();
    let layers = plan.layers();
    assert_eq!(layers.len(), 3, "expected 3 layers, got {layers:?}");
    assert_eq!(layers[0], vec![aid("A")]);
    assert_eq!(layers[1], vec![aid("B"), aid("C")]); // sorted alphabetically
    assert_eq!(layers[2], vec![aid("D")]);
}

#[tokio::test]
async fn test_cycle() {
    // A → B → C → A
    let actions = boxed(vec![
        StubAction::new("A", &["C"], 0),
        StubAction::new("B", &["A"], 0),
        StubAction::new("C", &["B"], 0),
    ]);
    let cache = MockCache::new();
    let err = Plan::compute(&actions, &cache, &ctx()).await.unwrap_err();
    match err {
        PlanError::Cycle(ids) => {
            assert_eq!(ids, vec![aid("A"), aid("B"), aid("C")]);
        }
        other => panic!("expected Cycle, got {other:?}"),
    }
}

#[tokio::test]
async fn test_missing_dep() {
    let actions = boxed(vec![StubAction::new("A", &["GHOST"], 0)]);
    let cache = MockCache::new();
    let err = Plan::compute(&actions, &cache, &ctx()).await.unwrap_err();
    match err {
        PlanError::MissingDep { from, to } => {
            assert_eq!(from, aid("A"));
            assert_eq!(to, aid("GHOST"));
        }
        other => panic!("expected MissingDep, got {other:?}"),
    }
}

#[tokio::test]
async fn test_hash_changed() {
    // Cached input = [0x01;32], current input = [0x02;32]
    let cached = Sha256([0x01; 32]);
    let actions = boxed(vec![StubAction::new("A", &[], 0x02)]);
    let cache = MockCache::new().with_event(BuildEvent::Success {
        id: aid("A"),
        input: cached,
        output: Sha256([0; 32]),
        ms: 1,
    });
    let plan = Plan::compute(&actions, &cache, &ctx()).await.unwrap();
    match plan.reasons().get(&aid("A")) {
        Some(ChangeReason::InputHashChanged { from, to }) => {
            assert_eq!(*from, cached);
            assert_eq!(*to, Sha256([0x02; 32]));
        }
        other => panic!("expected InputHashChanged, got {other:?}"),
    }
}

#[tokio::test]
async fn test_cascade_upstream_moved() {
    // A: no cached build → stale (NoCachedBuild)
    // B depends on A, B's hash unchanged vs cache → UpstreamMoved{A}
    let b_hash = Sha256([0x42; 32]);
    let actions = boxed(vec![
        StubAction::new("A", &[], 0x01),
        StubAction::new("B", &["A"], 0x42),
    ]);
    // Cache has B's prior build with matching input hash, but no entry for A.
    let cache = MockCache::new().with_event(BuildEvent::Success {
        id: aid("B"),
        input: b_hash,
        output: Sha256([0; 32]),
        ms: 1,
    });
    let plan = Plan::compute(&actions, &cache, &ctx()).await.unwrap();
    assert_eq!(
        plan.reasons().get(&aid("A")),
        Some(&ChangeReason::NoCachedBuild)
    );
    match plan.reasons().get(&aid("B")) {
        Some(ChangeReason::UpstreamMoved { dep }) => assert_eq!(*dep, aid("A")),
        other => panic!("expected UpstreamMoved, got {other:?}"),
    }
}

#[tokio::test]
async fn test_failed_prior_rebuilds() {
    // Cached BuildEvent::Failed → must always re-run as NoCachedBuild
    let actions = boxed(vec![StubAction::new("A", &[], 0x01)]);
    let cache = MockCache::new().with_event(BuildEvent::Failed {
        id: aid("A"),
        input: Sha256([0x01; 32]),
        error: BuildError::Cancelled,
        ms: 1,
    });
    let plan = Plan::compute(&actions, &cache, &ctx()).await.unwrap();
    assert_eq!(
        plan.reasons().get(&aid("A")),
        Some(&ChangeReason::NoCachedBuild)
    );
}

#[tokio::test]
async fn test_empty_actions() {
    let actions: Vec<Box<dyn Action>> = vec![];
    let cache = MockCache::new();
    let plan = Plan::compute(&actions, &cache, &ctx()).await.unwrap();
    assert!(plan.layers().is_empty());
    assert!(plan.reasons().is_empty());
    assert_eq!(plan.stale().count(), 0);
}

#[tokio::test]
async fn test_cancel_during_compute_returns_cancelled() {
    // Two-layer plan; pre-cancel the token so `Plan::compute` should
    // bail at the top of the second layer iteration with
    // `PlanError::Build(BuildError::Cancelled)`.
    let actions = boxed(vec![
        StubAction::new("A", &[], 0),
        StubAction::new("B", &["A"], 0),
    ]);
    let cache = MockCache::new();
    let cancel = CancellationToken::new();
    cancel.cancel();
    let ctx = Ctx {
        cancel,
        workdir: PathBuf::from("/tmp"),
        env: HashMap::new(),
    };
    let err = Plan::compute(&actions, &cache, &ctx).await.unwrap_err();
    match err {
        PlanError::Build(BuildError::Cancelled) => (),
        other => panic!("expected Build(Cancelled), got {other:?}"),
    }
}

/// Stub action whose `input_hash` sleeps for a configurable duration.
/// Used to verify cancel interrupts a layer's in-flight fan-out.
struct SlowAction {
    id: ActionId,
    delay: std::time::Duration,
}

#[async_trait]
impl Action for SlowAction {
    fn id(&self) -> ActionId {
        self.id.clone()
    }
    fn deps(&self) -> Vec<ActionId> {
        Vec::new()
    }
    async fn input_hash(&self, _ctx: &Ctx) -> std::result::Result<Sha256, BuildError> {
        tokio::time::sleep(self.delay).await;
        Ok(Sha256([0; 32]))
    }
    async fn execute(&self, _ctx: &Ctx) -> std::result::Result<BuildOutput, BuildError> {
        Ok(BuildOutput {
            output_hash: Sha256([0; 32]),
            stdout: String::new(),
        })
    }
}

#[tokio::test]
async fn test_cancel_during_layer_fanout_short_circuits() {
    // Layer of 4 slow actions, each sleeping 10s in `input_hash`. Fire
    // cancel 100ms after compute starts. Without the inner select! against
    // ctx.cancel the compute would wait the full 10s. With it, the futures
    // observe the cancel and return Cancelled in ~the cancel delay.
    let actions: Vec<Box<dyn Action>> = (0..4)
        .map(|i| {
            Box::new(SlowAction {
                id: aid(&format!("slow-{i}")),
                delay: std::time::Duration::from_secs(10),
            }) as Box<dyn Action>
        })
        .collect();
    let cache = MockCache::new();
    let cancel = CancellationToken::new();
    let ctx = Ctx {
        cancel: cancel.clone(),
        workdir: PathBuf::from("/tmp"),
        env: HashMap::new(),
    };
    let canceller = tokio::spawn(async move {
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        cancel.cancel();
    });
    let start = std::time::Instant::now();
    let result = Plan::compute(&actions, &cache, &ctx).await;
    let elapsed = start.elapsed();
    canceller.await.unwrap();
    assert!(
        elapsed < std::time::Duration::from_secs(2),
        "compute should short-circuit on cancel; took {elapsed:?}"
    );
    match result {
        Err(PlanError::Build(BuildError::Cancelled)) => (),
        other => panic!("expected Build(Cancelled), got {other:?}"),
    }
}

// ---------------------------------------------------------------------------
// OutputMissing — a cached Success is not proof the artifact is still here
// (issue #73)
// ---------------------------------------------------------------------------

/// The shared-cache regression: machine A records a Success, machine B has
/// the same manifest and the same (shared) cache but no artifact. Before
/// this, B reported `0 stale actions` and installed nothing at all.
#[tokio::test]
async fn artifact_missing_is_stale_even_when_the_hash_matches() {
    let action = StubAction::new("a", &[], 1).without_artifact();
    let hash = Sha256([1; 32]);
    let cache = MockCache::new().with_event(BuildEvent::Success {
        id: aid("a"),
        input: hash,
        output: hash,
        ms: 1,
    });

    let plan = Plan::compute(&boxed(vec![action]), &cache, &ctx())
        .await
        .expect("plan");

    assert_eq!(
        plan.reasons().get(&aid("a")),
        Some(&ChangeReason::OutputMissing),
        "a cached Success with no artifact on this machine must be stale"
    );
}

/// Same inputs, artifact present: still a no-op. Guards against the fix
/// turning every sync into a rebuild.
#[tokio::test]
async fn artifact_present_with_matching_hash_stays_up_to_date() {
    let hash = Sha256([1; 32]);
    let cache = MockCache::new().with_event(BuildEvent::Success {
        id: aid("a"),
        input: hash,
        output: hash,
        ms: 1,
    });

    let plan = Plan::compute(&boxed(vec![StubAction::new("a", &[], 1)]), &cache, &ctx())
        .await
        .expect("plan");

    assert!(
        plan.reasons().is_empty(),
        "unchanged inputs + present artifact must stay up to date, got {:?}",
        plan.reasons()
    );
}

/// A changed hash still wins over the presence probe — the reason must
/// stay the more informative `InputHashChanged`.
#[tokio::test]
async fn changed_hash_outranks_missing_artifact() {
    let cache = MockCache::new().with_event(BuildEvent::Success {
        id: aid("a"),
        input: Sha256([9; 32]),
        output: Sha256([9; 32]),
        ms: 1,
    });

    let plan = Plan::compute(
        &boxed(vec![StubAction::new("a", &[], 1).without_artifact()]),
        &cache,
        &ctx(),
    )
    .await
    .expect("plan");

    assert!(
        matches!(
            plan.reasons().get(&aid("a")),
            Some(ChangeReason::InputHashChanged { .. })
        ),
        "got {:?}",
        plan.reasons().get(&aid("a"))
    );
}