Skip to main content

hyprstream_core/server/
state.rs

1//! Server state management
2
3use crate::services::oauth::replay_key::ReplayKey;
4use ed25519_dalek::{SigningKey, VerifyingKey};
5use hyprstream_rpc_std::model_client::{LoadModelRequest, ModelClient};
6use hyprstream_rpc_std::policy_client::PolicyClient;
7use hyprstream_rpc_std::registry_client::RegistryClient;
8use hyprstream_util::TtlCache;
9use std::collections::HashMap;
10use std::sync::Arc;
11use tokio::sync::RwLock;
12
13/// 1,000 sustained proofs or mount tickets/s for the longest (five-minute)
14/// replay window, plus 20% headroom. Ordinary DPoP proofs expire after 120s.
15/// Fixed BLAKE3-256 digest keys at 128 bytes/live entry plan this at about
16/// 43.9 MiB before allocator slack.
17const DPOP_JTI_MAX_ENTRIES: usize = 360_000;
18const DPOP_JTI_REAP_BUDGET: usize = 64;
19
20// Re-export config types so other server modules can access them via server::state
21pub use crate::config::{CorsConfig, SamplingParamDefaults, ServerConfig};
22
23// Re-export context storage types
24#[cfg(feature = "metrics")]
25pub use hyprstream_metrics::storage::context::{ContextRecord, ContextStore, SearchResult};
26
27/// Shared server state
28#[derive(Clone)]
29pub struct ServerState {
30    /// Model client for inference operations via ZMQ
31    pub model_client: ModelClient,
32
33    /// Policy client for authorization checks via ZMQ
34    pub policy_client: PolicyClient,
35
36    /// Registry client for model operations
37    pub registry: RegistryClient,
38
39    /// Server configuration (from unified config system)
40    pub config: Arc<ServerConfig>,
41
42    /// Metrics collector
43    pub metrics: Arc<Metrics>,
44
45    /// Ed25519 signing key for creating JWT tokens
46    pub signing_key: Arc<SigningKey>,
47
48    /// Ed25519 verifying key for validating JWT tokens (derived from signing_key).
49    ///
50    /// This is the cluster CA key. Locally-issued at+JWTs may instead be signed by
51    /// a rotation slot (see `published_jwt_verifying_keys`), so token validation
52    /// tries this key AND the published rotation keys.
53    pub verifying_key: Arc<VerifyingKey>,
54
55    /// Live, shared handle to the node's published Ed25519 rotation-slot verifying
56    /// keys — the SAME set the `/oauth/jwks` endpoint publishes. Locally-issued
57    /// tokens signed by the OAuth rotation active/lead/drain slots (WIT, S6 grant /
58    /// token-exchange, WITs) verify against these in addition to `verifying_key`.
59    /// Only node-published public keys are ever admitted here (never token-supplied
60    /// keys). Populated + kept current by the key-rotation task.
61    pub published_jwt_verifying_keys: crate::auth::key_rotation::PublishedEd25519Keys,
62
63    /// Context store for RAG/CAG (optional)
64    #[cfg(feature = "metrics")]
65    pub context_store:
66        Option<Arc<ContextStore<hyprstream_metrics::storage::duckdb::DuckDbBackend>>>,
67
68    /// Cached resource URL for WWW-Authenticate headers (avoids per-request config reload)
69    pub resource_url: String,
70
71    /// OAuth issuer URL for local tokens (matches the `iss` claim in locally-issued JWTs).
72    pub oauth_issuer_url: String,
73
74    /// Federation key resolver for multi-issuer JWT verification.
75    /// Contains trusted issuers (empty if none configured).
76    pub federation_resolver: Arc<crate::auth::FederationKeyResolver>,
77
78    /// Per-request DPoP JTI dedup cache (RFC 9449 §11.1 replay prevention).
79    /// Backed by the shared `TtlCache` with atomic check-and-record
80    /// (`insert_if_absent_no_evict`); TTL = iat + 120s; fail-closed at capacity.
81    pub dpop_jti_seen: Arc<TtlCache<ReplayKey, ()>>,
82
83    /// Per-subject request rate limiter (fixed window, 300 req/60s default).
84    pub rate_limiter: Arc<crate::server::middleware::RateLimiter>,
85
86    /// Dedicated global limiter for public browser provisioning. This executes
87    /// before checkpoint resolution and hybrid projection signing.
88    pub browser_provisioning_rate_limiter: Arc<crate::server::middleware::RateLimiter>,
89
90    /// Stable global limiter for public metadata and pre-upgrade routes.
91    ///
92    /// There is no authenticated tenant on these routes, and client-controlled
93    /// forwarding headers must not select independent buckets.
94    pub public_route_rate_limiter: Arc<crate::server::middleware::RateLimiter>,
95
96    /// Mandatory resolver-backed, audited PEP shared by every 9P transport.
97    pub ninep_decider: Arc<dyn hyprstream_9p::AccessDecider>,
98
99    /// Armed only for the opt-in holder-scoped canonical 9P read export.
100    pub ninep_vfs_pep: Option<Arc<hyprstream_vfs::NamespacePep>>,
101}
102
103/// Security state shared by every authenticated HTTP resource face.
104///
105/// Keeping this separate from [`ServerState`] lets thin faces such as Xet use
106/// the exact OAI authentication and rate-limit chain without constructing
107/// inference clients they never call.
108#[derive(Clone)]
109pub struct ResourceAuthState {
110    pub verifying_key: Arc<VerifyingKey>,
111    pub published_jwt_verifying_keys: crate::auth::key_rotation::PublishedEd25519Keys,
112    pub composite_key_set: Arc<hyprstream_rpc::auth::CompositeKeySet>,
113    pub resource_url: String,
114    pub oauth_issuer_url: String,
115    pub federation_resolver: Arc<dyn hyprstream_rpc::auth::FederationKeySource>,
116    pub dpop_jti_seen: Arc<TtlCache<ReplayKey, ()>>,
117    pub rate_limiter: Arc<crate::server::middleware::RateLimiter>,
118}
119
120impl ResourceAuthState {
121    pub fn new(
122        verifying_key: VerifyingKey,
123        resource_url: String,
124        oauth_issuer_url: String,
125        federation_resolver: Arc<dyn hyprstream_rpc::auth::FederationKeySource>,
126    ) -> Self {
127        Self {
128            verifying_key: Arc::new(verifying_key),
129            published_jwt_verifying_keys: crate::auth::key_rotation::global_ed25519_verifying_keys(
130            ),
131            composite_key_set: hyprstream_rpc::auth::global_composite_key_set(),
132            resource_url,
133            oauth_issuer_url,
134            federation_resolver,
135            dpop_jti_seen: Arc::new(TtlCache::new(DPOP_JTI_MAX_ENTRIES, DPOP_JTI_REAP_BUDGET)),
136            rate_limiter: Arc::new(crate::server::middleware::RateLimiter::new(300, 60)),
137        }
138    }
139}
140
141/// Metrics collector
142pub struct Metrics {
143    /// Total requests processed
144    pub total_requests: Arc<std::sync::atomic::AtomicU64>,
145
146    /// Total tokens generated
147    pub total_tokens: Arc<std::sync::atomic::AtomicU64>,
148
149    /// Average latency in milliseconds
150    pub avg_latency_ms: Arc<RwLock<f64>>,
151
152    /// Active requests
153    pub active_requests: Arc<std::sync::atomic::AtomicU32>,
154}
155
156impl Default for Metrics {
157    fn default() -> Self {
158        Self {
159            total_requests: Arc::new(std::sync::atomic::AtomicU64::new(0)),
160            total_tokens: Arc::new(std::sync::atomic::AtomicU64::new(0)),
161            avg_latency_ms: Arc::new(RwLock::new(0.0)),
162            active_requests: Arc::new(std::sync::atomic::AtomicU32::new(0)),
163        }
164    }
165}
166
167impl ServerState {
168    /// Return the narrow state consumed by the shared HTTP authentication core.
169    pub fn resource_auth_state(&self) -> ResourceAuthState {
170        ResourceAuthState {
171            verifying_key: Arc::clone(&self.verifying_key),
172            published_jwt_verifying_keys: Arc::clone(&self.published_jwt_verifying_keys),
173            composite_key_set: hyprstream_rpc::auth::global_composite_key_set(),
174            resource_url: self.resource_url.clone(),
175            oauth_issuer_url: self.oauth_issuer_url.clone(),
176            federation_resolver: self.federation_resolver.clone(),
177            dpop_jti_seen: Arc::clone(&self.dpop_jti_seen),
178            rate_limiter: Arc::clone(&self.rate_limiter),
179        }
180    }
181
182    /// Create a new server state with ZMQ clients
183    ///
184    /// This is the primary method for creating server state with ZMQ-based services.
185    /// The model_client and policy_client should be created after starting their
186    /// respective services in main.rs.
187    pub async fn new(
188        config: ServerConfig,
189        model_client: ModelClient,
190        policy_client: PolicyClient,
191        registry: RegistryClient,
192        signing_key: SigningKey,
193        jwt_verifying_key: VerifyingKey,
194        resource_url: String,
195        oauth_issuer_url: String,
196        trusted_issuers: &HashMap<String, crate::config::TrustedIssuerConfig>,
197        ninep_decider: Arc<dyn hyprstream_9p::AccessDecider>,
198        ninep_vfs_pep: Option<Arc<hyprstream_vfs::NamespacePep>>,
199    ) -> Result<Self, anyhow::Error> {
200        if let Some(export) = &config.ninep_canonical_read {
201            export.validate()?;
202            anyhow::ensure!(
203                ninep_vfs_pep.is_some(),
204                "canonical 9P export needs armed VFS PEP"
205            );
206        }
207        let signing_key = Arc::new(signing_key);
208        // Use the CA JWT verifying key (not the service's own key) so HTTP Bearer tokens
209        // issued by PolicyService can be verified correctly.
210        let verifying_key = Arc::new(jwt_verifying_key);
211
212        // Live handle to the node's published Ed25519 rotation-slot verifying keys.
213        // Shared with the OAuth key-rotation task so tokens signed by the active
214        // rotation slot (not the CA key) still validate here.
215        let published_jwt_verifying_keys =
216            crate::auth::key_rotation::global_ed25519_verifying_keys();
217
218        // Preload models for faster first request
219        if !config.preload_models.is_empty() {
220            tracing::info!("Preloading {} models", config.preload_models.len());
221            for model_name in &config.preload_models {
222                tracing::info!("Preloading model: {}", model_name);
223                match model_client
224                    .load(&LoadModelRequest {
225                        model_ref: model_name.to_owned(),
226                        max_context: None,
227                        kv_quant: None,
228                    })
229                    .await
230                {
231                    Ok(_) => tracing::info!("Preloaded: {}", model_name),
232                    Err(e) => tracing::warn!("Failed to preload model '{}': {}", model_name, e),
233                }
234            }
235        }
236
237        // Initialize metrics
238        let metrics = Arc::new(Metrics::default());
239
240        // Wire the unified federation:register PolicyService gate. Same
241        // gate as CIMD client registration — single atproto-style
242        // trust decision applies to both clients and peers.
243        let federation_resolver = Arc::new(
244            crate::auth::FederationKeyResolver::new(trusted_issuers)
245                .with_policy_client(Arc::new(policy_client.clone())),
246        );
247
248        Ok(Self {
249            model_client,
250            policy_client,
251            registry,
252            config: Arc::new(config),
253            metrics,
254            signing_key,
255            verifying_key,
256            published_jwt_verifying_keys,
257            #[cfg(feature = "metrics")]
258            context_store: None, // Initialize via enable_context_store() if needed
259            resource_url,
260            oauth_issuer_url,
261            federation_resolver,
262            dpop_jti_seen: Arc::new(TtlCache::new(DPOP_JTI_MAX_ENTRIES, DPOP_JTI_REAP_BUDGET)),
263            rate_limiter: Arc::new(crate::server::middleware::RateLimiter::new(300, 60)),
264            browser_provisioning_rate_limiter: Arc::new(
265                crate::server::middleware::RateLimiter::new(60, 60),
266            ),
267            public_route_rate_limiter: Arc::new(crate::server::middleware::RateLimiter::new(
268                3_000, 60,
269            )),
270            ninep_decider,
271            ninep_vfs_pep,
272        })
273    }
274
275    /// Get server metrics
276    pub async fn get_metrics(&self) -> serde_json::Value {
277        serde_json::json!({
278            "total_requests": self.metrics.total_requests.load(std::sync::atomic::Ordering::Relaxed),
279            "total_tokens": self.metrics.total_tokens.load(std::sync::atomic::Ordering::Relaxed),
280            "avg_latency_ms": *self.metrics.avg_latency_ms.read().await,
281            "active_requests": self.metrics.active_requests.load(std::sync::atomic::Ordering::Relaxed),
282        })
283    }
284
285    /// Enable context storage for RAG/CAG functionality.
286    ///
287    /// This initializes a context store using DuckDB for embedding storage.
288    /// Call this during server initialization if RAG/CAG is needed.
289    ///
290    /// # Arguments
291    /// - `db_path` - Path to DuckDB database (use ":memory:" for in-memory)
292    /// - `embedding_dim` - Dimension of embeddings (e.g., 768 for many models)
293    ///
294    /// # Returns
295    /// A mutable reference to the initialized ContextStore
296    #[cfg(feature = "metrics")]
297    pub async fn enable_context_store(
298        &mut self,
299        db_path: &str,
300        embedding_dim: i32,
301    ) -> Result<(), anyhow::Error> {
302        use hyprstream_metrics::storage::context::{context_schema, ContextStore};
303        use hyprstream_metrics::storage::duckdb::DuckDbBackend;
304        use hyprstream_metrics::StorageBackend;
305
306        let backend = Arc::new(DuckDbBackend::new(
307            db_path.to_owned(),
308            std::collections::HashMap::new(),
309            None,
310        )?);
311        backend
312            .init()
313            .await
314            .map_err(|e| anyhow::anyhow!("Failed to init DuckDB: {}", e))?;
315        backend
316            .create_table("context", &context_schema(embedding_dim))
317            .await
318            .map_err(|e| anyhow::anyhow!("Failed to create context table: {}", e))?;
319
320        let store = ContextStore::new(backend, "context", embedding_dim);
321        self.context_store = Some(Arc::new(store));
322
323        tracing::info!(
324            "Context store enabled with embedding_dim={} at {}",
325            embedding_dim,
326            db_path
327        );
328
329        Ok(())
330    }
331
332    /// Get the context store if initialized
333    #[cfg(feature = "metrics")]
334    pub fn get_context_store(
335        &self,
336    ) -> Option<&Arc<ContextStore<hyprstream_metrics::storage::duckdb::DuckDbBackend>>> {
337        self.context_store.as_ref()
338    }
339}
340
341// Config types are already imported at the top, no need to re-export here