trusty-mpm 0.19.2

trusty-mpm: unified multi-agent orchestration platform (core, daemon, CLI, TUI, Telegram)
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
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//! Unit tests for `provisioner::workspace` (extracted to keep `workspace.rs`
//! under the 500-SLOC production cap after the #1935 base-checkout +
//! per-session worktree rework added `GitBackend::ensure_base_checkout` /
//! `GitBackend::worktree_add` plus their `FakeGitBackend` implementations).
//!
//! Why: splitting the test module into its own file (rather than trimming
//! doc comments or logic in the production code) keeps the Why/What/Test
//! documentation density in the production module intact while satisfying
//! the mechanical SLOC gate (`scripts/check_line_cap.sh`) — this file's
//! basename (`tests.rs`) classifies it under the 1500-SLOC test/benchmark cap.
//! What: `WorkspaceProvisioner`/`FakeGitBackend` unit tests covering path
//! isolation, session-id subdirectory naming, base-checkout reuse across
//! sessions, blank-ref handling, and TASK.md write/skip behaviour.
//! Test: this file IS the test module; run with `cargo test -p trusty-mpm`.

use super::base_lock::{LOCK_STALE_AFTER, lock_is_stale};
use super::*;
use tempfile::TempDir;

fn make_provisioner(root: &TempDir) -> WorkspaceProvisioner<FakeGitBackend> {
    // Skip the global `prepare_session` deploy: these tests verify path
    // isolation only and must not touch the shared `~/.claude/` tree.
    WorkspaceProvisioner::without_prepare(FakeGitBackend::new(), root.path().to_owned())
}

#[test]
fn repo_slug_extraction() {
    assert_eq!(
        repo_slug("https://github.com/owner/trusty-tools"),
        "trusty-tools"
    );
    assert_eq!(
        repo_slug("https://github.com/owner/trusty-tools.git"),
        "trusty-tools"
    );
    assert_eq!(repo_slug("git@github.com:owner/my-repo.git"), "my-repo");
}

#[test]
fn provisioner_isolation_path() {
    let root = TempDir::new().unwrap();
    let prov = make_provisioner(&root);
    let id = ManagedSessionId::new();

    let ws = prov
        .provision(&id, "https://github.com/owner/trusty-tools", "main", "task")
        .unwrap();

    // Path must be inside workspace_root, not the operator's project directory.
    assert!(ws.path.starts_with(root.path()));
    assert!(ws.path.to_string_lossy().contains("trusty-tools"));
    assert!(ws.path.to_string_lossy().contains(&id.to_string()));
}

#[test]
fn provisioner_path_not_in_existing_project() {
    // The workspace must NOT be inside any real project dir.
    // We simulate this by checking the path is inside workspace_root (a tempdir).
    let root = TempDir::new().unwrap();
    let prov = make_provisioner(&root);
    let id = ManagedSessionId::new();

    let ws = prov
        .provision(&id, "https://github.com/owner/myrepo.git", "feat/x", "task")
        .unwrap();

    // Must start with the mpm-owned workspace root, not any other path.
    assert!(ws.path.starts_with(root.path()));
    // Must not be equal to the workspace root itself.
    assert_ne!(&ws.path, root.path());
}

#[test]
fn provisioner_uses_session_id_subdir() {
    let root = TempDir::new().unwrap();
    let prov = make_provisioner(&root);
    let id = ManagedSessionId::new();

    let ws = prov
        .provision(&id, "https://github.com/owner/repo", "main", "task")
        .unwrap();

    // The leaf directory must be the session id.
    let leaf = ws.path.file_name().unwrap().to_string_lossy();
    assert_eq!(leaf.as_ref(), id.to_string());
}

#[test]
fn provision_in_uses_explicit_project_dir() {
    // The #1220 path: caller supplies a pre-resolved `<owner>/<repo>` project
    // dir. #1935: the session worktree nests under the project dir's shared
    // `.base/.worktrees/<session-id>/`, not directly under the project dir.
    let root = TempDir::new().unwrap();
    let prov = make_provisioner(&root);
    let id = ManagedSessionId::new();
    let project_dir = root.path().join("bobmatnyc").join("trusty-tools");

    let ws = prov
        .provision_in(
            &project_dir,
            &id,
            "https://github.com/bobmatnyc/trusty-tools",
            "main",
            "task",
        )
        .unwrap();

    // Path must be exactly <project_dir>/.base/.worktrees/<session-id> —
    // isolated under the project dir, nested under the shared base checkout.
    assert_eq!(
        ws.path,
        project_dir
            .join(".base")
            .join(".worktrees")
            .join(id.to_string())
    );
    assert!(ws.path.starts_with(&project_dir));
}

/// #1935: a second session provisioned for the SAME project must reuse the
/// existing base checkout (no re-clone) and land in its OWN worktree.
///
/// Why: this is the entire point of the fix — before #1935 every session
/// paid for an independent full clone; now only the FIRST session for a
/// project establishes the base checkout, and every subsequent session for
/// that project reuses it via `worktree_add`.
/// What: provisions two sessions against the same project dir, asserts (1)
/// `FakeGitBackend::ensure_base_checkout` observed exactly one call whose
/// `HEAD` marker was absent (the actual clone) — the second call is a no-op
/// because the marker now exists; (2) the two sessions get DISTINCT
/// worktree paths sharing the same `.base/` parent.
/// Test: this function IS the test.
#[test]
fn provision_reuses_base_checkout_across_sessions() {
    let root = TempDir::new().unwrap();
    let prov = make_provisioner(&root);
    let project_dir = root.path().join("owner").join("repo");

    let id1 = ManagedSessionId::new();
    let ws1 = prov
        .provision_in(
            &project_dir,
            &id1,
            "https://github.com/owner/repo",
            "main",
            "t1",
        )
        .unwrap();

    let id2 = ManagedSessionId::new();
    let ws2 = prov
        .provision_in(
            &project_dir,
            &id2,
            "https://github.com/owner/repo",
            "main",
            "t2",
        )
        .unwrap();

    let base_dir = project_dir.join(".base");
    // Both worktrees share the same base checkout directory...
    assert!(ws1.path.starts_with(&base_dir));
    assert!(ws2.path.starts_with(&base_dir));
    // ...but are otherwise distinct, isolated directories.
    assert_ne!(ws1.path, ws2.path);
    // The base checkout itself was established exactly once (idempotent
    // `HEAD` marker check inside `FakeGitBackend::ensure_base_checkout`).
    assert!(base_dir.join("HEAD").is_file());
}

#[test]
fn provisioner_records_repo_url_and_branch() {
    let root = TempDir::new().unwrap();
    let prov = make_provisioner(&root);
    let id = ManagedSessionId::new();

    let ws = prov
        .provision(
            &id,
            "https://github.com/owner/repo",
            "feat/my-branch",
            "task",
        )
        .unwrap();

    assert_eq!(ws.repo_url, "https://github.com/owner/repo");
    assert_eq!(ws.branch, "feat/my-branch");
}

/// Why: WI-A #1585 — when `LaunchParams::ref_` is absent the spawn path
/// forwards `git_ref = ""` to `spawn_managed`/`SpawnParams`. This test
/// locks in the contract that a blank `git_ref` is passed through to the
/// git backend as `""` AND that provision still succeeds (no early error).
/// #1935: the production fix now lives in `RealGitBackend::worktree_add`
/// (`provision_in` no longer calls `clone_repo` at all): when
/// `git_ref.trim().is_empty()` the fetch source falls back to `HEAD` (the
/// remote's default branch pointer) instead of an empty, invalid ref name —
/// mirroring the same blank-ref-tolerance contract `RealGitBackend::clone_repo`
/// still upholds for `content::catalog_sync`'s unrelated use of the same trait.
/// What: provisions with `git_ref = ""` and asserts (1) provision
/// succeeds, (2) the returned `branch` field is `""` (the provisioner does
/// not substitute a default) — the internal per-session branch name backing
/// the worktree is unrelated to this field, which always echoes the
/// REQUESTED ref verbatim.
/// Test: this is the test.
#[test]
fn blank_git_ref_omits_branch_flag() {
    let root = TempDir::new().unwrap();
    // Use FakeGitBackend via make_provisioner so we can read its call log.
    // make_provisioner returns a provisioner whose backend is a FakeGitBackend
    // but we cannot access it post-move. We build explicitly here so we can
    // inspect calls.
    let fake = FakeGitBackend::new();
    // SAFETY: the calls Mutex is shared across the borrow boundary through
    // raw pointers; instead, use a shared Arc<FakeGitBackend> — but since the
    // type does not impl Clone we verify the contract via ws.branch instead.
    let prov = WorkspaceProvisioner::without_prepare(FakeGitBackend::new(), root.path().to_owned());
    let id = ManagedSessionId::new();

    let ws = prov
        .provision(&id, "https://github.com/owner/repo", "", "task")
        .unwrap();

    // Provision must succeed: the provisioner does not reject a blank ref.
    assert!(ws.path.starts_with(root.path()), "workspace inside root");
    // The branch field records what was passed — blank — not a substituted default.
    // This pins the invariant that the provisioner passes "" through to the
    // backend verbatim; only `RealGitBackend::worktree_add` substitutes `HEAD`
    // as the actual fetch source when the ref is blank.
    assert_eq!(
        ws.branch, "",
        "blank ref must be stored as-is, not substituted"
    );
    drop(fake); // explicitly drop to silence unused-variable lint
}

/// Why: closes #1693 — the task description must be written to TASK.md in
/// the workspace root so the agent can read its brief without requiring
/// interactive input. This test locks in the write behaviour.
/// What: provisions with a non-empty task and asserts TASK.md exists and
/// contains exactly the task string.
/// Test: this is the test.
#[test]
fn provision_writes_task_md() {
    let root = TempDir::new().unwrap();
    let prov = make_provisioner(&root);
    let id = ManagedSessionId::new();
    let task = "Fix the authentication bug in the login flow";

    let ws = prov
        .provision(&id, "https://github.com/owner/repo", "main", task)
        .unwrap();

    let task_file = ws.path.join("TASK.md");
    assert!(
        task_file.exists(),
        "TASK.md must be written when task is non-empty"
    );
    let content = std::fs::read_to_string(&task_file).unwrap();
    assert_eq!(content, task, "TASK.md must contain the exact task text");
}

/// Why: closes #1693 — when no task is provided the workspace must NOT
/// receive an empty TASK.md (an empty file is misleading and wastes I/O).
/// What: provisions with an empty task string and asserts TASK.md is absent.
/// Test: this is the test.
#[test]
fn provision_skips_task_md_when_empty() {
    let root = TempDir::new().unwrap();
    let prov = make_provisioner(&root);
    let id = ManagedSessionId::new();

    let ws = prov
        .provision(&id, "https://github.com/owner/repo", "main", "")
        .unwrap();

    let task_file = ws.path.join("TASK.md");
    assert!(
        !task_file.exists(),
        "TASK.md must NOT be created when task is empty"
    );
}

// ── trusty-review PR #1936 (#1935) findings: RealGitBackend::ensure_base_checkout ──
//
// Both tests below exercise `RealGitBackend` against real local git
// repositories (no network required — `file://` remotes and local `git init`
// only), mirroring the graceful-skip-if-git-unavailable pattern already used
// by `decommission_worktree_tests.rs`.

/// Create a local bare "origin" repo with a single commit and return its path.
///
/// Why: shared fixture for the two `ensure_base_checkout` regression tests
/// below — both need a real, clonable bare repo to point `RealGitBackend` at.
/// What: `git init --bare`, then clones it to a scratch work dir, commits a
/// README, and pushes back to the bare repo. Returns `None` (callers must
/// skip, not fail) if the local `git` binary is unavailable, matching the
/// established pattern in `decommission_worktree_tests.rs`.
/// Test: exercised transitively by every test that calls it.
fn make_local_bare_origin(scratch: &TempDir) -> Option<PathBuf> {
    use std::process::Command;
    let bare = scratch.path().join("origin.git");
    let work = scratch.path().join("seed");
    if !Command::new("git")
        .args(["init", "--bare", "-b", "main"])
        .arg(&bare)
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
    {
        return None;
    }
    if !Command::new("git")
        .args(["clone"])
        .arg(&bare)
        .arg(&work)
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
    {
        return None;
    }
    std::fs::write(work.join("README.md"), "seed").unwrap();
    let work_s = work.to_str().unwrap();
    for args in [
        vec!["-C", work_s, "add", "."],
        vec![
            "-C",
            work_s,
            "-c",
            "user.email=t@t",
            "-c",
            "user.name=t",
            "commit",
            "-m",
            "seed",
        ],
        vec!["-C", work_s, "push", "origin", "main"],
    ] {
        if !Command::new("git")
            .args(&args)
            .status()
            .map(|s| s.success())
            .unwrap_or(false)
        {
            return None;
        }
    }
    Some(bare)
}

/// trusty-review finding #1 (TOCTOU race, PR #1936): two sessions
/// provisioning the SAME project for the first time must not fail when they
/// race on `ensure_base_checkout`.
///
/// Why: before the fix, both racing callers observed `base_dir.join("HEAD")`
/// absent, both attempted `git clone --bare`, and the loser hit git's
/// "destination path ... already exists and is not an empty directory"
/// error, surfacing as a hard `ProvisionError::Git` and failing that
/// session's provisioning outright.
/// What: spawns several real OS threads that all call
/// `RealGitBackend.ensure_base_checkout(repo_url, &base_dir)` concurrently
/// against the exact same, not-yet-existing `base_dir`, then asserts (1)
/// every thread returned `Ok(())` — no race loser propagates the "already
/// exists" error — and (2) exactly one genuinely established bare checkout
/// exists at `base_dir` afterward (`git rev-parse --is-bare-repository`
/// reports `true`). Skips gracefully if the local `git` binary is
/// unavailable.
/// Test: this function IS the test.
#[test]
fn ensure_base_checkout_recovers_from_concurrent_race() {
    let scratch = TempDir::new().unwrap();
    let Some(bare_origin) = make_local_bare_origin(&scratch) else {
        eprintln!("ensure_base_checkout_recovers_from_concurrent_race: git unavailable, skipping");
        return;
    };
    let repo_url = format!("file://{}", bare_origin.display());

    let root = TempDir::new().unwrap();
    let base_dir = root.path().join("project").join(".base");

    let handles: Vec<_> = (0..4)
        .map(|_| {
            let repo_url = repo_url.clone();
            let base_dir = base_dir.clone();
            std::thread::spawn(move || {
                RealGitBackend::default().ensure_base_checkout(&repo_url, &base_dir)
            })
        })
        .collect();

    for handle in handles {
        let result = handle.join().expect("thread must not panic");
        assert!(
            result.is_ok(),
            "every racing ensure_base_checkout call must recover and return Ok, got {result:?}"
        );
    }

    assert!(
        is_established_bare_checkout(&base_dir),
        "base_dir must be a genuinely established bare checkout after the race settles"
    );
}

/// trusty-review finding #2 (fragile bare-clone detection, PR #1936): a
/// stale NON-bare directory sitting at the base-checkout path must not be
/// silently accepted as a valid shared base.
///
/// Why: the previous idempotency guard (`base_dir.join("HEAD").is_file()`)
/// only checks for a file NAMED `HEAD` at the root of `base_dir` — it never
/// confirms that directory is actually a valid, complete git repository.
/// Verified empirically (see this test): a plain non-bare `git init`/`clone`
/// does NOT put a `HEAD` file at its OWN root (that lives at `.git/HEAD`
/// instead), but a directory that merely CONTAINS a stray file literally
/// named `HEAD` — e.g. left over from a `git clone --bare` that crashed
/// mid-clone (git writes `HEAD` early, before the rest of the object
/// database/refs), or any other stale/corrupt artifact occupying
/// `<project_dir>/.base/` — passes the old check and would be silently
/// treated as an established base, so cloning is skipped and a corrupt
/// directory is left as the "base"; later `git worktree add` /
/// `git fetch` calls against it then fail confusingly.
/// What: pre-creates `base_dir` containing ONLY a `HEAD` file (no `.git`,
/// no object database, no refs — the minimum needed to fool the OLD
/// file-existence check) and calls `RealGitBackend.ensure_base_checkout`
/// pointed at a real, unrelated bare origin. Asserts the call returns `Err`
/// (loud failure) rather than `Ok` (which would mean the corrupt directory
/// was silently treated as valid). Skips gracefully if the local `git`
/// binary is unavailable.
/// Test: this function IS the test.
#[test]
fn ensure_base_checkout_rejects_stale_non_bare_directory() {
    let scratch = TempDir::new().unwrap();
    let Some(bare_origin) = make_local_bare_origin(&scratch) else {
        eprintln!(
            "ensure_base_checkout_rejects_stale_non_bare_directory: git unavailable, skipping"
        );
        return;
    };
    let repo_url = format!("file://{}", bare_origin.display());

    let root = TempDir::new().unwrap();
    let base_dir = root.path().join("project").join(".base");
    std::fs::create_dir_all(&base_dir).unwrap();
    std::fs::write(base_dir.join("HEAD"), "ref: refs/heads/main\n").unwrap();

    assert!(
        base_dir.join("HEAD").is_file(),
        "sanity check: the stale directory has a file literally named HEAD"
    );
    assert!(
        !is_established_bare_checkout(&base_dir),
        "sanity check: a lone HEAD file with no repo structure must NOT read as bare"
    );

    let result = RealGitBackend::default().ensure_base_checkout(&repo_url, &base_dir);
    assert!(
        result.is_err(),
        "a stale non-repo directory must be rejected loudly, not silently reused: {result:?}"
    );
    // #1937 item 1: the error must be ACTIONABLE — name the exact path and the
    // `rm -rf` command the operator should run to allow re-provisioning.
    let msg = result.unwrap_err().to_string();
    assert!(
        msg.contains(&base_dir.display().to_string()),
        "error must name the exact stale base path, got: {msg}"
    );
    assert!(
        msg.contains("rm -rf"),
        "error must include the `rm -rf` recovery command, got: {msg}"
    );
}

/// #1937 item 3: `FakeGitBackend`'s idempotency/stale-detection must match
/// `RealGitBackend`'s so a fake-backend test catches the same stale-directory
/// condition a real-backend test would — the fidelity gap flagged in the
/// PR #1936 review.
///
/// Why: before this fix `FakeGitBackend::ensure_base_checkout` reused any
/// directory containing a stray `HEAD` file (the same superficial probe the
/// real backend abandoned), so a future author simulating a stale `.base` with
/// the fake would get a false-positive "already established" pass instead of
/// the loud rejection the real backend now produces. This test mirrors
/// `ensure_base_checkout_rejects_stale_non_bare_directory` against the fake.
/// What: pre-seeds `base_dir` with ONLY a stray `HEAD` file (no `config`
/// `bare = true` marker — the fake's stand-in for "not a valid bare checkout")
/// and asserts `FakeGitBackend::ensure_base_checkout` returns an actionable
/// `Err` naming the path and the `rm -rf` recovery command, exactly like the
/// real backend — not a silent `Ok` reuse.
/// Test: this function IS the test.
#[test]
fn fake_ensure_base_checkout_rejects_stale_non_bare_directory() {
    let root = TempDir::new().unwrap();
    let base_dir = root.path().join("project").join(".base");
    std::fs::create_dir_all(&base_dir).unwrap();
    // A lone HEAD file with no `config bare = true` marker is the fake's
    // stand-in for a stale, non-bare, mid-crash directory.
    std::fs::write(base_dir.join("HEAD"), "ref: refs/heads/main\n").unwrap();

    let fake = FakeGitBackend::new();
    let result = fake.ensure_base_checkout("https://github.com/owner/repo", &base_dir);
    assert!(
        result.is_err(),
        "fake must reject a stale non-bare directory loudly, not silently reuse it: {result:?}"
    );
    let msg = result.unwrap_err().to_string();
    assert!(
        msg.contains(&base_dir.display().to_string()) && msg.contains("rm -rf"),
        "fake error must be actionable (path + `rm -rf`), got: {msg}"
    );
}

/// #1937 item 3 (positive path): a FRESH `FakeGitBackend::ensure_base_checkout`
/// establishes a valid fake bare checkout, and a SECOND call reuses it
/// idempotently (no error, no clobber).
///
/// Why: locks in that the tightened validity check does not regress the
/// happy-path reuse contract — the fake must still recognise the base it just
/// wrote as established on the next call.
/// What: calls `ensure_base_checkout` twice against an empty base path; asserts
/// both return `Ok`, the second is recognised via `fake_is_established_bare_checkout`,
/// and the written markers (`HEAD` + `bare = true` `config`) are present.
/// Test: this function IS the test.
#[test]
fn fake_ensure_base_checkout_is_idempotent_on_valid_base() {
    let root = TempDir::new().unwrap();
    let base_dir = root.path().join("project").join(".base");
    let fake = FakeGitBackend::new();

    fake.ensure_base_checkout("https://github.com/owner/repo", &base_dir)
        .expect("first ensure must establish the fake base");
    assert!(
        super::base_lock::fake_is_established_bare_checkout(&base_dir),
        "a freshly-established fake base must read as a valid bare checkout"
    );

    // Second call must be an idempotent no-op reuse, not a stale rejection.
    fake.ensure_base_checkout("https://github.com/owner/repo", &base_dir)
        .expect("second ensure must reuse the established fake base");
    assert!(base_dir.join("HEAD").is_file());
    assert!(base_dir.join("config").is_file());
}

/// A lock marker abandoned by a crashed holder must not permanently deadlock
/// future base-checkout provisioning attempts.
///
/// Why: the base-checkout lock (added to fix trusty-review finding #1) is a
/// plain marker file, not a kernel-tracked `flock` — if the process holding
/// it is killed mid-clone, nothing ever runs its `Drop` impl to remove the
/// marker. Without stale-lock recovery, every future `ensure_base_checkout`
/// call for that project would wait out `LOCK_ACQUIRE_TIMEOUT` and then fail
/// forever, which is worse than the race it was meant to fix.
/// What: writes a lock marker file and backdates its modified time past
/// `LOCK_STALE_AFTER` via `File::set_modified` (no `filetime` dependency
/// needed — this has been in `std` since Rust 1.75, well under this
/// workspace's 1.91 MSRV). Asserts `lock_is_stale` reports it as stale, then
/// asserts `acquire_base_checkout_lock` recovers PROMPTLY (well under
/// `LOCK_ACQUIRE_TIMEOUT`) rather than blocking out the full timeout, and
/// that dropping the returned guard removes the marker file.
/// Test: this function IS the test.
#[test]
fn base_checkout_lock_recovers_stale_lock_marker() {
    let root = TempDir::new().unwrap();
    let lock_path = root.path().join(".base.lock");
    std::fs::write(&lock_path, b"").unwrap();

    let stale_time =
        std::time::SystemTime::now() - (LOCK_STALE_AFTER + std::time::Duration::from_secs(10));
    std::fs::File::options()
        .write(true)
        .open(&lock_path)
        .unwrap()
        .set_modified(stale_time)
        .unwrap();

    assert!(
        lock_is_stale(&lock_path),
        "a lock marker older than LOCK_STALE_AFTER must be considered stale"
    );

    let start = std::time::Instant::now();
    let guard = acquire_base_checkout_lock(&lock_path).expect("must recover from a stale lock");
    assert!(
        start.elapsed() < std::time::Duration::from_secs(5),
        "stale-lock recovery must not wait out the full acquire timeout"
    );

    drop(guard);
    assert!(
        !lock_path.exists(),
        "dropping the lock guard must remove the marker file"
    );
}

// ── #2184: RealGitBackend applies the resolved GitIdentity to every command ──

/// Why: a `RealGitBackend::default()` (no identity resolved) must build a
/// PLAIN `git` command — no env overrides, no `-c` args — so every existing
/// production call site (which constructs `RealGitBackend::default()` when it
/// has no project context) is byte-for-byte unaffected by #2184.
/// Test: itself.
#[test]
fn default_identity_produces_plain_git_command() {
    let backend = RealGitBackend::default();
    let cmd = backend.command();
    assert_eq!(
        cmd.get_args().count(),
        0,
        "no -c args for an empty identity"
    );
    assert_eq!(
        cmd.get_envs().count(),
        0,
        "no env overrides for an empty identity"
    );
}

/// Why: the resolved `GitIdentity::env` overrides must be applied to every
/// command this backend builds, so `git`/its credential helper authenticate
/// as the right per-project identity.
/// Test: itself.
#[test]
fn git_identity_env_applied_to_command() {
    let identity = crate::core::git_identity::GitIdentity {
        env: vec![("GH_CONFIG_DIR".to_string(), "/cfg/project".to_string())],
        commit_name: None,
        commit_email: None,
    };
    let backend = RealGitBackend::new(identity);
    let cmd = backend.command();
    let envs: Vec<_> = cmd.get_envs().collect();
    assert!(
        envs.iter().any(|(k, v)| {
            *k == std::ffi::OsStr::new("GH_CONFIG_DIR")
                && *v == Some(std::ffi::OsStr::new("/cfg/project"))
        }),
        "GH_CONFIG_DIR override must be applied: {envs:?}"
    );
}

/// Why: a resolved commit-identity override must render as `-c user.name=…`/
/// `-c user.email=…` BEFORE any subcommand arg (git only accepts `-c`
/// overrides in that position).
/// Test: itself.
#[test]
fn git_identity_commit_args_applied_to_command() {
    let identity = crate::core::git_identity::GitIdentity {
        env: vec![],
        commit_name: Some("Bot".to_string()),
        commit_email: Some("bot@example.com".to_string()),
    };
    let backend = RealGitBackend::new(identity);
    let cmd = backend.command();
    let args: Vec<&std::ffi::OsStr> = cmd.get_args().collect();
    assert_eq!(
        args,
        vec![
            std::ffi::OsStr::new("-c"),
            std::ffi::OsStr::new("user.name=Bot"),
            std::ffi::OsStr::new("-c"),
            std::ffi::OsStr::new("user.email=bot@example.com"),
        ]
    );
}