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
//! Prometheus `/metrics` endpoint — pull-based, stateless.
//!
//! Every gauge is computed from the current store state on each scrape; nothing
//! is persisted. The endpoint is off by default and enabled via `[metrics]
//! enabled = true`. When disabled, the handler returns `404` so the route table
//! stays static while the endpoint is effectively absent.
//!
//! This complements webhooks (push of discrete events) with pull-based,
//! continuous dashboard state. Pulpo emits the numbers; it is not a TSDB —
//! Prometheus scrapes and stores them.

use std::sync::Arc;

use axum::extract::State;
use axum::http::{StatusCode, header};
use axum::response::{IntoResponse, Response};
use pulpo_common::session::{Session, SessionStatus, meta};

/// Prometheus text exposition format content type (version 0.0.4).
const CONTENT_TYPE: &str = "text/plain; version=0.0.4";

/// All session statuses, so every series is always present (zero when empty).
/// A stable label set keeps Prometheus graphs continuous across scrapes.
const ALL_STATUSES: [SessionStatus; 6] = [
    SessionStatus::Active,
    SessionStatus::Idle,
    SessionStatus::Ready,
    SessionStatus::Stopped,
    SessionStatus::Lost,
    SessionStatus::Creating,
];

/// Statuses considered terminal (the session is no longer doing work).
/// Cost is summed only across non-terminal sessions to reflect live spend.
const fn is_terminal(status: SessionStatus) -> bool {
    matches!(status, SessionStatus::Stopped | SessionStatus::Lost)
}

/// Escape a Prometheus label value per the text exposition spec:
/// backslash, double-quote, and newline are escaped. Returns owned only when
/// an escape was needed; otherwise borrows the input.
fn escape_label_value(value: &str) -> std::borrow::Cow<'_, str> {
    if value.contains(['\\', '"', '\n']) {
        let mut out = String::with_capacity(value.len() + 8);
        for ch in value.chars() {
            match ch {
                '\\' => out.push_str("\\\\"),
                '"' => out.push_str("\\\""),
                '\n' => out.push_str("\\n"),
                other => out.push(other),
            }
        }
        std::borrow::Cow::Owned(out)
    } else {
        std::borrow::Cow::Borrowed(value)
    }
}

/// Render the Prometheus text exposition for the given sessions.
///
/// Pure and total: no I/O, no panics. Computes every gauge from the snapshot so
/// the result is fully unit-testable. Output ends with a trailing newline and
/// parses as valid Prometheus text.
#[allow(clippy::cast_precision_loss)]
pub fn render_metrics(sessions: &[Session]) -> String {
    use std::fmt::Write as _;

    let mut out = String::new();

    // pulpo_sessions{status="..."} — count per status (every status present).
    out.push_str("# HELP pulpo_sessions Number of sessions by status.\n");
    out.push_str("# TYPE pulpo_sessions gauge\n");
    for status in ALL_STATUSES {
        let count = sessions.iter().filter(|s| s.status == status).count();
        // Status labels are a fixed enum (no escaping needed) but route through
        // the escaper so the format stays correct if the enum ever grows.
        let status_str = status.to_string();
        let label = escape_label_value(&status_str);
        // Writing to a String is infallible; discard the always-Ok Result.
        let _ = writeln!(out, "pulpo_sessions{{status=\"{label}\"}} {count}");
    }

    // pulpo_session_cost_usd — summed cost across non-terminal sessions.
    // `+ 0.0` collapses a possible `-0.0` (from summing negatives/empties) to
    // `0.0` so the rendered value is never the surprising `-0`.
    let cost_usd: f64 = sessions
        .iter()
        .filter(|s| !is_terminal(s.status))
        .filter_map(|s| s.meta_parsed::<f64>(meta::SESSION_COST_USD))
        .sum::<f64>()
        + 0.0;
    out.push_str(
        "# HELP pulpo_session_cost_usd Summed cost in USD across non-terminal sessions.\n",
    );
    out.push_str("# TYPE pulpo_session_cost_usd gauge\n");
    let _ = writeln!(out, "pulpo_session_cost_usd {cost_usd}");

    // pulpo_session_tokens — summed input+output tokens across all sessions.
    let tokens: u64 = sessions
        .iter()
        .map(|s| {
            s.meta_parsed::<u64>(meta::TOTAL_INPUT_TOKENS).unwrap_or(0)
                + s.meta_parsed::<u64>(meta::TOTAL_OUTPUT_TOKENS).unwrap_or(0)
        })
        .sum();
    out.push_str("# HELP pulpo_session_tokens Summed input and output tokens across sessions.\n");
    out.push_str("# TYPE pulpo_session_tokens gauge\n");
    let _ = writeln!(out, "pulpo_session_tokens {tokens}");

    // pulpo_sessions_with_budget — count of sessions with a cost budget set.
    let with_budget = sessions
        .iter()
        .filter(|s| s.meta_str(meta::BUDGET_COST_USD).is_some())
        .count();
    out.push_str("# HELP pulpo_sessions_with_budget Number of sessions with a cost budget set.\n");
    out.push_str("# TYPE pulpo_sessions_with_budget gauge\n");
    let _ = writeln!(out, "pulpo_sessions_with_budget {with_budget}");

    // pulpo_build_info{version="..."} — always 1, carries the version label.
    let version = escape_label_value(env!("CARGO_PKG_VERSION"));
    out.push_str("# HELP pulpo_build_info Build information; value is always 1.\n");
    out.push_str("# TYPE pulpo_build_info gauge\n");
    let _ = writeln!(out, "pulpo_build_info{{version=\"{version}\"}} 1");

    out
}

/// `GET /api/v1/metrics` — Prometheus scrape endpoint.
///
/// Returns `404` when `[metrics] enabled` is false (endpoint disabled). When
/// enabled, lists sessions from the store and renders the text exposition.
pub async fn metrics(State(state): State<Arc<super::AppState>>) -> Response {
    let enabled = state.config.read().await.metrics.enabled;
    if !enabled {
        return StatusCode::NOT_FOUND.into_response();
    }

    match state.store.list_sessions().await {
        Ok(sessions) => {
            let body = render_metrics(&sessions);
            ([(header::CONTENT_TYPE, CONTENT_TYPE)], body).into_response()
        }
        Err(e) => {
            tracing::error!("metrics: failed to list sessions: {e}");
            StatusCode::INTERNAL_SERVER_ERROR.into_response()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use uuid::Uuid;

    fn session(status: SessionStatus, meta: &[(&str, &str)]) -> Session {
        let metadata = if meta.is_empty() {
            None
        } else {
            Some(
                meta.iter()
                    .map(|(k, v)| ((*k).to_owned(), (*v).to_owned()))
                    .collect::<HashMap<_, _>>(),
            )
        };
        Session {
            id: Uuid::new_v4(),
            name: "s".into(),
            workdir: "/tmp".into(),
            command: "claude".into(),
            status,
            metadata,
            ..Default::default()
        }
    }

    /// Extract the value of a single (label-less) metric line.
    fn metric_value(text: &str, name: &str) -> Option<String> {
        text.lines().find_map(|line| {
            line.strip_prefix(name)
                .and_then(|rest| rest.strip_prefix(' '))
                .map(str::to_owned)
        })
    }

    #[test]
    fn test_escape_label_value_plain() {
        assert_eq!(escape_label_value("plain"), "plain");
        // Borrowed (no allocation) when nothing to escape.
        assert!(matches!(
            escape_label_value("plain"),
            std::borrow::Cow::Borrowed(_)
        ));
    }

    #[test]
    fn test_escape_label_value_special_chars() {
        assert_eq!(
            escape_label_value(r#"a\b"c"#),
            r#"a\\b\"c"#,
            "backslash and quote must be escaped"
        );
        assert_eq!(escape_label_value("line1\nline2"), "line1\\nline2");
    }

    #[test]
    fn test_is_terminal() {
        assert!(is_terminal(SessionStatus::Stopped));
        assert!(is_terminal(SessionStatus::Lost));
        assert!(!is_terminal(SessionStatus::Active));
        assert!(!is_terminal(SessionStatus::Idle));
        assert!(!is_terminal(SessionStatus::Ready));
        assert!(!is_terminal(SessionStatus::Creating));
    }

    #[test]
    fn test_render_empty_has_all_series_and_help() {
        let text = render_metrics(&[]);
        // Every status series present and zero.
        for status in ["active", "idle", "ready", "stopped", "lost", "creating"] {
            assert!(
                text.contains(&format!("pulpo_sessions{{status=\"{status}\"}} 0")),
                "missing zero series for {status}:\n{text}"
            );
        }
        // HELP and TYPE lines for each metric.
        for metric in [
            "pulpo_sessions",
            "pulpo_session_cost_usd",
            "pulpo_session_tokens",
            "pulpo_sessions_with_budget",
            "pulpo_build_info",
        ] {
            assert!(
                text.contains(&format!("# HELP {metric} ")),
                "no HELP {metric}"
            );
            assert!(
                text.contains(&format!("# TYPE {metric} gauge")),
                "no TYPE {metric}"
            );
        }
        // Trailing newline.
        assert!(text.ends_with('\n'));
        // build_info carries the crate version and is 1.
        assert!(text.contains(&format!(
            "pulpo_build_info{{version=\"{}\"}} 1",
            env!("CARGO_PKG_VERSION")
        )));
        // Empty snapshot → zero aggregates.
        assert_eq!(
            metric_value(&text, "pulpo_session_cost_usd").as_deref(),
            Some("0")
        );
        assert_eq!(
            metric_value(&text, "pulpo_session_tokens").as_deref(),
            Some("0")
        );
        assert_eq!(
            metric_value(&text, "pulpo_sessions_with_budget").as_deref(),
            Some("0")
        );
    }

    #[test]
    fn test_render_per_status_counts() {
        let sessions = vec![
            session(SessionStatus::Active, &[]),
            session(SessionStatus::Active, &[]),
            session(SessionStatus::Idle, &[]),
            session(SessionStatus::Stopped, &[]),
            session(SessionStatus::Lost, &[]),
            session(SessionStatus::Ready, &[]),
            session(SessionStatus::Creating, &[]),
        ];
        let text = render_metrics(&sessions);
        assert!(text.contains("pulpo_sessions{status=\"active\"} 2"));
        assert!(text.contains("pulpo_sessions{status=\"idle\"} 1"));
        assert!(text.contains("pulpo_sessions{status=\"ready\"} 1"));
        assert!(text.contains("pulpo_sessions{status=\"stopped\"} 1"));
        assert!(text.contains("pulpo_sessions{status=\"lost\"} 1"));
        assert!(text.contains("pulpo_sessions{status=\"creating\"} 1"));
    }

    #[test]
    fn test_render_cost_sum_excludes_terminal() {
        let sessions = vec![
            session(SessionStatus::Active, &[(meta::SESSION_COST_USD, "1.5")]),
            session(SessionStatus::Idle, &[(meta::SESSION_COST_USD, "2.25")]),
            // Terminal sessions must NOT count toward live cost.
            session(SessionStatus::Stopped, &[(meta::SESSION_COST_USD, "100")]),
            session(SessionStatus::Lost, &[(meta::SESSION_COST_USD, "100")]),
            // No cost metadata → contributes nothing.
            session(SessionStatus::Active, &[]),
        ];
        let text = render_metrics(&sessions);
        assert_eq!(
            metric_value(&text, "pulpo_session_cost_usd").as_deref(),
            Some("3.75")
        );
    }

    #[test]
    fn test_render_token_sum() {
        let sessions = vec![
            session(
                SessionStatus::Active,
                &[
                    (meta::TOTAL_INPUT_TOKENS, "100"),
                    (meta::TOTAL_OUTPUT_TOKENS, "50"),
                ],
            ),
            // Only input tokens present (output absent → treated as 0).
            session(SessionStatus::Idle, &[(meta::TOTAL_INPUT_TOKENS, "10")]),
            // Even terminal sessions count toward cumulative token totals.
            session(SessionStatus::Stopped, &[(meta::TOTAL_OUTPUT_TOKENS, "5")]),
        ];
        let text = render_metrics(&sessions);
        assert_eq!(
            metric_value(&text, "pulpo_session_tokens").as_deref(),
            Some("165")
        );
    }

    #[test]
    fn test_render_with_budget_count() {
        let sessions = vec![
            session(SessionStatus::Active, &[(meta::BUDGET_COST_USD, "10")]),
            session(SessionStatus::Idle, &[(meta::BUDGET_COST_USD, "5")]),
            session(SessionStatus::Active, &[]),
        ];
        let text = render_metrics(&sessions);
        assert_eq!(
            metric_value(&text, "pulpo_sessions_with_budget").as_deref(),
            Some("2")
        );
    }

    #[test]
    fn test_render_ignores_malformed_cost_metadata() {
        // A non-numeric cost value must be ignored, not panic or poison the sum.
        let sessions = vec![
            session(
                SessionStatus::Active,
                &[(meta::SESSION_COST_USD, "not-a-number")],
            ),
            session(SessionStatus::Active, &[(meta::SESSION_COST_USD, "2.0")]),
        ];
        let text = render_metrics(&sessions);
        assert_eq!(
            metric_value(&text, "pulpo_session_cost_usd").as_deref(),
            Some("2")
        );
    }

    // -- Handler tests --

    use crate::api::AppState;
    use crate::backend::StubBackend;
    use crate::config::{Config, MetricsConfig, NodeConfig};
    use crate::peers::PeerRegistry;
    use crate::session::manager::SessionManager;
    use crate::store::Store;
    use axum::body::to_bytes;
    use axum::http::StatusCode;

    async fn state_with_metrics(enabled: bool) -> Arc<AppState> {
        let tmpdir = tempfile::tempdir().unwrap();
        let tmpdir = Box::leak(Box::new(tmpdir));
        let store = Store::new(tmpdir.path().to_str().unwrap()).await.unwrap();
        store.migrate().await.unwrap();
        let config = Config {
            node: NodeConfig {
                name: "test-node".into(),
                port: 7433,
                data_dir: tmpdir.path().to_str().unwrap().into(),
                ..NodeConfig::default()
            },
            metrics: MetricsConfig { enabled },
            ..Default::default()
        };
        let backend = Arc::new(StubBackend);
        let manager = SessionManager::new(backend, store.clone(), None).with_no_stale_grace();
        let peer_registry = PeerRegistry::new(&HashMap::new());
        AppState::new(config, manager, peer_registry, store)
    }

    #[tokio::test]
    async fn test_handler_disabled_returns_404() {
        let state = state_with_metrics(false).await;
        let resp = metrics(State(state)).await;
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn test_handler_enabled_returns_text() {
        let state = state_with_metrics(true).await;
        let resp = metrics(State(state)).await;
        assert_eq!(resp.status(), StatusCode::OK);
        assert_eq!(
            resp.headers().get(header::CONTENT_TYPE).unwrap(),
            CONTENT_TYPE
        );
        let body = to_bytes(resp.into_body(), usize::MAX).await.unwrap();
        let text = String::from_utf8(body.to_vec()).unwrap();
        assert!(text.contains("# TYPE pulpo_sessions gauge"));
        assert!(text.contains("pulpo_build_info{version="));
    }

    #[tokio::test]
    async fn test_handler_store_error_returns_500() {
        let state = state_with_metrics(true).await;
        // Drop the sessions table so list_sessions errors → 500.
        sqlx::query("DROP TABLE sessions")
            .execute(state.store.pool())
            .await
            .unwrap();
        let resp = metrics(State(state)).await;
        assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
    }
}