pulpod 0.0.41

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
478
479
480
481
482
483
484
485
486
487
488
use rmcp::model::{
    AnnotateAble, ErrorData, ListResourcesResult, RawResource, ReadResourceResult, ResourceContents,
};

use super::PulpoMcp;

pub fn list_resources() -> ListResourcesResult {
    ListResourcesResult {
        resources: vec![
            RawResource {
                uri: "pulpo://sessions".into(),
                name: "sessions".into(),
                description: Some("JSON list of all sessions".into()),
                mime_type: Some("application/json".into()),
                ..RawResource::new("pulpo://sessions", "sessions")
            }
            .no_annotation(),
            RawResource {
                uri: "pulpo://nodes".into(),
                name: "nodes".into(),
                description: Some("JSON with local node info and peers".into()),
                mime_type: Some("application/json".into()),
                ..RawResource::new("pulpo://nodes", "nodes")
            }
            .no_annotation(),
            RawResource {
                uri: "pulpo://cluster/status".into(),
                name: "cluster_status".into(),
                description: Some(
                    "Curated, agent-readable cluster status: session counts, idle/waiting sessions, node info, and peers"
                        .into(),
                ),
                mime_type: Some("application/json".into()),
                ..RawResource::new("pulpo://cluster/status", "cluster_status")
            }
            .no_annotation(),
        ],
        ..Default::default()
    }
}

async fn read_cluster_status(mcp: &PulpoMcp, uri: &str) -> Result<ReadResourceResult, ErrorData> {
    let sessions = mcp
        .session_manager
        .list_sessions()
        .await
        .map_err(|e| ErrorData::internal_error(e.to_string(), None))?;

    let total = sessions.len();
    let mut by_status = std::collections::HashMap::new();
    let mut idle = Vec::new();

    for s in &sessions {
        *by_status.entry(s.status.to_string()).or_insert(0usize) += 1;

        if let Some(idle_since) = s.idle_since {
            let idle_minutes =
                u64::try_from((chrono::Utc::now() - idle_since).num_minutes().max(0)).unwrap_or(0);
            idle.push(serde_json::json!({
                "id": s.id.to_string(),
                "name": s.name,
                "idle_minutes": idle_minutes,
            }));
        }
    }

    let local = mcp.build_node_info();
    let peers = mcp.peer_registry.get_all().await;
    let available_count = 1 + peers
        .iter()
        .filter(|p| p.status == pulpo_common::peer::PeerStatus::Online)
        .count();

    let running_count = by_status.get("active").copied().unwrap_or(0);
    let completed_count = by_status.get("ready").copied().unwrap_or(0);
    let summary = format!(
        "{total} sessions ({running_count} active, {completed_count} ready). {available_count} nodes online."
    );

    let peer_list: Vec<_> = peers
        .iter()
        .map(|p| {
            serde_json::json!({
                "name": p.name,
                "status": format!("{:?}", p.status).to_lowercase(),
                "session_count": p.session_count,
            })
        })
        .collect();

    let result = serde_json::json!({
        "summary": summary,
        "sessions": {
            "total": total,
            "by_status": by_status,
            "idle": idle,
        },
        "nodes": {
            "local": {
                "name": local.name,
                "cpus": local.cpus,
                "memory_mb": local.memory_mb,
            },
            "peers": peer_list,
            "available_count": available_count,
        },
    });
    let json = serde_json::to_string_pretty(&result).unwrap_or_default();
    Ok(ReadResourceResult {
        contents: vec![ResourceContents::text(json, uri)],
    })
}

pub async fn read_resource(mcp: &PulpoMcp, uri: &str) -> Result<ReadResourceResult, ErrorData> {
    match uri {
        "pulpo://sessions" => {
            let sessions = mcp
                .session_manager
                .list_sessions()
                .await
                .map_err(|e| ErrorData::internal_error(e.to_string(), None))?;
            let json = serde_json::to_string_pretty(&sessions).unwrap_or_default();
            Ok(ReadResourceResult {
                contents: vec![ResourceContents::text(json, uri)],
            })
        }
        "pulpo://nodes" => {
            let local = mcp.build_node_info();
            let peers = mcp.peer_registry.get_all().await;
            let result = serde_json::json!({
                "local": local,
                "peers": peers,
            });
            let json = serde_json::to_string_pretty(&result).unwrap_or_default();
            Ok(ReadResourceResult {
                contents: vec![ResourceContents::text(json, uri)],
            })
        }
        "pulpo://cluster/status" => read_cluster_status(mcp, uri).await,
        _ if uri.starts_with("pulpo://sessions/") && uri.ends_with("/output") => {
            let id = uri
                .strip_prefix("pulpo://sessions/")
                .and_then(|s| s.strip_suffix("/output"))
                .unwrap_or_default();
            let session = mcp
                .session_manager
                .get_session(id)
                .await
                .map_err(|e| ErrorData::internal_error(e.to_string(), None))?
                .ok_or_else(|| {
                    ErrorData::resource_not_found(format!("session not found: {id}"), None)
                })?;
            let output = mcp.session_manager.capture_output(id, &session.name, 200);
            Ok(ReadResourceResult {
                contents: vec![ResourceContents::text(output, uri)],
            })
        }
        _ => Err(ErrorData::resource_not_found(
            format!("unknown resource: {uri}"),
            None,
        )),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::backend::Backend;
    use crate::config::{AuthConfig, Config, NodeConfig};
    use crate::mcp::SpawnSessionParams;
    use crate::peers::PeerRegistry;
    use crate::session::manager::SessionManager;
    use crate::store::Store;
    use anyhow::Result;
    use rmcp::handler::server::wrapper::Parameters;
    use std::collections::HashMap;
    use std::sync::{Arc, Mutex};

    struct MockBackend {
        alive: Mutex<bool>,
        captured_output: Mutex<String>,
    }

    impl MockBackend {
        fn new() -> Self {
            Self {
                alive: Mutex::new(true),
                captured_output: Mutex::new("output line 1\noutput line 2".into()),
            }
        }
    }

    impl Backend for MockBackend {
        fn create_session(&self, _: &str, _: &str, _: &str) -> Result<()> {
            Ok(())
        }
        fn kill_session(&self, _: &str) -> Result<()> {
            Ok(())
        }
        fn is_alive(&self, _: &str) -> Result<bool> {
            Ok(*self.alive.lock().unwrap())
        }
        fn capture_output(&self, _: &str, _: usize) -> Result<String> {
            Ok(self.captured_output.lock().unwrap().clone())
        }
        fn send_input(&self, _: &str, _: &str) -> Result<()> {
            Ok(())
        }
        fn setup_logging(&self, _: &str, _: &str) -> Result<()> {
            Ok(())
        }
    }

    fn test_config() -> Config {
        Config {
            node: NodeConfig {
                name: "test-node".into(),
                port: 7433,
                data_dir: "/tmp/test".into(),
                ..NodeConfig::default()
            },
            auth: AuthConfig::default(),
            peers: HashMap::new(),
            watchdog: crate::config::WatchdogConfig::default(),
            inks: HashMap::new(),
            notifications: crate::config::NotificationsConfig::default(),
            docker: crate::config::DockerConfig::default(),
        }
    }

    async fn test_mcp() -> PulpoMcp {
        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 backend = Arc::new(MockBackend::new());
        let manager =
            SessionManager::new(backend, store.clone(), HashMap::new(), None).with_no_stale_grace();
        let peer_registry = PeerRegistry::new(&HashMap::new());
        PulpoMcp::new(manager, peer_registry, test_config())
    }

    async fn test_mcp_with_pool() -> (PulpoMcp, sqlx::SqlitePool) {
        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 pool = store.pool().clone();
        let backend = Arc::new(MockBackend::new());
        let manager =
            SessionManager::new(backend, store.clone(), HashMap::new(), None).with_no_stale_grace();
        let peer_registry = PeerRegistry::new(&HashMap::new());
        (PulpoMcp::new(manager, peer_registry, test_config()), pool)
    }

    #[test]
    fn test_list_resources_returns_expected() {
        let result = list_resources();
        assert_eq!(result.resources.len(), 3);
        let uris: Vec<&str> = result.resources.iter().map(|r| r.uri.as_str()).collect();
        assert!(uris.contains(&"pulpo://sessions"));
        assert!(uris.contains(&"pulpo://nodes"));
        assert!(uris.contains(&"pulpo://cluster/status"));
    }

    #[tokio::test]
    async fn test_read_resource_sessions() {
        let mcp = test_mcp().await;
        let result = read_resource(&mcp, "pulpo://sessions").await.unwrap();
        assert_eq!(result.contents.len(), 1);
        // Serialize the content to verify it's valid text
        let json = serde_json::to_string(&result.contents[0]).unwrap();
        assert!(json.contains("text"));
    }

    #[tokio::test]
    async fn test_read_resource_sessions_with_data() {
        let mcp = test_mcp().await;
        // Create a session
        let params = SpawnSessionParams {
            workdir: Some("/tmp".into()),
            name: "test".into(),
            command: Some("echo test".into()),
            description: None,
            ink: None,
            node: None,
        };
        mcp.spawn_session(Parameters(params)).await;

        let result = read_resource(&mcp, "pulpo://sessions").await.unwrap();
        let json = serde_json::to_string(&result.contents[0]).unwrap();
        assert!(json.contains("tmp"));
    }

    #[tokio::test]
    async fn test_read_resource_nodes() {
        let mcp = test_mcp().await;
        let result = read_resource(&mcp, "pulpo://nodes").await.unwrap();
        assert_eq!(result.contents.len(), 1);
        let json = serde_json::to_string(&result.contents[0]).unwrap();
        assert!(json.contains("test-node"));
        assert!(json.contains("peers"));
    }

    #[tokio::test]
    async fn test_read_resource_session_output() {
        let mcp = test_mcp().await;
        // Create a session first
        let params = SpawnSessionParams {
            workdir: Some("/tmp".into()),
            name: "test".into(),
            command: Some("echo test".into()),
            description: None,
            ink: None,
            node: None,
        };
        let spawn_result = mcp.spawn_session(Parameters(params)).await;
        let session: pulpo_common::session::Session = serde_json::from_str(&spawn_result).unwrap();

        let uri = format!("pulpo://sessions/{}/output", session.id);
        let result = read_resource(&mcp, &uri).await.unwrap();
        let json = serde_json::to_string(&result.contents[0]).unwrap();
        assert!(json.contains("output line"));
    }

    #[test]
    fn test_mock_backend_kill_session() {
        let backend = MockBackend::new();
        assert!(backend.kill_session("test-session").is_ok());
    }

    #[tokio::test]
    async fn test_read_resource_session_output_not_found() {
        let mcp = test_mcp().await;
        let result = read_resource(&mcp, "pulpo://sessions/nonexistent/output").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_read_resource_unknown() {
        let mcp = test_mcp().await;
        let result = read_resource(&mcp, "pulpo://unknown").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_read_resource_sessions_store_error() {
        let (mcp, pool) = test_mcp_with_pool().await;
        // Drop the sessions table to trigger a store error in list_sessions
        sqlx::query("DROP TABLE sessions")
            .execute(&pool)
            .await
            .unwrap();
        let result = read_resource(&mcp, "pulpo://sessions").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_read_resource_session_output_store_error() {
        let (mcp, pool) = test_mcp_with_pool().await;
        // Drop the sessions table to trigger a store error in get_session
        sqlx::query("DROP TABLE sessions")
            .execute(&pool)
            .await
            .unwrap();
        let result = read_resource(&mcp, "pulpo://sessions/some-id/output").await;
        assert!(result.is_err());
    }

    #[test]
    fn test_mock_backend_methods() {
        let b = MockBackend::new();
        assert!(b.create_session("n", "d", "c").is_ok());
        assert!(b.kill_session("n").is_ok());
        assert!(b.is_alive("n").unwrap());
        assert!(b.capture_output("n", 10).unwrap().contains("output line"));
        assert!(b.send_input("n", "t").is_ok());
        assert!(b.setup_logging("n", "p").is_ok());
    }

    // -- cluster/status resource tests --

    #[tokio::test]
    async fn test_read_resource_cluster_status_empty() {
        let mcp = test_mcp().await;
        let result = read_resource(&mcp, "pulpo://cluster/status").await.unwrap();
        assert_eq!(result.contents.len(), 1);
        let json = serde_json::to_string(&result.contents[0]).unwrap();
        assert!(json.contains("0 sessions"));
        assert!(json.contains("total"));
        assert!(json.contains("test-node"));
    }

    #[tokio::test]
    async fn test_read_resource_cluster_status_with_sessions() {
        let mcp = test_mcp().await;
        // Create a session
        let params = SpawnSessionParams {
            workdir: Some("/tmp".into()),
            name: "test".into(),
            command: Some("echo test".into()),
            description: None,
            ink: None,
            node: None,
        };
        mcp.spawn_session(Parameters(params)).await;

        let result = read_resource(&mcp, "pulpo://cluster/status").await.unwrap();
        let json = serde_json::to_string(&result.contents[0]).unwrap();
        assert!(json.contains("1 sessions"));
        assert!(json.contains("active"));
    }

    #[tokio::test]
    async fn test_read_resource_cluster_status_with_idle_session() {
        let (mcp, pool) = test_mcp_with_pool().await;
        // Create a session
        let params = SpawnSessionParams {
            workdir: Some("/tmp".into()),
            name: "test".into(),
            command: Some("echo test".into()),
            description: None,
            ink: None,
            node: None,
        };
        let spawn_result = mcp.spawn_session(Parameters(params)).await;
        let session: pulpo_common::session::Session = serde_json::from_str(&spawn_result).unwrap();

        // Set idle_since to 15 minutes ago
        let idle_since = (chrono::Utc::now() - chrono::Duration::minutes(15)).to_rfc3339();
        sqlx::query("UPDATE sessions SET idle_since = ? WHERE id = ?")
            .bind(&idle_since)
            .bind(session.id.to_string())
            .execute(&pool)
            .await
            .unwrap();

        let result = read_resource(&mcp, "pulpo://cluster/status").await.unwrap();
        let json = serde_json::to_string(&result.contents[0]).unwrap();
        assert!(json.contains("idle"));
        assert!(json.contains("idle_minutes"));
    }

    #[tokio::test]
    async fn test_read_resource_cluster_status_store_error() {
        let (mcp, pool) = test_mcp_with_pool().await;
        // Drop the sessions table to trigger a store error
        sqlx::query("DROP TABLE sessions")
            .execute(&pool)
            .await
            .unwrap();
        let result = read_resource(&mcp, "pulpo://cluster/status").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_read_resource_cluster_status_nodes_info() {
        let mcp = test_mcp().await;
        let result = read_resource(&mcp, "pulpo://cluster/status").await.unwrap();
        let json = serde_json::to_string(&result.contents[0]).unwrap();
        // Should have nodes section with local info
        assert!(json.contains("available_count"));
        assert!(json.contains("test-node"));
    }

    #[tokio::test]
    async fn test_read_resource_cluster_status_with_peers() {
        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 backend = Arc::new(MockBackend::new());
        let manager =
            SessionManager::new(backend, store.clone(), HashMap::new(), None).with_no_stale_grace();
        let mut peers = HashMap::new();
        peers.insert(
            "remote".into(),
            pulpo_common::peer::PeerEntry::Simple("10.0.0.1:7433".into()),
        );
        let peer_registry = PeerRegistry::new(&peers);
        let mcp = PulpoMcp::new(manager, peer_registry, test_config());

        let result = read_resource(&mcp, "pulpo://cluster/status").await.unwrap();
        let json = serde_json::to_string(&result.contents[0]).unwrap();
        assert!(json.contains("remote"));
        assert!(json.contains("session_count"));
    }
}