speccoder-runtime 0.3.0

Application services of speccoder: the only crate that executes anything.
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//! The evidence tour of one task, built from the signed statements on its
//! commit and the commit itself — nothing else, so it never says more than
//! `attest verify` can check. Six steps in a fixed order; a step without
//! evidence says so in the verdict's own words instead of being left out.

use std::path::Path;

use serde_json::{Value, json};
use speccoder_core::predicate::TaskVerificationV0;
use speccoder_core::tour::{EvidenceTour, Source, StepKind, TourStep};
use speccoder_core::verify_branch::{TickOutcome, TickReport, Unverifiable};
use speccoder_core::{CommandData, CommandResult, CommandStatus};

use crate::attest::statement;
use crate::evidence::banner;
use crate::git::Git;

/// The most of a requirement passage a tour quotes.
const PASSAGE_MAX: usize = 1500;

fn step(kind: StepKind, source: Source, body: String, facts: Value) -> TourStep {
    TourStep {
        kind,
        source,
        title: kind.title().to_owned(),
        body_markdown: body,
        facts,
    }
}

fn phase<'a>(statements: &'a [Value], name: &str) -> Option<&'a Value> {
    statements.iter().find_map(|s| {
        let p = &s["predicate"];
        (s["predicateType"] == statement::TEST_RESULT
            && p["configuration"][0]["name"] == format!("speccoder:phase/{name}"))
        .then_some(p)
    })
}

fn names(v: &Value) -> Vec<String> {
    v.as_array()
        .into_iter()
        .flatten()
        .filter_map(|x| x.as_str().map(str::to_owned))
        .collect()
}

fn short(commit: &str) -> &str {
    &commit[..commit.len().min(7)]
}

/// Why a step has nothing, in the verdict's words.
fn nothing(verdict: Option<&str>, what: &str) -> String {
    match verdict {
        Some("attested") => format!("{what} — {}", banner("attested").1),
        Some("regression_only") => format!("{what} — {}", banner("regression_only").1),
        _ => what.to_owned(),
    }
}

/// `> ` before every line, at most [`PASSAGE_MAX`] characters.
fn quote(text: &str) -> String {
    let mut out = String::new();
    for line in text.trim().lines() {
        if out.chars().count() > PASSAGE_MAX {
            out.push_str("> …\n");
            break;
        }
        out.push_str(&format!("> {line}\n"));
    }
    out
}

/// Step 1: the requirement as the commit's tree has it, through the
/// format adapter — the task's story and the requirements its title
/// cites; `signed` when the story hashes to the signed one.
fn requirement(
    repo: &Path,
    prefix: &str,
    feature: &str,
    task: &str,
    title: &str,
    commit: &str,
    tv: Option<&TaskVerificationV0>,
) -> TourStep {
    let read = crate::attest::tree::intent_tops(repo, commit)
        .and_then(|tops| crate::attest::tree::materialize(repo, commit, Some(&tops)))
        .ok()
        .and_then(|dir| {
            let at = dir.path().join(prefix);
            let format = crate::format::at(&at);
            let snap = format.inspect(&at, Some(feature))?;
            let story = format
                .bundle(&snap, task)
                .ok()
                .map(|b| b.story)
                .unwrap_or_default();
            let unit = snap.feature.as_ref().map(|f| f.feature.dir.clone())?;
            let cited: Vec<(String, String)> = format
                .requirements_file()
                .and_then(|f| {
                    std::fs::read_to_string(Path::new(&snap.root).join(&unit).join(f)).ok()
                })
                .map(|text| {
                    format
                        .requirements_in(&text)
                        .into_iter()
                        .filter(|(id, _)| crate::attest::cites(title, id))
                        .collect()
                })
                .unwrap_or_default();
            Some((story, cited))
        });
    let Some((story, cited)) = read.filter(|(s, c)| !s.trim().is_empty() || !c.is_empty()) else {
        return step(
            StepKind::Requirement,
            Source::NotRecorded,
            format!("no requirement passage found at `{}`", short(commit)),
            Value::Null,
        );
    };
    let hash = crate::integrity::sha256_hex(story.as_bytes());
    let section = tv.and_then(|tv| tv.spec.as_ref());
    let signed = !story.trim().is_empty() && section.is_some_and(|s| s.section_hash == hash);
    let ids: Vec<&str> = cited.iter().map(|(id, _)| id.as_str()).collect();
    let name = match (section.and_then(|s| s.section.clone()), ids.is_empty()) {
        (Some(s), true) => s,
        (Some(s), false) => format!("{s}: {}", ids.join(", ")),
        (None, false) => ids.join(", "),
        (None, true) => "the task's requirement".to_owned(),
    };
    let mut passage = String::new();
    for (_, line) in &cited {
        passage.push_str(line.trim());
        passage.push('\n');
    }
    if !story.trim().is_empty() {
        if !passage.is_empty() {
            passage.push('\n');
        }
        passage.push_str(&story);
    }
    step(
        StepKind::Requirement,
        if signed {
            Source::Signed
        } else {
            Source::Committed
        },
        format!("{name} (at `{}`)\n{}", short(commit), quote(&passage)),
        json!({ "section": section.and_then(|s| s.section.clone()), "cited": ids, "story_sha256": hash, "commit": commit }),
    )
}

/// Step 2: the acceptance tests the signed trace names, and where they
/// were decided (the predicate's `acceptance.source`, else not recorded).
fn frozen_tests(tv: Option<&TaskVerificationV0>, verdict: Option<&str>) -> TourStep {
    let tests: Vec<String> = tv
        .and_then(|tv| tv.trace.as_ref())
        .and_then(Value::as_array)
        .into_iter()
        .flatten()
        .filter_map(|r| r["test"].as_str().map(str::to_owned))
        .collect::<std::collections::BTreeSet<_>>()
        .into_iter()
        .collect();
    let source = tv
        .and_then(|tv| tv.acceptance.as_ref())
        .and_then(|a| a["source"].as_str())
        .map_or_else(
            || "where decided: not recorded".to_owned(),
            |s| format!("decided: {s}"),
        );
    if tests.is_empty() {
        return step(
            StepKind::FrozenTests,
            Source::NotRecorded,
            nothing(verdict, "no task-specific test"),
            json!({ "tests": [], "acceptance": tv.and_then(|tv| tv.acceptance.clone()) }),
        );
    }
    step(
        StepKind::FrozenTests,
        Source::Signed,
        format!(
            "{} ({source})",
            tests
                .iter()
                .map(|t| format!("`{t}`"))
                .collect::<Vec<_>>()
                .join(", ")
        ),
        json!({ "tests": tests, "acceptance": tv.and_then(|tv| tv.acceptance.clone()) }),
    )
}

/// Step 3: the tests failing first, how, and the signed excerpt.
fn red(statements: &[Value], tv: Option<&TaskVerificationV0>, verdict: Option<&str>) -> TourStep {
    let Some(red) = phase(statements, "red") else {
        return step(
            StepKind::Red,
            Source::NotRecorded,
            nothing(verdict, "no red phase"),
            Value::Null,
        );
    };
    let failed = names(&red["failedTests"]);
    let excerpt = tv.and_then(|tv| tv.red.as_ref());
    let kind = excerpt.map_or("kind not recorded", |r| match r.kind {
        speccoder_core::predicate::RedKind::Assertion => "assertion",
        speccoder_core::predicate::RedKind::MissingSymbol => "missing symbol",
    });
    let block = match excerpt {
        Some(r) if r.withheld => {
            "output excerpt withheld (it was not clean: a local path or a credential)".to_owned()
        }
        Some(r) => format!(
            "```text\n{}\n```",
            r.excerpt
                .as_deref()
                .unwrap_or_default()
                .replace("```", "'''")
        ),
        None => "no output excerpt recorded".to_owned(),
    };
    step(
        StepKind::Red,
        Source::Signed,
        format!(
            "failed: {} ({kind})\n{block}",
            if failed.is_empty() {
                "—".to_owned()
            } else {
                failed
                    .iter()
                    .map(|t| format!("`{t}`"))
                    .collect::<Vec<_>>()
                    .join(", ")
            }
        ),
        json!({ "failed": failed, "red": excerpt }),
    )
}

/// Step 4: the commit's diff, by its stat (`committed`: git objects).
fn diff(repo: &Path, commit: &str) -> TourStep {
    let stat = Git::at(repo)
        .run(&["show", "--stat=100", "--format=", commit])
        .map(|b| String::from_utf8_lossy(&b).trim().to_owned())
        .unwrap_or_default();
    let numstat = Git::at(repo)
        .run(&["show", "--numstat", "--format=", commit])
        .map(|b| String::from_utf8_lossy(&b).into_owned())
        .unwrap_or_default();
    let (mut files, mut added, mut removed) = (0usize, 0u64, 0u64);
    for line in numstat.lines() {
        let mut f = line.split('\t');
        if let (Some(a), Some(r), Some(_)) = (f.next(), f.next(), f.next()) {
            files += 1;
            added += a.parse::<u64>().unwrap_or(0);
            removed += r.parse::<u64>().unwrap_or(0);
        }
    }
    let lines: Vec<&str> = stat.lines().collect();
    let shown = lines.len().min(30);
    let mut block = lines[..shown].join("\n");
    if lines.len() > shown {
        block.push_str(&format!("\n… {} more", lines.len() - shown));
    }
    step(
        StepKind::Diff,
        Source::Committed,
        format!(
            "`{}`: {files} file(s), +{added} −{removed}\n```text\n{block}\n```",
            short(commit)
        ),
        json!({ "commit": commit, "files": files, "added": added, "removed": removed }),
    )
}

/// Step 5: what passed after — acceptance, held-out, the checks against
/// their baseline — and signed dependencies, shown, never failing.
fn green(statements: &[Value], tv: Option<&TaskVerificationV0>, verdict: Option<&str>) -> TourStep {
    let Some(green) = phase(statements, "green") else {
        return step(
            StepKind::Green,
            Source::NotRecorded,
            nothing(verdict, "no green phase"),
            Value::Null,
        );
    };
    let passed = names(&green["passedTests"]).len();
    let failed = names(&green["failedTests"]).len();
    let held_out = phase(statements, "held_out").map_or_else(
        || "held-out: not run".to_owned(),
        |h| {
            if h["result"] == "PASSED" {
                "held-out passed".to_owned()
            } else {
                "held-out failed".to_owned()
            }
        },
    );
    let warned = phase(statements, "baseline").map(|b| names(&b["warnedTests"]).len());
    let checks = match warned {
        Some(0) => "checks green, no regression (baseline warnings: 0)".to_owned(),
        Some(n) => format!("checks green, no regression (baseline warnings: {n})"),
        None => "no baseline recorded".to_owned(),
    };
    let mut body = format!("{passed} passed, {failed} failed; {held_out}; {checks}");
    let deps = tv.and_then(|tv| tv.dependencies.clone());
    if let Some(d) = deps.as_ref().and_then(Value::as_array) {
        body.push_str(&format!(
            "\ndependencies added (signed): {}",
            d.iter()
                .map(|x| format!(
                    "`{}@{}`",
                    x["name"].as_str().unwrap_or("?"),
                    x["version"].as_str().unwrap_or("?")
                ))
                .collect::<Vec<_>>()
                .join(", ")
        ));
    }
    step(
        StepKind::Green,
        Source::Signed,
        body,
        json!({ "passed": passed, "failed": failed, "held_out": phase(statements, "held_out").map(|h| &h["result"]), "baseline_warnings": warned, "dependencies": deps }),
    )
}

/// Step 6: the acceptance set's strength and mutation results.
fn strength(tv: Option<&TaskVerificationV0>, verdict: Option<&str>) -> TourStep {
    let st = tv
        .and_then(|tv| tv.strength.as_ref())
        .filter(|v| !v.is_null());
    let mutation = tv
        .and_then(|tv| tv.mutation.as_ref())
        .filter(|v| !v.is_null());
    if st.is_none() && mutation.is_none() {
        return step(
            StepKind::Strength,
            Source::NotRecorded,
            nothing(verdict, "not measured"),
            Value::Null,
        );
    }
    let mut parts = Vec::new();
    if let Some(st) = st {
        let share = |v: &Value| v["share"].as_f64().map(|x| format!("{x:.2}"));
        if let Some(v) = share(&st["visible"]) {
            parts.push(format!("visible {v}"));
        }
        if let Some(c) = share(&st["combined"]) {
            parts.push(format!("combined {c}"));
        }
        let near = st["near_misses"].as_array().map_or(0, Vec::len);
        parts.push(format!("{near} near miss(es) tried"));
        let rejected = st["permitted"]
            .as_array()
            .into_iter()
            .flatten()
            .filter(|p| p["outcome"] == "rejected")
            .count();
        if rejected > 0 {
            parts.push(format!(
                "{rejected} permitted alternative(s) rejected — over-strict"
            ));
        }
    }
    if let Some(m) = mutation {
        parts.push(format!(
            "{} of {} mutants caught",
            m["caught"].as_u64().unwrap_or(0),
            m["caught"].as_u64().unwrap_or(0) + m["missed"].as_u64().unwrap_or(0)
        ));
    }
    step(
        StepKind::Strength,
        Source::Signed,
        parts.join(", "),
        json!({ "strength": st, "mutation": mutation }),
    )
}

/// The tour of one checked tick. `prefix` is the project's path in the
/// repository.
#[must_use]
pub fn of(repo: &Path, prefix: &str, r: &TickReport) -> EvidenceTour {
    let mut tour = EvidenceTour {
        feature: r.tick.feature.clone(),
        task: r.tick.task.clone(),
        title: r.tick.title.clone(),
        verdict: r.verdict.clone(),
        commit: r.commit.clone(),
        steps: Vec::new(),
        unverifiable: None,
    };
    let Some(commit) = r.commit.as_deref() else {
        tour.steps.push(step(
            StepKind::Requirement,
            Source::NotRecorded,
            "no commit carries this task's evidence".to_owned(),
            Value::Null,
        ));
        tour.unverifiable = Some(
            r.detail
                .clone()
                .unwrap_or_else(|| Unverifiable::TickedWithoutEvidence.word().to_owned()),
        );
        return tour;
    };
    let statements = crate::evidence::statements(repo, commit);
    let tv: Option<TaskVerificationV0> = statements
        .iter()
        .find(|s| s["predicateType"] == statement::TASK_VERIFICATION)
        .and_then(|s| serde_json::from_value(s["predicate"].clone()).ok());
    let tv = tv.as_ref();
    tour.steps.push(requirement(
        repo,
        prefix,
        &r.tick.feature,
        &r.tick.task,
        &r.tick.title,
        commit,
        tv,
    ));
    if r.outcome == TickOutcome::Unverifiable {
        tour.unverifiable = Some(match (&r.reason, &r.detail) {
            (Some(reason), Some(d)) => format!("{} — {d}", reason.word()),
            (Some(reason), None) => reason.word().to_owned(),
            (None, _) => "the evidence does not verify".to_owned(),
        });
        return tour;
    }
    let verdict = r.verdict.as_deref();
    tour.steps.push(frozen_tests(tv, verdict));
    tour.steps.push(red(&statements, tv, verdict));
    tour.steps.push(diff(repo, commit));
    tour.steps.push(green(&statements, tv, verdict));
    tour.steps.push(strength(tv, verdict));
    // Reserved fields another spec owns: shown as the signed facts they
    // are, never interpreted here.
    if let Some(tv) = tv {
        for name in tv.reserved_present() {
            let value = match name {
                "bug" => tv.bug.as_ref(),
                "context" => tv.context.as_ref(),
                _ => None,
            };
            if let (Some(v), Some(last)) = (value, tour.steps.last_mut()) {
                last.body_markdown.push_str(&format!(
                    "\nsigned `{name}` (shown as recorded): `{}`",
                    v.to_string().chars().take(300).collect::<String>()
                ));
            }
        }
    }
    tour
}

/// The words a tour's summary line ends with.
#[must_use]
pub fn words(r: &TickReport) -> String {
    match r.outcome {
        TickOutcome::Evidenced => banner(r.verdict.as_deref().unwrap_or_default())
            .1
            .to_owned(),
        TickOutcome::Stale => "STALE — evidence for an earlier wording".to_owned(),
        TickOutcome::Unverifiable => "UNVERIFIABLE".to_owned(),
        TickOutcome::Regressed => "REGRESSED".to_owned(),
    }
}

/// Every tick's tour, as collapsed Markdown, in the report's order.
#[must_use]
pub fn markdown_all(checked: &crate::attest::branch::Checked) -> Vec<String> {
    checked
        .report
        .ticks
        .iter()
        .map(|r| of(&checked.repo, &checked.prefix, r).markdown(&words(r)))
        .collect()
}

/// `speccoder attest tour` and `/api/tour`: the tour of `feature`'s
/// `task` at `head`, with the check's trust when given (the outcome then
/// decides how far the tour goes), else from the statements as recorded.
#[must_use]
pub fn command(
    start: &Path,
    feature: &str,
    task: &str,
    opts: &crate::attest::branch::Options,
    command: &str,
) -> CommandResult {
    let checked = match crate::attest::branch::locate(start, opts, feature, task) {
        Ok(c) => c,
        Err(r) => return CommandResult::new(command, r.status, &r.message),
    };
    let Some(r) = checked.report.ticks.first() else {
        return CommandResult::new(
            command,
            CommandStatus::NotFound,
            &format!("{feature} has no ticked task {task} at {}", opts.head),
        );
    };
    let tour = of(&checked.repo, &checked.prefix, r);
    CommandResult::new(command, CommandStatus::Ok, &format!("{task}: {}", words(r)))
        .with_data(CommandData::EvidenceTour(Box::new(tour)))
}