//! Runtime fail-closed gate for local recovery state. use std::{ collections::HashSet, path::{Path, PathBuf}, sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}, }; #[derive(Clone, Debug)] pub(crate) struct RecoveryQuarantine { inner: Arc>, } #[derive(Debug)] struct RecoveryState { root: PathBuf, status: RecoveryStatus, } #[derive(Debug)] enum RecoveryStatus { Recovering, Settled { failed_ids: HashSet }, } impl RecoveryQuarantine { pub(crate) fn recovering(root: PathBuf) -> Self { Self { inner: Arc::new(RwLock::new(RecoveryState { root, status: RecoveryStatus::Recovering, })), } } pub(crate) fn begin(&self, root: PathBuf) { let mut state = self.write(); state.root = root; state.status = RecoveryStatus::Recovering; } /// Settles the current recovery epoch. A result from a superseded root is /// ignored and leaves the newer root fail-closed. pub(crate) fn settle(&self, root: &Path, failed_ids: HashSet) -> bool { let mut state = self.write(); if state.root != root { return false; } state.status = RecoveryStatus::Settled { failed_ids }; true } pub(crate) fn is_blocked(&self, current_root: &Path, game_id: &str) -> bool { let state = self.read(); if state.root != current_root { return true; } match &state.status { RecoveryStatus::Recovering => true, RecoveryStatus::Settled { failed_ids } => failed_ids.contains(game_id), } } /// Returns the failed IDs for a settled snapshot of this exact root. /// Recovering or mismatched roots deliberately return no projection: the /// runtime gate still blocks every game until the authoritative load /// publishes and settles. pub(crate) fn failed_ids(&self, current_root: &Path) -> HashSet { let state = self.read(); if state.root != current_root { return HashSet::new(); } match &state.status { RecoveryStatus::Recovering => HashSet::new(), RecoveryStatus::Settled { failed_ids } => failed_ids.clone(), } } fn read(&self) -> RwLockReadGuard<'_, RecoveryState> { self.inner .read() .unwrap_or_else(std::sync::PoisonError::into_inner) } fn write(&self) -> RwLockWriteGuard<'_, RecoveryState> { self.inner .write() .unwrap_or_else(std::sync::PoisonError::into_inner) } } #[cfg(test)] mod tests { use super::*; #[test] fn recovering_and_root_mismatch_block_every_game() { let gate = RecoveryQuarantine::recovering(PathBuf::from("/games")); assert!(gate.is_blocked(Path::new("/games"), "healthy")); assert!(gate.is_blocked(Path::new("/other"), "healthy")); } #[test] fn settled_gate_blocks_only_failed_ids_for_exact_root() { let gate = RecoveryQuarantine::recovering(PathBuf::from("/games")); assert!(gate.settle(Path::new("/games"), HashSet::from(["broken".to_string()]))); assert!(gate.is_blocked(Path::new("/games"), "broken")); assert!(!gate.is_blocked(Path::new("/games"), "healthy")); assert!(gate.is_blocked(Path::new("/other"), "healthy")); assert_eq!( gate.failed_ids(Path::new("/games")), HashSet::from(["broken".to_string()]) ); } #[test] fn stale_root_cannot_settle_new_recovery_epoch() { let gate = RecoveryQuarantine::recovering(PathBuf::from("/old")); gate.begin(PathBuf::from("/new")); assert!(!gate.settle(Path::new("/old"), HashSet::new())); assert!(gate.is_blocked(Path::new("/new"), "game")); } }