crosslink 0.8.0

A synced issue tracker CLI for multi-agent AI 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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
// Swarm lifecycle: reset, archive, list, retry-failed, adopt, sync-status,
// resume, launch, gate, checkpoint.

use anyhow::{bail, Context, Result};
use std::path::Path;

use super::io::*;
use super::status::{probe_agent_status, resolve_agents};
use super::types::*;
use crate::commands::kickoff::{self, ContainerMode, KickoffOpts, VerifyLevel};
use crate::db::Database;
use crate::shared_writer::SharedWriter;
use crate::sync::SyncManager;

// ---------------------------------------------------------------------------
// swarm reset / archive / list
// ---------------------------------------------------------------------------

/// Archive the current swarm plan to swarm/archive/{timestamp}/ and clear the active slot.
pub fn archive(crosslink_dir: &Path) -> Result<()> {
    let sync = SyncManager::new(crosslink_dir)?;
    if !sync.is_initialized() {
        bail!("Hub cache not initialized. Run `crosslink sync` first.");
    }
    sync.fetch()?;

    let ctx = resolve_swarm(&sync)?;
    let plan_path_str = ctx.plan_path();
    let plan_path = sync.cache_path().join(&plan_path_str);
    if !plan_path.exists() {
        bail!("No active swarm plan to archive.");
    }

    let plan: SwarmPlan =
        read_hub_json(&sync, &plan_path_str).context("Failed to read swarm plan")?;

    // Create archive directory with timestamp
    let timestamp = chrono::Utc::now().format("%Y%m%d-%H%M%S").to_string();
    let archive_prefix = format!("swarm/archive/{timestamp}");

    // Copy plan.json to archive
    let plan_json = std::fs::read_to_string(&plan_path)?;
    let archive_plan = sync
        .cache_path()
        .join(format!("{archive_prefix}/plan.json"));
    if let Some(parent) = archive_plan.parent() {
        std::fs::create_dir_all(parent)?;
    }
    std::fs::write(&archive_plan, &plan_json)?;

    // Copy phase files to archive
    let phases_dir = sync.cache_path().join(format!("{}/phases", ctx.base));
    if phases_dir.is_dir() {
        let archive_phases = sync.cache_path().join(format!("{archive_prefix}/phases"));
        std::fs::create_dir_all(&archive_phases)?;
        if let Ok(entries) = std::fs::read_dir(&phases_dir) {
            for entry in entries.flatten() {
                let name = entry.file_name();
                let dest = archive_phases.join(&name);
                // INTENTIONAL: archive copy is best-effort — partial archive is acceptable
                let _ = std::fs::copy(entry.path(), dest);
            }
        }
    }

    // Copy checkpoints to archive
    let checkpoints_dir = sync.cache_path().join(ctx.checkpoints_dir());
    if checkpoints_dir.is_dir() {
        let archive_cp = sync
            .cache_path()
            .join(format!("{archive_prefix}/checkpoints"));
        std::fs::create_dir_all(&archive_cp)?;
        if let Ok(entries) = std::fs::read_dir(&checkpoints_dir) {
            for entry in entries.flatten() {
                let name = entry.file_name();
                let dest = archive_cp.join(&name);
                // INTENTIONAL: archive copy is best-effort — partial archive is acceptable
                let _ = std::fs::copy(entry.path(), dest);
            }
        }
    }

    // INTENTIONAL: swarm file cleanup is best-effort — partial removal is acceptable, git commit tracks the state
    let _ = std::fs::remove_file(&plan_path);
    let _ = std::fs::remove_dir_all(&phases_dir);
    let _ = std::fs::remove_dir_all(&checkpoints_dir);
    let _ = std::fs::remove_file(sync.cache_path().join("swarm/active.json"));
    // For multi-swarm, remove the UUID directory itself if empty
    if !ctx.is_legacy {
        let _ = std::fs::remove_dir_all(sync.cache_path().join(ctx.base));
    }

    // Stage all swarm/ changes (additions, modifications, and deletions) on the
    // hub branch cache. This is safe because the cache is a dedicated worktree
    // for crosslink/hub, not the user's working tree.
    let cache = sync.cache_path();
    if let Ok(o) = std::process::Command::new("git")
        .current_dir(cache)
        .args(["add", "--all", "--", "swarm/"])
        .output()
    {
        if !o.status.success() {
            tracing::warn!(
                "git add failed during swarm archive: {}",
                String::from_utf8_lossy(&o.stderr).trim()
            );
        }
    }
    if let Ok(o) = std::process::Command::new("git")
        .current_dir(cache)
        .args([
            "commit",
            "-m",
            &format!("swarm: archive '{}' to {}", plan.title, archive_prefix),
        ])
        .output()
    {
        if !o.status.success() {
            let msg = String::from_utf8_lossy(&o.stderr);
            if !msg.contains("nothing to commit") {
                tracing::warn!("git commit failed during swarm archive: {}", msg.trim());
            }
        }
    }
    let remote = sync.remote();
    if let Ok(o) = std::process::Command::new("git")
        .current_dir(cache)
        .args(["push", remote, crate::sync::HUB_BRANCH])
        .output()
    {
        if !o.status.success() {
            tracing::warn!(
                "could not push swarm archive to hub: {} — archive is saved locally",
                String::from_utf8_lossy(&o.stderr).trim()
            );
        }
    }

    println!("Archived swarm '{}' to {}/", plan.title, archive_prefix);
    println!("Active swarm slot is now clear. Run `crosslink swarm init` to start a new swarm.");
    Ok(())
}

/// Reset the active swarm. Archives first unless --no-archive.
pub fn reset(crosslink_dir: &Path, no_archive: bool) -> Result<()> {
    if !no_archive {
        return archive(crosslink_dir);
    }

    let sync = SyncManager::new(crosslink_dir)?;
    if !sync.is_initialized() {
        bail!("Hub cache not initialized.");
    }
    sync.fetch()?;

    let ctx = resolve_swarm(&sync)?;
    let plan_path = sync.cache_path().join(ctx.plan_path());
    if !plan_path.exists() {
        bail!("No active swarm plan to reset.");
    }

    // INTENTIONAL: swarm file cleanup is best-effort — partial removal is acceptable, git commit tracks the state
    let _ = std::fs::remove_file(&plan_path);
    let _ = std::fs::remove_dir_all(sync.cache_path().join(format!("{}/phases", ctx.base)));
    let _ = std::fs::remove_dir_all(sync.cache_path().join(ctx.checkpoints_dir()));
    let _ = std::fs::remove_file(sync.cache_path().join("swarm/active.json"));
    if !ctx.is_legacy {
        let _ = std::fs::remove_dir_all(sync.cache_path().join(ctx.base));
    }

    // Stage all swarm/ changes on the hub branch cache (see archive() comment).
    let cache = sync.cache_path();
    if let Ok(o) = std::process::Command::new("git")
        .current_dir(cache)
        .args(["add", "--all", "--", "swarm/"])
        .output()
    {
        if !o.status.success() {
            tracing::warn!(
                "git add failed during swarm reset: {}",
                String::from_utf8_lossy(&o.stderr).trim()
            );
        }
    }
    if let Ok(o) = std::process::Command::new("git")
        .current_dir(cache)
        .args(["commit", "-m", "swarm: reset (no archive)"])
        .output()
    {
        if !o.status.success() {
            let msg = String::from_utf8_lossy(&o.stderr);
            if !msg.contains("nothing to commit") {
                tracing::warn!("git commit failed during swarm reset: {}", msg.trim());
            }
        }
    }
    let remote = sync.remote();
    if let Ok(o) = std::process::Command::new("git")
        .current_dir(cache)
        .args(["push", remote, crate::sync::HUB_BRANCH])
        .output()
    {
        if !o.status.success() {
            tracing::warn!(
                "could not push swarm reset to hub: {} — reset is saved locally",
                String::from_utf8_lossy(&o.stderr).trim()
            );
        }
    }

    println!("Swarm plan deleted. Active slot is clear.");
    Ok(())
}

/// List active and archived swarms.
pub fn list_swarms(crosslink_dir: &Path) -> Result<()> {
    let sync = SyncManager::new(crosslink_dir)?;
    if !sync.is_initialized() {
        bail!("Hub cache not initialized. Run `crosslink sync` first.");
    }
    sync.fetch()?;

    match resolve_swarm(&sync) {
        Ok(ctx) => {
            if let Ok(plan) = read_hub_json::<SwarmPlan>(&sync, &ctx.plan_path()) {
                let mode = if ctx.is_legacy { " (legacy)" } else { "" };
                println!(
                    "Active: {} (created {}){}",
                    plan.title, plan.created_at, mode
                );
            }
        }
        Err(_) => {
            println!("No active swarm.");
        }
    }

    let archive_dir = sync.cache_path().join("swarm/archive");
    if archive_dir.is_dir() {
        let mut archives: Vec<String> = Vec::new();
        if let Ok(entries) = std::fs::read_dir(&archive_dir) {
            for entry in entries.flatten() {
                if entry.file_type().is_ok_and(|t| t.is_dir()) {
                    let name = entry.file_name().to_string_lossy().to_string();
                    let plan_file = entry.path().join("plan.json");
                    let title = std::fs::read_to_string(&plan_file)
                        .ok()
                        .and_then(|c| serde_json::from_str::<SwarmPlan>(&c).ok())
                        .map_or_else(|| "(unknown)".to_string(), |p| p.title);
                    archives.push(format!("  {name}{title}"));
                }
            }
        }
        archives.sort();
        if !archives.is_empty() {
            println!("\nArchived swarms:");
            for a in &archives {
                println!("{a}");
            }
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// swarm launch --retry-failed
// ---------------------------------------------------------------------------

/// Relaunch agents that previously failed in a phase.
pub fn launch_retry_failed(
    crosslink_dir: &Path,
    db: &Database,
    writer: Option<&SharedWriter>,
    phase_slug: &str,
    quiet: bool,
) -> Result<()> {
    let sync = SyncManager::new(crosslink_dir)?;
    if !sync.is_initialized() {
        bail!("Hub cache not initialized. Run `crosslink sync` first.");
    }

    let (mut phase, phase_file) = load_phase(&sync, phase_slug)?;

    let root = crosslink_dir
        .parent()
        .ok_or_else(|| anyhow::anyhow!("Cannot determine repo root"))?;
    let resolved = resolve_agents(&phase, root);

    // Find failed agents and reset them to planned
    let mut retry_count = 0;
    for agent in &mut phase.agents {
        let live = resolved
            .iter()
            .find(|r| r.slug == agent.slug)
            .map_or("planned", |r| r.live_status.as_str());

        if agent.status == AgentStatus::Failed || live == "FAILED" || live == "failed" {
            agent.status = AgentStatus::Planned;
            agent.started_at = None;
            agent.completed_at = None;
            retry_count += 1;
        }
    }

    if retry_count == 0 {
        println!("No failed agents to retry in '{}'.", phase.name);
        return Ok(());
    }

    write_hub_json(&sync, &phase_file, &phase)?;
    commit_hub_files(
        &sync,
        &[&phase_file],
        &format!("swarm: reset {retry_count} failed agents for retry"),
    )?;

    println!("Reset {retry_count} failed agent(s) to planned. Launching...");

    launch(crosslink_dir, db, writer, phase_slug, quiet)
}

// ---------------------------------------------------------------------------
// swarm adopt
// ---------------------------------------------------------------------------

/// Associate an external agent/branch with a swarm phase slot.
///
/// When an agent is launched manually (outside `swarm launch`), this command
/// lets you link it to a swarm slot so status/gate/merge see it correctly.
pub fn adopt(crosslink_dir: &Path, agent_slug: &str, slot_slug: &str) -> Result<()> {
    let (sync, plan, mut phases) = load_plan_and_phases(crosslink_dir)?;

    // Find the slot
    let mut found = false;
    for (_path, phase) in &mut phases {
        for agent in &mut phase.agents {
            if agent.slug == slot_slug {
                agent.status = AgentStatus::Running;
                agent.branch = Some(format!("feature/{agent_slug}"));
                agent.started_at = Some(chrono::Utc::now().to_rfc3339());
                found = true;
                break;
            }
        }
        if found {
            break;
        }
    }

    if !found {
        bail!(
            "Slot '{}' not found in any phase. Available slots: {}",
            slot_slug,
            phases
                .iter()
                .flat_map(|(_, p)| p.agents.iter().map(|a| a.slug.as_str()))
                .collect::<Vec<_>>()
                .join(", ")
        );
    }

    save_plan_and_phases(
        &sync,
        &plan,
        &phases,
        &format!("swarm: adopt agent '{agent_slug}' into slot '{slot_slug}'"),
    )?;
    println!("Adopted '{agent_slug}' into swarm slot '{slot_slug}' (branch: feature/{agent_slug})");
    Ok(())
}

// ---------------------------------------------------------------------------
// swarm sync-status
// ---------------------------------------------------------------------------

/// Sync live agent statuses from worktree probes back into phase JSON files.
///
/// Bridges the gap between `swarm status` (reads live state) and
/// `swarm merge`/`swarm gate` (reads phase JSON).
pub fn sync_status(crosslink_dir: &Path) -> Result<()> {
    let sync = SyncManager::new(crosslink_dir)?;
    if !sync.is_initialized() {
        bail!("Hub cache not initialized. Run `crosslink sync` first.");
    }
    sync.fetch()?;

    let ctx = resolve_swarm(&sync)?;
    let plan: SwarmPlan = read_hub_json(&sync, &ctx.plan_path()).context("No swarm plan found.")?;

    let root = crosslink_dir
        .parent()
        .ok_or_else(|| anyhow::anyhow!("Cannot determine repo root"))?;

    let mut updated_count = 0;
    let mut paths_to_commit: Vec<String> = Vec::new();

    for phase_name in &plan.phases {
        let phase_path = ctx.phase_path(phase_name);
        let mut phase: PhaseDefinition = match read_hub_json(&sync, &phase_path) {
            Ok(p) => p,
            Err(_) => continue,
        };

        if phase.status == PhaseStatus::Completed {
            continue;
        }

        let mut phase_changed = false;

        for agent in &mut phase.agents {
            let live = probe_agent_status(root, &agent.slug);

            let new_status =
                if live == "DONE" || live == "completed" || live.starts_with("completed") {
                    Some(AgentStatus::Completed)
                } else if live == "FAILED" || live == "failed" || live.starts_with("failed") {
                    Some(AgentStatus::Failed)
                } else if live.starts_with("running") {
                    Some(AgentStatus::Running)
                } else {
                    None
                };

            if let Some(status) = new_status {
                if agent.status != status {
                    let old = format!("{}", agent.status);
                    agent.status = status.clone();
                    if matches!(status, AgentStatus::Completed | AgentStatus::Failed)
                        && agent.completed_at.is_none()
                    {
                        agent.completed_at = Some(chrono::Utc::now().to_rfc3339());
                    }
                    println!("  {} {}{}", agent.slug, old, agent.status);
                    phase_changed = true;
                    updated_count += 1;
                }
            }
        }

        if phase_changed {
            let all_done = phase.agents.iter().all(|a| {
                matches!(
                    a.status,
                    AgentStatus::Completed | AgentStatus::Merged | AgentStatus::Failed
                )
            });
            if all_done && phase.status == PhaseStatus::InProgress {
                let any_failed = phase.agents.iter().any(|a| a.status == AgentStatus::Failed);
                if any_failed {
                    phase.status = PhaseStatus::Failed;
                    println!("  Phase '{}' → failed", phase.name);
                }
            }

            write_hub_json(&sync, &phase_path, &phase)?;
            paths_to_commit.push(phase_path);
        }
    }

    if paths_to_commit.is_empty() {
        println!("All phase statuses are up to date.");
    } else {
        let refs: Vec<&str> = paths_to_commit.iter().map(String::as_str).collect();
        commit_hub_files(
            &sync,
            &refs,
            &format!("swarm: sync {updated_count} agent status(es) from live state"),
        )?;
        println!("Synced {updated_count} agent status update(s).");
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// swarm resume
// ---------------------------------------------------------------------------

/// Reconstruct swarm state and output structured next-steps.
pub fn resume(crosslink_dir: &Path) -> Result<()> {
    let sync = SyncManager::new(crosslink_dir)?;
    if !sync.is_initialized() {
        bail!("Hub cache not initialized. Run `crosslink sync` first.");
    }

    let ctx = resolve_swarm(&sync)?;
    let plan: SwarmPlan = read_hub_json(&sync, &ctx.plan_path())
        .context("No swarm plan found. Run `crosslink swarm init --doc <file>` first.")?;

    let root = crosslink_dir
        .parent()
        .ok_or_else(|| anyhow::anyhow!("Cannot determine repo root"))?;

    // Find the latest checkpoint
    let checkpoint_dir = sync.cache_path().join(ctx.checkpoints_dir());
    let latest_checkpoint = find_latest_checkpoint(&checkpoint_dir);

    if let Some(ref cp) = latest_checkpoint {
        println!("Latest checkpoint: {} ({})", cp.phase, cp.created_at);
        if let Some(ref notes) = cp.handoff_notes {
            println!("  Notes: {notes}");
        }
        println!();
    }

    // Find the active phase (first non-completed phase)
    let mut active_phase: Option<PhaseDefinition> = None;
    let mut active_phase_name: Option<String> = None;
    let mut completed_count = 0;

    for phase_name in &plan.phases {
        let phase_file = ctx.phase_path(phase_name);
        let phase: PhaseDefinition = match read_hub_json(&sync, &phase_file) {
            Ok(p) => p,
            Err(_) => continue,
        };

        if phase.status == PhaseStatus::Completed {
            completed_count += 1;
            continue;
        }

        active_phase = Some(phase);
        active_phase_name = Some(phase_name.clone());
        break;
    }

    let (Some(phase), Some(phase_name)) = (active_phase, active_phase_name) else {
        println!(
            "All {} phases completed. Swarm build is done.",
            plan.phases.len()
        );
        return Ok(());
    };

    println!(
        "Resume point: {} ({}/{})",
        phase_name,
        completed_count,
        plan.phases.len()
    );
    println!();

    // Categorize agents by live status
    let resolved = resolve_agents(&phase, root);
    let mut actions: Vec<String> = Vec::new();
    let mut action_num = 1;

    // Agents completed but not merged
    let ready_to_merge: Vec<&ResolvedAgent> = resolved
        .iter()
        .filter(|a| a.live_status == "DONE" && a.defined_status != AgentStatus::Merged)
        .collect();

    if !ready_to_merge.is_empty() {
        for agent in &ready_to_merge {
            let branch = agent.branch.as_deref().unwrap_or_else(|| &agent.slug);
            actions.push(format!(
                "{}. Merge {}: review and merge {} to dev",
                action_num, agent.slug, branch
            ));
            action_num += 1;
        }
    }

    // Agents still running
    let running: Vec<&ResolvedAgent> = resolved
        .iter()
        .filter(|a| a.live_status.starts_with("running"))
        .collect();

    if !running.is_empty() {
        for agent in &running {
            actions.push(format!(
                "{}. Check {}: crosslink kickoff status {}",
                action_num, agent.slug, agent.slug
            ));
            action_num += 1;
        }
    }

    // Agents that failed
    let failed: Vec<&ResolvedAgent> = resolved
        .iter()
        .filter(|a| a.live_status == "FAILED" || a.live_status == "failed")
        .collect();

    if !failed.is_empty() {
        for agent in &failed {
            actions.push(format!(
                "{}. Investigate {} failure: crosslink kickoff report {}",
                action_num, agent.slug, agent.slug
            ));
            action_num += 1;
        }
    }

    // Agents not yet started
    let planned: Vec<&ResolvedAgent> = resolved
        .iter()
        .filter(|a| a.live_status == "planned")
        .collect();

    if !planned.is_empty() {
        let slugs: Vec<&str> = planned.iter().map(|a| a.slug.as_str()).collect();
        actions.push(format!(
            "{}. Launch remaining agents: {}",
            action_num,
            slugs.join(", ")
        ));
        action_num += 1;
    }

    // Unknown/stale agents
    let unknown: Vec<&ResolvedAgent> = resolved
        .iter()
        .filter(|a| a.live_status.starts_with("unknown"))
        .collect();

    if !unknown.is_empty() {
        for agent in &unknown {
            actions.push(format!(
                "{}. Check stale agent {}: worktree exists but no active session",
                action_num, agent.slug
            ));
            action_num += 1;
        }
    }

    // If all agents are done/merged, suggest gate
    let all_agents_resolved = ready_to_merge.is_empty()
        && running.is_empty()
        && failed.is_empty()
        && planned.is_empty()
        && unknown.is_empty();

    let phase_slug = slugify_phase(&phase_name);
    if all_agents_resolved {
        actions.push(format!(
            "{action_num}. All agents merged. Run gate: crosslink swarm gate {phase_slug}"
        ));
        action_num += 1;
        actions.push(format!(
            "{action_num}. If gate passes: crosslink swarm checkpoint {phase_slug}"
        ));
    } else if ready_to_merge.is_empty() && running.is_empty() && planned.is_empty() {
        // Only failed/unknown agents remain
        actions.push(format!(
            "{action_num}. After resolving failures: crosslink swarm gate {phase_slug}"
        ));
    } else {
        actions.push(format!(
            "{action_num}. After merges complete: crosslink swarm gate {phase_slug}"
        ));
        action_num += 1;
        if completed_count + 1 < plan.phases.len() {
            actions.push(format!(
                "{action_num}. If gate passes: crosslink swarm checkpoint {phase_slug}"
            ));
        }
    }

    println!("Next actions:");
    for action in &actions {
        println!("  {action}");
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// swarm launch
// ---------------------------------------------------------------------------

/// Launch all planned agents for a phase via `kickoff run`.
pub fn launch(
    crosslink_dir: &Path,
    db: &Database,
    writer: Option<&SharedWriter>,
    phase_slug: &str,
    quiet: bool,
) -> Result<()> {
    let sync = SyncManager::new(crosslink_dir)?;
    if !sync.is_initialized() {
        bail!("Hub cache not initialized. Run `crosslink sync` first.");
    }

    let (mut phase, phase_file) = load_phase(&sync, phase_slug)?;

    if phase.status == PhaseStatus::Completed {
        bail!("Phase '{}' is already completed", phase.name);
    }

    check_dependencies(&sync, &phase)?;

    let planned_agents: Vec<usize> = phase
        .agents
        .iter()
        .enumerate()
        .filter(|(_, a)| a.status == AgentStatus::Planned)
        .map(|(i, _)| i)
        .collect();

    if planned_agents.is_empty() {
        println!("No planned agents to launch in '{}'.", phase.name);
        println!("Use `crosslink swarm status` to see current agent states.");
        return Ok(());
    }

    let now = chrono::Utc::now().to_rfc3339();

    if !quiet {
        println!(
            "Launching {} agent{} for {}...",
            planned_agents.len(),
            if planned_agents.len() == 1 { "" } else { "s" },
            phase.name
        );
        println!();
    }

    for idx in &planned_agents {
        let slug = phase.agents[*idx].slug.clone();
        let description = phase.agents[*idx].description.clone();
        let issue_id = phase.agents[*idx].issue_id;
        let branch = phase.agents[*idx].branch.clone();

        let opts = KickoffOpts {
            description: &description,
            issue: issue_id,
            container: ContainerMode::None,
            verify: VerifyLevel::Local,
            model: "opus",
            image: kickoff::DEFAULT_AGENT_IMAGE,
            timeout: std::time::Duration::from_secs(3600),
            dry_run: false,
            branch: branch.as_deref(),
            quiet,
            design_doc: None,
            doc_path: None,
            skip_permissions: false,
        };

        match kickoff::run(crosslink_dir, db, writer, &opts) {
            Ok(compact_name) => {
                phase.agents[*idx].status = AgentStatus::Running;
                phase.agents[*idx].started_at = Some(now.clone());
                phase.agents[*idx].agent_id = Some(compact_name.clone());
                phase.agents[*idx].branch = Some(format!("feature/{compact_name}"));
            }
            Err(e) => {
                tracing::error!("Failed to launch {}: {}", slug, e);
                phase.agents[*idx].status = AgentStatus::Failed;
            }
        }
    }

    phase.status = PhaseStatus::InProgress;

    write_hub_json(&sync, &phase_file, &phase)?;
    commit_hub_files(
        &sync,
        &[phase_file.as_str()],
        &format!("swarm: launch {}", phase.name),
    )?;

    if !quiet {
        let running = phase
            .agents
            .iter()
            .filter(|a| a.status == AgentStatus::Running)
            .count();
        let failed = phase
            .agents
            .iter()
            .filter(|a| a.status == AgentStatus::Failed)
            .count();
        println!();
        println!(
            "{} agent{} launched, {} failed.",
            running,
            if running == 1 { "" } else { "s" },
            failed
        );
        println!();
        println!("Monitor with: crosslink swarm status");
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// swarm gate
// ---------------------------------------------------------------------------

/// Run the project gate (test suite) for a phase and record the result.
pub fn gate(crosslink_dir: &Path, phase_slug: &str) -> Result<()> {
    // Auto-sync agent statuses before gating so phase JSON reflects live state
    if let Err(e) = sync_status(crosslink_dir) {
        tracing::warn!("could not sync agent statuses: {}", e);
    }

    let sync = SyncManager::new(crosslink_dir)?;
    if !sync.is_initialized() {
        bail!("Hub cache not initialized. Run `crosslink sync` first.");
    }

    let (mut phase, phase_file) = load_phase(&sync, phase_slug)?;

    // Check that agents are resolved (all completed/merged/failed)
    let root = crosslink_dir
        .parent()
        .ok_or_else(|| anyhow::anyhow!("Cannot determine repo root"))?;

    let unresolved: Vec<&AgentEntry> = phase
        .agents
        .iter()
        .filter(|a| a.status == AgentStatus::Planned || a.status == AgentStatus::Running)
        .collect();

    if !unresolved.is_empty() {
        let live_unresolved: Vec<&AgentEntry> = unresolved
            .into_iter()
            .filter(|a| {
                let live = probe_agent_status(root, &a.slug);
                live == "planned" || live.starts_with("running")
            })
            .collect();

        if !live_unresolved.is_empty() {
            let names: Vec<&str> = live_unresolved.iter().map(|a| a.slug.as_str()).collect();
            bail!(
                "Cannot gate: {} agent(s) still unresolved: {}",
                live_unresolved.len(),
                names.join(", ")
            );
        }
    }

    // Detect project conventions and get test command
    let conventions = kickoff::detect_conventions(root);
    let test_cmd = conventions.test_command.as_deref().unwrap_or("cargo test");

    println!("Running gate: {test_cmd}");
    println!();

    let cmd_parts: Vec<&str> = test_cmd.split_whitespace().collect();
    let (program, args) = cmd_parts
        .split_first()
        .ok_or_else(|| anyhow::anyhow!("Empty gate test command"))?;
    let output = std::process::Command::new(program)
        .args(args)
        .current_dir(root)
        .output()
        .with_context(|| format!("Failed to run gate command: {test_cmd}"))?;

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    let gate_passed = output.status.success();
    let now = chrono::Utc::now().to_rfc3339();

    // Try to parse test counts from output (Rust cargo test format)
    let (tests_total, tests_passed) = parse_test_counts(&stdout, &stderr);

    let gate_result = GateResult {
        status: if gate_passed {
            "passed".to_string()
        } else {
            "failed".to_string()
        },
        tests_total,
        tests_passed,
        ran_at: Some(now),
    };

    phase.gate = Some(gate_result);

    write_hub_json(&sync, &phase_file, &phase)?;
    commit_hub_files(
        &sync,
        &[phase_file.as_str()],
        &format!(
            "swarm: gate {}{}",
            phase.name,
            if gate_passed { "passed" } else { "failed" }
        ),
    )?;

    if gate_passed {
        let tests_info = tests_total
            .map(|t| format!(" ({t} tests)"))
            .unwrap_or_default();
        println!("Gate passed{tests_info}");
        println!();
        println!(
            "Next: crosslink swarm checkpoint {}",
            slugify_phase(&phase.name)
        );
    } else {
        println!("Gate FAILED.");
        if !stderr.is_empty() {
            let tail: Vec<&str> = stderr.lines().rev().take(20).collect();
            for line in tail.iter().rev() {
                println!("  {line}");
            }
        }
        println!();
        println!("Fix failures and re-run: crosslink swarm gate {phase_slug}");
    }

    Ok(())
}

/// Parse test counts from combined stdout/stderr (supports cargo test output).
pub(super) fn parse_test_counts(stdout: &str, stderr: &str) -> (Option<u64>, Option<u64>) {
    // cargo test format: "test result: ok. 142 passed; 0 failed; 0 ignored; ..."
    for text in [stdout, stderr] {
        for line in text.lines() {
            if line.starts_with("test result:") {
                let mut passed: Option<u64> = None;
                let mut failed: Option<u64> = None;

                for part in line.split(';') {
                    let part = part.trim();
                    if part.ends_with("passed") {
                        passed = part.split_whitespace().find_map(|w| w.parse::<u64>().ok());
                    } else if part.ends_with("failed") {
                        failed = part.split_whitespace().find_map(|w| w.parse::<u64>().ok());
                    }
                }

                if let (Some(p), Some(f)) = (passed, failed) {
                    return (Some(p + f), Some(p));
                }
            }
        }
    }
    (None, None)
}

// ---------------------------------------------------------------------------
// swarm checkpoint
// ---------------------------------------------------------------------------

/// Record a checkpoint after a phase completes.
pub fn checkpoint(
    crosslink_dir: &Path,
    phase_slug: &str,
    notes: Option<&str>,
    force: bool,
) -> Result<()> {
    let sync = SyncManager::new(crosslink_dir)?;
    if !sync.is_initialized() {
        bail!("Hub cache not initialized. Run `crosslink sync` first.");
    }

    let (mut phase, phase_file) = load_phase(&sync, phase_slug)?;

    // Verify gate passed (unless --force)
    if !force {
        match &phase.gate {
            Some(g) if g.status == "passed" => {}
            Some(g) => bail!(
                "Gate status is '{}', not 'passed'. Use --force to checkpoint anyway.",
                g.status
            ),
            None => bail!(
                "No gate result recorded. Run `crosslink swarm gate {phase_slug}` first, or use --force."
            ),
        }
    }

    let now = chrono::Utc::now().to_rfc3339();

    // Get current dev branch SHA
    let dev_sha = std::process::Command::new("git")
        .args(["rev-parse", "HEAD"])
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string());

    let agents_merged: Vec<String> = phase
        .agents
        .iter()
        .filter(|a| a.status == AgentStatus::Merged || a.status == AgentStatus::Completed)
        .map(|a| a.slug.clone())
        .collect();

    let agents_pending: Vec<String> = phase
        .agents
        .iter()
        .filter(|a| a.status != AgentStatus::Merged && a.status != AgentStatus::Completed)
        .map(|a| a.slug.clone())
        .collect();

    let test_result = phase
        .gate
        .as_ref()
        .and_then(|g| match (g.tests_total, g.tests_passed) {
            (Some(total), Some(passed)) => Some(TestResult {
                total,
                passed,
                failed: total.saturating_sub(passed),
            }),
            _ => None,
        });

    let cp = Checkpoint {
        phase: phase.name.clone(),
        created_at: now.clone(),
        agents_merged,
        agents_pending,
        dev_branch_sha: dev_sha,
        test_result,
        handoff_notes: notes.map(ToString::to_string),
    };

    let ctx = resolve_swarm(&sync)?;
    let cp_slug = slugify_phase(&phase.name);
    let cp_path = ctx.checkpoint_path(&cp_slug);
    write_hub_json(&sync, &cp_path, &cp)?;

    // Mark phase completed
    phase.status = PhaseStatus::Completed;
    phase.checkpoint = Some(cp_slug);
    for agent in &mut phase.agents {
        if agent.status == AgentStatus::Completed {
            agent.status = AgentStatus::Merged;
            agent.completed_at = Some(now.clone());
        }
    }

    write_hub_json(&sync, &phase_file, &phase)?;
    commit_hub_files(
        &sync,
        &[phase_file.as_str(), cp_path.as_str()],
        &format!("swarm: checkpoint {}", phase.name),
    )?;

    println!("Checkpoint recorded for {}", phase.name);
    if let Some(n) = notes {
        println!("  Notes: {n}");
    }

    // Check if there's a next phase
    let plan: SwarmPlan = read_hub_json(&sync, &ctx.plan_path())?;
    let current_idx = plan
        .phases
        .iter()
        .position(|p| slugify_phase(p) == slugify_phase(&phase.name));

    if let Some(idx) = current_idx {
        if idx + 1 < plan.phases.len() {
            let next = &plan.phases[idx + 1];
            println!();
            println!(
                "Next phase: {} → crosslink swarm launch {}",
                next,
                slugify_phase(next)
            );
        } else {
            println!();
            println!("All phases completed. Swarm build is done.");
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Shared helpers
// ---------------------------------------------------------------------------

/// Find the latest checkpoint file by modification time.
pub(super) fn find_latest_checkpoint(dir: &Path) -> Option<Checkpoint> {
    if !dir.is_dir() {
        return None;
    }

    let mut entries: Vec<_> = std::fs::read_dir(dir)
        .ok()?
        .filter_map(std::result::Result::ok)
        .filter(|e| e.path().extension().is_some_and(|ext| ext == "json"))
        .collect();

    entries.sort_by_key(|e| e.metadata().ok().and_then(|m| m.modified().ok()));

    if let Some(entry) = entries.last() {
        let content = std::fs::read_to_string(entry.path()).ok()?;
        serde_json::from_str(&content).ok()
    } else {
        None
    }
}