ccswarm 0.9.1

AI Agent Workflow DevOps toolchain complementing Claude Code Agent Teams
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
use super::super::*;

impl CliRunner {
    pub(crate) async fn handle_sangha(&self, action: &SanghaAction) -> Result<()> {
        let proposals_dir = self.repo_path.join("coordination/proposals");
        tokio::fs::create_dir_all(&proposals_dir).await?;

        match action {
            SanghaAction::Propose {
                title,
                description,
                proposal_type,
            } => {
                let proposal =
                    create_sangha_proposal(&proposals_dir, title, description, proposal_type, None)
                        .await?;
                let id = proposal
                    .get("id")
                    .and_then(|value| value.as_str())
                    .unwrap_or("?");

                if self.json_output {
                    println!(
                        "{}",
                        serde_json::to_string_pretty(&serde_json::json!({
                            "status": "success",
                            "message": "Proposal created",
                            "data": proposal,
                        }))?
                    );
                } else {
                    println!(
                        "{} Proposal created: {}",
                        "OK".bright_green().bold(),
                        id.bright_cyan()
                    );
                    println!("  Title: {}", title);
                    println!("  Type:  {}", proposal_type);
                    println!(
                        "  Vote:  ccswarm sangha vote {} --approve",
                        id.bright_yellow()
                    );
                }
            }
            SanghaAction::Vote {
                id,
                approve,
                reason,
            } => {
                let filepath = coordination_json_path(&proposals_dir, id, "Proposal")?;
                if !filepath.exists() {
                    anyhow::bail!("Proposal '{}' not found", id);
                }

                let content = tokio::fs::read_to_string(&filepath).await?;
                let mut proposal: serde_json::Value = serde_json::from_str(&content)?;

                let vote = serde_json::json!({
                    "approve": approve,
                    "reason": reason,
                    "voted_at": chrono::Utc::now().to_rfc3339(),
                });

                if let Some(votes) = proposal.get_mut("votes").and_then(|v| v.as_array_mut()) {
                    votes.push(vote);
                }

                let updated = serde_json::to_string_pretty(&proposal)?;
                tokio::fs::write(&filepath, updated).await?;

                let action_str = if *approve { "approved" } else { "rejected" };
                if self.json_output {
                    println!(
                        "{}",
                        serde_json::to_string_pretty(&serde_json::json!({
                            "status": "success",
                            "message": format!("Vote recorded: {}", action_str),
                            "data": { "id": id, "vote": action_str },
                        }))?
                    );
                } else {
                    println!(
                        "{} Vote recorded on {}: {}",
                        "OK".bright_green().bold(),
                        id.bright_cyan(),
                        action_str.bright_yellow()
                    );
                }
            }
            SanghaAction::List { status } => {
                let mut entries = tokio::fs::read_dir(&proposals_dir).await?;
                let mut proposals: Vec<serde_json::Value> = Vec::new();

                while let Some(entry) = entries.next_entry().await? {
                    let path = entry.path();
                    if path.extension().is_some_and(|e| e == "json")
                        && let Ok(content) = tokio::fs::read_to_string(&path).await
                        && let Ok(p) = serde_json::from_str::<serde_json::Value>(&content)
                    {
                        if let Some(filter) = status {
                            if p.get("status")
                                .and_then(|s| s.as_str())
                                .is_some_and(|s| s == filter)
                            {
                                proposals.push(p);
                            }
                        } else {
                            proposals.push(p);
                        }
                    }
                }

                if self.json_output {
                    println!(
                        "{}",
                        serde_json::to_string_pretty(&serde_json::json!({
                            "status": "success",
                            "data": proposals,
                        }))?
                    );
                } else if proposals.is_empty() {
                    println!("No proposals found.");
                } else {
                    println!("{}", "Proposals".bright_cyan().bold());
                    println!("{}", "=========".bright_cyan());
                    for p in &proposals {
                        let id = p.get("id").and_then(|v| v.as_str()).unwrap_or("?");
                        let title = p.get("title").and_then(|v| v.as_str()).unwrap_or("?");
                        let st = p.get("status").and_then(|v| v.as_str()).unwrap_or("?");
                        let votes = p
                            .get("votes")
                            .and_then(|v| v.as_array())
                            .map(|a| a.len())
                            .unwrap_or(0);
                        println!(
                            "  {} [{}] {} ({} votes)",
                            id.bright_yellow(),
                            st.bright_white(),
                            title,
                            votes
                        );
                    }
                    println!("\nTotal: {} proposals", proposals.len());
                }
            }
            SanghaAction::Status { id } => {
                let filepath = coordination_json_path(&proposals_dir, id, "Proposal")?;
                if !filepath.exists() {
                    anyhow::bail!("Proposal '{}' not found", id);
                }

                let content = tokio::fs::read_to_string(&filepath).await?;
                let proposal: serde_json::Value = serde_json::from_str(&content)?;

                if self.json_output {
                    println!(
                        "{}",
                        serde_json::to_string_pretty(&serde_json::json!({
                            "status": "success",
                            "data": proposal,
                        }))?
                    );
                } else {
                    let title = proposal
                        .get("title")
                        .and_then(|v| v.as_str())
                        .unwrap_or("?");
                    let desc = proposal
                        .get("description")
                        .and_then(|v| v.as_str())
                        .unwrap_or("?");
                    let st = proposal
                        .get("status")
                        .and_then(|v| v.as_str())
                        .unwrap_or("?");
                    let ptype = proposal
                        .get("proposal_type")
                        .and_then(|v| v.as_str())
                        .unwrap_or("?");

                    println!(
                        "{} {}",
                        "Proposal:".bright_cyan().bold(),
                        id.bright_yellow()
                    );
                    println!("  Title:       {}", title);
                    println!("  Description: {}", desc);
                    println!("  Type:        {}", ptype);
                    println!("  Status:      {}", st);

                    if let Some(votes) = proposal.get("votes").and_then(|v| v.as_array()) {
                        let approve_count = votes
                            .iter()
                            .filter(|v| v.get("approve").and_then(|a| a.as_bool()).unwrap_or(false))
                            .count();
                        let reject_count = votes.len() - approve_count;
                        println!(
                            "  Votes:       {} approve, {} reject",
                            approve_count.to_string().bright_green(),
                            reject_count.to_string().bright_red()
                        );
                    }
                }
            }
        }

        Ok(())
    }

    pub(crate) async fn handle_extend(&self, action: &ExtendAction) -> Result<()> {
        let extensions_dir = self.repo_path.join("coordination/extensions");
        let proposals_dir = self.repo_path.join("coordination/proposals");
        tokio::fs::create_dir_all(&extensions_dir).await?;
        tokio::fs::create_dir_all(&proposals_dir).await?;

        match action {
            ExtendAction::Propose {
                title,
                description,
                agent,
                auto_sangha,
            } => {
                let id = format!("ext-{}", &uuid::Uuid::new_v4().to_string()[..8]);
                let mut extension = serde_json::json!({
                    "id": id,
                    "title": title,
                    "description": description,
                    "agent": agent,
                    "status": "proposed",
                    "created_at": chrono::Utc::now().to_rfc3339(),
                });

                let sangha_proposal_id = if *auto_sangha {
                    let proposal = create_sangha_proposal(
                        &proposals_dir,
                        title,
                        description,
                        "extension",
                        Some(&id),
                    )
                    .await?;
                    proposal
                        .get("id")
                        .and_then(|value| value.as_str())
                        .map(str::to_string)
                } else {
                    None
                };
                if let Some(proposal_id) = &sangha_proposal_id {
                    extension["sangha_proposal_id"] = serde_json::json!(proposal_id);
                    extension["status"] = serde_json::json!("pending_consensus");
                }

                let filepath = extensions_dir.join(format!("{}.json", id));
                let content = serde_json::to_string_pretty(&extension)?;
                tokio::fs::write(&filepath, content).await?;

                if self.json_output {
                    println!(
                        "{}",
                        serde_json::to_string_pretty(&serde_json::json!({
                            "status": "success",
                            "message": "Extension proposed",
                            "data": extension,
                        }))?
                    );
                } else {
                    println!(
                        "{} Extension proposed: {}",
                        "OK".bright_green().bold(),
                        id.bright_cyan()
                    );
                    println!("  Title: {}", title);
                    println!("  Agent: {}", agent);
                    if let Some(proposal_id) = sangha_proposal_id {
                        println!(
                            "  Sangha: ccswarm lab sangha vote {} --approve",
                            proposal_id.bright_yellow()
                        );
                    }
                }
            }
            ExtendAction::AutoPropose {
                agent,
                reason,
                auto_sangha,
            } => {
                let title = format!("Extend {} agent capabilities", agent);
                let description = reason.clone().unwrap_or_else(|| {
                    "Automatically proposed from local workflow context: add or update agent facets, validation commands, and operating guidance where repeated work would benefit from specialized capability.".to_string()
                });
                let id = format!("ext-{}", &uuid::Uuid::new_v4().to_string()[..8]);
                let mut extension = serde_json::json!({
                    "id": id,
                    "title": title,
                    "description": description,
                    "agent": agent,
                    "status": "proposed",
                    "source": "auto",
                    "created_at": chrono::Utc::now().to_rfc3339(),
                });

                let sangha_proposal_id = if *auto_sangha {
                    let proposal = create_sangha_proposal(
                        &proposals_dir,
                        &title,
                        &description,
                        "extension",
                        Some(&id),
                    )
                    .await?;
                    proposal
                        .get("id")
                        .and_then(|value| value.as_str())
                        .map(str::to_string)
                } else {
                    None
                };
                if let Some(proposal_id) = &sangha_proposal_id {
                    extension["sangha_proposal_id"] = serde_json::json!(proposal_id);
                    extension["status"] = serde_json::json!("pending_consensus");
                }

                let filepath = extensions_dir.join(format!("{}.json", id));
                let content = serde_json::to_string_pretty(&extension)?;
                tokio::fs::write(&filepath, content).await?;

                if self.json_output {
                    println!(
                        "{}",
                        serde_json::to_string_pretty(&serde_json::json!({
                            "status": "success",
                            "message": "Extension auto-proposed",
                            "data": extension,
                        }))?
                    );
                } else {
                    println!(
                        "{} Extension auto-proposed: {}",
                        "OK".bright_green().bold(),
                        id.bright_cyan()
                    );
                    println!("  Title: {}", title);
                    println!("  Agent: {}", agent);
                    if let Some(proposal_id) = sangha_proposal_id {
                        println!(
                            "  Sangha: ccswarm lab sangha vote {} --approve",
                            proposal_id.bright_yellow()
                        );
                    }
                }
            }
            ExtendAction::List { status } => {
                let mut entries = tokio::fs::read_dir(&extensions_dir).await?;
                let mut extensions: Vec<serde_json::Value> = Vec::new();

                while let Some(entry) = entries.next_entry().await? {
                    let path = entry.path();
                    if path.extension().is_some_and(|e| e == "json")
                        && let Ok(content) = tokio::fs::read_to_string(&path).await
                        && let Ok(ext) = serde_json::from_str::<serde_json::Value>(&content)
                    {
                        if let Some(filter) = status {
                            if ext
                                .get("status")
                                .and_then(|s| s.as_str())
                                .is_some_and(|s| s == filter)
                            {
                                extensions.push(ext);
                            }
                        } else {
                            extensions.push(ext);
                        }
                    }
                }

                if self.json_output {
                    println!(
                        "{}",
                        serde_json::to_string_pretty(&serde_json::json!({
                            "status": "success",
                            "data": extensions,
                        }))?
                    );
                } else if extensions.is_empty() {
                    println!("No extensions found.");
                } else {
                    println!("{}", "Extensions".bright_cyan().bold());
                    println!("{}", "==========".bright_cyan());
                    for ext in &extensions {
                        let id = ext.get("id").and_then(|v| v.as_str()).unwrap_or("?");
                        let title = ext.get("title").and_then(|v| v.as_str()).unwrap_or("?");
                        let st = ext.get("status").and_then(|v| v.as_str()).unwrap_or("?");
                        let agent = ext.get("agent").and_then(|v| v.as_str()).unwrap_or("?");
                        println!(
                            "  {} [{}] {} (agent: {})",
                            id.bright_yellow(),
                            st.bright_white(),
                            title,
                            agent.bright_cyan()
                        );
                    }
                    println!("\nTotal: {} extensions", extensions.len());
                }
            }
            ExtendAction::Status { id } => {
                let filepath = coordination_json_path(&extensions_dir, id, "Extension")?;
                if !filepath.exists() {
                    anyhow::bail!("Extension '{}' not found", id);
                }

                let content = tokio::fs::read_to_string(&filepath).await?;
                let extension: serde_json::Value = serde_json::from_str(&content)?;

                if self.json_output {
                    println!(
                        "{}",
                        serde_json::to_string_pretty(&serde_json::json!({
                            "status": "success",
                            "data": extension,
                        }))?
                    );
                } else {
                    let title = extension
                        .get("title")
                        .and_then(|v| v.as_str())
                        .unwrap_or("?");
                    let desc = extension
                        .get("description")
                        .and_then(|v| v.as_str())
                        .unwrap_or("?");
                    let st = extension
                        .get("status")
                        .and_then(|v| v.as_str())
                        .unwrap_or("?");
                    let agent = extension
                        .get("agent")
                        .and_then(|v| v.as_str())
                        .unwrap_or("?");

                    println!(
                        "{} {}",
                        "Extension:".bright_cyan().bold(),
                        id.bright_yellow()
                    );
                    println!("  Title:       {}", title);
                    println!("  Description: {}", desc);
                    println!("  Agent:       {}", agent);
                    println!("  Status:      {}", st);
                }
            }
            ExtendAction::History { limit } => {
                let mut entries = tokio::fs::read_dir(&extensions_dir).await?;
                let mut extensions: Vec<serde_json::Value> = Vec::new();

                while let Some(entry) = entries.next_entry().await? {
                    let path = entry.path();
                    if path.extension().is_some_and(|e| e == "json")
                        && let Ok(content) = tokio::fs::read_to_string(&path).await
                        && let Ok(ext) = serde_json::from_str::<serde_json::Value>(&content)
                    {
                        extensions.push(ext);
                    }
                }

                // Sort by created_at descending
                extensions.sort_by(|a, b| {
                    let a_time = a.get("created_at").and_then(|v| v.as_str()).unwrap_or("");
                    let b_time = b.get("created_at").and_then(|v| v.as_str()).unwrap_or("");
                    b_time.cmp(a_time)
                });

                extensions.truncate(*limit);

                if self.json_output {
                    println!(
                        "{}",
                        serde_json::to_string_pretty(&serde_json::json!({
                            "status": "success",
                            "data": extensions,
                        }))?
                    );
                } else if extensions.is_empty() {
                    println!("No extension history.");
                } else {
                    println!(
                        "{} (last {})",
                        "Extension History".bright_cyan().bold(),
                        limit
                    );
                    println!("{}", "=================".bright_cyan());
                    for ext in &extensions {
                        let id = ext.get("id").and_then(|v| v.as_str()).unwrap_or("?");
                        let title = ext.get("title").and_then(|v| v.as_str()).unwrap_or("?");
                        let st = ext.get("status").and_then(|v| v.as_str()).unwrap_or("?");
                        let created = ext
                            .get("created_at")
                            .and_then(|v| v.as_str())
                            .unwrap_or("?");
                        println!(
                            "  {} [{}] {} ({})",
                            id.bright_yellow(),
                            st.bright_white(),
                            title,
                            created.bright_black()
                        );
                    }
                }
            }
        }

        Ok(())
    }
}

async fn create_sangha_proposal(
    proposals_dir: &std::path::Path,
    title: &str,
    description: &str,
    proposal_type: &str,
    related_extension_id: Option<&str>,
) -> Result<serde_json::Value> {
    let id = format!("prop-{}", &uuid::Uuid::new_v4().to_string()[..8]);
    let mut proposal = serde_json::json!({
        "id": id,
        "title": title,
        "description": description,
        "proposal_type": proposal_type,
        "status": "open",
        "votes": [],
        "created_at": chrono::Utc::now().to_rfc3339(),
    });

    if let Some(extension_id) = related_extension_id {
        proposal["related_extension_id"] = serde_json::json!(extension_id);
    }

    let proposal_id = proposal
        .get("id")
        .and_then(|value| value.as_str())
        .ok_or_else(|| anyhow::anyhow!("generated proposal is missing id"))?;
    let filepath = proposals_dir.join(format!("{}.json", proposal_id));
    let content = serde_json::to_string_pretty(&proposal)?;
    tokio::fs::write(&filepath, content).await?;

    Ok(proposal)
}

fn coordination_json_path(
    base_dir: &std::path::Path,
    id: &str,
    label: &str,
) -> Result<std::path::PathBuf> {
    if id.is_empty()
        || id.len() > 128
        || !id
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || ch == '-' || ch == '_')
    {
        anyhow::bail!("{} ID '{}' is not a safe coordination ID", label, id);
    }

    Ok(base_dir.join(format!("{}.json", id)))
}