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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//! Timeout and deadline wrappers for runnables.
//!
//! Provides [`Runnable`] wrappers that enforce time limits on execution.
//! [`RunnableTimeout`] uses a relative duration from invocation, while
//! [`RunnableDeadline`] enforces an absolute point in time, which is useful
//! when multiple operations share a common deadline.
//!
//! - [`TimeoutBehavior`] — controls what happens on timeout (error, default value, or fallback runnable)
//! - [`TimeoutConfig`] — configuration combining a duration with a [`TimeoutBehavior`]
//! - [`RunnableTimeout`] — wrapper that enforces a relative timeout on each invocation
//! - [`RunnableDeadline`] — wrapper that enforces an absolute deadline across invocations

use std::sync::Arc;
use std::time::{Duration, Instant};

use async_trait::async_trait;
use futures::stream::{self, StreamExt};
use serde_json::Value;

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

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

/// Behavior when a timeout is reached.
///
/// Controls what happens when a runnable exceeds its allotted time.
pub enum TimeoutBehavior {
    /// Return a `CognisError::Other` with a timeout message.
    Error,
    /// Return a default `Value` instead of an error.
    Default(Value),
    /// Delegate to a fallback runnable on timeout.
    Fallback(Arc<dyn Runnable>),
}

/// Configuration for timeout behavior on a runnable.
///
/// # Example
/// ```ignore
/// use std::time::Duration;
/// use cognis_core::runnables::timeout::TimeoutConfig;
///
/// let config = TimeoutConfig::new(Duration::from_secs(5))
///     .with_behavior(TimeoutBehavior::Error);
/// ```
pub struct TimeoutConfig {
    /// Maximum duration allowed for the operation.
    pub duration: Duration,
    /// What to do when the timeout is reached.
    pub on_timeout: TimeoutBehavior,
}

impl TimeoutConfig {
    /// Create a new timeout configuration with the given duration.
    ///
    /// Defaults to `TimeoutBehavior::Error`.
    pub fn new(duration: Duration) -> Self {
        Self {
            duration,
            on_timeout: TimeoutBehavior::Error,
        }
    }

    /// Set the timeout behavior.
    pub fn with_behavior(mut self, behavior: TimeoutBehavior) -> Self {
        self.on_timeout = behavior;
        self
    }

    /// Convenience: set `TimeoutBehavior::Default` with the given value.
    pub fn with_default_value(self, value: Value) -> Self {
        self.with_behavior(TimeoutBehavior::Default(value))
    }

    /// Convenience: set `TimeoutBehavior::Fallback` with the given runnable.
    pub fn with_fallback(self, fallback: Arc<dyn Runnable>) -> Self {
        self.with_behavior(TimeoutBehavior::Fallback(fallback))
    }
}

/// A runnable wrapper that enforces a relative timeout on the inner runnable.
///
/// If the inner runnable does not complete within the configured duration,
/// the behavior is determined by `TimeoutConfig::on_timeout`.
///
/// # Example
/// ```ignore
/// use std::time::Duration;
/// use cognis_core::runnables::{RunnableLambda, RunnableExt};
///
/// let with_timeout = my_runnable.with_timeout(Duration::from_secs(5));
/// ```
pub struct RunnableTimeout {
    /// The wrapped runnable.
    inner: Arc<dyn Runnable>,
    /// Timeout configuration.
    config: TimeoutConfig,
}

impl RunnableTimeout {
    /// Create a new timeout wrapper with the given configuration.
    pub fn new(inner: Arc<dyn Runnable>, config: TimeoutConfig) -> Self {
        Self { inner, config }
    }

    /// Handle a timeout according to the configured behavior.
    async fn handle_timeout(&self, input: Value, config: Option<&RunnableConfig>) -> Result<Value> {
        match &self.config.on_timeout {
            TimeoutBehavior::Error => Err(CognisError::Other(format!(
                "Timeout: {} exceeded {:?}",
                self.inner.name(),
                self.config.duration
            ))),
            TimeoutBehavior::Default(val) => Ok(val.clone()),
            TimeoutBehavior::Fallback(fallback) => fallback.invoke(input, config).await,
        }
    }
}

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

    async fn invoke(&self, input: Value, config: Option<&RunnableConfig>) -> Result<Value> {
        match tokio::time::timeout(
            self.config.duration,
            self.inner.invoke(input.clone(), config),
        )
        .await
        {
            Ok(result) => result,
            Err(_elapsed) => self.handle_timeout(input, config).await,
        }
    }

    async fn stream(
        &self,
        input: Value,
        config: Option<&RunnableConfig>,
    ) -> Result<RunnableStream> {
        // Apply timeout to getting the first chunk from the stream.
        let duration = self.config.duration;
        match tokio::time::timeout(duration, self.inner.stream(input.clone(), config)).await {
            Ok(Ok(inner_stream)) => {
                // Wrap the stream so that each item is subject to the timeout as well.
                // We use a simple approach: timeout on the first chunk.
                let deadline = tokio::time::Instant::now() + duration;
                let wrapped = inner_stream.take_while(move |_| {
                    let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
                    async move { remaining > Duration::ZERO }
                });
                Ok(Box::pin(wrapped))
            }
            Ok(Err(e)) => Err(e),
            Err(_elapsed) => {
                // Timeout getting the stream itself.
                match &self.config.on_timeout {
                    TimeoutBehavior::Error => Err(CognisError::Other(format!(
                        "Timeout: {} stream exceeded {:?}",
                        self.inner.name(),
                        self.config.duration
                    ))),
                    TimeoutBehavior::Default(val) => {
                        let val = val.clone();
                        Ok(Box::pin(stream::once(async move { Ok(val) })))
                    }
                    TimeoutBehavior::Fallback(fallback) => fallback.stream(input, config).await,
                }
            }
        }
    }
}

/// A runnable wrapper that enforces an absolute deadline on the inner runnable.
///
/// Unlike `RunnableTimeout` which uses a relative duration from the time of
/// invocation, `RunnableDeadline` uses an absolute `Instant`. This is useful
/// when multiple operations share a common deadline.
///
/// # Example
/// ```ignore
/// use std::time::{Duration, Instant};
/// use cognis_core::runnables::timeout::RunnableDeadline;
///
/// let deadline = Instant::now() + Duration::from_secs(10);
/// let with_deadline = RunnableDeadline::new(Arc::new(my_runnable), deadline);
/// ```
pub struct RunnableDeadline {
    /// The wrapped runnable.
    inner: Arc<dyn Runnable>,
    /// Absolute deadline.
    deadline: Instant,
}

impl RunnableDeadline {
    /// Create a new deadline wrapper with the given absolute deadline.
    pub fn new(inner: Arc<dyn Runnable>, deadline: Instant) -> Self {
        Self { inner, deadline }
    }

    /// Calculate remaining duration until the deadline. Returns zero if past.
    fn remaining(&self) -> Duration {
        self.deadline.saturating_duration_since(Instant::now())
    }
}

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

    async fn invoke(&self, input: Value, config: Option<&RunnableConfig>) -> Result<Value> {
        let remaining = self.remaining();
        if remaining.is_zero() {
            return Err(CognisError::Other(format!(
                "Deadline exceeded for {}",
                self.inner.name()
            )));
        }
        match tokio::time::timeout(remaining, self.inner.invoke(input, config)).await {
            Ok(result) => result,
            Err(_elapsed) => Err(CognisError::Other(format!(
                "Deadline exceeded for {}",
                self.inner.name()
            ))),
        }
    }

    async fn stream(
        &self,
        input: Value,
        config: Option<&RunnableConfig>,
    ) -> Result<RunnableStream> {
        let remaining = self.remaining();
        if remaining.is_zero() {
            return Err(CognisError::Other(format!(
                "Deadline exceeded for {} stream",
                self.inner.name()
            )));
        }
        match tokio::time::timeout(remaining, self.inner.stream(input, config)).await {
            Ok(Ok(inner_stream)) => {
                let deadline = self.deadline;
                let wrapped = inner_stream.take_while(move |_| {
                    let now = Instant::now();
                    async move { now < deadline }
                });
                Ok(Box::pin(wrapped))
            }
            Ok(Err(e)) => Err(e),
            Err(_elapsed) => Err(CognisError::Other(format!(
                "Deadline exceeded for {} stream",
                self.inner.name()
            ))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::runnables::lambda::RunnableLambda;
    use futures::StreamExt;
    use serde_json::json;

    /// Helper: fast runnable that returns input doubled.
    fn fast_double() -> RunnableLambda {
        RunnableLambda::new("fast_double", |v: Value| async move {
            let n = v.as_i64().unwrap();
            Ok(json!(n * 2))
        })
    }

    /// Helper: slow runnable that sleeps for `delay_ms` then returns input.
    fn slow_identity(delay_ms: u64) -> RunnableLambda {
        RunnableLambda::new("slow_identity", move |v: Value| async move {
            tokio::time::sleep(Duration::from_millis(delay_ms)).await;
            Ok(v)
        })
    }

    /// Helper: slow runnable that sleeps then returns input doubled.
    fn slow_double(delay_ms: u64) -> RunnableLambda {
        RunnableLambda::new("slow_double", move |v: Value| async move {
            tokio::time::sleep(Duration::from_millis(delay_ms)).await;
            let n = v.as_i64().unwrap();
            Ok(json!(n * 2))
        })
    }

    /// Helper: runnable that always errors.
    fn error_runnable() -> RunnableLambda {
        RunnableLambda::new("error", |_v: Value| async move {
            Err(CognisError::Other("inner error".to_string()))
        })
    }

    // ==================== TimeoutConfig tests ====================

    #[test]
    fn test_timeout_config_defaults_to_error_behavior() {
        let config = TimeoutConfig::new(Duration::from_secs(5));
        assert_eq!(config.duration, Duration::from_secs(5));
        assert!(matches!(config.on_timeout, TimeoutBehavior::Error));
    }

    #[test]
    fn test_timeout_config_with_default_value() {
        let config =
            TimeoutConfig::new(Duration::from_secs(1)).with_default_value(json!("fallback"));
        assert!(matches!(config.on_timeout, TimeoutBehavior::Default(_)));
        if let TimeoutBehavior::Default(v) = &config.on_timeout {
            assert_eq!(v, &json!("fallback"));
        }
    }

    #[test]
    fn test_timeout_config_with_fallback() {
        let fb = Arc::new(fast_double()) as Arc<dyn Runnable>;
        let config = TimeoutConfig::new(Duration::from_secs(1)).with_fallback(fb);
        assert!(matches!(config.on_timeout, TimeoutBehavior::Fallback(_)));
    }

    // ==================== RunnableTimeout invoke tests ====================

    #[tokio::test]
    async fn test_timeout_fast_operation_succeeds() {
        let inner = fast_double();
        let config = TimeoutConfig::new(Duration::from_secs(1));
        let timed = RunnableTimeout::new(Arc::new(inner), config);

        let result = timed.invoke(json!(5), None).await.unwrap();
        assert_eq!(result, json!(10));
    }

    #[tokio::test]
    async fn test_timeout_slow_operation_errors() {
        let inner = slow_identity(200);
        let config = TimeoutConfig::new(Duration::from_millis(50));
        let timed = RunnableTimeout::new(Arc::new(inner), config);

        let result = timed.invoke(json!(1), None).await;
        assert!(result.is_err());
        let err_msg = format!("{}", result.unwrap_err());
        assert!(err_msg.contains("Timeout"));
    }

    #[tokio::test]
    async fn test_timeout_slow_operation_returns_default() {
        let inner = slow_identity(200);
        let config =
            TimeoutConfig::new(Duration::from_millis(50)).with_default_value(json!("default"));
        let timed = RunnableTimeout::new(Arc::new(inner), config);

        let result = timed.invoke(json!(1), None).await.unwrap();
        assert_eq!(result, json!("default"));
    }

    #[tokio::test]
    async fn test_timeout_slow_operation_uses_fallback() {
        let inner = slow_identity(200);
        let fallback = Arc::new(fast_double()) as Arc<dyn Runnable>;
        let config = TimeoutConfig::new(Duration::from_millis(50)).with_fallback(fallback);
        let timed = RunnableTimeout::new(Arc::new(inner), config);

        let result = timed.invoke(json!(7), None).await.unwrap();
        assert_eq!(result, json!(14)); // fallback doubles the input
    }

    #[tokio::test]
    async fn test_timeout_name_delegates() {
        let inner = fast_double();
        let config = TimeoutConfig::new(Duration::from_secs(1));
        let timed = RunnableTimeout::new(Arc::new(inner), config);
        assert_eq!(timed.name(), "fast_double");
    }

    #[tokio::test]
    async fn test_timeout_inner_error_propagated() {
        let inner = error_runnable();
        let config = TimeoutConfig::new(Duration::from_secs(1));
        let timed = RunnableTimeout::new(Arc::new(inner), config);

        let result = timed.invoke(json!(1), None).await;
        assert!(result.is_err());
        let err_msg = format!("{}", result.unwrap_err());
        assert!(err_msg.contains("inner error"));
    }

    #[tokio::test]
    async fn test_timeout_just_within_limit() {
        // Operation takes 30ms, timeout is 100ms -- should succeed.
        let inner = slow_double(30);
        let config = TimeoutConfig::new(Duration::from_millis(100));
        let timed = RunnableTimeout::new(Arc::new(inner), config);

        let result = timed.invoke(json!(3), None).await.unwrap();
        assert_eq!(result, json!(6));
    }

    #[tokio::test]
    async fn test_timeout_completes_quickly_when_fast() {
        let inner = fast_double();
        let config = TimeoutConfig::new(Duration::from_secs(10));
        let timed = RunnableTimeout::new(Arc::new(inner), config);

        let start = Instant::now();
        let _result = timed.invoke(json!(1), None).await.unwrap();
        let elapsed = start.elapsed();

        // Should complete well under the 10s timeout.
        assert!(
            elapsed < Duration::from_millis(100),
            "Fast operation should complete quickly, took {:?}",
            elapsed
        );
    }

    // ==================== RunnableTimeout stream tests ====================

    #[tokio::test]
    async fn test_timeout_stream_fast_succeeds() {
        let inner = fast_double();
        let config = TimeoutConfig::new(Duration::from_secs(1));
        let timed = RunnableTimeout::new(Arc::new(inner), config);

        let mut stream = timed.stream(json!(4), None).await.unwrap();
        let item = stream.next().await.unwrap().unwrap();
        assert_eq!(item, json!(8));
    }

    #[tokio::test]
    async fn test_timeout_stream_slow_errors() {
        let inner = slow_identity(200);
        let config = TimeoutConfig::new(Duration::from_millis(50));
        let timed = RunnableTimeout::new(Arc::new(inner), config);

        // The stream creation itself should timeout.
        let result = timed.stream(json!(1), None).await;
        assert!(result.is_err());
    }

    // ==================== RunnableDeadline tests ====================

    #[tokio::test]
    async fn test_deadline_fast_operation_succeeds() {
        let inner = fast_double();
        let deadline = Instant::now() + Duration::from_secs(1);
        let dl = RunnableDeadline::new(Arc::new(inner), deadline);

        let result = dl.invoke(json!(5), None).await.unwrap();
        assert_eq!(result, json!(10));
    }

    #[tokio::test]
    async fn test_deadline_slow_operation_errors() {
        let inner = slow_identity(200);
        let deadline = Instant::now() + Duration::from_millis(50);
        let dl = RunnableDeadline::new(Arc::new(inner), deadline);

        let result = dl.invoke(json!(1), None).await;
        assert!(result.is_err());
        let err_msg = format!("{}", result.unwrap_err());
        assert!(err_msg.contains("Deadline exceeded"));
    }

    #[tokio::test]
    async fn test_deadline_already_past() {
        let inner = fast_double();
        // Deadline in the past.
        let deadline = Instant::now() - Duration::from_secs(1);
        let dl = RunnableDeadline::new(Arc::new(inner), deadline);

        let result = dl.invoke(json!(5), None).await;
        assert!(result.is_err());
        let err_msg = format!("{}", result.unwrap_err());
        assert!(err_msg.contains("Deadline exceeded"));
    }

    #[tokio::test]
    async fn test_deadline_name_delegates() {
        let inner = fast_double();
        let deadline = Instant::now() + Duration::from_secs(1);
        let dl = RunnableDeadline::new(Arc::new(inner), deadline);
        assert_eq!(dl.name(), "fast_double");
    }

    #[tokio::test]
    async fn test_deadline_stream_succeeds() {
        let inner = fast_double();
        let deadline = Instant::now() + Duration::from_secs(1);
        let dl = RunnableDeadline::new(Arc::new(inner), deadline);

        let mut stream = dl.stream(json!(6), None).await.unwrap();
        let item = stream.next().await.unwrap().unwrap();
        assert_eq!(item, json!(12));
    }

    #[tokio::test]
    async fn test_deadline_stream_past_deadline_errors() {
        let inner = fast_double();
        let deadline = Instant::now() - Duration::from_secs(1);
        let dl = RunnableDeadline::new(Arc::new(inner), deadline);

        let result = dl.stream(json!(1), None).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_deadline_shared_across_operations() {
        // Two operations sharing a common deadline of 150ms.
        // Each takes 50ms, so both should succeed.
        let deadline = Instant::now() + Duration::from_millis(150);

        let dl1 = RunnableDeadline::new(Arc::new(slow_double(50)), deadline);
        let dl2 = RunnableDeadline::new(Arc::new(slow_double(50)), deadline);

        let r1 = dl1.invoke(json!(2), None).await.unwrap();
        let r2 = dl2.invoke(json!(3), None).await.unwrap();
        assert_eq!(r1, json!(4));
        assert_eq!(r2, json!(6));
    }

    // ==================== Extension method tests ====================

    #[tokio::test]
    async fn test_ext_with_timeout() {
        use crate::runnables::ext::RunnableExt;

        let timed = fast_double().with_timeout(Duration::from_secs(1));
        let result = timed.invoke(json!(10), None).await.unwrap();
        assert_eq!(result, json!(20));
    }

    #[tokio::test]
    async fn test_ext_with_timeout_config() {
        use crate::runnables::ext::RunnableExt;

        let config =
            TimeoutConfig::new(Duration::from_millis(50)).with_default_value(json!("timed_out"));
        let timed = slow_identity(200).with_timeout_config(config);
        let result = timed.invoke(json!(1), None).await.unwrap();
        assert_eq!(result, json!("timed_out"));
    }
}