stemedb/crates/stemedb-api/src/state.rs
jordan d3a88585fe feat: Phase 6 UAT - Admission control, HLC recency, cluster coordination
This commit includes comprehensive work on Phase 6 features:

## Admission Control (Phase 6 admission middleware)
- AdmissionStore implementation backed by TrustRankStore
- PoW verification with tier-based difficulty computation
- Trust tier progression (Newcomer → Established → Trusted → Authority)
- API integration with admission status endpoints

## HLC Recency Lens (Phase 6C)
- HlcRecencyLens for distributed system ordering
- Hybrid logical clock integration with causality preservation

## Cluster Coordination (Phase 6C)
- Multi-node cluster tests (availability, partition tolerance)
- CRDT convergence tests for anti-entropy sync
- Gateway handler improvements

## Aphoria Code Linter (Phase 2A)
- RFC/OWASP corpus builders with network fetching and caching
- Concept hierarchy with auto-alias creation on conflict detection
- Multiple security extractors (TLS, JWT, CORS, secrets, rate limiting)

## Code Organization
- Split large files into modules to comply with 500-line limit
- Improved test organization with separate test modules
- Fixed rkyv serialization for EigenTrustState (AgentScore struct)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-03 00:43:37 -07:00

103 lines
3.6 KiB
Rust

//! Application state shared across all request handlers.
use std::sync::Arc;
use tokio::sync::Mutex;
use stemedb_query::QueryEngine;
use stemedb_storage::{
GenericAdmissionStore, GenericAliasStore, GenericEscalationStore, GenericQuotaStore,
GenericTrustRankStore, HybridStore,
};
use stemedb_wal::group_commit::{GroupCommitBuffer, GroupCommitConfig};
use stemedb_wal::Journal;
/// Quota store type alias for convenience.
pub type QuotaStoreImpl = GenericQuotaStore<Arc<HybridStore>>;
/// Escalation store type alias for convenience.
pub type EscalationStoreImpl = GenericEscalationStore<HybridStore>;
/// Alias store type alias for convenience.
pub type AliasStoreImpl = GenericAliasStore<Arc<HybridStore>>;
/// Trust rank store type alias for convenience.
pub type TrustRankStoreImpl = GenericTrustRankStore<Arc<HybridStore>>;
/// Admission store type alias for convenience.
pub type AdmissionStoreImpl = GenericAdmissionStore<Arc<TrustRankStoreImpl>>;
/// Application state shared across all HTTP handlers.
///
/// This is passed to every request via axum's `State` extractor.
#[derive(Clone)]
pub struct AppState {
/// Group commit buffer for batched WAL writes (used by write handlers)
pub commit_buffer: GroupCommitBuffer,
/// Write-ahead log for reading records (IngestWorker uses this)
pub journal: Arc<Mutex<Journal>>,
/// Key-value store for reading assertions
pub store: Arc<HybridStore>,
/// Quota store for economic throttling (The Meter)
pub quota_store: Arc<QuotaStoreImpl>,
/// Escalation store for high-conflict assertion tracking
pub escalation_store: Arc<EscalationStoreImpl>,
/// Alias store for cross-scheme entity resolution
pub alias_store: Arc<AliasStoreImpl>,
/// Trust rank store for reputation tracking
pub trust_rank_store: Arc<TrustRankStoreImpl>,
/// Admission store for PoW-based admission control (The Shield)
pub admission_store: Arc<AdmissionStoreImpl>,
}
impl AppState {
/// Create a new application state.
///
/// Takes two journals: one for the group commit buffer (writes) and
/// one for reading (used by IngestWorker). Both should be opened on
/// the same directory.
pub fn new(write_journal: Journal, read_journal: Journal, store: Arc<HybridStore>) -> Self {
let commit_buffer = GroupCommitBuffer::new(write_journal, GroupCommitConfig::default());
let journal = Arc::new(Mutex::new(read_journal));
// Create quota store backed by the same KV store
let quota_store = Arc::new(GenericQuotaStore::new(Arc::clone(&store)));
// Create escalation store backed by the same KV store
let escalation_store = Arc::new(GenericEscalationStore::new(Arc::clone(&store)));
// Create alias store for cross-scheme concept resolution
let alias_store = Arc::new(GenericAliasStore::new(Arc::clone(&store)));
// Create trust rank store for reputation tracking
let trust_rank_store = Arc::new(GenericTrustRankStore::new(Arc::clone(&store)));
// Create admission store for PoW-based admission control
let admission_store = Arc::new(GenericAdmissionStore::new(Arc::clone(&trust_rank_store)));
Self {
commit_buffer,
journal,
store,
quota_store,
escalation_store,
alias_store,
trust_rank_store,
admission_store,
}
}
/// Get a QueryEngine for this state.
///
/// Creates a new QueryEngine each time since it cannot be cloned.
pub fn query_engine(&self) -> QueryEngine<HybridStore> {
QueryEngine::new(self.store.clone())
}
}