Skip to main content

stikk_model/
capability.rs

1//! Derived capability levels and the signing readiness they come from.
2//!
3//! Design `stikk-04` AC-01…04, OPL-04. stikk has no accounts. What a session may do is *derived*
4//! per session from prikk-side facts — which signing roles are ready, and whether read-only mode is
5//! on — and is displayed, never stored. The capability is checked twice (design OPL-04): once to
6//! decide UI affordance, once at the seam before a mutating call, because readiness can lapse between
7//! render and click.
8//!
9//! Critically, [`Readiness`] records only **whether** a role's key material is present, never the
10//! material itself (threat model C-I1, data model LC-13). This type cannot hold a secret.
11
12/// Whether prikk's repository-side trust policy has adopted a MAINTAINER key (design `FR-104`; RFC
13/// 016 F2/F3).
14///
15/// Presence of `PRIKK_MAINTAINER_KEY_ID`/`_SEED` in the environment is necessary but not sufficient:
16/// prikk's `verify_signer_trusted` also requires the key to be **adopted** in the repository's trust
17/// policy before a MAINTAINER-gated operation succeeds. **Adoption is object trust, not ref
18/// authority** — prikk accepts that key's signatures on objects; adopting a key never lets it move a
19/// ref (`RefStore::publish` still requires this operator's own signature). Say so wherever this state
20/// is named; wording that lets a reader conclude "may publish here" is a defect (prikk RFC 138 §7.3).
21///
22/// Through prikk 0.33, nothing exposed a way to check adoption ahead of attempting the gated
23/// operation itself (RFC 016 F3): `prikk trust maintainer` offered only `add`/`remove`, and `verify`'s
24/// `sealed-block <id>: <key_id>` line is historical signer attribution — a since-revoked key still
25/// prints — not current policy, and does not exist before a repository's first seal, which is exactly
26/// when this question is asked. **No increment may ever resolve `Unknown` from `verify` output**
27/// (RFC 016's own named trap); that remains true regardless of what follows.
28///
29/// **prikk 0.34 shipped the surface** (upstream RFC 138): `prikk trust maintainer list` and
30/// `check --key-id`, both with `--format json`, `check` exiting `0` whichever way the answer comes out.
31/// Two things follow, and they are easy to conflate:
32///
33/// - **`Ready` became *constructible* at ≥ 0.34 — it is not yet *constructed*.** stikk reads neither
34///   command: RFC 021 raised the validated ceiling to 0.38 and deliberately built no seam method for
35///   this (its Decision 5). So `stikk-prikk::env` still returns only `NotReady`/`Unknown`, and the
36///   `[MNT]` badge still reads `?` on every version. Anything claiming otherwise is describing prikk's
37///   capability, not stikk's behaviour.
38/// - **`Unknown` is permanent, not transitional.** stikk's floor is prikk 0.28 (`ASM-2`), so every
39///   session on 0.28–0.33 has no way to answer the question no matter what stikk builds. This type
40///   stays three-valued for good; the increment that reads 0.34's surface makes the answer
41///   *version-conditional*, never unconditional.
42///
43/// This is the gate for all **eight** of prikk's `GatedOperation` variants — `Seal`, `Merge`,
44/// `SyncBuild`, `SyncSeal`, `SyncAdoptTag`, `TagCreate`, `BranchCreate`, `BranchClose` — not seal's
45/// alone; seal is only stikk's first consumer of it.
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub enum MaintainerReadiness {
48    /// Key material present **and** adopted in the repository's trust policy. **Still unconstructed
49    /// today**, though no longer unconstructible in principle — see this type's own doc.
50    /// `prikk trust maintainer check` shipped in 0.34 (upstream RFC 138), so the answer now exists on
51    /// ≥ 0.34; stikk does not read it yet, so `stikk-prikk::env` continues to produce only
52    /// `NotReady`/`Unknown`. It remains documented the way RFC 017 documented
53    /// `StikkError::IntegrityFinding`: present because it is the shape of the answer, and now also
54    /// because the answer itself exists upstream and an increment will come to fetch it.
55    Ready,
56    /// Key material absent — no `PRIKK_MAINTAINER_KEY_ID`/`_SEED` pair in the environment.
57    NotReady,
58    /// Key material present; adoption is **unverifiable by stikk today**, and permanently
59    /// unverifiable on prikk 0.28–0.33 whatever stikk builds (RFC 016 F3; RFC 021 §5). This
60    /// is what `stikk-prikk::env` returns whenever both variables are set — never a caveat layered on
61    /// a boolean, because `Unknown` must never render as a pass (`C-T2c′`, the same rule this
62    /// project's design already applies to `FR-035`'s three-valued author-signature outcome).
63    Unknown,
64}
65
66/// Whether each signing role's key material is available to the current session, plus whether the
67/// session is in read-only mode.
68///
69/// This carries no key material — only presence flags (and, for MAINTAINER, the further-unverifiable
70/// adoption question — see [`MaintainerReadiness`]). It is the input to [`Capability::derive`].
71#[derive(Debug, Clone, Copy, PartialEq, Eq)]
72pub struct Readiness {
73    /// True when an AUTHOR key id and seed are both present in the environment (presence only — the
74    /// seed value is never read; see `stikk-prikk::env`).
75    pub author_ready: bool,
76    /// Whether a MAINTAINER key is ready to sign — see [`MaintainerReadiness`] for why this is
77    /// three-valued rather than a presence-only bool the way `author_ready` is.
78    pub maintainer_readiness: MaintainerReadiness,
79    /// True when the session is in read-only mode (a global override, or the default when no signing
80    /// readiness is present). When set, no capability above [`Capability::Viewer`] is granted.
81    pub read_only: bool,
82}
83
84impl Readiness {
85    /// A session with no signing readiness and no read-only override — the Viewer default.
86    #[must_use]
87    pub const fn none() -> Self {
88        Self {
89            author_ready: false,
90            maintainer_readiness: MaintainerReadiness::NotReady,
91            read_only: false,
92        }
93    }
94
95    /// True when a human at the machine may run recovery actions (doctor repair, lock clearing,
96    /// compaction) under explicit confirmation (design AC-04; RFC 012 F-a).
97    ///
98    /// Lives here, not on [`Capability`], because the one fact that decides it — `read_only` — is
99    /// exactly what [`Capability::derive`] discards on the way to a [`Capability`]: by the time a
100    /// capability exists, a read-only session and a no-keys session are indistinguishable, so
101    /// `may_operate` could not be implemented correctly as a `Capability` method. `AC-04`'s
102    /// "orthogonal to the [mutating axis]" describes how Operator is *derived* — any human at the
103    /// machine, not a signing role — never an exemption from the global read-only switch: `FR-121`
104    /// governs, and a read-only mode that still permits clearing another writer's lock would itself be
105    /// the "confident-but-wrong picture" (`T-T4`) this project refuses. Each recovery action still
106    /// carries its own typed confirmation (`FR-102`) regardless of this check.
107    #[must_use]
108    pub const fn may_operate(self) -> bool {
109        !self.read_only
110    }
111}
112
113/// What a session may do, derived from [`Readiness`] (design AC-01…04).
114///
115/// The levels are cumulative for the mutating axis (`Maintainer` implies `Author` implies `Viewer`).
116/// `Operator` is orthogonal — recovery actions available to any human at the machine under explicit
117/// confirmation — and is represented as a separate query on [`Readiness`] itself
118/// ([`Readiness::may_operate`]) rather than a point on this ladder or a method here: `derive` below
119/// discards `read_only` on the way to a `Capability`, so the one fact `may_operate` needs does not
120/// survive to exist as a method on this type (RFC 012 F-a).
121#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
122pub enum Capability {
123    /// Every read surface. The default when signing readiness is absent or read-only mode is on.
124    Viewer,
125    /// Viewer plus queue-affecting operations: commit, rollback draft.
126    Author,
127    /// Author plus history-publishing: seal, merge execution, ref/tag publication, trust changes.
128    Maintainer,
129}
130
131impl Capability {
132    /// Derive the mutating-axis capability from readiness. Read-only mode collapses everything to
133    /// [`Capability::Viewer`] regardless of key presence (design NFR-S01).
134    ///
135    /// Grants `Maintainer` on [`MaintainerReadiness::Ready`] **or** [`MaintainerReadiness::Unknown`]
136    /// (RFC 016 Q1): the affordance is still offered, since hiding seal from someone whose key *is*
137    /// adopted would be its own confident-but-wrong picture (`C-T4d`). What `Unknown` changes is what
138    /// stikk *claims* about the outcome, not what it *offers* — the badge and the ceremony's own copy
139    /// carry that distinction; this method does not.
140    #[must_use]
141    pub const fn derive(readiness: Readiness) -> Self {
142        if readiness.read_only {
143            return Self::Viewer;
144        }
145        match readiness.maintainer_readiness {
146            MaintainerReadiness::Ready | MaintainerReadiness::Unknown => Self::Maintainer,
147            MaintainerReadiness::NotReady => {
148                if readiness.author_ready {
149                    Self::Author
150                } else {
151                    Self::Viewer
152                }
153            }
154        }
155    }
156
157    /// True when this capability permits queue-affecting operations (commit, rollback draft).
158    #[must_use]
159    pub const fn may_author(self) -> bool {
160        matches!(self, Self::Author | Self::Maintainer)
161    }
162
163    /// True when this capability permits history-publishing operations (seal, merge, publication,
164    /// trust changes).
165    #[must_use]
166    pub const fn may_publish(self) -> bool {
167        matches!(self, Self::Maintainer)
168    }
169
170    /// The stable machine-readable name (design CL-07).
171    #[must_use]
172    pub const fn name(self) -> &'static str {
173        match self {
174            Self::Viewer => "viewer",
175            Self::Author => "author",
176            Self::Maintainer => "maintainer",
177        }
178    }
179}
180
181#[cfg(test)]
182mod tests;