axon-lang 1.21.1

AXON v1.5.1 — first crates.io publication of the AXON language full-stack runtime. Lexer/parser/type-checker/IR generator (re-exported from axon-frontend) plus the native Rust runtime: typed channels (TypedEventBus with QoS×5, π-calculus mobility, capability extrusion via shield D8 — Fase 13.f.2), Free Monad CPS handlers (Fase 2), lease kernel + reconcile loop (Fase 3+5), Epistemic Security Kernel (ESK Fase 6), Trust Types + ReplayLog (Fase 11.a+11.c), Stateful PEM over WebSocket (Fase 11.d), Ontological Tool Synthesis (Fase 11.e), Mobile Typed Channels (Fase 13). Crate publishes as `axon-lang` to mirror the Python PyPI package; library import remains `use axon::*` so existing call sites keep working unchanged.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Request Middleware — automatic request ID, timing, and logging for AxonServer.
//!
//! Provides an axum middleware layer that intercepts every request to:
//!   - Generate a unique sequential request ID (`X-Request-Id` header)
//!   - Time the request duration (start → response)
//!   - Auto-record to the RequestLogger (method, path, status, latency, client key)
//!   - Tag slow requests above a configurable threshold
//!
//! This replaces manual `request_logger.record()` calls in individual handlers
//! and provides consistent observability across all endpoints.
//!
//! Configuration:
//!   - `MiddlewareConfig` — enabled flag, slow request threshold
//!   - `GET /v1/middleware` — view current middleware configuration
//!   - `PUT /v1/middleware` — update middleware settings at runtime

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Instant;

use axum::body::Body;
use axum::extract::Request;
use axum::http::HeaderValue;
use axum::middleware::Next;
use axum::response::Response;
use serde::{Deserialize, Serialize};

// ── Request ID generator ────────────────────────────────────────────────

/// Atomic sequential request ID generator.
///
/// Produces unique IDs of the form `axr-{counter}` for each request.
/// Counter is monotonically increasing and never resets during server lifetime.
pub struct RequestIdGenerator {
    counter: AtomicU64,
    prefix: String,
}

impl RequestIdGenerator {
    /// Create a new generator with the default prefix "axr".
    pub fn new() -> Self {
        RequestIdGenerator {
            counter: AtomicU64::new(0),
            prefix: "axr".to_string(),
        }
    }

    /// Create a generator with a custom prefix.
    pub fn with_prefix(prefix: &str) -> Self {
        RequestIdGenerator {
            counter: AtomicU64::new(0),
            prefix: prefix.to_string(),
        }
    }

    /// Generate the next request ID.
    pub fn next_id(&self) -> String {
        let n = self.counter.fetch_add(1, Ordering::Relaxed);
        format!("{}-{}", self.prefix, n)
    }

    /// Current counter value (number of IDs generated).
    pub fn count(&self) -> u64 {
        self.counter.load(Ordering::Relaxed)
    }
}

impl Default for RequestIdGenerator {
    fn default() -> Self {
        Self::new()
    }
}

// ── Middleware configuration ─────────────────────────────────────────────

/// Configuration for the request middleware layer.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MiddlewareConfig {
    /// Whether the middleware is enabled.
    pub enabled: bool,
    /// Slow request threshold in milliseconds. Requests exceeding this
    /// are tagged in the log. 0 = disabled.
    pub slow_threshold_ms: u64,
    /// Whether to inject X-Request-Id response header.
    pub inject_request_id: bool,
    /// Whether to inject X-Response-Time header (latency in ms).
    pub inject_response_time: bool,
}

impl Default for MiddlewareConfig {
    fn default() -> Self {
        MiddlewareConfig {
            enabled: true,
            slow_threshold_ms: 5000,
            inject_request_id: true,
            inject_response_time: true,
        }
    }
}

impl MiddlewareConfig {
    /// Disabled middleware — passes through without recording.
    pub fn disabled() -> Self {
        MiddlewareConfig {
            enabled: false,
            slow_threshold_ms: 0,
            inject_request_id: false,
            inject_response_time: false,
        }
    }
}

// ── Middleware update ────────────────────────────────────────────────────

/// Partial update for middleware configuration.
#[derive(Debug, Clone, Deserialize)]
pub struct MiddlewareUpdate {
    pub enabled: Option<bool>,
    pub slow_threshold_ms: Option<u64>,
    pub inject_request_id: Option<bool>,
    pub inject_response_time: Option<bool>,
}

/// Apply a partial update to a MiddlewareConfig. Returns list of changed fields.
pub fn apply_update(config: &mut MiddlewareConfig, update: &MiddlewareUpdate) -> Vec<String> {
    let mut changes = Vec::new();

    if let Some(enabled) = update.enabled {
        if enabled != config.enabled {
            config.enabled = enabled;
            changes.push("enabled".to_string());
        }
    }
    if let Some(threshold) = update.slow_threshold_ms {
        if threshold != config.slow_threshold_ms {
            config.slow_threshold_ms = threshold;
            changes.push("slow_threshold_ms".to_string());
        }
    }
    if let Some(inject_id) = update.inject_request_id {
        if inject_id != config.inject_request_id {
            config.inject_request_id = inject_id;
            changes.push("inject_request_id".to_string());
        }
    }
    if let Some(inject_time) = update.inject_response_time {
        if inject_time != config.inject_response_time {
            config.inject_response_time = inject_time;
            changes.push("inject_response_time".to_string());
        }
    }

    changes
}

// ── Request metadata ────────────────────────────────────────────────────

/// Metadata captured for a single request by the middleware.
#[derive(Debug, Clone, Serialize)]
pub struct RequestMeta {
    /// Unique request ID (e.g., "axr-42").
    pub request_id: String,
    /// HTTP method.
    pub method: String,
    /// Request path.
    pub path: String,
    /// Response status code.
    pub status: u16,
    /// Latency in microseconds.
    pub latency_us: u64,
    /// Latency in milliseconds (convenience).
    pub latency_ms: u64,
    /// Client identifier.
    pub client_key: String,
    /// Whether this was flagged as a slow request.
    pub slow: bool,
}

// ── Middleware state ─────────────────────────────────────────────────────

/// Shared state for the request middleware, held in an Arc for cloning.
pub struct MiddlewareState<S> {
    pub id_generator: RequestIdGenerator,
    pub config: Arc<Mutex<MiddlewareConfig>>,
    pub server_state: Arc<Mutex<S>>,
}

// ── Helper: extract client key from headers ─────────────────────────────

fn client_key_from_headers(headers: &axum::http::HeaderMap) -> String {
    headers
        .get("authorization")
        .and_then(|v| v.to_str().ok())
        .map(|v| v.to_string())
        .unwrap_or_else(|| "anonymous".to_string())
}

// ── Core middleware function ─────────────────────────────────────────────

/// The request middleware function for use with `axum::middleware::from_fn`.
///
/// Extracts method/path/client, generates request ID, times the request,
/// records to the RequestLogger, and injects response headers.
///
/// This is designed to be used with `axum::middleware::from_fn` in the
/// router setup. The ServerState access is done via the shared state
/// that axum provides.
pub async fn request_middleware_fn(
    state: axum::extract::State<Arc<Mutex<crate::axon_server::ServerState>>>,
    request: Request<Body>,
    next: Next,
) -> Response {
    let start = Instant::now();

    // Extract request info before passing to handler
    let method = request.method().to_string();
    let path = request.uri().path().to_string();
    let client_key = client_key_from_headers(request.headers());

    // Read config and generate ID
    let (enabled, slow_threshold_ms, inject_id, inject_time, request_id) = {
        let s = state.lock().unwrap();
        let cfg = &s.middleware_config;
        let id = s.request_id_gen.next_id();
        (cfg.enabled, cfg.slow_threshold_ms, cfg.inject_request_id, cfg.inject_response_time, id)
    };

    // Call the actual handler
    let mut response = next.run(request).await;

    if !enabled {
        return response;
    }

    // Compute latency
    let elapsed = start.elapsed();
    let _latency_us = elapsed.as_micros() as u64;
    let latency_ms = elapsed.as_millis() as u64;
    let status = response.status().as_u16();
    let _slow = slow_threshold_ms > 0 && latency_ms >= slow_threshold_ms;

    // Record to request logger
    {
        let mut s = state.lock().unwrap();
        s.request_logger.record(&method, &path, status, elapsed, &client_key);
    }

    // Inject response headers
    if inject_id {
        if let Ok(val) = HeaderValue::from_str(&request_id) {
            response.headers_mut().insert("x-request-id", val);
        }
    }
    if inject_time {
        if let Ok(val) = HeaderValue::from_str(&format!("{}ms", latency_ms)) {
            response.headers_mut().insert("x-response-time", val);
        }
    }

    response
}

// ── Stats ───────────────────────────────────────────────────────────────

/// Middleware statistics snapshot.
#[derive(Debug, Clone, Serialize)]
pub struct MiddlewareStats {
    /// Total requests processed by the middleware.
    pub total_requests: u64,
    /// Current configuration.
    pub config: MiddlewareConfig,
}

// ── Tests ────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn request_id_generator_sequential() {
        let gen = RequestIdGenerator::new();
        assert_eq!(gen.next_id(), "axr-0");
        assert_eq!(gen.next_id(), "axr-1");
        assert_eq!(gen.next_id(), "axr-2");
        assert_eq!(gen.count(), 3);
    }

    #[test]
    fn request_id_generator_custom_prefix() {
        let gen = RequestIdGenerator::with_prefix("req");
        assert_eq!(gen.next_id(), "req-0");
        assert_eq!(gen.next_id(), "req-1");
    }

    #[test]
    fn request_id_generator_default() {
        let gen = RequestIdGenerator::default();
        assert_eq!(gen.next_id(), "axr-0");
    }

    #[test]
    fn default_config() {
        let cfg = MiddlewareConfig::default();
        assert!(cfg.enabled);
        assert_eq!(cfg.slow_threshold_ms, 5000);
        assert!(cfg.inject_request_id);
        assert!(cfg.inject_response_time);
    }

    #[test]
    fn disabled_config() {
        let cfg = MiddlewareConfig::disabled();
        assert!(!cfg.enabled);
        assert_eq!(cfg.slow_threshold_ms, 0);
        assert!(!cfg.inject_request_id);
        assert!(!cfg.inject_response_time);
    }

    #[test]
    fn config_serializable() {
        let cfg = MiddlewareConfig::default();
        let json = serde_json::to_value(&cfg).unwrap();
        assert_eq!(json["enabled"], true);
        assert_eq!(json["slow_threshold_ms"], 5000);
        assert_eq!(json["inject_request_id"], true);
        assert_eq!(json["inject_response_time"], true);
    }

    #[test]
    fn config_deserializable() {
        let json = serde_json::json!({
            "enabled": false,
            "slow_threshold_ms": 1000,
            "inject_request_id": false,
            "inject_response_time": true,
        });
        let cfg: MiddlewareConfig = serde_json::from_value(json).unwrap();
        assert!(!cfg.enabled);
        assert_eq!(cfg.slow_threshold_ms, 1000);
        assert!(!cfg.inject_request_id);
        assert!(cfg.inject_response_time);
    }

    #[test]
    fn apply_update_changes_tracked() {
        let mut cfg = MiddlewareConfig::default();
        let update = MiddlewareUpdate {
            enabled: None,
            slow_threshold_ms: Some(2000),
            inject_request_id: Some(false),
            inject_response_time: None,
        };
        let changes = apply_update(&mut cfg, &update);
        assert_eq!(changes.len(), 2);
        assert!(changes.contains(&"slow_threshold_ms".to_string()));
        assert!(changes.contains(&"inject_request_id".to_string()));
        assert_eq!(cfg.slow_threshold_ms, 2000);
        assert!(!cfg.inject_request_id);
    }

    #[test]
    fn apply_update_no_op_when_same() {
        let mut cfg = MiddlewareConfig::default();
        let update = MiddlewareUpdate {
            enabled: Some(true),
            slow_threshold_ms: Some(5000),
            inject_request_id: Some(true),
            inject_response_time: Some(true),
        };
        let changes = apply_update(&mut cfg, &update);
        assert!(changes.is_empty());
    }

    #[test]
    fn apply_update_all_fields() {
        let mut cfg = MiddlewareConfig::default();
        let update = MiddlewareUpdate {
            enabled: Some(false),
            slow_threshold_ms: Some(100),
            inject_request_id: Some(false),
            inject_response_time: Some(false),
        };
        let changes = apply_update(&mut cfg, &update);
        assert_eq!(changes.len(), 4);
        assert!(!cfg.enabled);
        assert_eq!(cfg.slow_threshold_ms, 100);
        assert!(!cfg.inject_request_id);
        assert!(!cfg.inject_response_time);
    }

    #[test]
    fn request_meta_serializable() {
        let meta = RequestMeta {
            request_id: "axr-42".to_string(),
            method: "POST".to_string(),
            path: "/v1/deploy".to_string(),
            status: 200,
            latency_us: 1500,
            latency_ms: 1,
            client_key: "token_abc".to_string(),
            slow: false,
        };
        let json = serde_json::to_value(&meta).unwrap();
        assert_eq!(json["request_id"], "axr-42");
        assert_eq!(json["method"], "POST");
        assert_eq!(json["path"], "/v1/deploy");
        assert_eq!(json["status"], 200);
        assert_eq!(json["latency_us"], 1500);
        assert_eq!(json["slow"], false);
    }

    #[test]
    fn request_meta_slow_flag() {
        let meta = RequestMeta {
            request_id: "axr-99".to_string(),
            method: "GET".to_string(),
            path: "/v1/health".to_string(),
            status: 200,
            latency_us: 6_000_000,
            latency_ms: 6000,
            client_key: "anonymous".to_string(),
            slow: true,
        };
        let json = serde_json::to_value(&meta).unwrap();
        assert_eq!(json["slow"], true);
        assert_eq!(json["latency_ms"], 6000);
    }

    #[test]
    fn middleware_stats_serializable() {
        let stats = MiddlewareStats {
            total_requests: 42,
            config: MiddlewareConfig::default(),
        };
        let json = serde_json::to_value(&stats).unwrap();
        assert_eq!(json["total_requests"], 42);
        assert_eq!(json["config"]["enabled"], true);
    }

    #[test]
    fn client_key_extraction() {
        let mut headers = axum::http::HeaderMap::new();
        assert_eq!(client_key_from_headers(&headers), "anonymous");

        headers.insert("authorization", HeaderValue::from_static("Bearer token123"));
        assert_eq!(client_key_from_headers(&headers), "Bearer token123");
    }
}