pulpod 0.3.1

Pulpo daemon — manages agent sessions via tmux/Docker
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
pub mod webhook;

use std::sync::Arc;

use pulpo_common::event::{Event, PulpoEvent};
use tokio::sync::Semaphore;
use tracing::{info, warn};

use crate::config::WebhookEndpointConfig;

/// Maximum number of webhook deliveries in flight at once, across every
/// endpoint. Each event can spawn one delivery task per admitting endpoint —
/// with no cap, a burst of events (or many configured endpoints) could spawn
/// unboundedly many concurrent outbound requests. Beyond this cap, a delivery
/// is dropped (and logged) rather than queued, matching the existing
/// "in-memory queue, best-effort, no durable outbox" contract `deliver`
/// already documents: a dropped delivery here is no worse than one that
/// exhausts its retries.
pub(crate) const MAX_CONCURRENT_DELIVERIES: usize = 16;

/// Spawn a detached, best-effort delivery task for every webhook endpoint
/// whose filter admits `event`, bounded by `semaphore` (see
/// [`MAX_CONCURRENT_DELIVERIES`]). Delivery (including retries) happens
/// concurrently with the caller — this is the "in-memory queue": nothing is
/// persisted, so a delivery that's still retrying when the daemon exits is
/// simply lost. Returns the number of deliveries actually spawned (an
/// admitting endpoint dropped for lack of a permit is not counted).
fn dispatch_webhooks(
    client: &reqwest::Client,
    webhooks: &[WebhookEndpointConfig],
    event: &Event,
    semaphore: &Arc<Semaphore>,
) -> usize {
    let mut spawned = 0;
    for w in webhooks {
        if !webhook::webhook_wants(w, event) {
            continue;
        }
        let Ok(permit) = Arc::clone(semaphore).try_acquire_owned() else {
            warn!(
                webhook = %w.name,
                event = %format!("{}.{}", event.event_type, event.subtype),
                max_concurrent = MAX_CONCURRENT_DELIVERIES,
                "Dropping webhook delivery: max concurrent deliveries reached"
            );
            continue;
        };
        let client = client.clone();
        let config = w.clone();
        let event = event.clone();
        tokio::spawn(async move {
            webhook::deliver(&client, &config, &event).await;
            drop(permit);
        });
        spawned += 1;
    }
    spawned
}

/// Run the single event dispatcher loop.
///
/// Subscribes to the broadcast bus once, converts each [`PulpoEvent`] into the
/// canonical [`Event`] envelope (skipping events that aren't externally
/// forwarded), and fans it out to every admitting webhook endpoint via
/// [`dispatch_webhooks`] — a plain POST with a fixed retry schedule (see
/// [`webhook::deliver`]), best-effort, with no durable outbox.
pub async fn run_dispatcher_loop(
    webhooks: Vec<WebhookEndpointConfig>,
    node_name: String,
    mut rx: tokio::sync::broadcast::Receiver<PulpoEvent>,
    mut shutdown: tokio::sync::watch::Receiver<bool>,
) {
    let client = webhook::build_client();
    let semaphore = Arc::new(Semaphore::new(MAX_CONCURRENT_DELIVERIES));
    loop {
        tokio::select! {
            result = rx.recv() => {
                match result {
                    Ok(pulpo_event) => {
                        let Some(event) = Event::from_pulpo_event(&pulpo_event, &node_name) else {
                            continue;
                        };
                        dispatch_webhooks(&client, &webhooks, &event, &semaphore);
                    }
                    Err(tokio::sync::broadcast::error::RecvError::Lagged(n)) => {
                        warn!(missed = n, "Event dispatcher lagged, skipping events");
                    }
                    Err(tokio::sync::broadcast::error::RecvError::Closed) => {
                        info!("Event bus closed, stopping event dispatcher");
                        break;
                    }
                }
            }
            _ = shutdown.changed() => {
                info!("Event dispatcher shutting down");
                break;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pulpo_common::event::SessionEvent;

    fn session_pulpo_event(status: &str) -> PulpoEvent {
        PulpoEvent::Session(SessionEvent {
            session_id: "id-1".into(),
            session_name: "s".into(),
            status: status.into(),
            node_name: "n".into(),
            timestamp: "2026-06-13T12:00:00Z".into(),
            ..Default::default()
        })
    }

    fn webhook_config(name: &str, url: &str, events: Vec<String>) -> WebhookEndpointConfig {
        WebhookEndpointConfig {
            name: name.into(),
            url: url.into(),
            events,
            min_severity: None,
            secret: None,
        }
    }

    /// A freshly-full semaphore at the production cap, for tests that don't care
    /// about the concurrency limit itself.
    fn test_semaphore() -> Arc<Semaphore> {
        Arc::new(Semaphore::new(MAX_CONCURRENT_DELIVERIES))
    }

    // --- dispatch_webhooks ---

    #[tokio::test]
    async fn test_dispatch_admitting_endpoints_only() {
        let client = reqwest::Client::new();
        let event = Event::from_pulpo_event(&session_pulpo_event("active"), "mac-mini").unwrap();
        let webhooks = vec![
            webhook_config("all", "http://127.0.0.1:1/a", vec![]), // empty filter -> admits
            webhook_config(
                "stopped-only",
                "http://127.0.0.1:1/b",
                vec!["stopped".into()],
            ), // filtered out
        ];

        let n = dispatch_webhooks(&client, &webhooks, &event, &test_semaphore());
        assert_eq!(n, 1);
    }

    #[tokio::test]
    async fn test_dispatch_multiple_endpoints() {
        let client = reqwest::Client::new();
        let event = Event::from_pulpo_event(&session_pulpo_event("ready"), "n").unwrap();
        let webhooks = vec![
            webhook_config("a", "http://127.0.0.1:1/a", vec![]),
            webhook_config("b", "http://127.0.0.1:1/b", vec![]),
        ];

        assert_eq!(
            dispatch_webhooks(&client, &webhooks, &event, &test_semaphore()),
            2
        );
    }

    #[tokio::test]
    async fn test_dispatch_no_webhooks_is_noop() {
        let client = reqwest::Client::new();
        let event = Event::from_pulpo_event(&session_pulpo_event("active"), "n").unwrap();
        assert_eq!(
            dispatch_webhooks(&client, &[], &event, &test_semaphore()),
            0
        );
    }

    #[tokio::test]
    async fn test_dispatch_none_admit_is_noop() {
        let client = reqwest::Client::new();
        let event = Event::from_pulpo_event(&session_pulpo_event("active"), "n").unwrap();
        let webhooks = vec![webhook_config(
            "stopped-only",
            "http://127.0.0.1:1/a",
            vec!["stopped".into()],
        )];
        assert_eq!(
            dispatch_webhooks(&client, &webhooks, &event, &test_semaphore()),
            0
        );
    }

    #[tokio::test]
    async fn test_dispatch_drops_when_no_permits_available() {
        // Simulate every concurrency slot already being in use (e.g. a flood of
        // prior events still delivering to slow endpoints): a saturated semaphore
        // must make `dispatch_webhooks` drop the delivery — not spawn it anyway,
        // and not block waiting for a permit to free up.
        let client = reqwest::Client::new();
        let event = Event::from_pulpo_event(&session_pulpo_event("active"), "n").unwrap();
        let webhooks = vec![
            webhook_config("a", "http://127.0.0.1:1/a", vec![]),
            webhook_config("b", "http://127.0.0.1:1/b", vec![]),
        ];

        let semaphore = Arc::new(Semaphore::new(1));
        // Hold the single permit ourselves so the semaphore is fully saturated.
        let held = Arc::clone(&semaphore).try_acquire_owned().unwrap();
        assert_eq!(semaphore.available_permits(), 0);

        let spawned = dispatch_webhooks(&client, &webhooks, &event, &semaphore);
        assert_eq!(spawned, 0, "no permits available -> nothing spawned");
        assert_eq!(
            semaphore.available_permits(),
            0,
            "a dropped delivery must not touch the semaphore"
        );

        drop(held);
        assert_eq!(semaphore.available_permits(), 1);
    }

    #[tokio::test]
    async fn test_dispatch_flood_never_exceeds_concurrency_cap() {
        // A burst of far more admitting endpoints than the cap allows must still
        // spawn no more than `MAX_CONCURRENT_DELIVERIES` deliveries at once —
        // regardless of how many endpoints admit the event.
        let client = reqwest::Client::new();
        let event = Event::from_pulpo_event(&session_pulpo_event("active"), "n").unwrap();
        let webhooks: Vec<_> = (0..MAX_CONCURRENT_DELIVERIES * 4)
            .map(|i| webhook_config(&format!("hook-{i}"), "http://127.0.0.1:1/hook", vec![]))
            .collect();

        let semaphore = test_semaphore();
        let spawned = dispatch_webhooks(&client, &webhooks, &event, &semaphore);

        assert_eq!(spawned, MAX_CONCURRENT_DELIVERIES);
        assert_eq!(semaphore.available_permits(), 0);
    }

    fn usage_alert_pulpo_event() -> PulpoEvent {
        PulpoEvent::UsageAlert(pulpo_common::event::UsageAlertEvent {
            session_id: "s".into(),
            session_name: "n".into(),
            node_name: "x".into(),
            alert_kind: "budget_threshold".into(),
            message: "m".into(),
            cost_usd: Some(0.85),
            budget_usd: Some(1.0),
            timestamp: "2026-06-13T12:00:00Z".into(),
        })
    }

    #[tokio::test]
    async fn test_dispatch_usage_alert_routed_uniformly() {
        let client = reqwest::Client::new();
        let event = Event::from_pulpo_event(&usage_alert_pulpo_event(), "n").unwrap();
        let webhooks = vec![
            webhook_config(
                "usage",
                "http://127.0.0.1:1/a",
                vec!["usage_alert.*".into()],
            ),
            webhook_config(
                "lifecycle-only",
                "http://127.0.0.1:1/b",
                vec!["lifecycle.*".into()],
            ),
        ];
        assert_eq!(
            dispatch_webhooks(&client, &webhooks, &event, &test_semaphore()),
            1
        );
    }

    // --- dispatcher loop ---

    async fn run_until_idle(
        handle: tokio::task::JoinHandle<()>,
        shutdown_tx: &tokio::sync::watch::Sender<bool>,
    ) {
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        shutdown_tx.send(true).unwrap();
        tokio::time::timeout(std::time::Duration::from_secs(2), handle)
            .await
            .expect("finish")
            .expect("no panic");
    }

    /// Minimal capture server, used to prove the dispatcher loop actually
    /// spawns and completes a real delivery (not just that it "would").
    async fn capture_server() -> (String, std::sync::Arc<tokio::sync::Mutex<Vec<String>>>) {
        let captured = std::sync::Arc::new(tokio::sync::Mutex::new(Vec::new()));
        let captured_clone = captured.clone();
        let app = axum::Router::new().route(
            "/hook",
            axum::routing::post(move |body: String| {
                let captured = captured_clone.clone();
                async move {
                    captured.lock().await.push(body);
                    axum::http::StatusCode::OK
                }
            }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(axum::serve(listener, app).into_future());
        (format!("http://{addr}/hook"), captured)
    }

    #[tokio::test]
    async fn test_dispatcher_delivers_matching_webhook() {
        let (url, captured) = capture_server().await;
        let webhooks = vec![webhook_config("hook", &url, vec![])];
        let (event_tx, rx) = tokio::sync::broadcast::channel::<PulpoEvent>(16);
        let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);

        event_tx.send(session_pulpo_event("active")).unwrap();
        let handle = tokio::spawn(run_dispatcher_loop(
            webhooks,
            "mac-mini".into(),
            rx,
            shutdown_rx,
        ));
        run_until_idle(handle, &shutdown_tx).await;

        let bodies = captured.lock().await;
        assert_eq!(bodies.len(), 1);
        let json: serde_json::Value = serde_json::from_str(&bodies[0]).unwrap();
        assert_eq!(json["node"], "mac-mini");
        assert_eq!(json["type"], "lifecycle");
    }

    #[tokio::test]
    async fn test_dispatcher_skips_filtered_event() {
        let webhooks = vec![webhook_config(
            "hook",
            "http://127.0.0.1:1/hook",
            vec!["stopped".into()],
        )];
        let (event_tx, rx) = tokio::sync::broadcast::channel::<PulpoEvent>(16);
        let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);

        event_tx.send(session_pulpo_event("active")).unwrap();
        let handle = tokio::spawn(run_dispatcher_loop(webhooks, "n".into(), rx, shutdown_rx));
        run_until_idle(handle, &shutdown_tx).await;
    }

    #[tokio::test]
    async fn test_dispatcher_skips_session_deleted() {
        let (url, captured) = capture_server().await;
        let webhooks = vec![webhook_config("hook", &url, vec![])];
        let (event_tx, rx) = tokio::sync::broadcast::channel::<PulpoEvent>(16);
        let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);

        event_tx
            .send(PulpoEvent::SessionDeleted(
                pulpo_common::event::SessionDeletedEvent {
                    session_id: "s".into(),
                    session_name: "n".into(),
                    node_name: "x".into(),
                    timestamp: "t".into(),
                },
            ))
            .unwrap();
        let handle = tokio::spawn(run_dispatcher_loop(webhooks, "n".into(), rx, shutdown_rx));
        run_until_idle(handle, &shutdown_tx).await;

        // SessionDeleted is housekeeping -> nothing delivered.
        assert!(captured.lock().await.is_empty());
    }

    #[tokio::test]
    async fn test_dispatcher_usage_alert_reaches_webhook() {
        let (url, captured) = capture_server().await;
        let webhooks = vec![webhook_config("hook", &url, vec!["usage_alert.*".into()])];
        let (event_tx, rx) = tokio::sync::broadcast::channel::<PulpoEvent>(16);
        let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);

        event_tx.send(usage_alert_pulpo_event()).unwrap();
        let handle = tokio::spawn(run_dispatcher_loop(webhooks, "n".into(), rx, shutdown_rx));
        run_until_idle(handle, &shutdown_tx).await;

        let bodies = captured.lock().await;
        assert_eq!(bodies.len(), 1);
        let json: serde_json::Value = serde_json::from_str(&bodies[0]).unwrap();
        assert_eq!(json["type"], "usage_alert");
    }

    #[tokio::test]
    async fn test_dispatcher_usage_alert_dropped_by_lifecycle_filter() {
        let webhooks = vec![webhook_config(
            "hook",
            "http://127.0.0.1:1/hook",
            vec!["lifecycle.*".into()],
        )];
        let (event_tx, rx) = tokio::sync::broadcast::channel::<PulpoEvent>(16);
        let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);

        event_tx.send(usage_alert_pulpo_event()).unwrap();
        let handle = tokio::spawn(run_dispatcher_loop(webhooks, "n".into(), rx, shutdown_rx));
        run_until_idle(handle, &shutdown_tx).await;
    }

    #[tokio::test]
    async fn test_dispatcher_shutdown() {
        let (event_tx, _) = tokio::sync::broadcast::channel::<PulpoEvent>(16);
        let rx = event_tx.subscribe();
        let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);
        shutdown_tx.send(true).unwrap();
        tokio::time::timeout(
            std::time::Duration::from_secs(2),
            run_dispatcher_loop(vec![], "n".into(), rx, shutdown_rx),
        )
        .await
        .expect("should exit on shutdown");
    }

    #[tokio::test]
    async fn test_dispatcher_channel_closed() {
        let (event_tx, rx) = tokio::sync::broadcast::channel::<PulpoEvent>(16);
        let (_shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);
        drop(event_tx);
        tokio::time::timeout(
            std::time::Duration::from_secs(2),
            run_dispatcher_loop(vec![], "n".into(), rx, shutdown_rx),
        )
        .await
        .expect("should exit when channel closes");
    }

    #[tokio::test]
    async fn test_dispatcher_lagged() {
        let (event_tx, rx) = tokio::sync::broadcast::channel::<PulpoEvent>(1);
        let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);
        // Overflow before the loop starts to force a Lagged error.
        for _ in 0..5 {
            let _ = event_tx.send(session_pulpo_event("active"));
        }
        let handle = tokio::spawn(run_dispatcher_loop(vec![], "n".into(), rx, shutdown_rx));
        run_until_idle(handle, &shutdown_tx).await;
    }
}