artifactize-core 0.21.1

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
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
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
mod states;
mod view;
pub use states::{ArtifactCondition, EvalCondition, VerifyAction};
#[cfg(test)]
mod tests;

use std::{
    collections::{BTreeMap, BTreeSet},
    path::Path,
};

use serde::Serialize;
use tokio_util::sync::CancellationToken;

use crate::{
    cache,
    config::{Profile, read_workspace_config},
    graph::{Evidence, Graph, Readiness},
    project::{VerifyOptions, selection::select_profiles},
    remote::Session,
    store::{self, Claim, LastRequest},
};

use super::selection::Selection;

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StatusView {
    pub selection: Selection,
    pub recursive: bool,
    pub force: bool,
    pub selected_eval_ids: Vec<crate::config::EvalId>,
    pub included_eval_ids: Vec<crate::config::EvalId>,
    pub satisfied: bool,
    pub obligations: Vec<crate::config::ArtifactName>,
    pub artifacts: Vec<ArtifactState>,
    pub evals: Vec<EvalState>,
    pub counts: Counts,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub unreadable: Vec<store::Unreadable>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ArtifactState {
    pub id: crate::config::ArtifactName,
    pub kind: crate::config::ArtifactKind,
    #[serde(with = "crate::platform::path_serde")]
    pub path: std::path::PathBuf,
    pub tags: Vec<String>,
    pub state: ArtifactCondition,
    pub reason: String,
    pub eval_ids: Vec<crate::config::EvalId>,
    pub passed: usize,
    pub total: usize,
    pub satisfied: bool,
    pub obligations: Vec<crate::config::ArtifactName>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EvalState {
    pub id: crate::config::EvalId,
    pub target: crate::config::ArtifactName,
    pub title: String,
    pub profile: Profile,
    pub selected: bool,
    pub included: bool,
    pub force: bool,
    pub state: EvalCondition,
    pub action: VerifyAction,
    pub reason: String,
    pub blocked_by: Vec<crate::store::Blocker>,
    pub obligations: Vec<crate::config::ArtifactName>,
    /// The Eval definition hash: the evaluation criteria the key covers.
    pub eval_def_hash: crate::types::DefinitionHash,
    /// The target's current fingerprint; null without one.
    pub fingerprint: Option<crate::types::Fingerprint>,
    /// Each Artifact the eval depends on, with its current fingerprint or null without one.
    pub fingerprints: BTreeMap<crate::config::ArtifactName, Option<crate::types::Fingerprint>>,
    /// The reuse key composed of the hash and the fingerprints; null when one is missing.
    pub key: Option<crate::types::ReuseKey>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last: Option<LastRequest>,
    /// Why the key no longer matches the newest cached result for this Eval definition: the
    /// target's changed files and the dependency Artifacts whose fingerprints changed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub changes: Option<cache::Changes>,
}

#[derive(Debug, Default, Serialize)]
pub struct Counts {
    pub artifacts: BTreeMap<ArtifactCondition, usize>,
    pub evals: BTreeMap<EvalCondition, usize>,
    pub execute: usize,
    pub reuse: usize,
    pub wait: usize,
    pub blocked: usize,
}

/// All effects needed by the status projection have finished before this is assembled.
struct Snapshot<'a> {
    required: &'a BTreeSet<&'a crate::config::ArtifactName>,
    selected_eval_ids: &'a [crate::config::EvalId],
    included_eval_ids: &'a [crate::config::EvalId],
    fingerprints: &'a BTreeMap<&'a crate::config::ArtifactName, cache::PreparedFingerprint>,
    known: &'a Known<'a>,
}

/// What the store and the remote review store say about the keys a scan computed so far.
#[derive(Default)]
struct Known<'c> {
    cached: BTreeMap<crate::config::EvalId, Claim>,
    previous: BTreeMap<(crate::config::EvalId, crate::types::DefinitionHash), store::Execution>,
    latest: BTreeMap<crate::config::EvalId, LastRequest>,
    waiting: Vec<store::RequestView>,
    unreadable: Vec<store::Unreadable>,
    /// Every key computed so far, by eval.
    keys: BTreeMap<&'c crate::config::EvalId, cache::Key>,
}

/// What a scan knows part way: the status as far as its fingerprints go, the results it found
/// to reuse, and the evals whose action may still change.
pub(crate) struct Progress<'a> {
    pub view: StatusView,
    pub cached: &'a BTreeMap<crate::config::EvalId, Claim>,
    pub checking: BTreeSet<&'a crate::config::EvalId>,
}

/// Where a scan hands what it knows part way.
pub(crate) type Publish<'p> = &'p mut (dyn for<'a> FnMut(Progress<'a>) + Send);

/// Prepare current fingerprints without executing evals or changing saved evidence.
pub async fn status(
    repo: &Path,
    state_dir: Option<&Path>,
    selection: &Selection,
    options: &VerifyOptions,
    cancellation: CancellationToken,
) -> Result<StatusView, String> {
    // Options are checked before anything is read.
    super::fingerprint_parallelism(options)?;
    let config = read_workspace_config(repo).map_err(|error| error.to_string())?;
    scan(config, state_dir, selection, options, cancellation, None)
        .await
        .map(|(view, _)| view)
}

/// [`status`] of a workspace configuration already read, with the claims it found by eval.
/// With `publish`, what it knows is handed there as its fingerprints come in.
pub(crate) async fn scan(
    config: crate::config::RepoConfig,
    state_dir: Option<&Path>,
    selection: &Selection,
    options: &VerifyOptions,
    cancellation: CancellationToken,
    publish: Option<Publish<'_>>,
) -> Result<(StatusView, BTreeMap<crate::config::EvalId, Claim>), String> {
    let parallelism = super::fingerprint_parallelism(options)?;
    let config = select_profiles(
        config,
        selection,
        options.profile.as_ref(),
        options.recursive,
    )?;
    let graph = Graph::new(&config).map_err(|error| error.to_string())?;
    let selected = selection.resolve(&config)?;
    let required: BTreeSet<_> = graph
        .dependency_closure(&selected.roots)
        .map_err(|error| error.to_string())?
        .into_iter()
        .collect();
    let selected_eval_ids: Vec<_> = selected.evals.iter().map(|eval| eval.id.clone()).collect();
    let included_eval_ids: Vec<_> = selection
        .included_evals(&config, options.recursive)?
        .iter()
        .map(|eval| eval.id.clone())
        .collect();
    let state = store::state_dir(state_dir)?;
    // Read-only: a store record that completed after the local latest counts as reuse without
    // being mirrored, as verify would take it before claiming. --force reads nothing from the
    // store, so its prediction has none.
    let remote = if options.force {
        None
    } else {
        Session::open(Some(&state), Some(&config.root))?
    };
    let scope = Scope {
        config: &config,
        graph: &graph,
        selection,
        options,
        required: &required,
        selected_eval_ids: &selected_eval_ids,
        included_eval_ids: &included_eval_ids,
        state: &state,
        remote: remote.as_deref(),
    };
    let targets = cache::fingerprint_targets(&config, &required);
    let fingerprints = match publish {
        None => {
            cache::prepare(&config, targets, &state, &parallelism, cancellation.clone()).await?
        }
        Some(publish) => {
            scope
                .progressively(targets, &parallelism, cancellation.clone(), publish)
                .await?
        }
    };
    // Read once every fingerprint is in, so a Run that ended meanwhile counts.
    let known = scope.read(&fingerprints).await?;
    if cancellation.is_cancelled() {
        return Err("Project preparation was cancelled.".into());
    }
    let view = scope.assemble(&fingerprints, &known)?;
    Ok((view, known.cached))
}

/// What one scan reads against.
struct Scope<'a> {
    config: &'a crate::config::RepoConfig,
    graph: &'a Graph<'a>,
    selection: &'a Selection,
    options: &'a VerifyOptions,
    required: &'a BTreeSet<&'a crate::config::ArtifactName>,
    selected_eval_ids: &'a [crate::config::EvalId],
    included_eval_ids: &'a [crate::config::EvalId],
    state: &'a Path,
    remote: Option<&'a Session>,
}

type Fingerprints<'a> = BTreeMap<&'a crate::config::ArtifactName, cache::PreparedFingerprint>;

impl<'a> Scope<'a> {
    /// Prepare the fingerprints, handing `publish` what is known each time some come in: first
    /// the graph with every eval being checked, then each action as soon as it cannot change.
    async fn progressively(
        &self,
        targets: BTreeSet<&'a crate::config::ArtifactName>,
        parallelism: &cache::Parallelism,
        cancellation: CancellationToken,
        publish: Publish<'_>,
    ) -> Result<Fingerprints<'a>, String> {
        let mut fingerprints = Fingerprints::new();
        let first = Known::default();
        publish(Progress {
            view: self.assemble(&fingerprints, &first)?,
            cached: &first.cached,
            checking: self.checking(&fingerprints, &first.cached),
        });
        let mut known = self.read(&fingerprints).await?;
        let (ready, mut arrived) = tokio::sync::mpsc::unbounded_channel();
        // A failed store read stops the fingerprints nobody would read any more.
        let preparation = cancellation.child_token();
        // Dependency first: an eval's action is known once its gates' are, so what is upstream
        // is fingerprinted first and the graph fills in from there.
        let targets = self
            .graph
            .components()
            .iter()
            .flat_map(|component| &component.artifacts)
            .filter_map(|id| targets.get(*id).copied());
        let preparing = cache::prepare_each(
            self.config,
            targets,
            self.state,
            parallelism,
            preparation.clone(),
            move |id, fingerprint| {
                let _ = ready.send((id, fingerprint));
            },
        );
        let publishing = async {
            while let Some((id, fingerprint)) = arrived.recv().await {
                let started = tokio::time::Instant::now();
                fingerprints.insert(id, fingerprint);
                while let Ok((id, fingerprint)) = arrived.try_recv() {
                    fingerprints.insert(id, fingerprint);
                }
                self.absorb(&mut known, &fingerprints).await?;
                publish(Progress {
                    view: self.assemble(&fingerprints, &known)?,
                    cached: &known.cached,
                    checking: self.checking(&fingerprints, &known.cached),
                });
                // Publishing takes from the fingerprints' time: leave them as long again.
                tokio::time::sleep(started.elapsed()).await;
            }
            Ok::<_, String>(())
        };
        let publishing = async {
            let published = publishing.await;
            if published.is_err() {
                preparation.cancel();
            }
            published
        };
        let (prepared, published) = tokio::join!(preparing, publishing);
        prepared?;
        published?;
        Ok(fingerprints)
    }

    /// What the store says about `fingerprints`, read afresh.
    async fn read(&self, fingerprints: &Fingerprints<'a>) -> Result<Known<'a>, String> {
        let mut known = Known {
            latest: store::read_latest_requests(self.state, &self.config.root).await?,
            waiting: store::read_waiting(self.state, Some(&self.config.root)).await?,
            unreadable: store::read_unreadable(self.state, Some(&self.config.root)).await?,
            ..Known::default()
        };
        self.absorb(&mut known, fingerprints).await?;
        Ok(known)
    }

    /// Look up the keys `fingerprints` complete that `known` has not looked up yet.
    async fn absorb(
        &self,
        known: &mut Known<'a>,
        fingerprints: &Fingerprints<'a>,
    ) -> Result<(), String> {
        let new: BTreeMap<&'a crate::config::EvalId, cache::Key> = self
            .config
            .evals
            .iter()
            .filter(|eval| !known.keys.contains_key(&eval.id))
            .filter_map(|eval| {
                Some((
                    &eval.id,
                    cache::eval_key(self.config, eval, fingerprints).ok()?,
                ))
            })
            .collect();
        let selected: BTreeSet<_> = self.selected_eval_ids.iter().collect();
        let keys: BTreeMap<_, _> = new
            .iter()
            .filter(|(id, _)| !(self.options.force && selected.contains(**id)))
            .map(|(id, key)| (*id, key))
            .collect();
        let mut cached = store::read_profiled_executions(
            self.state,
            self.config
                .evals
                .iter()
                .filter_map(|eval| {
                    keys.get(&eval.id).map(|key| {
                        (
                            eval.id.clone(),
                            key.value.clone(),
                            eval.declaration.profile().into(),
                        )
                    })
                })
                .collect(),
        )
        .await?;
        if let Some(remote) = self.remote {
            let all: Vec<_> = keys
                .values()
                .map(|key| key.value.clone())
                .collect::<BTreeSet<_>>()
                .into_iter()
                .collect();
            for execution in remote.lookup(&all).await? {
                let key = execution.key.clone().expect("remote key");
                for (id, _) in keys.iter().filter(|(_, candidate)| candidate.value == key) {
                    let newer = match cached.get(*id) {
                        Some(Claim::Reuse(local)) => execution.completed_after(local),
                        _ => true,
                    };
                    if newer {
                        cached.insert((*id).clone(), Claim::Reuse(Box::new(execution.clone())));
                    }
                }
            }
        }
        // Explain changed keys against the newest cached result for the same Eval definition.
        let stale: Vec<_> = keys
            .iter()
            .filter(|(id, _)| !matches!(cached.get(**id), Some(Claim::Reuse(_))))
            .map(|(id, key)| ((*id).clone(), key.eval_def_hash.clone()))
            .collect();
        known
            .previous
            .extend(store::read_latest_cached(self.state, &stale).await?);
        known.cached.extend(cached);
        known.keys.extend(new);
        Ok(())
    }

    fn assemble(
        &self,
        fingerprints: &Fingerprints<'a>,
        known: &Known<'a>,
    ) -> Result<StatusView, String> {
        view::assemble(
            self.config,
            self.graph,
            self.selection,
            self.options,
            Snapshot {
                required: self.required,
                selected_eval_ids: self.selected_eval_ids,
                included_eval_ids: self.included_eval_ids,
                fingerprints,
                known,
            },
        )
    }

    /// The evals whose action may still change: a fingerprint their key needs is not in yet,
    /// or, unless they reuse a result, the action of an eval they wait for may still change.
    fn checking(
        &self,
        fingerprints: &Fingerprints<'a>,
        cached: &BTreeMap<crate::config::EvalId, Claim>,
    ) -> BTreeSet<&'a crate::config::EvalId> {
        let evals: BTreeMap<_, _> = self
            .config
            .evals
            .iter()
            .map(|eval| (&eval.id, eval))
            .collect();
        let mut settled = BTreeSet::new();
        let mut checking = BTreeSet::new();
        // Dependency first, so an eval's gates are decided before it; gates in its own cycle
        // are not, and the cycle stays being checked until every fingerprint is in.
        for component in self.graph.components() {
            for artifact in component
                .artifacts
                .iter()
                .filter(|id| self.required.contains(*id))
            {
                for id in &self.graph.artifacts()[*artifact].evals {
                    let keyed = cache::dependencies(evals[id]).into_iter().all(|input| {
                        self.config.artifacts[input].fingerprint.is_none()
                            || fingerprints.contains_key(input)
                    });
                    let decided = keyed
                        && (matches!(cached.get(*id), Some(Claim::Reuse(_)))
                            || self
                                .graph
                                .gates(id)
                                .iter()
                                .all(|gate| settled.contains(gate)));
                    if decided {
                        settled.insert(*id);
                    } else {
                        checking.insert(*id);
                    }
                }
            }
        }
        checking
    }
}

impl Counts {
    fn action(&mut self, action: VerifyAction) {
        match action {
            VerifyAction::Execute => self.execute += 1,
            VerifyAction::Reuse => self.reuse += 1,
            VerifyAction::Wait => self.wait += 1,
            VerifyAction::Blocked => self.blocked += 1,
        }
    }
}