camel-processor 0.24.0

Message processors for rust-camel
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
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

use tower::{Service, ServiceExt};

use camel_api::loop_eip::{LoopConfig, LoopMode};
use camel_api::{BoxProcessor, CamelError, Exchange, Value};

pub const CAMEL_LOOP_INDEX: &str = "CamelLoopIndex";
pub const CAMEL_LOOP_SIZE: &str = "CamelLoopSize";

#[derive(Clone)]
pub struct LoopService {
    config: LoopConfig,
    sub_pipeline: BoxProcessor,
}

impl LoopService {
    pub fn new(config: LoopConfig, sub_pipeline: BoxProcessor) -> Self {
        Self {
            config,
            sub_pipeline,
        }
    }
}

impl Service<Exchange> for LoopService {
    type Response = Exchange;
    type Error = CamelError;
    type Future = Pin<Box<dyn Future<Output = Result<Exchange, CamelError>> + Send>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, mut exchange: Exchange) -> Self::Future {
        let config = self.config.clone();
        let mut pipeline = self.sub_pipeline.clone();

        Box::pin(async move {
            match config.mode {
                LoopMode::Count(n) => {
                    // clamp to config.max_iterations. The audit finding was that
                    // Count(n) had no cap (only While did); a `count: u32::MAX`
                    // would exhaust CPU. The clamp is applied uniformly to Count
                    // and While. The CAMEL_LOOP_SIZE property is set to the
                    // *clamped* count so downstream steps observe the effective
                    // iteration count.
                    let n_clamped = n.min(config.max_iterations);
                    if n > config.max_iterations {
                        tracing::warn!(
                            requested = n,
                            clamped_to = config.max_iterations,
                            "LoopMode::Count exceeded max_iterations; clamping"
                        );
                    }
                    exchange.set_property(CAMEL_LOOP_SIZE, Value::from(n_clamped as u64));
                    for i in 0..n_clamped {
                        exchange.set_property(CAMEL_LOOP_INDEX, Value::from(i as u64));
                        exchange = pipeline.ready().await?.call(exchange).await?;
                    }
                }
                LoopMode::While(ref predicate) => {
                    exchange.set_property(CAMEL_LOOP_SIZE, Value::from(0u64));
                    for i in 0..config.max_iterations {
                        if !predicate(&exchange) {
                            break;
                        }
                        exchange.set_property(CAMEL_LOOP_INDEX, Value::from(i as u64));
                        exchange = pipeline.ready().await?.call(exchange).await?;
                    }
                    if predicate(&exchange) {
                        tracing::warn!(
                            "Loop while-mode hit max_iterations ({}) safety guard. Predicate still true.",
                            config.max_iterations
                        );
                    }
                }
            }
            Ok(exchange)
        })
    }
}

// ── LoopSegment (ADR-0025 OutcomePipeline) ─────────────────────────────

/// Outcome-aware structural EIP segment for the Loop pattern.
///
/// Operates at the `PipelineOutcome` layer so that `Stopped(ex)` from a
/// sub-step (e.g. Stop EIP) is preserved with the exchange including all
/// mutations. Supports both Count and While modes, mirroring `LoopService`
/// semantics exactly.
///
/// Unlike `LoopService` (which operates at the Tower layer), `LoopSegment`
/// correctly short-circuits on `PipelineOutcome::Stopped` or `Failed`.
pub struct LoopSegment {
    pub config: camel_api::loop_eip::LoopConfig,
    pub body: camel_api::OutcomeSegment,
}

impl Clone for LoopSegment {
    fn clone(&self) -> Self {
        Self {
            config: self.config.clone(),
            body: self.body.clone(),
        }
    }
}

impl camel_api::OutcomePipeline for LoopSegment {
    fn clone_box(&self) -> Box<dyn camel_api::OutcomePipeline> {
        Box::new(self.clone())
    }

    fn run<'a>(
        &'a mut self,
        exchange: camel_api::Exchange,
    ) -> Pin<Box<dyn Future<Output = camel_api::PipelineOutcome> + Send + 'a>> {
        use camel_api::{PipelineOutcome, Value};

        let config = self.config.clone();
        let body = &mut self.body;

        Box::pin(async move {
            match config.mode {
                camel_api::loop_eip::LoopMode::Count(n) => {
                    let n_clamped = n.min(config.max_iterations);
                    if n > config.max_iterations {
                        tracing::warn!(
                            requested = n,
                            clamped_to = config.max_iterations,
                            "LoopMode::Count exceeded max_iterations; clamping"
                        );
                    }
                    let mut ex = exchange;
                    ex.set_property(CAMEL_LOOP_SIZE, Value::from(n_clamped as u64));
                    for i in 0..n_clamped {
                        ex.set_property(CAMEL_LOOP_INDEX, Value::from(i as u64));
                        match body.run(ex).await {
                            PipelineOutcome::Completed(next) => {
                                ex = next;
                            }
                            other => return other,
                        }
                    }
                    PipelineOutcome::Completed(ex)
                }
                camel_api::loop_eip::LoopMode::While(ref predicate) => {
                    let mut ex = exchange;
                    ex.set_property(CAMEL_LOOP_SIZE, Value::from(0u64));
                    let mut i = 0u64;
                    while i < config.max_iterations as u64 {
                        if !predicate(&ex) {
                            break;
                        }
                        ex.set_property(CAMEL_LOOP_INDEX, Value::from(i));
                        match body.run(ex).await {
                            PipelineOutcome::Completed(next) => {
                                ex = next;
                            }
                            other => return other,
                        }
                        i += 1;
                    }
                    if predicate(&ex) {
                        tracing::warn!(
                            "Loop while-mode hit max_iterations ({}) safety guard. Predicate still true.",
                            config.max_iterations
                        );
                    }
                    PipelineOutcome::Completed(ex)
                }
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::{Arc, Mutex};

    use camel_api::loop_eip::{LoopConfig, LoopMode, MAX_LOOP_ITERATIONS};
    use camel_api::{
        Body, BoxProcessor, BoxProcessorExt, CamelError, Exchange, FilterPredicate,
        IdentityProcessor, Message,
    };
    use tower::{Service, ServiceExt};

    use super::{CAMEL_LOOP_INDEX, CAMEL_LOOP_SIZE, LoopService};

    fn identity_pipeline() -> BoxProcessor {
        BoxProcessor::new(IdentityProcessor)
    }

    fn counter_pipeline(counter: Arc<AtomicUsize>) -> BoxProcessor {
        BoxProcessor::from_fn(move |exchange: Exchange| {
            let counter = Arc::clone(&counter);
            Box::pin(async move {
                counter.fetch_add(1, Ordering::SeqCst);
                Ok(exchange)
            })
        })
    }

    #[tokio::test]
    async fn test_loop_count_iterates_n_times() {
        let counter = Arc::new(AtomicUsize::new(0));
        let config = LoopConfig::new(LoopMode::Count(3));
        let mut service = LoopService::new(config, counter_pipeline(Arc::clone(&counter)));

        let exchange = Exchange::new(Message::new("test"));
        let result = service.ready().await.unwrap().call(exchange).await;

        assert!(result.is_ok());
        assert_eq!(counter.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn test_loop_count_sets_properties() {
        let seen_indices = Arc::new(Mutex::new(Vec::<u64>::new()));
        let seen_indices_for_pipeline = Arc::clone(&seen_indices);

        let pipeline = BoxProcessor::from_fn(move |exchange: Exchange| {
            let seen_indices = Arc::clone(&seen_indices_for_pipeline);
            Box::pin(async move {
                if let Some(index) = exchange.property(CAMEL_LOOP_INDEX).and_then(|v| v.as_u64()) {
                    seen_indices.lock().unwrap().push(index);
                }
                Ok(exchange)
            })
        });

        let config = LoopConfig::new(LoopMode::Count(3));
        let mut service = LoopService::new(config, pipeline);

        let exchange = Exchange::new(Message::new("test"));
        let result = service.ready().await.unwrap().call(exchange).await.unwrap();

        assert_eq!(*seen_indices.lock().unwrap(), vec![0, 1, 2]);
        assert_eq!(
            result.property(CAMEL_LOOP_SIZE).and_then(|v| v.as_u64()),
            Some(3)
        );
    }

    #[tokio::test]
    async fn test_loop_count_zero_is_noop() {
        let config = LoopConfig::new(LoopMode::Count(0));
        let mut service = LoopService::new(config, identity_pipeline());

        let exchange = Exchange::new(Message::new("test"));
        let result = service.ready().await.unwrap().call(exchange).await.unwrap();

        assert_eq!(result.input.body.as_text(), Some("test"));
        assert_eq!(
            result.property(CAMEL_LOOP_SIZE).and_then(|v| v.as_u64()),
            Some(0)
        );
        assert!(result.property(CAMEL_LOOP_INDEX).is_none());
    }

    #[tokio::test]
    async fn test_loop_while_stops_when_predicate_false() {
        let counter = Arc::new(AtomicUsize::new(0));

        let predicate = FilterPredicate::new(|exchange: &Exchange| {
            exchange
                .property("iterations")
                .and_then(|v| v.as_u64())
                .unwrap_or(0)
                < 2
        });

        let counter_for_pipeline = Arc::clone(&counter);
        let pipeline = BoxProcessor::from_fn(move |mut exchange: Exchange| {
            let counter = Arc::clone(&counter_for_pipeline);
            Box::pin(async move {
                let current = exchange
                    .property("iterations")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(0);
                exchange.set_property("iterations", current + 1);
                counter.fetch_add(1, Ordering::SeqCst);
                Ok(exchange)
            })
        });

        let config = LoopConfig::new(LoopMode::While(predicate));
        let mut service = LoopService::new(config, pipeline);

        let exchange = Exchange::new(Message::new("test"));
        let result = service.ready().await.unwrap().call(exchange).await.unwrap();

        assert_eq!(counter.load(Ordering::SeqCst), 2);
        assert_eq!(
            result.property("iterations").and_then(|v| v.as_u64()),
            Some(2)
        );
        assert_eq!(
            result.property(CAMEL_LOOP_INDEX).and_then(|v| v.as_u64()),
            Some(1)
        );
        assert_eq!(
            result.property(CAMEL_LOOP_SIZE).and_then(|v| v.as_u64()),
            Some(0)
        );
    }

    #[tokio::test]
    async fn test_loop_while_respects_max_iterations() {
        let counter = Arc::new(AtomicUsize::new(0));
        let predicate = FilterPredicate::new(|_exchange: &Exchange| true);
        let config = LoopConfig::new(LoopMode::While(predicate));
        let mut service = LoopService::new(config, counter_pipeline(Arc::clone(&counter)));

        let exchange = Exchange::new(Message::new("test"));
        let result = service.ready().await.unwrap().call(exchange).await;

        assert!(result.is_ok());
        assert_eq!(counter.load(Ordering::SeqCst), MAX_LOOP_ITERATIONS);
    }

    #[tokio::test]
    async fn test_loop_error_propagation() {
        let pipeline = BoxProcessor::from_fn(|_exchange: Exchange| {
            Box::pin(async { Err(CamelError::ProcessorError("boom".into())) })
        });

        let config = LoopConfig::new(LoopMode::Count(3));
        let mut service = LoopService::new(config, pipeline);

        let exchange = Exchange::new(Message::new("test"));
        let result = service.ready().await.unwrap().call(exchange).await;

        assert!(matches!(result, Err(CamelError::ProcessorError(msg)) if msg == "boom"));
    }

    // ── D-M9 Batch 1: LoopMode::Count(n) clamped to MAX_LOOP_ITERATIONS ──

    /// D-M9: a `Count(u32::MAX as usize)` (or any value above
    /// `MAX_LOOP_ITERATIONS`) is clamped to `MAX_LOOP_ITERATIONS` and
    /// runs at most that many iterations. Without the clamp, a malicious
    /// or typo'd `count: 4294967295` would exhaust CPU. The pre-existing
    /// `Count(3)` test continues to pass — small values are unaffected.
    #[tokio::test]
    async fn test_loop_count_clamped_to_max_iterations() {
        let counter = Arc::new(AtomicUsize::new(0));
        let config = LoopConfig::new(LoopMode::Count(usize::MAX));
        let mut service = LoopService::new(config, counter_pipeline(Arc::clone(&counter)));

        let exchange = Exchange::new(Message::new("test"));
        let result = service.ready().await.unwrap().call(exchange).await;

        assert!(result.is_ok());
        // Must run exactly MAX_LOOP_ITERATIONS, not u32::MAX iterations.
        assert_eq!(counter.load(Ordering::SeqCst), MAX_LOOP_ITERATIONS);
        // The CAMEL_LOOP_SIZE property is set to the *clamped* count.
        assert_eq!(
            result
                .unwrap()
                .property(CAMEL_LOOP_SIZE)
                .and_then(|v| v.as_u64()),
            Some(MAX_LOOP_ITERATIONS as u64)
        );
    }

    #[tokio::test]
    async fn test_loop_count_with_custom_max_iterations() {
        let counter = Arc::new(AtomicUsize::new(0));
        let config = LoopConfig::new(LoopMode::Count(15_000)).with_max_iterations(15_000);
        let mut service = LoopService::new(config, counter_pipeline(Arc::clone(&counter)));
        let exchange = Exchange::new(Message::new("test"));
        let _ = service.ready().await.unwrap().call(exchange).await.unwrap();
        assert_eq!(counter.load(Ordering::SeqCst), 15_000);
    }

    #[tokio::test]
    async fn test_loop_while_with_custom_max_iterations() {
        let counter = Arc::new(AtomicUsize::new(0));
        let predicate = FilterPredicate::new(|_| true);
        let config = LoopConfig::new(LoopMode::While(predicate)).with_max_iterations(50);
        let mut service = LoopService::new(config, counter_pipeline(Arc::clone(&counter)));
        let exchange = Exchange::new(Message::new("test"));
        let _ = service.ready().await.unwrap().call(exchange).await.unwrap();
        assert_eq!(counter.load(Ordering::SeqCst), 50);
    }

    #[tokio::test]
    async fn test_loop_pipeline_chaining() {
        let pipeline = BoxProcessor::from_fn(|mut exchange: Exchange| {
            Box::pin(async move {
                if let Body::Text(s) = &exchange.input.body {
                    exchange.input.body = Body::Text(format!("{s}x"));
                }
                Ok(exchange)
            })
        });

        let config = LoopConfig::new(LoopMode::Count(3));
        let mut service = LoopService::new(config, pipeline);

        let exchange = Exchange::new(Message::new("start"));
        let result = service.ready().await.unwrap().call(exchange).await.unwrap();

        assert_eq!(result.input.body.as_text(), Some("startxxx"));
    }
}