From 41fa1684de8eb13c5b76c3a3e174fece04ac5afa Mon Sep 17 00:00:00 2001 From: aur-build Date: Wed, 18 Nov 2026 16:49:40 +0000 Subject: db: the review, step, release and subscription states are enums too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit M-8 named four more machines after `build_state`, and each had the same defect in miniature: `&str` constants, a writer that took any `&str`, and a rule that lived only in the caller's discipline. They are `CandidateState`, `StepState`, `ReleaseState` and `SubscriptionState` now, all parsed through one `db::state_col` at the row boundary — a column value the code cannot name fails the read rather than becoming a state it did not ask for. The one that mattered is the review machine. `resolve_in(tx, id, &str)` would move a candidate to any state from any state, so an approval racing a rejection, or a stale worker resolving a review that had already been superseded, silently overwrote what a human had decided — the direct route to breaking I2 and I3 with no symptom anywhere. It is a compare-and-swap on `CandidateState` now: a decided review is only ever frozen, never re-decided, and a refusal names both states. `supersede_in` goes through it, so the freeze cannot resurrect a superseded review either. `StepState` gives I5 teeth. `begin_step` upserted over any existing checkpoint, including one recorded `succeeded`, so a resume that asked to re-run a finished step could rewrite the very row that told it to skip; the upsert now refuses to touch a done checkpoint and `finish_step` is a CAS. The blocked path (`running → pending`) and the reaper's `running → interrupted` are unchanged, and both are in the table. Two behaviour changes, both for the better and both tested: the UI's pause button now says "orphaned: nothing to pause" instead of rewriting an orphaned subscription back to a pausable state, and a step checkpoint that is already done makes `begin_step` an error rather than a quiet overwrite. Five tests in `tests/state_machine.rs` cover the review CAS in both directions, the frozen checkpoint, the interrupted/blocked transitions, and the unknown value in a candidate and a step row. --- src/db/model.rs | 311 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 286 insertions(+), 25 deletions(-) (limited to 'src/db/model.rs') diff --git a/src/db/model.rs b/src/db/model.rs index bf3e017..263db4e 100644 --- a/src/db/model.rs +++ b/src/db/model.rs @@ -92,13 +92,83 @@ impl Identity { } } -/// Candidate/review states (§7.1). -pub mod candidate_state { - pub const PENDING: &str = "pending_review"; - pub const APPROVED: &str = "approved"; - pub const REJECTED: &str = "rejected"; - pub const SUPERSEDED: &str = "superseded"; - pub const BLOCKED: &str = "blocked"; +/// The review state machine (§7.1). +/// +/// A candidate is what a human decides on, and the decision is the one thing in +/// this system that must never be overwritten by accident: an approval that +/// silently replaced a rejection, or a superseded review resurrected, would +/// break I2 and I3 without any symptom. So the states are an enum, and +/// [`crate::db::candidates::resolve_in`] refuses a move the machine forbids. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum CandidateState { + /// Open: nobody has decided yet. The only state a reviewer can act on. + PendingReview, + Approved, + Rejected, + /// Frozen: a newer review of the same pkgbase took its place (§7.1). + Superseded, + /// Not approved because something it needs is missing (§10.4). + Blocked, +} + +impl CandidateState { + pub const ALL: &'static [CandidateState] = &[ + CandidateState::PendingReview, + CandidateState::Approved, + CandidateState::Rejected, + CandidateState::Superseded, + CandidateState::Blocked, + ]; + + pub fn as_str(&self) -> &'static str { + match self { + CandidateState::PendingReview => "pending_review", + CandidateState::Approved => "approved", + CandidateState::Rejected => "rejected", + CandidateState::Superseded => "superseded", + CandidateState::Blocked => "blocked", + } + } + + pub fn parse(s: &str) -> Option { + CandidateState::ALL.iter().copied().find(|c| c.as_str() == s) + } + + /// The state a reviewer can still act on. + pub fn is_open(&self) -> bool { + *self == CandidateState::PendingReview + } + + /// May a candidate in this state be written to `to`? Re-writing the state it + /// already has is a no-op, not a move. + pub fn can_transition(&self, to: CandidateState) -> bool { + use CandidateState::*; + if *self == to { + return true; + } + match self { + PendingReview => matches!(to, Approved | Rejected | Superseded | Blocked), + // A decided review is only ever frozen, never re-decided (§7.1). + Approved | Rejected => to == Superseded, + Blocked => matches!(to, PendingReview | Approved | Rejected), + Superseded => false, + } + } + + pub fn predecessors(to: CandidateState) -> Vec<&'static str> { + CandidateState::ALL + .iter() + .filter(|from| from.can_transition(to)) + .map(|from| from.as_str()) + .collect() + } +} + +impl std::fmt::Display for CandidateState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.as_str()) + } } pub mod candidate_kind { @@ -106,12 +176,87 @@ pub mod candidate_kind { pub const FIRST_INTRODUCTION: &str = "first_introduction"; } -pub mod release_state { - pub const PUBLISHED: &str = "published"; - pub const WITHDRAWN: &str = "withdrawn"; - pub const FAILED_UNBUILT: &str = "failed_unbuilt"; +/// The release state machine (§9). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum ReleaseState { + /// In the served index. + Published, + /// Taken out of the index (§9 withdrawal); the row stays (§15). + Withdrawn, + /// Approved, but the build never produced it: the old version still serves. + FailedUnbuilt, +} + +impl ReleaseState { + pub const ALL: &'static [ReleaseState] = &[ + ReleaseState::Published, + ReleaseState::Withdrawn, + ReleaseState::FailedUnbuilt, + ]; + + pub fn as_str(&self) -> &'static str { + match self { + ReleaseState::Published => "published", + ReleaseState::Withdrawn => "withdrawn", + ReleaseState::FailedUnbuilt => "failed_unbuilt", + } + } + + pub fn parse(s: &str) -> Option { + ReleaseState::ALL.iter().copied().find(|r| r.as_str() == s) + } + + /// Withdrawal is the only move a release makes on its own; a failed-unbuilt + /// release is a record of an attempt, and a published one is live. + pub fn can_transition(&self, to: ReleaseState) -> bool { + use ReleaseState::*; + if *self == to { + return true; + } + match self { + Published => matches!(to, Withdrawn | FailedUnbuilt), + Withdrawn | FailedUnbuilt => false, + } + } +} + +impl std::fmt::Display for ReleaseState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.as_str()) + } +} + +/// What the state enums below have in common: a closed set of column values, +/// parsed where a row is read. `db::state_col` uses this to fail a row whose +/// state the code does not know instead of defaulting it. +pub trait StateEnum: Copy + Sized { + /// The name of the machine, for the error message. + const KIND: &'static str; + fn parse(text: &str) -> Option; + fn as_str(&self) -> &'static str; +} + +macro_rules! impl_state_enum { + ($t:ty, $kind:literal) => { + impl StateEnum for $t { + const KIND: &'static str = $kind; + fn parse(text: &str) -> Option { + <$t>::parse(text) + } + fn as_str(&self) -> &'static str { + <$t>::as_str(self) + } + } + }; } +impl_state_enum!(BuildState, "build"); +impl_state_enum!(CandidateState, "candidate"); +impl_state_enum!(StepState, "build step"); +impl_state_enum!(ReleaseState, "release"); +impl_state_enum!(SubscriptionState, "subscription"); + /// The build state machine (§7.1, §8.2). /// /// This used to be seven `&str` constants and a `set_state(&str)` that wrote any @@ -238,13 +383,82 @@ impl std::fmt::Display for BuildState { } } -pub mod step_state { - pub const PENDING: &str = "pending"; - pub const RUNNING: &str = "running"; - pub const SUCCEEDED: &str = "succeeded"; - pub const FAILED: &str = "failed"; - pub const INTERRUPTED: &str = "interrupted"; - pub const SKIPPED: &str = "skipped"; +/// The step checkpoint state machine (§8.3, I5). +/// +/// The checkpoint row is what a resumed build reads to decide what not to redo, +/// so the rule that matters is that a completed checkpoint is frozen: a step +/// recorded `succeeded` or `skipped` is never re-run and never rewritten. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum StepState { + Pending, + Running, + Succeeded, + Failed, + /// Killed mid-step; the reaper writes this so the resume re-runs it (§8.2). + Interrupted, + Skipped, +} + +impl StepState { + pub const ALL: &'static [StepState] = &[ + StepState::Pending, + StepState::Running, + StepState::Succeeded, + StepState::Failed, + StepState::Interrupted, + StepState::Skipped, + ]; + + pub fn as_str(&self) -> &'static str { + match self { + StepState::Pending => "pending", + StepState::Running => "running", + StepState::Succeeded => "succeeded", + StepState::Failed => "failed", + StepState::Interrupted => "interrupted", + StepState::Skipped => "skipped", + } + } + + pub fn parse(text: &str) -> Option { + StepState::ALL.iter().copied().find(|st| st.as_str() == text) + } + + /// I5: the resume skips a step in one of these two states, so nothing may + /// move it again — a checkpoint that can be rewritten is not a checkpoint. + pub fn is_done(&self) -> bool { + matches!(self, StepState::Succeeded | StepState::Skipped) + } + + pub fn can_transition(&self, to: StepState) -> bool { + use StepState::*; + if *self == to { + return true; + } + match self { + Pending => matches!(to, Running | Succeeded | Failed | Interrupted | Skipped), + // `Running → Pending` is the blocked path: the step did not happen, + // so the checkpoint goes back to waiting. + Running => matches!(to, Pending | Succeeded | Failed | Interrupted | Skipped), + Failed | Interrupted => matches!(to, Pending | Running), + Succeeded | Skipped => false, + } + } + + pub fn predecessors(to: StepState) -> Vec<&'static str> { + StepState::ALL + .iter() + .filter(|from| from.can_transition(to)) + .map(|from| from.as_str()) + .collect() + } +} + +impl std::fmt::Display for StepState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.as_str()) + } } pub mod notice_kind { @@ -259,11 +473,58 @@ pub mod notice_kind { pub const DOWNGRADE: &str = "downgrade"; } -pub mod subscription_state { - pub const ACTIVE: &str = "active"; - pub const PAUSED: &str = "paused"; - pub const ORPHANED: &str = "orphaned"; - pub const REMOVED: &str = "removed"; +/// Whether a pkgbase is being watched (§4.1, §12). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum SubscriptionState { + Active, + Paused, + /// The pkgbase left AUR; the watcher stopped looking (§12). + Orphaned, + Removed, +} + +impl SubscriptionState { + pub const ALL: &'static [SubscriptionState] = &[ + SubscriptionState::Active, + SubscriptionState::Paused, + SubscriptionState::Orphaned, + SubscriptionState::Removed, + ]; + + pub fn as_str(&self) -> &'static str { + match self { + SubscriptionState::Active => "active", + SubscriptionState::Paused => "paused", + SubscriptionState::Orphaned => "orphaned", + SubscriptionState::Removed => "removed", + } + } + + pub fn parse(text: &str) -> Option { + SubscriptionState::ALL.iter().copied().find(|st| st.as_str() == text) + } + + /// Pause and resume are the only moves the UI makes; orphaned and removed + /// are facts about the pkgbase, reported once. + pub fn can_transition(&self, to: SubscriptionState) -> bool { + use SubscriptionState::*; + if *self == to { + return true; + } + match self { + Active => matches!(to, Paused | Orphaned | Removed), + Paused => matches!(to, Active | Orphaned | Removed), + Orphaned => matches!(to, Active | Removed), + Removed => false, + } + } +} + +impl std::fmt::Display for SubscriptionState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.as_str()) + } } /// The pipeline steps (§8.1). Order matters: a build resumes at the first step @@ -369,7 +630,7 @@ pub struct ReleaseRow { pub pkgver: String, pub pkgrel: String, pub world_snapshot_id: Option, - pub state: String, + pub state: ReleaseState, pub published_at: Option, } @@ -404,7 +665,7 @@ pub struct CandidateRow { pub risk_level: String, pub risk_signals_json: String, pub missing_deps_json: String, - pub state: String, + pub state: CandidateState, pub detected_at: String, pub resolved_at: Option, pub pkgbase: String, -- cgit v1.2.3