oxillama-server 0.1.3

OpenAI-compatible HTTP API server for OxiLLaMa
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
//! Background batch processing worker.
//!
//! The `BatchWorker` runs as a dedicated Tokio task.  It receives job IDs
//! from the `BatchQueue`, reads the input JSONL from disk line-by-line, sends
//! each line as a generate request to the inference engine via the existing
//! `BatchRequest` queue, collects results, and writes output JSONL back to
//! disk atomically.
//!
//! The worker checks `cancel_requested` in `status.json` after every line and
//! marks remaining lines as cancelled if the flag is set.

use std::sync::Arc;

use serde_json::Value;
use tokio::sync::mpsc;
use tracing::{debug, error, info, warn};

use crate::batch_spool::queue::BatchQueueReceiver;
use crate::batch_spool::store::{BatchJobStatus, BatchStore};
use crate::queue::{BatchRequest, UsageStats};

/// Spawn the batch background worker task.
///
/// - `rx` — work-item receiver from `BatchQueue`.
/// - `inference_tx` — sender into the inference queue (shared with route handlers).
/// - `store` — disk-backed store reference.
pub fn spawn_batch_worker(
    mut rx: BatchQueueReceiver,
    inference_tx: mpsc::Sender<BatchRequest>,
    store: Arc<BatchStore>,
) {
    tokio::spawn(async move {
        info!("batch worker started");
        while let Some(item) = rx.recv().await {
            let job_id = item.job_id.clone();
            debug!(job_id, "batch worker picked up job");

            if let Err(e) = process_job(&job_id, &inference_tx, &store).await {
                error!(job_id, error = %e, "batch job failed");
                // Best-effort: mark as failed on disk.
                if let Ok(mut meta) = store.read_status(&job_id) {
                    meta.status = BatchJobStatus::Failed;
                    meta.updated_at = unix_now();
                    let _ = store.update_status(&job_id, &meta);
                }
            }
        }
        info!("batch worker queue closed — exiting");
    });
}

/// Process a single batch job end-to-end.
async fn process_job(
    job_id: &str,
    inference_tx: &mpsc::Sender<BatchRequest>,
    store: &Arc<BatchStore>,
) -> Result<(), String> {
    // Read input lines.
    let input_lines = tokio::task::spawn_blocking({
        let store = Arc::clone(store);
        let job_id = job_id.to_string();
        move || store.read_input_lines(&job_id)
    })
    .await
    .map_err(|e| format!("spawn_blocking join error: {e}"))?
    .map_err(|e| format!("read_input_lines: {e}"))?;

    // Update status to InProgress.
    {
        let store = Arc::clone(store);
        let job_id = job_id.to_string();
        tokio::task::spawn_blocking(move || -> std::io::Result<()> {
            let mut meta = store.read_status(&job_id)?;
            meta.status = BatchJobStatus::InProgress;
            meta.updated_at = unix_now();
            store.update_status(&job_id, &meta)
        })
        .await
        .map_err(|e| format!("spawn_blocking join: {e}"))?
        .map_err(|e| format!("update InProgress: {e}"))?;
    }

    let total = input_lines.len() as u32;
    let mut completed = 0_u32;
    let mut failed = 0_u32;

    for (line_idx, line) in input_lines.iter().enumerate() {
        // Check cancel flag before each line.
        {
            let store_c = Arc::clone(store);
            let job_id_c = job_id.to_string();
            let cancelled = tokio::task::spawn_blocking(move || -> bool {
                store_c
                    .read_status(&job_id_c)
                    .map(|m| m.cancel_requested)
                    .unwrap_or(false)
            })
            .await
            .unwrap_or(false);

            if cancelled {
                // Mark all remaining lines as cancelled.
                let remaining = total - completed - failed;
                let store_c = Arc::clone(store);
                let job_id_c = job_id.to_string();
                tokio::task::spawn_blocking(move || -> std::io::Result<()> {
                    let mut meta = store_c.read_status(&job_id_c)?;
                    meta.status = BatchJobStatus::Cancelled;
                    meta.failed_lines += remaining;
                    meta.updated_at = unix_now();
                    store_c.update_status(&job_id_c, &meta)
                })
                .await
                .ok();

                // Write cancelled markers for remaining lines.
                for i in (line_idx as u32)..total {
                    let cancelled_line = serde_json::json!({
                        "custom_id": format!("line-{i}"),
                        "status": "cancelled",
                    });
                    let store_c = Arc::clone(store);
                    let job_id_c = job_id.to_string();
                    let line_str = cancelled_line.to_string();
                    tokio::task::spawn_blocking(move || {
                        let _ = store_c.append_output(&job_id_c, &line_str);
                    })
                    .await
                    .ok();
                }
                return Ok(());
            }
        }

        // Parse the line as a BatchRequestItem.
        let request_body = match parse_request_line(line) {
            Ok(body) => body,
            Err(e) => {
                warn!(
                    job_id,
                    line = line_idx,
                    error = %e,
                    "skipping malformed batch input line"
                );
                let error_record = serde_json::json!({
                    "custom_id": format!("line-{line_idx}"),
                    "error": e,
                });
                let store_c = Arc::clone(store);
                let job_id_c = job_id.to_string();
                let line_str = error_record.to_string();
                tokio::task::spawn_blocking(move || {
                    let _ = store_c.append_error(&job_id_c, &line_str);
                })
                .await
                .ok();
                failed += 1;
                continue;
            }
        };

        // Submit to the inference queue and wait for the result.
        let (reply_tx, reply_rx) =
            tokio::sync::oneshot::channel::<Result<(String, UsageStats), String>>();

        let prompt = extract_prompt(&request_body);
        let max_tokens = extract_max_tokens(&request_body);
        let sampler = oxillama_runtime::sampling::SamplerConfig::default();

        if inference_tx
            .send(BatchRequest::Generate {
                prompt,
                max_tokens,
                config: sampler,
                cache_prompt: false, // batch jobs use full prefill
                lora_selection: vec![],
                reply: reply_tx,
            })
            .await
            .is_err()
        {
            error!(
                job_id,
                line = line_idx,
                "inference queue closed during batch"
            );
            failed += 1;
            continue;
        }

        let result = match reply_rx.await {
            Ok(r) => r,
            Err(e) => {
                error!(job_id, line = line_idx, error = %e, "reply channel closed");
                failed += 1;
                continue;
            }
        };

        let (output_line, success) = match result {
            Ok((text, usage)) => {
                let record = serde_json::json!({
                    "custom_id": format!("line-{line_idx}"),
                    "response": {
                        "status_code": 200,
                        "body": {
                            "choices": [{"message": {"role": "assistant", "content": text}}],
                            "usage": {
                                "prompt_tokens": usage.prompt_tokens,
                                "completion_tokens": usage.completion_tokens,
                                "total_tokens": usage.total_tokens,
                            }
                        }
                    }
                });
                (record.to_string(), true)
            }
            Err(e) => {
                let record = serde_json::json!({
                    "custom_id": format!("line-{line_idx}"),
                    "error": e,
                });
                (record.to_string(), false)
            }
        };

        if success {
            completed += 1;
        } else {
            failed += 1;
        }

        // Append output line and update status.
        {
            let store_c = Arc::clone(store);
            let job_id_c = job_id.to_string();
            let line_str = output_line.clone();
            tokio::task::spawn_blocking(move || {
                let _ = store_c.append_output(&job_id_c, &line_str);
            })
            .await
            .ok();
        }

        {
            let store_c = Arc::clone(store);
            let job_id_c = job_id.to_string();
            let c = completed;
            let f = failed;
            tokio::task::spawn_blocking(move || -> std::io::Result<()> {
                let mut meta = store_c.read_status(&job_id_c)?;
                meta.completed_lines = c;
                meta.failed_lines = f;
                meta.updated_at = unix_now();
                store_c.update_status(&job_id_c, &meta)
            })
            .await
            .ok();
        }
    }

    // Finalize job status.
    {
        let store_c = Arc::clone(store);
        let job_id_c = job_id.to_string();
        tokio::task::spawn_blocking(move || -> std::io::Result<()> {
            let mut meta = store_c.read_status(&job_id_c)?;
            meta.status = if meta.failed_lines == 0 {
                BatchJobStatus::Completed
            } else {
                BatchJobStatus::Failed
            };
            meta.completed_lines = completed;
            meta.failed_lines = failed;
            meta.updated_at = unix_now();
            store_c.update_status(&job_id_c, &meta)
        })
        .await
        .map_err(|e| format!("spawn_blocking join: {e}"))?
        .map_err(|e| format!("finalize status: {e}"))?;
    }

    info!(job_id, completed, failed, "batch job complete");
    Ok(())
}

/// Parse a JSONL line into a request body `Value`.
fn parse_request_line(line: &str) -> Result<Value, String> {
    serde_json::from_str(line.trim()).map_err(|e| format!("JSON parse error: {e}"))
}

/// Extract a prompt string from a batch request body.
///
/// Supports both `{ "prompt": "..." }` (completions) and
/// `{ "messages": [...] }` (chat completions) formats.
fn extract_prompt(body: &Value) -> String {
    // Chat completions format.
    if let Some(messages) = body.get("messages").and_then(|m| m.as_array()) {
        let mut prompt = String::new();
        for msg in messages {
            let role = msg.get("role").and_then(|r| r.as_str()).unwrap_or("user");
            let content = msg.get("content").and_then(|c| c.as_str()).unwrap_or("");
            prompt.push_str(&format!("<|{role}|>\n{content}\n<|end|>\n"));
        }
        prompt.push_str("<|assistant|>\n");
        return prompt;
    }
    // Completions format.
    body.get("prompt")
        .and_then(|p| p.as_str())
        .unwrap_or("")
        .to_string()
}

/// Extract `max_tokens` from a batch request body, defaulting to 256.
fn extract_max_tokens(body: &Value) -> usize {
    body.get("max_tokens")
        .and_then(|v| v.as_u64())
        .map(|n| n as usize)
        .unwrap_or(256)
}

fn unix_now() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs() as i64)
        .unwrap_or(0)
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::batch_spool::store::BatchStore;
    use crate::queue::UsageStats;
    use std::env::temp_dir;

    fn temp_store(tag: &str) -> Arc<BatchStore> {
        let id = uuid::Uuid::new_v4().as_simple().to_string();
        let dir = temp_dir().join(format!("oxillama_batch_worker_test_{tag}_{id}"));
        Arc::new(BatchStore::new(dir).expect("store"))
    }

    /// (a) worker_processes_all_lines — submit a 5-line batch with a mock
    ///     inference engine; output.jsonl should have 5 lines when complete.
    #[tokio::test]
    async fn worker_processes_all_lines() {
        let store = temp_store("processes_all");
        let job_id = "worker_job_a";

        // Build 5 input lines.
        let mut input = String::new();
        for i in 0..5_u32 {
            input.push_str(&format!(r#"{{"prompt":"hello {i}","max_tokens":5}}"#));
            input.push('\n');
        }
        store
            .create_job(job_id, &input, "/v1/completions", 5)
            .expect("create_job");

        // Spin up a mock inference worker.
        let (inference_tx, mut inference_rx) = tokio::sync::mpsc::channel::<BatchRequest>(16);

        tokio::spawn(async move {
            while let Some(req) = inference_rx.recv().await {
                if let BatchRequest::Generate { reply, .. } = req {
                    let _ = reply.send(Ok((
                        "mock output".to_string(),
                        UsageStats {
                            prompt_tokens: 2,
                            completion_tokens: 3,
                            total_tokens: 5,
                        },
                    )));
                }
            }
        });

        // Create queue and batch worker.
        let (batch_tx, batch_rx) = crate::batch_spool::queue::new_batch_queue(8);
        spawn_batch_worker(batch_rx, inference_tx, Arc::clone(&store));

        // Submit the job.
        batch_tx
            .send(crate::batch_spool::queue::BatchWorkItem {
                job_id: job_id.to_string(),
            })
            .await
            .expect("send job");

        // Wait for completion (poll with timeout).
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
        loop {
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
            let meta = store.read_status(job_id).expect("read status");
            if matches!(
                meta.status,
                BatchJobStatus::Completed | BatchJobStatus::Failed
            ) {
                break;
            }
            if std::time::Instant::now() > deadline {
                panic!("batch job did not complete within deadline");
            }
        }

        let output_lines = store.read_output_lines(job_id).expect("read output");
        assert_eq!(
            output_lines.len(),
            5,
            "output.jsonl should have exactly 5 lines, got: {output_lines:?}"
        );
    }

    /// (b) worker_cancels_remaining — cancel after line 2; lines 3-5 are cancelled.
    #[tokio::test]
    async fn worker_cancels_remaining() {
        let store = temp_store("cancels");
        let job_id = "worker_job_b";

        // Build 5 slow input lines.
        let mut input = String::new();
        for i in 0..5_u32 {
            input.push_str(&format!(r#"{{"prompt":"item {i}","max_tokens":5}}"#));
            input.push('\n');
        }
        store
            .create_job(job_id, &input, "/v1/completions", 5)
            .expect("create_job");

        // Slow mock inference worker — allows time to set cancel flag.
        let (inference_tx, mut inference_rx) = tokio::sync::mpsc::channel::<BatchRequest>(16);

        let store_for_cancel = Arc::clone(&store);
        let job_id_str = job_id.to_string();

        tokio::spawn(async move {
            let mut count = 0_u32;
            while let Some(req) = inference_rx.recv().await {
                count += 1;
                if let BatchRequest::Generate { reply, .. } = req {
                    // After the 2nd line, set cancel flag.
                    if count == 2 {
                        if let Ok(mut meta) = store_for_cancel.read_status(&job_id_str) {
                            meta.cancel_requested = true;
                            let _ = store_for_cancel.update_status(&job_id_str, &meta);
                        }
                    }
                    // Small delay to give worker a chance to check the flag.
                    tokio::time::sleep(std::time::Duration::from_millis(10)).await;
                    let _ = reply.send(Ok((
                        "output".to_string(),
                        UsageStats {
                            prompt_tokens: 1,
                            completion_tokens: 1,
                            total_tokens: 2,
                        },
                    )));
                }
            }
        });

        let (batch_tx, batch_rx) = crate::batch_spool::queue::new_batch_queue(4);
        spawn_batch_worker(batch_rx, inference_tx, Arc::clone(&store));

        batch_tx
            .send(crate::batch_spool::queue::BatchWorkItem {
                job_id: job_id.to_string(),
            })
            .await
            .expect("send");

        // Wait for cancellation.
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
        loop {
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
            let meta = store.read_status(job_id).expect("read status");
            if matches!(
                meta.status,
                BatchJobStatus::Cancelled | BatchJobStatus::Completed | BatchJobStatus::Failed
            ) {
                break;
            }
            if std::time::Instant::now() > deadline {
                panic!("batch job did not reach terminal status within deadline");
            }
        }

        let final_meta = store.read_status(job_id).expect("read final status");
        assert_eq!(
            final_meta.status,
            BatchJobStatus::Cancelled,
            "job should be cancelled: {final_meta:?}"
        );
    }
}