paladin-ai 0.5.0

Enterprise AI orchestration framework with multi-agent coordination patterns
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
//! Concrete agent → orchestrator bridge adapter.
//!
//! This module implements [`OrchestratorPort`] over the root-crate
//! [`Orchestrator`]. It lives in the **root crate** (not `paladin-ports`)
//! because it depends on the concrete `Orchestrator`: a lower crate cannot
//! depend on the root crate without introducing a circular dependency. This is
//! the same placement constraint documented for the Epic 3 content processors.
//!
//! The adapter enforces a [`BridgePolicy`] *before* performing any underlying
//! orchestrator call: a disallowed action yields
//! [`OrchestratorBridgeError::ActionNotAllowed`] and a cap-exceeding action
//! yields [`OrchestratorBridgeError::QuotaExceeded`]. Caps are tracked
//! per-adapter-instance; construct one adapter per agent execution so caps are
//! scoped to that execution.

use std::sync::Arc;
use std::sync::atomic::{AtomicU32, Ordering};

use async_trait::async_trait;
use uuid::Uuid;

use paladin_ports::output::notification_port::NotificationDeliveryPort;
use paladin_ports::output::orchestrator_port::{
    BridgeAction, BridgePolicy, EventDispatchResult, FireEventRequest, OrchestratorBridgeError,
    OrchestratorPort, QueueItemRequest, ScheduleJobRequest, SendNotificationRequest,
};

use crate::core::base::component::event::Event;
use crate::core::platform::container::job::Job;
use crate::core::platform::container::notification::{
    Notification, NotificationChannel, NotificationContent, NotificationPriority,
    NotificationRecipient,
};
use crate::core::platform::container::orchestration_context::OrchestrationContext;

use super::Orchestrator;

/// Per-execution counters for the four bridge actions.
#[derive(Debug, Default)]
struct ActionCounters {
    jobs_scheduled: AtomicU32,
    items_queued: AtomicU32,
    events_fired: AtomicU32,
    notifications_sent: AtomicU32,
}

impl ActionCounters {
    fn counter(&self, action: BridgeAction) -> &AtomicU32 {
        match action {
            BridgeAction::ScheduleJob => &self.jobs_scheduled,
            BridgeAction::QueueItem => &self.items_queued,
            BridgeAction::FireEvent => &self.events_fired,
            BridgeAction::SendNotification => &self.notifications_sent,
        }
    }
}

/// Concrete [`OrchestratorPort`] backed by an [`Orchestrator`].
pub struct OrchestratorBridgeAdapter {
    orchestrator: Arc<Orchestrator>,
    policy: BridgePolicy,
    notification: Option<Arc<dyn NotificationDeliveryPort>>,
    counters: ActionCounters,
}

impl OrchestratorBridgeAdapter {
    /// Create a new bridge adapter with the given policy and no notification
    /// delivery port. `send_notification` will fail until a delivery port is
    /// attached via [`OrchestratorBridgeAdapter::with_notification_port`].
    pub fn new(orchestrator: Arc<Orchestrator>, policy: BridgePolicy) -> Self {
        Self {
            orchestrator,
            policy,
            notification: None,
            counters: ActionCounters::default(),
        }
    }

    /// Attach a notification delivery port enabling `send_notification`.
    pub fn with_notification_port(
        mut self,
        notification: Arc<dyn NotificationDeliveryPort>,
    ) -> Self {
        self.notification = Some(notification);
        self
    }

    /// Enforce the policy for an action: reject disallowed actions and count the
    /// invocation against the per-execution cap.
    fn enforce(&self, action: BridgeAction) -> Result<(), OrchestratorBridgeError> {
        if !self.policy.is_allowed(action) {
            return Err(OrchestratorBridgeError::ActionNotAllowed(
                action.as_str().to_string(),
            ));
        }
        let cap = self.policy.cap_for(action);
        // fetch_add returns the previous value; the new count is previous + 1.
        let new_count = self.counters.counter(action).fetch_add(1, Ordering::SeqCst) + 1;
        if new_count > cap {
            return Err(OrchestratorBridgeError::QuotaExceeded {
                action: action.as_str().to_string(),
                limit: cap,
            });
        }
        Ok(())
    }

    fn context() -> OrchestrationContext {
        OrchestrationContext::new("agent_orchestrator_bridge".to_string(), "agent".to_string())
    }
}

#[async_trait]
impl OrchestratorPort for OrchestratorBridgeAdapter {
    async fn schedule_job(
        &self,
        request: ScheduleJobRequest,
    ) -> Result<Uuid, OrchestratorBridgeError> {
        self.enforce(BridgeAction::ScheduleJob)?;
        if request.name.trim().is_empty() {
            return Err(OrchestratorBridgeError::InvalidRequest(
                "job name cannot be empty".to_string(),
            ));
        }
        let job = Job::new(request.name, request.description, Vec::new());
        self.orchestrator
            .schedule_job(job, request.schedule, Self::context())
            .await
            .map_err(|e| OrchestratorBridgeError::OrchestratorError(e.to_string()))
    }

    async fn queue_item(&self, request: QueueItemRequest) -> Result<Uuid, OrchestratorBridgeError> {
        self.enforce(BridgeAction::QueueItem)?;
        if request.queue_name.trim().is_empty() {
            return Err(OrchestratorBridgeError::InvalidRequest(
                "queue name cannot be empty".to_string(),
            ));
        }
        let description = request.payload.to_string();
        let job = Job::new(
            format!("queued-item-{}", request.queue_name),
            description,
            Vec::new(),
        );
        self.orchestrator
            .ensure_queue(&request.queue_name)
            .await
            .map_err(|e| OrchestratorBridgeError::OrchestratorError(e.to_string()))?;
        self.orchestrator
            .queue_job(job, &request.queue_name, Self::context())
            .await
            .map_err(|e| OrchestratorBridgeError::OrchestratorError(e.to_string()))
    }

    async fn fire_event(
        &self,
        request: FireEventRequest,
    ) -> Result<EventDispatchResult, OrchestratorBridgeError> {
        self.enforce(BridgeAction::FireEvent)?;
        if request.event_type.trim().is_empty() {
            return Err(OrchestratorBridgeError::InvalidRequest(
                "event type cannot be empty".to_string(),
            ));
        }
        let event = Event::new(request.event_type, request.payload, request.source);
        let trigger_ids = self
            .orchestrator
            .process_event(event)
            .await
            .map_err(|e| OrchestratorBridgeError::OrchestratorError(e.to_string()))?;
        Ok(EventDispatchResult {
            triggered_count: trigger_ids.len(),
            trigger_ids,
        })
    }

    async fn send_notification(
        &self,
        request: SendNotificationRequest,
    ) -> Result<Uuid, OrchestratorBridgeError> {
        self.enforce(BridgeAction::SendNotification)?;
        let delivery = self.notification.as_ref().ok_or_else(|| {
            OrchestratorBridgeError::InvalidRequest(
                "no notification delivery port is configured".to_string(),
            )
        })?;

        let (channel, recipient) = map_channel_and_recipient(&request.channel, &request.recipient);
        let content = NotificationContent::new(
            request.subject,
            request.body,
            "agent_notification".to_string(),
        );
        let notification =
            Notification::new(recipient, content, channel, NotificationPriority::Normal)
                .map_err(|e| OrchestratorBridgeError::InvalidRequest(e.to_string()))?;
        let id = notification.id;

        delivery
            .deliver_notification(notification)
            .await
            .map_err(|e| OrchestratorBridgeError::OrchestratorError(e.to_string()))?;
        Ok(id)
    }
}

/// Map an agent-supplied channel string and recipient identifier onto a
/// compatible [`NotificationChannel`]/[`NotificationRecipient`] pair.
fn map_channel_and_recipient(
    channel: &str,
    recipient: &str,
) -> (NotificationChannel, NotificationRecipient) {
    match channel.to_ascii_lowercase().as_str() {
        "email" => (
            NotificationChannel::Email,
            NotificationRecipient::Email(recipient.to_string()),
        ),
        "sms" => (
            NotificationChannel::Sms,
            NotificationRecipient::Phone(recipient.to_string()),
        ),
        "webhook" => (
            NotificationChannel::Webhook,
            NotificationRecipient::WebhookUrl(recipient.to_string()),
        ),
        _ => (
            NotificationChannel::System,
            NotificationRecipient::SystemComponent(recipient.to_string()),
        ),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use paladin_ports::output::notification_port::{
        DeliveryCapabilities, NotificationDeliveryResult, NotificationPortResult,
        NotificationStatus,
    };
    use std::collections::HashSet;
    use std::time::Duration;

    fn interval_schedule() -> paladin_core::platform::container::schedule::Schedule {
        paladin_core::platform::container::schedule::Schedule::Interval(Duration::from_secs(60))
    }

    fn allow_only(action: BridgeAction) -> BridgePolicy {
        let mut allowed = HashSet::new();
        allowed.insert(action);
        BridgePolicy::new(allowed, 1, 1, 1, 1)
    }

    struct MockDeliveryPort;

    #[async_trait]
    impl NotificationDeliveryPort for MockDeliveryPort {
        fn channel(&self) -> NotificationChannel {
            NotificationChannel::System
        }
        fn can_handle(&self, _notification: &Notification) -> bool {
            true
        }
        async fn deliver_notification(
            &self,
            notification: Notification,
        ) -> NotificationPortResult<NotificationDeliveryResult> {
            Ok(NotificationDeliveryResult {
                notification_id: notification.id,
                status: NotificationStatus::Delivered,
                external_id: Some("mock".to_string()),
                processing_time_ms: 1,
                error_message: None,
                delivered_at: chrono::Utc::now(),
                channel: notification.channel,
                metadata: Default::default(),
            })
        }
        async fn health_check(&self) -> bool {
            true
        }
        fn capabilities(&self) -> DeliveryCapabilities {
            DeliveryCapabilities {
                supports_bulk: false,
                supports_receipts: false,
                supports_attachments: false,
                supports_rich_content: false,
                supports_templates: false,
                max_attachment_size: None,
                rate_limit: None,
            }
        }
    }

    #[tokio::test]
    async fn schedule_job_success() {
        let orchestrator = Arc::new(Orchestrator::new());
        let adapter =
            OrchestratorBridgeAdapter::new(Arc::clone(&orchestrator), BridgePolicy::default());
        let result = adapter
            .schedule_job(ScheduleJobRequest {
                name: "follow-up".to_string(),
                description: "re-analyze tomorrow".to_string(),
                schedule: interval_schedule(),
            })
            .await;
        assert!(result.is_ok());
        let stats = orchestrator.get_stats().await;
        assert_eq!(stats.scheduler_stats.total_jobs, 1);
    }

    #[tokio::test]
    async fn schedule_job_action_not_allowed() {
        let orchestrator = Arc::new(Orchestrator::new());
        let adapter =
            OrchestratorBridgeAdapter::new(orchestrator, allow_only(BridgeAction::FireEvent));
        let err = adapter
            .schedule_job(ScheduleJobRequest {
                name: "x".to_string(),
                description: "y".to_string(),
                schedule: interval_schedule(),
            })
            .await
            .unwrap_err();
        assert!(matches!(err, OrchestratorBridgeError::ActionNotAllowed(_)));
    }

    #[tokio::test]
    async fn schedule_job_quota_exceeded() {
        let orchestrator = Arc::new(Orchestrator::new());
        let adapter =
            OrchestratorBridgeAdapter::new(orchestrator, allow_only(BridgeAction::ScheduleJob));
        // First call within cap (1).
        assert!(
            adapter
                .schedule_job(ScheduleJobRequest {
                    name: "a".to_string(),
                    description: "d".to_string(),
                    schedule: interval_schedule(),
                })
                .await
                .is_ok()
        );
        // Second call exceeds cap.
        let err = adapter
            .schedule_job(ScheduleJobRequest {
                name: "b".to_string(),
                description: "d".to_string(),
                schedule: interval_schedule(),
            })
            .await
            .unwrap_err();
        assert!(matches!(
            err,
            OrchestratorBridgeError::QuotaExceeded { limit: 1, .. }
        ));
    }

    #[tokio::test]
    async fn queue_item_success() {
        let orchestrator = Arc::new(Orchestrator::new());
        let adapter = OrchestratorBridgeAdapter::new(orchestrator, BridgePolicy::default());
        let result = adapter
            .queue_item(QueueItemRequest {
                queue_name: "analysis".to_string(),
                payload: serde_json::json!({"doc": "1"}),
            })
            .await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn queue_item_action_not_allowed() {
        let orchestrator = Arc::new(Orchestrator::new());
        let adapter =
            OrchestratorBridgeAdapter::new(orchestrator, allow_only(BridgeAction::ScheduleJob));
        let err = adapter
            .queue_item(QueueItemRequest {
                queue_name: "q".to_string(),
                payload: serde_json::json!({}),
            })
            .await
            .unwrap_err();
        assert!(matches!(err, OrchestratorBridgeError::ActionNotAllowed(_)));
    }

    #[tokio::test]
    async fn fire_event_success() {
        let orchestrator = Arc::new(Orchestrator::new());
        let adapter = OrchestratorBridgeAdapter::new(orchestrator, BridgePolicy::default());
        let result = adapter
            .fire_event(FireEventRequest {
                event_type: "critical_finding".to_string(),
                payload: serde_json::json!({"severity": "high"}),
                source: "analyst_agent".to_string(),
            })
            .await;
        let dispatch = result.expect("event should dispatch");
        // No listeners registered, so no triggers are created.
        assert_eq!(dispatch.triggered_count, 0);
    }

    #[tokio::test]
    async fn fire_event_quota_exceeded() {
        let orchestrator = Arc::new(Orchestrator::new());
        let adapter =
            OrchestratorBridgeAdapter::new(orchestrator, allow_only(BridgeAction::FireEvent));
        assert!(
            adapter
                .fire_event(FireEventRequest {
                    event_type: "e".to_string(),
                    payload: serde_json::json!({}),
                    source: "s".to_string(),
                })
                .await
                .is_ok()
        );
        let err = adapter
            .fire_event(FireEventRequest {
                event_type: "e".to_string(),
                payload: serde_json::json!({}),
                source: "s".to_string(),
            })
            .await
            .unwrap_err();
        assert!(matches!(
            err,
            OrchestratorBridgeError::QuotaExceeded { limit: 1, .. }
        ));
    }

    #[tokio::test]
    async fn send_notification_success() {
        let orchestrator = Arc::new(Orchestrator::new());
        let adapter = OrchestratorBridgeAdapter::new(orchestrator, BridgePolicy::default())
            .with_notification_port(Arc::new(MockDeliveryPort));
        let result = adapter
            .send_notification(SendNotificationRequest {
                channel: "system".to_string(),
                recipient: "ops".to_string(),
                subject: "alert".to_string(),
                body: "critical finding".to_string(),
            })
            .await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn send_notification_without_port_fails() {
        let orchestrator = Arc::new(Orchestrator::new());
        let adapter = OrchestratorBridgeAdapter::new(orchestrator, BridgePolicy::default());
        let err = adapter
            .send_notification(SendNotificationRequest {
                channel: "system".to_string(),
                recipient: "ops".to_string(),
                subject: "alert".to_string(),
                body: "body".to_string(),
            })
            .await
            .unwrap_err();
        assert!(matches!(err, OrchestratorBridgeError::InvalidRequest(_)));
    }

    #[tokio::test]
    async fn send_notification_action_not_allowed() {
        let orchestrator = Arc::new(Orchestrator::new());
        let adapter =
            OrchestratorBridgeAdapter::new(orchestrator, allow_only(BridgeAction::ScheduleJob))
                .with_notification_port(Arc::new(MockDeliveryPort));
        let err = adapter
            .send_notification(SendNotificationRequest {
                channel: "system".to_string(),
                recipient: "ops".to_string(),
                subject: "alert".to_string(),
                body: "body".to_string(),
            })
            .await
            .unwrap_err();
        assert!(matches!(err, OrchestratorBridgeError::ActionNotAllowed(_)));
    }
}