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
40/// What a session may do, derived from [`Readiness`] (design AC-01…04).
41///
42/// The levels are cumulative for the mutating axis (`Maintainer` implies `Author` implies `Viewer`).
43/// `Operator` is orthogonal — recovery actions available to any human at the machine under explicit
44/// confirmation — and is represented as a separate query ([`Capability::may_operate`]) rather than a
45/// point on the ladder.
46#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
47pub enum Capability {
48 /// Every read surface. The default when signing readiness is absent or read-only mode is on.
49 Viewer,
50 /// Viewer plus queue-affecting operations: commit, rollback draft.
51 Author,
52 /// Author plus history-publishing: seal, merge execution, ref/tag publication, trust changes.
53 Maintainer,
54}
55
56impl Capability {
57 /// Derive the mutating-axis capability from readiness. Read-only mode collapses everything to
58 /// [`Capability::Viewer`] regardless of key presence (design NFR-S01).
59 #[must_use]
60 pub const fn derive(readiness: Readiness) -> Self {
61 if readiness.read_only {
62 return Self::Viewer;
63 }
64 if readiness.maintainer_ready {
65 Self::Maintainer
66 } else if readiness.author_ready {
67 Self::Author
68 } else {
69 Self::Viewer
70 }
71 }
72
73 /// True when this capability permits queue-affecting operations (commit, rollback draft).
74 #[must_use]
75 pub const fn may_author(self) -> bool {
76 matches!(self, Self::Author | Self::Maintainer)
77 }
78
79 /// True when this capability permits history-publishing operations (seal, merge, publication,
80 /// trust changes).
81 #[must_use]
82 pub const fn may_publish(self) -> bool {
83 matches!(self, Self::Maintainer)
84 }
85
86 /// True when a human at the machine may run recovery actions (doctor repair, lock clearing,
87 /// compaction) under explicit confirmation. Orthogonal to the mutating axis: recovery is not a
88 /// signing act, it is an operator act, and read-only mode does not remove it (design AC-04) — but
89 /// each recovery action still carries its own typed confirmation (FR-102).
90 #[must_use]
91 pub const fn may_operate(self) -> bool {
92 true
93 }
94
95 /// The stable machine-readable name (design CL-07).
96 #[must_use]
97 pub const fn name(self) -> &'static str {
98 match self {
99 Self::Viewer => "viewer",
100 Self::Author => "author",
101 Self::Maintainer => "maintainer",
102 }
103 }
104}
105
106#[cfg(test)]
107mod tests;