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/// No supported prikk (0.28–0.33) exposes a way to check adoption ahead of attempting the gated
23/// operation itself (RFC 016 F3): `prikk trust maintainer` offers 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. So today only two of the three states below are reachable; **no
27/// future increment may resolve `Unknown` from `verify` output** (RFC 016's own named trap).
28///
29/// This is the gate for all **eight** of prikk's `GatedOperation` variants — `Seal`, `Merge`,
30/// `SyncBuild`, `SyncSeal`, `SyncAdoptTag`, `TagCreate`, `BranchCreate`, `BranchClose` — not seal's
31/// alone; seal is only stikk's first consumer of it.
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum MaintainerReadiness {
34 /// Key material present **and** adopted in the repository's trust policy. **Unconstructible
35 /// today** — see this type's own doc — and that is correct, not a gap: no supported prikk can
36 /// answer the adoption question, so `stikk-prikk::env` never produces this variant. It exists
37 /// because it is the shape the answer arrives in once `prikk trust maintainer check` ships
38 /// (upstream RFC 138, accepted and ruled, unreleased), documented the way RFC 017 documented
39 /// `StikkError::IntegrityFinding`: unreachable now, present because it is the shape of the answer.
40 Ready,
41 /// Key material absent — no `PRIKK_MAINTAINER_KEY_ID`/`_SEED` pair in the environment.
42 NotReady,
43 /// Key material present; adoption is **unverifiable** on any supported prikk (RFC 016 F3). This
44 /// is what `stikk-prikk::env` returns whenever both variables are set — never a caveat layered on
45 /// a boolean, because `Unknown` must never render as a pass (`C-T2c′`, the same rule this
46 /// project's design already applies to `FR-035`'s three-valued author-signature outcome).
47 Unknown,
48}
49
50/// Whether each signing role's key material is available to the current session, plus whether the
51/// session is in read-only mode.
52///
53/// This carries no key material — only presence flags (and, for MAINTAINER, the further-unverifiable
54/// adoption question — see [`MaintainerReadiness`]). It is the input to [`Capability::derive`].
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56pub struct Readiness {
57 /// True when an AUTHOR key id and seed are both present in the environment (presence only — the
58 /// seed value is never read; see `stikk-prikk::env`).
59 pub author_ready: bool,
60 /// Whether a MAINTAINER key is ready to sign — see [`MaintainerReadiness`] for why this is
61 /// three-valued rather than a presence-only bool the way `author_ready` is.
62 pub maintainer_readiness: MaintainerReadiness,
63 /// True when the session is in read-only mode (a global override, or the default when no signing
64 /// readiness is present). When set, no capability above [`Capability::Viewer`] is granted.
65 pub read_only: bool,
66}
67
68impl Readiness {
69 /// A session with no signing readiness and no read-only override — the Viewer default.
70 #[must_use]
71 pub const fn none() -> Self {
72 Self {
73 author_ready: false,
74 maintainer_readiness: MaintainerReadiness::NotReady,
75 read_only: false,
76 }
77 }
78
79 /// True when a human at the machine may run recovery actions (doctor repair, lock clearing,
80 /// compaction) under explicit confirmation (design AC-04; RFC 012 F-a).
81 ///
82 /// Lives here, not on [`Capability`], because the one fact that decides it — `read_only` — is
83 /// exactly what [`Capability::derive`] discards on the way to a [`Capability`]: by the time a
84 /// capability exists, a read-only session and a no-keys session are indistinguishable, so
85 /// `may_operate` could not be implemented correctly as a `Capability` method. `AC-04`'s
86 /// "orthogonal to the [mutating axis]" describes how Operator is *derived* — any human at the
87 /// machine, not a signing role — never an exemption from the global read-only switch: `FR-121`
88 /// governs, and a read-only mode that still permits clearing another writer's lock would itself be
89 /// the "confident-but-wrong picture" (`T-T4`) this project refuses. Each recovery action still
90 /// carries its own typed confirmation (`FR-102`) regardless of this check.
91 #[must_use]
92 pub const fn may_operate(self) -> bool {
93 !self.read_only
94 }
95}
96
97/// What a session may do, derived from [`Readiness`] (design AC-01…04).
98///
99/// The levels are cumulative for the mutating axis (`Maintainer` implies `Author` implies `Viewer`).
100/// `Operator` is orthogonal — recovery actions available to any human at the machine under explicit
101/// confirmation — and is represented as a separate query on [`Readiness`] itself
102/// ([`Readiness::may_operate`]) rather than a point on this ladder or a method here: `derive` below
103/// discards `read_only` on the way to a `Capability`, so the one fact `may_operate` needs does not
104/// survive to exist as a method on this type (RFC 012 F-a).
105#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
106pub enum Capability {
107 /// Every read surface. The default when signing readiness is absent or read-only mode is on.
108 Viewer,
109 /// Viewer plus queue-affecting operations: commit, rollback draft.
110 Author,
111 /// Author plus history-publishing: seal, merge execution, ref/tag publication, trust changes.
112 Maintainer,
113}
114
115impl Capability {
116 /// Derive the mutating-axis capability from readiness. Read-only mode collapses everything to
117 /// [`Capability::Viewer`] regardless of key presence (design NFR-S01).
118 ///
119 /// Grants `Maintainer` on [`MaintainerReadiness::Ready`] **or** [`MaintainerReadiness::Unknown`]
120 /// (RFC 016 Q1): the affordance is still offered, since hiding seal from someone whose key *is*
121 /// adopted would be its own confident-but-wrong picture (`C-T4d`). What `Unknown` changes is what
122 /// stikk *claims* about the outcome, not what it *offers* — the badge and the ceremony's own copy
123 /// carry that distinction; this method does not.
124 #[must_use]
125 pub const fn derive(readiness: Readiness) -> Self {
126 if readiness.read_only {
127 return Self::Viewer;
128 }
129 match readiness.maintainer_readiness {
130 MaintainerReadiness::Ready | MaintainerReadiness::Unknown => Self::Maintainer,
131 MaintainerReadiness::NotReady => {
132 if readiness.author_ready {
133 Self::Author
134 } else {
135 Self::Viewer
136 }
137 }
138 }
139 }
140
141 /// True when this capability permits queue-affecting operations (commit, rollback draft).
142 #[must_use]
143 pub const fn may_author(self) -> bool {
144 matches!(self, Self::Author | Self::Maintainer)
145 }
146
147 /// True when this capability permits history-publishing operations (seal, merge, publication,
148 /// trust changes).
149 #[must_use]
150 pub const fn may_publish(self) -> bool {
151 matches!(self, Self::Maintainer)
152 }
153
154 /// The stable machine-readable name (design CL-07).
155 #[must_use]
156 pub const fn name(self) -> &'static str {
157 match self {
158 Self::Viewer => "viewer",
159 Self::Author => "author",
160 Self::Maintainer => "maintainer",
161 }
162 }
163}
164
165#[cfg(test)]
166mod tests;