inferno-ai 0.10.3

Enterprise AI/ML model runner with automatic updates, real-time monitoring, and multi-interface support
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! Priority Queue Infrastructure for Advanced Request Management
//!
//! This module implements a priority queue based on BinaryHeap that supports:
//! - Priority levels: VIP, High, Normal, Low
//! - Deadline-based ordering
//! - FIFO ordering for equal priority requests
//! - Queue statistics and metrics

use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::BinaryHeap;

/// Priority levels for request processing
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Hash)]
pub enum Priority {
    /// Payment-backed, highest priority requests
    VIP = 4,
    /// Premium users with higher SLA
    High = 3,
    /// Standard users
    Normal = 2,
    /// Batch/background operations
    Low = 1,
}

impl Priority {
    /// Get the weight for fair scheduling
    pub fn weight(&self) -> u32 {
        match self {
            Priority::VIP => 8,
            Priority::High => 4,
            Priority::Normal => 2,
            Priority::Low => 1,
        }
    }

    /// Convert from numeric representation
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            1 => Some(Priority::Low),
            2 => Some(Priority::Normal),
            3 => Some(Priority::High),
            4 => Some(Priority::VIP),
            _ => None,
        }
    }
}

/// Metadata for a queued inference request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestMetadata {
    /// Unique identifier for this request
    pub request_id: String,
    /// User or client identifier
    pub user_id: String,
    /// Priority level for this request
    pub priority: Priority,
    /// When the request was submitted
    pub created_at: u64, // Unix timestamp in milliseconds
    /// Optional deadline in seconds from creation
    pub deadline_secs: Option<u64>,
    /// Estimated token budget for this request
    pub estimated_tokens: u32,
    /// Target model identifier
    pub model_id: String,
    /// User-defined tags for routing
    pub tags: Vec<String>,
    /// Number of retry attempts already made
    pub retry_count: u32,
    /// IDs of other requests this depends on
    pub dependencies: Vec<String>,
}

impl RequestMetadata {
    /// Create a new request metadata
    pub fn new(request_id: String, user_id: String, priority: Priority, model_id: String) -> Self {
        Self {
            request_id,
            user_id,
            priority,
            created_at: Self::current_timestamp(),
            deadline_secs: None,
            estimated_tokens: 256, // Default estimate
            model_id,
            tags: Vec::new(),
            retry_count: 0,
            dependencies: Vec::new(),
        }
    }

    /// Set the deadline for this request (in seconds from now)
    pub fn with_deadline(mut self, deadline_secs: u64) -> Self {
        self.deadline_secs = Some(deadline_secs);
        self
    }

    /// Set the estimated token count
    pub fn with_estimated_tokens(mut self, tokens: u32) -> Self {
        self.estimated_tokens = tokens;
        self
    }

    /// Add a tag to the request
    pub fn with_tag(mut self, tag: String) -> Self {
        self.tags.push(tag);
        self
    }

    /// Add dependency on another request
    pub fn with_dependency(mut self, dep_id: String) -> Self {
        self.dependencies.push(dep_id);
        self
    }

    /// Get the current effective priority, considering age and deadline
    pub fn effective_priority(&self) -> i32 {
        let mut priority_value = self.priority as i32;

        // Age boost: older requests get priority boost
        let age_ms = Self::current_timestamp().saturating_sub(self.created_at);
        let age_secs = age_ms / 1000;
        priority_value += (age_secs / 10) as i32;

        // Deadline escalation
        if let Some(deadline_secs) = self.deadline_secs {
            let elapsed_secs = age_secs;
            let remaining_secs = deadline_secs.saturating_sub(elapsed_secs);

            if remaining_secs < 10 {
                // Critical: boost to VIP + 10
                priority_value = (Priority::VIP as i32) + 10;
            } else if remaining_secs < 30 {
                // Urgent: boost to VIP level
                priority_value = (Priority::VIP as i32) + 5;
            }
        }

        priority_value
    }

    /// Get current Unix timestamp in milliseconds
    fn current_timestamp() -> u64 {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_millis() as u64)
            .unwrap_or(0)
    }

    /// Get age in milliseconds
    pub fn age_ms(&self) -> u64 {
        Self::current_timestamp().saturating_sub(self.created_at)
    }
}

/// Wrapper for priority queue ordering
#[derive(Debug, Clone)]
struct QueuedRequest {
    /// The request metadata
    metadata: RequestMetadata,
    /// Position in queue for FIFO ordering when priorities are equal
    sequence: u64,
}

impl PartialEq for QueuedRequest {
    fn eq(&self, other: &Self) -> bool {
        self.metadata.request_id == other.metadata.request_id
    }
}

impl Eq for QueuedRequest {}

impl PartialOrd for QueuedRequest {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for QueuedRequest {
    fn cmp(&self, other: &Self) -> Ordering {
        // First, compare effective priority (higher priority = greater in Ord = popped first from max-heap)
        let self_priority = self.metadata.effective_priority();
        let other_priority = other.metadata.effective_priority();

        match self_priority.cmp(&other_priority) {
            Ordering::Equal => {
                // If priority is equal, maintain FIFO order (lower sequence = earlier = should pop first)
                // Lower sequence should compare as "greater" so it gets popped first
                other.sequence.cmp(&self.sequence)
            }
            ordering => ordering,
        }
    }
}

/// Statistics about the queue
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueueStats {
    pub queued_count: usize,
    pub total_weight: u32,
    pub estimated_wait_ms: u64,
}

/// Priority queue for managing inference requests
#[derive(Debug)]
pub struct PriorityQueue {
    heap: BinaryHeap<QueuedRequest>,
    sequence_counter: u64,
}

impl PriorityQueue {
    /// Create a new empty priority queue
    pub fn new() -> Self {
        Self {
            heap: BinaryHeap::new(),
            sequence_counter: 0,
        }
    }

    /// Add a request to the queue
    pub fn push(&mut self, metadata: RequestMetadata) {
        let queued = QueuedRequest {
            metadata,
            sequence: self.sequence_counter,
        };
        self.sequence_counter = self.sequence_counter.wrapping_add(1);
        self.heap.push(queued);
    }

    /// Remove and return the highest priority request
    pub fn pop(&mut self) -> Option<RequestMetadata> {
        self.heap.pop().map(|q| q.metadata)
    }

    /// Peek at the highest priority request without removing it
    pub fn peek(&self) -> Option<&RequestMetadata> {
        self.heap.peek().map(|q| &q.metadata)
    }

    /// Check if the queue is empty
    pub fn is_empty(&self) -> bool {
        self.heap.is_empty()
    }

    /// Get the number of queued requests
    pub fn len(&self) -> usize {
        self.heap.len()
    }

    /// Get statistics about the queue
    pub fn stats(&self) -> QueueStats {
        let queued_count = self.heap.len();
        let total_weight: u32 = self.heap.iter().map(|q| q.metadata.priority.weight()).sum();

        // Estimate wait based on token counts and weights
        let estimated_tokens: u32 = self.heap.iter().map(|q| q.metadata.estimated_tokens).sum();
        // Assume ~50 tokens/sec average processing speed
        let estimated_wait_ms = ((estimated_tokens as f64 / 50.0) * 1000.0) as u64;

        QueueStats {
            queued_count,
            total_weight,
            estimated_wait_ms,
        }
    }

    /// Find and remove a request by ID
    pub fn remove_by_id(&mut self, request_id: &str) -> Option<RequestMetadata> {
        let mut removed = None;
        let temp: Vec<QueuedRequest> = self
            .heap
            .drain()
            .filter(|q| {
                if q.metadata.request_id == request_id {
                    removed = Some(q.metadata.clone());
                    false
                } else {
                    true
                }
            })
            .collect();

        temp.into_iter().for_each(|q| {
            self.heap.push(q);
        });

        removed
    }

    /// Get all pending requests (for debugging/monitoring)
    pub fn iter(&self) -> impl Iterator<Item = &RequestMetadata> {
        self.heap.iter().map(|q| &q.metadata)
    }

    /// Drain all requests from the queue
    pub fn drain(&mut self) -> impl Iterator<Item = RequestMetadata> + '_ {
        self.heap.drain().map(|q| q.metadata)
    }
}

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

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

    #[test]
    fn test_priority_ordering() {
        let mut queue = PriorityQueue::new();

        // Add requests with different priorities
        queue.push(RequestMetadata::new(
            "req1".to_string(),
            "user1".to_string(),
            Priority::Normal,
            "model1".to_string(),
        ));

        queue.push(RequestMetadata::new(
            "req2".to_string(),
            "user2".to_string(),
            Priority::VIP,
            "model1".to_string(),
        ));

        queue.push(RequestMetadata::new(
            "req3".to_string(),
            "user3".to_string(),
            Priority::Low,
            "model1".to_string(),
        ));

        queue.push(RequestMetadata::new(
            "req4".to_string(),
            "user4".to_string(),
            Priority::High,
            "model1".to_string(),
        ));

        // Should pop in priority order: VIP, High, Normal, Low
        assert_eq!(queue.pop().unwrap().request_id, "req2"); // VIP
        assert_eq!(queue.pop().unwrap().request_id, "req4"); // High
        assert_eq!(queue.pop().unwrap().request_id, "req1"); // Normal
        assert_eq!(queue.pop().unwrap().request_id, "req3"); // Low
        assert!(queue.is_empty());
    }

    #[test]
    fn test_fifo_ordering_same_priority() {
        let mut queue = PriorityQueue::new();

        // Add multiple requests with same priority
        queue.push(RequestMetadata::new(
            "req1".to_string(),
            "user1".to_string(),
            Priority::Normal,
            "model1".to_string(),
        ));

        queue.push(RequestMetadata::new(
            "req2".to_string(),
            "user2".to_string(),
            Priority::Normal,
            "model1".to_string(),
        ));

        queue.push(RequestMetadata::new(
            "req3".to_string(),
            "user3".to_string(),
            Priority::Normal,
            "model1".to_string(),
        ));

        // Should pop in FIFO order
        assert_eq!(queue.pop().unwrap().request_id, "req1");
        assert_eq!(queue.pop().unwrap().request_id, "req2");
        assert_eq!(queue.pop().unwrap().request_id, "req3");
    }

    #[test]
    fn test_deadline_handling() {
        let mut queue = PriorityQueue::new();

        // Add a low-priority request
        queue.push(RequestMetadata::new(
            "req1".to_string(),
            "user1".to_string(),
            Priority::Low,
            "model1".to_string(),
        ));

        // Add a high-priority request with deadline approaching
        let mut urgent_req = RequestMetadata::new(
            "req2".to_string(),
            "user2".to_string(),
            Priority::Normal,
            "model1".to_string(),
        );
        urgent_req.deadline_secs = Some(5); // 5 seconds deadline

        queue.push(urgent_req);

        // Even though it's Normal priority, deadline escalation should make it higher
        let first = queue.pop().unwrap();
        assert_eq!(first.request_id, "req2");
    }

    #[test]
    fn test_queue_stats() {
        let mut queue = PriorityQueue::new();

        queue.push(RequestMetadata::new(
            "req1".to_string(),
            "user1".to_string(),
            Priority::VIP,
            "model1".to_string(),
        ));

        queue.push(RequestMetadata::new(
            "req2".to_string(),
            "user2".to_string(),
            Priority::High,
            "model1".to_string(),
        ));

        let stats = queue.stats();
        assert_eq!(stats.queued_count, 2);
        assert_eq!(stats.total_weight, 8 + 4); // VIP weight + High weight
    }

    #[test]
    fn test_remove_by_id() {
        let mut queue = PriorityQueue::new();

        queue.push(RequestMetadata::new(
            "req1".to_string(),
            "user1".to_string(),
            Priority::Normal,
            "model1".to_string(),
        ));

        queue.push(RequestMetadata::new(
            "req2".to_string(),
            "user2".to_string(),
            Priority::Normal,
            "model1".to_string(),
        ));

        queue.push(RequestMetadata::new(
            "req3".to_string(),
            "user3".to_string(),
            Priority::Normal,
            "model1".to_string(),
        ));

        let removed = queue.remove_by_id("req2");
        assert!(removed.is_some());
        assert_eq!(removed.unwrap().request_id, "req2");

        // Verify remaining requests are correct
        assert_eq!(queue.len(), 2);
        assert_eq!(queue.pop().unwrap().request_id, "req1");
        assert_eq!(queue.pop().unwrap().request_id, "req3");
    }

    #[test]
    fn test_empty_queue() {
        let queue = PriorityQueue::new();
        assert!(queue.is_empty());
        assert_eq!(queue.len(), 0);
        assert!(queue.peek().is_none());
    }

    #[test]
    fn test_priority_weight() {
        assert_eq!(Priority::VIP.weight(), 8);
        assert_eq!(Priority::High.weight(), 4);
        assert_eq!(Priority::Normal.weight(), 2);
        assert_eq!(Priority::Low.weight(), 1);
    }

    #[test]
    fn test_request_builder() {
        let req = RequestMetadata::new(
            "req1".to_string(),
            "user1".to_string(),
            Priority::Normal,
            "model1".to_string(),
        )
        .with_deadline(60)
        .with_estimated_tokens(512)
        .with_tag("batch".to_string())
        .with_dependency("dep1".to_string());

        assert_eq!(req.deadline_secs, Some(60));
        assert_eq!(req.estimated_tokens, 512);
        assert_eq!(req.tags, vec!["batch".to_string()]);
        assert_eq!(req.dependencies, vec!["dep1".to_string()]);
    }
}