oxibonsai-runtime 0.1.4

Inference runtime, sampling, tokenizer, and server for OxiBonsai
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
//! Integration tests for the RAG HTTP endpoints.
//!
//! Uses `tower::ServiceExt::oneshot` to exercise handlers in-process,
//! mirroring the pattern used in `server_tests.rs`.

#[cfg(feature = "rag")]
mod tests {
    use axum::body::Body;
    use axum::http::{Method, Request, StatusCode};
    use tower::ServiceExt;

    use oxibonsai_core::config::Qwen3Config;
    use oxibonsai_runtime::engine::InferenceEngine;
    use oxibonsai_runtime::rag_server::create_rag_router;
    use oxibonsai_runtime::sampling::SamplingParams;

    // ── Helpers ──────────────────────────────────────────────────────────────

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

    fn json_body(value: serde_json::Value) -> Body {
        Body::from(serde_json::to_string(&value).expect("serialize"))
    }

    fn json_request(method: Method, path: &str, body: serde_json::Value) -> Request<Body> {
        Request::builder()
            .method(method)
            .uri(path)
            .header("content-type", "application/json")
            .body(json_body(body))
            .expect("build request")
    }

    async fn body_json(resp: axum::response::Response) -> serde_json::Value {
        let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
            .await
            .expect("read body");
        serde_json::from_slice(&bytes).expect("parse JSON")
    }

    // ── test_index_documents_returns_200 ─────────────────────────────────────

    #[tokio::test]
    async fn test_index_documents_returns_200() {
        let app = test_rag_router();

        let req = json_request(
            Method::POST,
            "/rag/index",
            serde_json::json!({
                "documents": [
                    "Rust is a systems programming language emphasising memory safety.",
                    "Axum is an ergonomic and modular web framework built on Tokio.",
                    "RAG combines retrieval with language model generation."
                ]
            }),
        );

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

        let json = body_json(resp).await;
        assert_eq!(
            json["indexed"], 3,
            "three documents should be indexed; got {json}"
        );
        assert!(
            json["chunks"].as_u64().unwrap_or(0) >= 1,
            "should produce at least one chunk; got {json}"
        );
        let ids = json["document_ids"].as_array().expect("document_ids array");
        assert_eq!(ids.len(), 3, "should have one id per document");
    }

    // ── test_index_empty_documents_handled ───────────────────────────────────

    #[tokio::test]
    async fn test_index_empty_documents_handled() {
        let app = test_rag_router();

        let req = json_request(
            Method::POST,
            "/rag/index",
            serde_json::json!({ "documents": [] }),
        );

        let resp = app.oneshot(req).await.expect("response");
        assert_eq!(
            resp.status(),
            StatusCode::BAD_REQUEST,
            "empty documents list should return 400"
        );

        let json = body_json(resp).await;
        assert!(
            json["error"].is_string(),
            "response should contain an error field; got {json}"
        );
    }

    // ── test_rag_stats_returns_json ───────────────────────────────────────────

    #[tokio::test]
    async fn test_rag_stats_returns_json() {
        let app = test_rag_router();

        let req = Request::get("/rag/stats")
            .body(Body::empty())
            .expect("build request");

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

        let json = body_json(resp).await;
        assert!(
            json["documents_indexed"].is_number(),
            "stats must contain documents_indexed; got {json}"
        );
        assert!(
            json["chunks_indexed"].is_number(),
            "stats must contain chunks_indexed; got {json}"
        );
        assert!(
            json["embedding_dim"].is_number(),
            "stats must contain embedding_dim; got {json}"
        );
        assert!(
            json["store_memory_bytes"].is_number(),
            "stats must contain store_memory_bytes; got {json}"
        );
        assert!(
            json["store_memory_human"].is_string(),
            "stats must contain store_memory_human; got {json}"
        );
    }

    // ── test_rag_query_returns_answer ─────────────────────────────────────────

    #[tokio::test]
    async fn test_rag_query_returns_answer() {
        // We need two separate one-shot calls; use a make_service approach or
        // build the router twice.  For simplicity we build two routers backed
        // by independent state: one to index, one to query — but then they
        // share no state.  Instead, we verify the query endpoint works even
        // when the store is empty (pipeline answers with empty context).
        let app = test_rag_router();

        let req = json_request(
            Method::POST,
            "/rag/query",
            serde_json::json!({
                "query": "What is Rust?",
                "max_tokens": 5
            }),
        );

        let resp = app.oneshot(req).await.expect("response");
        assert_eq!(
            resp.status(),
            StatusCode::OK,
            "rag query should return 200 even with empty index"
        );

        let json = body_json(resp).await;
        assert!(
            json["answer"].is_string(),
            "response must have answer field; got {json}"
        );
        assert!(
            json["prompt_used"].is_string(),
            "response must have prompt_used; got {json}"
        );
        assert!(
            json["usage"].is_object(),
            "response must have usage; got {json}"
        );
        let usage = &json["usage"];
        assert!(usage["completion_tokens"].is_number());
        assert!(usage["prompt_tokens"].is_number());
        assert!(usage["chunks_retrieved"].is_number());
        assert!(usage["documents_searched"].is_number());
    }

    // ── test_clear_index_resets_stats ─────────────────────────────────────────

    #[tokio::test]
    async fn test_clear_index_resets_stats() {
        // Build a router backed by its own shared state.
        let config = Qwen3Config::tiny_test();
        let engine = InferenceEngine::new(config, SamplingParams::default(), 42);
        let router = create_rag_router(engine);

        // We need multiple requests on the same router state.  axum routers
        // implement `Service + Clone`, so we can call `into_make_service` and
        // use `tower_service::Service::call` directly.  Simpler: use the router
        // via `axum::serve` in a test server — but that requires a port.
        //
        // The easiest approach: wrap the router in a `Arc<Mutex<Router>>` or
        // share via `axum::Router::into_make_service` with a memory-listener.
        // For this test we use `hyper`'s in-memory transport via axum helpers.
        //
        // Actually the simplest correct approach: use the router directly via
        // `axum::Router`'s `Service` impl.  Each `oneshot` consumes the router,
        // but we can clone it first (Router: Clone).
        let index_req = json_request(
            Method::POST,
            "/rag/index",
            serde_json::json!({
                "documents": [
                    "Rust memory safety ensures freedom from data races.",
                    "Axum is built on top of Hyper and Tokio."
                ]
            }),
        );

        let resp = router
            .clone()
            .oneshot(index_req)
            .await
            .expect("index response");
        assert_eq!(resp.status(), StatusCode::OK, "indexing should succeed");

        // After indexing, stats should show documents.
        let stats_resp = router
            .clone()
            .oneshot(Request::get("/rag/stats").body(Body::empty()).expect("req"))
            .await
            .expect("stats response");
        let stats = body_json(stats_resp).await;
        assert_eq!(
            stats["documents_indexed"], 2,
            "after indexing 2 docs, stats should show 2; got {stats}"
        );

        // Clear the index.
        let clear_req = Request::builder()
            .method(Method::DELETE)
            .uri("/rag/index")
            .body(Body::empty())
            .expect("delete request");
        let clear_resp = router
            .clone()
            .oneshot(clear_req)
            .await
            .expect("clear response");
        assert_eq!(clear_resp.status(), StatusCode::OK);
        let clear_json = body_json(clear_resp).await;
        assert_eq!(
            clear_json["status"], "cleared",
            "clear response should contain status:cleared; got {clear_json}"
        );

        // Stats after clearing should show 0 documents.
        let stats_after_resp = router
            .clone()
            .oneshot(Request::get("/rag/stats").body(Body::empty()).expect("req"))
            .await
            .expect("stats after clear");
        let stats_after = body_json(stats_after_resp).await;
        assert_eq!(
            stats_after["documents_indexed"], 0,
            "after clear, documents_indexed should be 0; got {stats_after}"
        );
        assert_eq!(
            stats_after["chunks_indexed"], 0,
            "after clear, chunks_indexed should be 0; got {stats_after}"
        );
    }

    // ── test_rag_query_includes_context_when_requested ────────────────────────

    #[tokio::test]
    async fn test_rag_query_includes_context_when_requested() {
        let app = test_rag_router();

        // Request with include_context: true
        let req = json_request(
            Method::POST,
            "/rag/query",
            serde_json::json!({
                "query": "Tell me about memory safety.",
                "max_tokens": 3,
                "include_context": true
            }),
        );

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

        let json = body_json(resp).await;
        // retrieved_chunks should be present (may be empty if store is empty,
        // but the field itself must not be null).
        assert!(
            json["retrieved_chunks"].is_array(),
            "retrieved_chunks should be an array when include_context=true; got {json}"
        );
    }

    #[tokio::test]
    async fn test_rag_query_omits_context_by_default() {
        let app = test_rag_router();

        let req = json_request(
            Method::POST,
            "/rag/query",
            serde_json::json!({
                "query": "What is Axum?",
                "max_tokens": 3
            }),
        );

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

        let json = body_json(resp).await;
        assert!(
            json["retrieved_chunks"].is_null(),
            "retrieved_chunks should be absent when include_context is not set; got {json}"
        );
    }

    // ── test_rag_router_wires_all_routes ──────────────────────────────────────

    #[tokio::test]
    async fn test_rag_router_wires_all_routes() {
        // Verify that all four expected routes are registered by issuing a
        // request to each and confirming we do NOT get 404/405 Method Not
        // Allowed for routes that should exist.

        // GET /rag/stats
        {
            let app = test_rag_router();
            let req = Request::get("/rag/stats").body(Body::empty()).expect("req");
            let resp = app.oneshot(req).await.expect("resp");
            assert_ne!(
                resp.status(),
                StatusCode::NOT_FOUND,
                "GET /rag/stats should be registered"
            );
        }

        // POST /rag/index
        {
            let app = test_rag_router();
            let req = json_request(
                Method::POST,
                "/rag/index",
                serde_json::json!({ "documents": ["hello world this is a test document"] }),
            );
            let resp = app.oneshot(req).await.expect("resp");
            assert_ne!(
                resp.status(),
                StatusCode::NOT_FOUND,
                "POST /rag/index should be registered"
            );
        }

        // DELETE /rag/index
        {
            let app = test_rag_router();
            let req = Request::builder()
                .method(Method::DELETE)
                .uri("/rag/index")
                .body(Body::empty())
                .expect("req");
            let resp = app.oneshot(req).await.expect("resp");
            assert_ne!(
                resp.status(),
                StatusCode::NOT_FOUND,
                "DELETE /rag/index should be registered"
            );
            assert_eq!(
                resp.status(),
                StatusCode::OK,
                "DELETE /rag/index should return 200"
            );
        }

        // POST /rag/query
        {
            let app = test_rag_router();
            let req = json_request(
                Method::POST,
                "/rag/query",
                serde_json::json!({ "query": "test query", "max_tokens": 1 }),
            );
            let resp = app.oneshot(req).await.expect("resp");
            assert_ne!(
                resp.status(),
                StatusCode::NOT_FOUND,
                "POST /rag/query should be registered"
            );
        }
    }

    // ── test_rag_query_rejects_empty_query ────────────────────────────────────

    #[tokio::test]
    async fn test_rag_query_rejects_empty_query() {
        let app = test_rag_router();

        let req = json_request(
            Method::POST,
            "/rag/query",
            serde_json::json!({ "query": "   " }),
        );

        let resp = app.oneshot(req).await.expect("response");
        assert_eq!(
            resp.status(),
            StatusCode::BAD_REQUEST,
            "blank query should return 400"
        );

        let json = body_json(resp).await;
        assert!(
            json["error"].is_string(),
            "error field should be present; got {json}"
        );
    }
}