aprender-core 0.31.2

Next-generation machine learning library in pure Rust
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
#![allow(clippy::disallowed_methods)]
//! QA Example: apr serve Falsification Suite (PMAT-QA-RUST-001)
//!
//! Popperian falsification tests for `apr serve` HTTP endpoints.
//! The most comprehensive QA example, testing OpenAI API compatibility.
//!
//! # Tests (35 Points)
//!
//! | ID | Test | Points | Criterion |
//! |----|------|--------|-----------|
//! | P017 | Health endpoint | 2 | `/health` returns 200 |
//! | P018 | Compute mode | 2 | Response contains cpu/gpu/cuda |
//! | P019 | Valid JSON | 2 | Response parses as JSON |
//! | P020 | OpenAI structure | 3 | `choices[0].message.content` exists |
//! | P021 | Non-empty content | 2 | Content length > 0 |
//! | P022 | No token artifacts | 2 | No raw tokens in output |
//! | P023 | No BPE artifacts | 2 | No Ä /ÄŠ in output |
//! | P024 | SSE streaming | 3 | `data: {` prefix present |
//! | P025 | Stream termination | 2 | `[DONE]` marker present |
//! | P026 | Determinism T=0 | 3 | Same request → same response |
//! | P027 | Malformed JSON | 2 | Returns 400 on bad input |
//! | P028 | Coherency | 2 | Output is intelligible |
//! | P029 | No multi-turn loop | 3 | No fake Human:/Assistant: |
//! | P030 | Trace brick level | 1 | `brick_trace` in response |
//! | P031 | Trace step level | 1 | `step_trace` in response |
//! | P032 | Trace layer level | 1 | `layer_trace` in response |
//! | P033 | Default suppression | 2 | No trace fields without header |
//!
//! # Usage
//!
//! ```bash
//! cargo run --example qa_serve
//! cargo run --example qa_serve -- --model path/to/model.gguf --port 8080
//! cargo run --example qa_serve -- --all-models
//! ```

use std::env;
use std::io::{BufRead, BufReader};
use std::net::TcpStream;
use std::path::PathBuf;
use std::process::{Child, Command, Stdio};
use std::thread;
use std::time::{Duration, Instant};

// Colors
const RED: &str = "\x1b[0;31m";
const GREEN: &str = "\x1b[0;32m";
const YELLOW: &str = "\x1b[0;33m";
const BLUE: &str = "\x1b[0;34m";
const CYAN: &str = "\x1b[0;36m";
const NC: &str = "\x1b[0m";

struct TestResult {
    id: &'static str,
    name: &'static str,
    passed: bool,
    details: Option<String>,
    points: u32,
}

impl TestResult {
    fn pass(id: &'static str, name: &'static str, points: u32) -> Self {
        Self {
            id,
            name,
            passed: true,
            details: None,
            points,
        }
    }
    fn pass_with_details(
        id: &'static str,
        name: &'static str,
        points: u32,
        details: String,
    ) -> Self {
        Self {
            id,
            name,
            passed: true,
            details: Some(details),
            points,
        }
    }
    fn fail(id: &'static str, name: &'static str, points: u32, details: String) -> Self {
        Self {
            id,
            name,
            passed: false,
            details: Some(details),
            points,
        }
    }
    fn skip(id: &'static str, name: &'static str, _points: u32, reason: String) -> Self {
        Self {
            id,
            name,
            passed: true,
            details: Some(format!("SKIP: {}", reason)),
            points: 0,
        }
    }
    fn print(&self) {
        let status = if self.passed {
            format!("{}[PASS]{}", GREEN, NC)
        } else {
            format!("{}[FAIL]{}", RED, NC)
        };
        println!("{} {}: {}", status, self.id, self.name);
        if let Some(ref d) = self.details {
            println!("       {}", d);
        }
    }
}

struct QaConfig {
    model_path: Option<PathBuf>,
    apr_binary: PathBuf,
    port: u16,
    #[allow(dead_code)]
    all_models: bool,
    verbose: bool,
}

impl Default for QaConfig {
    fn default() -> Self {
        Self {
            model_path: None,
            apr_binary: find_apr_binary(),
            port: 18080, // Use high port to avoid conflicts with common services
            all_models: false,
            verbose: false,
        }
    }
}

fn find_apr_binary() -> PathBuf {
    // GH-301: Use CARGO_TARGET_DIR if set, then standard target/, then PATH
    let mut candidates: Vec<PathBuf> = Vec::new();
    if let Ok(target_dir) = env::var("CARGO_TARGET_DIR") {
        candidates.push(PathBuf::from(&target_dir).join("release").join("apr"));
        candidates.push(PathBuf::from(&target_dir).join("debug").join("apr"));
    }
    candidates.push(PathBuf::from("target/release/apr"));
    candidates.push(PathBuf::from("target/debug/apr"));
    for path in candidates {
        if path.exists() {
            return path;
        }
    }
    PathBuf::from("apr")
}

fn find_default_model() -> Option<PathBuf> {
    let home = env::var("HOME").unwrap_or_default();
    for p in [
        format!("{home}/.cache/pacha/models/d4c4d9763127153c.gguf"),
        format!("{home}/.cache/huggingface/models/qwen2.5-coder-0.5b-gguf/qwen2.5-coder-0.5b-instruct-q4_k_m.gguf"),
        "models/qwen2.5-coder-0.5b-instruct-q4_k_m.gguf".to_string(),
    ] {
        if PathBuf::from(&p).exists() { return Some(PathBuf::from(p)); }
    }
    None
}

/// Check if something is already listening on the port
fn check_port_in_use(port: u16) -> bool {
    TcpStream::connect(format!("127.0.0.1:{}", port)).is_ok()
}

/// Verify server is OUR server (responds with valid APR health check)
fn verify_server_health(port: u16) -> bool {
    match http_get("127.0.0.1", port, "/health") {
        Ok((status, body)) => {
            // Must be 200 AND contain expected fields
            status == 200 && body.contains("status") && body.contains("healthy")
        }
        Err(_) => false,
    }
}

/// Build the Command to launch the server process
fn build_server_command(config: &QaConfig, model: &PathBuf) -> Command {
    let port_str = config.port.to_string();
    let serve_args = vec![
        "serve",
        model.to_str().unwrap_or(""),
        "--port",
        &port_str,
        "--gpu",
    ];

    let mut cmd = if config.apr_binary.to_string_lossy() == "cargo" {
        let mut c = Command::new("cargo");
        c.args([
            "run",
            "-p",
            "apr-cli",
            "--release",
            "--features",
            "inference",
            "--",
        ]);
        c.args(&serve_args);
        c
    } else {
        let mut c = Command::new(&config.apr_binary);
        c.args(&serve_args);
        c
    };

    cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
    cmd
}

/// Poll until the server health check passes, returning true on success
fn await_server_health(port: u16, verbose: bool) -> bool {
    for i in 0..60 {
        thread::sleep(Duration::from_secs(1));
        if verify_server_health(port) {
            if verbose {
                eprintln!("{}Server health check passed after {}s{}", GREEN, i + 1, NC);
            }
            return true;
        }
        if verbose && i % 10 == 9 {
            eprintln!("{}Waiting for server... {}s{}", YELLOW, i + 1, NC);
        }
    }
    false
}

/// Start the apr serve process
fn start_server(config: &QaConfig, model: &PathBuf) -> Option<Child> {
    if check_port_in_use(config.port) {
        eprintln!(
            "{}ERROR: Port {} already in use. Cannot start server.{}",
            RED, config.port, NC
        );
        eprintln!(
            "{}Try: lsof -i :{} to find what's using it{}",
            YELLOW, config.port, NC
        );
        return None;
    }

    let mut cmd = build_server_command(config, model);

    if config.verbose {
        eprintln!("{}DEBUG: Starting server {:?}{}", CYAN, cmd, NC);
    }

    let child = match cmd.spawn() {
        Ok(c) => c,
        Err(e) => {
            eprintln!("{}Failed to spawn server: {}{}", RED, e, NC);
            return None;
        }
    };

    if await_server_health(config.port, config.verbose) {
        return Some(child);
    }

    eprintln!(
        "{}Server failed to respond to health check within 60s{}",
        RED, NC
    );
    None
}

/// HTTP GET request (minimal, no external deps)
fn http_get(host: &str, port: u16, path: &str) -> Result<(u16, String), String> {
    let addr = format!("{}:{}", host, port);
    let mut stream = TcpStream::connect(&addr).map_err(|e| e.to_string())?;
    stream.set_read_timeout(Some(Duration::from_secs(30))).ok();

    use std::io::Write;
    let request = format!(
        "GET {} HTTP/1.1\r\nHost: {}:{}\r\nConnection: close\r\n\r\n",
        path, host, port
    );
    stream
        .write_all(request.as_bytes())
        .map_err(|e| e.to_string())?;

    let mut reader = BufReader::new(stream);
    let mut status_line = String::new();
    reader
        .read_line(&mut status_line)
        .map_err(|e| e.to_string())?;

    let status: u16 = status_line
        .split_whitespace()
        .nth(1)
        .and_then(|s| s.parse().ok())
        .unwrap_or(0);

    // Skip headers
    loop {
        let mut line = String::new();
        reader.read_line(&mut line).ok();
        if line.trim().is_empty() {
            break;
        }
    }

    let mut body = String::new();
    for line in reader.lines().map_while(Result::ok) {
        body.push_str(&line);
        body.push('\n');
    }

    Ok((status, body))
}

/// HTTP POST request with JSON body
fn http_post(
    host: &str,
    port: u16,
    path: &str,
    body: &str,
    headers: &[(&str, &str)],
) -> Result<(u16, String), String> {
    let addr = format!("{}:{}", host, port);
    let mut stream = TcpStream::connect(&addr).map_err(|e| e.to_string())?;
    stream.set_read_timeout(Some(Duration::from_secs(60))).ok();

    use std::io::Write;
    let mut header_str = String::new();
    for (k, v) in headers {
        header_str.push_str(&format!("{}: {}\r\n", k, v));
    }

    let request = format!(
        "POST {} HTTP/1.1\r\nHost: {}:{}\r\nContent-Type: application/json\r\nContent-Length: {}\r\n{}Connection: close\r\n\r\n{}",
        path, host, port, body.len(), header_str, body
    );
    stream
        .write_all(request.as_bytes())
        .map_err(|e| e.to_string())?;

    let mut reader = BufReader::new(stream);
    let mut status_line = String::new();
    reader
        .read_line(&mut status_line)
        .map_err(|e| e.to_string())?;

    let status: u16 = status_line
        .split_whitespace()
        .nth(1)
        .and_then(|s| s.parse().ok())
        .unwrap_or(0);

    // Skip headers
    loop {
        let mut line = String::new();
        reader.read_line(&mut line).ok();
        if line.trim().is_empty() {
            break;
        }
    }

    let mut resp_body = String::new();
    for line in reader.lines().map_while(Result::ok) {
        resp_body.push_str(&line);
        resp_body.push('\n');
    }

    Ok((status, resp_body))
}

/// Find the end of a JSON string value (position of the closing unescaped quote)
fn find_closing_quote(chars: &[char], start: usize) -> usize {
    let mut end = start;
    while end < chars.len() {
        if chars[end] == '"' && (end == 0 || chars[end - 1] != '\\') {
            break;
        }
        end += 1;
    }
    end
}

/// Extract JSON field (minimal parser)
fn extract_json_content(json: &str) -> Option<String> {
    // Look for "content": "..." pattern
    let start = json.find("\"content\":")?;
    let rest = &json[start + 10..];
    let quote_start = rest.find('"')?;
    let content_start = quote_start + 1;
    let chars: Vec<char> = rest.chars().collect();
    let end = find_closing_quote(&chars, content_start);
    Some(chars[content_start..end].iter().collect())
}

// === TEST FUNCTIONS ===

fn test_health(config: &QaConfig) -> TestResult {
    match http_get("127.0.0.1", config.port, "/health") {
        Ok((status, _)) if status == 200 => TestResult::pass("P017", "Health Endpoint", 2),
        Ok((status, _)) => {
            TestResult::fail("P017", "Health Endpoint", 2, format!("Status {}", status))
        }
        Err(e) => TestResult::fail("P017", "Health Endpoint", 2, e),
    }
}

fn test_compute_mode(config: &QaConfig) -> TestResult {
    match http_get("127.0.0.1", config.port, "/health") {
        Ok((_, body)) => {
            if body.contains("cpu") || body.contains("gpu") || body.contains("cuda") {
                TestResult::pass_with_details("P018", "Compute Mode", 2, "Mode found".to_string())
            } else {
                TestResult::fail("P018", "Compute Mode", 2, "No mode in response".to_string())
            }
        }
        Err(e) => TestResult::fail("P018", "Compute Mode", 2, e),
    }
}

fn test_valid_json(config: &QaConfig) -> TestResult {
    let body = r#"{"model":"default","messages":[{"role":"user","content":"Hi"}],"max_tokens":5}"#;
    match http_post("127.0.0.1", config.port, "/v1/chat/completions", body, &[]) {
        Ok((200, resp)) => {
            if resp.contains('{') && resp.contains('}') && resp.contains("choices") {
                TestResult::pass("P019", "Valid JSON Response", 2)
            } else {
                TestResult::fail("P019", "Valid JSON Response", 2, "Invalid JSON".to_string())
            }
        }
        Ok((status, _)) => TestResult::fail(
            "P019",
            "Valid JSON Response",
            2,
            format!("Status {}", status),
        ),
        Err(e) => TestResult::fail("P019", "Valid JSON Response", 2, e),
    }
}

fn test_openai_structure(config: &QaConfig) -> TestResult {
    let body =
        r#"{"model":"default","messages":[{"role":"user","content":"Say hi"}],"max_tokens":10}"#;
    match http_post("127.0.0.1", config.port, "/v1/chat/completions", body, &[]) {
        Ok((200, resp)) => {
            if resp.contains("choices") && resp.contains("message") && resp.contains("content") {
                TestResult::pass("P020", "OpenAI Structure", 3)
            } else {
                TestResult::fail("P020", "OpenAI Structure", 3, "Missing fields".to_string())
            }
        }
        Ok((s, _)) => TestResult::fail("P020", "OpenAI Structure", 3, format!("Status {}", s)),
        Err(e) => TestResult::fail("P020", "OpenAI Structure", 3, e),
    }
}

fn test_non_empty_content(config: &QaConfig) -> TestResult {
    let body =
        r#"{"model":"default","messages":[{"role":"user","content":"Say hello"}],"max_tokens":10}"#;
    match http_post("127.0.0.1", config.port, "/v1/chat/completions", body, &[]) {
        Ok((200, resp)) => {
            if let Some(content) = extract_json_content(&resp) {
                if !content.is_empty() {
                    TestResult::pass_with_details(
                        "P021",
                        "Non-Empty Content",
                        2,
                        format!("Len: {}", content.len()),
                    )
                } else {
                    TestResult::fail("P021", "Non-Empty Content", 2, "Empty content".to_string())
                }
            } else {
                TestResult::fail(
                    "P021",
                    "Non-Empty Content",
                    2,
                    "No content field".to_string(),
                )
            }
        }
        Ok((s, _)) => TestResult::fail("P021", "Non-Empty Content", 2, format!("Status {}", s)),
        Err(e) => TestResult::fail("P021", "Non-Empty Content", 2, e),
    }
}

include!("includes/qa_serve_include_01.rs");