crosslink 0.9.0-beta.1

A synced issue tracker CLI for multi-agent AI development
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
//! Code clone detection via cpitd (Copy Paste Is The Devil).
//!
//! Shells out to the `cpitd` Python tool, parses its JSON output,
//! and creates crosslink issues for detected code clones.

use std::collections::HashMap;
use std::fmt::Write as _;
use std::process::Command;

use anyhow::{bail, Context, Result};
use serde::Deserialize;

use crate::CpitdCommands;

use crate::db::Database;

pub fn run(command: CpitdCommands, db: &Database, quiet: bool) -> Result<()> {
    match command {
        CpitdCommands::Scan {
            paths,
            min_tokens,
            ignore,
            dry_run,
        } => scan(db, &paths, min_tokens, &ignore, dry_run, quiet),
        CpitdCommands::Status => status(db),
        CpitdCommands::Clear => clear(db),
    }
}
use crate::utils::format_issue_id;

// ---------------------------------------------------------------------------
// cpitd JSON output types
// ---------------------------------------------------------------------------

#[derive(Debug, Deserialize)]
struct CpitdOutput {
    #[serde(default)]
    clone_reports: Vec<CpitdCloneReport>,
    /// `total_pairs` is the field name in cpitd ≤ 0.2.x; cpitd 0.3.0
    /// renamed it to `total_groups` to reflect the move from "pairs of
    /// files" to "N-way clone groups". Accept either via `serde(alias)`
    /// so the empty-output path doesn't fail to parse. The full schema
    /// migration for non-empty `clone_reports` (new `locations` model
    /// vs. old `file_a`/`file_b`/`groups`) is a separate concern —
    /// this annotation just unblocks the "no clones found" path.
    #[serde(default, alias = "total_groups")]
    total_pairs: usize,
}

#[derive(Debug, Deserialize)]
struct CpitdCloneReport {
    file_a: String,
    file_b: String,
    total_cloned_lines: usize,
    groups: Vec<CpitdCloneGroup>,
}

#[derive(Debug, Deserialize)]
struct CpitdCloneGroup {
    lines_a: Vec<usize>,
    lines_b: Vec<usize>,
    line_count: usize,
    token_count: usize,
}

// ---------------------------------------------------------------------------
// Installation detection
// ---------------------------------------------------------------------------

/// Whether the `cpitd` binary is resolvable on PATH.
///
/// Exposed so non-CLI callers (e.g. the sentinel cpitd source) can gate on
/// the binary's presence and degrade gracefully when it's absent.
pub fn cpitd_available() -> bool {
    find_cpitd()
}

fn find_cpitd() -> bool {
    Command::new("cpitd")
        .arg("--version")
        .output()
        .is_ok_and(|o| o.status.success())
}

fn suggest_install() -> Result<()> {
    bail!(
        "cpitd is not installed or not found in PATH.\n\n\
         cpitd (Copy Paste Is The Devil) is a language-agnostic code clone\n\
         detector that integrates with crosslink to track duplicated code\n\
         as issues.\n\n\
         Run `crosslink init --force` to auto-install, or install manually:\n\n\
         \x20 pip install cpitd\n\n\
         Or visit: https://github.com/scythia-marrow/cpitd"
    )
}

// ---------------------------------------------------------------------------
// Running cpitd
// ---------------------------------------------------------------------------

fn run_cpitd(paths: &[String], min_tokens: u32, ignore_patterns: &[String]) -> Result<CpitdOutput> {
    let mut cmd = Command::new("cpitd");

    if paths.is_empty() {
        cmd.arg(".");
    } else {
        for p in paths {
            cmd.arg(p);
        }
    }

    cmd.arg("--format").arg("json");
    cmd.arg("--min-tokens").arg(min_tokens.to_string());

    for pattern in ignore_patterns {
        cmd.arg("--ignore").arg(pattern);
    }

    let output = cmd.output().context("Failed to execute cpitd")?;

    let stdout = String::from_utf8(output.stdout).context("cpitd output is not valid UTF-8")?;

    if stdout.trim().is_empty() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        if stderr.trim().is_empty() {
            // cpitd exited 0 with no output means no clones in some edge cases
            return Ok(CpitdOutput {
                clone_reports: vec![],
                total_pairs: 0,
            });
        }
        bail!("cpitd produced no output. stderr: {}", stderr.trim());
    }

    serde_json::from_str(&stdout).context("Failed to parse cpitd JSON output")
}

// ---------------------------------------------------------------------------
// Deduplication
// ---------------------------------------------------------------------------

fn dedup_marker(file_a: &str, file_b: &str) -> String {
    let (a, b) = if file_a <= file_b {
        (file_a, file_b)
    } else {
        (file_b, file_a)
    };
    format!("<!-- cpitd:file_a={a}:file_b={b} -->")
}

fn find_existing_clone_issue(db: &Database, file_a: &str, file_b: &str) -> Result<Option<i64>> {
    let marker = dedup_marker(file_a, file_b);
    let issues = db.list_issues(Some("open"), Some("cpitd"), None)?;
    for issue in issues {
        if let Some(ref desc) = issue.description {
            if desc.contains(&marker) {
                return Ok(Some(issue.id));
            }
        }
    }
    Ok(None)
}

// ---------------------------------------------------------------------------
// Issue creation
// ---------------------------------------------------------------------------

fn shorten_path(path: &str) -> &str {
    std::path::Path::new(path)
        .file_name()
        .and_then(|f| f.to_str())
        .unwrap_or(path)
}

fn format_clone_description(report: &CpitdCloneReport) -> String {
    let marker = dedup_marker(&report.file_a, &report.file_b);

    let mut desc = format!(
        "{}\n\n\
         Detected code clones between:\n\
         - `{}`\n\
         - `{}`\n\n\
         Total cloned lines: {}\n\n\
         Clone groups:\n",
        marker, report.file_a, report.file_b, report.total_cloned_lines,
    );

    for (i, group) in report.groups.iter().enumerate() {
        let _ = writeln!(
            desc,
            "{}. Lines {}-{} <-> Lines {}-{} ({} lines, {} tokens)",
            i + 1,
            group.lines_a[0],
            group.lines_a[1],
            group.lines_b[0],
            group.lines_b[1],
            group.line_count,
            group.token_count,
        );
    }

    desc.push_str("\nConsider extracting shared logic into a common function or module.");
    desc
}

fn create_clone_issue(db: &Database, report: &CpitdCloneReport, quiet: bool) -> Result<i64> {
    let title = format!(
        "Code clone: {} <-> {} ({} lines)",
        shorten_path(&report.file_a),
        shorten_path(&report.file_b),
        report.total_cloned_lines,
    );

    let description = format_clone_description(report);
    let id = db.create_issue(&title, Some(&description), "low")?;
    db.add_label(id, "cpitd")?;
    db.add_label(id, "refactor")?;

    if !quiet {
        println!("  Created issue {}: {}", format_issue_id(id), title);
    }

    Ok(id)
}

fn relate_clone_issues(db: &Database, created: &[(i64, String, String)]) {
    let mut file_to_issues: HashMap<&str, Vec<i64>> = HashMap::new();
    for (id, file_a, file_b) in created {
        file_to_issues.entry(file_a).or_default().push(*id);
        file_to_issues.entry(file_b).or_default().push(*id);
    }
    for ids in file_to_issues.values() {
        for i in 0..ids.len() {
            for j in (i + 1)..ids.len() {
                // INTENTIONAL: relation may already exist — duplicate insert is harmless
                let _ = db.add_relation(ids[i], ids[j]);
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Public commands
// ---------------------------------------------------------------------------

/// Outcome of a clone scan: the crosslink issue ids that were created or
/// updated. `created` holds `(id, file_a, file_b)` so callers can relate or
/// surface newly-filed clones; `updated` holds ids that already existed and
/// got a rescan comment.
#[derive(Debug, Default)]
pub struct ScanOutcome {
    /// Newly created clone issues: `(issue_id, file_a, file_b)`.
    pub created: Vec<(i64, String, String)>,
    /// Existing clone issues that were re-confirmed and commented on.
    pub updated: Vec<i64>,
}

/// Core scan-and-file-issues logic, usable as a library function.
///
/// Shells to `cpitd`, parses its JSON, and files/updates crosslink clone
/// issues, returning the created/updated issue ids. Emits no progress output
/// (the CLI wrapper handles user-facing prints). The caller must ensure the
/// `cpitd` binary is present (see [`cpitd_available`]); if it is absent this
/// returns an error from the underlying command.
pub fn scan_and_file(
    db: &Database,
    paths: &[String],
    min_tokens: u32,
    ignore_patterns: &[String],
) -> Result<ScanOutcome> {
    let output = run_cpitd(paths, min_tokens, ignore_patterns)?;

    let mut outcome = ScanOutcome::default();

    for report in &output.clone_reports {
        if let Some(existing_id) = find_existing_clone_issue(db, &report.file_a, &report.file_b)? {
            let comment = format!(
                "[cpitd rescan] {} total cloned lines, {} group(s)",
                report.total_cloned_lines,
                report.groups.len(),
            );
            db.add_comment(existing_id, &comment, "note")?;
            outcome.updated.push(existing_id);
        } else {
            let id = create_clone_issue(db, report, true)?;
            outcome
                .created
                .push((id, report.file_a.clone(), report.file_b.clone()));
        }
    }

    if outcome.created.len() > 1 {
        relate_clone_issues(db, &outcome.created);
    }

    Ok(outcome)
}

pub fn scan(
    db: &Database,
    paths: &[String],
    min_tokens: u32,
    ignore_patterns: &[String],
    dry_run: bool,
    quiet: bool,
) -> Result<()> {
    if !find_cpitd() {
        return suggest_install();
    }

    if !quiet {
        println!("Running cpitd clone detection...");
    }

    if dry_run {
        let output = run_cpitd(paths, min_tokens, ignore_patterns)?;
        if output.clone_reports.is_empty() {
            if !quiet {
                println!("No code clones detected.");
            }
            return Ok(());
        }
        if !quiet {
            println!("Found {} clone pair(s).\n", output.total_pairs);
        }
        for report in &output.clone_reports {
            println!(
                "  Would create: {} <-> {} ({} lines, {} group(s))",
                report.file_a,
                report.file_b,
                report.total_cloned_lines,
                report.groups.len(),
            );
        }
        return Ok(());
    }

    let outcome = scan_and_file(db, paths, min_tokens, ignore_patterns)?;

    if outcome.created.is_empty() && outcome.updated.is_empty() {
        if !quiet {
            println!("No code clones detected.");
        }
        return Ok(());
    }

    if !quiet {
        for (id, _, _) in &outcome.created {
            // Title already printed by create_clone_issue when not quiet; but
            // scan_and_file runs quiet, so surface the created ids here.
            println!("  Created issue {}", format_issue_id(*id));
        }
        for id in &outcome.updated {
            println!(
                "  Updated issue {} (clone still present)",
                format_issue_id(*id)
            );
        }
        println!(
            "\ncpitd scan complete: {} created, {} updated",
            outcome.created.len(),
            outcome.updated.len()
        );
    }

    Ok(())
}

pub fn status(db: &Database) -> Result<()> {
    let issues = db.list_issues(Some("open"), Some("cpitd"), None)?;

    if issues.is_empty() {
        println!("No open cpitd clone issues.");
    } else {
        println!("{} open clone issue(s):\n", issues.len());
        for issue in &issues {
            println!("  {:<5} {}", format_issue_id(issue.id), issue.title);
        }
    }

    Ok(())
}

pub fn clear(db: &Database) -> Result<()> {
    let issues = db.list_issues(Some("open"), Some("cpitd"), None)?;

    if issues.is_empty() {
        println!("No open cpitd clone issues to close.");
        return Ok(());
    }

    let count = issues.len();
    for issue in &issues {
        db.close_issue(issue.id)?;
    }

    println!("Closed {count} cpitd clone issue(s).");
    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_parse_cpitd_json() {
        let json = r#"{
            "clone_reports": [
                {
                    "file_a": "src/foo.rs",
                    "file_b": "src/bar.rs",
                    "total_cloned_lines": 15,
                    "groups": [
                        {
                            "lines_a": [10, 24],
                            "lines_b": [30, 44],
                            "line_count": 15,
                            "token_count": 120
                        }
                    ]
                }
            ],
            "total_pairs": 1
        }"#;

        let output: CpitdOutput = serde_json::from_str(json).unwrap();
        assert_eq!(output.total_pairs, 1);
        assert_eq!(output.clone_reports.len(), 1);
        assert_eq!(output.clone_reports[0].file_a, "src/foo.rs");
        assert_eq!(output.clone_reports[0].file_b, "src/bar.rs");
        assert_eq!(output.clone_reports[0].total_cloned_lines, 15);
        assert_eq!(output.clone_reports[0].groups.len(), 1);
        assert_eq!(output.clone_reports[0].groups[0].lines_a, vec![10, 24]);
        assert_eq!(output.clone_reports[0].groups[0].line_count, 15);
        assert_eq!(output.clone_reports[0].groups[0].token_count, 120);
    }

    #[test]
    fn test_parse_cpitd_empty() {
        let json = r#"{"clone_reports": [], "total_pairs": 0}"#;
        let output: CpitdOutput = serde_json::from_str(json).unwrap();
        assert_eq!(output.total_pairs, 0);
        assert!(output.clone_reports.is_empty());
    }

    #[test]
    fn test_dedup_marker_commutative() {
        let m1 = dedup_marker("src/a.rs", "src/b.rs");
        let m2 = dedup_marker("src/b.rs", "src/a.rs");
        assert_eq!(m1, m2);
    }

    #[test]
    fn test_dedup_marker_contains_paths() {
        let marker = dedup_marker("foo.py", "bar.py");
        assert!(marker.contains("bar.py"));
        assert!(marker.contains("foo.py"));
        assert!(marker.starts_with("<!-- cpitd:"));
        assert!(marker.ends_with(" -->"));
    }

    #[test]
    fn test_shorten_path_basic() {
        assert_eq!(shorten_path("src/commands/foo.rs"), "foo.rs");
        assert_eq!(shorten_path("bar.py"), "bar.py");
        assert_eq!(shorten_path("a/b/c/d.txt"), "d.txt");
    }

    #[test]
    fn test_format_clone_description() {
        let report = CpitdCloneReport {
            file_a: "src/foo.rs".to_string(),
            file_b: "src/bar.rs".to_string(),
            total_cloned_lines: 10,
            groups: vec![CpitdCloneGroup {
                lines_a: vec![1, 10],
                lines_b: vec![20, 29],
                line_count: 10,
                token_count: 80,
            }],
        };
        let desc = format_clone_description(&report);
        assert!(desc.contains("<!-- cpitd:"));
        assert!(desc.contains("src/bar.rs"));
        assert!(desc.contains("src/foo.rs"));
        assert!(desc.contains("10"));
        assert!(desc.contains("80 tokens"));
    }
}