govctl 0.15.0

Project governance CLI for RFC, ADR, and Work Item management
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
use crate::config::Config;
use crate::diagnostic::{Diagnostic, DiagnosticCode};
use crate::model::{ClauseKind, ConformanceEntry, ProjectIndex, RfcIndex, RfcStatus};
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

/// Validate the complete current Conformance Case graph.
pub fn validate_cases(config: &Config, cases: &[ConformanceEntry]) -> Vec<Diagnostic> {
    let mut prospective = config.clone();
    prospective.schema.version = crate::cmd::migrate::CURRENT_SCHEMA_VERSION;
    let mut index = match crate::load::load_project(&prospective) {
        Ok(index) => index,
        Err(errors) => return errors,
    };
    index.conformance_cases = cases.to_vec();
    crate::validate::validate_project(&index, &prospective)
        .diagnostics
        .into_iter()
        .filter(|diagnostic| {
            diagnostic.code == DiagnosticCode::E1305ConformanceGraphInvalid
                || diagnostic.message.contains("Conformance Case")
        })
        .collect()
}

pub(super) fn validate_cases_with_index(
    config: &Config,
    cases: &[ConformanceEntry],
    index: &ProjectIndex,
) -> Vec<Diagnostic> {
    let guards = match crate::parse::load_guards(config) {
        Ok(guards) => guards,
        Err(error) => return vec![error],
    };
    let guard_ids = guards
        .iter()
        .map(|guard| guard.meta().id.as_str())
        .collect::<HashSet<_>>();
    let allowed_tags = config
        .tags
        .allowed
        .iter()
        .map(String::as_str)
        .collect::<HashSet<_>>();
    let mut diagnostics = Vec::new();
    let mut ids = HashMap::<&str, &Path>::new();
    let mut locators = HashMap::<(PathBuf, &str), &str>::new();

    for entry in cases {
        let id = entry.meta().id.as_str();
        let display = config.display_path(&entry.path).display().to_string();
        let expected_stem = entry.path.file_stem().and_then(|stem| stem.to_str());
        if expected_stem != Some(id) {
            diagnostics.push(invalid(
                format!("Conformance Case ID '{id}' must equal its filename stem"),
                &display,
            ));
        }
        if let Some(previous) = ids.insert(id, &entry.path) {
            diagnostics.push(invalid(
                format!(
                    "Duplicate Conformance Case ID '{id}' in '{}' and '{}'",
                    config.display_path(previous).display(),
                    display
                ),
                &display,
            ));
        }

        if let Some(path) = validate_case_contents(
            config,
            entry,
            &index.rfcs,
            &guard_ids,
            &allowed_tags,
            &mut diagnostics,
        ) {
            let key = (path, entry.spec.case.selector.as_str());
            if let Some(other) = locators.insert(key, id) {
                diagnostics.push(invalid(
                    format!(
                        "Conformance Cases '{other}' and '{id}' use the same path and selector"
                    ),
                    &display,
                ));
            }
        }
    }

    for guard in guards {
        for reference in &guard.meta().refs {
            if cases.iter().any(|case| case.meta().id == *reference) {
                diagnostics.push(invalid(
                    format!(
                        "Verification Guard '{}' cannot reference Conformance Case '{}'",
                        guard.meta().id,
                        reference
                    ),
                    &config.display_path(&guard.path).display().to_string(),
                ));
            }
        }
    }

    diagnostics
}

/// Validate one prospective Case without making unrelated project diagnostics
/// part of the mutation boundary.
pub(crate) fn validate_case_mutation(
    config: &Config,
    entry: &ConformanceEntry,
    cases: &[ConformanceEntry],
    rfcs: &[RfcIndex],
) -> Vec<Diagnostic> {
    let guards = match crate::parse::load_guards(config) {
        Ok(guards) => guards,
        Err(error) => return vec![error],
    };
    let guard_ids = guards
        .iter()
        .map(|guard| guard.meta().id.as_str())
        .collect::<HashSet<_>>();
    let allowed_tags = config
        .tags
        .allowed
        .iter()
        .map(String::as_str)
        .collect::<HashSet<_>>();
    let id = entry.meta().id.as_str();
    let display = config.display_path(&entry.path).display().to_string();
    let mut diagnostics = Vec::new();

    if entry.path.file_stem().and_then(|stem| stem.to_str()) != Some(id) {
        diagnostics.push(invalid(
            format!("Conformance Case ID '{id}' must equal its filename stem"),
            &display,
        ));
    }
    if let Some(other) = cases
        .iter()
        .find(|other| other.path != entry.path && other.meta().id == id)
    {
        diagnostics.push(invalid(
            format!(
                "Duplicate Conformance Case ID '{id}' in '{}' and '{}'",
                config.display_path(&other.path).display(),
                display
            ),
            &display,
        ));
    }

    if let Some(path) = validate_case_contents(
        config,
        entry,
        rfcs,
        &guard_ids,
        &allowed_tags,
        &mut diagnostics,
    ) {
        for other in cases.iter().filter(|other| other.path != entry.path) {
            if other.spec.case.selector == entry.spec.case.selector
                && canonical_scenario_path(config, &other.spec.case.path)
                    .is_ok_and(|other_path| other_path == path)
            {
                diagnostics.push(invalid(
                    format!(
                        "Conformance Cases '{}' and '{id}' use the same path and selector",
                        other.meta().id
                    ),
                    &display,
                ));
                break;
            }
        }
    }

    for guard in guards {
        if guard.meta().refs.iter().any(|reference| reference == id) {
            diagnostics.push(invalid(
                format!(
                    "Verification Guard '{}' cannot reference Conformance Case '{id}'",
                    guard.meta().id
                ),
                &config.display_path(&guard.path).display().to_string(),
            ));
        }
    }

    diagnostics
}

fn validate_case_contents(
    config: &Config,
    entry: &ConformanceEntry,
    rfcs: &[RfcIndex],
    guard_ids: &HashSet<&str>,
    allowed_tags: &HashSet<&str>,
    diagnostics: &mut Vec<Diagnostic>,
) -> Option<PathBuf> {
    let id = entry.meta().id.as_str();
    let display = config.display_path(&entry.path).display().to_string();

    if entry.meta().title.trim().is_empty() {
        diagnostics.push(invalid("Conformance Case title cannot be empty", &display));
    }
    validate_unique_values("tag", &entry.meta().tags, &display, diagnostics);
    for tag in &entry.meta().tags {
        if !allowed_tags.contains(tag.as_str()) {
            diagnostics.push(invalid(
                format!("Conformance Case '{id}' uses unregistered tag '{tag}'"),
                &display,
            ));
        }
    }
    if entry.spec.case.selector.trim().is_empty() {
        diagnostics.push(invalid(
            "Conformance Case selector cannot be empty",
            &display,
        ));
    }
    let path = match canonical_scenario_path(config, &entry.spec.case.path) {
        Ok(path) => Some(path),
        Err(message) => {
            diagnostics.push(invalid(message, &display));
            None
        }
    };

    if entry.spec.case.requirements.is_empty() {
        diagnostics.push(invalid(
            "Conformance Case must contain at least one requirement",
            &display,
        ));
    }
    let mut requirement_refs = HashSet::new();
    for requirement in &entry.spec.case.requirements {
        if !requirement_refs.insert(requirement.clause_ref.as_str()) {
            diagnostics.push(invalid(
                format!(
                    "Conformance Case '{id}' repeats requirement '{}'",
                    requirement.clause_ref
                ),
                &display,
            ));
            continue;
        }
        validate_requirement(rfcs, requirement, &display, diagnostics);
    }

    validate_unique_values("Guard", &entry.spec.case.guards, &display, diagnostics);
    for guard in &entry.spec.case.guards {
        if !guard_ids.contains(guard.as_str()) {
            diagnostics.push(invalid(
                format!("Conformance Case '{id}' references unknown Guard '{guard}'"),
                &display,
            ));
        }
    }

    path
}

fn validate_requirement(
    rfcs: &[RfcIndex],
    requirement: &crate::model::RequirementBinding,
    display: &str,
    diagnostics: &mut Vec<Diagnostic>,
) {
    let Some((rfc_id, clause_id)) = requirement.clause_ref.split_once(':') else {
        diagnostics.push(invalid(
            format!(
                "Requirement '{}' is not a fully qualified Clause ID",
                requirement.clause_ref
            ),
            display,
        ));
        return;
    };
    let Some(rfc) = rfcs.iter().find(|entry| entry.rfc.rfc_id == rfc_id) else {
        diagnostics.push(invalid(
            format!("Requirement RFC '{rfc_id}' does not exist"),
            display,
        ));
        return;
    };
    let Some(clause) = rfc
        .clauses
        .iter()
        .find(|entry| entry.spec.clause_id == clause_id)
    else {
        diagnostics.push(invalid(
            format!(
                "Requirement Clause '{}' does not exist",
                requirement.clause_ref
            ),
            display,
        ));
        return;
    };
    if clause.spec.kind == ClauseKind::Informative {
        diagnostics.push(invalid(
            format!(
                "Requirement '{}' identifies an informative Clause",
                requirement.clause_ref
            ),
            display,
        ));
    }

    let Ok(version) = semver::Version::parse(&requirement.version) else {
        diagnostics.push(invalid(
            format!(
                "Requirement version '{}' is not semantic versioning",
                requirement.version
            ),
            display,
        ));
        return;
    };
    let version_exists = rfc
        .rfc
        .changelog
        .iter()
        .any(|entry| entry.version == requirement.version);
    if !version_exists {
        diagnostics.push(invalid(
            format!(
                "Requirement version '{}' is absent from {} changelog",
                requirement.version, rfc_id
            ),
            display,
        ));
    }

    match &clause.spec.since {
        Some(since) => {
            if semver::Version::parse(since).is_ok_and(|since| version < since) {
                diagnostics.push(invalid(
                    format!(
                        "Requirement version '{}' predates Clause {} since '{}'",
                        requirement.version, requirement.clause_ref, since
                    ),
                    display,
                ));
            }
        }
        None if rfc.rfc.status != RfcStatus::Draft => diagnostics.push(invalid(
            format!(
                "Requirement '{}' targets a pending Clause outside a draft RFC",
                requirement.clause_ref
            ),
            display,
        )),
        None => {}
    }
    if rfc.rfc.status == RfcStatus::Draft && requirement.version != rfc.rfc.version {
        diagnostics.push(invalid(
            format!(
                "Draft requirement '{}' must bind current RFC version '{}'",
                requirement.clause_ref, rfc.rfc.version
            ),
            display,
        ));
    }
}

fn canonical_scenario_path(config: &Config, value: &str) -> Result<PathBuf, String> {
    if value.trim().is_empty() {
        return Err("Conformance Case path cannot be empty".to_string());
    }
    let relative = Path::new(value);
    if relative.is_absolute() {
        return Err(format!(
            "Conformance Case path '{value}' must be repository-relative"
        ));
    }
    let root = config
        .project_root()
        .canonicalize()
        .map_err(|err| format!("Cannot resolve project root: {err}"))?;
    let target = root
        .join(relative)
        .canonicalize()
        .map_err(|err| format!("Cannot resolve Conformance Case path '{value}': {err}"))?;
    if !target.starts_with(&root) {
        return Err(format!(
            "Conformance Case path '{value}' escapes the project root"
        ));
    }
    let gov_root = config
        .gov_root
        .canonicalize()
        .map_err(|err| format!("Cannot resolve gov root: {err}"))?;
    if target.starts_with(gov_root) {
        return Err(format!(
            "Conformance Case path '{value}' resolves inside the gov root"
        ));
    }
    if !target.is_file() {
        return Err(format!(
            "Conformance Case path '{value}' is not a regular file"
        ));
    }
    Ok(target)
}

fn validate_unique_values(
    label: &str,
    values: &[String],
    display: &str,
    diagnostics: &mut Vec<Diagnostic>,
) {
    let mut seen = HashSet::new();
    for value in values {
        if !seen.insert(value.as_str()) {
            diagnostics.push(invalid(
                format!("Conformance Case repeats {label} '{value}'"),
                display,
            ));
        }
    }
}

fn invalid(message: impl Into<String>, source: &str) -> Diagnostic {
    Diagnostic::new(
        DiagnosticCode::E1305ConformanceGraphInvalid,
        message,
        source,
    )
}