shallguard 0.1.2

ShallGuard requirement traceability, executable coverage, and review tooling
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
//! One-command orchestration for impact, coverage, bundle, and local review.

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

use anyhow::{Context, Result, bail};

use crate::DocSpec;
use crate::bundle::{self, BundleOptions};
use crate::coverage::{self, CoverageOptions};
use crate::docs::parse_doc;
use crate::impact::{self, BaseSelection, ImpactOptions};
use crate::review::{self, ReviewOptions, ReviewProvider, ReviewRun};
use crate::{ProgressCallback, report_progress};

/// Conventional artifacts produced by orchestrated local review.
#[derive(Debug)]
pub struct ReviewArtifactPaths<'a> {
    /// Machine-readable requirement impact.
    pub impact_json: &'a Path,
    /// Human-readable requirement impact.
    pub impact_markdown: &'a Path,
    /// Machine-readable executable coverage.
    pub coverage_json: &'a Path,
    /// Human-readable executable coverage.
    pub coverage_markdown: &'a Path,
    /// Intermediate LLVM profiles and exports.
    pub coverage_work_dir: &'a Path,
    /// Deterministic review bundle directory.
    pub bundle_dir: &'a Path,
    /// Validated local review output directory.
    pub review_output_dir: &'a Path,
}

/// Inputs for replaying a bundle or orchestrating a complete local review.
pub struct ReviewWorkflowOptions<'a> {
    /// Comparison base. `None` replays the existing bundle.
    pub base: Option<BaseSelection<'a>>,
    /// Repository-relative traceability baseline used by impact analysis.
    pub baseline_path: &'a Path,
    /// Whether orchestrated mode should execute relevant verification tests.
    pub with_coverage: bool,
    /// Local model CLI.
    pub provider: ReviewProvider,
    /// Optional provider-specific model identifier.
    pub model: Option<&'a str>,
    /// Optional on-device Codex backend.
    pub local_provider: Option<&'a str>,
    /// Optional final requirement allowlist.
    pub requirements: &'a BTreeSet<String>,
    /// Per-capsule model timeout.
    pub timeout: std::time::Duration,
    /// Continue a compatible review output and reuse its completed checkpoints.
    pub resume: bool,
    /// Optional portable content-addressed validated-response cache.
    pub cache_dir: Option<&'a Path>,
    /// Paths used for generated and final artifacts.
    pub artifacts: ReviewArtifactPaths<'a>,
    /// Optional human-readable progress callback.
    pub progress: Option<ProgressCallback>,
}

/// Outcome of a complete or replayed local review workflow.
#[derive(Debug)]
#[shallguard::enforces("REQ-REV-005")]
pub struct ReviewWorkflowRun {
    /// Validated and failed local model review counts.
    pub review: ReviewRun,
    /// Number of requirements selected by impact analysis.
    pub impacted_requirements: usize,
    /// Number of impacted automated requirements selected for coverage.
    pub covered_requirements: usize,
    /// Whether deterministic impact policy reported errors.
    pub impact_policy_failed: bool,
    /// Whether a selected test or LLVM coverage infrastructure failed.
    pub coverage_execution_failed: bool,
}

impl ReviewWorkflowRun {
    /// Returns true when any deterministic, execution, or provider stage failed.
    pub fn has_failures(&self) -> bool {
        self.impact_policy_failed || self.coverage_execution_failed || self.review.failures > 0
    }
}

/// Executes an existing bundle or generates every prerequisite from a base.
///
/// Orchestrated mode writes the impact artifact first, runs coverage only for
/// impacted requirements with automated evidence, embeds that coverage into a
/// deterministic bundle, and finally performs the advisory model review.
///
/// # Errors
///
/// Returns an error when impact analysis, artifact serialization, coverage
/// setup, bundle generation, or local-review setup cannot proceed reliably.
pub fn run(
    root: &Path,
    docs: &[DocSpec],
    options: &ReviewWorkflowOptions<'_>,
) -> Result<ReviewWorkflowRun> {
    let output_exists = options
        .artifacts
        .review_output_dir
        .try_exists()
        .with_context(|| {
            format!(
                "checking review output {}",
                options.artifacts.review_output_dir.display()
            )
        })?;
    let resume_existing = options.resume && output_exists;
    let base = if resume_existing { None } else { options.base };
    if options.with_coverage && base.is_none() && !resume_existing {
        bail!("--with-coverage requires --base <revision> or --target <branch>");
    }
    if !options.resume {
        require_new_review_output(options.artifacts.review_output_dir)?;
    }
    if base.is_some() {
        require_absent(options.artifacts.bundle_dir, "generated bundle")?;
    }

    let mut impacted_requirements = 0usize;
    let mut covered_requirements = 0usize;
    let mut impact_policy_failed = false;
    let mut coverage_execution_failed = false;

    if let Some(base) = &base {
        report_progress(
            options.progress,
            format!("impact: analyzing changes against {}", base_label(*base)),
        );
        let impact = impact::analyze(
            root,
            docs,
            &ImpactOptions {
                base: *base,
                baseline_path: options.baseline_path,
            },
        )?;
        impacted_requirements = impact.requirements.len();
        if impacted_requirements == 0 {
            bail!("impact analysis selected no requirements for local review");
        }
        impact_policy_failed = impact.has_policy_errors();
        report_progress(
            options.progress,
            format!(
                "impact: selected {impacted_requirements} requirement(s); {} policy finding(s)",
                impact.findings.len()
            ),
        );
        write_artifact(options.artifacts.impact_json, &impact.to_json()?)?;
        write_artifact(options.artifacts.impact_markdown, &impact.to_markdown())?;

        let coverage_file = if options.with_coverage {
            let automated = automated_requirement_descriptions(root, docs)?;
            let automated_ids = automated.keys().cloned().collect::<BTreeSet<_>>();
            let impacted = impact
                .requirements
                .iter()
                .map(|requirement| requirement.id.as_str())
                .collect::<BTreeSet<_>>();
            let selected =
                select_coverage_requirements(&impacted, &automated_ids, options.requirements);
            covered_requirements = selected.len();
            if selected.is_empty() {
                report_progress(
                    options.progress,
                    "coverage: no impacted requirement has automated evidence; skipping",
                );
                None
            } else {
                report_progress(
                    options.progress,
                    format!(
                        "coverage: selecting tests for {covered_requirements} impacted automated \
                         requirement(s)"
                    ),
                );
                report_coverage_requirements(options.progress, &selected, &automated);
                let coverage = coverage::generate(
                    root,
                    docs,
                    &CoverageOptions {
                        packages: &BTreeSet::new(),
                        requirements: &selected,
                        work_dir: options.artifacts.coverage_work_dir,
                        progress: options.progress,
                    },
                )?;
                coverage_execution_failed = coverage.has_execution_errors();
                write_artifact(options.artifacts.coverage_json, &coverage.to_json()?)?;
                write_artifact(options.artifacts.coverage_markdown, &coverage.to_markdown())?;
                Some(options.artifacts.coverage_json)
            }
        } else {
            report_progress(options.progress, "coverage: disabled by --without-coverage");
            None
        };

        report_progress(
            options.progress,
            format!(
                "bundle: creating {impacted_requirements} capsule(s) in {}",
                options.artifacts.bundle_dir.display()
            ),
        );
        bundle::generate(
            root,
            docs,
            &BundleOptions {
                impact_file: options.artifacts.impact_json,
                coverage_file,
                output_dir: options.artifacts.bundle_dir,
            },
        )?;
        report_progress(options.progress, "bundle: deterministic capsules are ready");
    } else {
        report_progress(
            options.progress,
            format!(
                "bundle: {} existing capsules from {}",
                if resume_existing {
                    "resuming"
                } else {
                    "replaying"
                },
                options.artifacts.bundle_dir.display()
            ),
        );
    }

    report_progress(options.progress, "review: starting local semantic review");
    let review = review::generate(&ReviewOptions {
        bundle_dir: options.artifacts.bundle_dir,
        output_dir: options.artifacts.review_output_dir,
        provider: options.provider,
        model: options.model,
        local_provider: options.local_provider,
        requirements: options.requirements,
        timeout: options.timeout,
        resume: options.resume,
        cache_dir: options.cache_dir,
        progress: options.progress,
    })?;

    Ok(ReviewWorkflowRun {
        review,
        impacted_requirements,
        covered_requirements,
        impact_policy_failed,
        coverage_execution_failed,
    })
}

fn base_label<'a>(base: BaseSelection<'a>) -> &'a str {
    match base {
        BaseSelection::Revision(revision) => revision,
        BaseSelection::MergeBaseWith(target) => target,
    }
}

fn automated_requirement_descriptions(
    root: &Path,
    docs: &[DocSpec],
) -> Result<BTreeMap<String, String>> {
    let mut automated = BTreeMap::new();
    for doc in docs {
        for requirement in parse_doc(root, doc)?
            .requirements
            .into_iter()
            .filter(|requirement| requirement.automated)
        {
            automated.insert(requirement.id, requirement.title);
        }
    }
    Ok(automated)
}

#[shallguard::enforces("REQ-COV-001")]
fn select_coverage_requirements(
    impacted: &BTreeSet<&str>,
    automated: &BTreeSet<String>,
    requested: &BTreeSet<String>,
) -> BTreeSet<String> {
    automated
        .iter()
        .filter(|requirement| impacted.contains(requirement.as_str()))
        .filter(|requirement| requested.is_empty() || requested.contains(requirement.as_str()))
        .cloned()
        .collect()
}

fn report_coverage_requirements(
    progress: Option<ProgressCallback>,
    requirements: &BTreeSet<String>,
    descriptions: &BTreeMap<String, String>,
) {
    for line in coverage_requirement_progress_lines(requirements, descriptions) {
        report_progress(progress, line);
    }
}

#[shallguard::enforces("REQ-CLI-003")]
fn coverage_requirement_progress_lines(
    requirements: &BTreeSet<String>,
    descriptions: &BTreeMap<String, String>,
) -> Vec<String> {
    requirements
        .iter()
        .enumerate()
        .map(|(index, requirement)| {
            let description = descriptions
                .get(requirement)
                .map_or("description unavailable", String::as_str);
            format!(
                "coverage: requirement [{}/{}] {requirement} - {description}",
                index + 1,
                requirements.len(),
            )
        })
        .collect()
}

fn write_artifact(path: &Path, content: &str) -> Result<()> {
    if let Some(parent) = path
        .parent()
        .filter(|parent| !parent.as_os_str().is_empty())
    {
        std::fs::create_dir_all(parent)
            .with_context(|| format!("creating artifact parent {}", parent.display()))?;
    }
    std::fs::write(path, content).with_context(|| format!("writing artifact {}", path.display()))
}

fn require_absent(path: &Path, description: &str) -> Result<()> {
    if path
        .try_exists()
        .with_context(|| format!("checking {}", path.display()))?
    {
        bail!(
            "{description} {} already exists; choose another path or retain it as an artifact",
            path.display()
        );
    }
    Ok(())
}

#[shallguard::enforces("REQ-CLI-011")]
fn require_new_review_output(path: &Path) -> Result<()> {
    if path
        .try_exists()
        .with_context(|| format!("checking review output {}", path.display()))?
    {
        bail!(
            "review output {} already exists; inspect it with `cargo shallguard review show \
             --output {}`, continue it with `cargo shallguard review --resume --output {}`, or \
             choose another --output path",
            path.display(),
            path.display(),
            path.display()
        );
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn successful_run() -> ReviewWorkflowRun {
        ReviewWorkflowRun {
            review: ReviewRun {
                output_dir: Path::new("review-output").to_path_buf(),
                reviews: 1,
                verdicts: shallguard::review::ReviewVerdictCounts::default(),
                failures: 0,
            },
            impacted_requirements: 1,
            covered_requirements: 1,
            impact_policy_failed: false,
            coverage_execution_failed: false,
        }
    }

    #[shallguard::verifies("REQ-CLI-011")]
    #[test]
    fn existing_review_output_points_to_show_and_resume() {
        let directory = tempfile::tempdir().expect("temporary review output creates");
        let error = require_new_review_output(directory.path())
            .expect_err("existing review output is rejected");
        let message = error.to_string();
        assert!(message.contains("cargo shallguard review show --output"));
        assert!(message.contains("cargo shallguard review --resume --output"));
    }

    #[shallguard::verifies("REQ-COV-001")]
    #[test]
    fn coverage_selection_intersects_impact_automation_and_request() {
        let impacted = BTreeSet::from(["REQ-AA-001", "REQ-AA-002", "REQ-AA-003"]);
        let automated = BTreeSet::from([
            "REQ-AA-001".to_string(),
            "REQ-AA-002".to_string(),
            "REQ-BB-001".to_string(),
        ]);
        let requested = BTreeSet::from(["REQ-AA-002".to_string(), "REQ-AA-003".to_string()]);

        assert_eq!(
            select_coverage_requirements(&impacted, &automated, &requested),
            BTreeSet::from(["REQ-AA-002".to_string()])
        );
    }

    #[test]
    fn empty_request_selects_every_automated_impact() {
        let impacted = BTreeSet::from(["REQ-AA-001", "REQ-AA-002"]);
        let automated = BTreeSet::from(["REQ-AA-001".to_string()]);

        assert_eq!(
            select_coverage_requirements(&impacted, &automated, &BTreeSet::new()),
            automated
        );
    }

    #[shallguard::verifies("REQ-CLI-003")]
    #[test]
    fn coverage_requirement_progress_is_sorted_one_per_line_with_descriptions() {
        let requirements = BTreeSet::from(["REQ-AA-002".to_string(), "REQ-AA-001".to_string()]);
        let descriptions = BTreeMap::from([
            ("REQ-AA-001".to_string(), "First behavior".to_string()),
            ("REQ-AA-002".to_string(), "Second behavior".to_string()),
        ]);

        assert_eq!(
            coverage_requirement_progress_lines(&requirements, &descriptions),
            vec![
                "coverage: requirement [1/2] REQ-AA-001 - First behavior",
                "coverage: requirement [2/2] REQ-AA-002 - Second behavior",
            ]
        );
    }

    #[shallguard::verifies("REQ-REV-005")]
    #[test]
    fn deterministic_or_provider_failure_fails_the_workflow() {
        let mut run = successful_run();
        assert!(!run.has_failures());

        run.coverage_execution_failed = true;
        assert!(run.has_failures());
        run.coverage_execution_failed = false;
        run.review.failures = 1;
        assert!(run.has_failures());
    }
}