Skip to main content

release_kit/commands/
setup.rs

1//! `rk setup`: execute the repository-side setup against the detected forge.
2//!
3//! Preview is the default and is an offline rendering — it materializes
4//! nothing and invokes no external command. Apply runs each step as the
5//! observe-compare-apply-verify lifecycle: observe the current state,
6//! report and skip when satisfied, otherwise materialize the embedded
7//! script into the run's private journal directory, verify its digest,
8//! spawn it as `sh <path>`, and read the state back. `check` calls the same
9//! observe functions with the mutating half unreachable from its code path.
10
11use std::ffi::OsString;
12use std::fs;
13use std::path::PathBuf;
14use std::time::Instant;
15
16use zeroize::Zeroizing;
17
18use crate::cli::setup::{SetupAction, SetupArgs};
19use crate::detect::Forge;
20use crate::diagnostic::{Diagnostic, Reason};
21use crate::digest::Digest;
22use crate::embedded;
23use crate::error::RkError;
24use crate::events::{ChildStream, Event, EventKind};
25use crate::output::Output;
26use crate::setup::app_jwt::{self, AppApi};
27use crate::setup::context::{Ctx, SECRET_VARS};
28use crate::setup::journal::Journal;
29use crate::setup::observe::{self, StepState};
30use crate::setup::process::{self, Exec, Outcome};
31use crate::setup::secrets;
32use crate::setup::steps::{Mutates, STEPS, StepSpec, spec};
33
34/// Dispatch the setup surface.
35///
36/// # Errors
37///
38/// Every failure classified through the matrix, each carrying its `reason`.
39pub fn run(args: &SetupArgs) -> Result<(), RkError> {
40    match &args.action {
41        Some(SetupAction::Script { name, forge }) => script(name, forge.as_deref()),
42        Some(SetupAction::Check {
43            target,
44            repo,
45            forge,
46            required_check,
47            json,
48        }) => {
49            let ctx = Ctx::resolve(
50                target,
51                repo.as_deref(),
52                forge.as_deref(),
53                required_check.as_deref(),
54            )?;
55            reject_check_flag_on_gitlab(&ctx)?;
56            check(Output::new(*json), ctx)
57        }
58        Some(SetupAction::Step {
59            name,
60            target,
61            repo,
62            forge,
63            required_check,
64            apply,
65            json,
66        }) => {
67            let selected = spec(name).ok_or_else(|| {
68                RkError::Usage(format!("unknown step '{name}'; rk setup --list names them"))
69            })?;
70            let ctx = Ctx::resolve(
71                target,
72                repo.as_deref(),
73                forge.as_deref(),
74                required_check.as_deref(),
75            )?;
76            reject_check_flag_on_gitlab(&ctx)?;
77            if *apply {
78                refuse_an_excluded_step(&ctx, selected)?;
79                require_check_for(&ctx, &[selected])?;
80                execute(Output::new(*json), ctx, &[selected], "setup step")
81            } else {
82                preview(Output::new(*json), &ctx, &[selected])
83            }
84        }
85        None if args.list => list(args.forge.as_deref()),
86        None => {
87            let target = args.target.clone().ok_or_else(|| {
88                RkError::Usage("name a --target, or pass --list to see the steps".into())
89            })?;
90            let ctx = Ctx::resolve(
91                &target,
92                args.repo.as_deref(),
93                args.forge.as_deref(),
94                args.required_check.as_deref(),
95            )?;
96            reject_check_flag_on_gitlab(&ctx)?;
97            let all: Vec<&StepSpec> = STEPS.iter().collect();
98            if args.apply {
99                require_check_for(&ctx, &all)?;
100                execute(Output::new(args.json), ctx, &all, "setup")
101            } else {
102                preview(Output::new(args.json), &ctx, &all)
103            }
104        }
105    }
106}
107
108/// Whether a full run skips this step at this target.
109///
110/// `optional` is the payload's claim that a step is not universal; the
111/// target's own configuration answers whether it wants this one. Today
112/// `protect-release-lines` is the single such step, and a project that
113/// sets `setup.release_lines` gets it run rather than skipped and
114/// remembered.
115fn skipped_by_a_full_run(ctx: &Ctx, step: &StepSpec, selected: usize) -> bool {
116    if !step.optional || selected <= 1 {
117        return false;
118    }
119    !(step.name == "protect-release-lines" && ctx.release_lines())
120}
121
122/// A step the target declared it does not run is refused by name rather
123/// than applied. The committed file is the one statement of which steps
124/// this target runs, so a run that installed what the file excludes would
125/// leave the forge and the declaration disagreeing with nobody to notice;
126/// changing the model is one edit, and that edit is the auditable act.
127fn refuse_an_excluded_step(ctx: &Ctx, step: &StepSpec) -> Result<(), RkError> {
128    ctx.excluded(step.name).map_or(Ok(()), |reason| {
129        Err(RkError::Usage(format!(
130            "{} is excluded by {}: {reason}; remove it from setup.excluded_steps to run it",
131            step.name,
132            crate::config::CONFIG_PATH
133        )))
134    })
135}
136
137/// On GitLab `--required-check` is a usage error, per the forge document:
138/// the forge requires the whole pipeline and names no individual check, and
139/// a flag silently discarded would read as configured while nothing uses it.
140fn reject_check_flag_on_gitlab(ctx: &Ctx) -> Result<(), RkError> {
141    if ctx.forge == Forge::Gitlab && ctx.required_check.is_some() {
142        return Err(RkError::Usage(
143            "--required-check is refused on gitlab: the forge requires the whole pipeline and names no individual check".into(),
144        ));
145    }
146    Ok(())
147}
148
149/// On GitHub the trunk protection needs the check name before any step
150/// runs: a wrong or missing one does not fail, it hangs the merge button,
151/// so a full apply refuses up front rather than writing eight steps and
152/// stopping. A target that excludes the protection is asked for nothing,
153/// because the value would answer a step this run never reaches.
154fn require_check_for(ctx: &Ctx, steps: &[&StepSpec]) -> Result<(), RkError> {
155    let needs = ctx.forge == Forge::Github
156        && ctx.required_check.is_none()
157        && steps
158            .iter()
159            .any(|step| step.name == "protect-trunk" && ctx.excluded(step.name).is_none());
160    if needs {
161        return Err(RkError::refusal(
162            Diagnostic::new(
163                Reason::PrerequisiteUnmet,
164                "protect-trunk refuses until the required check is named, and nothing was written",
165            )
166            .expected("the name of the CI check the release merge must pass")
167            .action(format!(
168                "set setup.required_check in {}, or pass --required-check <name>; gh api repos/{}/commits/HEAD/check-runs lists the project's check names",
169                crate::config::CONFIG_PATH,
170                ctx.repo
171            ))
172            .step("protect-trunk"),
173        ));
174    }
175    Ok(())
176}
177
178/// `rk setup --list`: the ordered steps, what each proves, and which needs
179/// input on which forge — visible before a first apply rather than
180/// discovered by one.
181fn list(forge: Option<&str>) -> Result<(), RkError> {
182    let forge = forge
183        .map(|name| {
184            Forge::parse(name).ok_or_else(|| {
185                RkError::Usage(format!(
186                    "unknown forge '{name}'; the forges are: github, gitlab"
187                ))
188            })
189        })
190        .transpose()?;
191    let out = Output::human();
192    for (idx, step) in STEPS.iter().enumerate() {
193        let mut line = format!(
194            "{:2}. {} [{}] proves: {}",
195            idx + 1,
196            step.name,
197            step.chapter,
198            step.proves
199        );
200        if step.name == "protect-trunk" && forge != Some(Forge::Gitlab) {
201            line.push_str(" (needs --required-check on github)");
202        }
203        if step.destructive {
204            line.push_str(" (destructive)");
205        }
206        if step.optional {
207            line.push_str(" (optional; a full apply skips it)");
208        }
209        out.result_line(line);
210    }
211    out.next(&[
212        "rk setup --target . previews every step".to_owned(),
213        "rk setup script <name> prints one embedded script".to_owned(),
214    ]);
215    Ok(())
216}
217
218/// `rk setup script <name>`: the audit escape hatch, printed byte-identical
219/// to the embedded file.
220fn script(name: &str, forge: Option<&str>) -> Result<(), RkError> {
221    if name == "package-check" {
222        return Err(RkError::Usage(
223            "package-check reads its command from the technology binding and has no script".into(),
224        ));
225    }
226    if name == "branch-reminder" {
227        return Err(RkError::Usage(
228            "branch-reminder writes an embedded hook body and has no script; rk setup step branch-reminder previews the write".into(),
229        ));
230    }
231    if name == "forge-version" {
232        return Err(RkError::Usage(
233            "forge-version reads the forge's own version and has no script; rk setup step forge-version previews the read".into(),
234        ));
235    }
236    let forge = match forge {
237        Some(value) => Forge::parse(value).ok_or_else(|| {
238            RkError::Usage(format!(
239                "unknown forge '{value}'; the forges are: github, gitlab"
240            ))
241        })?,
242        None => Forge::Github,
243    };
244    let path = format!("{}/{name}", forge.as_str());
245    let file = embedded::SETUP.get_file(&path).ok_or(RkError::NotFound {
246        kind: "setup step",
247        name: name.to_owned(),
248    })?;
249    Output::human().result_raw(&String::from_utf8_lossy(file.contents()));
250    Ok(())
251}
252
253/// The shared run state: the boundary, the resolved context, the journal,
254/// and the event stream.
255struct Engine {
256    out: Output,
257    ctx: Ctx,
258    journal: Option<Journal>,
259    secrets: Vec<Zeroizing<Vec<u8>>>,
260    /// The run's one read of the named key file; see [`key_file_for`].
261    key: Option<secrets::KeyFile>,
262    /// The run's App JWT, minted at most once; see [`app_jwt_for`].
263    app_jwt: Option<String>,
264    seq: u64,
265    command: &'static str,
266    run_id: String,
267}
268
269impl Engine {
270    /// Open the run: journal first, before any remote mutation. An apply
271    /// that cannot create its journal refuses; observability-only modes
272    /// warn and continue, because refusing them over observability is
273    /// self-defeating.
274    fn open(
275        out: Output,
276        ctx: Ctx,
277        command: &'static str,
278        journal_required: bool,
279    ) -> Result<Self, RkError> {
280        // A stale key export is refused wherever a run opens, so every
281        // mode catches it and not the one step that would have used it.
282        secrets::refuse_legacy_key()?;
283        let journal =
284            match Journal::create(command, ctx.target.as_str(), ctx.forge.as_str(), &ctx.repo) {
285                Ok(journal) => Some(journal),
286                Err(source) if journal_required => {
287                    return Err(RkError::refusal(
288                        Diagnostic::new(
289                            Reason::JournalUnavailable,
290                            format!("the run journal cannot be created: {source}"),
291                        )
292                        .expected("a writable state root for the journal")
293                        .target_state("nothing was run and nothing changed"),
294                    ));
295                }
296                Err(source) => {
297                    out.warn(format!("no run journal for this run: {source}"));
298                    None
299                }
300            };
301        let run_id = journal
302            .as_ref()
303            .map_or_else(|| "unjournaled".to_owned(), |j| j.run_id().to_owned());
304        let mut engine = Self {
305            out,
306            ctx,
307            journal,
308            secrets: Ctx::secret_values(),
309            key: None,
310            app_jwt: None,
311            seq: 0,
312            command,
313            run_id,
314        };
315        let opening = Event::opening(
316            engine.next_seq(),
317            crate::applog::now_utc(),
318            engine.run_id.clone(),
319            engine.command,
320        );
321        engine.emit(&opening);
322        if engine.ctx.self_hosted_gitlab() {
323            engine.out.warn(
324                "this remote is a self-hosted GitLab: registry trusted publishing covers GitLab.com only, so the OIDC invariant cannot be satisfied here",
325            );
326        }
327        Ok(engine)
328    }
329
330    const fn next_seq(&mut self) -> u64 {
331        let seq = self.seq;
332        self.seq += 1;
333        seq
334    }
335
336    fn event(&mut self, kind: EventKind, step: Option<&str>) -> Event {
337        let mut event = Event::opening(
338            self.next_seq(),
339            crate::applog::now_utc(),
340            self.run_id.clone(),
341            self.command,
342        );
343        event.kind = kind;
344        event.step = step.map(str::to_owned);
345        event
346    }
347
348    fn emit(&mut self, event: &Event) {
349        self.out.event(event);
350        if let Some(journal) = &mut self.journal {
351            if let Ok(line) = serde_json::to_string(event) {
352                journal.event_line(&line);
353            }
354        }
355    }
356
357    /// Run one external command: echo it, stream and redact its output,
358    /// journal everything, surface its exit.
359    fn exec(&mut self, exec: &Exec, passthrough: bool) -> Result<Outcome, RkError> {
360        let echo = exec.echo();
361        self.out.frame(&echo);
362        if let Some(journal) = &mut self.journal {
363            journal.transcript(echo.as_bytes());
364            journal.transcript(b"\n");
365        }
366        let secrets = std::mem::take(&mut self.secrets);
367        let step_name: Option<String> = None;
368        let mut chunks: Vec<(ChildStream, Vec<u8>)> = Vec::new();
369        let spawned = process::run(exec, |stream, chunk| {
370            chunks.push((stream, process::redact(chunk, &secrets)));
371        });
372        self.secrets = secrets;
373        for (stream, chunk) in chunks {
374            if passthrough {
375                self.out.child_passthrough(stream, &chunk);
376            }
377            let event = self.event(EventKind::ChildOutput, step_name.as_deref());
378            let event = event.child_output(stream, &chunk);
379            self.emit(&event);
380            if let Some(journal) = &mut self.journal {
381                journal.transcript(&chunk);
382            }
383        }
384        spawned.map_err(|source| {
385            RkError::refusal(
386                Diagnostic::new(
387                    Reason::SubprocessSpawn,
388                    format!("{} did not spawn: {source}", exec.program.to_string_lossy()),
389                )
390                .expected("a POSIX sh and the forge CLI on PATH")
391                .run(self.run_path()),
392            )
393        })
394    }
395
396    fn run_path(&self) -> String {
397        self.journal.as_ref().map_or_else(
398            || "no journal was written".to_owned(),
399            |j| j.dir.display().to_string(),
400        )
401    }
402
403    fn finish(&mut self, exit_code: i32, reason: Option<&str>) {
404        let mut event = self.event(EventKind::RunFinished, None);
405        event.exit_code = Some(exit_code);
406        event.status = Some(if exit_code == 0 {
407            "ok".into()
408        } else {
409            "failed".into()
410        });
411        self.emit(&event);
412        if let Some(journal) = &mut self.journal {
413            journal.finish(exit_code, reason);
414        }
415    }
416}
417
418/// Attach the failure to its run journal and close the run.
419fn fail(engine: &mut Engine, error: RkError) -> RkError {
420    let error = match error {
421        RkError::Refusal(mut diagnostic) => {
422            diagnostic.run.get_or_insert_with(|| engine.run_path());
423            RkError::Refusal(diagnostic)
424        }
425        RkError::Subprocess(mut diagnostic) => {
426            diagnostic.run.get_or_insert_with(|| engine.run_path());
427            RkError::Subprocess(diagnostic)
428        }
429        RkError::CheckFailed(mut diagnostic) => {
430            diagnostic.run.get_or_insert_with(|| engine.run_path());
431            RkError::CheckFailed(diagnostic)
432        }
433        other => other,
434    };
435    engine.finish(i32::from(error.exit_code()), Some(error.reason().as_str()));
436    error
437}
438
439/// Preview: walk the ordered steps and print, for each, the step name, what
440/// it proves, and the resolved invocation — without materializing anything
441/// and without invoking any external command. What preview shows is what
442/// apply would run, because both read the same step table and environment
443/// construction.
444fn preview(out: Output, ctx: &Ctx, steps: &[&StepSpec]) -> Result<(), RkError> {
445    let mut engine = Engine::open(out, clone_ctx(ctx), "setup preview", false)?;
446    out.result_line(format!(
447        "DRY RUN: rk setup would run these steps against {} on {}; re-run with --apply",
448        engine.ctx.repo,
449        engine.ctx.forge.as_str()
450    ));
451    for (idx, step) in steps.iter().enumerate() {
452        out.result_line(format!(
453            "step {}/{} {} — proves {}",
454            idx + 1,
455            steps.len(),
456            step.name,
457            step.proves
458        ));
459        if let Some(reason) = engine.ctx.excluded(step.name).map(str::to_owned) {
460            out.result_line(format!(
461                "  excluded by {}: {reason}",
462                crate::config::CONFIG_PATH
463            ));
464            let mut event = engine.event(EventKind::StepFinished, Some(step.name));
465            event.status = Some("excluded".into());
466            event.detail = Some(reason);
467            engine.emit(&event);
468            continue;
469        }
470        // Preview is the rehearsal of apply, so a credential apply could
471        // not use is a preview failure: the operator learns it here rather
472        // than one flag later, and before an invocation is claimed.
473        if step.name == "bot-secrets" && engine.ctx.forge == Forge::Github {
474            secrets::resolve_key_file(&engine.ctx.target)?;
475        }
476        out.result_line(format!("  {}", render_invocation(&engine.ctx, step)));
477        if step.name == "protect-trunk"
478            && engine.ctx.forge == Forge::Github
479            && engine.ctx.required_check.is_none()
480        {
481            out.result_line("  needs: --required-check <name> before apply");
482        }
483        if skipped_by_a_full_run(&engine.ctx, step, steps.len()) {
484            out.result_line(format!(
485                "  optional: a full apply skips it; set setup.release_lines, or rk setup step {} --apply runs it",
486                step.name
487            ));
488        }
489        let mut event = engine.event(EventKind::StepFinished, Some(step.name));
490        event.status = Some("previewed".into());
491        engine.emit(&event);
492    }
493    let next = next_for_apply(&engine.ctx, steps);
494    out.next(&[
495        next,
496        "rk setup check --target . proves what is already true".to_owned(),
497    ]);
498    engine.finish(0, None);
499    Ok(())
500}
501
502/// The one line preview prints per step: the exact spawn shape, with every
503/// non-secret variable resolved.
504fn render_invocation(ctx: &Ctx, step: &StepSpec) -> String {
505    match step.name {
506        "branch-reminder" => {
507            "would write: the post-merge reminder hook at $(git rev-parse --git-path hooks)/post-merge".to_owned()
508        }
509        "package-check" => match ctx.tech {
510            Some("rust") => "would run: cargo publish --dry-run --allow-dirty".to_owned(),
511            Some("python") => "would run: python3 -m build".to_owned(),
512            Some("bash") => "nothing to run: no registry for this technology".to_owned(),
513            _ => "needs: a version file naming the technology".to_owned(),
514        },
515        "forge-version" => {
516            let (major, minor) = observe::GITLAB_VERSION_FLOOR;
517            match ctx.forge {
518                Forge::Github => {
519                    "nothing to read: github.com is a rolling service and declares no version floor"
520                        .to_owned()
521                }
522                Forge::Gitlab => format!(
523                    "would read: GET /version, and compare it against the {major}.{minor} floor; nothing is written"
524                ),
525            }
526        }
527        name => {
528            let check = ctx
529                .required_check
530                .as_ref()
531                .filter(|_| ctx.forge == Forge::Github && name == "protect-trunk")
532                .map(|value| format!(" RK_REQUIRED_CHECK={value}"))
533                .unwrap_or_default();
534            // The ruleset a protection step installs is the one
535            // per-target fact the operator most needs to see before an
536            // apply, because a renamed ruleset leaves the old one standing.
537            let ruleset = match name {
538                "protect-trunk" => format!(" RK_TRUNK_RULESET={} RK_TITLE_CHECK={}", ctx.trunk_ruleset(), ctx.title_check()),
539                "protect-tags" => format!(" RK_TAG_RULESET={}", ctx.tag_ruleset()),
540                "protect-release-lines" => format!(" RK_LINES_RULESET={}", ctx.lines_ruleset()),
541                _ => String::new(),
542            };
543            format!(
544                "would run: sh <embedded setup/{}/{name}> with RK_REPO={} RK_TRUNK_BRANCH={}{ruleset}{check}",
545                ctx.forge.as_str(),
546                ctx.repo,
547                ctx.trunk()
548            )
549        }
550    }
551}
552
553fn next_for_apply(ctx: &Ctx, steps: &[&StepSpec]) -> String {
554    let check = ctx
555        .required_check
556        .as_ref()
557        .map(|value| format!(" --required-check {value}"))
558        .unwrap_or_default();
559    if steps.len() == 1 {
560        format!(
561            "rk setup step {} --target {} --apply{check}",
562            steps[0].name, ctx.target
563        )
564    } else {
565        format!("rk setup --target {} --apply{check}", ctx.target)
566    }
567}
568
569/// A `Ctx` copy for engine ownership; the context is plain data.
570fn clone_ctx(ctx: &Ctx) -> Ctx {
571    ctx.clone()
572}
573
574/// Apply: run the selected steps in order, each through the full lifecycle.
575fn execute(
576    out: Output,
577    ctx: Ctx,
578    steps: &[&StepSpec],
579    command: &'static str,
580) -> Result<(), RkError> {
581    guard_sh()?;
582    let mut engine = Engine::open(out, ctx, command, true)?;
583    let mut done: Vec<(String, String)> = Vec::new();
584    for (idx, step) in steps.iter().enumerate() {
585        // A declared exclusion states the skip and runs nothing. A single
586        // step named on the command line never arrives here: that form
587        // refuses before the run opens.
588        if let Some(reason) = engine.ctx.excluded(step.name).map(str::to_owned) {
589            engine.out.frame(format!(
590                "step {}/{} {} — excluded ({}: {reason})",
591                idx + 1,
592                steps.len(),
593                step.name,
594                crate::config::CONFIG_PATH
595            ));
596            let mut finished = engine.event(EventKind::StepFinished, Some(step.name));
597            finished.status = Some("excluded".into());
598            finished.detail = Some(reason);
599            engine.emit(&finished);
600            done.push((step.name.to_owned(), "excluded".to_owned()));
601            continue;
602        }
603        // An optional step applies only by name: a full run states the skip
604        // rather than acting on a condition the operator never asserted.
605        if skipped_by_a_full_run(&engine.ctx, step, steps.len()) {
606            engine.out.frame(format!(
607                "step {}/{} {} — skipped (optional; set setup.release_lines, or rk setup step {} --apply runs it)",
608                idx + 1,
609                steps.len(),
610                step.name,
611                step.name
612            ));
613            let mut finished = engine.event(EventKind::StepFinished, Some(step.name));
614            finished.status = Some("skipped".into());
615            engine.emit(&finished);
616            done.push((step.name.to_owned(), "skipped".to_owned()));
617            continue;
618        }
619        engine.out.frame(format!(
620            "step {}/{} {} — {}",
621            idx + 1,
622            steps.len(),
623            step.name,
624            step.proves
625        ));
626        let mut started = engine.event(EventKind::StepStarted, Some(step.name));
627        started.status = Some("running".into());
628        engine.emit(&started);
629        let clock = Instant::now();
630        let status = match apply_step(&mut engine, step) {
631            Ok(status) => status,
632            Err(error) => {
633                let error = attach_progress(error, &done, step, steps);
634                let mut finished = engine.event(EventKind::StepFinished, Some(step.name));
635                finished.status = Some("failed".into());
636                finished.reason = Some(error.reason());
637                finished.duration_ms = Some(elapsed_ms(clock));
638                engine.emit(&finished);
639                return Err(fail(&mut engine, error));
640            }
641        };
642        engine.out.frame(format!(
643            "{} {}: {}",
644            if matches!(status, Done::Skipped(_)) {
645                "skipped"
646            } else {
647                "ok"
648            },
649            step.name,
650            status.line()
651        ));
652        let mut finished = engine.event(EventKind::StepFinished, Some(step.name));
653        finished.status = Some(status.wire().into());
654        finished.detail = Some(status.line());
655        finished.exit_code = Some(0);
656        finished.duration_ms = Some(elapsed_ms(clock));
657        engine.emit(&finished);
658        done.push((step.name.to_owned(), status.wire().to_owned()));
659    }
660    engine.out.result_line(format!(
661        "setup: {} completed against {}",
662        step_count(done.len()),
663        engine.ctx.repo
664    ));
665    for (name, status) in &done {
666        engine.out.result_line(format!("  {status} {name}"));
667    }
668    engine.out.next(&[
669        format!("rk setup check --target {}", engine.ctx.target),
670        "rk guide setup orders what no command performs".to_owned(),
671    ]);
672    engine.finish(0, None);
673    Ok(())
674}
675
676/// A step count rendered with the noun that agrees with it, so no summary
677/// line can regrow a dangling plural.
678fn step_count(count: usize) -> String {
679    format!("{count} {}", if count == 1 { "step" } else { "steps" })
680}
681
682fn elapsed_ms(clock: Instant) -> u64 {
683    u64::try_from(clock.elapsed().as_millis()).unwrap_or(u64::MAX)
684}
685
686/// What one applied step reported.
687enum Done {
688    /// The desired state already held; nothing ran.
689    Satisfied(String),
690    /// The target is ineligible for this step.
691    Skipped(String),
692    /// The script ran and the postcondition was read back.
693    Changed(String, Option<String>),
694    /// A read-only step ran and passed.
695    Passed(String),
696}
697
698impl Done {
699    const fn wire(&self) -> &'static str {
700        match self {
701            Self::Satisfied(_) => "satisfied",
702            Self::Skipped(_) => "skipped",
703            Self::Changed(..) => "applied",
704            Self::Passed(_) => "passed",
705        }
706    }
707
708    fn line(&self) -> String {
709        match self {
710            Self::Satisfied(detail) | Self::Passed(detail) | Self::Skipped(detail) => {
711                detail.clone()
712            }
713            Self::Changed(detail, limitation) => limitation.as_ref().map_or_else(
714                || detail.clone(),
715                |limit| format!("{detail} (limitation: {limit})"),
716            ),
717        }
718    }
719}
720
721/// One step, full lifecycle.
722#[allow(clippy::too_many_lines)]
723fn apply_step(engine: &mut Engine, step: &StepSpec) -> Result<Done, RkError> {
724    // Prerequisites are observed, not remembered: the forge is the
725    // authority on whether an earlier step's state holds.
726    for prereq in step.prereqs {
727        let state = observe_with(engine, prereq)?;
728        if !state.satisfied() {
729            return Err(RkError::refusal(
730                Diagnostic::new(
731                    Reason::PrerequisiteUnmet,
732                    format!(
733                        "{} requires {prereq} first: {}",
734                        step.name,
735                        state_detail(&state)
736                    ),
737                )
738                .expected(format!("{prereq} satisfied before {}", step.name))
739                .action(format!(
740                    "rk setup step {prereq} --target {} --apply",
741                    engine.ctx.target
742                ))
743                .step(step.name),
744            ));
745        }
746    }
747    match step.name {
748        "package-check" => {
749            if engine.ctx.tech.is_none() {
750                return Err(RkError::Usage(
751                    "no version file names a technology; rk binding --list names the bindings"
752                        .into(),
753                ));
754            }
755            let state = observe_with(engine, "package-check")?;
756            match state {
757                StepState::Satisfied { detail, .. } => Ok(Done::Passed(detail)),
758                StepState::Unsatisfied { detail } | StepState::Inapplicable { detail } => {
759                    Err(RkError::subprocess(
760                        Diagnostic::new(
761                            Reason::SubprocessFailed,
762                            format!("package-check failed: {detail}"),
763                        )
764                        .expected(step.proves.to_owned())
765                        .step(step.name),
766                    ))
767                }
768                StepState::Unknown { detail } => Err(RkError::subprocess(
769                    Diagnostic::new(
770                        Reason::SubprocessFailed,
771                        format!("package-check could not run: {detail}"),
772                    )
773                    .step(step.name),
774                )),
775            }
776        }
777        // The step mutates nothing, so apply and check read the same answer
778        // and apply writes nothing at all. An unreadable version is a
779        // refusal, never a pass: the floor exists to stop a protection the
780        // forge cannot honor, and a floor nobody could read proves neither
781        // way.
782        "forge-version" => match observe_with(engine, "forge-version")? {
783            StepState::Satisfied { detail, .. } => Ok(Done::Satisfied(detail)),
784            StepState::Unsatisfied { detail } | StepState::Inapplicable { detail } => {
785                Err(RkError::refusal(
786                    Diagnostic::new(Reason::PrerequisiteUnmet, detail)
787                        .expected(step.proves.to_owned())
788                        .action("upgrade the instance, or host the project on gitlab.com")
789                        .target_state("unchanged")
790                        .step(step.name),
791                ))
792            }
793            StepState::Unknown { detail } => Err(RkError::refusal(
794                Diagnostic::new(Reason::ForgeTemporary, detail)
795                    .expected("a readable forge version")
796                    .action("glab auth login, then rerun")
797                    .target_state("unchanged")
798                    .step(step.name),
799            )),
800        },
801        "branch-reminder" => {
802            use crate::setup::branch_reminder::{HookState, hook_body, hook_path, observe_hook};
803            match observe_hook(&engine.ctx.target) {
804                HookState::Installed => Ok(Done::Satisfied(
805                    "the post-merge reminder hook is installed".into(),
806                )),
807                HookState::Foreign => Err(RkError::refusal(
808                    Diagnostic::new(
809                        Reason::StateDrift,
810                        "a foreign post-merge hook exists; the reminder is never written over it",
811                    )
812                    .expected("no post-merge hook, or one carrying the release-kit marker")
813                    .action(
814                        "merge by hand: guard each call behind its own capability probe inside the existing hook — `rk branches prune --help >/dev/null 2>&1` before `rk branches prune --quiet || :`, and the same pair for `rk worktree prune`",
815                    )
816                    .target_state("unchanged")
817                    .step(step.name),
818                )),
819                HookState::Unreadable(detail) => Err(RkError::refusal(
820                    Diagnostic::new(
821                        Reason::StateDrift,
822                        format!("the post-merge hook cannot be read: {detail}"),
823                    )
824                    .target_state("unchanged")
825                    .step(step.name),
826                )),
827                HookState::Absent | HookState::Drifted => {
828                    let path = hook_path(&engine.ctx.target).map_err(|detail| {
829                        RkError::refusal(
830                            Diagnostic::new(
831                                Reason::PrerequisiteUnmet,
832                                format!("the hooks directory cannot be resolved: {detail}"),
833                            )
834                            .expected("a git repository whose hooks directory git can name")
835                            .step(step.name),
836                        )
837                    })?;
838                    crate::atomic::write(&path, hook_body())?;
839                    #[cfg(unix)]
840                    {
841                        use std::os::unix::fs::PermissionsExt as _;
842                        std::fs::set_permissions(
843                            &path,
844                            std::fs::Permissions::from_mode(0o755),
845                        )?;
846                    }
847                    Ok(Done::Changed(
848                        "wrote the post-merge reminder hook".into(),
849                        None,
850                    ))
851                }
852            }
853        }
854        "single-trunk" => {
855            let guard = {
856                let ctx = clone_ctx(&engine.ctx);
857                let mut runner = |exec: &Exec| engine.exec(exec, false);
858                observe::single_trunk_guard(&ctx, &mut runner)?
859            };
860            // A destructive step fails closed: an ancestry the guard cannot
861            // establish is treated exactly like one it refuted.
862            match &guard {
863                StepState::Satisfied { .. } => {}
864                StepState::Unsatisfied { detail }
865                | StepState::Inapplicable { detail }
866                | StepState::Unknown { detail } => {
867                    return Err(RkError::refusal(
868                        Diagnostic::new(
869                            Reason::DestructiveRefusal,
870                            format!("single-trunk refuses: {detail}"),
871                        )
872                        .expected(
873                            "proof that every candidate branch is absent, or an ancestor of the trunk",
874                        )
875                        .step(step.name),
876                    ));
877                }
878            }
879            run_forge_step(engine, step)
880        }
881        "bot-secrets" => {
882            // Validate before observing: a wrong path or a wrong mode is
883            // the operator's answer either way, and the refusal costs no
884            // forge call. Only GitHub reads a key file; GitLab's credential
885            // is a token, and its step must not fail over a variable it
886            // never consumes.
887            let key = match engine.ctx.forge {
888                // The run's one read: an install-bot observation earlier in
889                // this run already holds the bytes, and this step stores
890                // those very bytes rather than reopening the path.
891                Forge::Github => key_file_for(engine)?.map(|key| key.bytes.clone()),
892                Forge::Gitlab => None,
893            };
894            let provided = match engine.ctx.forge {
895                // Both halves of an App identity, or neither: a run holding
896                // only one of them would store half a credential.
897                Forge::Github => secrets::value_of("RK_BOT_APP_ID").is_some() && key.is_some(),
898                Forge::Gitlab => secrets::value_of("RK_BOT_TOKEN").is_some(),
899            };
900            let state = observe_with(engine, step.name)?;
901            if !provided {
902                if state.satisfied() {
903                    return Ok(Done::Satisfied(state_detail(&state)));
904                }
905                let wanted = match engine.ctx.forge {
906                    Forge::Github => {
907                        "export RK_BOT_APP_ID and RK_BOT_PRIVATE_KEY_FILE, the second naming the .pem; rk forge github carries the walkthrough"
908                    }
909                    Forge::Gitlab => {
910                        "rk setup step install-bot --apply stores the token, or export RK_BOT_TOKEN to rotate one"
911                    }
912                };
913                return Err(RkError::refusal(
914                    Diagnostic::new(
915                        Reason::PrerequisiteUnmet,
916                        "bot-secrets has no credentials to store",
917                    )
918                    .expected("the bot credentials in the environment, the key as a path")
919                    .action(wanted.to_owned())
920                    .step(step.name),
921                ));
922            }
923            if let Some(journal) = &mut engine.journal {
924                for name in SECRET_VARS {
925                    if secrets::value_of(name).is_some() {
926                        journal.record_secret(name, true, "environment");
927                    }
928                }
929                if key.is_some() {
930                    journal.record_secret(secrets::PRIVATE_KEY_FILE, true, "file");
931                }
932            }
933            // The bytes rk validated are the bytes the child receives and
934            // the bytes the redactor holds: one read, one value, so nothing
935            // can be substituted between the check and the forge.
936            let stdin = key;
937            run_forge_step_with(engine, step, stdin, Vec::new())
938        }
939        "protections-check" => {
940            let (outcome, _) = run_script(engine, step)?;
941            if !outcome.success() {
942                return Err(classify_failure(engine, step, &outcome));
943            }
944            // The script is the operator-auditable mirror; the observation
945            // is the authoritative shape check, so the step passes only
946            // when both agree.
947            match observe_with(engine, step.name)? {
948                StepState::Satisfied { detail, limitation } => {
949                    Ok(Done::Passed(limitation.map_or_else(
950                        || detail.clone(),
951                        |limit| format!("{detail} (limitation: {limit})"),
952                    )))
953                }
954                StepState::Unsatisfied { detail } | StepState::Inapplicable { detail } => {
955                    Err(RkError::refusal(
956                        Diagnostic::new(
957                            Reason::StateDrift,
958                            format!("protections-check passed its script and the observation disagrees: {detail}"),
959                        )
960                        .expected(step.proves.to_owned())
961                        .step(step.name),
962                    ))
963                }
964                // An unreadable readback is a retryable outage, not drift,
965                // exactly as the postcondition lifecycle classifies it.
966                StepState::Unknown { detail } => Err(RkError::refusal(
967                    Diagnostic::new(
968                        Reason::ForgeTemporary,
969                        format!(
970                            "protections-check passed its script and the readback could not confirm it: {detail}"
971                        ),
972                    )
973                    .expected(step.proves.to_owned())
974                    .action("check authentication and connectivity, then rerun")
975                    .step(step.name),
976                )),
977            }
978        }
979        // GitHub's grant is the one write the forge offers a command, and
980        // it takes a user credential; everything else here — the pre- and
981        // post-observation, and the installation id the script is handed —
982        // happens as the App itself. GitLab's install-bot needs none of
983        // this and takes the generic lifecycle below.
984        "install-bot" if engine.ctx.forge == Forge::Github => {
985            match observe_with(engine, step.name)? {
986                StepState::Satisfied { detail, .. } => {
987                    return Ok(Done::Satisfied(detail));
988                }
989                StepState::Unsatisfied { .. } | StepState::Inapplicable { .. } => {}
990                StepState::Unknown { detail } => {
991                    return Err(RkError::refusal(
992                        Diagnostic::new(
993                            Reason::ForgeTemporary,
994                            format!("{} cannot observe the current state: {detail}", step.name),
995                        )
996                        .expected("a readable forge answer before anything mutates")
997                        .action("check the App credentials and connectivity, then rerun")
998                        .step(step.name),
999                    ));
1000                }
1001            }
1002            let installation = github_installation_id(engine, step)?;
1003            run_forge_step_with(
1004                engine,
1005                step,
1006                None,
1007                vec![("RK_BOT_INSTALLATION".into(), installation.into())],
1008            )
1009        }
1010        _ => {
1011            if step.mutates == Mutates::Forge {
1012                // The lifecycle applies only on a state it has read: an
1013                // observation that cannot decide fails closed here exactly
1014                // as it does after the write, so no mutation ever rides on
1015                // an unreadable forge answer.
1016                match observe_with(engine, step.name)? {
1017                    StepState::Satisfied { detail, limitation } => {
1018                        let detail = if step.name == "private-vulnerability-reporting" {
1019                            limitation.map_or_else(
1020                                || detail.clone(),
1021                                |limit| format!("{detail} (limitation: {limit})"),
1022                            )
1023                        } else {
1024                            detail
1025                        };
1026                        return Ok(Done::Satisfied(detail));
1027                    }
1028                    StepState::Inapplicable { detail }
1029                        if step.name == "private-vulnerability-reporting" =>
1030                    {
1031                        return Ok(Done::Skipped(detail));
1032                    }
1033                    StepState::Unsatisfied { .. } | StepState::Inapplicable { .. } => {}
1034                    StepState::Unknown { detail } => {
1035                        return Err(RkError::refusal(
1036                            Diagnostic::new(
1037                                Reason::ForgeTemporary,
1038                                format!("{} cannot observe the current state: {detail}", step.name),
1039                            )
1040                            .expected("a readable forge answer before anything mutates")
1041                            .action("check authentication and connectivity, then rerun")
1042                            .step(step.name),
1043                        ));
1044                    }
1045                }
1046            }
1047            run_forge_step(engine, step)
1048        }
1049    }
1050}
1051
1052/// The id of the App's installation on this repository's owner, read as
1053/// the App itself. The grant needs it, and no user credential can read
1054/// it: the observation that just ran answered 404, so the repository
1055/// endpoint that names the id directly has nothing to say yet. The
1056/// account-level installation is a direct read for either account kind —
1057/// the user endpoint answers for a person, the organization endpoint for
1058/// an organization — so nothing here lists or paginates.
1059fn github_installation_id(engine: &mut Engine, step: &StepSpec) -> Result<String, RkError> {
1060    let refuse = |message: String, action: &str| {
1061        RkError::refusal(
1062            Diagnostic::new(Reason::PrerequisiteUnmet, message)
1063                .expected("the App installed on the repository's owner")
1064                .action(action.to_owned())
1065                .step(step.name),
1066        )
1067    };
1068    let jwt = match app_jwt_for(engine)? {
1069        Ok(jwt) => jwt,
1070        Err(detail) => {
1071            return Err(refuse(
1072                format!("install-bot has no App token: {detail}"),
1073                app_jwt::REMEDIATION,
1074            ));
1075        }
1076    };
1077    let owner = engine
1078        .ctx
1079        .repo
1080        .split('/')
1081        .next()
1082        .unwrap_or_default()
1083        .to_owned();
1084    let ctx = clone_ctx(&engine.ctx);
1085    for path in [
1086        format!("users/{owner}/installation"),
1087        format!("orgs/{owner}/installation"),
1088    ] {
1089        match app_jwt::api_get(&ctx, &jwt, &path) {
1090            AppApi::Ok(body) => {
1091                return body["id"].as_i64().map(|id| id.to_string()).ok_or_else(|| {
1092                    refuse(
1093                        format!("the forge answered {path} without an installation id"),
1094                        "check RK_BOT_APP_ID and the key file name the same App",
1095                    )
1096                });
1097            }
1098            AppApi::Missing => {}
1099            AppApi::Refused(detail) => {
1100                return Err(refuse(
1101                    detail,
1102                    "check RK_BOT_APP_ID and the key file name the same App",
1103                ));
1104            }
1105            AppApi::Failed(detail) => {
1106                return Err(RkError::refusal(
1107                    Diagnostic::new(
1108                        Reason::ForgeTemporary,
1109                        format!("install-bot cannot read the App's installation: {detail}"),
1110                    )
1111                    .action("check connectivity, then rerun")
1112                    .step(step.name),
1113                ));
1114            }
1115        }
1116    }
1117    Err(refuse(
1118        format!("the App has no installation on {owner}"),
1119        "install the App on the account first; the setup guide's step 5 walks it",
1120    ))
1121}
1122
1123/// Materialize, spawn, classify, and verify one forge-mutating step.
1124fn run_forge_step(engine: &mut Engine, step: &StepSpec) -> Result<Done, RkError> {
1125    run_forge_step_with(engine, step, None, Vec::new())
1126}
1127
1128/// The same, with bytes written to the step's standard input and values
1129/// `rk` derived added to its environment.
1130fn run_forge_step_with(
1131    engine: &mut Engine,
1132    step: &StepSpec,
1133    stdin: Option<Zeroizing<Vec<u8>>>,
1134    extra_env: Vec<(OsString, OsString)>,
1135) -> Result<Done, RkError> {
1136    let (outcome, _) = run_script_with(engine, step, stdin, extra_env)?;
1137    if !outcome.success() {
1138        return Err(classify_failure(engine, step, &outcome));
1139    }
1140    let state = observe_with(engine, step.name)?;
1141    match state {
1142        StepState::Satisfied { detail, limitation } => Ok(Done::Changed(detail, limitation)),
1143        StepState::Inapplicable { detail } if step.name == "private-vulnerability-reporting" => {
1144            Ok(Done::Skipped(detail))
1145        }
1146        StepState::Unsatisfied { detail } | StepState::Inapplicable { detail } => {
1147            Err(RkError::refusal(
1148                Diagnostic::new(
1149                    Reason::StateDrift,
1150                    format!(
1151                        "{} ran and its postcondition does not hold: {detail}",
1152                        step.name
1153                    ),
1154                )
1155                .expected(step.proves.to_owned())
1156                .step(step.name),
1157            ))
1158        }
1159        // The lifecycle ends with a proven postcondition; a readback that
1160        // cannot run leaves the step unproven, and an unproven apply is a
1161        // failure a retry can cure, never a success.
1162        StepState::Unknown { detail } => Err(RkError::refusal(
1163            Diagnostic::new(
1164                Reason::ForgeTemporary,
1165                format!(
1166                    "{} ran and the readback could not confirm it: {detail}",
1167                    step.name
1168                ),
1169            )
1170            .expected(step.proves.to_owned())
1171            .action(format!(
1172                "rk setup step {} --target {} --apply re-asserts and re-proves it",
1173                step.name, engine.ctx.target
1174            ))
1175            .step(step.name),
1176        )),
1177    }
1178}
1179
1180/// Observe one step through the engine's executor.
1181///
1182/// `install-bot` on GitHub is the one observation that authenticates as
1183/// the App itself, so it routes through [`app_jwt_for`] here — where the
1184/// engine can mint once and register the redaction needles — rather than
1185/// through the credential-free name dispatch in [`observe::observe`].
1186fn observe_with(engine: &mut Engine, step: &str) -> Result<StepState, RkError> {
1187    if step == "install-bot" && engine.ctx.forge == Forge::Github {
1188        let jwt = match app_jwt_for(engine)? {
1189            Ok(jwt) => jwt,
1190            Err(detail) => return Ok(StepState::Unknown { detail }),
1191        };
1192        return Ok(observe::github_install_bot(&engine.ctx, &jwt));
1193    }
1194    let ctx = clone_ctx(&engine.ctx);
1195    let mut runner = |exec: &Exec| engine.exec(exec, false);
1196    observe::observe(&ctx, step, &mut runner)
1197}
1198
1199/// The run's validated key file, read exactly once per run: the first
1200/// consumer resolves it and every later one reuses the same bytes, so the
1201/// file that authenticated the App is the file `bot-secrets` stores, and
1202/// no replacement between steps can split the two. The bytes become a
1203/// redaction needle the moment they are read.
1204fn key_file_for(engine: &mut Engine) -> Result<Option<&secrets::KeyFile>, RkError> {
1205    if engine.key.is_none() {
1206        engine.key = secrets::resolve_key_file(&engine.ctx.target)?;
1207        if let Some(key) = &engine.key {
1208            engine.secrets.push(key.bytes.clone());
1209        }
1210    }
1211    Ok(engine.key.as_ref())
1212}
1213
1214/// The run's App JWT, minted at most once: the key comes from the run's
1215/// one read, and the minted token and its signature segment become
1216/// redaction needles before anything else spawns. Every install-bot
1217/// observation and the grant's installation-id discovery reuse the one
1218/// token, whose nine-minute life covers a run's contiguous step easily.
1219///
1220/// The inner value is `Err` with a one-line detail where no token can
1221/// exist — absent exports, or a signer that failed — which an observation
1222/// reports as `unknown` and an apply turns into a refusal.
1223fn app_jwt_for(engine: &mut Engine) -> Result<Result<String, String>, RkError> {
1224    if let Some(jwt) = &engine.app_jwt {
1225        return Ok(Ok(jwt.clone()));
1226    }
1227    let app_id = app_jwt::app_id(engine.ctx.bot_app_id())?;
1228    let key_bytes = key_file_for(engine)?.map(|key| key.bytes.clone());
1229    let (Some(app_id), Some(key_bytes)) = (app_id, key_bytes) else {
1230        return Ok(Err(format!(
1231            "the installation is readable only to the App itself; {}",
1232            app_jwt::REMEDIATION
1233        )));
1234    };
1235    let credentials = app_jwt::AppCredentials { app_id, key_bytes };
1236    let ctx = clone_ctx(&engine.ctx);
1237    Ok(match app_jwt::mint(&ctx, &credentials) {
1238        Ok(jwt) => {
1239            engine
1240                .secrets
1241                .push(Zeroizing::new(jwt.clone().into_bytes()));
1242            if let Some(signature) = jwt.rsplit('.').next() {
1243                engine
1244                    .secrets
1245                    .push(Zeroizing::new(signature.as_bytes().to_vec()));
1246            }
1247            engine.app_jwt = Some(jwt.clone());
1248            Ok(jwt)
1249        }
1250        Err(detail) => Err(detail),
1251    })
1252}
1253
1254fn state_detail(state: &StepState) -> String {
1255    match state {
1256        StepState::Satisfied { detail, .. }
1257        | StepState::Unsatisfied { detail }
1258        | StepState::Inapplicable { detail }
1259        | StepState::Unknown { detail } => detail.clone(),
1260    }
1261}
1262
1263/// Materialize the step's script into the run's private directory, prove
1264/// the written bytes by digest, and spawn it through the interpreter.
1265fn run_script(engine: &mut Engine, step: &StepSpec) -> Result<(Outcome, PathBuf), RkError> {
1266    run_script_with(engine, step, None, Vec::new())
1267}
1268
1269/// The same, with bytes written to the script's standard input.
1270///
1271/// A credential travels this way and no other: `rk` reads it, validates it,
1272/// and hands the child the bytes it validated, so nothing between the check
1273/// and the forge can substitute a different file.
1274fn run_script_with(
1275    engine: &mut Engine,
1276    step: &StepSpec,
1277    stdin: Option<Zeroizing<Vec<u8>>>,
1278    extra_env: Vec<(OsString, OsString)>,
1279) -> Result<(Outcome, PathBuf), RkError> {
1280    let rel = format!("{}/{}", engine.ctx.forge.as_str(), step.name);
1281    let bytes = embedded::SETUP
1282        .get_file(&rel)
1283        .map(include_dir::File::contents)
1284        .ok_or_else(|| RkError::Other(anyhow::anyhow!("no embedded script at setup/{rel}")))?;
1285    let journal = engine
1286        .journal
1287        .as_mut()
1288        .ok_or_else(|| RkError::Other(anyhow::anyhow!("an apply always has a journal")))?;
1289    let dir = journal.scripts_dir().join(engine.ctx.forge.as_str());
1290    fs::create_dir_all(&dir)?;
1291    restrict(&dir, 0o700);
1292    let path = dir.join(step.name);
1293    fs::write(&path, bytes)?;
1294    restrict(&path, 0o600);
1295    let written = fs::read(&path)?;
1296    let digest = Digest::of(&written);
1297    if digest != Digest::of(bytes) {
1298        return Err(RkError::Other(anyhow::anyhow!(
1299            "the materialized script at {} differs from the embedded bytes",
1300            path.display()
1301        )));
1302    }
1303    journal.record_script(format!("scripts/{rel}"), digest.to_string());
1304    let mut env = engine.ctx.child_env(step.name);
1305    env.extend(extra_env);
1306    let exec = Exec {
1307        program: crate::probes::sh_bin(),
1308        args: vec![path.clone().into_os_string()],
1309        env,
1310        cwd: engine.ctx.target.as_std_path().to_path_buf(),
1311        stdin,
1312    };
1313    let outcome = engine.exec(&exec, true)?;
1314    Ok((outcome, path))
1315}
1316
1317/// Honest classification: `gh` documents exit 4 as authentication required;
1318/// beyond that only an HTTP status in the response says more, and a step
1319/// that fails for a reason nothing establishes stays `subprocess-failed`
1320/// with its own stderr surfaced verbatim.
1321fn classify_failure(engine: &Engine, step: &StepSpec, outcome: &Outcome) -> RkError {
1322    let stderr = String::from_utf8_lossy(&outcome.stderr);
1323    // A signalled child usually writes nothing before it dies, and
1324    // "no output" would blame the forge for a kill that came from
1325    // outside it. The adapter already resolved 128+N; say which.
1326    let last = if outcome.exit_code >= 128 {
1327        format!("killed by signal {}", outcome.exit_code - 128)
1328    } else {
1329        stderr
1330            .lines()
1331            .rev()
1332            .find(|line| !line.trim().is_empty())
1333            .unwrap_or("no output")
1334            .to_owned()
1335    };
1336    let reason = if (engine.ctx.forge == Forge::Github && outcome.exit_code == 4)
1337        || stderr.contains("HTTP 401")
1338    {
1339        Reason::ForgeAuthentication
1340    } else if stderr.contains("HTTP 403") {
1341        Reason::ForgePermission
1342    } else if stderr.contains("HTTP 429") || stderr.contains("rate limit") {
1343        Reason::ForgeRateLimit
1344    } else {
1345        Reason::SubprocessFailed
1346    };
1347    let diagnostic = Diagnostic::new(reason, format!("the forge refused '{}': {last}", step.name))
1348        .expected(step.proves.to_owned())
1349        .action(format!(
1350            "rk setup step {} --target {} --apply",
1351            step.name, engine.ctx.target
1352        ))
1353        .step(step.name);
1354    let diagnostic = match reason {
1355        Reason::ForgePermission => diagnostic.expected(format!(
1356            "repository administration write on {} for the authenticated account",
1357            engine.ctx.repo
1358        )),
1359        _ => diagnostic,
1360    };
1361    match reason {
1362        Reason::SubprocessFailed => RkError::subprocess(diagnostic),
1363        _ => RkError::refusal(diagnostic),
1364    }
1365}
1366
1367/// Fold the run's progress into the failure, so the diagnostic answers
1368/// what state the target is in.
1369fn attach_progress(
1370    error: RkError,
1371    done: &[(String, String)],
1372    failed: &StepSpec,
1373    steps: &[&StepSpec],
1374) -> RkError {
1375    let remaining = steps.len().saturating_sub(done.len() + 1);
1376    let state = format!(
1377        "{} completed; {} failed; {remaining} not attempted",
1378        step_count(done.len()),
1379        failed.name
1380    );
1381    match error {
1382        RkError::Refusal(mut diagnostic) => {
1383            diagnostic.target_state.get_or_insert(state);
1384            RkError::Refusal(diagnostic)
1385        }
1386        RkError::Subprocess(mut diagnostic) => {
1387            diagnostic.target_state.get_or_insert(state);
1388            RkError::Subprocess(diagnostic)
1389        }
1390        other => other,
1391    }
1392}
1393
1394/// `rk setup check`: observe and verify every step, report per step, and
1395/// judge at the end. The mutating half is unreachable from this path: it
1396/// calls only the observe functions.
1397fn check(out: Output, ctx: Ctx) -> Result<(), RkError> {
1398    let mut engine = Engine::open(out, ctx, "setup check", false)?;
1399    let mut unsatisfied = 0usize;
1400    let mut unverifiable = 0usize;
1401    for step in &STEPS {
1402        let clock = Instant::now();
1403        // A step the target declared it does not run is stated and judged
1404        // by nothing: no forge call, no verdict, and no weight in the exit
1405        // code. The reason travels with it, so a reader can tell a chosen
1406        // subset from an incomplete setup.
1407        if let Some(reason) = engine.ctx.excluded(step.name).map(str::to_owned) {
1408            engine
1409                .out
1410                .result_line(format!("excluded {} — {reason}", step.name));
1411            let mut finished = engine.event(EventKind::StepFinished, Some(step.name));
1412            finished.status = Some("excluded".into());
1413            finished.detail = Some(reason);
1414            finished.duration_ms = Some(elapsed_ms(clock));
1415            engine.emit(&finished);
1416            continue;
1417        }
1418        let state = observe_with(&mut engine, step.name)?;
1419        let (label, wire) = match &state {
1420            StepState::Satisfied { .. } => ("ok", "satisfied"),
1421            // An optional step whose condition does not hold is stated, not
1422            // judged: nothing is wrong and nothing was skipped silently.
1423            StepState::Inapplicable { .. } => ("skipped", "skipped"),
1424            StepState::Unsatisfied { .. } => {
1425                unsatisfied += 1;
1426                ("unsatisfied", "unsatisfied")
1427            }
1428            // A step the check cannot verify has not passed: an unreadable
1429            // forge answer must never read as a clean setup.
1430            StepState::Unknown { .. } => {
1431                unverifiable += 1;
1432                ("unknown", "unknown")
1433            }
1434        };
1435        let mut line = format!("{label} {} — {}", step.name, state_detail(&state));
1436        if let StepState::Satisfied {
1437            limitation: Some(limit),
1438            ..
1439        } = &state
1440        {
1441            use std::fmt::Write as _;
1442            let _ = write!(line, " (limitation: {limit})");
1443        }
1444        engine.out.result_line(line);
1445        let mut finished = engine.event(EventKind::StepFinished, Some(step.name));
1446        finished.status = Some(wire.into());
1447        finished.detail = Some(state_detail(&state));
1448        finished.duration_ms = Some(elapsed_ms(clock));
1449        engine.emit(&finished);
1450    }
1451    if engine.ctx.excluded_count() > 0 {
1452        let excluded = step_count(engine.ctx.excluded_count());
1453        engine.out.result_line(format!(
1454            "{excluded} excluded by {}; this check judges the rest",
1455            crate::config::CONFIG_PATH
1456        ));
1457    }
1458    if unsatisfied > 0 || unverifiable > 0 {
1459        let error = RkError::check_failed(
1460            Diagnostic::new(
1461                Reason::StateDrift,
1462                format!(
1463                    "{} {} not satisfied and {unverifiable} could not be verified",
1464                    step_count(unsatisfied),
1465                    if unsatisfied == 1 { "is" } else { "are" }
1466                ),
1467            )
1468            .expected("every step's proof column to hold and to be readable")
1469            .action(format!(
1470                "rk setup --target {} --apply re-asserts them",
1471                engine.ctx.target
1472            )),
1473        );
1474        return Err(fail(&mut engine, error));
1475    }
1476    engine
1477        .out
1478        .next(&["rk guide release orders the first release".to_owned()]);
1479    engine.finish(0, None);
1480    Ok(())
1481}
1482
1483/// Restrict a materialized path's mode: data, not an executable — nothing
1484/// ever executes a script directly, so no mode is load-bearing.
1485fn restrict(path: &std::path::Path, mode: u32) {
1486    #[cfg(unix)]
1487    {
1488        use std::os::unix::fs::PermissionsExt as _;
1489        let _ = fs::set_permissions(path, fs::Permissions::from_mode(mode));
1490    }
1491    #[cfg(not(unix))]
1492    let _ = (path, mode);
1493}
1494
1495/// A POSIX shell must spawn before anything else does; every step runs
1496/// through it.
1497fn guard_sh() -> Result<(), RkError> {
1498    let ok = std::process::Command::new(crate::probes::sh_bin())
1499        .args(["-c", "exit 0"])
1500        .status()
1501        .is_ok_and(|status| status.success());
1502    if ok {
1503        Ok(())
1504    } else {
1505        Err(RkError::refusal(
1506            Diagnostic::new(Reason::PrerequisiteUnmet, "no POSIX sh runs on this host")
1507                .expected("a working sh on PATH; every step spawns through it")
1508                .action("install a POSIX shell, then rerun")
1509                .target_state("nothing was run and nothing changed"),
1510        ))
1511    }
1512}
1513
1514#[cfg(test)]
1515mod tests {
1516    /// Every summary line reports its count through one helper, so none of
1517    /// them can regrow a dangling plural.
1518    #[test]
1519    fn a_step_count_carries_a_noun_that_agrees_with_it() {
1520        assert_eq!(super::step_count(0), "0 steps");
1521        assert_eq!(super::step_count(1), "1 step");
1522        assert_eq!(super::step_count(2), "2 steps");
1523    }
1524}