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
// E-ana tablet — kickoff command: launch agents to implement features
mod cleanup;
mod graph;
mod helpers;
mod launch;
mod monitor;
pub(crate) mod pipeline;
mod plan;
mod prompt;
mod run;
mod types;
mod wizard;

#[cfg(test)]
mod tests;

// Re-export public types used by external callers (swarm, main, etc.)
pub use types::{
    ContainerMode, KickoffOpts, KickoffReport, PlanOpts, ReportFormat, VerifyLevel,
    DEFAULT_AGENT_IMAGE,
};

// Re-export parse functions (used by dispatch and swarm)
pub use types::{parse_container_mode, parse_duration, parse_verify_level};

// Re-export public command functions (used from main.rs dispatch)
pub use cleanup::cleanup;
pub use graph::graph;
pub use monitor::{list, logs, report, report_all, status, stop};
pub use plan::{plan, show_plan};
pub use run::run;

// Re-export crate-internal items used by other modules within this crate
pub(crate) use helpers::{
    command_available, detect_conventions, format_duration, slugify, tmux_session_name,
};

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

use crate::db::Database;
use crate::shared_writer::SharedWriter;
use crate::KickoffCommands;

pub fn dispatch(
    command: KickoffCommands,
    crosslink_dir: &Path,
    db: &Database,
    writer: Option<&SharedWriter>,
    quiet: bool,
    json: bool,
) -> Result<()> {
    match command {
        KickoffCommands::Run {
            description,
            issue,
            container,
            verify,
            model,
            image,
            timeout,
            dry_run,
            branch,
            doc,
            skip_permissions,
        } => {
            let parsed_doc = if let Some(ref path) = doc {
                let content = std::fs::read_to_string(path)
                    .with_context(|| format!("Failed to read design doc: {}", path.display()))?;
                let d = super::design_doc::parse_design_doc(&content);
                for warning in super::design_doc::validate_design_doc(&d) {
                    tracing::warn!("{}", warning);
                }
                Some(d)
            } else {
                None
            };
            let opts = KickoffOpts {
                description: &description,
                issue,
                container: parse_container_mode(&container)?,
                verify: parse_verify_level(&verify)?,
                model: &model,
                image: &image,
                timeout: parse_duration(&timeout)?,
                dry_run,
                branch: branch.as_deref(),
                quiet,
                design_doc: parsed_doc.as_ref(),
                doc_path: doc.as_ref().map(|p| p.to_str().unwrap_or("unknown")),
                skip_permissions,
            };
            // Update pipeline state if launching from a design doc
            if let Some(ref doc_path) = doc {
                // pipeline state update is best-effort — don't fail the launch
                let _ = pipeline::mark_running(doc_path, "pending", "pending", issue);
            }
            run(crosslink_dir, db, writer, &opts)?;
            Ok(())
        }
        KickoffCommands::Status { agent } => agent.as_ref().map_or_else(
            || pipeline_status_overview(crosslink_dir, json),
            |id| status(crosslink_dir, id),
        ),
        KickoffCommands::Logs { agent, lines } => logs(crosslink_dir, &agent, lines),
        KickoffCommands::Stop { agent, force } => stop(crosslink_dir, &agent, force),
        KickoffCommands::Plan {
            doc,
            issue,
            model,
            timeout,
            dry_run,
        } => {
            let content = std::fs::read_to_string(&doc)
                .with_context(|| format!("Failed to read design doc: {}", doc.display()))?;
            let design_doc = super::design_doc::parse_design_doc(&content);
            for warning in super::design_doc::validate_design_doc(&design_doc) {
                tracing::warn!("{}", warning);
            }
            let plan_opts = PlanOpts {
                doc: &design_doc,
                doc_path: Some(&doc),
                model: &model,
                timeout: parse_duration(&timeout)?,
                dry_run,
                issue,
                quiet,
            };
            plan(crosslink_dir, db, &plan_opts)
        }
        KickoffCommands::ShowPlan { agent } => show_plan(crosslink_dir, &agent),
        KickoffCommands::Report {
            agent,
            json: report_json,
            markdown,
            all,
        } => {
            let format = if report_json {
                ReportFormat::Json
            } else if markdown {
                ReportFormat::Markdown
            } else {
                ReportFormat::Table
            };
            if all {
                report_all(crosslink_dir, format)
            } else {
                let agent =
                    agent.ok_or_else(|| anyhow::anyhow!("Agent ID required (or use --all)"))?;
                report(crosslink_dir, &agent, format)
            }
        }
        KickoffCommands::List { status } => list(crosslink_dir, &status, json, quiet),
        KickoffCommands::Graph { all, no_pager: _ } => graph(crosslink_dir, all, json, quiet),
        KickoffCommands::Cleanup {
            dry_run,
            force,
            keep,
            json: cleanup_json,
        } => cleanup(crosslink_dir, dry_run, force, keep, cleanup_json),
        KickoffCommands::Launch {
            doc,
            plan: do_plan,
            run: do_run,
            verify,
            model,
            timeout,
            container,
            issue,
            dry_run,
            skip_permissions,
        } => dispatch_launch(
            crosslink_dir,
            db,
            writer,
            quiet,
            json,
            doc,
            do_plan,
            do_run,
            &verify,
            &model,
            &timeout,
            &container,
            issue,
            dry_run,
            skip_permissions,
        ),
    }
}

/// Dispatch the unified `crosslink kickoff [doc] [--plan|--run]` entry point.
///
/// If no flags are given and stdin is a TTY, launches the interactive wizard.
/// With `--plan` or `--run`, goes directly to the appropriate function.
#[allow(clippy::too_many_arguments)]
fn dispatch_launch(
    crosslink_dir: &Path,
    db: &Database,
    writer: Option<&SharedWriter>,
    quiet: bool,
    _json: bool,
    doc: Option<PathBuf>,
    do_plan: bool,
    do_run: bool,
    verify: &str,
    model: &str,
    timeout: &str,
    container: &str,
    issue: Option<i64>,
    dry_run: bool,
    skip_permissions: bool,
) -> Result<()> {
    // Non-interactive: --plan or --run flag provided
    if do_plan {
        let doc_path = doc.ok_or_else(|| {
            anyhow::anyhow!(
                "--plan requires a design document path: crosslink kickoff .design/foo.md --plan"
            )
        })?;
        let content = std::fs::read_to_string(&doc_path)
            .with_context(|| format!("Failed to read design doc: {}", doc_path.display()))?;
        let design_doc = super::design_doc::parse_design_doc(&content);
        for warning in super::design_doc::validate_design_doc(&design_doc) {
            eprintln!("Warning: {warning}");
        }
        let plan_opts = PlanOpts {
            doc: &design_doc,
            doc_path: Some(&doc_path),
            model,
            timeout: parse_duration(timeout)?,
            dry_run,
            issue,
            quiet,
        };
        return plan(crosslink_dir, db, &plan_opts);
    }

    if do_run {
        let Some(ref doc_path) = doc else {
            bail!("--run requires a design document or description");
        };

        let content = std::fs::read_to_string(doc_path)
            .with_context(|| format!("Failed to read design doc: {}", doc_path.display()))?;
        let parsed = super::design_doc::parse_design_doc(&content);
        for warning in super::design_doc::validate_design_doc(&parsed) {
            eprintln!("Warning: {warning}");
        }

        let description = if parsed.title.is_empty() {
            doc_path
                .file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or("feature")
                .to_string()
        } else {
            parsed.title.clone()
        };
        let parsed_doc = Some(parsed);

        let opts = KickoffOpts {
            description: &description,
            issue,
            container: parse_container_mode(container)?,
            verify: parse_verify_level(verify)?,
            model,
            image: types::DEFAULT_AGENT_IMAGE,
            timeout: parse_duration(timeout)?,
            dry_run,
            branch: None,
            quiet,
            design_doc: parsed_doc.as_ref(),
            doc_path: doc.as_ref().map(|p| p.to_str().unwrap_or("unknown")),
            skip_permissions,
        };
        if let Some(ref doc_path) = doc {
            let _ = pipeline::mark_running(doc_path, "pending", "pending", issue);
        }
        run(crosslink_dir, db, writer, &opts)?;
        return Ok(());
    }

    // Interactive mode: launch the wizard
    // If a doc path was provided, skip source selection (wizard pre-selects it)
    let choices = if let Some(ref doc_path) = doc {
        // Skip to stage selection with this doc pre-selected
        wizard_with_preselected_doc(crosslink_dir, doc_path)?
    } else {
        wizard::launch_wizard(crosslink_dir)?
    };

    let Some(choices) = choices else {
        if !quiet {
            println!("Kickoff cancelled.");
        }
        return Ok(());
    };

    // Dispatch based on wizard choices
    match choices.stage {
        wizard::WizardStage::Plan => {
            let doc_path = match &choices.source {
                wizard::WizardSource::DesignDoc(p) => p.clone(),
                wizard::WizardSource::QuickDescription(_) => {
                    bail!("Plan mode requires a design document");
                }
            };
            let config = choices.plan_config.unwrap_or_default();
            let content = std::fs::read_to_string(&doc_path)
                .with_context(|| format!("Failed to read design doc: {}", doc_path.display()))?;
            let design_doc = super::design_doc::parse_design_doc(&content);
            let plan_opts = PlanOpts {
                doc: &design_doc,
                doc_path: Some(&doc_path),
                model: &config.model,
                timeout: parse_duration(&config.timeout)?,
                dry_run: false,
                issue,
                quiet,
            };
            plan(crosslink_dir, db, &plan_opts)
        }
        wizard::WizardStage::Run => {
            let config = choices.run_config.unwrap_or_default();
            let (description, parsed_doc, doc_path_str) = match &choices.source {
                wizard::WizardSource::DesignDoc(p) => {
                    let content = std::fs::read_to_string(p)
                        .with_context(|| format!("Failed to read design doc: {}", p.display()))?;
                    let d = super::design_doc::parse_design_doc(&content);
                    let title = if d.title.is_empty() {
                        p.file_stem()
                            .and_then(|s| s.to_str())
                            .unwrap_or("feature")
                            .to_string()
                    } else {
                        d.title.clone()
                    };
                    let path_str = p.to_str().unwrap_or("unknown").to_string();
                    (title, Some(d), Some(path_str))
                }
                wizard::WizardSource::QuickDescription(desc) => (desc.clone(), None, None),
            };

            let opts = KickoffOpts {
                description: &description,
                issue: config.issue,
                container: parse_container_mode(&config.container)?,
                verify: parse_verify_level(&config.verify)?,
                model: &config.model,
                image: types::DEFAULT_AGENT_IMAGE,
                timeout: parse_duration(&config.timeout)?,
                dry_run: false,
                branch: None,
                quiet,
                design_doc: parsed_doc.as_ref(),
                doc_path: doc_path_str.as_deref(),
                skip_permissions: false,
            };
            run(crosslink_dir, db, writer, &opts)?;
            Ok(())
        }
    }
}

/// Launch wizard with a pre-selected design doc (skips source selection screen).
fn wizard_with_preselected_doc(
    crosslink_dir: &Path,
    doc_path: &Path,
) -> Result<Option<wizard::WizardChoices>> {
    // For now, launch the full wizard — the doc pre-selection is a UX optimization
    // that we handle by verifying the doc exists upfront
    if !doc_path.exists() {
        bail!(
            "Design document not found: {}\nCreate one with: crosslink design \"feature description\"",
            doc_path.display()
        );
    }
    wizard::launch_wizard(crosslink_dir)
}

/// Show pipeline status overview when `crosslink kickoff status` is called with no args.
fn pipeline_status_overview(crosslink_dir: &Path, json: bool) -> Result<()> {
    let root = crosslink_dir
        .parent()
        .ok_or_else(|| anyhow::anyhow!("Cannot determine repo root"))?;

    let states = pipeline::scan_pipeline_states(root);
    let agents = monitor::discover_agents(crosslink_dir).unwrap_or_default();

    if states.is_empty() {
        println!("No pipeline state files found in .design/");
        println!("Create a design doc with: crosslink design \"feature description\"");
        return Ok(());
    }

    if json {
        let json_states: Vec<_> = states.iter().map(|(_, s)| s).collect();
        println!("{}", serde_json::to_string_pretty(&json_states)?);
        return Ok(());
    }

    println!(
        "{:<34} {:<12} {:<14} {:<8} RUN",
        "DESIGN DOC", "STAGE", "PLAN", "GAPS"
    );

    for (doc_path, state) in &states {
        let filename = doc_path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("unknown");

        let stage = &state.stage;

        let plan_display = state.plans.last().map_or_else(
            || "\u{2014}".to_string(),
            |plan| {
                let age = plan.completed_at.as_ref().map_or_else(String::new, |ts| {
                    chrono::DateTime::parse_from_rfc3339(ts).map_or_else(
                        |_| String::new(),
                        |dt| {
                            let elapsed = chrono::Utc::now()
                                .signed_duration_since(dt.with_timezone(&chrono::Utc));
                            let mins = elapsed.num_minutes();
                            if mins < 60 {
                                format!(" ({mins}m)")
                            } else {
                                format!(" ({}h)", mins / 60)
                            }
                        },
                    )
                });
                format!("{}{}", plan.status, age)
            },
        );

        let gaps_display = state.plans.last().map_or_else(
            || "\u{2014}".to_string(),
            |plan| {
                if plan.status == "done" {
                    format!("{}/{}", plan.blocking_gaps, plan.advisory_gaps)
                } else {
                    "\u{2014}".to_string()
                }
            },
        );

        let run_display = state.runs.last().map_or_else(
            || "\u{2014}".to_string(),
            |run| {
                let live = agents.iter().find(|a| a.id == run.agent_id);
                live.map_or_else(
                    || format!("{} ({})", run.agent_id, run.status),
                    |agent| {
                        if agent.session.is_some() {
                            format!("{} ({})", run.agent_id, agent.status)
                        } else {
                            format!("{} ({})", run.agent_id, run.status)
                        }
                    },
                )
            },
        );

        println!(
            "{:<34} {:<12} {:<14} {:<8} {}",
            helpers::truncate_str(filename, 33),
            stage,
            helpers::truncate_str(&plan_display, 13),
            gaps_display,
            helpers::truncate_str(&run_display, 40),
        );
    }

    Ok(())
}