ossctl_core/protocol/plan.rs
1//! Public wire DTO for the sealed, content-addressed release plan
2//! (`ossctl release plan` — ADR-0002 §3).
3//!
4//! A [`ReleasePlan`] is the read-only pre-image a human approves: the ordered
5//! concrete target set a cut would publish, the invariant phase sequence the
6//! coordinator will drive, the git `HEAD` it was sealed against, the chosen
7//! release version, and the content-addressed [`ReleasePlan::plan_id`]. `release
8//! cut --plan <plan_id>` re-derives the plan from *current* repo state and
9//! refuses (`plan_stale`) if the id no longer matches — so a commit, a manifest
10//! rename, a schema bump, or a different chosen version between approval and
11//! execution aborts rather than silently publishing something else.
12//!
13//! Consumers read this document under the CLI's canonical `data` envelope:
14//! `{schema_version, data: <this shape>, warnings}` — the same envelope every
15//! `ossctl --json` command shares (`crate::SCHEMA_VERSION` versions that wire
16//! envelope). Like `facts` and `audit`, the plan is *derived*, never authored,
17//! so it has no document version of its own; the envelope's `schema_version` is
18//! the wire version consumers gate on. [`ReleasePlan::contract_schema_version`]
19//! is a *content* field (the contract-document version the plan was sealed
20//! against, part of the content address), not the wire-envelope version.
21//!
22//! The plan **reuses** [`Ecosystem`], [`Registry`], and [`Adapter`] from the
23//! canonical contract model rather than restating their wire strings: the plan,
24//! the contract, and the release adapters must agree on `"rust"` /
25//! `"crates.io"` / `"cargo-publish"` down to the byte, and sharing the one enum
26//! makes that agreement structural instead of coincidental.
27
28use serde::Serialize;
29
30use crate::contract::schema::{Adapter, Ecosystem, Registry};
31
32/// A sealed, content-addressed release plan — the artifact `release plan`
33/// emits and a human approves.
34///
35/// Every field except [`Self::plan_id`] is an *input* to the content address
36/// (`plan_id` is the SHA-256 digest **over** those inputs — plus the full
37/// normalized contract and a domain/seal-format tag — so it is derived, never
38/// authored, and is deliberately excluded from the hashed pre-image: a hash
39/// cannot cover itself). See [`crate::release::plan`] for the exact pre-image.
40#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
41pub struct ReleasePlan {
42 /// The content-addressed plan id: a lowercase 64-character SHA-256 hex
43 /// digest over the sealed pre-image (see [`crate::release::plan`] for
44 /// exactly what is hashed). Stable — identical inputs always yield this same
45 /// id; any change to a hashed input yields a different one. This is the
46 /// token passed back as `release cut --plan <plan_id>`.
47 pub plan_id: String,
48 /// The `OSS-RELEASE.md` contract-document schema version this plan was
49 /// sealed against (part of the content address). A schema bump between
50 /// approval and execution is drift.
51 pub contract_schema_version: u32,
52 /// The git `HEAD` commit sha the plan was sealed against. A new commit
53 /// between approval and execution is drift.
54 pub head_sha: String,
55 /// The chosen release version (the human's bump per design §3.4), supplied
56 /// as a validated input to `release plan`. Sealed verbatim; changing it
57 /// requires a new plan.
58 pub version: String,
59 /// The ordered, concrete publish targets a cut would execute — one per
60 /// configured `ecosystem → package → registry`, with `null` package names
61 /// resolved from detected repo facts where possible.
62 pub targets: Vec<PlanTarget>,
63 /// The coordinator phase sequence a cut drives (ADR-0002 §2): dry-run-all →
64 /// build-all → publish-all → tag → dist (the post-tag distribution finalize,
65 /// e.g. the Homebrew formula whose tarball only exists after the tag). When the
66 /// plan owns a version bump ([`Self::bump`] is `Some`), a leading
67 /// [`PlanPhase::Bump`] is prepended — the engine sets the workspace version,
68 /// rewrites the intra-workspace pins, refreshes the lockfile, finalizes the
69 /// CHANGELOG, and runs any declared `bump_hook` **before** the crates are built,
70 /// so the publish barrier builds the crates at the new version. The sequence is
71 /// **part of the content address** (it authenticates the execution shape the
72 /// approver saw — a plan with a bump phase can never be cut as one without);
73 /// carried here so the sealed artifact is self-describing.
74 pub phases: Vec<PlanPhase>,
75 /// The engine-owned version-bump phase, or `null` for a plan that publishes the
76 /// version already in the tree (the default, `--bump`-less path — unchanged).
77 ///
78 /// Present only when `release plan --bump <level>` computed a new version from
79 /// the current manifest version + the semantic bump level (`release-rust-workspace-
80 /// multicrate` facet 2). It carries the deterministic edit set the [`PlanPhase::Bump`]
81 /// phase applies at cut time — the computed `to_version`, the intra-workspace `=`-pin
82 /// rewrites, the CHANGELOG-finalize intent, and any contract-declared `bump_hook` —
83 /// all folded into the content address, so approving a `--bump minor` plan and cutting
84 /// it as `--bump major` (or without a bump) is drift, not a silent re-version.
85 ///
86 /// Omitted from the canonical JSON when `null` (`skip_serializing_if`), so a
87 /// `--bump`-less plan's wire shape and `plan_id` are byte-for-byte what they were
88 /// before this field existed — the `--bump` path is a strict, additive superset.
89 #[serde(skip_serializing_if = "Option::is_none")]
90 pub bump: Option<BumpPlan>,
91 /// The Homebrew tap repo (`owner/repo`) the cut's generated formula is
92 /// pushed to, or `null` when the contract configured none. Copied verbatim
93 /// from the (already content-addressed) normalized contract's
94 /// `distribution.homebrew_tap` — carried on the plan, like [`Self::phases`],
95 /// only so the coordinator can hand it to the Homebrew adapter's
96 /// first-formula bootstrap without re-reading the contract. Being a copy of a
97 /// value the pre-image already hashes, it changes no `plan_id`.
98 pub homebrew_tap: Option<String>,
99 /// The SPDX license expression the cut's generated Homebrew formula records,
100 /// copied from the normalized contract's `license`. Carried for the same
101 /// reason (and with the same content-address neutrality) as
102 /// [`Self::homebrew_tap`].
103 pub license: Option<String>,
104}
105
106/// One concrete publish destination in a sealed plan.
107///
108/// Mirrors the contract's [`crate::contract::schema::Target`] but is a distinct
109/// wire type: the plan may *resolve* a `null` package name from repo facts, so
110/// its `package` is the concrete name the human approves, not necessarily the
111/// contract's (which the executor would otherwise infer at cut time).
112#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
113pub struct PlanTarget {
114 /// The ecosystem this target publishes for.
115 pub ecosystem: Ecosystem,
116 /// The package/crate name — resolved from the detected manifest facts when
117 /// the contract left it `null`; still `null` if no manifest named it (the
118 /// executor would infer it at cut time).
119 pub package: Option<String>,
120 /// The publish destination.
121 pub registry: Registry,
122 /// The release tool pinned for this target.
123 pub adapter: Adapter,
124}
125
126/// The engine-owned version-bump phase's deterministic edit set (`release-rust-
127/// workspace-multicrate` facet 2) — the content-addressed intent the
128/// [`PlanPhase::Bump`] phase applies at cut time.
129///
130/// The human supplies only the semantic bump [`level`](Self::level); the engine
131/// **computes** [`to_version`](Self::to_version) from [`from_version`](Self::from_version)
132/// (the current manifest version) + that level (major → X+1.0.0, minor → X.Y+1.0,
133/// patch → X.Y.Z+1). There is no hand-typed literal version — this honours the
134/// single-source-version decision (`release-drop-version-flag`): the number is
135/// derived, never dictated. Every field is part of the sealed pre-image, so a
136/// different bump level or a different derived edit set is drift.
137#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
138pub struct BumpPlan {
139 /// The semantic bump level the human requested (`--bump major|minor|patch`).
140 pub level: BumpLevel,
141 /// The current workspace version the bump was computed **from** (read from
142 /// `[workspace.package] version`).
143 pub from_version: String,
144 /// The computed new version the bump lands **at** — set into `[workspace.package]
145 /// version` and threaded into every publish/tag as the release version.
146 pub to_version: String,
147 /// The intra-workspace `=`-version pin rewrites the bump applies, one per
148 /// (dependent-crate → pinned workspace dependency) edge whose pin tracks the
149 /// bumped version (e.g. the bin's `lib = "=<from>"` → `lib = "=<to>"`). Sorted,
150 /// deterministic; empty for a single-crate workspace with no intra-workspace pins.
151 pub pin_rewrites: Vec<PinRewrite>,
152 /// Whether the bump finalizes the CHANGELOG (`[Unreleased]` → a dated
153 /// `[to_version]` section). The concrete date is a cut-time value and is
154 /// deliberately **not** sealed (it would make the `plan_id` change per day); the
155 /// changelog *mode* that governs the finalize is already part of the hashed
156 /// contract. `false` only when the contract declares no changelog machinery.
157 pub changelog_finalize: bool,
158 /// The contract-declared command the engine runs in the clean checkout after the
159 /// version edits (`release.bump_hook`), so version-embedding artifacts (test
160 /// snapshots that embed the version) regenerate against the new version before the
161 /// bump commit — `release-rust-workspace-multicrate` facet 3. `null` = no hook.
162 /// Copied from the (already-hashed) contract; carried here so the executor need
163 /// not re-read it.
164 #[serde(skip_serializing_if = "Option::is_none")]
165 pub bump_hook: Option<String>,
166}
167
168/// One intra-workspace `=`-version pin the [`PlanPhase::Bump`] phase rewrites in
169/// lockstep with the workspace version (e.g. the bin crate's `lib-core = "=0.1.5"`
170/// → `lib-core = "=0.1.6"`). Derived deterministically from the workspace graph.
171#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
172pub struct PinRewrite {
173 /// The workspace member whose manifest carries the pin (the dependent crate).
174 pub in_package: String,
175 /// The pinned intra-workspace dependency crate (the pin's subject).
176 pub dependency: String,
177 /// The current pin requirement (`=<from_version>`).
178 pub from: String,
179 /// The rewritten pin requirement (`=<to_version>`).
180 pub to: String,
181}
182
183/// The semantic version-bump level a human requests with `--bump` — the *only*
184/// version input the engine accepts (it computes the number; the human never types
185/// it). A wire enum whose kebab string is stable and part of the plan's content
186/// address.
187#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
188pub enum BumpLevel {
189 /// `X.Y.Z` → `(X+1).0.0` — a breaking change.
190 Major,
191 /// `X.Y.Z` → `X.(Y+1).0` — a backwards-compatible feature.
192 Minor,
193 /// `X.Y.Z` → `X.Y.(Z+1)` — a backwards-compatible fix.
194 Patch,
195}
196
197impl BumpLevel {
198 /// The wire string for this level (kebab; the single source the `Serialize`
199 /// impl emits, so text and JSON never drift).
200 #[must_use]
201 pub fn as_str(self) -> &'static str {
202 match self {
203 Self::Major => "major",
204 Self::Minor => "minor",
205 Self::Patch => "patch",
206 }
207 }
208
209 /// Parse a wire string into a level, or `None` if unrecognized (the CLI turns a
210 /// `None` into the strict `--bump` value error).
211 #[must_use]
212 pub fn parse(s: &str) -> Option<Self> {
213 match s {
214 "major" => Some(Self::Major),
215 "minor" => Some(Self::Minor),
216 "patch" => Some(Self::Patch),
217 _ => None,
218 }
219 }
220
221 /// Every valid wire string, for "must be one of …" messages.
222 pub const VALID: &'static [&'static str] = &["major", "minor", "patch"];
223}
224
225impl serde::Serialize for BumpLevel {
226 fn serialize<S: serde::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
227 ser.serialize_str(self.as_str())
228 }
229}
230
231/// One phase of the coordinator's irreversibility-ordered pipeline (ADR-0002
232/// §2). Every plan drives [`PlanPhase::SEQUENCE`]; a plan that owns a version
233/// bump prepends [`PlanPhase::Bump`].
234#[derive(Debug, Clone, Copy, PartialEq, Eq)]
235pub enum PlanPhase {
236 /// Engine-owned version bump (present only for a `--bump` plan): set the
237 /// workspace version, rewrite the intra-workspace `=`-pins, refresh the
238 /// lockfile, finalize the CHANGELOG, run any declared `bump_hook`, and commit —
239 /// **before** any crate is built, so the crates build at the new version.
240 Bump,
241 /// Dry-run every target (re-runnable, no side effects).
242 DryRunAll,
243 /// Build every target (re-runnable).
244 BuildAll,
245 /// Publish every target (per-target irreversible).
246 PublishAll,
247 /// Create + push the one shared git tag and GitHub Release (coordinator-only).
248 Tag,
249 /// Post-tag distribution finalize: targets whose artifact only exists after the
250 /// tag (the Homebrew formula, whose `url` is the just-created tag archive) are
251 /// finalized with the real, post-tag-computed `sha256`.
252 Dist,
253}
254
255impl PlanPhase {
256 /// The wire string for this phase (kebab-case; the single source the
257 /// `Serialize` impl also emits, so text and JSON never drift).
258 #[must_use]
259 pub fn as_str(self) -> &'static str {
260 match self {
261 Self::Bump => "bump",
262 Self::DryRunAll => "dry-run-all",
263 Self::BuildAll => "build-all",
264 Self::PublishAll => "publish-all",
265 Self::Tag => "tag",
266 Self::Dist => "dist",
267 }
268 }
269
270 /// The invariant phase order a `--bump`-less cut drives, dry-run-all → dist. A
271 /// `--bump` plan prepends [`PlanPhase::Bump`] (see [`ReleasePlan::phases`]).
272 pub const SEQUENCE: [PlanPhase; 5] = [
273 Self::DryRunAll,
274 Self::BuildAll,
275 Self::PublishAll,
276 Self::Tag,
277 Self::Dist,
278 ];
279
280 /// The invariant phase order a cut drives, dry-run-all → dist (borrowed view
281 /// of [`Self::SEQUENCE`]; no allocation).
282 #[must_use]
283 pub fn sequence() -> &'static [PlanPhase] {
284 &Self::SEQUENCE
285 }
286}
287
288impl serde::Serialize for PlanPhase {
289 fn serialize<S: serde::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
290 ser.serialize_str(self.as_str())
291 }
292}