release-kit 0.8.7

A canonical release workflow: a technology-agnostic method, per-technology bindings, and the rk CLI that lands and serves them.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! The committed setup proof: what the last complete observation proved.
//!
//! Four records answer four different questions, and none substitutes for
//! another. The target configuration is the desired state, the landing
//! record is what landed, the host journal is what one run did on one
//! machine, and `rk setup check` is what the forge answers now. This file
//! is the fifth: a portable, non-secret statement that a run able to see
//! every step saw each one hold, against a named setup contract.
//!
//! The record never becomes current truth. A reader judges whether it
//! still describes this target's contract, and a live check that reads
//! the forge still decides what holds now.

use std::collections::BTreeMap;

use camino::Utf8Path;
use serde::{Deserialize, Serialize};

use super::context::Ctx;
use super::report::{Observed, Report, stance};
use super::steps::STEPS;
use crate::digest::Digest;

/// Where the proof lives, relative to the target.
pub const PROOF_PATH: &str = ".release-kit/setup-proof.json";

/// The record's schema.
pub const SCHEMA: &str = "rk.setup-proof/1";

/// The schema family, so a newer record reads as newer rather than foreign.
const SCHEMA_FAMILY: &str = "rk.setup-proof/";

/// The setup contract a proof is judged against: the answers that decide
/// which steps run and what each one asserts.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Subject {
    /// The resolved target answers, by name, each in its canonical text.
    pub target: BTreeMap<String, String>,
    /// Every step's contract, in step-table order.
    pub steps: Vec<SubjectStep>,
}

/// One step's contract.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SubjectStep {
    /// The step.
    pub name: String,
    /// What the step proves.
    pub proves: String,
    /// The steps that must hold before this one applies.
    pub prerequisites: Vec<String>,
    /// Where the step stands at this target.
    pub stance: String,
    /// Why, where it does not apply or the target excludes it.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub reason: String,
}

/// One step's normalized result.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ProvenStep {
    /// The step.
    pub name: String,
    /// `satisfied`, `satisfied-with-limitation`, `skipped`, `excluded`,
    /// `not-applicable`, or `redundant`.
    pub state: String,
    /// The stable limitation, where the forge enforces less than the step
    /// claims.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limitation: Option<String>,
}

/// The committed record.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Proof {
    /// Always [`SCHEMA`].
    pub schema: String,
    /// The binary that ran the observation: provenance, not a judgment.
    pub rk_version: String,
    /// When the observation completed, UTC.
    pub verified_at: String,
    /// The contract the observation proved.
    pub subject: Subject,
    /// The SHA-256 of the subject's canonical bytes.
    pub subject_digest: String,
    /// Every step's normalized result, in step-table order.
    pub steps: Vec<ProvenStep>,
}

/// Where a committed proof stands against this target now.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Standing {
    /// No proof is committed.
    Absent,
    /// The proof describes this target's current contract.
    Compatible(Proof),
    /// The proof is whole, but the contract moved, by the fields named.
    Stale {
        /// The record.
        proof: Proof,
        /// Each changed field, by name.
        differences: Vec<String>,
    },
    /// The file is not a proof this binary can read.
    Invalid {
        /// `unreadable`, `malformed`, `unsupported-schema`,
        /// `future-schema`, or `digest-mismatch`.
        reason: &'static str,
        /// What was found, one line.
        detail: String,
    },
}

impl Standing {
    /// The word a report leads with.
    #[must_use]
    pub const fn word(&self) -> &'static str {
        match self {
            Self::Absent => "absent",
            Self::Compatible(_) => "compatible",
            Self::Stale { .. } => "stale",
            Self::Invalid { .. } => "invalid",
        }
    }
}

/// The contract this target states now.
#[must_use]
pub fn subject(ctx: &Ctx) -> Subject {
    let steps = STEPS
        .iter()
        .map(|step| {
            let stance = stance(ctx, step);
            SubjectStep {
                name: step.name.to_owned(),
                proves: step.proves.to_owned(),
                prerequisites: step.prereqs.iter().map(|&name| name.to_owned()).collect(),
                stance: stance.word().to_owned(),
                reason: stance.detail(),
            }
        })
        .collect();
    Subject {
        target: ctx.proof_fields(),
        steps,
    }
}

/// The subject's digest over its canonical bytes: compact JSON, whose maps
/// are ordered by key and whose lists keep step-table order.
#[must_use]
pub fn digest(subject: &Subject) -> String {
    let bytes = serde_json::to_vec(subject).unwrap_or_default();
    Digest::of(&bytes).to_string()
}

/// A proof of `report`, or `None` where the report is not a complete
/// observation.
///
/// Only a normalized word and a stable limitation enter the record per
/// step: a forge's own answer, a process's output, and every local
/// coordinate stay out.
///
/// SATISFIES setup-proof:a-checkpoint-records-only-a-complete-observation
/// SATISFIES setup-proof:the-proof-carries-no-secret-or-machine-coordinate
#[must_use]
pub fn of(ctx: &Ctx, report: &Report, verified_at: String) -> Option<Proof> {
    if !report.checkpointable() {
        return None;
    }
    let steps = report
        .rows
        .iter()
        .map(|row| {
            let (state, limitation) = match &row.observed {
                None => (row.stance.word().to_owned(), None),
                Some(Observed::Satisfied { limitation }) => (
                    Observed::Satisfied {
                        limitation: limitation.clone(),
                    }
                    .wire()
                    .to_owned(),
                    limitation.clone(),
                ),
                Some(other) => (other.wire().to_owned(), None),
            };
            ProvenStep {
                name: row.name.to_owned(),
                state,
                limitation,
            }
        })
        .collect();
    let subject = subject(ctx);
    Some(Proof {
        schema: SCHEMA.to_owned(),
        rk_version: env!("CARGO_PKG_VERSION").to_owned(),
        verified_at,
        subject_digest: digest(&subject),
        subject,
        steps,
    })
}

/// The record's bytes: pretty JSON with one trailing newline.
#[must_use]
pub fn render(proof: &Proof) -> String {
    let mut text = serde_json::to_string_pretty(proof).unwrap_or_default();
    text.push('\n');
    text
}

/// Write the proof in one rename, so an interrupted write leaves the
/// previous record whole.
///
/// # Errors
///
/// The underlying I/O failure.
pub fn write(target: &Utf8Path, proof: &Proof) -> std::io::Result<()> {
    let path = target.join(PROOF_PATH);
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    crate::atomic::write(path.as_std_path(), render(proof).as_bytes())
}

/// Read the committed proof and judge it against `current`, offline.
///
/// SATISFIES setup-proof:the-status-reads-the-proof-offline
#[must_use]
pub fn judge(target: &Utf8Path, current: &Subject) -> Standing {
    let path = target.join(PROOF_PATH);
    let text = match std::fs::read_to_string(&path) {
        Ok(text) => text,
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Standing::Absent,
        Err(err) => {
            return Standing::Invalid {
                reason: "unreadable",
                detail: format!("{PROOF_PATH} is unreadable: {err}"),
            };
        }
    };
    let proof = match parse(&text) {
        Ok(proof) => proof,
        Err((reason, detail)) => return Standing::Invalid { reason, detail },
    };
    let differences = differences(&proof.subject, current);
    if differences.is_empty() {
        Standing::Compatible(proof)
    } else {
        Standing::Stale { proof, differences }
    }
}

/// Parse and verify one record: its schema first, then its shape, then
/// that its subject is the one its digest names.
fn parse(text: &str) -> Result<Proof, (&'static str, String)> {
    /// The one field read before the schema is known.
    #[derive(Deserialize)]
    struct Envelope {
        schema: String,
    }
    let malformed = |err: serde_json::Error| {
        (
            "malformed",
            format!("{PROOF_PATH} is not a setup proof: {err}"),
        )
    };
    let envelope: Envelope = serde_json::from_str(text).map_err(malformed)?;
    if envelope.schema != SCHEMA {
        let newer = envelope
            .schema
            .strip_prefix(SCHEMA_FAMILY)
            .and_then(|version| version.parse::<u32>().ok())
            .is_some_and(|version| version > 1);
        return Err(if newer {
            (
                "future-schema",
                format!(
                    "{PROOF_PATH} is {}, newer than the {SCHEMA} this binary reads; a newer rk reads it",
                    envelope.schema
                ),
            )
        } else {
            (
                "unsupported-schema",
                format!(
                    "{PROOF_PATH} declares {}, and this binary reads {SCHEMA}",
                    envelope.schema
                ),
            )
        });
    }
    let proof: Proof = serde_json::from_str(text).map_err(malformed)?;
    if digest(&proof.subject) != proof.subject_digest {
        return Err((
            "digest-mismatch",
            format!(
                "{PROOF_PATH} names a subject digest its own subject does not produce, so the record was edited after it was written"
            ),
        ));
    }
    Ok(proof)
}

/// Every field where the recorded contract and the current one differ,
/// by name: `target.<answer>`, `step <name> <part>`, or a step that one
/// side lacks.
#[must_use]
pub fn differences(recorded: &Subject, current: &Subject) -> Vec<String> {
    let mut found = Vec::new();
    let keys: std::collections::BTreeSet<&String> = recorded
        .target
        .keys()
        .chain(current.target.keys())
        .collect();
    for key in keys {
        if recorded.target.get(key) != current.target.get(key) {
            found.push(format!("target.{key}"));
        }
    }
    for step in &recorded.steps {
        match current.steps.iter().find(|now| now.name == step.name) {
            None => found.push(format!("step {} is no longer in the step table", step.name)),
            Some(now) => {
                for (part, changed) in [
                    ("proves", step.proves != now.proves),
                    ("prerequisites", step.prerequisites != now.prerequisites),
                    (
                        "stance",
                        step.stance != now.stance || step.reason != now.reason,
                    ),
                ] {
                    if changed {
                        found.push(format!("step {} {part}", step.name));
                    }
                }
            }
        }
    }
    for now in &current.steps {
        if !recorded.steps.iter().any(|step| step.name == now.name) {
            found.push(format!("step {} is new", now.name));
        }
    }
    found
}

/// The `rk.setup-status/1` document.
#[derive(Debug, Serialize)]
pub struct StatusDocument {
    /// Always `rk.setup-status/1`.
    pub schema: &'static str,
    /// `absent`, `compatible`, `stale`, or `invalid`.
    pub state: &'static str,
    /// The record's provenance, where one was read.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub checkpoint: Option<Checkpoint>,
    /// Each changed contract field, where the proof is stale.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub differences: Vec<String>,
    /// Each step's recorded result, where one was read.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub steps: Vec<ProvenStep>,
    /// Why the file is not readable as a proof.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub invalid: Option<InvalidProof>,
}

/// A record's provenance.
#[derive(Debug, Serialize)]
pub struct Checkpoint {
    /// The binary that proved it.
    pub rk_version: String,
    /// When.
    pub verified_at: String,
    /// The subject it proved.
    pub subject_digest: String,
}

/// Why a file is not a readable proof.
#[derive(Debug, Serialize)]
pub struct InvalidProof {
    /// The closed reason word.
    pub reason: &'static str,
    /// What was found.
    pub detail: String,
}

impl StatusDocument {
    /// The document for one standing.
    #[must_use]
    pub fn of(standing: &Standing) -> Self {
        let mut document = Self {
            schema: "rk.setup-status/1",
            state: standing.word(),
            checkpoint: None,
            differences: Vec::new(),
            steps: Vec::new(),
            invalid: None,
        };
        let proof = match standing {
            Standing::Absent => None,
            Standing::Compatible(proof) => Some(proof),
            Standing::Stale { proof, differences } => {
                document.differences.clone_from(differences);
                Some(proof)
            }
            Standing::Invalid { reason, detail } => {
                document.invalid = Some(InvalidProof {
                    reason,
                    detail: detail.clone(),
                });
                None
            }
        };
        if let Some(proof) = proof {
            document.checkpoint = Some(Checkpoint {
                rk_version: proof.rk_version.clone(),
                verified_at: proof.verified_at.clone(),
                subject_digest: proof.subject_digest.clone(),
            });
            document.steps.clone_from(&proof.steps);
        }
        document
    }
}

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

    fn sample_subject() -> Subject {
        Subject {
            target: BTreeMap::from([
                ("forge".to_owned(), "github".to_owned()),
                ("repo".to_owned(), "acme/widget".to_owned()),
            ]),
            steps: vec![SubjectStep {
                name: "default-branch".to_owned(),
                proves: "the trunk is the default branch".to_owned(),
                prerequisites: Vec::new(),
                stance: "applicable".to_owned(),
                reason: String::new(),
            }],
        }
    }

    fn sample() -> Proof {
        let subject = sample_subject();
        Proof {
            schema: SCHEMA.to_owned(),
            rk_version: "0.0.0".to_owned(),
            verified_at: "2026-09-27T00:00:00Z".to_owned(),
            subject_digest: digest(&subject),
            subject,
            steps: vec![
                ProvenStep {
                    name: "default-branch".to_owned(),
                    state: "satisfied".to_owned(),
                    limitation: None,
                },
                ProvenStep {
                    name: "auto-merge".to_owned(),
                    state: "satisfied-with-limitation".to_owned(),
                    limitation: Some("weaker".to_owned()),
                },
            ],
        }
    }

    /// The record's shape, held by snapshot: a renamed field is a schema
    /// bump, never a silent parser break.
    ///
    /// SATISFIES distribution:machine-output-declares-its-schema
    #[test]
    fn the_setup_proof_shape_is_held() {
        let text = serde_json::to_string(&sample()).expect("serializes");
        assert_eq!(
            text,
            format!(
                r#"{{"schema":"rk.setup-proof/1","rk_version":"0.0.0","verified_at":"2026-09-27T00:00:00Z","subject":{{"target":{{"forge":"github","repo":"acme/widget"}},"steps":[{{"name":"default-branch","proves":"the trunk is the default branch","prerequisites":[],"stance":"applicable"}}]}},"subject_digest":"{}","steps":[{{"name":"default-branch","state":"satisfied"}},{{"name":"auto-merge","state":"satisfied-with-limitation","limitation":"weaker"}}]}}"#,
                digest(&sample_subject())
            )
        );
    }

    /// The status document's shape, held by snapshot in each state.
    ///
    /// SATISFIES distribution:machine-output-declares-its-schema
    #[test]
    fn the_setup_status_shape_is_held() {
        let absent = serde_json::to_string(&StatusDocument::of(&Standing::Absent)).expect("ok");
        assert_eq!(absent, r#"{"schema":"rk.setup-status/1","state":"absent"}"#);
        let stale = serde_json::to_string(&StatusDocument::of(&Standing::Stale {
            proof: sample(),
            differences: vec!["target.repo".to_owned()],
        }))
        .expect("ok");
        assert_eq!(
            stale,
            format!(
                r#"{{"schema":"rk.setup-status/1","state":"stale","checkpoint":{{"rk_version":"0.0.0","verified_at":"2026-09-27T00:00:00Z","subject_digest":"{}"}},"differences":["target.repo"],"steps":[{{"name":"default-branch","state":"satisfied"}},{{"name":"auto-merge","state":"satisfied-with-limitation","limitation":"weaker"}}]}}"#,
                digest(&sample_subject())
            )
        );
        let invalid = serde_json::to_string(&StatusDocument::of(&Standing::Invalid {
            reason: "malformed",
            detail: "not json".to_owned(),
        }))
        .expect("ok");
        assert_eq!(
            invalid,
            r#"{"schema":"rk.setup-status/1","state":"invalid","invalid":{"reason":"malformed","detail":"not json"}}"#
        );
    }

    /// Each way a file fails to be a proof reads as its own reason.
    #[test]
    fn each_unreadable_proof_names_its_own_reason() {
        let reason = |text: &str| match parse(text) {
            Err((reason, _)) => reason,
            Ok(proof) => panic!("{proof:?}"),
        };
        assert_eq!(reason("not json"), "malformed");
        assert_eq!(reason(r#"{"schema":"rk.setup-proof/1"}"#), "malformed");
        assert_eq!(reason(r#"{"schema":"rk.setup-proof/2"}"#), "future-schema");
        assert_eq!(reason(r#"{"schema":"rk.status/12"}"#), "unsupported-schema");
        let mut edited = sample();
        edited
            .subject
            .target
            .insert("repo".into(), "acme/other".into());
        assert_eq!(reason(&render(&edited)), "digest-mismatch");
        assert_eq!(parse(&render(&sample())), Ok(sample()));
    }

    /// A changed contract names what changed; an equal one names nothing.
    #[test]
    fn a_changed_contract_names_each_field() {
        let recorded = sample_subject();
        assert!(differences(&recorded, &recorded).is_empty());
        let mut current = recorded.clone();
        current.target.insert("repo".into(), "acme/other".into());
        current.steps[0].proves = "something else".into();
        current.steps[0].stance = "excluded".into();
        assert_eq!(
            differences(&recorded, &current),
            [
                "target.repo",
                "step default-branch proves",
                "step default-branch stance"
            ]
        );
        current.steps.clear();
        assert!(
            differences(&recorded, &current)
                .contains(&"step default-branch is no longer in the step table".to_owned())
        );
    }
}