heddle-cli 0.2.2

An AI-native version control system
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
// SPDX-License-Identifier: Apache-2.0
//! Coverage for item 3.2 of the heddle 6→8 plan: `heddle attempt N -- <cmd>`.
//!
//! Exercises:
//!   1. `heddle attempt 1 -- true` — the degenerate case (equivalent
//!      to `heddle try`). One attempt, succeeds, recommends itself,
//!      parent unchanged.
//!   2. `heddle attempt 3 -- true` — three parallel successes; ranking
//!      picks one; the other two stay around.
//!   3. `heddle attempt 3 -- false` — three failures; all dropped;
//!      no recommendation.
//!   4. Mixed: one branch succeeds, another fails — ranking puts the
//!      success first; failure dropped.
//!   5. `--evaluate "false"` after primary success — surfaces as
//!      `evaluate_failed` and ranks below pure successes.
//!   6. `N > 10` rejected with a clear error.
//!   7. `--name-prefix my-test` → threads named `my-test-1`, ….
//!   8. `--shared-target` (Rust workspace default-on) doesn't error
//!      and produces N attempts.

use std::fs;

use serde_json::Value;
use tempfile::TempDir;

use super::{heddle, heddle_output};

/// Bootstrap a minimal repo with a single capture so the parent has a
/// HEAD. Same shape as the `try_cmd.rs` setup so the two suites stay
/// comparable.
fn setup_repo() -> TempDir {
    let temp = TempDir::new().unwrap();
    heddle(&["init"], Some(temp.path())).unwrap();
    fs::write(temp.path().join("base.txt"), "base\n").unwrap();
    heddle(&["capture", "-m", "init"], Some(temp.path())).unwrap();
    temp
}

/// Resolve the parent thread's HEAD via `heddle log --json` so we
/// observe through the same path an agent would.
fn parent_head(repo: &std::path::Path) -> String {
    let raw = heddle(&["--output", "json", "log", "--limit", "1"], Some(repo)).unwrap();
    let value: Value = serde_json::from_str(&raw).unwrap();
    value["states"][0]["change_id_full"]
        .as_str()
        .or_else(|| value["states"][0]["change_id"].as_str())
        .unwrap()
        .to_string()
}

/// Capture the parent's worktree top-level files. Used to confirm the
/// invariant that `heddle attempt` never touches the parent's files.
fn worktree_snapshot(repo: &std::path::Path) -> Vec<(String, Vec<u8>)> {
    let mut out = Vec::new();
    for entry in fs::read_dir(repo).unwrap().flatten() {
        let path = entry.path();
        let name = entry.file_name();
        let name_str = name.to_string_lossy().to_string();
        if name_str.starts_with('.') {
            continue;
        }
        if path.is_file() {
            out.push((name_str, fs::read(&path).unwrap()));
        }
    }
    out.sort();
    out
}

#[test]
fn attempt_n_one_is_degenerate_try() {
    let temp = setup_repo();
    let head_before = parent_head(temp.path());
    let worktree_before = worktree_snapshot(temp.path());

    let raw = heddle(
        &["--output", "json", "attempt", "1", "--", "true"],
        Some(temp.path()),
    )
    .expect("heddle attempt 1 -- true should succeed");
    let value: Value = serde_json::from_str(&raw).expect("output should be JSON");

    assert_eq!(value["status"], "completed", "raw: {raw}");
    assert_eq!(value["attempts_total"], 1);
    assert_eq!(value["attempts_succeeded"], 1);
    assert!(value["recommended"].is_string(), "raw: {raw}");
    assert!(
        value["next_action"]
            .as_str()
            .unwrap()
            .contains("heddle merge"),
        "next_action should suggest a merge verb: {raw}"
    );

    // Parent invariants.
    assert_eq!(head_before, parent_head(temp.path()));
    assert_eq!(worktree_before, worktree_snapshot(temp.path()));
}

#[test]
fn attempt_three_parallel_successes_produce_ranking() {
    let temp = setup_repo();
    let head_before = parent_head(temp.path());

    let raw = heddle(
        &["--output", "json", "attempt", "3", "--", "true"],
        Some(temp.path()),
    )
    .expect("attempt 3 -- true should succeed");
    let value: Value = serde_json::from_str(&raw).unwrap();

    assert_eq!(value["status"], "completed", "raw: {raw}");
    assert_eq!(value["attempts_total"], 3);
    assert_eq!(value["attempts_succeeded"], 3);

    let recommended = value["recommended"].as_str().expect("recommended set");
    assert!(
        recommended.starts_with("attempt-"),
        "recommended should be a generated attempt-* name (got {recommended})"
    );

    // Ranking should have rank-1 = recommended.
    let attempts = value["attempts"].as_array().unwrap();
    assert_eq!(attempts.len(), 3);
    assert_eq!(
        attempts[0]["thread"].as_str().unwrap(),
        recommended,
        "rank-1 attempt should equal the recommendation"
    );

    // Parent HEAD must not move.
    assert_eq!(head_before, parent_head(temp.path()));

    // The two non-recommended attempts should still exist as Active
    // threads (we only drop failures).
    let list_raw = heddle(&["--output", "json", "thread", "list"], Some(temp.path())).unwrap();
    let list: Value = serde_json::from_str(&list_raw).unwrap();
    let active_attempts: Vec<&str> = list["threads"]
        .as_array()
        .unwrap()
        .iter()
        .filter(|t| t["thread_state"] == "active")
        .filter_map(|t| t["name"].as_str())
        .filter(|n| n.starts_with("attempt-"))
        .collect();
    assert_eq!(
        active_attempts.len(),
        3,
        "all three success attempts should survive cleanup; got {active_attempts:?}"
    );
}

#[test]
fn attempt_all_failures_drops_all_and_yields_no_recommendation() {
    let temp = setup_repo();
    let head_before = parent_head(temp.path());
    let worktree_before = worktree_snapshot(temp.path());

    // `false` exits non-zero; the entire command returns 0 from
    // heddle's perspective (it printed a structured "no winner"
    // result), so we use heddle(...) which checks the outer exit
    // code rather than heddle_output.
    let raw = heddle(
        &["--output", "json", "attempt", "3", "--", "false"],
        Some(temp.path()),
    )
    .expect("attempt 3 -- false: outer cmd should still exit 0 (it's a structured no-win)");
    let value: Value = serde_json::from_str(&raw).unwrap();

    assert_eq!(value["status"], "failed", "raw: {raw}");
    assert_eq!(value["attempts_total"], 3);
    assert_eq!(value["attempts_succeeded"], 0);
    assert_eq!(value["attempts_dropped"], 3);
    assert!(
        value["recommended"].is_null(),
        "no recommendation on all-fail: {raw}"
    );

    // Parent invariants.
    assert_eq!(head_before, parent_head(temp.path()));
    assert_eq!(worktree_before, worktree_snapshot(temp.path()));

    // No active attempt-* threads should remain.
    let list_raw = heddle(&["--output", "json", "thread", "list"], Some(temp.path())).unwrap();
    let list: Value = serde_json::from_str(&list_raw).unwrap();
    let active_attempts: Vec<&str> = list["threads"]
        .as_array()
        .unwrap()
        .iter()
        .filter(|t| t["thread_state"] == "active")
        .filter_map(|t| t["name"].as_str())
        .filter(|n| n.starts_with("attempt-"))
        .collect();
    assert!(
        active_attempts.is_empty(),
        "all failed attempts should be dropped; got {active_attempts:?}"
    );
}

#[test]
fn attempt_mixed_success_and_failure_ranks_success_first() {
    let temp = setup_repo();

    // Build a script that flips behaviour based on which thread's
    // checkout it's running in. Each attempt's checkout sits at
    // `<...>-heddle-threads/<thread-name>/root`, so the parent
    // directory's basename is the thread name (`mixed-1`, `mixed-2`,
    // …). We succeed on threads whose name ends in `-1` and fail on
    // the rest. With N=3 that yields exactly one success and two
    // failures.
    let script_dir = TempDir::new().unwrap();
    let script_path = script_dir.path().join("mixed.sh");
    fs::write(
        &script_path,
        "#!/bin/sh\nset -e\n\
        thread_name=$(basename \"$(dirname \"$(pwd)\")\")\n\
        case \"$thread_name\" in\n  *-1) printf 'win\\n' > out.txt; exit 0 ;;\n  *) exit 1 ;;\nesac\n",
    )
    .unwrap();
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&script_path).unwrap().permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&script_path, perms).unwrap();
    }

    let raw = heddle(
        &[
            "--output",
            "json",
            "attempt",
            "3",
            "--name-prefix",
            "mixed",
            "--",
            "sh",
            script_path.to_str().unwrap(),
        ],
        Some(temp.path()),
    )
    .expect("mixed attempt outer should succeed");
    let value: Value = serde_json::from_str(&raw).unwrap();

    assert_eq!(value["status"], "completed", "raw: {raw}");
    assert_eq!(value["attempts_total"], 3);
    assert_eq!(value["attempts_succeeded"], 1);
    assert_eq!(value["attempts_dropped"], 2);

    let recommended = value["recommended"].as_str().expect("recommendation");
    assert!(
        recommended.ends_with("-1"),
        "the only succeeding attempt is the one ending in -1; got {recommended}"
    );

    // Rank-1 is the recommended attempt.
    let attempts = value["attempts"].as_array().unwrap();
    assert_eq!(attempts[0]["thread"].as_str().unwrap(), recommended);
    assert_eq!(attempts[0]["status"], "succeeded");
    // The remaining two are failures.
    for failed in &attempts[1..] {
        assert_eq!(failed["status"], "failed");
    }
}

#[test]
fn attempt_n_zero_is_rejected() {
    let temp = setup_repo();
    let output = heddle_output(
        &["--output", "json", "attempt", "0", "--", "true"],
        Some(temp.path()),
    )
    .unwrap();
    assert!(!output.status.success(), "N=0 should fail");
    let stderr = std::str::from_utf8(&output.stderr).unwrap_or("");
    assert!(stderr.contains("at least 1"), "stderr: {stderr}");
}

#[test]
fn attempt_n_eleven_is_rejected_with_clear_error() {
    let temp = setup_repo();
    let output = heddle_output(
        &["--output", "json", "attempt", "11", "--", "true"],
        Some(temp.path()),
    )
    .unwrap();
    assert!(!output.status.success(), "N>10 should fail");
    let stderr = std::str::from_utf8(&output.stderr).unwrap_or("");
    assert!(
        stderr.contains("capped at 10"),
        "expected the cap error; stderr: {stderr}"
    );
}

#[test]
fn attempt_evaluate_failure_ranks_below_clean_success() {
    let temp = setup_repo();

    // We need at least one "primary success + evaluate failure" attempt
    // to verify ranking. Use N=1 with `--evaluate "false"`; expectation
    // is `attempts_succeeded == 0`, `recommended` is the
    // EvaluateFailed attempt (fallback path), and the message reflects
    // "no clean wins".
    let raw = heddle(
        &[
            "--output",
            "json",
            "attempt",
            "1",
            "--evaluate",
            "false",
            "--",
            "true",
        ],
        Some(temp.path()),
    )
    .expect("evaluate-failure outer should still exit 0");
    let value: Value = serde_json::from_str(&raw).unwrap();

    assert_eq!(value["attempts_total"], 1);
    assert_eq!(value["attempts_succeeded"], 0);
    let attempts = value["attempts"].as_array().unwrap();
    assert_eq!(attempts[0]["status"], "evaluate_failed");
    // We still surface a recommendation as a fallback ("primary worked,
    // tests didn't"), so the user has something to inspect.
    assert!(
        value["recommended"].is_string(),
        "fallback recommendation expected when evaluate fails: {raw}"
    );
}

#[test]
fn attempt_with_explicit_name_prefix_uses_that_prefix() {
    let temp = setup_repo();
    let raw = heddle(
        &[
            "--output",
            "json",
            "attempt",
            "2",
            "--name-prefix",
            "my-prefix",
            "--",
            "true",
        ],
        Some(temp.path()),
    )
    .expect("attempt with --name-prefix should succeed");
    let value: Value = serde_json::from_str(&raw).unwrap();
    let attempts = value["attempts"].as_array().unwrap();
    let names: Vec<&str> = attempts
        .iter()
        .filter_map(|a| a["thread"].as_str())
        .collect();
    assert!(
        names.contains(&"my-prefix-1"),
        "expected my-prefix-1; got {names:?}"
    );
    assert!(
        names.contains(&"my-prefix-2"),
        "expected my-prefix-2; got {names:?}"
    );
}

#[test]
fn attempt_shared_target_default_on_for_rust_workspace() {
    // Construct a Rust-workspace-shaped repo: a `Cargo.toml` at the
    // root is the trigger. We don't need a real cargo workspace; the
    // attempt code only checks for the file's existence.
    let temp = setup_repo();
    fs::write(
        temp.path().join("Cargo.toml"),
        "[workspace]\nmembers = []\n",
    )
    .unwrap();

    let raw = heddle(
        &["--output", "json", "attempt", "2", "--", "true"],
        Some(temp.path()),
    )
    .expect("attempt 2 against a fake Rust workspace should succeed");
    let value: Value = serde_json::from_str(&raw).unwrap();
    assert_eq!(value["attempts_total"], 2);
    assert_eq!(value["attempts_succeeded"], 2);

    // The thread checkouts should each contain a `.cargo/config.toml`
    // that redirects to the shared target dir. The thread path layout
    // for `--workspace heavy` is
    // `<repo_parent>/.<repo_name>-heddle-threads/<thread>/root/`; we
    // assert the cargo config exists there.
    let attempts = value["attempts"].as_array().unwrap();
    let repo_parent = temp.path().parent().expect("temp dir has a parent");
    let repo_basename = temp
        .path()
        .file_name()
        .and_then(|n| n.to_str())
        .expect("temp dir has a basename");
    let threads_dir = repo_parent.join(format!(".{repo_basename}-heddle-threads"));
    for attempt in attempts {
        let name = attempt["thread"].as_str().unwrap();
        let cargo_config = threads_dir
            .join(name)
            .join("root")
            .join(".cargo/config.toml");
        assert!(
            cargo_config.is_file(),
            "expected .cargo/config.toml redirect under attempt thread '{name}' at {}",
            cargo_config.display()
        );
        // Spot-check the redirect target: it should mention the
        // shared target dir under `.heddle/targets/<fingerprint>`.
        let body = fs::read_to_string(&cargo_config).unwrap();
        assert!(
            body.contains(".heddle/targets/") || body.contains("[build]"),
            "cargo config didn't look like a shared-target redirect: {body}"
        );
    }
}