artifactize-core 0.13.0

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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
mod states;
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,
}

/// 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> {
    let parallelism = super::fingerprint_parallelism(options)?;
    let config = read_workspace_config(repo).map_err(|error| error.to_string())?;
    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)?;
    let mut latest = store::read_latest_requests(&state, &config.root).await?;
    let mut evidence: BTreeMap<_, _> = latest
        .keys()
        .map(|id| (id.clone(), Evidence::Stale))
        .collect();
    if options.force {
        evidence.extend(
            selected_eval_ids
                .iter()
                .map(|id| (id.clone(), Evidence::Stale)),
        );
    }
    let selected_ids: BTreeSet<_> = selected_eval_ids.iter().collect();
    let included_ids: BTreeSet<_> = included_eval_ids.iter().collect();
    let fingerprints = cache::prepare(
        &config,
        cache::fingerprint_targets(&config, &required),
        &state,
        &parallelism,
        cancellation.clone(),
    )
    .await?;
    let all_keys = cache::eval_keys(&config, &fingerprints);
    let keys: BTreeMap<_, _> = all_keys
        .iter()
        .filter(|(id, _)| !(options.force && selected_ids.contains(*id)))
        .map(|(id, key)| (*id, key))
        .collect();
    let mut cached = store::read_profiled_executions(
        &state,
        config
            .evals
            .iter()
            .filter_map(|eval| {
                keys.get(&eval.id).map(|key| {
                    (
                        eval.id.clone(),
                        key.value.clone(),
                        eval.declaration.profile().into(),
                    )
                })
            })
            .collect(),
    )
    .await?;
    // 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.
    if !options.force
        && let Some(remote) = Session::open(Some(&state), Some(&config.root))?
    {
        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())));
                }
            }
        }
    }
    let claim = |id: &crate::types::EvalId| cached.get(id);
    // Explain changed keys against the newest cached result for the same Eval definition.
    let stale: Vec<_> = keys
        .iter()
        .filter(|(id, _)| !matches!(claim(id), Some(Claim::Reuse(_))))
        .map(|(id, key)| ((*id).clone(), key.eval_def_hash.clone()))
        .collect();
    let previous = store::read_latest_cached(&state, &stale).await?;
    for eval in &config.evals {
        if let Some(Claim::Reuse(execution)) = claim(&eval.id) {
            evidence.insert(
                eval.id.clone(),
                Evidence::Current(execution.verdict().expect("completed cache entry")),
            );
        }
    }
    if cancellation.is_cancelled() {
        return Err("Project preparation was cancelled.".into());
    }
    let evaluation = graph.evaluate(&evidence);
    let deferred: BTreeSet<_> = store::read_waiting(&state, Some(&config.root))
        .await?
        .into_iter()
        .filter(|view| view.request.status() == crate::types::RequestStatus::Deferred)
        .filter(|view| {
            matches!(
                claim(&view.request.eval_id),
                Some(Claim::WaitHuman(id)) if Some(id) == view.request.execution_id.as_ref()
            )
        })
        .map(|view| view.request.eval_id)
        .collect();
    let mut shown_evidence = evidence.clone();
    for id in &deferred {
        shown_evidence.insert(id.clone(), Evidence::Deferred);
    }
    let shown = graph.evaluate(&shown_evidence);
    let obligations: Vec<_> = evaluation
        .obligations
        .iter()
        .filter(|id| required.contains(*id))
        .map(|id| (*id).clone())
        .collect();
    let mut counts = Counts::default();
    let mut artifacts = Vec::new();
    for id in &required {
        let artifact = &evaluation.artifacts[id.as_str()];
        let status = ArtifactCondition::from(shown.artifacts[id.as_str()].status);
        let own = &graph.artifacts()[id.as_str()];
        let unmet = graph
            .dependency_closure(&[*id])
            .map_err(|error| error.to_string())?
            .into_iter()
            .filter(|id| !evaluation.artifacts[id.as_str()].own_satisfied)
            .map(|id| (*id).clone())
            .collect::<Vec<_>>();
        let reason = if artifact.satisfied {
            "All required obligations are satisfied.".into()
        } else if own.evals.is_empty() && !own.basis {
            "No Evals are declared and this Artifact is not a basis.".into()
        } else {
            format!(
                "Unmet obligations: {}",
                unmet
                    .iter()
                    .map(ToString::to_string)
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        };
        *counts.artifacts.entry(status).or_default() += 1;
        artifacts.push(ArtifactState {
            kind: config.artifacts[*id].kind,
            path: config.artifacts[*id].path.clone(),
            id: (*id).clone(),
            tags: config.artifacts[*id].tags.clone(),
            state: status,
            reason,
            eval_ids: own.evals.iter().map(|id| (*id).clone()).collect(),
            passed: artifact.passed,
            total: artifact.total,
            satisfied: artifact.satisfied,
            obligations: unmet,
        });
    }
    let obligations_by_artifact: BTreeMap<_, _> = artifacts
        .iter()
        .map(|artifact| (artifact.id.as_str(), &artifact.obligations))
        .collect();
    let mut evals = Vec::new();
    for eval in config
        .evals
        .iter()
        .filter(|eval| required.contains(&eval.target))
    {
        let current = &evaluation.evals[eval.id.as_str()];
        let selected = selected_ids.contains(&eval.id);
        let included = included_ids.contains(&eval.id);
        let force = options.force && selected;
        let (action, reason) = match current.readiness {
            // verify takes cached results before gates resolve, even behind RED; the state keeps
            // the gate.
            _ if matches!(current.evidence, Some(Evidence::Current(_))) => (
                VerifyAction::Reuse,
                format!(
                    "The current eval and fingerprints have a completed {}{}{}",
                    match claim(&eval.id) {
                        Some(Claim::Reuse(execution)) if execution.origin.is_some() => format!(
                            "result in the remote review store, from {}",
                            execution
                                .producer
                                .as_ref()
                                .map_or("an unknown producer", |producer| producer.name.as_str())
                        ),
                        _ => "cached result".into(),
                    },
                    match claim(&eval.id) {
                        Some(Claim::Reuse(execution))
                            if execution.profile
                                != crate::config::StoredProfile::from(
                                    eval.declaration.profile()
                                ) =>
                            format!(
                                " produced by profile {}",
                                crate::query::profile_name(&execution.profile, &execution.options)
                            ),
                        _ => String::new(),
                    },
                    match current.readiness {
                        Readiness::Ready => ".".into(),
                        Readiness::Wait => format!(
                            "; its gates still wait for: {}",
                            current
                                .unmet_gates
                                .iter()
                                .map(ToString::to_string)
                                .collect::<Vec<_>>()
                                .join(", ")
                        ),
                        // Status never treats an ERROR as a blocker, so every cause is RED.
                        Readiness::Blocked => format!(
                            "; its gates are blocked by RED: {}",
                            current
                                .causes
                                .iter()
                                .map(|cause| cause.eval().as_str())
                                .collect::<Vec<_>>()
                                .join(", ")
                        ),
                    }
                ),
            ),
            Readiness::Blocked => (
                VerifyAction::Blocked,
                current.blocked_reason().unwrap_or_default(),
            ),
            Readiness::Wait => (
                VerifyAction::Wait,
                format!(
                    "Waiting for current GREEN dependency evidence: {}",
                    current
                        .unmet_gates
                        .iter()
                        .map(ToString::to_string)
                        .collect::<Vec<_>>()
                        .join(", ")
                ),
            ),
            Readiness::Ready
                if !force
                    && matches!(claim(&eval.id), Some(Claim::Wait(_) | Claim::WaitHuman(_))) =>
            {
                (
                    VerifyAction::Wait,
                    "The current eval and fingerprints have a live execution.".into(),
                )
            }
            Readiness::Ready => match eval.declaration.profile() {
                Profile::Human { .. } => (
                    VerifyAction::Execute,
                    "Record a request awaiting a Human claim and submission.".into(),
                ),
                Profile::Runtime { .. } | Profile::Agent { .. } => (
                    VerifyAction::Execute,
                    if force {
                        "An explicitly forced Eval requires a new execution.".into()
                    } else {
                        match cache::eval_key(&config, eval, &fingerprints) {
                            Ok(_) => {
                                "The current eval and fingerprints have no completed cached result."
                                    .into()
                            }
                            Err(cache::Unkeyed::Target) => {
                                "The Artifact declares fingerprint: false; saved noncached results satisfy only their own Run."
                                    .into()
                            }
                            Err(cache::Unkeyed::Dependency(id)) => format!(
                                "Dependency {id} declares fingerprint: false, so this eval has no reuse key; saved noncached results satisfy only their own Run."
                            ),
                        }
                    },
                ),
            },
        };
        let status = if !force && matches!(claim(&eval.id), Some(Claim::WaitHuman(_))) {
            if deferred.contains(&eval.id) {
                EvalCondition::Deferred
            } else {
                EvalCondition::WaitingHuman
            }
        } else {
            EvalCondition::from(shown.evals[eval.id.as_str()].status)
        };
        *counts.evals.entry(status).or_default() += 1;
        if included {
            counts.action(action);
        }
        evals.push(EvalState {
            id: eval.id.clone(),
            target: eval.target.clone(),
            title: eval.declaration.title.clone(),
            profile: eval.declaration.profile().clone(),
            selected,
            included,
            force,
            state: status,
            action,
            reason,
            blocked_by: current.blocked_by.iter().copied().map(Into::into).collect(),
            obligations: obligations_by_artifact[eval.target.as_str()].clone(),
            eval_def_hash: cache::eval_definition_hash(&eval.declaration),
            fingerprint: fingerprints
                .get(&eval.target)
                .map(|fingerprint| fingerprint.value.clone()),
            fingerprints: cache::dependencies(&config, eval)
                .into_iter()
                .map(|id| {
                    (
                        config.artifacts[id].name.clone(),
                        fingerprints
                            .get(id)
                            .map(|fingerprint| fingerprint.value.clone()),
                    )
                })
                .collect(),
            key: all_keys.get(&eval.id).map(|key| key.value.clone()),
            last: latest.remove(eval.id.as_str()),
            changes: keys.get(&eval.id).and_then(|key| {
                previous
                    .get(&(eval.id.clone(), key.eval_def_hash.clone()))
                    .map(|execution| {
                        cache::changes(
                            execution,
                            &eval.target,
                            key,
                            fingerprints[&eval.target].manifest.as_ref(),
                        )
                    })
            }),
        });
    }
    let unreadable = store::read_unreadable(&state, Some(&config.root)).await?;
    Ok(StatusView {
        unreadable,
        selection: selection.clone(),
        recursive: options.recursive,
        force: options.force,
        selected_eval_ids,
        included_eval_ids,
        satisfied: obligations.is_empty(),
        obligations,
        artifacts,
        evals,
        counts,
    })
}

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,
        }
    }
}