aprender-serve 0.64.0

Pure Rust ML inference engine built from scratch - model serving for GGUF and safetensors
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

impl EmptyInputResult {
    pub fn handled_gracefully(input_type: &str, response: &str) -> Self {
        Self {
            input_type: input_type.to_string(),
            handled: true,
            response_type: response.to_string(),
            panicked: false,
            meets_qa025: true,
        }
    }

    pub fn panicked(input_type: &str) -> Self {
        Self {
            input_type: input_type.to_string(),
            handled: false,
            response_type: "panic".to_string(),
            panicked: true,
            meets_qa025: false,
        }
    }
}

/// Empty input test cases
#[derive(Debug, Clone)]
pub struct EmptyInputTestCase {
    /// Test name
    pub name: String,
    /// Prompt value
    pub prompt: String,
    /// Expected behavior
    pub expected_behavior: String,
}

impl EmptyInputTestCase {
    pub fn empty_string() -> Self {
        Self {
            name: "Empty string".to_string(),
            prompt: String::new(),
            expected_behavior: "Return error or empty output".to_string(),
        }
    }

    pub fn whitespace_only() -> Self {
        Self {
            name: "Whitespace only".to_string(),
            prompt: "   \n\t  ".to_string(),
            expected_behavior: "Treat as empty or process whitespace".to_string(),
        }
    }

    pub fn single_space() -> Self {
        Self {
            name: "Single space".to_string(),
            prompt: " ".to_string(),
            expected_behavior: "Process or reject".to_string(),
        }
    }
}

/// IMP-178a: Test empty input result types
#[test]
fn test_imp_178a_empty_input_result() {
    let handled = EmptyInputResult::handled_gracefully("empty_string", "error_returned");
    assert!(
        handled.meets_qa025,
        "IMP-178a: Graceful handling should meet QA-025"
    );
    assert!(!handled.panicked, "IMP-178a: Handled should not panic");

    let panicked = EmptyInputResult::panicked("empty_string");
    assert!(
        !panicked.meets_qa025,
        "IMP-178a: Panic should NOT meet QA-025"
    );
    assert!(panicked.panicked, "IMP-178a: Panicked should be true");

    println!("\nIMP-178a: Empty Input Results:");
    println!(
        "  Handled: meets_qa025={}, response={}",
        handled.meets_qa025, handled.response_type
    );
    println!(
        "  Panicked: meets_qa025={}, panicked={}",
        panicked.meets_qa025, panicked.panicked
    );
}

/// IMP-178b: Test empty input test cases
#[test]
fn test_imp_178b_empty_input_cases() {
    let empty = EmptyInputTestCase::empty_string();
    assert!(
        empty.prompt.is_empty(),
        "IMP-178b: Empty string should be empty"
    );

    let whitespace = EmptyInputTestCase::whitespace_only();
    assert!(
        whitespace.prompt.trim().is_empty(),
        "IMP-178b: Whitespace only should trim to empty"
    );

    let space = EmptyInputTestCase::single_space();
    assert_eq!(
        space.prompt.len(),
        1,
        "IMP-178b: Single space should have length 1"
    );

    println!("\nIMP-178b: Empty Input Test Cases:");
    println!("  {}: prompt={:?}", empty.name, empty.prompt);
    println!("  {}: prompt={:?}", whitespace.name, whitespace.prompt);
    println!("  {}: prompt={:?}", space.name, space.prompt);
}

/// Input validation for empty checks
#[derive(Debug, Clone)]
pub struct InputValidator {
    /// Allow empty prompts
    pub allow_empty: bool,
    /// Trim whitespace before validation
    pub trim_whitespace: bool,
    /// Minimum prompt length
    pub min_length: usize,
}

impl InputValidator {
    pub fn strict() -> Self {
        Self {
            allow_empty: false,
            trim_whitespace: true,
            min_length: 1,
        }
    }

    pub fn permissive() -> Self {
        Self {
            allow_empty: true,
            trim_whitespace: false,
            min_length: 0,
        }
    }

    pub fn validate(&self, prompt: &str) -> std::result::Result<(), String> {
        let check = if self.trim_whitespace {
            prompt.trim()
        } else {
            prompt
        };

        if check.is_empty() && !self.allow_empty {
            return Err("Empty prompt not allowed".to_string());
        }

        if check.len() < self.min_length {
            return Err(format!(
                "Prompt too short: {} < {}",
                check.len(),
                self.min_length
            ));
        }

        Ok(())
    }
}

/// IMP-178c: Test input validation
#[test]
fn test_imp_178c_input_validation() {
    let strict = InputValidator::strict();
    assert!(
        strict.validate("hello").is_ok(),
        "IMP-178c: Normal input should pass strict"
    );
    assert!(
        strict.validate("").is_err(),
        "IMP-178c: Empty should fail strict"
    );
    assert!(
        strict.validate("   ").is_err(),
        "IMP-178c: Whitespace should fail strict (trimmed)"
    );

    let permissive = InputValidator::permissive();
    assert!(
        permissive.validate("").is_ok(),
        "IMP-178c: Empty should pass permissive"
    );
    assert!(
        permissive.validate("   ").is_ok(),
        "IMP-178c: Whitespace should pass permissive"
    );

    println!("\nIMP-178c: Input Validation:");
    println!(
        "  Strict: empty={:?}, whitespace={:?}",
        strict.validate(""),
        strict.validate("   ")
    );
    println!(
        "  Permissive: empty={:?}, whitespace={:?}",
        permissive.validate(""),
        permissive.validate("   ")
    );
}

/// IMP-178d: Real-world empty input handling
#[test]
#[ignore = "Requires running llama.cpp server on port 8082"]
fn test_imp_178d_realworld_empty_input() {
    let client = ModelHttpClient::with_timeout(30);

    // Test empty prompt
    let empty_request = CompletionRequest {
        model: "default".to_string(),
        prompt: String::new(),
        max_tokens: 10,
        temperature: Some(0.0),
        stream: false,
    };

    let result = client.llamacpp_completion("http://127.0.0.1:8082", &empty_request);

    // Any response (success or error) means no panic
    let handling = EmptyInputResult::handled_gracefully(
        "empty_string",
        if result.is_ok() { "success" } else { "error" },
    );

    println!("\nIMP-178d: Real-World Empty Input:");
    println!("  Input type: {}", handling.input_type);
    println!("  Response: {}", handling.response_type);
    println!("  Panicked: {}", handling.panicked);
    println!(
        "  QA-025: {}",
        if handling.meets_qa025 { "PASS" } else { "FAIL" }
    );
}

// ===========================================
// IMP-179: Max Context Length Exceeded (QA-026)
// ===========================================

/// Per spec QA-026: No panic on max context length exceeded
#[derive(Debug, Clone)]
pub struct MaxContextResult {
    /// Requested context length
    pub requested_length: usize,
    /// Maximum allowed length
    pub max_length: usize,
    /// Whether limit was exceeded
    pub exceeded: bool,
    /// How the excess was handled
    pub handling: String,
    /// Whether system panicked
    pub panicked: bool,
    /// Meets QA-026
    pub meets_qa026: bool,
}

impl MaxContextResult {
    pub fn within_limit(requested: usize, max: usize) -> Self {
        Self {
            requested_length: requested,
            max_length: max,
            exceeded: false,
            handling: "Processed normally".to_string(),
            panicked: false,
            meets_qa026: true,
        }
    }

    pub fn exceeded_graceful(requested: usize, max: usize, handling: &str) -> Self {
        Self {
            requested_length: requested,
            max_length: max,
            exceeded: true,
            handling: handling.to_string(),
            panicked: false,
            meets_qa026: true,
        }
    }

    pub fn exceeded_panic(requested: usize, max: usize) -> Self {
        Self {
            requested_length: requested,
            max_length: max,
            exceeded: true,
            handling: "Panic".to_string(),
            panicked: true,
            meets_qa026: false,
        }
    }
}

/// Context length handling strategies
#[derive(Debug, Clone, PartialEq)]
pub enum ContextOverflowStrategy {
    /// Reject the request with error
    Reject,
    /// Truncate from the beginning
    TruncateHead,
    /// Truncate from the end
    TruncateTail,
    /// Sliding window
    SlidingWindow,
}

/// Context length validator
#[derive(Debug, Clone)]
pub struct ContextLengthValidator {
    /// Maximum context length
    pub max_length: usize,
    /// Overflow handling strategy
    pub overflow_strategy: ContextOverflowStrategy,
}

impl ContextLengthValidator {
    pub fn new(max_length: usize, strategy: ContextOverflowStrategy) -> Self {
        Self {
            max_length,
            overflow_strategy: strategy,
        }
    }

    pub fn validate(&self, length: usize) -> MaxContextResult {
        if length <= self.max_length {
            MaxContextResult::within_limit(length, self.max_length)
        } else {
            let handling = match &self.overflow_strategy {
                ContextOverflowStrategy::Reject => "Rejected with error",
                ContextOverflowStrategy::TruncateHead => "Truncated from head",
                ContextOverflowStrategy::TruncateTail => "Truncated from tail",
                ContextOverflowStrategy::SlidingWindow => "Used sliding window",
            };
            MaxContextResult::exceeded_graceful(length, self.max_length, handling)
        }
    }
}

/// IMP-179a: Test max context result types
#[test]
fn test_imp_179a_max_context_result() {
    let within = MaxContextResult::within_limit(1000, 2048);
    assert!(!within.exceeded, "IMP-179a: Within limit should not exceed");
    assert!(
        within.meets_qa026,
        "IMP-179a: Within limit should meet QA-026"
    );

    let exceeded = MaxContextResult::exceeded_graceful(4000, 2048, "Truncated");
    assert!(exceeded.exceeded, "IMP-179a: Exceeded should be true");
    assert!(
        exceeded.meets_qa026,
        "IMP-179a: Graceful handling should meet QA-026"
    );

    let panic = MaxContextResult::exceeded_panic(10000, 2048);
    assert!(!panic.meets_qa026, "IMP-179a: Panic should NOT meet QA-026");

    println!("\nIMP-179a: Max Context Results:");
    println!(
        "  Within: {}/{}, exceeded={}, meets_qa026={}",
        within.requested_length, within.max_length, within.exceeded, within.meets_qa026
    );
    println!(
        "  Exceeded: {}/{}, handling={}, meets_qa026={}",
        exceeded.requested_length, exceeded.max_length, exceeded.handling, exceeded.meets_qa026
    );
}

/// IMP-179b: Test context length validation
#[test]
fn test_imp_179b_context_validation() {
    let reject_validator = ContextLengthValidator::new(2048, ContextOverflowStrategy::Reject);

    let within = reject_validator.validate(1000);
    assert!(
        !within.exceeded,
        "IMP-179b: 1000 tokens should be within 2048 limit"
    );

    let exceeded = reject_validator.validate(4000);
    assert!(
        exceeded.exceeded,
        "IMP-179b: 4000 tokens should exceed 2048 limit"
    );
    assert!(
        exceeded.handling.contains("Rejected"),
        "IMP-179b: Should use reject strategy"
    );

    let truncate_validator =
        ContextLengthValidator::new(2048, ContextOverflowStrategy::TruncateHead);
    let truncated = truncate_validator.validate(4000);
    assert!(
        truncated.handling.contains("head"),
        "IMP-179b: Should use truncate head strategy"
    );

    println!("\nIMP-179b: Context Validation:");
    println!(
        "  Reject strategy: {} tokens -> {}",
        4000, exceeded.handling
    );
    println!("  Truncate head: {} tokens -> {}", 4000, truncated.handling);
}

/// IMP-179c: Test overflow strategies
#[test]
fn test_imp_179c_overflow_strategies() {
    let strategies = vec![
        ContextOverflowStrategy::Reject,
        ContextOverflowStrategy::TruncateHead,
        ContextOverflowStrategy::TruncateTail,
        ContextOverflowStrategy::SlidingWindow,
    ];

    for strategy in &strategies {
        let validator = ContextLengthValidator::new(2048, strategy.clone());
        let result = validator.validate(5000);
        assert!(
            result.meets_qa026,
            "IMP-179c: All strategies should meet QA-026"
        );
        assert!(result.exceeded, "IMP-179c: All should detect exceeding");
    }

    println!("\nIMP-179c: Overflow Strategies:");
    for strategy in strategies {
        let validator = ContextLengthValidator::new(2048, strategy.clone());
        let result = validator.validate(5000);
        println!("  {:?}: {}", strategy, result.handling);
    }
}