pictor-runtime 0.1.0

Inference runtime, sampling, tokenizer, and server for Pictor
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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! Tests for the OpenAI-compatible chat completions server.
//!
//! Uses axum test utilities to verify HTTP endpoints.

use axum::body::Body;
use axum::http::{Request, StatusCode};
use std::sync::Arc;
use tower::ServiceExt;

use pictor_core::config::Qwen3Config;
use pictor_runtime::engine::InferenceEngine;
use pictor_runtime::metrics::InferenceMetrics;
use pictor_runtime::sampling::SamplingParams;
use pictor_runtime::server::{create_router, create_router_with_metrics};

fn test_router() -> axum::Router {
    let config = Qwen3Config::tiny_test();
    let params = SamplingParams::default();
    let engine = InferenceEngine::new(config, params, 42);
    create_router(engine, None)
}

// ══════════════════════════════════════════════════════════════
// Health endpoint
// ══════════════════════════════════════════════════════════════

#[tokio::test]
async fn health_returns_200() {
    let app = test_router();
    let req = Request::get("/health")
        .body(Body::empty())
        .expect("request");
    let resp = app.oneshot(req).await.expect("response");
    assert_eq!(resp.status(), StatusCode::OK);

    let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .expect("body");
    assert_eq!(&body[..], b"ok");
}

// ══════════════════════════════════════════════════════════════
// Models endpoint
// ══════════════════════════════════════════════════════════════

#[tokio::test]
async fn models_returns_json_with_model_list() {
    let app = test_router();
    let req = Request::get("/v1/models")
        .body(Body::empty())
        .expect("request");
    let resp = app.oneshot(req).await.expect("response");
    assert_eq!(resp.status(), StatusCode::OK);

    let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .expect("body");
    let json: serde_json::Value = serde_json::from_slice(&body).expect("parse json");

    assert_eq!(json["object"], "list");
    let data = json["data"].as_array().expect("data should be array");
    assert!(!data.is_empty(), "should have at least one model");
    assert_eq!(data[0]["id"], "bonsai-8b");
    assert_eq!(data[0]["object"], "model");
    assert_eq!(data[0]["owned_by"], "pictor");
}

// ══════════════════════════════════════════════════════════════
// Chat completions endpoint
// ══════════════════════════════════════════════════════════════

#[tokio::test]
async fn chat_completions_valid_request() {
    let app = test_router();
    let body = serde_json::json!({
        "messages": [
            {"role": "user", "content": "Hello"}
        ],
        "max_tokens": 5,
        "temperature": 0.0
    });
    let req = Request::post("/v1/chat/completions")
        .header("content-type", "application/json")
        .body(Body::from(serde_json::to_string(&body).expect("serialize")))
        .expect("request");

    let resp = app.oneshot(req).await.expect("response");
    assert_eq!(resp.status(), StatusCode::OK);

    let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .expect("body");
    let json: serde_json::Value = serde_json::from_slice(&bytes).expect("parse json");

    // Verify response shape
    assert!(json["id"].is_string(), "should have id");
    assert_eq!(json["object"], "chat.completion");
    assert!(json["choices"].is_array(), "should have choices");
    assert!(json["usage"].is_object(), "should have usage");
}

#[tokio::test]
async fn chat_completions_response_has_usage_field() {
    let app = test_router();
    let body = serde_json::json!({
        "messages": [
            {"role": "user", "content": "Hi"}
        ],
        "max_tokens": 3
    });
    let req = Request::post("/v1/chat/completions")
        .header("content-type", "application/json")
        .body(Body::from(serde_json::to_string(&body).expect("serialize")))
        .expect("request");

    let resp = app.oneshot(req).await.expect("response");
    let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .expect("body");
    let json: serde_json::Value = serde_json::from_slice(&bytes).expect("parse json");

    let usage = &json["usage"];
    assert!(
        usage["prompt_tokens"].is_number(),
        "should have prompt_tokens"
    );
    assert!(
        usage["completion_tokens"].is_number(),
        "should have completion_tokens"
    );
    assert!(
        usage["total_tokens"].is_number(),
        "should have total_tokens"
    );
}

#[tokio::test]
async fn chat_completions_response_has_id_format() {
    let app = test_router();
    let body = serde_json::json!({
        "messages": [
            {"role": "user", "content": "Test"}
        ],
        "max_tokens": 1
    });
    let req = Request::post("/v1/chat/completions")
        .header("content-type", "application/json")
        .body(Body::from(serde_json::to_string(&body).expect("serialize")))
        .expect("request");

    let resp = app.oneshot(req).await.expect("response");
    let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .expect("body");
    let json: serde_json::Value = serde_json::from_slice(&bytes).expect("parse json");

    let id = json["id"].as_str().expect("id should be string");
    assert!(
        id.starts_with("chatcmpl-"),
        "id should start with chatcmpl-, got: {id}"
    );
    assert!(id.len() > 9, "id should have content after prefix");
}

#[tokio::test]
async fn chat_completions_choice_has_message() {
    let app = test_router();
    let body = serde_json::json!({
        "messages": [
            {"role": "user", "content": "Test"}
        ],
        "max_tokens": 1
    });
    let req = Request::post("/v1/chat/completions")
        .header("content-type", "application/json")
        .body(Body::from(serde_json::to_string(&body).expect("serialize")))
        .expect("request");

    let resp = app.oneshot(req).await.expect("response");
    let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .expect("body");
    let json: serde_json::Value = serde_json::from_slice(&bytes).expect("parse json");

    let choices = json["choices"].as_array().expect("choices");
    assert!(!choices.is_empty(), "should have at least one choice");
    let choice = &choices[0];
    assert_eq!(choice["index"], 0);
    assert_eq!(choice["message"]["role"], "assistant");
    assert!(choice["message"]["content"].is_string());
    assert_eq!(choice["finish_reason"], "stop");
}

// ══════════════════════════════════════════════════════════════
// build_prompt tests (via public server module)
// ══════════════════════════════════════════════════════════════

// build_prompt is private, but tested via the server module's existing
// inline tests. We can verify behavior through the HTTP endpoint.

#[tokio::test]
async fn multi_turn_conversation_via_api() {
    let app = test_router();
    let body = serde_json::json!({
        "messages": [
            {"role": "system", "content": "You are a calculator."},
            {"role": "user", "content": "What is 2+2?"},
            {"role": "assistant", "content": "4"},
            {"role": "user", "content": "And 3+3?"}
        ],
        "max_tokens": 1
    });
    let req = Request::post("/v1/chat/completions")
        .header("content-type", "application/json")
        .body(Body::from(serde_json::to_string(&body).expect("serialize")))
        .expect("request");

    let resp = app.oneshot(req).await.expect("response");
    assert_eq!(resp.status(), StatusCode::OK);
}

// ══════════════════════════════════════════════════════════════
// Default values
// ══════════════════════════════════════════════════════════════

#[tokio::test]
async fn default_max_tokens_applied() {
    // Verify that omitting max_tokens in JSON results in the default (256)
    // by deserializing the request body directly, rather than generating
    // 256 tokens which would be too slow.
    let raw = serde_json::json!({
        "messages": [
            {"role": "user", "content": "Hello"}
        ]
    });
    let parsed: pictor_runtime::server::ChatCompletionRequest =
        serde_json::from_value(raw).expect("deserialize request");
    assert_eq!(parsed.max_tokens, 256, "default max_tokens should be 256");

    // Also verify the endpoint still works with an explicit small max_tokens
    let app = test_router();
    let body = serde_json::json!({
        "messages": [
            {"role": "user", "content": "Hello"}
        ],
        "max_tokens": 1
    });
    let req = Request::post("/v1/chat/completions")
        .header("content-type", "application/json")
        .body(Body::from(serde_json::to_string(&body).expect("serialize")))
        .expect("request");

    let resp = app.oneshot(req).await.expect("response");
    assert_eq!(resp.status(), StatusCode::OK);
}

#[tokio::test]
async fn default_temperature_applied() {
    let app = test_router();
    // Omit temperature: should use default (0.7)
    let body = serde_json::json!({
        "messages": [
            {"role": "user", "content": "Hello"}
        ],
        "max_tokens": 1
    });
    let req = Request::post("/v1/chat/completions")
        .header("content-type", "application/json")
        .body(Body::from(serde_json::to_string(&body).expect("serialize")))
        .expect("request");

    let resp = app.oneshot(req).await.expect("response");
    assert_eq!(resp.status(), StatusCode::OK);
}

#[test]
fn create_router_without_tokenizer() {
    let config = Qwen3Config::tiny_test();
    let params = SamplingParams::default();
    let engine = InferenceEngine::new(config, params, 42);
    let _router = create_router(engine, None);
    // Should not panic
}

// ══════════════════════════════════════════════════════════════
// Prometheus /metrics endpoint
// ══════════════════════════════════════════════════════════════

#[tokio::test]
async fn metrics_endpoint_returns_prometheus_format() {
    // create_router internally delegates to create_router_with_metrics,
    // so the /metrics route is always present.
    let app = test_router();
    let req = Request::get("/metrics")
        .body(Body::empty())
        .expect("request");
    let resp = app.oneshot(req).await.expect("response");

    assert_eq!(resp.status(), StatusCode::OK);

    let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .expect("body");
    let text = std::str::from_utf8(&body).expect("utf8");

    // Prometheus text format must contain HELP and TYPE annotations.
    assert!(
        text.contains("# HELP"),
        "metrics body should contain '# HELP' lines, got: {text}"
    );
    assert!(
        text.contains("# TYPE"),
        "metrics body should contain '# TYPE' lines, got: {text}"
    );
    // Every Pictor counter must appear.
    assert!(
        text.contains("pictor_requests_total"),
        "metrics should include requests_total"
    );
    assert!(
        text.contains("pictor_tokens_generated_total"),
        "metrics should include tokens_generated_total"
    );
}

#[tokio::test]
async fn metrics_content_type_is_prometheus() {
    let app = test_router();
    let req = Request::get("/metrics")
        .body(Body::empty())
        .expect("request");
    let resp = app.oneshot(req).await.expect("response");

    let ct = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("");
    assert!(
        ct.contains("text/plain"),
        "content-type should be text/plain, got: {ct}"
    );
}

#[tokio::test]
async fn metrics_track_request_count() {
    // Build a router that shares a metrics instance we can inspect afterwards.
    let config = Qwen3Config::tiny_test();
    let params = SamplingParams::default();
    let engine = InferenceEngine::new(config, params, 42);
    let metrics = Arc::new(InferenceMetrics::new());

    let initial_count = metrics.requests_total.get();

    let app = create_router_with_metrics(engine, None, Arc::clone(&metrics));

    // Fire one chat completion request to increment the counter.
    let body = serde_json::json!({
        "messages": [{"role": "user", "content": "ping"}],
        "max_tokens": 1
    });
    let req = Request::post("/v1/chat/completions")
        .header("content-type", "application/json")
        .body(Body::from(serde_json::to_string(&body).expect("serialize")))
        .expect("request");

    let resp = app.oneshot(req).await.expect("response");
    assert_eq!(resp.status(), StatusCode::OK);

    // The handler increments requests_total before processing.
    assert!(
        metrics.requests_total.get() > initial_count,
        "requests_total should have incremented from {initial_count}, got {}",
        metrics.requests_total.get()
    );
}

#[tokio::test]
async fn metrics_endpoint_shows_incremented_counter_after_request() {
    let config = Qwen3Config::tiny_test();
    let params = SamplingParams::default();
    let engine = InferenceEngine::new(config, params, 42);
    let metrics = Arc::new(InferenceMetrics::new());
    let app = create_router_with_metrics(engine, None, Arc::clone(&metrics));

    // Make a chat request, then query /metrics and confirm the counter text.
    let _chat_body = serde_json::json!({
        "messages": [{"role": "user", "content": "hello"}],
        "max_tokens": 1
    });

    // We need two separate oneshot calls — clone the app via into_make_service.
    // Instead, pre-record via Arc and just check the metrics render directly.
    metrics.requests_total.inc_by(7);
    metrics.tokens_generated_total.inc_by(42);

    let req = Request::get("/metrics")
        .body(Body::empty())
        .expect("request");
    let resp = app.oneshot(req).await.expect("response");
    let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .expect("body");
    let text = std::str::from_utf8(&body).expect("utf8");

    assert!(
        text.contains("pictor_requests_total 7"),
        "should see incremented counter in Prometheus output, got:\n{text}"
    );
    assert!(
        text.contains("pictor_tokens_generated_total 42"),
        "should see token counter in Prometheus output"
    );
}

// ══════════════════════════════════════════════════════════════
// create_router delegates to create_router_with_metrics
// ══════════════════════════════════════════════════════════════

#[test]
fn create_router_delegates_to_metrics_variant() {
    // create_router must wire up the /metrics route (it calls
    // create_router_with_metrics internally).
    let config = Qwen3Config::tiny_test();
    let params = SamplingParams::default();
    let engine = InferenceEngine::new(config, params, 42);
    let _router = create_router(engine, None);
    // Construction alone verifies the delegation — no panic = success.
}

// ══════════════════════════════════════════════════════════════
// Temperature is honored by the base /v1/chat/completions endpoint
// ══════════════════════════════════════════════════════════════

/// Request → SamplingParams mapping (b): an explicit `temperature` in the body
/// is carried through, and omitting it falls back to the engine/server default
/// of 0.7 — matching `SamplingParams::default().temperature`, which is what the
/// shipped `pictor serve` constructs its engine with.
#[test]
fn request_temperature_maps_into_sampling_params() {
    // Explicit temperature is preserved verbatim.
    let raw = serde_json::json!({
        "messages": [{"role": "user", "content": "hi"}],
        "temperature": 0.0
    });
    let parsed: pictor_runtime::server::ChatCompletionRequest =
        serde_json::from_value(raw).expect("deserialize request with temperature");
    assert_eq!(parsed.temperature, 0.0, "explicit temperature must be kept");

    let explicit = SamplingParams {
        temperature: parsed.temperature,
        ..SamplingParams::default()
    };
    assert_eq!(explicit.temperature, 0.0);
    // Every other knob must equal today's effective default so omitting them is
    // behavior-preserving.
    let def = SamplingParams::default();
    assert_eq!(explicit.top_k, def.top_k);
    assert_eq!(explicit.top_p, def.top_p);
    assert_eq!(explicit.repetition_penalty, def.repetition_penalty);

    // Omitting temperature falls back to 0.7 (== SamplingParams::default()).
    let raw_omitted = serde_json::json!({
        "messages": [{"role": "user", "content": "hi"}]
    });
    let parsed_omitted: pictor_runtime::server::ChatCompletionRequest =
        serde_json::from_value(raw_omitted).expect("deserialize request without temperature");
    assert!(
        (parsed_omitted.temperature - 0.7).abs() < f32::EPSILON,
        "omitted temperature must default to 0.7, got {}",
        parsed_omitted.temperature
    );
    assert!(
        (parsed_omitted.temperature - SamplingParams::default().temperature).abs() < f32::EPSILON,
        "default temperature must match SamplingParams::default()"
    );
}

/// End-to-end (a): two identical `temperature: 0.0` requests through the base
/// endpoint must produce byte-identical content. Before the fix the handler
/// discarded `temperature` and always used the startup sampler (effective
/// temp 0.7 → stochastic), so this would be flaky/non-greedy. Now temp=0 routes
/// to deterministic greedy argmax, so the two completions must match exactly.
#[tokio::test]
async fn base_endpoint_temperature_zero_is_deterministic() {
    async fn run_once() -> String {
        // Fresh engine per call so RNG/KV state cannot leak between the two runs;
        // greedy (temp=0) output must be identical regardless.
        let engine = InferenceEngine::new(Qwen3Config::tiny_test(), SamplingParams::default(), 42);
        let app = create_router(engine, None);
        let body = serde_json::json!({
            "messages": [{"role": "user", "content": "Hello"}],
            "max_tokens": 8,
            "temperature": 0.0
        });
        let req = Request::post("/v1/chat/completions")
            .header("content-type", "application/json")
            .body(Body::from(serde_json::to_string(&body).expect("serialize")))
            .expect("request");
        let resp = app.oneshot(req).await.expect("response");
        assert_eq!(resp.status(), StatusCode::OK);
        let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
            .await
            .expect("body");
        let json: serde_json::Value = serde_json::from_slice(&bytes).expect("parse json");
        json["choices"][0]["message"]["content"]
            .as_str()
            .expect("content should be a string")
            .to_string()
    }

    let first = run_once().await;
    let second = run_once().await;
    // Non-trivial: greedy decoding on the tiny model emits real tokens, so the
    // comparison below isn't just "" == "".
    assert!(
        !first.is_empty(),
        "temperature=0 greedy run should produce content, got empty"
    );
    assert_eq!(
        first, second,
        "temperature=0 through the base endpoint must be deterministic (greedy); \
         got {first:?} vs {second:?}"
    );
}