Skip to main content

stikk_model/
category.rs

1//! The request-category vocabulary the seam and operation layer share.
2//!
3//! Design `stikk-04` CT-03, AR-05. Rather than each operation inventing its own idempotency,
4//! cancellability, and locking story, every request the seam can make belongs to one of these ten
5//! categories, and the category carries that policy as data. The operation layer reads the policy off
6//! the type: a mutating category never auto-retries (NFR-S04), and stikk holds a single per-repository
7//! mutation gate for all of them (design CC-02).
8//!
9//! **The signing requirement is the category's too** ([`RequestCategory::signing_requirement`],
10//! RFC 045 §2b). It used to be read off the tier, which worked only while the two happened to
11//! agree: [`RequestCategory::Materialization`] is Tier Three and needs **no key at all** (RFC 045
12//! F1, measured), so a tier-keyed gate would have demanded a maintainer key for an operation that
13//! never signs anything.
14
15/// The confirmation tier a request's category requires before it may execute (design `FR-121`; RFC 013
16/// decision 4). Derived from [`RequestCategory`] alone, **never** declared per operation, so a new
17/// operation cannot forget to be gated — the same mechanical guarantee `FR-123` gives frontend parity.
18///
19/// Ordering matters: `Tier::One < Tier::Two < Tier::Three < Tier::ThreeTyped`, so a `>=` comparison
20/// reads as "requires at least this much ceremony."
21#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
22pub enum Tier {
23    /// Free — no confirmation, ever (RFC 013 decision 6: a standing constraint, not a default). Every
24    /// read category is this tier.
25    One,
26    /// Queue-affecting: an explicit yes.
27    Two,
28    /// History-publishing: restate the operation, then an explicit yes.
29    Three,
30    /// Recovery/trust: restate, then the typed target name, matched exactly (`FR-102`/`FR-103`).
31    ThreeTyped,
32}
33
34/// What signing readiness a category's requests need before they may run (design `AC-01`…`AC-04`;
35/// RFC 045 §2b).
36///
37/// **Beside the tier, not derived from it.** The tier says how much ceremony the *user* performs;
38/// this says what the *session* must already have. They coincide for every category that existed
39/// before RFC 045, which is exactly why reading one off the other went unnoticed until a Tier
40/// Three operation needed no key.
41#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
42pub enum SigningRequirement {
43    /// Nothing beyond being able to operate at all — a human at the machine, not in read-only
44    /// mode ([`crate::Readiness::may_operate`]).
45    ReadWrite,
46    /// An AUTHOR signing key.
47    Author,
48    /// A MAINTAINER signing key.
49    Maintainer,
50}
51
52/// One of the ten categories every prikk request belongs to (design CT-03 table).
53#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
54#[non_exhaustive]
55pub enum RequestCategory {
56    /// Reading history: ref lists, lineage, patch/block reads, ref chains.
57    ReadHistory,
58    /// Reading state: state trees, content at a block, comparisons.
59    ReadState,
60    /// Worktree analysis: changes vs a baseline.
61    WorktreeAnalysis,
62    /// Queue mutation: commit, rollback draft.
63    QueueMutation,
64    /// Publication: seal, merge execution, branch/tag publication.
65    Publication,
66    /// Exchange: bundle export/verify/import, sync build/accept/seal.
67    Exchange,
68    /// Integrity: verify, doctor (read).
69    Integrity,
70    /// Trust: key add/remove, tag adoption, readiness probe.
71    Trust,
72    /// Recovery: doctor repair, lock clearing, compaction.
73    Recovery,
74    /// Materialization: **writes the worktree from history** — branch switch now, checkout if it
75    /// ever comes (RFC 045 decision 2).
76    ///
77    /// **Appended rather than placed beside [`Self::Publication`]**, which is where it belongs by
78    /// meaning: these variants derive `Ord`, and inserting one in the middle would silently
79    /// renumber every category after it.
80    Materialization,
81}
82
83impl RequestCategory {
84    /// True when a request in this category can change repository state. A mutating request is
85    /// single-shot and never auto-retried (design SEAM-04, NFR-S04); a read never mutates.
86    #[must_use]
87    pub const fn mutates(self) -> bool {
88        match self {
89            Self::ReadHistory | Self::ReadState | Self::WorktreeAnalysis | Self::Integrity => false,
90            Self::QueueMutation
91            | Self::Publication
92            | Self::Exchange
93            | Self::Trust
94            | Self::Recovery
95            | Self::Materialization => true,
96        }
97    }
98
99    /// The confirmation tier this category requires (design `FR-121`; RFC 013 decision 4). Every read
100    /// category is [`Tier::One`] (free — RFC 013 decision 6); every mutating category maps to
101    /// [`Tier::Two`], [`Tier::Three`], or [`Tier::ThreeTyped`], beside [`Self::mutates`] for the same
102    /// reason: a new category cannot be added here without a reviewer also deciding its tier.
103    #[must_use]
104    pub const fn tier(self) -> Tier {
105        match self {
106            Self::ReadHistory | Self::ReadState | Self::WorktreeAnalysis | Self::Integrity => {
107                Tier::One
108            }
109            // **Exchange is Tier Two** (RFC 050 decision 2): measured at prikk 0.53, neither
110            // `bundle export` nor `bundle import` signs anything, and an import creates and
111            // advances no local ref -- it stores received history under `remotes/`. `FL-11`
112            // asks for a tier-2 confirmation and no more.
113            Self::QueueMutation | Self::Exchange => Tier::Two,
114            // **Materialization is Tier Three, not ThreeTyped** (RFC 045 decision 3): a switch
115            // refuses rather than overwrite anything not in history, every refusal writes
116            // nothing, and switching back restores every byte (F2, F3). What the user needs is to
117            // see which files change, which the preview gives.
118            Self::Publication | Self::Materialization => Tier::Three,
119            Self::Trust | Self::Recovery => Tier::ThreeTyped,
120        }
121    }
122
123    /// What signing readiness this category's requests need (RFC 045 §2b).
124    ///
125    /// **This is the gate's key, and the tier is not.** Every category keeps exactly the
126    /// requirement its tier implies — `the_requirement_matches_what_the_tier_used_to_imply`
127    /// asserts that over [`Self::ALL`], with [`Self::Materialization`] as the one exception it
128    /// names: Tier Three, and no key, because prikk needs none (RFC 045 F1).
129    /// [`Self::Exchange`] moved the other way in RFC 050 — down to Tier Two, where the implied
130    /// requirement is AUTHOR — so it is the second exception: Tier Two, and **no key at all**,
131    /// because neither an export nor an import signs anything (measured at prikk 0.53).
132    #[must_use]
133    pub const fn signing_requirement(self) -> SigningRequirement {
134        match self {
135            Self::QueueMutation => SigningRequirement::Author,
136            Self::Publication => SigningRequirement::Maintainer,
137            // The reads never reach a gate at all (`Tier::One` short-circuits it), and
138            // `Trust`/`Recovery` are `ThreeTyped`, whose ceremony is the typed name rather than a
139            // key: Operator is orthogonal to the signing ladder (`AC-04`).
140            //
141            // **`Exchange` needs no key** (RFC 050 decision 2): measured at prikk 0.53, an export
142            // and an import both succeed with no key configured at all. An import trusts nobody --
143            // it records the bundle's author-key material for continuity and says so -- so asking
144            // for a MAINTAINER key would have been ceremony over an operation that signs nothing.
145            Self::ReadHistory
146            | Self::ReadState
147            | Self::WorktreeAnalysis
148            | Self::Integrity
149            | Self::Exchange
150            | Self::Trust
151            | Self::Recovery
152            | Self::Materialization => SigningRequirement::ReadWrite,
153        }
154    }
155
156    /// True when a request in this category may be cancelled while in flight. Reads are freely
157    /// cancellable; a mutation is cancellable only *before* it executes (the operation layer's
158    /// preview stage), never mid-write, since prikk's own call is the atomic unit.
159    #[must_use]
160    pub const fn cancellable_in_flight(self) -> bool {
161        !self.mutates()
162    }
163
164    /// The stable machine-readable name, for the report format (design CL-07) and tests.
165    #[must_use]
166    pub const fn name(self) -> &'static str {
167        match self {
168            Self::ReadHistory => "read-history",
169            Self::ReadState => "read-state",
170            Self::WorktreeAnalysis => "worktree-analysis",
171            Self::QueueMutation => "queue-mutation",
172            Self::Publication => "publication",
173            Self::Exchange => "exchange",
174            Self::Integrity => "integrity",
175            Self::Trust => "trust",
176            Self::Recovery => "recovery",
177            Self::Materialization => "materialization",
178        }
179    }
180
181    /// Every category, in declaration order — used by the parity check that no category is dropped.
182    pub const ALL: [Self; 10] = [
183        Self::ReadHistory,
184        Self::ReadState,
185        Self::WorktreeAnalysis,
186        Self::QueueMutation,
187        Self::Publication,
188        Self::Exchange,
189        Self::Integrity,
190        Self::Trust,
191        Self::Recovery,
192        Self::Materialization,
193    ];
194}
195
196#[cfg(test)]
197mod tests;