pulpod 0.1.0

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
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
pub mod action_token;
pub mod outbox;
pub mod web_push;
pub mod webhook;

use chrono::Utc;
use pulpo_common::event::{Event, PulpoEvent};
use tracing::{info, warn};

use self::web_push::WebPushSink;
use self::webhook::webhook_wants;
use crate::config::WebhookEndpointConfig;
use crate::store::Store;

/// Enqueue a canonical event into the durable webhook outbox for every endpoint
/// whose filter admits it. Each row becomes due immediately (`next_attempt_at =
/// now`); the [`outbox::run_outbox_worker`] drains and delivers it with retry +
/// backoff. The serialized envelope is stored verbatim so retries replay the
/// identical body and `X-Pulpo-Event-Id`.
///
/// Returns the number of rows enqueued. Best-effort: a per-endpoint enqueue
/// failure is logged and skipped (never panics).
async fn enqueue_event_webhooks(
    store: &Store,
    webhooks: &[WebhookEndpointConfig],
    event: &Event,
) -> usize {
    let admitting: Vec<&WebhookEndpointConfig> = webhooks
        .iter()
        .filter(|w| webhook_wants(w, event))
        .collect();
    if admitting.is_empty() {
        return 0;
    }

    let envelope_json = match serde_json::to_string(event) {
        Ok(json) => json,
        Err(e) => {
            warn!(error = %e, "Failed to serialize event for webhook outbox");
            return 0;
        }
    };
    let now = Utc::now().to_rfc3339();

    let mut enqueued = 0;
    for w in admitting {
        match store
            .enqueue_webhook(&w.name, &event.event_id, &envelope_json, &now)
            .await
        {
            Ok(()) => enqueued += 1,
            Err(e) => warn!(
                webhook = %w.name,
                error = %e,
                "Failed to enqueue webhook delivery"
            ),
        }
    }
    enqueued
}

/// 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 routes it:
/// - **webhooks** → enqueued into the durable outbox (delivery happens in
///   [`outbox::run_outbox_worker`] with retry + exponential backoff, surviving
///   restarts and transient endpoint failures);
/// - **web-push** → delivered inline, best-effort (browser push is inherently
///   best-effort, so it is intentionally *not* routed through the outbox).
pub async fn run_dispatcher_loop(
    store: Store,
    webhooks: Vec<WebhookEndpointConfig>,
    web_push: Option<WebPushSink>,
    node_name: String,
    mut rx: tokio::sync::broadcast::Receiver<PulpoEvent>,
    mut shutdown: tokio::sync::watch::Receiver<bool>,
) {
    loop {
        tokio::select! {
            result = rx.recv() => {
                match result {
                    Ok(pulpo_event) => {
                        let Some(event) = Event::from_pulpo_event(&pulpo_event, &node_name) else {
                            continue;
                        };
                        // Webhooks: durable enqueue (delivered by the outbox worker).
                        enqueue_event_webhooks(&store, &webhooks, &event).await;
                        // Web-push: inline best-effort.
                        if let Some(push) = &web_push
                            && push.wants(&event)
                        {
                            push.deliver(&event).await;
                        }
                    }
                    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 crate::store::test_store;
    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, events: Vec<String>) -> WebhookEndpointConfig {
        WebhookEndpointConfig {
            name: name.into(),
            url: format!("http://example.com/{name}"),
            events,
            min_severity: None,
            secret: None,
        }
    }

    async fn pending_event_ids(store: &Store) -> Vec<String> {
        store
            .fetch_due_webhook_deliveries("2027-01-01T00:00:00Z", 100)
            .await
            .unwrap()
            .into_iter()
            .map(|r| format!("{}:{}", r.endpoint, r.event_id))
            .collect()
    }

    // --- enqueue_event_webhooks ---

    #[tokio::test]
    async fn test_enqueue_admitting_endpoints_only() {
        let store = test_store().await;
        let event = Event::from_pulpo_event(&session_pulpo_event("active"), "mac-mini").unwrap();
        let webhooks = vec![
            webhook_config("all", vec![]), // empty filter -> admits
            webhook_config("stopped-only", vec!["stopped".into()]), // filtered out
        ];

        let n = enqueue_event_webhooks(&store, &webhooks, &event).await;
        assert_eq!(n, 1);

        let rows = store
            .fetch_due_webhook_deliveries("2027-01-01T00:00:00Z", 10)
            .await
            .unwrap();
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].endpoint, "all");
        assert_eq!(rows[0].event_id, event.event_id);
        // Stored envelope is the exact serialized event.
        assert_eq!(
            rows[0].envelope_json,
            serde_json::to_string(&event).unwrap()
        );
        assert_eq!(rows[0].status, "pending");
    }

    #[tokio::test]
    async fn test_enqueue_multiple_endpoints() {
        let store = test_store().await;
        let event = Event::from_pulpo_event(&session_pulpo_event("ready"), "n").unwrap();
        let webhooks = vec![webhook_config("a", vec![]), webhook_config("b", vec![])];

        let n = enqueue_event_webhooks(&store, &webhooks, &event).await;
        assert_eq!(n, 2);
        let mut ids = pending_event_ids(&store).await;
        ids.sort();
        assert_eq!(
            ids,
            vec![
                format!("a:{}", event.event_id),
                format!("b:{}", event.event_id),
            ]
        );
    }

    #[tokio::test]
    async fn test_enqueue_no_webhooks_is_noop() {
        let store = test_store().await;
        let event = Event::from_pulpo_event(&session_pulpo_event("active"), "n").unwrap();
        assert_eq!(enqueue_event_webhooks(&store, &[], &event).await, 0);
    }

    #[tokio::test]
    async fn test_enqueue_none_admit_is_noop() {
        let store = test_store().await;
        let event = Event::from_pulpo_event(&session_pulpo_event("active"), "n").unwrap();
        let webhooks = vec![webhook_config("stopped-only", vec!["stopped".into()])];
        assert_eq!(enqueue_event_webhooks(&store, &webhooks, &event).await, 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_enqueue_usage_alert_routed_uniformly() {
        let store = test_store().await;
        let event = Event::from_pulpo_event(&usage_alert_pulpo_event(), "n").unwrap();
        // Universal routing: a `usage_alert.*` glob admits it, a lifecycle-only
        // glob does not (no more "usage alerts always flow" special case).
        let webhooks = vec![
            webhook_config("usage", vec!["usage_alert.*".into()]),
            webhook_config("lifecycle-only", vec!["lifecycle.*".into()]),
        ];
        let n = enqueue_event_webhooks(&store, &webhooks, &event).await;
        assert_eq!(n, 1);
        let rows = store
            .fetch_due_webhook_deliveries("2027-01-01T00:00:00Z", 10)
            .await
            .unwrap();
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].endpoint, "usage");
    }

    #[tokio::test]
    async fn test_enqueue_store_error_is_swallowed() {
        let store = test_store().await;
        sqlx::query("DROP TABLE webhook_outbox")
            .execute(store.pool())
            .await
            .unwrap();
        let event = Event::from_pulpo_event(&session_pulpo_event("active"), "n").unwrap();
        let webhooks = vec![webhook_config("a", vec![])];
        // Enqueue fails (table gone) -> 0 enqueued, no panic.
        assert_eq!(enqueue_event_webhooks(&store, &webhooks, &event).await, 0);
    }

    // --- 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");
    }

    #[tokio::test]
    async fn test_dispatcher_enqueues_matching_webhook() {
        let store = test_store().await;
        let webhooks = vec![webhook_config("hook", 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(
            store.clone(),
            webhooks,
            None,
            "mac-mini".into(),
            rx,
            shutdown_rx,
        ));
        run_until_idle(handle, &shutdown_tx).await;

        let rows = store
            .fetch_due_webhook_deliveries("2027-01-01T00:00:00Z", 10)
            .await
            .unwrap();
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].endpoint, "hook");
        let json: serde_json::Value = serde_json::from_str(&rows[0].envelope_json).unwrap();
        assert_eq!(json["node"], "mac-mini");
        assert_eq!(json["type"], "lifecycle");
    }

    #[tokio::test]
    async fn test_dispatcher_skips_filtered_event() {
        let store = test_store().await;
        // Endpoint only wants "stopped"; we send "active".
        let webhooks = vec![webhook_config("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(
            store.clone(),
            webhooks,
            None,
            "n".into(),
            rx,
            shutdown_rx,
        ));
        run_until_idle(handle, &shutdown_tx).await;

        assert!(pending_event_ids(&store).await.is_empty());
    }

    #[tokio::test]
    async fn test_dispatcher_skips_session_deleted() {
        let store = test_store().await;
        let webhooks = vec![webhook_config("hook", 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(
            store.clone(),
            webhooks,
            None,
            "n".into(),
            rx,
            shutdown_rx,
        ));
        run_until_idle(handle, &shutdown_tx).await;

        // SessionDeleted is housekeeping -> nothing enqueued.
        assert!(pending_event_ids(&store).await.is_empty());
    }

    #[tokio::test]
    async fn test_dispatcher_usage_alert_reaches_webhook() {
        let store = test_store().await;
        // Universal routing: a `usage_alert.*` glob admits the alert.
        let webhooks = vec![webhook_config("hook", 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(
            store.clone(),
            webhooks,
            None,
            "n".into(),
            rx,
            shutdown_rx,
        ));
        run_until_idle(handle, &shutdown_tx).await;

        let rows = store
            .fetch_due_webhook_deliveries("2027-01-01T00:00:00Z", 10)
            .await
            .unwrap();
        assert_eq!(rows.len(), 1);
        let json: serde_json::Value = serde_json::from_str(&rows[0].envelope_json).unwrap();
        assert_eq!(json["type"], "usage_alert");
    }

    #[tokio::test]
    async fn test_dispatcher_usage_alert_dropped_by_lifecycle_filter() {
        let store = test_store().await;
        // A lifecycle-only filter now drops usage alerts (uniform routing).
        let webhooks = vec![webhook_config("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(
            store.clone(),
            webhooks,
            None,
            "n".into(),
            rx,
            shutdown_rx,
        ));
        run_until_idle(handle, &shutdown_tx).await;

        assert!(pending_event_ids(&store).await.is_empty());
    }

    #[tokio::test]
    async fn test_dispatcher_with_web_push_sink_no_panic() {
        // Web-push delivered inline; no subscriptions -> no-op, and webhook still enqueued.
        let store = test_store().await;
        let webhooks = vec![webhook_config("hook", vec![])];
        let push = WebPushSink::new(store.clone(), "k".into(), "action-secret".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("ready")).unwrap();
        let handle = tokio::spawn(run_dispatcher_loop(
            store.clone(),
            webhooks,
            Some(push),
            "n".into(),
            rx,
            shutdown_rx,
        ));
        run_until_idle(handle, &shutdown_tx).await;

        assert_eq!(pending_event_ids(&store).await.len(), 1);
    }

    #[tokio::test]
    async fn test_dispatcher_shutdown() {
        let store = test_store().await;
        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(store, vec![], None, "n".into(), rx, shutdown_rx),
        )
        .await
        .expect("should exit on shutdown");
    }

    #[tokio::test]
    async fn test_dispatcher_channel_closed() {
        let store = test_store().await;
        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(store, vec![], None, "n".into(), rx, shutdown_rx),
        )
        .await
        .expect("should exit when channel closes");
    }

    #[tokio::test]
    async fn test_dispatcher_lagged() {
        let store = test_store().await;
        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(
            store,
            vec![],
            None,
            "n".into(),
            rx,
            shutdown_rx,
        ));
        run_until_idle(handle, &shutdown_tx).await;
    }
}