1use std::collections::{BTreeMap, BTreeSet};
13use std::path::{Path, PathBuf};
14
15use serde_json::{Value, json};
16use speccoder_core::predicate::TaskVerificationV0;
17use speccoder_core::verify_branch::{
18 self, Anchor, BranchReport, BranchTotals, Conclusion, Mode, SkippedTask, Tick, TickKind,
19 TickOutcome, TickReport, Unverifiable,
20};
21use speccoder_core::{
22 CommandData, CommandResult, CommandStatus, ObjectVerification, TraceRow, VerifyOutcome,
23};
24
25use super::verify::{Context, Target, Trust};
26use super::{notes, statement};
27use crate::format::Adapter;
28use crate::git::Git;
29
30const FS: char = '\u{1f}';
32const RS: char = '\u{1e}';
33
34#[derive(Debug, Clone)]
36pub enum AllowedSigners {
37 File(PathBuf),
39 Rev {
42 rev: String,
44 path: String,
46 },
47}
48
49impl AllowedSigners {
50 pub fn parse_rev(spec: &str) -> Result<Self, String> {
56 match spec.split_once(':') {
57 Some((rev, path)) if !rev.is_empty() && !path.is_empty() => Ok(Self::Rev {
58 rev: rev.to_owned(),
59 path: path.to_owned(),
60 }),
61 _ => Err(format!(
62 "--allowed-signers-rev takes <rev>:<path>, e.g. origin/main:.github/speccoder/allowed_signers (got `{spec}`)"
63 )),
64 }
65 }
66}
67
68#[derive(Debug, Clone)]
70pub struct Options {
71 pub head: String,
73 pub base: Option<String>,
76 pub all: bool,
78 pub allowed_signers: Option<AllowedSigners>,
80 pub identity: Option<String>,
82 pub issuer: Option<String>,
84 pub require_signed: bool,
86 pub require_verified: bool,
88}
89
90impl Default for Options {
91 fn default() -> Self {
92 Self {
93 head: "HEAD".to_owned(),
94 base: None,
95 all: false,
96 allowed_signers: None,
97 identity: None,
98 issuer: None,
99 require_signed: true,
100 require_verified: false,
101 }
102 }
103}
104
105#[derive(Debug, Clone, PartialEq, Eq)]
107pub struct Refusal {
108 pub status: CommandStatus,
110 pub message: String,
112}
113
114impl Refusal {
115 fn internal(what: impl std::fmt::Display) -> Self {
118 Self::new(
119 CommandStatus::Error,
120 format!("the check could not read the branch: {what}; nothing was verified"),
121 )
122 }
123
124 fn new(status: CommandStatus, message: impl Into<String>) -> Self {
125 Self {
126 status,
127 message: message.into(),
128 }
129 }
130}
131
132pub const NO_TRUST: &str = "no trust configured: pass --allowed-signers or --certificate-identity/--certificate-oidc-issuer";
134
135#[derive(Debug)]
137pub struct Checked {
138 pub report: BranchReport,
140 pub repo: PathBuf,
142 pub prefix: String,
144}
145
146#[must_use]
150pub fn verify(start: &Path, opts: &Options, command: &str) -> CommandResult {
151 match check(start, opts) {
152 Ok(c) => result(command, c.report),
153 Err(r) => CommandResult::new(command, r.status, &r.message),
154 }
155}
156
157#[must_use]
159pub fn result(command: &str, report: BranchReport) -> CommandResult {
160 let status = match report.conclusion {
161 Conclusion::Success => CommandStatus::Ok,
162 Conclusion::Failure => CommandStatus::AttestationFailed,
163 Conclusion::Neutral => CommandStatus::NotFound,
164 };
165 let mut message = report.title();
166 if report.accepted_unsigned {
167 message.push_str("; unsigned ticks were accepted (--require-signed=false)");
168 }
169 CommandResult::new(command, status, &message)
170 .with_data(CommandData::BranchVerify(Box::new(report)))
171}
172
173fn trust(
178 top: &Path,
179 opts: &Options,
180 optional: bool,
181) -> Result<(Option<Trust>, Value, Option<tempfile::TempDir>), Refusal> {
182 let sigstore = opts.identity.is_some() && opts.issuer.is_some();
183 if opts.identity.is_some() != opts.issuer.is_some() {
184 return Err(Refusal::new(
185 CommandStatus::UsageError,
186 "--certificate-identity and --certificate-oidc-issuer go together",
187 ));
188 }
189 let mut held = None;
190 let (allowed, source) = match &opts.allowed_signers {
191 None => (None, Value::Null),
192 Some(AllowedSigners::File(p)) => {
193 if !p.is_file() {
194 return Err(Refusal::new(
195 CommandStatus::UsageError,
196 format!("--allowed-signers {}: no such file", p.display()),
197 ));
198 }
199 (Some(p.clone()), json!({ "file": p.display().to_string() }))
200 }
201 Some(AllowedSigners::Rev { rev, path }) => {
202 let bytes = Git::at(top)
203 .run(&["show", &format!("{rev}:{path}")])
204 .map_err(|e| {
205 Refusal::new(
206 CommandStatus::UsageError,
207 format!(
208 "--allowed-signers-rev {rev}:{path}: cannot read it ({})",
209 e.0
210 ),
211 )
212 })?;
213 let dir = tempfile::tempdir().map_err(|e| {
214 Refusal::new(CommandStatus::Error, format!("no temporary directory: {e}"))
215 })?;
216 let file = dir.path().join("allowed_signers");
217 std::fs::write(&file, bytes).map_err(|e| {
218 Refusal::new(CommandStatus::Error, format!("cannot write the trust: {e}"))
219 })?;
220 held = Some(dir);
221 (Some(file), json!({ "rev": rev, "path": path }))
222 }
223 };
224 if allowed.is_none() && !sigstore {
225 if optional {
226 return Ok((
227 None,
228 json!({ "signatures": "not checked: no trust given" }),
229 None,
230 ));
231 }
232 return Err(Refusal::new(CommandStatus::UsageError, NO_TRUST));
233 }
234 let shown = json!({
235 "ssh_allowed_signers": source,
236 "gpg": true,
237 "sigstore": opts.identity.as_ref().map(|i| json!({ "identity": i, "issuer": opts.issuer })),
238 });
239 Ok((
240 Some(Trust {
241 allowed_signers: allowed,
242 identity: opts.identity.clone(),
243 issuer: opts.issuer.clone(),
244 }),
245 shown,
246 held,
247 ))
248}
249
250fn relative(path: &Path, root: &Path) -> Option<String> {
252 let canonical = |p: &Path| std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf());
253 let rel = canonical(path)
254 .strip_prefix(canonical(root))
255 .ok()?
256 .to_path_buf();
257 Some(rel.to_string_lossy().replace('\\', "/"))
258}
259
260fn as_prefix(rel: &str) -> String {
262 if rel.is_empty() {
263 String::new()
264 } else {
265 format!("{}/", rel.trim_end_matches('/'))
266 }
267}
268
269struct History {
273 latest: BTreeMap<String, String>,
274 bug_claims: BTreeSet<String>,
275}
276
277fn history(git: &Git, head: &str, base: Option<&str>) -> Result<History, Refusal> {
278 let log = git
279 .run(&[
280 "log",
281 "--grep=^Spec-Task: ",
282 "--format=%H%x1f%(trailers:key=Spec-Task,valueonly)%x1f%(trailers:key=Verdict,valueonly)%x1e",
283 head,
284 ])
285 .map(|b| String::from_utf8_lossy(&b).into_owned())
286 .map_err(|e| Refusal::internal(format!("git log {head}: {}", e.0)))?;
287 let in_range: Option<BTreeSet<String>> = base
288 .map(|b| {
289 git.run(&["rev-list", &format!("{b}..{head}")])
290 .map(|o| {
291 String::from_utf8_lossy(&o)
292 .lines()
293 .map(str::to_owned)
294 .collect()
295 })
296 .map_err(|e| Refusal::internal(format!("git rev-list {b}..{head}: {}", e.0)))
297 })
298 .transpose()?;
299 let mut latest = BTreeMap::new();
300 let mut bug_claims = BTreeSet::new();
301 for record in log.split(RS) {
302 let f: Vec<&str> = record.trim().split(FS).collect();
303 let [commit, task, verdict] = f[..] else {
304 continue;
305 };
306 let (task, verdict) = (task.trim(), verdict.trim());
307 if task.is_empty() || verdict.is_empty() {
308 continue;
309 }
310 if task.contains("/bug:") && in_range.as_ref().is_none_or(|r| r.contains(commit)) {
311 bug_claims.insert(task.to_owned());
312 }
313 latest
314 .entry(task.to_owned())
315 .or_insert_with(|| commit.to_owned());
316 }
317 Ok(History { latest, bug_claims })
318}
319
320fn unreachable_evidence(
324 git: &Git,
325 repo: &Path,
326 head: &str,
327) -> Result<BTreeMap<String, String>, Refusal> {
328 let reachable: BTreeSet<String> = git
329 .run(&["rev-list", head])
330 .map(|o| {
331 String::from_utf8_lossy(&o)
332 .lines()
333 .map(str::to_owned)
334 .collect()
335 })
336 .map_err(|e| Refusal::internal(format!("git rev-list {head}: {}", e.0)))?;
337 let noted = notes::try_noted(repo)
338 .map_err(|e| Refusal::internal(format!("git notes list: {}", e.0)))?;
339 let mut out = BTreeMap::new();
340 for commit in noted {
341 if reachable.contains(&commit) {
342 continue;
343 }
344 for s in crate::evidence::statements(repo, &commit) {
345 if s["predicateType"] == statement::TASK_VERIFICATION
346 && let Some(name) = s["subject"][0]["name"].as_str()
347 {
348 out.insert(name.to_owned(), commit.clone());
349 }
350 }
351 }
352 Ok(out)
353}
354
355fn changed(git: &Git, base: &str, head: &str) -> Result<Vec<String>, Refusal> {
358 git.run(&["diff", "--name-only", "-z", base, head])
359 .map(|o| crate::git::nul_fields(&o))
360 .map_err(|e| Refusal::internal(format!("git diff {base} {head}: {}", e.0)))
361}
362
363fn project_at(top: &Path, rev: &str, start_rel: &str) -> Result<bool, Refusal> {
365 let dir = super::tree::intent_tops(top, rev)
366 .and_then(|tops| super::tree::materialize(top, rev, Some(&tops)))
367 .map_err(Refusal::internal)?;
368 let at = dir.path().join(start_rel);
369 Ok(crate::format::at(&at).inspect(&at, None).is_some())
370}
371
372struct Unit {
374 name: String,
375 dir: String,
376 ticks: Vec<Tick>,
377 skipped: Vec<SkippedTask>,
378}
379
380fn unit(
383 format: &dyn Adapter,
384 project: &Path,
385 name: &str,
386 prefix: &str,
387 skips: &BTreeSet<String>,
388) -> Result<Unit, Refusal> {
389 let f = format
390 .inspect(project, Some(name))
391 .and_then(|snap| snap.feature)
392 .ok_or_else(|| Refusal::internal(format!("the unit {name} cannot be read at the head")))?;
393 let mut ticks = Vec::new();
394 let mut skipped = Vec::new();
395 if let Some(tasks) = &f.tasks {
396 let text = std::fs::read_to_string(project.join(&tasks.path))
397 .map_err(|e| Refusal::internal(format!("{prefix}{}: {e}", tasks.path)))?;
398 let lines: Vec<&str> = text.lines().collect();
399 for t in &tasks.tasks {
400 if !t.done {
401 if skips.contains(&format!("{name}/{}", t.id)) {
402 skipped.push(SkippedTask {
403 feature: name.to_owned(),
404 task: t.id.clone(),
405 });
406 }
407 continue;
408 }
409 let line = usize::try_from(t.line)
410 .ok()
411 .and_then(|l| l.checked_sub(1))
412 .and_then(|i| lines.get(i))
413 .copied()
414 .unwrap_or_default();
415 ticks.push(Tick {
416 feature: name.to_owned(),
417 task: t.id.clone(),
418 title: t.title.clone(),
419 kind: TickKind::Tick,
420 anchor: Anchor {
421 path: format!("{prefix}{}", tasks.path),
422 line: t.line,
423 },
424 line_sha256: Some(crate::integrity::sha256_hex(&crate::integrity::untick(
425 line.as_bytes(),
426 ))),
427 claim_sha256: None,
428 });
429 }
430 }
431 Ok(Unit {
432 name: name.to_owned(),
433 dir: f.feature.dir,
434 ticks,
435 skipped,
436 })
437}
438
439fn trailer_only(format: &dyn Adapter, project: &Path, prefix: &str, claim: &str) -> Option<Tick> {
443 let (unit, task) = claim.rsplit_once('/')?;
444 let slug = task
447 .strip_prefix("bug:")
448 .filter(|s| speccoder_core::bug::valid_slug(s))?;
449 let home = format.bug_unit(project).ok().filter(|h| h.kind.native())?;
450 if format.bug_dir(&home, slug).map(|(_, u)| u).as_deref() != Some(unit) {
451 return None;
452 }
453 let read = format.read_bug(project, &home, slug).ok()?;
454 let triad = speccoder_core::bug::Triad::new(
455 read.current.first().map_or("", String::as_str),
456 &read.expected,
457 &read.unchanged,
458 crate::integrity::sha256_hex,
459 )
460 .ok()?;
461 let text = std::fs::read_to_string(project.join(&read.file)).unwrap_or_default();
462 let line = text
463 .lines()
464 .position(|l| l.contains("Expected:"))
465 .and_then(|i| u32::try_from(i + 1).ok())
466 .unwrap_or(1);
467 Some(Tick {
468 feature: unit.to_owned(),
469 task: task.to_owned(),
470 title: read.title.clone().unwrap_or_else(|| triad.title()),
471 kind: TickKind::TrailerOnly,
472 anchor: Anchor {
473 path: format!("{prefix}{}", read.file),
474 line,
475 },
476 line_sha256: None,
477 claim_sha256: Some(triad.digest),
478 })
479}
480
481fn skips(git: &Git) -> Result<BTreeSet<String>, Refusal> {
483 git.run(&[
484 "for-each-ref",
485 "--format=%(refname)",
486 &format!("{}/", notes::SKIPPED),
487 ])
488 .map(|o| {
489 String::from_utf8_lossy(&o)
490 .lines()
491 .filter_map(|r| r.strip_prefix(&format!("{}/", notes::SKIPPED)))
492 .map(|r| r.replace("%3A", ":").replace("%25", "%"))
494 .collect()
495 })
496 .map_err(|e| Refusal::internal(format!("git for-each-ref {}: {}", notes::SKIPPED, e.0)))
497}
498
499fn detail(v: &ObjectVerification) -> Option<String> {
501 let r = v.reasons.first()?;
502 Some(match (&r.field, &r.expected, &r.found) {
503 (Some(field), Some(e), Some(f)) => format!("{} {field}: expected {e}, found {f}", r.code),
504 (_, _, Some(f)) => format!("{}: {f}", r.code),
505 _ => r.code.clone(),
506 })
507}
508
509fn outcome_of(
512 tick: &Tick,
513 commit: &str,
514 v: ObjectVerification,
515 statements: &[Value],
516 require_signed: bool,
517) -> TickReport {
518 let mut r = TickReport {
519 tick: tick.clone(),
520 commit: Some(commit.to_owned()),
521 outcome: TickOutcome::Evidenced,
522 verdict: v.verdict.clone(),
523 reason: None,
524 detail: None,
525 signer: v.signer.clone(),
526 decider: None,
527 stale: Vec::new(),
528 stale_anchor: None,
529 regression: None,
530 newer_fields: Vec::new(),
531 verification: None,
532 };
533 let unverifiable = |r: &mut TickReport, reason: Unverifiable, detail: Option<String>| {
534 r.outcome = TickOutcome::Unverifiable;
535 r.reason = Some(reason);
536 r.detail = detail;
537 };
538 let other = statements.iter().find_map(|s| {
540 s["predicateType"]
541 .as_str()
542 .filter(|t| t.starts_with("https://speccoder.dev/task-verification/"))
543 .filter(|t| *t != statement::TASK_VERIFICATION)
544 });
545 let tv = statements
546 .iter()
547 .find(|s| s["predicateType"] == statement::TASK_VERIFICATION)
548 .map(|s| serde_json::from_value::<TaskVerificationV0>(s["predicate"].clone()));
549 match v.outcome {
550 VerifyOutcome::Ok => {}
551 VerifyOutcome::Unsigned if !require_signed => {}
552 o => {
553 let reason = Unverifiable::of(o).unwrap_or(Unverifiable::Malformed);
554 match other {
555 Some(t) if tv.is_none() => unverifiable(
556 &mut r,
557 Unverifiable::Malformed,
558 Some(format!("predicate {t} not supported by this verifier")),
559 ),
560 _ => unverifiable(&mut r, reason, detail(&v)),
561 }
562 }
563 }
564 if r.outcome == TickOutcome::Evidenced {
565 match tv {
566 Some(Ok(tv)) => {
567 let triad = tv
572 .bug
573 .as_ref()
574 .and_then(|b| b["triad_sha256"].as_str().map(str::to_owned));
575 let agree = statements
576 .iter()
577 .filter(|s| s["predicateType"] == statement::TASK_VERIFICATION)
578 .all(|s| s["predicate"]["bug"]["triad_sha256"].as_str() == triad.as_deref());
579 let anchored = match tick.kind {
580 TickKind::Tick => Some(&tv.task.line_hash) == tick.line_sha256.as_ref(),
581 TickKind::TrailerOnly => triad.is_some() && agree,
582 };
583 if tick.kind == TickKind::TrailerOnly && triad.is_some() && !agree {
584 unverifiable(
585 &mut r,
586 Unverifiable::Mismatch,
587 Some(
588 "the note's task-verification statements name different triads"
589 .to_owned(),
590 ),
591 );
592 } else if !anchored {
593 unverifiable(
594 &mut r,
595 Unverifiable::Mismatch,
596 Some("the task line changed since its tick: task.line_hash differs from the line at the head".to_owned()),
597 );
598 }
599 if r.outcome == TickOutcome::Evidenced
601 && tick.kind == TickKind::TrailerOnly
602 && triad.as_deref() != tick.claim_sha256.as_deref()
603 {
604 r.outcome = TickOutcome::Stale;
605 r.stale_anchor = Some(tick.anchor.clone());
606 r.detail = Some(
607 "the bug's triad changed since its fix: the verdict is for the earlier words"
608 .to_owned(),
609 );
610 }
611 r.newer_fields = tv.newer_fields();
612 r.regression = tv
613 .regression
614 .as_ref()
615 .map(speccoder_core::regression::wording);
616 r.decider = tv.decider.map(|d| d.by);
617 }
618 Some(Err(e)) => unverifiable(
619 &mut r,
620 Unverifiable::Malformed,
621 Some(format!("the predicate is not task-verification v0: {e}")),
622 ),
623 None => unverifiable(
624 &mut r,
625 Unverifiable::Mismatch,
626 Some("no task-verification statement".to_owned()),
627 ),
628 }
629 }
630 if v.outcome == VerifyOutcome::Unsigned && require_signed {
631 r.detail =
632 Some("the statements are not signed (sign with `speccoder attest sign`)".to_owned());
633 }
634 r.verification = Some(v);
635 r
636}
637
638fn without_commit(tick: &Tick, lost: Option<&String>) -> TickReport {
640 let (reason, detail) = match lost {
641 Some(c) => (
642 Unverifiable::CommitUnreachable,
643 format!(
644 "its evidence is on {} — not reachable from the head (a squash or rebase merge?); merge with a merge commit to keep it",
645 &c[..c.len().min(12)]
646 ),
647 ),
648 None => (
649 Unverifiable::TickedWithoutEvidence,
650 "no commit carries this task's verdict (ticked by hand?)".to_owned(),
651 ),
652 };
653 TickReport {
654 tick: tick.clone(),
655 commit: None,
656 outcome: TickOutcome::Unverifiable,
657 verdict: None,
658 reason: Some(reason),
659 detail: Some(detail),
660 signer: None,
661 decider: None,
662 stale: Vec::new(),
663 stale_anchor: None,
664 regression: None,
665 newer_fields: Vec::new(),
666 verification: None,
667 }
668}
669
670fn line_of(text: &str, line: &str) -> Option<u32> {
672 let want = line.trim();
673 text.lines()
674 .position(|l| l.trim() == want)
675 .and_then(|i| u32::try_from(i + 1).ok())
676}
677
678fn mark_stale(
681 top: &Path,
682 format: &dyn Adapter,
683 project: &Path,
684 prefix: &str,
685 unit_dir: &str,
686 head: &str,
687 reports: &mut [TickReport],
688) {
689 let asked: Vec<(&str, Option<&str>)> = reports
690 .iter()
691 .map(|r| {
692 (
693 r.tick.title.as_str(),
694 r.commit
695 .as_deref()
696 .filter(|_| r.outcome == TickOutcome::Evidenced),
697 )
698 })
699 .collect();
700 let Some(found) = crate::drift::at(top, format, unit_dir, prefix, head, &asked) else {
701 return;
702 };
703 let spec = format
704 .requirements_file()
705 .map(|f| format!("{}/{f}", unit_dir.trim_end_matches('/')));
706 let spec_text = spec
707 .as_ref()
708 .and_then(|s| std::fs::read_to_string(project.join(s)).ok())
709 .unwrap_or_default();
710 for (r, drift) in reports.iter_mut().zip(found) {
711 let Some(drift) = drift.filter(|d| !d.requirements.is_empty()) else {
712 continue;
713 };
714 r.outcome = TickOutcome::Stale;
715 r.stale_anchor = drift.requirements.iter().find_map(|d| {
716 let now = d.now.as_deref()?;
717 Some(Anchor {
718 path: format!("{prefix}{}", spec.as_deref()?),
719 line: line_of(&spec_text, now)?,
720 })
721 });
722 r.detail = Some(format!(
723 "{} changed since the tick: the verdict is for the earlier wording",
724 drift
725 .requirements
726 .iter()
727 .map(|d| d.id.as_str())
728 .collect::<Vec<_>>()
729 .join(", ")
730 ));
731 r.stale = drift.requirements;
732 }
733}
734
735fn word(r: &TickReport) -> String {
737 match r.outcome {
738 TickOutcome::Evidenced => r.verdict.clone().unwrap_or_default(),
739 TickOutcome::Stale => "stale".to_owned(),
740 TickOutcome::Unverifiable => "unverifiable".to_owned(),
741 TickOutcome::Regressed => "regressed".to_owned(),
742 }
743}
744
745fn trace(
749 requirements: &[(String, String)],
750 reports: &[TickReport],
751 signed: &BTreeMap<String, Vec<Value>>,
752) -> Vec<TraceRow> {
753 let mut rows = Vec::new();
754 for (id, _) in requirements {
755 let citing: Vec<&TickReport> = reports
756 .iter()
757 .filter(|r| super::cites(&r.tick.title, id))
758 .collect();
759 if citing.is_empty() {
760 rows.push(TraceRow {
761 requirement: id.clone(),
762 scenario: None,
763 test: None,
764 task: None,
765 verdict: None,
766 changed: false,
767 });
768 }
769 for r in citing {
770 rows.push(TraceRow {
771 requirement: id.clone(),
772 scenario: None,
773 test: None,
774 task: Some(r.tick.task.clone()),
775 verdict: Some(word(r)),
776 changed: r.stale.iter().any(|d| d.id == *id),
777 });
778 }
779 }
780 for r in reports {
781 for row in signed.get(&r.tick.task).into_iter().flatten() {
782 let Some(test) = row["test"].as_str() else {
783 continue;
784 };
785 rows.push(TraceRow {
786 requirement: row["requirement"].as_str().unwrap_or("—").to_owned(),
787 scenario: row["scenario"].as_str().map(str::to_owned),
788 test: Some(test.to_owned()),
789 task: Some(r.tick.task.clone()),
790 verdict: Some(word(r)),
791 changed: false,
792 });
793 }
794 }
795 rows
796}
797
798pub fn check(start: &Path, opts: &Options) -> Result<Checked, Refusal> {
804 run(start, opts, None)
805}
806
807pub fn locate(start: &Path, opts: &Options, feature: &str, task: &str) -> Result<Checked, Refusal> {
815 let opts = Options {
816 all: true,
817 base: None,
818 ..opts.clone()
819 };
820 run(start, &opts, Some((feature, Some(task))))
821}
822
823pub fn recorded(start: &Path, head: &str, feature: &str) -> Result<Checked, Refusal> {
831 let opts = Options {
832 head: head.to_owned(),
833 all: true,
834 ..Options::default()
835 };
836 run(start, &opts, Some((feature, None)))
837}
838
839#[must_use]
843pub fn closed(start: &Path, head: &str, feature: &str) -> Vec<(String, String)> {
844 recorded(start, head, feature)
845 .map(|c| {
846 c.report
847 .ticks
848 .into_iter()
849 .filter_map(|t| t.commit.map(|c| (t.tick.task, c)))
850 .collect()
851 })
852 .unwrap_or_default()
853}
854
855fn as_recorded(commit: &str, task: &str, statements: &[Value]) -> ObjectVerification {
858 let tv = statements
859 .iter()
860 .find(|s| s["predicateType"] == statement::TASK_VERIFICATION);
861 ObjectVerification {
862 object: commit.to_owned(),
863 kind: "task".to_owned(),
864 task: task.to_owned(),
865 verdict: tv.and_then(|s| s["predicate"]["verdict"].as_str().map(str::to_owned)),
866 outcome: if statements.is_empty() {
867 VerifyOutcome::Missing
868 } else {
869 VerifyOutcome::Ok
870 },
871 reasons: Vec::new(),
872 signer: None,
873 statements: statements.len(),
874 same_tree_as: None,
875 }
876}
877
878#[allow(clippy::too_many_lines)] fn run(
880 start: &Path,
881 opts: &Options,
882 only: Option<(&str, Option<&str>)>,
883) -> Result<Checked, Refusal> {
884 let top =
885 Git::at(start).toplevel().ok().flatten().ok_or_else(|| {
886 Refusal::new(CommandStatus::GitPrecondition, "not in a git repository")
887 })?;
888 let git = Git::at(&top);
889 let resolve = |rev: &str| {
890 git.line(&[
891 "rev-parse",
892 "--verify",
893 "--quiet",
894 &format!("{rev}^{{commit}}"),
895 ])
896 .ok()
897 .filter(|c| !c.is_empty())
898 .ok_or_else(|| {
899 Refusal::new(
900 CommandStatus::GitPrecondition,
901 format!("{rev} is not a commit here (fetch it first)"),
902 )
903 })
904 };
905 let head = resolve(&opts.head)?;
906 let base = match opts.base.as_deref() {
907 Some(b) => {
908 let b = resolve(b)?;
909 Some(
910 git.line(&["merge-base", &b, &head])
911 .ok()
912 .filter(|m| !m.is_empty())
913 .ok_or_else(|| {
914 Refusal::new(
915 CommandStatus::GitPrecondition,
916 format!("{b} and {head} share no history"),
917 )
918 })?,
919 )
920 }
921 None => None,
922 };
923 let (trust, trust_shown, _held) = trust(&top, opts, only.is_some())?;
924 let mode = if opts.all || base.is_none() {
925 Mode::All
926 } else {
927 Mode::Touched
928 };
929 let mut report = BranchReport {
930 head: head.clone(),
931 base: base.clone(),
932 mode,
933 features: Vec::new(),
934 ticks: Vec::new(),
935 skipped: Vec::new(),
936 totals: BranchTotals::default(),
937 conclusion: Conclusion::Neutral,
938 trust: trust_shown,
939 accepted_unsigned: !opts.require_signed,
940 require_verified: opts.require_verified,
941 trace: Vec::new(),
942 share: None,
943 open_no_claim: Vec::new(),
944 not_evaluated: vec![verify_branch::EARLIER_TASK_REGRESSIONS.to_owned()],
945 newer_fields: Vec::new(),
946 };
947 let history = history(
948 &git,
949 &head,
950 base.as_deref().filter(|_| mode == Mode::Touched),
951 )?;
952 let start_rel = relative(start, &top).unwrap_or_default();
953 let dir = super::tree::intent_tops(&top, &head)
954 .and_then(|tops| super::tree::materialize(&top, &head, Some(&tops)))
955 .map_err(Refusal::internal)?;
956 let opened = {
957 let at = dir.path().join(&start_rel);
958 let format = crate::format::at(&at);
959 format.inspect(&at, None).map(|snap| (format, snap))
960 };
961 let Some((format, snap)) = opened else {
962 let reference = base.clone().or_else(|| match &opts.allowed_signers {
966 Some(AllowedSigners::Rev { rev, .. }) => resolve(rev).ok(),
967 _ => None,
968 });
969 if let Some(r) = reference
970 && project_at(&top, &r, &start_rel)?
971 {
972 return Err(Refusal::new(
973 CommandStatus::AttestationFailed,
974 format!(
975 "{head} holds no spec project where {r} holds one: the head removes or breaks it, and its ticks cannot be checked"
976 ),
977 ));
978 }
979 report.conclusion = Conclusion::of(&[], opts.require_verified, &report.not_evaluated);
980 return Ok(Checked {
981 report,
982 repo: top,
983 prefix: as_prefix(&start_rel),
984 });
985 };
986 let project = PathBuf::from(&snap.root);
987 let prefix = as_prefix(&relative(&project, dir.path()).unwrap_or_default());
988 let mut units: Vec<&speccoder_core::snapshot::FeatureRef> = snap
989 .features
990 .iter()
991 .filter(|f| only.is_none_or(|(name, _)| f.name == name))
992 .collect();
993 if mode == Mode::Touched
994 && let Some(b) = &base
995 {
996 let paths = changed(&git, b, &head)?;
997 let dirs: Vec<String> = units
998 .iter()
999 .map(|f| format!("{prefix}{}/", f.dir.trim_end_matches('/')))
1000 .collect();
1001 let policy = crate::tools::policy::Policy::new(&[], &[]).rules(format.rules());
1002 let shared = paths.iter().any(|p| {
1004 p.strip_prefix(&prefix).is_some_and(|rel| {
1005 !dirs.iter().any(|d| p.starts_with(d))
1006 && policy.write_protected(rel) == Some(crate::tools::paths::Class::Intent)
1007 })
1008 });
1009 if !shared {
1010 units = units
1011 .into_iter()
1012 .zip(&dirs)
1013 .filter(|(_, d)| paths.iter().any(|p| p.starts_with(d.as_str())))
1014 .map(|(f, _)| f)
1015 .collect();
1016 }
1017 }
1018 let skipped = skips(&git)?;
1019 let config_path = crate::verify::find_config(&project)
1020 .and_then(|d| relative(&d, dir.path()))
1021 .map_or_else(
1022 || format!("{prefix}{}", crate::verify::config::FILE),
1023 |d| format!("{}{}", as_prefix(&d), crate::verify::config::FILE),
1024 );
1025 let mut lost: Option<BTreeMap<String, String>> = None;
1026 for f in units {
1027 let mut u = unit(format, &project, &f.name, &prefix, &skipped)?;
1028 if let Some((_, Some(task))) = only {
1029 u.ticks.retain(|t| t.task == task);
1030 }
1031 report.features.push(u.name.clone());
1032 report.skipped.extend(u.skipped);
1033 let tasks_path = u.ticks.first().map(|t| t.anchor.path.clone());
1034 let unchecked = Trust::default();
1035 let cx = Context {
1036 repo: &top,
1037 feature: &u.name,
1038 tasks_path: tasks_path.as_deref(),
1039 config_path: &config_path,
1040 trust: trust.as_ref().unwrap_or(&unchecked),
1041 format: Some(format),
1042 };
1043 let checking = trust.is_some();
1044 if lost.is_none()
1047 && u.ticks.iter().any(|t| {
1048 !history
1049 .latest
1050 .contains_key(&format!("{}/{}", t.feature, t.task))
1051 })
1052 {
1053 lost = Some(unreachable_evidence(&git, &top, &head)?);
1054 }
1055 let width = std::thread::available_parallelism().map_or(4, std::num::NonZero::get);
1057 let mut reports: Vec<TickReport> = Vec::new();
1058 let mut signed: BTreeMap<String, Vec<Value>> = BTreeMap::new();
1059 for chunk in u.ticks.chunks(width.max(1)) {
1060 let done: Vec<(TickReport, Vec<Value>)> = std::thread::scope(|scope| {
1061 let handles: Vec<_> = chunk
1062 .iter()
1063 .map(|tick| {
1064 let commit = history
1065 .latest
1066 .get(&format!("{}/{}", tick.feature, tick.task))
1067 .cloned();
1068 let cx = &cx;
1069 scope.spawn(move || {
1070 let commit = commit?;
1071 let statements = crate::evidence::statements(cx.repo, &commit);
1072 if !checking {
1073 let v = as_recorded(&commit, &tick.task, &statements);
1074 let mut r = outcome_of(tick, &commit, v, &statements, false);
1075 if r.outcome == TickOutcome::Evidenced {
1076 r.detail = Some(
1077 "as recorded: signatures not checked (no trust given)"
1078 .to_owned(),
1079 );
1080 }
1081 return Some((r, statements));
1082 }
1083 let v = super::verify::object(
1084 cx,
1085 &Target {
1086 object: commit.clone(),
1087 kind: "task",
1088 task: tick.task.clone(),
1089 },
1090 );
1091 Some((
1092 outcome_of(tick, &commit, v, &statements, opts.require_signed),
1093 statements,
1094 ))
1095 })
1096 })
1097 .collect();
1098 handles
1099 .into_iter()
1100 .zip(chunk)
1101 .map(|(h, tick)| match h.join() {
1102 Ok(Some(done)) => done,
1103 Ok(None) => (
1104 without_commit(
1105 tick,
1106 lost.as_ref().and_then(|l| {
1107 l.get(&format!("{}/{}", tick.feature, tick.task))
1108 }),
1109 ),
1110 Vec::new(),
1111 ),
1112 Err(_) => {
1113 let mut r = without_commit(tick, None);
1114 r.reason = Some(Unverifiable::Malformed);
1115 r.detail = Some("the check panicked".to_owned());
1116 (r, Vec::new())
1117 }
1118 })
1119 .collect()
1120 });
1121 for (r, statements) in done {
1122 let rows: Vec<Value> = statements
1123 .iter()
1124 .filter(|s| s["predicateType"] == statement::TASK_VERIFICATION)
1125 .flat_map(|s| {
1126 s["predicate"]["trace"]
1127 .as_array()
1128 .cloned()
1129 .unwrap_or_default()
1130 })
1131 .collect();
1132 signed.insert(r.tick.task.clone(), rows);
1133 reports.push(r);
1134 }
1135 }
1136 mark_stale(&top, format, &project, &prefix, &u.dir, &head, &mut reports);
1137 let requirements = format.requirement_ids(&project, &u.dir);
1138 report.trace.extend(trace(&requirements, &reports, &signed));
1139 report.ticks.extend(reports);
1140 }
1141 for claim in &history.bug_claims {
1144 let Some(tick) = trailer_only(format, &project, &prefix, claim) else {
1145 if !report
1147 .not_evaluated
1148 .iter()
1149 .any(|n| n == verify_branch::TRAILER_ONLY_TASKS)
1150 {
1151 report
1152 .not_evaluated
1153 .push(verify_branch::TRAILER_ONLY_TASKS.to_owned());
1154 }
1155 continue;
1156 };
1157 if only.is_some_and(|(name, task)| {
1158 tick.feature != name || task.is_some_and(|t| t != tick.task)
1159 }) {
1160 continue;
1161 }
1162 let Some(commit) = history.latest.get(claim).cloned() else {
1163 continue;
1164 };
1165 let statements = crate::evidence::statements(&top, &commit);
1166 let unchecked = Trust::default();
1167 let cx = Context {
1168 repo: &top,
1169 feature: &tick.feature,
1170 tasks_path: None,
1171 config_path: &config_path,
1172 trust: trust.as_ref().unwrap_or(&unchecked),
1173 format: Some(format),
1174 };
1175 let r = if trust.is_some() {
1176 let v = super::verify::object(
1177 &cx,
1178 &Target {
1179 object: commit.clone(),
1180 kind: "task",
1181 task: tick.task.clone(),
1182 },
1183 );
1184 outcome_of(&tick, &commit, v, &statements, opts.require_signed)
1185 } else {
1186 let v = as_recorded(&commit, &tick.task, &statements);
1187 let mut r = outcome_of(&tick, &commit, v, &statements, false);
1188 if r.outcome == TickOutcome::Evidenced {
1189 r.detail = Some("as recorded: signatures not checked (no trust given)".to_owned());
1190 }
1191 r
1192 };
1193 if !report.features.contains(&tick.feature) {
1194 report.features.push(tick.feature.clone());
1195 }
1196 report.ticks.push(r);
1197 }
1198 let mut newer: BTreeSet<String> = BTreeSet::new();
1199 for t in &report.ticks {
1200 newer.extend(t.newer_fields.iter().cloned());
1201 }
1202 report.newer_fields = newer.into_iter().collect();
1203 report.totals = BranchTotals::of(&report.ticks, report.skipped.len());
1204 report.share = verify_branch::share(&report.ticks);
1205 report.conclusion = Conclusion::of(&report.ticks, opts.require_verified, &report.not_evaluated);
1206 Ok(Checked {
1207 report,
1208 repo: top,
1209 prefix,
1210 })
1211}
1212
1213#[derive(Debug, Clone, Default)]
1215pub struct Outputs {
1216 pub markdown: Option<PathBuf>,
1218 pub github_check: Option<PathBuf>,
1220 pub check_name: String,
1222 pub allow_neutral: bool,
1224 pub annotations: bool,
1227}
1228
1229#[derive(Debug)]
1231pub struct Published {
1232 pub result: CommandResult,
1234 pub workflow_commands: Vec<String>,
1236}
1237
1238#[must_use]
1241pub fn verify_and_publish(start: &Path, opts: &Options, out: &Outputs, command: &str) -> Published {
1242 let checked = match check(start, opts) {
1243 Ok(c) => c,
1244 Err(r) => {
1245 return Published {
1246 result: CommandResult::new(command, r.status, &r.message),
1247 workflow_commands: Vec::new(),
1248 };
1249 }
1250 };
1251 let tours = if out.markdown.is_some() || out.github_check.is_some() || out.annotations {
1253 crate::tour::markdown_all(&checked)
1254 } else {
1255 Vec::new()
1256 };
1257 let report = &checked.report;
1258 let mut failed = Vec::new();
1259 if let Some(path) = &out.markdown
1260 && let Err(e) = write(path, &super::check::markdown(report, &tours))
1261 {
1262 failed.push(e);
1263 }
1264 if let Some(path) = &out.github_check {
1265 let name = if out.check_name.is_empty() {
1266 super::check::NAME
1267 } else {
1268 &out.check_name
1269 };
1270 let body = super::check::request(report, &tours, name, out.allow_neutral);
1271 if let Err(e) = write(path, &format!("{body}\n")) {
1272 failed.push(e);
1273 }
1274 }
1275 let mut workflow_commands = Vec::new();
1276 if out.annotations {
1277 workflow_commands = super::check::workflow_commands(report);
1278 if let Some(summary) = std::env::var_os("GITHUB_STEP_SUMMARY") {
1279 use std::io::Write as _;
1280 let appended = std::fs::OpenOptions::new()
1281 .append(true)
1282 .create(true)
1283 .open(&summary)
1284 .and_then(|mut f| writeln!(f, "{}", super::check::markdown(report, &tours)));
1285 if let Err(e) = appended {
1286 failed.push(format!("the step summary was not written: {e}"));
1287 }
1288 }
1289 }
1290 let result = result(command, checked.report);
1291 let result = if failed.is_empty() {
1292 result
1293 } else {
1294 failed_writes(command, result, &failed)
1295 };
1296 Published {
1297 result,
1298 workflow_commands,
1299 }
1300}
1301
1302#[must_use]
1306pub fn badge(
1307 start: &Path,
1308 opts: &Options,
1309 svg: Option<&Path>,
1310 endpoint: Option<&Path>,
1311 command: &str,
1312) -> CommandResult {
1313 let opts = Options {
1314 all: true,
1315 ..opts.clone()
1316 };
1317 let checked = match check(start, &opts) {
1318 Ok(c) => c,
1319 Err(r) => return CommandResult::new(command, r.status, &r.message),
1320 };
1321 let badge = verify_branch::Badge::of(&checked.report);
1322 let mut failed = Vec::new();
1323 if let Some(path) = svg
1324 && let Err(e) = write(path, &badge.svg())
1325 {
1326 failed.push(e);
1327 }
1328 if let Some(path) = endpoint {
1329 let json = serde_json::to_string_pretty(&badge).unwrap_or_default();
1330 if let Err(e) = write(path, &format!("{json}\n")) {
1331 failed.push(e);
1332 }
1333 }
1334 let result = CommandResult::new(
1335 command,
1336 CommandStatus::Ok,
1337 &format!("evidence: {} ({})", badge.message, badge.color),
1338 )
1339 .with_data(CommandData::BranchVerify(Box::new(checked.report)));
1340 if failed.is_empty() {
1341 result
1342 } else {
1343 failed_writes(command, result, &failed)
1344 }
1345}
1346
1347fn write(path: &Path, text: &str) -> Result<(), String> {
1348 std::fs::write(path, text).map_err(|e| format!("cannot write {}: {e}", path.display()))
1349}
1350
1351fn failed_writes(command: &str, result: CommandResult, failed: &[String]) -> CommandResult {
1353 let message = format!("{}; {}", result.message(), failed.join("; "));
1354 let shown = CommandResult::new(command, CommandStatus::Error, &message);
1355 match result.data() {
1356 Some(CommandData::BranchVerify(r)) => shown.with_data(CommandData::BranchVerify(r.clone())),
1357 _ => shown,
1358 }
1359}
1360
1361#[cfg(test)]
1362mod tests {
1363 use super::*;
1364
1365 #[test]
1366 fn a_trust_revision_needs_both_parts() {
1367 assert!(matches!(
1368 AllowedSigners::parse_rev("origin/main:.github/speccoder/allowed_signers"),
1369 Ok(AllowedSigners::Rev { rev, path }) if rev == "origin/main" && path == ".github/speccoder/allowed_signers"
1370 ));
1371 assert!(AllowedSigners::parse_rev("main").is_err());
1372 assert!(AllowedSigners::parse_rev(":x").is_err());
1373 }
1374
1375 #[test]
1376 fn the_line_of_a_requirement_is_found_trimmed() {
1377 let text = "# Spec\n\n- **FR-001**: Users MUST log in.\n";
1378 assert_eq!(
1379 line_of(text, " - **FR-001**: Users MUST log in. "),
1380 Some(3)
1381 );
1382 assert_eq!(line_of(text, "nope"), None);
1383 }
1384
1385 #[test]
1386 fn a_git_failure_is_an_error_never_an_empty_answer() {
1387 let dir = tempfile::tempdir().unwrap();
1388 let run = |args: &[&str]| {
1389 assert!(
1390 std::process::Command::new("git")
1391 .args(["-c", "user.name=t", "-c", "user.email=t@example.com"])
1392 .args(["-c", "commit.gpgsign=false"])
1393 .args(args)
1394 .current_dir(dir.path())
1395 .output()
1396 .unwrap()
1397 .status
1398 .success()
1399 );
1400 };
1401 run(&["init", "-q"]);
1402 run(&["commit", "-q", "--allow-empty", "-m", "x"]);
1403 let git = Git::at(dir.path());
1404 let Err(r) = changed(&git, "no-such-base", "HEAD") else {
1406 panic!("a failed diff was an empty one");
1407 };
1408 assert_eq!(r.status, CommandStatus::Error);
1409 assert!(history(&git, "no-such-head", None).is_err());
1410 assert!(history(&git, "HEAD", Some("no-such-base")).is_err());
1411 assert!(unreachable_evidence(&git, dir.path(), "no-such-head").is_err());
1412 assert!(changed(&git, "HEAD", "HEAD").unwrap().is_empty());
1413 }
1414
1415 #[test]
1416 fn no_trust_is_refused_even_when_unsigned_ticks_are_accepted() {
1417 let dir = tempfile::tempdir().unwrap();
1418 let opts = Options {
1419 require_signed: false,
1420 ..Options::default()
1421 };
1422 let Err(r) = trust(dir.path(), &opts, false) else {
1423 panic!("trusted nothing")
1424 };
1425 assert_eq!(r.message, NO_TRUST);
1426 }
1427}