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
//! `check` — the `MODEL.md` §9 invariants that apply to Tier 0.
//!
//! It separates two things that look alike and are not. A cycle or an orphan
//! is **store corruption**: the tool is lying. A false close is a **finding
//! about the project**: the store is fine and what is wrong is the work,
//! which was called finished without being finished. Both exit non-zero
//! --this belongs in CI-- but they are not counted together.
use crate::args::Args;
use crate::event::State;
use crate::model::Tree;
use crate::output::outln;
pub fn check(a: &Tree, args: &Args) -> Result<i32, crate::failure::Failure> {
let mut store: Vec<String> = Vec::new();
let mut project: Vec<String> = Vec::new();
if a.broken_lines > 0 {
store.push(format!(
"{} unreadable line(s) in .vivac/events (skipped while reading)",
a.broken_lines
));
}
// One ULID, one `num`. With `num` as `Tree`'s own storage key, only the
// first of two claimants ever makes it into `nodes_iter` below -- the
// fold records the second at the moment it loses, since a scan
// afterwards has nothing left to see.
for d in &a.repeated_nums {
store.push(format!(
"number {} repeated: {} and {}",
d.num, d.first, d.second
));
}
for n in a.nodes_iter() {
// Invariant 11: provenance is a tree. The schema already rules out two
// parents --`spawns` travels inside the node-- so the only thing that
// can break here is the parent not existing.
if let Some(p) = n.parent {
if a.node_by_num(p).is_none() {
store.push(format!(
"{} points at a parent that does not exist",
n.alias()
));
}
}
// Invariant 1: acyclic. If the path to the root does not end at a node
// with no parent, it is going in circles.
let lineage = a.ancestors(n.num);
if lineage.first().is_some_and(|r| r.parent.is_some()) {
store.push(format!("{} sits in a provenance cycle", n.alias()));
}
// Invariant 10: false close.
//
// A **forced** close does not count as a violation: `MODEL.md` §9
// exempts it on purpose, because there are legitimate forced closes
// --a lane being abandoned-- and what was asked was that they be a
// decision and not an oversight. The trace is in the event and the
// render still marks it; what it does not do is break CI every day.
if n.state == State::Done && !n.forced_close && !a.open_blockers(n.num).is_empty() {
let pending_count = a.open_blockers(n.num);
let aliases: Vec<String> = pending_count.iter().map(|c| c.alias()).collect();
project.push(format!(
"{} is closed with {} open condition(s): {}",
n.alias(),
pending_count.len(),
aliases.join(", ")
));
}
}
store.sort();
project.sort();
if args.has("json") {
outln!(
"{}",
serde_json::to_string_pretty(&serde_json::json!({
"store": store,
"project": project,
"ok": store.is_empty() && project.is_empty(),
}))
.map_err(std::io::Error::other)?
);
} else {
outln!();
if store.is_empty() && project.is_empty() {
outln!(" No findings. {} nodes checked.", a.total());
outln!();
}
if !store.is_empty() {
outln!(
" STORE ({}) <- the tool is lying; it needs fixing",
store.len()
);
outln!();
for m in &store {
outln!(" {m}");
}
outln!();
}
if !project.is_empty() {
outln!(
" PROJECT ({}) <- the store is fine; the work is not",
project.len()
);
outln!();
for m in &project {
outln!(" {m}");
}
outln!();
outln!(" A false close is not repaired by editing the tree: reopen what");
outln!(" stayed open, or close it deliberately with --force.");
outln!();
}
}
Ok(i32::from(!(store.is_empty() && project.is_empty())))
}