nomograph-kit 0.7.0

Verified tool registry manager -- manages developer toolchains from git-based registries
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
//! Phase 3: Apply evaluated updates to registry TOML files.
//!
//! Reads evaluated.json from Phase 2. For approved updates:
//! 1. Load the tool's TOML file from tools/<name>.toml
//! 2. Update version and inline checksums using toml_edit (preserve formatting)
//! 3. Write the file back
//! 4. Git add, commit, push to a new branch
//! 5. Create MR via glab CLI

use std::collections::HashMap;
use std::path::Path;
use std::process::Command;

use anyhow::{Context, Result};

use super::EvaluateOutput;

/// Run the apply phase.
///
/// Reads `input` (evaluated.json), applies approved updates to the
/// tool TOML files in the current working directory, then creates
/// a branch and MR.
pub fn apply(input: &Path) -> Result<()> {
    let content = std::fs::read_to_string(input)
        .with_context(|| format!("failed to read {}", input.display()))?;
    let data: EvaluateOutput =
        serde_json::from_str(&content).context("failed to parse evaluated.json")?;

    // Filter to approved updates
    let to_apply: Vec<_> = data
        .evaluated
        .iter()
        .filter(|e| e.evaluation == "auto-approved" || e.evaluation == "approve")
        .collect();

    let flagged: Vec<_> = data
        .evaluated
        .iter()
        .filter(|e| e.evaluation == "flag")
        .collect();

    let rejected: Vec<_> = data
        .evaluated
        .iter()
        .filter(|e| e.evaluation == "reject")
        .collect();

    // Apply approved AND flagged updates -- the MR is the review surface.
    // Only rejected items (actual supply chain risk) are excluded.
    let all_to_apply: Vec<_> = to_apply.iter().chain(flagged.iter()).collect();

    eprintln!("kit apply: {} updates to apply ({} approved, {} flagged for review)",
        all_to_apply.len(), to_apply.len(), flagged.len());
    if !rejected.is_empty() {
        eprintln!("  rejected (excluded): {}", rejected.len());
    }

    if all_to_apply.is_empty() {
        eprintln!("Nothing to apply");
        return Ok(());
    }

    // Apply each update to its TOML file
    let mut applied_names: Vec<String> = Vec::new();

    for update in &all_to_apply {
        let name = &update.candidate.name;
        let old_version = &update.candidate.current_version;
        let new_version = &update.candidate.new_version;
        let tool_path = Path::new("tools").join(format!("{name}.toml"));

        if !tool_path.exists() {
            eprintln!("  warning: {name}.toml not found, skipping");
            continue;
        }

        eprintln!("  applying {name}: {old_version} -> {new_version}");

        // Use toml_edit for surgical updates that preserve formatting
        let raw = std::fs::read_to_string(&tool_path)
            .with_context(|| format!("failed to read {}", tool_path.display()))?;

        let updated = update_tool_toml(&raw, new_version, &update.candidate.checksums)
            .with_context(|| format!("failed to update {}", tool_path.display()))?;

        match updated {
            Some(new_content) => {
                std::fs::write(&tool_path, new_content)
                    .with_context(|| format!("failed to write {}", tool_path.display()))?;
            }
            None => {
                eprintln!("  warning: no [tool] table in {name}.toml, skipping");
                continue;
            }
        }

        applied_names.push(name.clone());
    }

    if applied_names.is_empty() {
        eprintln!("No files were modified");
        return Ok(());
    }

    // Create branch, commit, push, create MR
    let now = chrono::Utc::now();
    let branch = format!("kit/update-{}", now.format("%Y%m%d-%H%M%S"));
    let today = now.format("%Y-%m-%d").to_string();

    // Create and switch to the branch (delete if it exists from a previous run)
    let _ = run_git(&["branch", "-D", &branch]);
    run_git(&["checkout", "-b", &branch])?;

    // Stage all modified tool files
    for name in &applied_names {
        let path = format!("tools/{name}.toml");
        run_git(&["add", &path])?;
    }

    // Build commit message
    let mut commit_lines = vec![format!("kit: tool updates {today}")];
    commit_lines.push(String::new());
    for update in &all_to_apply {
        if applied_names.contains(&update.candidate.name) {
            commit_lines.push(format!(
                "- {}: {} -> {}",
                update.candidate.name,
                update.candidate.current_version,
                update.candidate.new_version
            ));
        }
    }
    commit_lines.push(String::new());
    commit_lines.push("AI-Assisted: yes".to_string());
    commit_lines.push("AI-Tools: kit CI".to_string());

    let commit_msg = commit_lines.join("\n");
    run_git(&["commit", "-m", &commit_msg])?;

    // Push the branch
    run_git(&["push", "-u", "origin", &branch])?;

    // Build MR description -- full audit trail
    let mut mr_body = String::from("## Tool Updates\n\n");

    if !to_apply.is_empty() {
        mr_body.push_str("### Approved\n\n");
        mr_body.push_str("| Tool | Version | Checksum | Evaluation |\n");
        mr_body.push_str("|------|---------|----------|------------|\n");
        for update in &to_apply {
            if applied_names.contains(&update.candidate.name) {
                let checksum_status = format_checksum_status(&update.candidate);
                mr_body.push_str(&format!(
                    "| **{}** | {} -> {} | {} | {} |\n",
                    update.candidate.name,
                    update.candidate.current_version,
                    update.candidate.new_version,
                    checksum_status,
                    update.evaluation,
                ));
            }
        }
        mr_body.push('\n');

        // Detail section with reasoning
        mr_body.push_str("<details>\n<summary>Approval details</summary>\n\n");
        for update in &to_apply {
            if applied_names.contains(&update.candidate.name) {
                mr_body.push_str(&format!("**{}**\n", update.candidate.name));
                if let Some(ref reason) = update.eval_reason {
                    mr_body.push_str(&format!("- Reason: {reason}\n"));
                }
                if let Some(ref note) = update.candidate.note {
                    mr_body.push_str(&format!("- Note: {note}\n"));
                }
                // Checksum detail per platform
                for (platform, verified) in &update.candidate.verified {
                    let status = match verified {
                        Some(true) => "verified",
                        Some(false) => "MISMATCH",
                        None => "unavailable",
                    };
                    mr_body.push_str(&format!("- {platform}: {status}\n"));
                }
                mr_body.push('\n');
            }
        }
        mr_body.push_str("</details>\n\n");
    }

    if !flagged.is_empty() {
        mr_body.push_str("### Flagged for Review\n\n");
        mr_body.push_str("| Tool | Version | Reason |\n");
        mr_body.push_str("|------|---------|--------|\n");
        for f in &flagged {
            mr_body.push_str(&format!(
                "| **{}** | {} -> {} | {} |\n",
                f.candidate.name,
                f.candidate.current_version,
                f.candidate.new_version,
                f.eval_reason.as_deref().unwrap_or("needs review")
            ));
        }
        mr_body.push('\n');

        if flagged.iter().any(|f| !f.review_reasons.is_empty()) {
            mr_body.push_str("<details>\n<summary>Review details</summary>\n\n");
            for f in &flagged {
                mr_body.push_str(&format!("**{}**\n", f.candidate.name));
                for reason in &f.review_reasons {
                    mr_body.push_str(&format!("- {reason}\n"));
                }
                if let Some(ref eval) = f.eval_reason {
                    mr_body.push_str(&format!("- LLM assessment: {eval}\n"));
                }
                mr_body.push('\n');
            }
            mr_body.push_str("</details>\n\n");
        }
    }

    if !rejected.is_empty() {
        mr_body.push_str("### Rejected\n\n");
        mr_body.push_str("| Tool | Version | Reason |\n");
        mr_body.push_str("|------|---------|--------|\n");
        for r in &rejected {
            mr_body.push_str(&format!(
                "| **{}** | {} -> {} | {} |\n",
                r.candidate.name,
                r.candidate.current_version,
                r.candidate.new_version,
                r.eval_reason.as_deref().unwrap_or("rejected")
            ));
        }
        mr_body.push('\n');
    }

    mr_body.push_str("\n---\n*Generated by kit CI (sense/respond/verify pipeline)*\n");

    // Create MR -- try glab first, fall back to API via CI_JOB_TOKEN
    let mr_title = format!("kit: tool updates {today}");

    let glab_ok = Command::new("glab")
        .args([
            "mr",
            "create",
            "--title",
            &mr_title,
            "--description",
            &mr_body,
            "--source-branch",
            &branch,
            "--remove-source-branch",
            "--yes",
        ])
        .status()
        .map(|s| s.success())
        .unwrap_or(false);

    if !glab_ok {
        // Fallback: create MR via API using CI_JOB_TOKEN (works in CI without GITLAB_TOKEN)
        if let (Ok(api_url), Ok(project_id), Ok(token)) = (
            std::env::var("CI_API_V4_URL"),
            std::env::var("CI_PROJECT_ID"),
            std::env::var("CI_JOB_TOKEN"),
        ) {
            let mr_json = serde_json::json!({
                "source_branch": branch,
                "target_branch": "main",
                "title": mr_title,
                "description": mr_body,
                "remove_source_branch": true,
            });

            let client = reqwest::blocking::Client::builder()
                .https_only(true)
                .timeout(std::time::Duration::from_secs(30))
                .build()
                .context("failed to create HTTP client")?;

            match client
                .post(format!("{api_url}/projects/{project_id}/merge_requests"))
                .header("JOB-TOKEN", &token)
                .json(&mr_json)
                .send()
            {
                Ok(resp) if resp.status().is_success() => {
                    eprintln!("MR created via API: {mr_title}");
                }
                Ok(resp) => {
                    eprintln!(
                        "warning: API MR creation failed (HTTP {})",
                        resp.status()
                    );
                    eprintln!("  branch {branch} was pushed -- create MR manually");
                }
                Err(e) => {
                    eprintln!("warning: API MR creation failed ({e})");
                    eprintln!("  branch {branch} was pushed -- create MR manually");
                }
            }
        } else {
            eprintln!("warning: glab mr create failed and no CI_JOB_TOKEN available");
            eprintln!("  branch {branch} was pushed -- create MR manually");
        }
    } else {
        eprintln!("MR created: {mr_title}");
    }

    eprintln!("\n{}", "=".repeat(60));
    eprintln!("Applied: {}", applied_names.len());
    for name in &applied_names {
        eprintln!("  {name}");
    }

    Ok(())
}

/// Update a tool TOML string with a new version and checksums.
///
/// Returns `Some(new_content)` on success, or `None` if the TOML has no
/// `[tool]` table.
fn update_tool_toml(
    raw: &str,
    new_version: &str,
    checksums: &HashMap<String, Option<String>>,
) -> Result<Option<String>> {
    let mut doc = raw
        .parse::<toml_edit::DocumentMut>()
        .context("failed to parse TOML")?;

    let tool_table = match doc.get_mut("tool").and_then(|t| t.as_table_mut()) {
        Some(t) => t,
        None => return Ok(None),
    };

    tool_table["version"] = toml_edit::value(new_version);

    // Update inline checksums from the check phase.
    // Filter to only platforms that have a computed hash (Some).
    let new_hashes: Vec<_> = checksums
        .iter()
        .filter_map(|(platform, sha)| {
            sha.as_ref().map(|hash| (platform.as_str(), hash.as_str()))
        })
        .collect();

    if !new_hashes.is_empty() {
        // Create the [tool.checksums] table if it doesn't exist yet.
        if tool_table.get("checksums").is_none() {
            tool_table.insert(
                "checksums",
                toml_edit::Item::Table(toml_edit::Table::new()),
            );
        }

        if let Some(checksums_table) = tool_table
            .get_mut("checksums")
            .and_then(|t| t.as_table_mut())
        {
            for (platform, hash) in &new_hashes {
                checksums_table[*platform] = toml_edit::value(*hash);
            }
        }
    }

    Ok(Some(doc.to_string()))
}

/// Format the checksum verification status for MR display.
fn format_checksum_status(candidate: &super::UpdateCandidate) -> String {
    if candidate.verified.is_empty() {
        return "no checksums".to_string();
    }
    let verified = candidate
        .verified
        .values()
        .filter(|v| **v == Some(true))
        .count();
    let total = candidate.verified.len();
    if verified == total {
        format!("{verified}/{total} verified")
    } else {
        let failed = candidate
            .verified
            .values()
            .filter(|v| **v == Some(false))
            .count();
        if failed > 0 {
            format!("{verified}/{total} verified, {failed} MISMATCH")
        } else {
            format!("{verified}/{total} verified")
        }
    }
}

/// Run a git command in the current directory.
fn run_git(args: &[&str]) -> Result<()> {
    let output = Command::new("git")
        .args(args)
        .output()
        .with_context(|| format!("failed to execute git {}", args.first().unwrap_or(&"")))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!(
            "git {} failed (exit {}): {}",
            args.first().unwrap_or(&""),
            output.status,
            stderr.trim()
        );
    }

    Ok(())
}

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

    #[test]
    fn update_version_and_existing_checksums() {
        let input = r#"[tool]
name = "muxr"
version = "0.6.0"

[tool.checksums]
macos-arm64 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
linux-x64 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
"#;

        let checksums = HashMap::from([
            (
                "macos-arm64".to_string(),
                Some("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc".to_string()),
            ),
            (
                "linux-x64".to_string(),
                Some("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd".to_string()),
            ),
        ]);

        let result = update_tool_toml(input, "0.7.0", &checksums).unwrap().unwrap();

        assert!(result.contains("version = \"0.7.0\""), "version should be updated");
        assert!(
            result.contains("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"),
            "macos checksum should be updated"
        );
        assert!(
            !result.contains("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
            "old macos checksum should be replaced"
        );
    }

    #[test]
    fn create_checksums_table_when_missing() {
        let input = r#"[tool]
name = "muxr"
version = "0.6.0"
source = "gitlab"
"#;

        let checksums = HashMap::from([(
            "macos-arm64".to_string(),
            Some("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee".to_string()),
        )]);

        let result = update_tool_toml(input, "0.7.0", &checksums).unwrap().unwrap();

        assert!(result.contains("version = \"0.7.0\""), "version should be updated");
        assert!(
            result.contains("[tool.checksums]"),
            "checksums table should be created"
        );
        assert!(
            result.contains("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"),
            "checksum should be written"
        );
    }

    #[test]
    fn skip_checksums_when_empty() {
        let input = r#"[tool]
name = "claude-code"
version = "1.0.0"
source = "npm"
"#;

        let checksums = HashMap::new();

        let result = update_tool_toml(input, "1.1.0", &checksums).unwrap().unwrap();

        assert!(result.contains("version = \"1.1.0\""), "version should be updated");
        assert!(
            !result.contains("checksums"),
            "no checksums table should be created when map is empty"
        );
    }

    #[test]
    fn skip_none_checksums() {
        let input = r#"[tool]
name = "test"
version = "1.0.0"
"#;

        // Only None values -- download failed for all platforms
        let checksums = HashMap::from([
            ("macos-arm64".to_string(), None),
            ("linux-x64".to_string(), None),
        ]);

        let result = update_tool_toml(input, "1.1.0", &checksums).unwrap().unwrap();

        assert!(result.contains("version = \"1.1.0\""), "version should be updated");
        assert!(
            !result.contains("checksums"),
            "no checksums table when all values are None"
        );
    }

    #[test]
    fn returns_none_without_tool_table() {
        let input = r#"[something]
key = "value"
"#;

        let result = update_tool_toml(input, "1.0.0", &HashMap::new()).unwrap();
        assert!(result.is_none(), "should return None without [tool] table");
    }

    #[test]
    fn preserves_formatting_and_other_fields() {
        let input = r#"[tool]
name = "gh"
description = "GitHub CLI"
version = "2.89.0"
source = "github"
repo = "cli/cli"
tier = "high"

[tool.checksums]
macos-arm64 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"#;

        let checksums = HashMap::from([(
            "macos-arm64".to_string(),
            Some("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff".to_string()),
        )]);

        let result = update_tool_toml(input, "2.90.0", &checksums).unwrap().unwrap();

        assert!(result.contains("name = \"gh\""), "name preserved");
        assert!(result.contains("description = \"GitHub CLI\""), "description preserved");
        assert!(result.contains("source = \"github\""), "source preserved");
        assert!(result.contains("repo = \"cli/cli\""), "repo preserved");
        assert!(result.contains("tier = \"high\""), "tier preserved");
        assert!(result.contains("version = \"2.90.0\""), "version updated");
        assert!(
            result.contains("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
            "checksum updated"
        );
    }
}