1use std::fmt::Write as _;
13use std::path::Path;
14
15use speccoder_core::{CommandData, CommandResult, CommandStatus, PrReport, StatusReport};
16
17use crate::git::Git;
18
19#[derive(Debug, Clone)]
21pub struct PrOptions<'a> {
22 pub remote: &'a str,
24 pub base: Option<&'a str>,
26 pub open: bool,
28 pub force: bool,
30 pub github: &'a crate::github::Ctx,
32}
33
34fn trailers(repo: &Git, base: &str, branch: &str) -> Vec<(String, String, String)> {
36 const FS: char = '\u{1f}';
37 const RS: char = '\u{1e}';
38 let format = format!(
39 "--format=%(trailers:key=Spec-Task,valueonly){FS}%(trailers:key=Mutation,valueonly){FS}%(trailers:key=Usage,valueonly){RS}"
40 );
41 let log = repo
42 .run(&["log", &format, &format!("{base}..{branch}")])
43 .map(|b| String::from_utf8_lossy(&b).into_owned())
44 .unwrap_or_default();
45 log.split(RS)
46 .filter_map(|r| {
47 let f: Vec<&str> = r.trim_start_matches('\n').split(FS).collect();
48 let [task, mutation, usage] = f[..] else {
49 return None;
50 };
51 let task = task.trim().rsplit('/').next()?.to_owned();
52 (!task.is_empty()).then(|| (task, mutation.trim().to_owned(), usage.trim().to_owned()))
53 })
54 .collect()
55}
56
57fn bug_trailers(repo: &Git, base: &str, branch: &str) -> Vec<(String, String, String)> {
60 const FS: char = '\u{1f}';
61 const RS: char = '\u{1e}';
62 let format = format!(
63 "--format=%(trailers:key=Spec-Task,valueonly){FS}%(trailers:key=Bug,valueonly){FS}%(trailers:key=Fixes,valueonly){RS}"
64 );
65 let log = repo
66 .run(&["log", &format, &format!("{base}..{branch}")])
67 .map(|b| String::from_utf8_lossy(&b).into_owned())
68 .unwrap_or_default();
69 log.split(RS)
70 .filter_map(|r| {
71 let f: Vec<&str> = r.trim_start_matches('\n').split(FS).collect();
72 let [task, bug, fixes] = f[..] else {
73 return None;
74 };
75 let task = task.trim().to_owned();
77 (task.contains('/') && !fixes.trim().is_empty())
78 .then(|| (task, bug.trim().to_owned(), fixes.trim().to_owned()))
79 })
80 .collect()
81}
82
83#[must_use]
90pub fn bug_fixes(status: &StatusReport, trailers: &[(String, String, String)]) -> Vec<u64> {
91 let states: Vec<(&str, &str)> = status
92 .tasks
93 .iter()
94 .map(|t| (t.id.as_str(), t.state.as_str()))
95 .collect();
96 closes(&status.feature, &states, trailers)
97}
98
99fn closes(
101 feature: &str,
102 states: &[(&str, &str)],
103 trailers: &[(String, String, String)],
104) -> Vec<u64> {
105 let mut out = Vec::new();
106 for (claim, bug, fixes) in trailers {
107 let Some((unit, task)) = claim.rsplit_once('/') else {
108 continue;
109 };
110 let trailer_only = task.strip_prefix("bug:").is_some_and(|slug| {
111 speccoder_core::bug::valid_slug(slug) && unit == format!("bugs/{slug}")
112 });
113 let holds = trailer_only
114 || (unit == feature
115 && states
116 .iter()
117 .find(|(id, _)| *id == task)
118 .is_some_and(|(_, state)| matches!(*state, "verified" | "attested")));
119 if !bug.starts_with("fixed ") || !holds {
120 continue;
121 }
122 for n in fixes
123 .lines()
124 .filter_map(|l| l.trim().trim_start_matches('#').parse::<u64>().ok())
125 {
126 if !out.contains(&n) {
127 out.push(n);
128 }
129 }
130 }
131 out
132}
133
134fn attestation(state: &str) -> String {
137 match state.strip_prefix("signed ") {
138 Some(signer) => format!("`{signer}`"),
139 None => state.to_owned(),
140 }
141}
142
143#[must_use]
147pub fn verify_section(
148 status: &StatusReport,
149 remote: &str,
150 sigstore: Option<(&str, &str)>,
151) -> Option<String> {
152 let attested = status
153 .tasks
154 .iter()
155 .filter(|t| t.attestation.as_deref().is_some_and(|a| a != "missing"))
156 .count();
157 let trust = sigstore.map_or_else(
158 || "--allowed-signers <file>".to_owned(),
159 |(identity, issuer)| {
160 format!("--certificate-identity {identity} --certificate-oidc-issuer {issuer}")
161 },
162 );
163 (attested > 0).then(|| {
164 format!(
165 "\n**Attestations.** {attested} tick(s) carry in-toto statements (DSSE, SLSA VSA) in the git note `{REF}` on their commit. To check them yourself:\n\n```sh\ngit fetch {remote} {REF}:{REF}\nspeccoder attest verify --feature {} {trust}\n```\n",
166 status.feature,
167 REF = crate::attest::notes::REF,
168 )
169 })
170}
171
172#[must_use]
175pub fn body(status: &StatusReport, trailers: &[(String, String, String)]) -> String {
176 let t = &status.totals;
177 let ticked = t.verified + t.regression_only + t.attested + t.ticked_without_evidence;
178 let mut out = String::new();
179 let _ = writeln!(
180 out,
181 "`{}`, implemented by speccoder. **{ticked} of {} tasks ticked**: {} verified (spec-derived acceptance tests went from red to green), {} regression only (the checks stayed green), {} attested by a person, {} ticked without evidence; {} skipped, {} open.\n",
182 status.feature,
183 status.tasks.len(),
184 t.verified,
185 t.regression_only,
186 t.attested,
187 t.ticked_without_evidence,
188 t.skipped,
189 t.ready + t.blocked
190 );
191 let _ = writeln!(
192 out,
193 "| Task | Evidence | Mutation | Attestation | Commit |\n|---|---|---|---|---|"
194 );
195 for task in &status.tasks {
196 let mutation = trailers
197 .iter()
198 .find(|(id, m, _)| *id == task.id && !m.is_empty())
199 .map(|(_, m, _)| m.replace(' ', ", "))
200 .unwrap_or_default();
201 let mut evidence = task.state.replace('_', " ");
202 if let Some(n) = &task.note {
203 let _ = write!(evidence, " — {n}");
204 }
205 if let Some(a) = &task.last_attempt {
206 let _ = write!(
207 evidence,
208 " (last attempt: {}, `{}`)",
209 a.outcome.replace('_', " "),
210 a.reference
211 );
212 }
213 let title: String = task.title.chars().take(70).collect();
214 let _ = writeln!(
215 out,
216 "| {} {} | {} | {} | {} | {} |",
217 task.id,
218 title.replace('|', "\\|"),
219 evidence.replace('|', "\\|"),
220 mutation,
221 task.attestation
222 .as_deref()
223 .map(attestation)
224 .unwrap_or_default(),
225 task.commit
226 .as_deref()
227 .map(|c| &c[..c.len().min(9)])
228 .unwrap_or("")
229 );
230 }
231 let (mut input, mut output) = (0u64, 0u64);
232 for (_, _, usage) in trailers {
233 if let Some(u) = crate::attempts::parse_usage(usage) {
234 input += u["input_tokens"].as_u64().unwrap_or(0);
235 output += u["output_tokens"].as_u64().unwrap_or(0);
236 }
237 }
238 if input + output > 0 {
239 let _ = writeln!(
240 out,
241 "\nTokens: {input} in, {output} out (`speccoder report --cost` has the breakdown)."
242 );
243 }
244 if !status.convergence.is_empty() {
245 let _ = writeln!(
246 out,
247 "\nConvergence appended {} round(s) of tasks after assessing the feature against its spec.",
248 status.convergence.len()
249 );
250 }
251 let _ = writeln!(
252 out,
253 "\n> `verified` means the spec-derived acceptance tests went from red to green and no check regressed. It does not mean the change is correct, complete or safe: review this like any contribution. Opened as a draft by `speccoder pr`; speccoder never merges."
254 );
255 out
256}
257
258pub const EVIDENCE_CHECK_DOCS: &str =
260 "https://hupe1980.github.io/speccoder/docs/github/#evidence-check";
261
262pub const BODY_LIMIT: usize = 65_536;
264
265#[must_use]
268pub fn evidence_workflow(repo_root: &Path) -> Option<String> {
269 let dir = repo_root.join(".github/workflows");
270 let mut files: Vec<std::path::PathBuf> = std::fs::read_dir(&dir)
271 .into_iter()
272 .flatten()
273 .flatten()
274 .map(|e| e.path())
275 .filter(|p| p.extension().is_some_and(|x| x == "yml" || x == "yaml"))
276 .collect();
277 files.sort();
278 files.into_iter().find_map(|p| {
279 let text = std::fs::read_to_string(&p).ok()?;
280 text.contains("speccoder-verify").then(|| {
281 format!(
282 ".github/workflows/{}",
283 p.file_name()
284 .map(|n| n.to_string_lossy())
285 .unwrap_or_default()
286 )
287 })
288 })
289}
290
291#[must_use]
294pub fn tours(tours: &[String], room: usize) -> String {
295 if tours.is_empty() {
296 return String::new();
297 }
298 let mut out = String::from("\n### Evidence tour\n\n");
299 let mut shown = 0;
300 for t in tours {
301 if out.chars().count() + t.chars().count() + 200 > room {
302 break;
303 }
304 out.push_str(t);
305 shown += 1;
306 }
307 if shown < tours.len() {
308 out.push_str(&format!(
309 "\n{} tour(s) not shown (GitHub's body limit) — `speccoder-verify tour --feature <unit> --task <id>`\n",
310 tours.len() - shown
311 ));
312 }
313 if shown == 0 && out.chars().count() > room {
314 return String::new();
315 }
316 out
317}
318
319#[must_use]
321pub fn pr(path: &Path, feature: Option<&str>, options: &PrOptions<'_>) -> CommandResult {
322 let refuse = |status: CommandStatus, m: &str| CommandResult::new("pr", status, m);
323 let status = crate::status::report(path, feature);
324 let Some(CommandData::Report(report)) = status.data() else {
325 return refuse(status.status(), status.message());
326 };
327 let Some(branch) = report.branch.clone() else {
328 return refuse(
329 CommandStatus::GitPrecondition,
330 "speccoder has not worked on this feature yet (no speccoder branch)",
331 )
332 .with_code("NOTHING-TO-HAND-OFF");
333 };
334 let repo_root = match Git::at(path).toplevel() {
335 Ok(Some(r)) => r,
336 _ => {
337 return refuse(CommandStatus::GitPrecondition, "not a git repository")
338 .with_code("NO-REPOSITORY");
339 }
340 };
341 let repo = Git::at(&repo_root);
342 let base = match options.base {
343 Some(b) => b.to_owned(),
344 None => match repo.line(&["rev-parse", "--abbrev-ref", "HEAD"]) {
345 Ok(b) if b != "HEAD" => b,
346 _ => {
347 return refuse(
348 CommandStatus::GitPrecondition,
349 "the checkout is not on a branch; pass --base",
350 );
351 }
352 },
353 };
354 if base == branch {
355 return refuse(
356 CommandStatus::GitPrecondition,
357 "the base is the speccoder branch itself",
358 );
359 }
360 let ahead = repo
361 .line(&["rev-list", "--count", &format!("{base}..{branch}")])
362 .ok()
363 .and_then(|n| n.parse::<usize>().ok())
364 .unwrap_or(0);
365 if ahead == 0 {
366 return refuse(
367 CommandStatus::GitPrecondition,
368 &format!("{branch} has nothing that {base} does not have"),
369 )
370 .with_code("NOTHING-TO-HAND-OFF");
371 }
372 if !repo
373 .succeeds(&["remote", "get-url", options.remote])
374 .unwrap_or(false)
375 {
376 return refuse(
377 CommandStatus::GitPrecondition,
378 &format!("no git remote `{}`", options.remote),
379 )
380 .with_code("NO-REMOTE");
381 }
382 let t = &report.totals;
383 let title = format!(
384 "{}: {} verified, {} regression only, {} open (speccoder)",
385 report.feature,
386 t.verified,
387 t.regression_only,
388 t.ready + t.blocked
389 );
390 let full =
392 crate::pipeline::full::read(&repo_root, &report.feature, &branch).filter(|t| !t.passed);
393 if let Some(t) = full.as_ref().filter(|_| !options.force) {
394 return refuse(
395 CommandStatus::VerificationFailed,
396 &format!(
397 "the full-tier checks failed on {branch}: {}; fix them, or pass --force",
398 t.failed.join(", ")
399 ),
400 );
401 }
402 let mut body = body(report, &trailers(&repo, &base, &branch));
403 if let Some(t) = &full {
404 body.push_str(&format!(
405 "\n> **Full-tier checks failed** on this branch: {}. Handed off with `--force`.\n",
406 t.failed.join(", ")
407 ));
408 }
409 let attest = crate::verify::find_config(path)
410 .and_then(|d| std::fs::read_to_string(d.join(crate::verify::config::FILE)).ok())
411 .and_then(|c| crate::substrate::run_config::parse(&c).ok())
412 .map(|c| c.attest);
413 let sigstore = attest
414 .as_ref()
415 .filter(|a| a.signer == crate::substrate::run_config::Signer::Sigstore)
416 .and_then(|a| {
417 a.sigstore_identity
418 .as_deref()
419 .zip(a.sigstore_issuer.as_deref())
420 });
421 if !report.issues.is_empty() {
423 body.push('\n');
424 for url in &report.issues {
425 body.push_str(&format!("Closes {url}\n"));
426 }
427 }
428 let fixes = bug_fixes(report, &bug_trailers(&repo, &base, &branch));
430 if !fixes.is_empty() {
431 body.push('\n');
432 for n in fixes {
433 body.push_str(&format!("Fixes #{n}\n"));
434 }
435 }
436 if let Some(section) = verify_section(report, options.remote, sigstore) {
437 body.push_str(§ion);
438 }
439 let tip = repo
441 .line(&["rev-parse", "--verify", &format!("{branch}^{{commit}}")])
442 .unwrap_or_default();
443 if let Ok(recorded) = crate::attest::branch::recorded(&repo_root, &tip, &report.feature) {
444 let room = BODY_LIMIT.saturating_sub(body.chars().count());
445 body.push_str(&tours(&crate::tour::markdown_all(&recorded), room));
446 }
447 let project_state = crate::format::at(path)
449 .detect(path)
450 .zip(crate::worktree::state_dir())
451 .map(|(root, state)| crate::worktree::project_state_dir(&state, &root));
452 let selection = project_state
453 .as_ref()
454 .map(|s| {
455 let closed = crate::held_out::closed_on(&repo_root, &tip, &report.feature);
456 crate::held_out::select_closed(s, &report.feature, &closed)
457 })
458 .unwrap_or_default();
459 let held_out_ref = format!("{}/{}", crate::held_out::REF_PREFIX, report.feature);
460 let evidence = evidence_workflow(&repo_root);
461 let refspec = format!("{branch}:refs/heads/{branch}");
462 let notes = crate::attest::notes::REF;
463 let push_notes = repo
464 .succeeds(&["rev-parse", "--verify", "-q", notes])
465 .unwrap_or(false);
466 let mut commands = vec![format!("git push {} {refspec}", options.remote)];
467 if push_notes {
468 commands.push(format!("git push {} {notes}", options.remote));
469 }
470 if !selection.sets.is_empty() {
471 commands.push(format!("git push {} {held_out_ref}", options.remote));
472 }
473 let github = crate::github::repo(&repo_root, Some(options.remote));
475 commands.push(match &github {
476 Ok(r) => format!(
477 "open (or update) a draft pull request on {}: {branch} → {base} (GitHub API, as you)",
478 r.slug()
479 ),
480 Err(e) => format!("no pull request can be opened: {e}"),
481 });
482 let mut out = PrReport {
483 feature: report.feature.clone(),
484 branch: branch.clone(),
485 base: base.clone(),
486 remote: options.remote.to_owned(),
487 title: title.clone(),
488 body: body.clone(),
489 commands,
490 opened: false,
491 url: None,
492 held_out_ref: (!selection.sets.is_empty()).then(|| held_out_ref.clone()),
493 held_out_tasks: selection.tasks.clone(),
494 held_out_withheld: selection.withheld,
495 evidence_check: if evidence.is_some() {
496 "present"
497 } else {
498 "absent"
499 }
500 .to_owned(),
501 };
502 if !options.open {
503 let mut message =
504 "dry run: nothing was pushed (confirm, or pass --yes, to push and open the draft)"
505 .to_owned();
506 message.push_str(&match &evidence {
507 Some(w) => format!("\nevidence check: {w} runs speccoder-verify"),
508 None => format!("\nevidence check: none — see {EVIDENCE_CHECK_DOCS}"),
509 });
510 if !selection.tasks.is_empty() {
511 message.push_str(&format!(
512 "\nheld-out: sets of {}",
513 selection.tasks.join(", ")
514 ));
515 }
516 if selection.withheld > 0 {
517 message.push_str(&format!(
518 "\nheld-out tests of {} open task(s) stay local",
519 selection.withheld
520 ));
521 }
522 if !selection.tampered.is_empty() {
523 message.push_str(&format!(
524 "\nheld-out sets changed since they judged, not published: {}",
525 selection.tampered.join(", ")
526 ));
527 }
528 return CommandResult::new("pr", CommandStatus::Ok, &message)
529 .with_data(CommandData::Pr(Box::new(out)));
530 }
531 let github = match github {
534 Ok(r) => r,
535 Err(e) => {
536 return crate::github::refused("pr", &format!("{e}; nothing was pushed"))
537 .with_data(CommandData::Pr(Box::new(out)));
538 }
539 };
540 if options.github.token(&github.host).is_none() {
541 return crate::github::refused(
542 "pr",
543 &format!(
544 "not signed in to {}: `speccoder auth login` (or set GH_TOKEN); nothing was pushed",
545 github.host
546 ),
547 )
548 .with_data(CommandData::Pr(Box::new(out)));
549 }
550 if let Err(e) = repo.run(&["push", "--quiet", options.remote, &refspec]) {
551 return CommandResult::new(
552 "pr",
553 CommandStatus::GitPrecondition,
554 &format!("push failed: {}", e.0),
555 )
556 .with_data(CommandData::Pr(Box::new(out)));
557 }
558 let notes_warning = push_notes
561 .then(|| repo.run(&["push", "--quiet", options.remote, &format!("{notes}:{notes}")]).err())
562 .flatten()
563 .map(|e| {
564 format!(
565 "; the attestation notes were not pushed ({}): `git fetch {r} {notes}:refs/notes/speccoder-remote && git notes --ref=speccoder merge refs/notes/speccoder-remote`, then `git push {r} {notes}`",
566 e.0.lines().last().unwrap_or_default().trim(),
567 r = options.remote,
568 )
569 })
570 .unwrap_or_default();
571 let held_out_warning = if selection.sets.is_empty() {
574 String::new()
575 } else {
576 let pushed = project_state
579 .as_ref()
580 .ok_or_else(|| "no state directory".to_owned())
581 .and_then(|s| {
582 let store = crate::held_out::publication_store(s, &repo_root)?;
583 let commit = crate::held_out::publish_closed(
584 &store,
585 &report.feature,
586 &tip,
587 &selection,
588 &s.join("tmp"),
589 )?;
590 match commit {
591 Some(c) => crate::held_out::push(
592 &repo_root,
593 &store,
594 options.remote,
595 &report.feature,
596 &c,
597 ),
598 None => Ok(()),
599 }
600 });
601 match pushed {
602 Ok(()) => String::new(),
603 Err(e) => format!(
604 "; the held-out tests were not pushed ({}): git fetch {r} {held_out_ref} and compare",
605 e.lines().last().unwrap_or_default().trim(),
606 r = options.remote,
607 ),
608 }
609 };
610 let notes_warning = format!("{notes_warning}{held_out_warning}");
611 let failed = |e: &str, out: PrReport| {
612 let r = crate::github::refused("pr", e);
613 let status = r.status();
614 r.reworded(status, &format!("pushed {branch}, but {e}"))
615 .with_data(CommandData::Pr(Box::new(out)))
616 };
617 match crate::github::pr_in(options.github, &github, &branch) {
622 Ok(Some(existing)) if existing.state == "OPEN" => {
623 out.opened = true;
624 out.url = Some(existing.url.clone());
625 if let Err(e) =
626 crate::github::update(options.github, &github, existing.number, &title, &body)
627 {
628 return failed(&e, out);
629 }
630 return CommandResult::new(
631 "pr",
632 CommandStatus::Ok,
633 &format!(
634 "pushed {branch}: updated pull request #{} and its evidence ({}){notes_warning}",
635 existing.number, existing.url
636 ),
637 )
638 .with_data(CommandData::Pr(Box::new(out)));
639 }
640 Ok(_) => {}
641 Err(e) => return failed(&e, out),
642 }
643 match crate::github::open_draft(options.github, &github, &base, &branch, &title, &body) {
644 Ok((number, url)) => {
645 out.opened = true;
646 out.url = Some(url.clone());
647 CommandResult::new(
648 "pr",
649 CommandStatus::Ok,
650 &format!("draft pull request #{number} opened: {url}{notes_warning}"),
651 )
652 .with_data(CommandData::Pr(Box::new(out)))
653 }
654 Err(e) => failed(&e, out),
655 }
656}
657
658#[cfg(test)]
659mod tests {
660 use super::*;
661
662 #[test]
663 fn a_workflow_running_the_verifier_is_found() {
664 let d = tempfile::tempdir().unwrap();
665 assert_eq!(evidence_workflow(d.path()), None);
666 let w = d.path().join(".github/workflows");
667 std::fs::create_dir_all(&w).unwrap();
668 std::fs::write(w.join("ci.yml"), "jobs: {}\n").unwrap();
669 std::fs::write(
670 w.join("evidence.yaml"),
671 "steps:\n - uses: hupe1980/speccoder/packaging/speccoder-verify-action@v0.2.0\n",
672 )
673 .unwrap();
674 assert_eq!(
675 evidence_workflow(d.path()).as_deref(),
676 Some(".github/workflows/evidence.yaml")
677 );
678 }
679
680 #[test]
681 fn only_the_units_own_held_bug_tasks_close_issues() {
682 let t = |claim: &str, n: u32| {
683 (
684 claim.to_owned(),
685 "fixed (standing_speckit) triad=sha256:x".to_owned(),
686 format!("#{n}"),
687 )
688 };
689 let states = [("T001", "verified"), ("T002", "ready")];
690 let trailers = [
691 t("001-bugfixes/T001", 1),
692 t("002-other/T001", 2),
694 t("001-bugfixes/T002", 3),
696 t("001-bugfixes/T009", 4),
698 t("bugs/empty-title/bug:empty-title", 5),
700 t("bugs/other/bug:empty-title", 6),
702 t("bugs/../x/bug:..", 7),
703 ];
704 assert_eq!(closes("001-bugfixes", &states, &trailers), [1, 5]);
705 }
706
707 #[test]
708 fn tours_fit_the_body_dropping_the_last_first() {
709 let all: Vec<String> = (0..10)
710 .map(|i| format!("<details>{i}{}</details>\n", "x".repeat(100)))
711 .collect();
712 let fitted = tours(&all, 700);
713 assert!(fitted.chars().count() <= 700, "{}", fitted.chars().count());
714 assert!(fitted.contains("<details>0"));
715 assert!(!fitted.contains("<details>9"));
716 assert!(fitted.contains("tour(s) not shown"));
717 assert_eq!(tours(&[], 100), "");
718 assert!(tours(&all, 100_000).contains("<details>9"));
719 }
720}