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 each signing role's key material is available to the current session, plus whether the
13/// session is in read-only mode.
14///
15/// This carries no key material — only presence flags. It is the input to [`Capability::derive`].
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub struct Readiness {
18    /// True when an AUTHOR key id and seed are both present in the environment (presence only — the
19    /// seed value is never read; see `stikk-prikk::env`).
20    pub author_ready: bool,
21    /// True when a MAINTAINER key id and seed are both present in the environment (presence only).
22    pub maintainer_ready: bool,
23    /// True when the session is in read-only mode (a global override, or the default when no signing
24    /// readiness is present). When set, no capability above [`Capability::Viewer`] is granted.
25    pub read_only: bool,
26}
27
28impl Readiness {
29    /// A session with no signing readiness and no read-only override — the Viewer default.
30    #[must_use]
31    pub const fn none() -> Self {
32        Self {
33            author_ready: false,
34            maintainer_ready: false,
35            read_only: false,
36        }
37    }
38
39    /// True when a human at the machine may run recovery actions (doctor repair, lock clearing,
40    /// compaction) under explicit confirmation (design AC-04; RFC 012 F-a).
41    ///
42    /// Lives here, not on [`Capability`], because the one fact that decides it — `read_only` — is
43    /// exactly what [`Capability::derive`] discards on the way to a [`Capability`]: by the time a
44    /// capability exists, a read-only session and a no-keys session are indistinguishable, so
45    /// `may_operate` could not be implemented correctly as a `Capability` method. `AC-04`'s
46    /// "orthogonal to the [mutating axis]" describes how Operator is *derived* — any human at the
47    /// machine, not a signing role — never an exemption from the global read-only switch: `FR-121`
48    /// governs, and a read-only mode that still permits clearing another writer's lock would itself be
49    /// the "confident-but-wrong picture" (`T-T4`) this project refuses. Each recovery action still
50    /// carries its own typed confirmation (`FR-102`) regardless of this check.
51    #[must_use]
52    pub const fn may_operate(self) -> bool {
53        !self.read_only
54    }
55}
56
57/// What a session may do, derived from [`Readiness`] (design AC-01…04).
58///
59/// The levels are cumulative for the mutating axis (`Maintainer` implies `Author` implies `Viewer`).
60/// `Operator` is orthogonal — recovery actions available to any human at the machine under explicit
61/// confirmation — and is represented as a separate query on [`Readiness`] itself
62/// ([`Readiness::may_operate`]) rather than a point on this ladder or a method here: `derive` below
63/// discards `read_only` on the way to a `Capability`, so the one fact `may_operate` needs does not
64/// survive to exist as a method on this type (RFC 012 F-a).
65#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
66pub enum Capability {
67    /// Every read surface. The default when signing readiness is absent or read-only mode is on.
68    Viewer,
69    /// Viewer plus queue-affecting operations: commit, rollback draft.
70    Author,
71    /// Author plus history-publishing: seal, merge execution, ref/tag publication, trust changes.
72    Maintainer,
73}
74
75impl Capability {
76    /// Derive the mutating-axis capability from readiness. Read-only mode collapses everything to
77    /// [`Capability::Viewer`] regardless of key presence (design NFR-S01).
78    #[must_use]
79    pub const fn derive(readiness: Readiness) -> Self {
80        if readiness.read_only {
81            return Self::Viewer;
82        }
83        if readiness.maintainer_ready {
84            Self::Maintainer
85        } else if readiness.author_ready {
86            Self::Author
87        } else {
88            Self::Viewer
89        }
90    }
91
92    /// True when this capability permits queue-affecting operations (commit, rollback draft).
93    #[must_use]
94    pub const fn may_author(self) -> bool {
95        matches!(self, Self::Author | Self::Maintainer)
96    }
97
98    /// True when this capability permits history-publishing operations (seal, merge, publication,
99    /// trust changes).
100    #[must_use]
101    pub const fn may_publish(self) -> bool {
102        matches!(self, Self::Maintainer)
103    }
104
105    /// The stable machine-readable name (design CL-07).
106    #[must_use]
107    pub const fn name(self) -> &'static str {
108        match self {
109            Self::Viewer => "viewer",
110            Self::Author => "author",
111            Self::Maintainer => "maintainer",
112        }
113    }
114}
115
116#[cfg(test)]
117mod tests;