cognis-core 0.2.0

Core traits and types for the Cognis LLM framework
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
use std::sync::Arc;

use async_trait::async_trait;
use serde_json::Value;
use tokio::sync::Semaphore;

use crate::error::{CognisError, Result};

use super::base::Runnable;
use super::config::RunnableConfig;

/// Invoke a runnable on multiple inputs concurrently, with optional concurrency limits.
///
/// Uses `tokio::sync::Semaphore` to bound parallelism. Results are returned in the
/// same order as inputs, regardless of completion order. Individual failures do not
/// prevent other inputs from being processed.
///
/// # Arguments
/// * `runnable` - The runnable to invoke on each input.
/// * `inputs` - A vector of JSON values to process.
/// * `max_concurrency` - Optional upper bound on concurrent invocations. If `None`,
///   all inputs are processed concurrently.
/// * `config` - Optional config forwarded to each invocation.
///
/// # Returns
/// A `Vec<Result<Value>>` with one result per input, preserving order.
pub async fn batch_invoke(
    runnable: &dyn Runnable,
    inputs: Vec<Value>,
    max_concurrency: Option<usize>,
    config: Option<&RunnableConfig>,
) -> Vec<Result<Value>> {
    if inputs.is_empty() {
        return Vec::new();
    }

    let concurrency = max_concurrency.unwrap_or(inputs.len());
    let semaphore = Arc::new(Semaphore::new(concurrency));

    let mut handles = Vec::with_capacity(inputs.len());

    // We need to collect futures that can be joined. Since `runnable` is not 'static,
    // we use `tokio::task::spawn` indirectly by driving futures on the current task
    // with index tracking.
    let config_owned = config.cloned();

    for (idx, input) in inputs.into_iter().enumerate() {
        let permit = semaphore.clone().acquire_owned().await.unwrap();
        let cfg = config_owned.clone();

        // We can't spawn because `runnable` isn't 'static, so we collect futures
        // and drive them with buffer_unordered. But for semaphore-based control,
        // we use a different approach: spawn tasks with Arc<dyn Runnable>.
        // However, `runnable` is a reference. We'll use the futures approach instead.
        drop(permit); // We'll use a different pattern below
        handles.push((idx, input, cfg));
    }

    // Use futures stream with semaphore for proper concurrency control
    use futures::stream::{self, StreamExt};

    let results: Vec<(usize, Result<Value>)> = stream::iter(handles)
        .map(|(idx, input, cfg)| {
            let sem = semaphore.clone();
            async move {
                let _permit = sem.acquire().await.unwrap();
                let result = runnable.invoke(input, cfg.as_ref()).await;
                (idx, result)
            }
        })
        .buffer_unordered(concurrency)
        .collect()
        .await;

    // Reconstruct ordered results
    let len = results.len();
    let mut ordered = Vec::with_capacity(len);
    ordered.resize_with(len, || Err(CognisError::Other("missing result".into())));
    for (idx, result) in results {
        ordered[idx] = result;
    }

    ordered
}

/// A runnable wrapper that pre-configures batch processing settings.
///
/// `RunnableBatch` wraps an inner `Runnable` and applies batch semantics:
/// - Input: a JSON array of items to process
/// - Output: a JSON array of results (one per input item)
///
/// If any individual invocation fails, the corresponding entry in the output
/// array will be `null` and the error is collected separately.
///
/// Batch concurrency is controlled by the `max_concurrency` field.
pub struct RunnableBatch {
    inner: Arc<dyn Runnable>,
    max_concurrency: Option<usize>,
    name: String,
    /// If true, return errors as `{"error": "..."}` objects in the output array
    /// instead of propagating the first error.
    return_exceptions: bool,
}

impl RunnableBatch {
    /// Create a new `RunnableBatch` wrapping the given runnable.
    pub fn new(inner: Arc<dyn Runnable>) -> Self {
        let name = format!("RunnableBatch<{}>", inner.name());
        Self {
            inner,
            max_concurrency: None,
            name,
            return_exceptions: true,
        }
    }

    /// Set the maximum number of concurrent invocations.
    pub fn with_max_concurrency(mut self, max_concurrency: usize) -> Self {
        self.max_concurrency = Some(max_concurrency);
        self
    }

    /// Set whether to return exceptions inline or propagate the first error.
    ///
    /// When `true` (default), failed items produce `{"error": "..."}` in the output array.
    /// When `false`, the first error causes the entire batch to fail.
    pub fn with_return_exceptions(mut self, return_exceptions: bool) -> Self {
        self.return_exceptions = return_exceptions;
        self
    }
}

#[async_trait]
impl Runnable for RunnableBatch {
    fn name(&self) -> &str {
        &self.name
    }

    /// Invoke the batch runnable.
    ///
    /// Input must be a JSON array. Each element is processed through the inner
    /// runnable. Output is a JSON array of results.
    async fn invoke(&self, input: Value, config: Option<&RunnableConfig>) -> Result<Value> {
        let items = input
            .as_array()
            .ok_or_else(|| CognisError::TypeMismatch {
                expected: "Array".into(),
                got: input_type_name(&input).to_string(),
            })?
            .clone();

        // Determine effective max_concurrency: explicit setting takes precedence,
        // then config, then unlimited.
        let effective_concurrency = self
            .max_concurrency
            .or_else(|| config.and_then(|c| c.max_concurrency));

        let results = batch_invoke(self.inner.as_ref(), items, effective_concurrency, config).await;

        if self.return_exceptions {
            // Convert results to JSON array, representing errors as objects
            let output: Vec<Value> = results
                .into_iter()
                .map(|r| match r {
                    Ok(v) => v,
                    Err(e) => serde_json::json!({"error": e.to_string()}),
                })
                .collect();
            Ok(Value::Array(output))
        } else {
            // Propagate the first error, or collect all successes
            let mut output = Vec::with_capacity(results.len());
            for r in results {
                output.push(r?);
            }
            Ok(Value::Array(output))
        }
    }
}

/// Helper to get a human-readable type name for a JSON value.
fn input_type_name(v: &Value) -> &'static str {
    match v {
        Value::Null => "Null",
        Value::Bool(_) => "Bool",
        Value::Number(_) => "Number",
        Value::String(_) => "String",
        Value::Array(_) => "Array",
        Value::Object(_) => "Object",
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::runnables::RunnableLambda;
    use serde_json::json;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::time::{Duration, Instant};

    /// Helper: creates a RunnableLambda that doubles an integer input.
    fn doubler() -> RunnableLambda {
        RunnableLambda::new("doubler", |v: Value| async move {
            let n = v.as_i64().ok_or_else(|| CognisError::TypeMismatch {
                expected: "integer".into(),
                got: format!("{v}"),
            })?;
            Ok(json!(n * 2))
        })
    }

    /// Helper: creates a slow doubler for timing tests.
    fn slow_doubler(delay_ms: u64) -> RunnableLambda {
        RunnableLambda::new("slow_doubler", move |v: Value| async move {
            tokio::time::sleep(Duration::from_millis(delay_ms)).await;
            let n = v.as_i64().unwrap();
            Ok(json!(n * 2))
        })
    }

    // ---- batch_invoke tests ----

    #[tokio::test]
    async fn test_batch_invoke_basic() {
        let runnable = doubler();
        let inputs = vec![json!(1), json!(2), json!(3), json!(4)];
        let results = batch_invoke(&runnable, inputs, None, None).await;

        assert_eq!(results.len(), 4);
        assert_eq!(results[0].as_ref().unwrap(), &json!(2));
        assert_eq!(results[1].as_ref().unwrap(), &json!(4));
        assert_eq!(results[2].as_ref().unwrap(), &json!(6));
        assert_eq!(results[3].as_ref().unwrap(), &json!(8));
    }

    #[tokio::test]
    async fn test_batch_invoke_preserves_order() {
        // Use varying delays to force out-of-order completion
        let runnable = RunnableLambda::new("reverse_delay", |v: Value| async move {
            let n = v.as_i64().unwrap();
            let delay = (10 - n) as u64 * 15;
            tokio::time::sleep(Duration::from_millis(delay)).await;
            Ok(json!(n * 100))
        });

        let inputs: Vec<Value> = (0..10).map(|i| json!(i)).collect();
        let results = batch_invoke(&runnable, inputs, None, None).await;

        assert_eq!(results.len(), 10);
        for (i, r) in results.iter().enumerate() {
            assert_eq!(
                r.as_ref().unwrap(),
                &json!(i as i64 * 100),
                "index {i} should be {}",
                i * 100
            );
        }
    }

    #[tokio::test]
    async fn test_batch_invoke_error_handling() {
        // Fail on even numbers, succeed on odd
        let runnable = RunnableLambda::new("fail_even", |v: Value| async move {
            let n = v.as_i64().unwrap();
            if n % 2 == 0 {
                Err(CognisError::Other(format!("even number: {n}")))
            } else {
                Ok(json!(n * 10))
            }
        });

        let inputs = vec![json!(1), json!(2), json!(3), json!(4), json!(5)];
        let results = batch_invoke(&runnable, inputs, None, None).await;

        assert_eq!(results.len(), 5);
        // Odd numbers succeed
        assert_eq!(results[0].as_ref().unwrap(), &json!(10));
        assert_eq!(results[2].as_ref().unwrap(), &json!(30));
        assert_eq!(results[4].as_ref().unwrap(), &json!(50));
        // Even numbers fail
        assert!(results[1].is_err());
        assert!(results[3].is_err());
    }

    #[tokio::test]
    async fn test_batch_invoke_max_concurrency_1_sequential() {
        let delay_ms: u64 = 40;
        let runnable = slow_doubler(delay_ms);
        let inputs: Vec<Value> = (0..4).map(|i| json!(i)).collect();

        let start = Instant::now();
        let results = batch_invoke(&runnable, inputs, Some(1), None).await;
        let elapsed = start.elapsed();

        assert_eq!(results.len(), 4);
        for (i, r) in results.iter().enumerate() {
            assert_eq!(r.as_ref().unwrap(), &json!(i as i64 * 2));
        }

        // With max_concurrency=1, should take ~4 * delay_ms
        assert!(
            elapsed >= Duration::from_millis(delay_ms * 4 - 20),
            "max_concurrency=1 should be sequential, elapsed: {elapsed:?}"
        );
    }

    #[tokio::test]
    async fn test_batch_invoke_concurrency_limiting() {
        // Track peak concurrency using an atomic counter
        let counter = Arc::new(AtomicUsize::new(0));
        let peak = Arc::new(AtomicUsize::new(0));

        let counter_clone = counter.clone();
        let peak_clone = peak.clone();

        let runnable = RunnableLambda::new("track_concurrency", move |v: Value| {
            let counter = counter_clone.clone();
            let peak = peak_clone.clone();
            async move {
                let current = counter.fetch_add(1, Ordering::SeqCst) + 1;
                // Update peak
                peak.fetch_max(current, Ordering::SeqCst);
                tokio::time::sleep(Duration::from_millis(50)).await;
                counter.fetch_sub(1, Ordering::SeqCst);
                Ok(v)
            }
        });

        let inputs: Vec<Value> = (0..10).map(|i| json!(i)).collect();
        let _results = batch_invoke(&runnable, inputs, Some(3), None).await;

        let observed_peak = peak.load(Ordering::SeqCst);
        assert!(
            observed_peak <= 3,
            "peak concurrency should be <= 3, was {observed_peak}"
        );
    }

    #[tokio::test]
    async fn test_batch_invoke_empty_inputs() {
        let runnable = doubler();
        let results = batch_invoke(&runnable, vec![], None, None).await;
        assert!(results.is_empty());
    }

    #[tokio::test]
    async fn test_batch_invoke_with_config() {
        let runnable = RunnableLambda::with_config(
            "config_reader",
            |v, config: Option<RunnableConfig>| async move {
                let tag = config
                    .and_then(|c| c.tags.first().cloned())
                    .unwrap_or_else(|| "none".to_string());
                Ok(json!({"value": v, "tag": tag}))
            },
        );

        let mut config = RunnableConfig::default();
        config.tags = vec!["test_tag".to_string()];

        let inputs = vec![json!(1), json!(2)];
        let results = batch_invoke(&runnable, inputs, None, Some(&config)).await;

        assert_eq!(results.len(), 2);
        for r in &results {
            let v = r.as_ref().unwrap();
            assert_eq!(v["tag"], "test_tag");
        }
    }

    // ---- RunnableBatch tests ----

    #[tokio::test]
    async fn test_runnable_batch_basic() {
        let inner = Arc::new(doubler()) as Arc<dyn Runnable>;
        let batch = RunnableBatch::new(inner);

        let input = json!([1, 2, 3, 4, 5]);
        let output = batch.invoke(input, None).await.unwrap();

        let arr = output.as_array().unwrap();
        assert_eq!(arr.len(), 5);
        assert_eq!(arr[0], json!(2));
        assert_eq!(arr[1], json!(4));
        assert_eq!(arr[2], json!(6));
        assert_eq!(arr[3], json!(8));
        assert_eq!(arr[4], json!(10));
    }

    #[tokio::test]
    async fn test_runnable_batch_with_max_concurrency() {
        let delay_ms: u64 = 40;
        let inner = Arc::new(slow_doubler(delay_ms)) as Arc<dyn Runnable>;
        let batch = RunnableBatch::new(inner).with_max_concurrency(2);

        let input = json!([1, 2, 3, 4]);
        let start = Instant::now();
        let output = batch.invoke(input, None).await.unwrap();
        let elapsed = start.elapsed();

        let arr = output.as_array().unwrap();
        assert_eq!(arr.len(), 4);
        assert_eq!(arr[0], json!(2));
        assert_eq!(arr[3], json!(8));

        // With concurrency=2 and 4 inputs: ~2 rounds of 2 = ~80ms
        // Should be faster than sequential (~160ms)
        assert!(
            elapsed < Duration::from_millis(delay_ms * 4 - 20),
            "batch with concurrency=2 should parallelize, elapsed: {elapsed:?}"
        );
    }

    #[tokio::test]
    async fn test_runnable_batch_return_exceptions() {
        let inner = Arc::new(RunnableLambda::new("fail_even", |v: Value| async move {
            let n = v.as_i64().unwrap();
            if n % 2 == 0 {
                Err(CognisError::Other(format!("even: {n}")))
            } else {
                Ok(json!(n * 10))
            }
        })) as Arc<dyn Runnable>;

        let batch = RunnableBatch::new(inner).with_return_exceptions(true);
        let input = json!([1, 2, 3, 4]);
        let output = batch.invoke(input, None).await.unwrap();

        let arr = output.as_array().unwrap();
        assert_eq!(arr.len(), 4);
        assert_eq!(arr[0], json!(10));
        assert!(arr[1].get("error").is_some());
        assert_eq!(arr[2], json!(30));
        assert!(arr[3].get("error").is_some());
    }

    #[tokio::test]
    async fn test_runnable_batch_propagate_errors() {
        let inner = Arc::new(RunnableLambda::new("fail_all", |_v: Value| async move {
            Err(CognisError::Other("always fails".into()))
        })) as Arc<dyn Runnable>;

        let batch = RunnableBatch::new(inner).with_return_exceptions(false);
        let input = json!([1, 2]);
        let result = batch.invoke(input, None).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_runnable_batch_invalid_input_type() {
        let inner = Arc::new(doubler()) as Arc<dyn Runnable>;
        let batch = RunnableBatch::new(inner);

        let result = batch.invoke(json!("not an array"), None).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("expected Array"));
    }

    #[tokio::test]
    async fn test_runnable_batch_empty_array() {
        let inner = Arc::new(doubler()) as Arc<dyn Runnable>;
        let batch = RunnableBatch::new(inner);

        let output = batch.invoke(json!([]), None).await.unwrap();
        assert_eq!(output, json!([]));
    }

    #[tokio::test]
    async fn test_runnable_batch_name() {
        let inner = Arc::new(doubler()) as Arc<dyn Runnable>;
        let batch = RunnableBatch::new(inner);
        assert_eq!(batch.name(), "RunnableBatch<doubler>");
    }
}