provenance-cli 0.2.3

Command-line interface for Provenance requirements traceability
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
use assert_cmd::Command;
use predicates::prelude::*;
use provenance_macros::verifies;
use std::collections::BTreeSet;
use std::path::PathBuf;

#[test]
fn skills_list_and_show_embedded_skill_files() {
    let skills = workspace_skill_files();

    let output = Command::cargo_bin("provenance")
        .unwrap()
        .args(["skills", "list", "--format", "json"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let listed: Vec<serde_json::Value> = serde_json::from_slice(&output).unwrap();
    let listed_names = listed
        .iter()
        .map(|skill| skill["name"].as_str().unwrap().to_string())
        .collect::<BTreeSet<_>>();

    assert_eq!(listed_names, skills.keys().cloned().collect());
    assert!(listed.iter().all(|skill| skill["description"]
        .as_str()
        .is_some_and(|description| !description.is_empty())));

    for (name, contents) in skills {
        Command::cargo_bin("provenance")
            .unwrap()
            .args(["skills", "show", &name])
            .assert()
            .success()
            .stdout(contents);
    }
}

#[test]
fn embedded_skills_include_turn_based_provenance_shaping_skill() {
    Command::cargo_bin("provenance")
        .unwrap()
        .args(["skills", "list", "--format", "json"])
        .assert()
        .success()
        .stdout(predicate::str::contains(r#""name": "provenance-shaping""#));

    Command::cargo_bin("provenance")
        .unwrap()
        .args(["skills", "show", "provenance-shaping"])
        .assert()
        .success()
        .stdout(predicate::str::contains("LAND-AS-YOU-GO"))
        .stdout(predicate::str::contains("Chart"))
        .stdout(predicate::str::contains("Work"));
}

#[test]
fn skills_install_default_writes_canonical_files_and_relative_claude_symlinks() {
    let dir = tempfile::tempdir().unwrap();
    let skill = "provenance-fork-tournament";

    install(dir.path(), &[])
        .success()
        .stdout(predicate::str::contains(r#""link_mode": "symlink""#));

    let canonical = dir
        .path()
        .join(".agents/skills")
        .join(skill)
        .join("SKILL.md");
    let canonical_contents = std::fs::read_to_string(&canonical).unwrap();
    assert!(canonical_contents.starts_with("---\nname: provenance-fork-tournament"));
    assert!(canonical_contents.contains(&format!(
        "Installed by provenance {}",
        env!("CARGO_PKG_VERSION")
    )));
    assert!(canonical_contents.contains("content hash fnv1a64:"));

    let link = dir.path().join(".claude/skills").join(skill);
    let link_metadata = std::fs::symlink_metadata(&link).unwrap();
    assert!(link_metadata.file_type().is_symlink());
    assert_eq!(
        std::fs::read_link(&link).unwrap(),
        PathBuf::from("../../.agents/skills/provenance-fork-tournament")
    );
    assert_eq!(
        std::fs::read_to_string(link.join("SKILL.md")).unwrap(),
        canonical_contents
    );
}

#[test]
fn fork_tournament_skill_documents_the_assertable_winner_lifecycle() {
    let skill = include_str!("../skills/provenance-fork-tournament/SKILL.md");

    assert!(skill.contains("--supporting-claim-id claim_<question>_<slot>"));
    assert!(skill.contains(r#""proposal_id":"prop_<question>_<slot>""#));
    assert!(skill.contains("provenance proposals assert --scope <scope>"));
    assert!(skill.contains("--id assertion_<question>_<winner_slot>"));
    assert!(skill.contains("--resolve-human-gate"));
    assert!(skill.contains(r#""decision_key":"pick_<question>_winner""#));
    assert!(skill.contains("--decision-key pick_<question>_winner"));
}

#[test]
fn skills_install_copy_flag_copies_claude_skills_instead_of_symlinking() {
    let dir = tempfile::tempdir().unwrap();
    let skill = "provenance-swarm-backtrace";

    install(dir.path(), &["--copy"])
        .success()
        .stdout(predicate::str::contains(r#""link_mode": "copy""#));

    let canonical = dir
        .path()
        .join(".agents/skills")
        .join(skill)
        .join("SKILL.md");
    let copied = dir
        .path()
        .join(".claude/skills")
        .join(skill)
        .join("SKILL.md");
    assert!(canonical.exists());
    assert!(copied.exists());
    assert!(!std::fs::symlink_metadata(copied.parent().unwrap())
        .unwrap()
        .file_type()
        .is_symlink());
    assert_eq!(
        std::fs::read_to_string(copied).unwrap(),
        std::fs::read_to_string(canonical).unwrap()
    );
}

#[test]
#[cfg(unix)]
#[verifies("rule_install_never_clobbers", examples)]
fn skills_install_copy_replaces_own_symlink_but_foreign_symlink_requires_force() {
    let dir = tempfile::tempdir().unwrap();
    let skill = "provenance-shaping";
    let link = dir.path().join(".claude/skills").join(skill);

    // Default install, then --copy: our own canonical symlink is replaced
    // with a real directory without needing --force.
    install(dir.path(), &[]).success();
    install(dir.path(), &["--copy"]).success();
    let metadata = std::fs::symlink_metadata(&link).unwrap();
    assert!(metadata.is_dir());
    assert!(!metadata.file_type().is_symlink());

    // A foreign symlink is not silently destroyed.
    std::fs::remove_dir_all(&link).unwrap();
    std::os::unix::fs::symlink("../../elsewhere", &link).unwrap();
    install(dir.path(), &["--copy"])
        .failure()
        .stderr(predicate::str::contains("rerun with --force"));
    assert_eq!(
        std::fs::read_link(&link).unwrap(),
        PathBuf::from("../../elsewhere")
    );

    install(dir.path(), &["--copy", "--force"]).success();
    assert!(std::fs::symlink_metadata(&link).unwrap().is_dir());
}

/// A plain file where a skill directory belongs: the symlink path and the
/// copy path both refuse it, and `--force` is what gets past it. The user's
/// file survives every refusal.
#[test]
#[verifies("rule_install_never_clobbers", examples)]
fn skills_install_refuses_a_file_where_a_skill_directory_belongs() {
    let dir = tempfile::tempdir().unwrap();
    let occupied = dir.path().join(".claude/skills/provenance-fork-tournament");
    std::fs::create_dir_all(occupied.parent().unwrap()).unwrap();
    std::fs::write(&occupied, "not a skill directory\n").unwrap();

    for arguments in [vec![], vec!["--copy"]] {
        install(dir.path(), &arguments)
            .failure()
            .stderr(predicate::str::contains(
                "exists and is not a skill directory; rerun with --force to overwrite",
            ));
        assert_eq!(
            std::fs::read_to_string(&occupied).unwrap(),
            "not a skill directory\n"
        );
    }

    install(dir.path(), &["--force"]).success();
    assert!(std::fs::metadata(&occupied).unwrap().is_dir());
    assert!(occupied.join("SKILL.md").exists());
}

/// The run-level `status` is read out of the parsed report rather than
/// matched anywhere in the output, so a per-file entry carrying the same word
/// cannot stand in for it.
#[test]
#[verifies("rule_install_never_clobbers", examples)]
#[verifies("rule_install_run_status", examples)]
fn skills_install_is_idempotent_and_requires_force_for_canonical_drift() {
    let dir = tempfile::tempdir().unwrap();
    let installed = dir
        .path()
        .join(".agents/skills/provenance-fork-tournament/SKILL.md");

    assert_eq!(install_status(dir.path(), &[]), "installed");
    assert_eq!(install_status(dir.path(), &[]), "unchanged");

    std::fs::write(&installed, "local edit\n").unwrap();
    install(dir.path(), &[])
        .failure()
        .stderr(predicate::str::contains("exists and differs"));

    assert_eq!(install_status(dir.path(), &["--force"]), "updated");
}

#[test]
fn skills_install_global_uses_home_agents_and_claude_skill_dirs() {
    let home = tempfile::tempdir().unwrap();
    let cwd = tempfile::tempdir().unwrap();

    Command::cargo_bin("provenance")
        .unwrap()
        .current_dir(cwd.path())
        .env("HOME", home.path())
        .args([
            "skills", "install", "--global", "--copy", "--format", "json",
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains(r#""global": true"#))
        .stdout(predicate::str::contains(r#""link_mode": "copy""#));

    assert!(home
        .path()
        .join(".agents/skills/provenance-grounded-writing/SKILL.md")
        .exists());
    assert!(home
        .path()
        .join(".claude/skills/provenance-grounded-writing/SKILL.md")
        .exists());
    assert!(!cwd
        .path()
        .join(".agents/skills/provenance-grounded-writing/SKILL.md")
        .exists());
}

#[test]
fn init_does_not_write_an_agents_md_skills_section() {
    let temp = tempfile::tempdir().unwrap();
    let repo = temp.path().join("repo");

    init_repo(&repo);

    let agents = repo.join("AGENTS.md");
    if agents.exists() {
        let contents = std::fs::read_to_string(agents).unwrap();
        assert!(!contents.contains("<!-- BEGIN PROVENANCE SKILLS -->"));
    }
}

#[test]
fn prime_reports_skill_install_status_and_install_command() {
    let temp = tempfile::tempdir().unwrap();
    let repo = temp.path().join("repo");
    init_repo(&repo);
    std::fs::remove_file(repo.join(".agents/skills/provenance-shaping/SKILL.md")).unwrap();

    Command::cargo_bin("provenance")
        .unwrap()
        .args([
            "prime",
            "--repo",
            repo.to_str().unwrap(),
            "--scope",
            "default",
            "--format",
            "json",
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains(r#""skills""#))
        .stdout(predicate::str::contains(r#""installed": false"#))
        .stdout(predicate::str::contains("provenance skills install"));

    install(&repo, &["--copy"]).success();

    Command::cargo_bin("provenance")
        .unwrap()
        .args([
            "prime",
            "--repo",
            repo.to_str().unwrap(),
            "--scope",
            "default",
            "--format",
            "json",
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains(r#""installed": true"#));
}

#[test]
fn install_status_uses_canonical_agents_skill_files_as_source_of_truth() {
    let temp = tempfile::tempdir().unwrap();
    let repo = temp.path().join("repo");
    init_repo(&repo);

    install(&repo, &["--copy"]).success();
    std::fs::remove_file(repo.join(".agents/skills/provenance-fork-tournament/SKILL.md")).unwrap();
    assert!(repo
        .join(".claude/skills/provenance-fork-tournament/SKILL.md")
        .exists());

    Command::cargo_bin("provenance")
        .unwrap()
        .args([
            "prime",
            "--repo",
            repo.to_str().unwrap(),
            "--scope",
            "default",
            "--format",
            "json",
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains(r#""installed": false"#))
        .stdout(predicate::str::contains("provenance-fork-tournament"));
}

#[test]
fn shaping_and_ideation_commands_emit_suppressible_skill_install_hint() {
    let temp = tempfile::tempdir().unwrap();
    let repo = temp.path().join("repo");
    init_repo(&repo);
    std::fs::remove_file(repo.join(".agents/skills/provenance-shaping/SKILL.md")).unwrap();

    Command::cargo_bin("provenance")
        .unwrap()
        .args([
            "questions",
            "list",
            "--repo",
            repo.to_str().unwrap(),
            "--scope",
            "default",
            "--format",
            "json",
        ])
        .assert()
        .success()
        .stderr(predicate::str::contains(
            "hint: provenance skills are not installed; run `provenance skills install`",
        ));

    Command::cargo_bin("provenance")
        .unwrap()
        .args([
            "proposals",
            "list",
            "--repo",
            repo.to_str().unwrap(),
            "--scope",
            "default",
            "--format",
            "json",
        ])
        .assert()
        .success()
        .stderr(predicate::str::contains(
            "hint: provenance skills are not installed; run `provenance skills install`",
        ));

    Command::cargo_bin("provenance")
        .unwrap()
        .args([
            "--quiet",
            "questions",
            "list",
            "--repo",
            repo.to_str().unwrap(),
            "--scope",
            "default",
            "--format",
            "json",
        ])
        .assert()
        .success()
        .stderr(predicate::str::is_empty());
}

/// Runs `provenance skills install` in `dir`, always asking for the JSON
/// report so a caller can read the run out of it.
fn install(dir: &std::path::Path, arguments: &[&str]) -> assert_cmd::assert::Assert {
    Command::cargo_bin("provenance")
        .unwrap()
        .current_dir(dir)
        .args(["skills", "install"])
        .args(arguments)
        .args(["--format", "json"])
        .assert()
}

/// The run-level `status` field of a successful install's JSON report.
fn install_status(dir: &std::path::Path, arguments: &[&str]) -> String {
    let output = install(dir, arguments)
        .success()
        .get_output()
        .stdout
        .clone();
    let report: serde_json::Value = serde_json::from_slice(&output).unwrap();
    report["status"].as_str().unwrap().to_string()
}

fn init_repo(repo: &std::path::Path) {
    Command::cargo_bin("provenance")
        .unwrap()
        .args([
            "init",
            "--path",
            repo.to_str().unwrap(),
            "--scope",
            "default",
            "--path-prefix",
            ".",
        ])
        .assert()
        .success();
}

fn workspace_skill_files() -> std::collections::BTreeMap<String, String> {
    let workspace = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .to_path_buf();
    let mut skills = std::collections::BTreeMap::new();
    for entry in std::fs::read_dir(workspace.join("skills")).unwrap() {
        let entry = entry.unwrap();
        if !entry.file_type().unwrap().is_dir() {
            continue;
        }
        let name = entry.file_name().to_string_lossy().to_string();
        let contents = std::fs::read_to_string(entry.path().join("SKILL.md")).unwrap();
        skills.insert(name, contents);
    }
    skills
}