scud-cli 1.67.0

Fast, simple task master for AI-driven development
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
//! JSON RPC server for IPC communication
//!
//! Handles stdin/stdout communication with JSON RPC protocol.

use anyhow::Result;
use serde_json::Value;
use std::collections::HashMap;
use std::io::{self, BufRead, Write};
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio::sync::Mutex;

use crate::commands::spawn::terminal::Harness;
use crate::extensions::runner::{spawn_agent, AgentEvent, SpawnConfig};

use super::types::*;

/// RPC Server configuration
#[derive(Debug, Clone)]
pub struct RpcServerConfig {
    /// Default working directory for agents
    pub working_dir: PathBuf,
    /// Default harness to use
    pub default_harness: Harness,
    /// Default model to use
    pub default_model: Option<String>,
    /// Project root for task storage access
    pub project_root: Option<PathBuf>,
}

impl Default for RpcServerConfig {
    fn default() -> Self {
        Self {
            working_dir: std::env::current_dir().unwrap_or_default(),
            default_harness: Harness::default(),
            default_model: None,
            project_root: None,
        }
    }
}

/// JSON RPC Server for subagent communication
pub struct RpcServer {
    config: RpcServerConfig,
    /// Active agent task handles
    active_agents: Arc<Mutex<HashMap<String, tokio::task::JoinHandle<()>>>>,
    /// Event channel for agent events
    event_tx: mpsc::Sender<AgentEvent>,
    event_rx: mpsc::Receiver<AgentEvent>,
}

impl RpcServer {
    /// Create a new RPC server with the given configuration
    pub fn new(config: RpcServerConfig) -> Self {
        let (event_tx, event_rx) = mpsc::channel(1000);
        Self {
            config,
            active_agents: Arc::new(Mutex::new(HashMap::new())),
            event_tx,
            event_rx,
        }
    }

    /// Run the RPC server loop
    ///
    /// Reads JSON RPC requests from stdin and writes responses/events to stdout.
    /// This function blocks until a shutdown request is received or stdin closes.
    pub async fn run(&mut self) -> Result<()> {
        // Emit server ready notification
        self.emit_notification(RpcNotification::server_ready(env!("CARGO_PKG_VERSION")))?;

        let stdin = io::stdin();
        let reader = stdin.lock();

        // Spawn a task to forward agent events to stdout
        let event_rx = std::mem::replace(&mut self.event_rx, mpsc::channel(1).1);
        let event_forwarder = tokio::spawn(Self::forward_events(event_rx));

        // Read requests from stdin line by line
        for line in reader.lines() {
            let line = match line {
                Ok(l) => l,
                Err(e) => {
                    eprintln!("Error reading stdin: {}", e);
                    break;
                }
            };

            // Skip empty lines
            if line.trim().is_empty() {
                continue;
            }

            // Parse the request
            let request: RpcRequest = match serde_json::from_str(&line) {
                Ok(req) => req,
                Err(e) => {
                    // Try to extract ID for error response
                    let id = Self::extract_id(&line).unwrap_or(RpcId::Null);
                    self.emit_response(RpcResponse::error(
                        id,
                        RpcError::parse_error(&e.to_string()),
                    ))?;
                    continue;
                }
            };

            // Validate JSON RPC version
            if request.jsonrpc != JSONRPC_VERSION {
                if let Some(id) = request.id.clone() {
                    self.emit_response(RpcResponse::error(
                        id,
                        RpcError::invalid_request("Invalid JSON-RPC version"),
                    ))?;
                }
                continue;
            }

            // Handle the request
            let should_shutdown = self.handle_request(request).await?;
            if should_shutdown {
                break;
            }
        }

        // Emit shutdown notification
        self.emit_notification(RpcNotification::server_shutdown())?;

        // Cancel the event forwarder
        event_forwarder.abort();

        Ok(())
    }

    /// Handle a single RPC request
    async fn handle_request(&mut self, request: RpcRequest) -> Result<bool> {
        let id = request.id.clone();
        let method = request.method.as_str();

        match method {
            "ping" => {
                if let Some(id) = id {
                    self.emit_response(RpcResponse::success(
                        id,
                        serde_json::json!({"pong": true}),
                    ))?;
                }
            }

            "shutdown" => {
                if let Some(id) = id {
                    self.emit_response(RpcResponse::success(
                        id,
                        serde_json::json!({"status": "shutting_down"}),
                    ))?;
                }
                return Ok(true);
            }

            "spawn" => {
                let result = self.handle_spawn(request.params).await;
                if let Some(id) = id {
                    match result {
                        Ok(value) => self.emit_response(RpcResponse::success(id, value))?,
                        Err(e) => self.emit_response(RpcResponse::error(id, e))?,
                    }
                }
            }

            "spawn_task" => {
                let result = self.handle_spawn_task(request.params).await;
                if let Some(id) = id {
                    match result {
                        Ok(value) => self.emit_response(RpcResponse::success(id, value))?,
                        Err(e) => self.emit_response(RpcResponse::error(id, e))?,
                    }
                }
            }

            "cancel" => {
                let result = self.handle_cancel(request.params).await;
                if let Some(id) = id {
                    match result {
                        Ok(value) => self.emit_response(RpcResponse::success(id, value))?,
                        Err(e) => self.emit_response(RpcResponse::error(id, e))?,
                    }
                }
            }

            "list_agents" => {
                let result = self.handle_list_agents().await;
                if let Some(id) = id {
                    self.emit_response(RpcResponse::success(id, result))?;
                }
            }

            "list_tasks" => {
                let result = self.handle_list_tasks(request.params).await;
                if let Some(id) = id {
                    match result {
                        Ok(value) => self.emit_response(RpcResponse::success(id, value))?,
                        Err(e) => self.emit_response(RpcResponse::error(id, e))?,
                    }
                }
            }

            "get_task" => {
                let result = self.handle_get_task(request.params).await;
                if let Some(id) = id {
                    match result {
                        Ok(value) => self.emit_response(RpcResponse::success(id, value))?,
                        Err(e) => self.emit_response(RpcResponse::error(id, e))?,
                    }
                }
            }

            "set_status" => {
                let result = self.handle_set_status(request.params).await;
                if let Some(id) = id {
                    match result {
                        Ok(value) => self.emit_response(RpcResponse::success(id, value))?,
                        Err(e) => self.emit_response(RpcResponse::error(id, e))?,
                    }
                }
            }

            "next_task" => {
                let result = self.handle_next_task(request.params).await;
                if let Some(id) = id {
                    match result {
                        Ok(value) => self.emit_response(RpcResponse::success(id, value))?,
                        Err(e) => self.emit_response(RpcResponse::error(id, e))?,
                    }
                }
            }

            _ => {
                if let Some(id) = id {
                    self.emit_response(RpcResponse::error(id, RpcError::method_not_found(method)))?;
                }
            }
        }

        Ok(false)
    }

    /// Handle "spawn" method - spawn agent with custom prompt
    async fn handle_spawn(&self, params: Value) -> Result<Value, RpcError> {
        let params: SpawnParams =
            serde_json::from_value(params).map_err(|e| RpcError::invalid_params(&e.to_string()))?;

        // Parse harness
        let harness = if let Some(h) = params.harness {
            Harness::parse(&h).map_err(|e| RpcError::invalid_params(&e.to_string()))?
        } else {
            self.config.default_harness
        };

        // Determine working directory
        let working_dir = params
            .working_dir
            .map(PathBuf::from)
            .unwrap_or_else(|| self.config.working_dir.clone());

        let config = SpawnConfig {
            task_id: params.task_id.clone(),
            prompt: params.prompt,
            working_dir,
            harness,
            model: params.model.or_else(|| self.config.default_model.clone()),
        };

        // Spawn the agent
        let event_tx = self.event_tx.clone();
        let task_id = params.task_id.clone();

        match spawn_agent(config, event_tx).await {
            Ok(handle) => {
                // Track the agent handle
                let mut agents = self.active_agents.lock().await;
                agents.insert(
                    task_id.clone(),
                    tokio::spawn(async move {
                        let _ = handle.await;
                    }),
                );

                Ok(serde_json::json!({
                    "status": "spawned",
                    "task_id": task_id
                }))
            }
            Err(e) => Err(RpcError::spawn_failed(&e.to_string())),
        }
    }

    /// Handle "spawn_task" method - spawn agent for a task in the graph
    async fn handle_spawn_task(&self, params: Value) -> Result<Value, RpcError> {
        let params: SpawnTaskParams =
            serde_json::from_value(params).map_err(|e| RpcError::invalid_params(&e.to_string()))?;

        // Load task from storage
        let storage = crate::storage::Storage::new(self.config.project_root.clone());

        // Get the tag
        let tag = params
            .tag
            .or_else(|| storage.get_active_group().ok().flatten());
        let tag =
            tag.ok_or_else(|| RpcError::invalid_params("No tag specified and no active tag"))?;

        // Load the task group
        let group = storage
            .load_group(&tag)
            .map_err(|e: anyhow::Error| RpcError::task_not_found(&e.to_string()))?;

        // Find the task
        let task = group
            .tasks
            .iter()
            .find(|t| t.id == params.task_id)
            .ok_or_else(|| RpcError::task_not_found(&params.task_id))?;

        // Generate prompt for the task
        let prompt = crate::commands::spawn::agent::generate_prompt(task, &tag);

        // Parse harness (prefer params, then task's agent_type, then default)
        let harness = if let Some(h) = params.harness {
            Harness::parse(&h).map_err(|e| RpcError::invalid_params(&e.to_string()))?
        } else if let Some(agent_type) = &task.agent_type {
            // Try to load from agent definition
            if let Some(agent_def) =
                crate::agents::AgentDef::try_load(agent_type, &self.config.working_dir)
            {
                agent_def.harness().unwrap_or(self.config.default_harness)
            } else {
                self.config.default_harness
            }
        } else {
            self.config.default_harness
        };

        // Get model
        let model = params.model.or_else(|| {
            if let Some(agent_type) = &task.agent_type {
                if let Some(agent_def) =
                    crate::agents::AgentDef::try_load(agent_type, &self.config.working_dir)
                {
                    return agent_def.model().map(String::from);
                }
            }
            self.config.default_model.clone()
        });

        let config = SpawnConfig {
            task_id: params.task_id.clone(),
            prompt,
            working_dir: self.config.working_dir.clone(),
            harness,
            model,
        };

        // Spawn the agent
        let event_tx = self.event_tx.clone();
        let task_id = params.task_id.clone();

        match spawn_agent(config, event_tx).await {
            Ok(handle) => {
                // Track the agent handle
                let mut agents = self.active_agents.lock().await;
                agents.insert(
                    task_id.clone(),
                    tokio::spawn(async move {
                        let _ = handle.await;
                    }),
                );

                Ok(serde_json::json!({
                    "status": "spawned",
                    "task_id": task_id,
                    "tag": tag
                }))
            }
            Err(e) => Err(RpcError::spawn_failed(&e.to_string())),
        }
    }

    /// Handle "cancel" method - cancel a running agent
    async fn handle_cancel(&self, params: Value) -> Result<Value, RpcError> {
        let task_id = params
            .get("task_id")
            .and_then(|v| v.as_str())
            .ok_or_else(|| RpcError::invalid_params("Missing task_id"))?;

        let mut agents = self.active_agents.lock().await;

        if let Some(handle) = agents.remove(task_id) {
            handle.abort();
            Ok(serde_json::json!({
                "status": "cancelled",
                "task_id": task_id
            }))
        } else {
            Err(RpcError::task_not_found(task_id))
        }
    }

    /// Handle "list_agents" method - list currently active agents
    async fn handle_list_agents(&self) -> Value {
        let agents = self.active_agents.lock().await;
        let agent_ids: Vec<&String> = agents.keys().collect();
        serde_json::json!({
            "agents": agent_ids,
            "count": agent_ids.len()
        })
    }

    /// Handle "list_tasks" method - list tasks from storage
    async fn handle_list_tasks(&self, params: Value) -> Result<Value, RpcError> {
        let params: ListTasksParams = serde_json::from_value(params).unwrap_or_default();

        let storage = crate::storage::Storage::new(self.config.project_root.clone());

        let tag = params
            .tag
            .or_else(|| storage.get_active_group().ok().flatten());
        let tag =
            tag.ok_or_else(|| RpcError::invalid_params("No tag specified and no active tag"))?;

        let group = storage
            .load_group(&tag)
            .map_err(|e: anyhow::Error| RpcError::internal_error(&e.to_string()))?;

        let tasks: Vec<Value> = group
            .tasks
            .iter()
            .filter(|t| {
                params
                    .status
                    .as_ref()
                    .map(|s| t.status.as_str().to_lowercase() == s.to_lowercase())
                    .unwrap_or(true)
            })
            .map(|t| {
                serde_json::json!({
                    "id": t.id,
                    "title": t.title,
                    "status": t.status.as_str(),
                    "complexity": t.complexity,
                    "dependencies": t.dependencies
                })
            })
            .collect();

        Ok(serde_json::json!({
            "tag": tag,
            "tasks": tasks,
            "count": tasks.len()
        }))
    }

    /// Handle "get_task" method - get a single task
    async fn handle_get_task(&self, params: Value) -> Result<Value, RpcError> {
        let params: GetTaskParams =
            serde_json::from_value(params).map_err(|e| RpcError::invalid_params(&e.to_string()))?;

        let storage = crate::storage::Storage::new(self.config.project_root.clone());

        let tag = params
            .tag
            .or_else(|| storage.get_active_group().ok().flatten());
        let tag =
            tag.ok_or_else(|| RpcError::invalid_params("No tag specified and no active tag"))?;

        let group = storage
            .load_group(&tag)
            .map_err(|e: anyhow::Error| RpcError::internal_error(&e.to_string()))?;

        let task = group
            .tasks
            .iter()
            .find(|t| t.id == params.task_id)
            .ok_or_else(|| RpcError::task_not_found(&params.task_id))?;

        Ok(serde_json::json!({
            "id": task.id,
            "title": task.title,
            "description": task.description,
            "status": task.status.as_str(),
            "complexity": task.complexity,
            "priority": format!("{:?}", task.priority).to_lowercase(),
            "dependencies": task.dependencies,
            "agent_type": task.agent_type,
            "assigned_to": task.assigned_to
        }))
    }

    /// Handle "set_status" method - update task status
    async fn handle_set_status(&self, params: Value) -> Result<Value, RpcError> {
        let params: SetStatusParams =
            serde_json::from_value(params).map_err(|e| RpcError::invalid_params(&e.to_string()))?;

        let storage = crate::storage::Storage::new(self.config.project_root.clone());

        let tag = params
            .tag
            .or_else(|| storage.get_active_group().ok().flatten());
        let tag =
            tag.ok_or_else(|| RpcError::invalid_params("No tag specified and no active tag"))?;

        let mut group = storage
            .load_group(&tag)
            .map_err(|e: anyhow::Error| RpcError::internal_error(&e.to_string()))?;

        // Find and update the task
        let task = group
            .tasks
            .iter_mut()
            .find(|t| t.id == params.task_id)
            .ok_or_else(|| RpcError::task_not_found(&params.task_id))?;

        // Parse status
        let new_status =
            crate::models::task::TaskStatus::from_str(&params.status).ok_or_else(|| {
                RpcError::invalid_params(&format!("Invalid status: {}", params.status))
            })?;

        let old_status = task.status.as_str().to_string();
        task.status = new_status;

        // Save the group
        storage
            .update_group(&tag, &group)
            .map_err(|e: anyhow::Error| RpcError::internal_error(&e.to_string()))?;

        Ok(serde_json::json!({
            "task_id": params.task_id,
            "old_status": old_status,
            "new_status": params.status
        }))
    }

    /// Handle "next_task" method - get next available task
    async fn handle_next_task(&self, params: Value) -> Result<Value, RpcError> {
        let params: NextTaskParams = serde_json::from_value(params).unwrap_or_default();

        let storage = crate::storage::Storage::new(self.config.project_root.clone());

        // Use next command logic
        let result = crate::commands::helpers::find_next_task(
            &storage,
            params.tag.as_deref(),
            params.all_tags,
        );

        match result {
            Some((task, tag)) => Ok(serde_json::json!({
                "task_id": task.id,
                "title": task.title,
                "tag": tag,
                "complexity": task.complexity
            })),
            None => Ok(serde_json::json!({
                "task_id": null,
                "message": "No tasks available"
            })),
        }
    }

    /// Emit a JSON RPC response to stdout
    fn emit_response(&self, response: RpcResponse) -> Result<()> {
        let json = serde_json::to_string(&response)?;
        let mut stdout = io::stdout().lock();
        writeln!(stdout, "{}", json)?;
        stdout.flush()?;
        Ok(())
    }

    /// Emit a JSON RPC notification to stdout
    fn emit_notification(&self, notification: RpcNotification) -> Result<()> {
        let json = serde_json::to_string(&notification)?;
        let mut stdout = io::stdout().lock();
        writeln!(stdout, "{}", json)?;
        stdout.flush()?;
        Ok(())
    }

    /// Try to extract ID from malformed JSON for error responses
    fn extract_id(json_str: &str) -> Option<RpcId> {
        // Simple heuristic: try to parse just enough to get the id
        let parsed: Result<Value, _> = serde_json::from_str(json_str);
        if let Ok(v) = parsed {
            if let Some(id) = v.get("id") {
                if let Some(n) = id.as_i64() {
                    return Some(RpcId::Number(n));
                }
                if let Some(s) = id.as_str() {
                    return Some(RpcId::String(s.to_string()));
                }
            }
        }
        None
    }

    /// Forward agent events to stdout as JSON RPC notifications
    async fn forward_events(mut event_rx: mpsc::Receiver<AgentEvent>) {
        while let Some(event) = event_rx.recv().await {
            let notification = match event {
                AgentEvent::Started { task_id } => RpcNotification::agent_started(&task_id),
                AgentEvent::Output { task_id, line } => {
                    RpcNotification::agent_output(&task_id, &line)
                }
                AgentEvent::Completed { result } => RpcNotification::agent_completed(
                    &result.task_id,
                    result.success,
                    result.exit_code,
                    result.duration_ms,
                ),
                AgentEvent::SpawnFailed { task_id, error } => {
                    RpcNotification::agent_spawn_failed(&task_id, &error)
                }
            };

            // Write to stdout
            if let Ok(json) = serde_json::to_string(&notification) {
                let mut stdout = io::stdout().lock();
                let _ = writeln!(stdout, "{}", json);
                let _ = stdout.flush();
            }
        }
    }
}

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

    #[test]
    fn test_server_config_default() {
        let config = RpcServerConfig::default();
        assert_eq!(config.default_harness, Harness::default());
    }

    #[test]
    fn test_extract_id_number() {
        let id = RpcServer::extract_id(r#"{"id": 42, "invalid": true}"#);
        assert_eq!(id, Some(RpcId::Number(42)));
    }

    #[test]
    fn test_extract_id_string() {
        let id = RpcServer::extract_id(r#"{"id": "abc-123"}"#);
        assert_eq!(id, Some(RpcId::String("abc-123".to_string())));
    }
}