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/// How prikk's own key-status report describes the relationship between the key that would sign and
13/// what the repository already knows (prikk ≥ 0.41's `key-status-v1` `binding` field).
14///
15/// **stikk mirrors prikk's vocabulary rather than inventing one** (RFC 026 Decision 3). A collapsed
16/// vocabulary plus a paragraph explaining the collapse is how stikk ended up, until 0.6.0 removed it,
17/// with a `MaintainerReadiness` type that could not express `not-adopted` — the state RFC 025 wanted a
18/// fourth variant for, which prikk had a name for all along.
19///
20/// Measured against a real prikk 0.41.0, one repository per state (RFC 026 Handoff B).
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum Binding {
23 /// The key that would sign is the key this repository already records for that id. The only state
24 /// that earns a plain statement on a confirmation card.
25 Matches,
26 /// No signature by this id exists yet, so nothing is recorded to disagree with. **Arms**: prikk
27 /// accepts a first signature and binds the id then. This is the default state of every freshly
28 /// created repository, so it is the first commit a new user makes.
29 Unrecorded,
30 /// MAINTAINER only: the key is usable but the repository's trust policy has not adopted it, and
31 /// prikk will refuse the seal. **Withholds.**
32 NotAdopted,
33 /// The key that would sign is *not* the key recorded for this id. prikk refuses at signing time,
34 /// so offering the action would be offering a failure. **Withholds**, and the card says which two
35 /// things disagree.
36 Mismatch,
37 /// prikk reported `null`: there was no usable seed to bind, or no repository to ask. A real state,
38 /// not a parse failure.
39 Absent,
40}
41
42/// Whether one signing role can sign, as far as this session can tell.
43///
44/// **Two unknowns live here and they must never share a variant** (RFC 026 §3). [`Self::Unknown`]
45/// grants the capability with a caveat rendered; [`Self::Unverifiable`] withholds it and says so. A
46/// single variant that sometimes did each is the shape `C-T2c′` exists to forbid.
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub enum RoleReadiness {
49 /// No key material this session can use. At ≤ 0.39 the variables are absent; at ≥ 0.41 prikk said
50 /// `usable: false`, and its own `reason` travels separately for display (`ER-02`).
51 NotReady,
52 /// Key material is present, and whether it binds is **unanswerable on this prikk** — the ≤ 0.39
53 /// band, where `binding` does not exist. **Grants**, with the caveat rendered: RFC 016's rule is
54 /// to offer the action and let prikk refuse, never to render the caveat as a pass.
55 Unknown,
56 /// **prikk 0.40 exactly**: stikk cannot see whether there is key material at all. 0.40 moved seeds
57 /// to a key directory and shipped no way to ask about them, so presence-in-the-environment stopped
58 /// being an answer and nothing replaced it until 0.41. **Withholds**, and the UI says *unknown*
59 /// rather than *not ready* — Q1(b), ruled: honest and actionable beats confident and wrong.
60 Unverifiable,
61 /// prikk answered (≥ 0.41).
62 Known(Binding),
63}
64
65impl RoleReadiness {
66 /// Whether this state arms the role's operations.
67 ///
68 /// The one place the grant/withhold split is decided, so a caller cannot re-derive it differently.
69 #[must_use]
70 pub const fn arms(self) -> bool {
71 match self {
72 Self::NotReady | Self::Unverifiable => false,
73 Self::Unknown => true,
74 Self::Known(binding) => matches!(binding, Binding::Matches | Binding::Unrecorded),
75 }
76 }
77}
78
79/// Whether each signing role's key material is available to the current session, plus whether the
80/// session is in read-only mode.
81///
82/// This carries no key material — only presence flags (and, for MAINTAINER, the further-unverifiable
83/// adoption question — see [`RoleReadiness`]). It is the input to [`Capability::derive`].
84#[derive(Debug, Clone, Copy, PartialEq, Eq)]
85pub struct Readiness {
86 /// Whether the AUTHOR role can sign, as far as this session can tell.
87 ///
88 /// **Was `author_ready: bool`** until RFC 026. That asymmetry with MAINTAINER was right when AUTHOR
89 /// had nothing to be unsure about; prikk ≥ 0.41 answers `binding` for **both** roles, and
90 /// `mismatch` applies to both, so both get the same vocabulary.
91 pub author: RoleReadiness,
92 /// Whether the MAINTAINER role can sign, as far as this session can tell.
93 pub maintainer: RoleReadiness,
94 /// True when the session is in read-only mode (a global override, or the default when no signing
95 /// readiness is present). When set, no capability above [`Capability::Viewer`] is granted.
96 pub read_only: bool,
97}
98
99impl Readiness {
100 /// A session with no signing readiness and no read-only override — the Viewer default.
101 #[must_use]
102 pub const fn none() -> Self {
103 Self {
104 author: RoleReadiness::NotReady,
105 maintainer: RoleReadiness::NotReady,
106 read_only: false,
107 }
108 }
109
110 /// True when a human at the machine may run recovery actions (doctor repair, lock clearing,
111 /// compaction) under explicit confirmation (design AC-04; RFC 012 F-a).
112 ///
113 /// Lives here, not on [`Capability`], because the one fact that decides it — `read_only` — is
114 /// exactly what [`Capability::derive`] discards on the way to a [`Capability`]: by the time a
115 /// capability exists, a read-only session and a no-keys session are indistinguishable, so
116 /// `may_operate` could not be implemented correctly as a `Capability` method. `AC-04`'s
117 /// "orthogonal to the [mutating axis]" describes how Operator is *derived* — any human at the
118 /// machine, not a signing role — never an exemption from the global read-only switch: `FR-121`
119 /// governs, and a read-only mode that still permits clearing another writer's lock would itself be
120 /// the "confident-but-wrong picture" (`T-T4`) this project refuses. Each recovery action still
121 /// carries its own typed confirmation (`FR-102`) regardless of this check.
122 #[must_use]
123 pub const fn may_operate(self) -> bool {
124 !self.read_only
125 }
126}
127
128/// What a session may do, derived from [`Readiness`] (design AC-01…04).
129///
130/// The levels are cumulative for the mutating axis (`Maintainer` implies `Author` implies `Viewer`).
131/// `Operator` is orthogonal — recovery actions available to any human at the machine under explicit
132/// confirmation — and is represented as a separate query on [`Readiness`] itself
133/// ([`Readiness::may_operate`]) rather than a point on this ladder or a method here: `derive` below
134/// discards `read_only` on the way to a `Capability`, so the one fact `may_operate` needs does not
135/// survive to exist as a method on this type (RFC 012 F-a).
136#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
137pub enum Capability {
138 /// Every read surface. The default when signing readiness is absent or read-only mode is on.
139 Viewer,
140 /// Viewer plus queue-affecting operations: commit, rollback draft.
141 Author,
142 /// Author plus history-publishing: seal, merge execution, ref/tag publication, trust changes.
143 Maintainer,
144}
145
146impl Capability {
147 /// Derive the mutating-axis capability from readiness. Read-only mode collapses everything to
148 /// [`Capability::Viewer`] regardless of key presence (design NFR-S01).
149 ///
150 /// **The one fold, and the only place the grant/withhold decision is made** — through
151 /// [`RoleReadiness::arms`], so no gate can grow its own rule (RFC 026 §3).
152 ///
153 /// `Unknown` grants (RFC 016 Q1): the affordance is still offered, since hiding seal from someone
154 /// whose key *is* adopted would be its own confident-but-wrong picture (`C-T4d`). What `Unknown`
155 /// changes is what stikk *claims* about the outcome, not what it *offers*. `Unverifiable`,
156 /// `Mismatch` and `NotAdopted` withhold, for the opposite reason: prikk will refuse, so offering
157 /// the action would be offering a failure.
158 #[must_use]
159 pub const fn derive(readiness: Readiness) -> Self {
160 if readiness.read_only {
161 return Self::Viewer;
162 }
163 if readiness.maintainer.arms() {
164 Self::Maintainer
165 } else if readiness.author.arms() {
166 Self::Author
167 } else {
168 Self::Viewer
169 }
170 }
171
172 /// True when this capability permits queue-affecting operations (commit, rollback draft).
173 #[must_use]
174 pub const fn may_author(self) -> bool {
175 matches!(self, Self::Author | Self::Maintainer)
176 }
177
178 /// True when this capability permits history-publishing operations (seal, merge, publication,
179 /// trust changes).
180 #[must_use]
181 pub const fn may_publish(self) -> bool {
182 matches!(self, Self::Maintainer)
183 }
184
185 /// The stable machine-readable name (design CL-07).
186 #[must_use]
187 pub const fn name(self) -> &'static str {
188 match self {
189 Self::Viewer => "viewer",
190 Self::Author => "author",
191 Self::Maintainer => "maintainer",
192 }
193 }
194}
195
196#[cfg(test)]
197mod tests;