cflx 0.6.327

Conflux – a spec-driven parallel coding orchestrator that runs AI agents on git worktrees
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
//! Repository-level tests for the transient final Apply commit lock retry.
//!
//! These exercise real Git processes, a real `pre-commit` hook, and real
//! `index.lock` files against a temporary repository, so they are
//! integration-scoped evidence rather than unit evidence: they prove the
//! classifier recognises the lock message and lock identity Git actually
//! produces, and that both finalization paths recover while still running
//! repository verification. No in-memory double can establish either.

use std::fs;
use std::path::{Path, PathBuf};
use std::time::Instant;

use tempfile::TempDir;
use tokio_util::sync::CancellationToken;

use super::test_support::{LockReleasingEnvironment, LOCK_SENTINEL};
use super::{
    run_final_commit_with_retry, FinalCommitEnvironment, GitFinalCommitEnvironment,
    FINAL_COMMIT_MAX_ATTEMPTS, FINAL_COMMIT_RETRY_DELAY,
};
use crate::config::OrchestratorConfig;
use crate::execution::apply::{create_final_commit, create_final_commit_with_environment};
use crate::vcs::git::commands::run_git;
use crate::vcs::git::GitWorkspaceManager;
use crate::vcs::{VcsBackend, VcsError, VerifiedCommitOutcome};

const CHANGE_ID: &str = "demo-change";

fn expected_commit_message() -> String {
    format!("Apply: {}", CHANGE_ID)
}

async fn git_ok(repo: &Path, args: &[&str]) {
    run_git(args, repo)
        .await
        .unwrap_or_else(|error| panic!("git {:?} failed: {}", args, error));
}

/// A repository whose `pre-commit` hook records every invocation, so "the
/// commit still ran repository verification" is observable.
struct LockRepo {
    _temp_dir: TempDir,
    path: PathBuf,
    hook_log: PathBuf,
    worktrees_dir: PathBuf,
}

impl LockRepo {
    /// Initialize a repository with one base commit and a counting hook.
    async fn init() -> Self {
        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("repo");
        fs::create_dir_all(&path).unwrap();

        git_ok(&path, &["init", "-q", "-b", "main"]).await;
        git_ok(&path, &["config", "user.email", "test@example.com"]).await;
        git_ok(&path, &["config", "user.name", "Test User"]).await;
        git_ok(&path, &["config", "commit.gpgsign", "false"]).await;

        // The hook lives inside `.git` so `git add -A` never stages its log.
        let hooks_dir = path.join(".git").join("hooks");
        fs::create_dir_all(&hooks_dir).unwrap();
        let hook = hooks_dir.join("pre-commit");
        fs::write(
            &hook,
            "#!/bin/sh\necho ran >> \"$(git rev-parse --git-dir)/hook.log\"\nexit 0\n",
        )
        .unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            fs::set_permissions(&hook, fs::Permissions::from_mode(0o755)).unwrap();
        }

        fs::write(path.join("base.txt"), "base\n").unwrap();
        git_ok(&path, &["add", "-A"]).await;
        git_ok(&path, &["commit", "-q", "--no-verify", "-m", "base"]).await;

        Self {
            hook_log: path.join(".git").join("hook.log"),
            worktrees_dir: temp_dir.path().join("worktrees"),
            path,
            _temp_dir: temp_dir,
        }
    }

    /// Leave uncommitted apply output, selecting the add-and-commit path.
    fn write_apply_output(&self) {
        fs::write(self.path.join("applied.txt"), "apply output\n").unwrap();
    }

    /// Record a WIP snapshot so the worktree is clean, selecting the amend path.
    async fn snapshot_apply_output(&self) {
        self.write_apply_output();
        git_ok(&self.path, &["add", "-A"]).await;
        git_ok(
            &self.path,
            &[
                "commit",
                "-q",
                "--no-verify",
                "--allow-empty",
                "-m",
                "WIP: demo-change (1/1 tasks, apply#1)",
            ],
        )
        .await;
    }

    fn workspace_manager(&self) -> GitWorkspaceManager {
        GitWorkspaceManager::new(
            self.worktrees_dir.clone(),
            self.path.clone(),
            1,
            OrchestratorConfig::default(),
        )
    }

    fn lock_path(&self) -> PathBuf {
        self.path.join(".git").join("index.lock")
    }

    /// Hold the managed worktree's index lock, as a competing Git process does.
    fn hold_index_lock(&self) -> PathBuf {
        let lock = self.lock_path();
        fs::write(&lock, LOCK_SENTINEL).unwrap();
        lock
    }

    fn hook_runs(&self) -> usize {
        fs::read_to_string(&self.hook_log)
            .map(|log| log.lines().count())
            .unwrap_or(0)
    }

    async fn commit_subjects(&self) -> Vec<String> {
        run_git(&["log", "--format=%s"], &self.path)
            .await
            .expect("git log")
            .lines()
            .map(str::to_string)
            .collect()
    }

    async fn final_commit_count(&self) -> usize {
        self.commit_subjects()
            .await
            .iter()
            .filter(|subject| *subject == &expected_commit_message())
            .count()
    }

    async fn head_file_names(&self) -> String {
        run_git(&["show", "--name-only", "--format=", "HEAD"], &self.path)
            .await
            .expect("git show")
    }

    async fn is_clean(&self) -> bool {
        run_git(&["status", "--porcelain"], &self.path)
            .await
            .expect("git status")
            .is_empty()
    }
}

/// Fabricate the contention error Git reports for this repository, so an
/// already-committed attempt can be made to look ambiguous.
async fn fabricated_lock_error(repo: &Path, command: &str) -> VcsError {
    let git_dir = run_git(&["rev-parse", "--absolute-git-dir"], repo)
        .await
        .expect("git dir");
    let stderr = format!(
        "fatal: Unable to create '{}/index.lock': File exists.\n\n\
         Another git process seems to be running in this repository.\n",
        git_dir
    );
    VcsError::Command {
        backend: VcsBackend::Git,
        message: format!("{} failed: {}", command, stderr),
        command: Some(command.to_string()),
        working_dir: Some(repo.to_path_buf()),
        stderr: Some(stderr),
        stdout: Some(String::new()),
    }
}

// ---- recovery on both finalization paths --------------------------------

#[cfg_attr(windows, ignore)]
#[tokio::test]
async fn final_apply_commit_lock_recovers_add_and_commit_when_contention_clears() {
    let repo = LockRepo::init().await;
    repo.write_apply_output();
    let environment = LockReleasingEnvironment::holding(repo.lock_path());

    let outcome = create_final_commit_with_environment(
        &repo.workspace_manager(),
        &repo.path,
        CHANGE_ID,
        None,
        &environment,
        None,
    )
    .await
    .expect("finalization must recover once contention clears");

    assert_eq!(outcome, VerifiedCommitOutcome::Committed);
    assert_eq!(
        environment.sleeps(),
        vec![FINAL_COMMIT_RETRY_DELAY],
        "the first attempt must have hit real contention and waited once"
    );
    assert!(
        environment.lock_was_untouched(),
        "the finalization retry policy must never delete or rewrite a lock it does not own"
    );
    assert!(
        !repo.lock_path().exists(),
        "the competing process released its own lock"
    );
    assert_eq!(
        repo.final_commit_count().await,
        1,
        "exactly one final Apply commit: {:?}",
        repo.commit_subjects().await
    );
    assert!(
        repo.head_file_names().await.contains("applied.txt"),
        "the apply output must land in the final commit"
    );
    assert!(
        repo.is_clean().await,
        "no workspace content may be left out"
    );
    assert!(
        repo.hook_runs() >= 1,
        "the recovered final commit must run repository verification"
    );
}

#[cfg_attr(windows, ignore)]
#[tokio::test]
async fn final_apply_commit_lock_recovers_amend_when_contention_clears() {
    let repo = LockRepo::init().await;
    repo.snapshot_apply_output().await;
    let hooks_before = repo.hook_runs();
    let environment = LockReleasingEnvironment::holding(repo.lock_path());

    let outcome = create_final_commit_with_environment(
        &repo.workspace_manager(),
        &repo.path,
        CHANGE_ID,
        None,
        &environment,
        None,
    )
    .await
    .expect("amend finalization must recover once contention clears");

    assert_eq!(outcome, VerifiedCommitOutcome::Committed);
    assert_eq!(
        environment.sleeps(),
        vec![FINAL_COMMIT_RETRY_DELAY],
        "the first attempt must have hit real contention and waited once"
    );
    assert!(
        environment.lock_was_untouched(),
        "the finalization retry policy must never delete or rewrite a lock it does not own"
    );
    assert!(!repo.lock_path().exists());
    assert_eq!(
        repo.commit_subjects().await,
        vec![expected_commit_message(), "base".to_string()],
        "the WIP snapshot must be replaced by exactly one final Apply commit"
    );
    assert!(repo.head_file_names().await.contains("applied.txt"));
    assert!(repo.is_clean().await);
    assert!(
        repo.hook_runs() > hooks_before,
        "the recovered amend must run repository verification"
    );
}

// ---- exhaustion, cancellation, and ineligible failures -------------------

#[cfg_attr(windows, ignore)]
#[tokio::test]
async fn final_apply_commit_lock_exhaustion_preserves_workspace_and_lock() {
    let repo = LockRepo::init().await;
    repo.write_apply_output();
    let lock = repo.hold_index_lock();

    let started = Instant::now();
    let error = create_final_commit(&repo.workspace_manager(), &repo.path, CHANGE_ID)
        .await
        .expect_err("a lock held past the retry budget must fail");

    assert!(
        started.elapsed() >= FINAL_COMMIT_RETRY_DELAY * (FINAL_COMMIT_MAX_ATTEMPTS - 1),
        "five attempts must be separated by four fixed delays"
    );
    let VcsError::Command {
        message,
        command,
        working_dir,
        stderr,
        ..
    } = &error
    else {
        panic!("expected a command error, got {:?}", error);
    };
    assert!(
        message.contains(&format!(
            "attempt {}/{}",
            FINAL_COMMIT_MAX_ATTEMPTS, FINAL_COMMIT_MAX_ATTEMPTS
        )),
        "message must report the retry outcome: {}",
        message
    );
    assert_eq!(command.as_deref(), Some("git add -A"));
    assert_eq!(working_dir.as_deref(), Some(repo.path.as_path()));
    assert!(stderr.as_deref().unwrap_or_default().contains("index.lock"));

    assert_eq!(
        fs::read_to_string(&lock).unwrap(),
        LOCK_SENTINEL,
        "the finalization retry policy must never delete or rewrite a live lock"
    );
    assert_eq!(
        fs::read_to_string(repo.path.join("applied.txt")).unwrap(),
        "apply output\n",
        "workspace contents must survive for explicit recovery"
    );
    assert_eq!(repo.final_commit_count().await, 0);
}

#[cfg_attr(windows, ignore)]
#[tokio::test]
async fn final_apply_commit_lock_cancellation_stops_retrying() {
    let repo = LockRepo::init().await;
    repo.write_apply_output();
    let lock = repo.hold_index_lock();
    let cancel_token = CancellationToken::new();
    cancel_token.cancel();

    let error = create_final_commit_with_environment(
        &repo.workspace_manager(),
        &repo.path,
        CHANGE_ID,
        Some(&cancel_token),
        &GitFinalCommitEnvironment,
        None,
    )
    .await
    .expect_err("a cancelled retry must fail");

    assert!(
        error
            .to_string()
            .contains("cancellation observed before the retry delay"),
        "error must explain the suppressed retry: {}",
        error
    );
    assert!(lock.exists());
    assert_eq!(repo.final_commit_count().await, 0);
}

#[cfg_attr(windows, ignore)]
#[tokio::test]
async fn final_apply_commit_lock_does_not_retry_another_worktrees_lock() {
    let repo = LockRepo::init().await;
    let other = LockRepo::init().await;
    repo.write_apply_output();
    let mut attempts = 0_u32;

    let error = run_final_commit_with_retry(
        |_attempt| {
            attempts += 1;
            let repo_path = repo.path.clone();
            let other_git_dir = other.path.join(".git");
            async move {
                let stderr = format!(
                    "fatal: Unable to create '{}/index.lock': File exists.\n",
                    other_git_dir.display()
                );
                Err(VcsError::Command {
                    backend: VcsBackend::Git,
                    message: format!("git add -A failed: {}", stderr),
                    command: Some("git add -A".to_string()),
                    working_dir: Some(repo_path),
                    stderr: Some(stderr),
                    stdout: Some(String::new()),
                })
            }
        },
        &GitFinalCommitEnvironment,
        &repo.path,
        &expected_commit_message(),
        None,
    )
    .await
    .expect_err("another worktree's lock is not this finalization's contention");

    assert_eq!(attempts, 1, "an ineligible lock must not be retried");
    assert!(
        !error.to_string().contains("Final Apply commit failed on"),
        "a terminal failure keeps the original error: {}",
        error
    );
}

#[cfg_attr(windows, ignore)]
#[tokio::test]
async fn final_apply_commit_lock_does_not_retry_malformed_or_permission_lock_output() {
    let repo = LockRepo::init().await;
    repo.write_apply_output();
    let git_dir = run_git(&["rev-parse", "--absolute-git-dir"], &repo.path)
        .await
        .expect("git dir");

    for stderr in [
        // Permission failure on this repository's own lock path.
        format!("fatal: Unable to create '{git_dir}/index.lock': Permission denied\n"),
        // Truncated/malformed lock prose for the same path.
        format!("fatal: Unable to create '{git_dir}/index.lock\n"),
        format!("fatal: cannot lock '{git_dir}/index.lock': File exists.\n"),
    ] {
        let mut attempts = 0_u32;
        let error = run_final_commit_with_retry(
            |_attempt| {
                attempts += 1;
                let repo_path = repo.path.clone();
                let stderr = stderr.clone();
                async move {
                    Err(VcsError::Command {
                        backend: VcsBackend::Git,
                        message: format!("git add -A failed: {}", stderr),
                        command: Some("git add -A".to_string()),
                        working_dir: Some(repo_path),
                        stderr: Some(stderr),
                        stdout: Some(String::new()),
                    })
                }
            },
            &GitFinalCommitEnvironment,
            &repo.path,
            &expected_commit_message(),
            None,
        )
        .await
        .expect_err("only an existing-index.lock failure is contention");

        assert_eq!(attempts, 1, "must not retry: {stderr}");
        assert!(
            !error.to_string().contains("Final Apply commit failed on"),
            "a terminal failure keeps the original error: {error}"
        );
    }
    assert_eq!(repo.final_commit_count().await, 0);
}

#[cfg_attr(windows, ignore)]
#[tokio::test]
async fn final_apply_commit_lock_does_not_retry_configuration_failure() {
    let repo = LockRepo::init().await;
    repo.write_apply_output();
    // A representative non-lock Git failure: the commit stage cannot build an
    // author identity.
    git_ok(&repo.path, &["config", "user.email", ""]).await;
    git_ok(&repo.path, &["config", "user.name", ""]).await;

    let error = create_final_commit(&repo.workspace_manager(), &repo.path, CHANGE_ID)
        .await
        .expect_err("an identity failure must be terminal");

    // Any retried failure ends in the annotated exhaustion error, so its
    // absence proves the identity failure was never retried.
    assert!(
        !error.to_string().contains("Final Apply commit failed on"),
        "a terminal failure keeps the original error: {}",
        error
    );
    assert_eq!(repo.final_commit_count().await, 0);
}

// ---- ambiguous success ---------------------------------------------------

#[cfg_attr(windows, ignore)]
#[tokio::test]
async fn final_apply_commit_lock_ambiguous_success_is_not_duplicated_on_the_add_path() {
    let repo = LockRepo::init().await;
    repo.write_apply_output();
    let message = expected_commit_message();
    let mut attempts = 0_u32;

    let outcome = run_final_commit_with_retry(
        |_attempt| {
            attempts += 1;
            let repo_path = repo.path.clone();
            let message = message.clone();
            async move {
                // The commit lands, but the command reports contention.
                run_git(&["add", "-A"], &repo_path).await?;
                run_git(&["commit", "-q", "-m", &message], &repo_path).await?;
                Err(fabricated_lock_error(&repo_path, "git add -A").await)
            }
        },
        &GitFinalCommitEnvironment,
        &repo.path,
        &message,
        None,
    )
    .await
    .expect("an ambiguous success must be recognised from repository state");

    assert_eq!(outcome, VerifiedCommitOutcome::Committed);
    assert_eq!(attempts, 1, "an ambiguous success must not be retried");
    assert_eq!(
        repo.commit_subjects().await,
        vec![message, "base".to_string()],
        "no duplicate final commit"
    );
    assert!(repo.hook_runs() >= 1, "the landed commit ran the hook");
}

#[cfg_attr(windows, ignore)]
#[tokio::test]
async fn final_apply_commit_lock_ambiguous_success_is_not_duplicated_on_the_amend_path() {
    let repo = LockRepo::init().await;
    repo.snapshot_apply_output().await;
    let message = expected_commit_message();
    let mut attempts = 0_u32;

    run_final_commit_with_retry(
        |_attempt| {
            attempts += 1;
            let repo_path = repo.path.clone();
            let message = message.clone();
            async move {
                run_git(
                    &["commit", "-q", "--amend", "--allow-empty", "-m", &message],
                    &repo_path,
                )
                .await?;
                Err(fabricated_lock_error(&repo_path, "git add -A").await)
            }
        },
        &GitFinalCommitEnvironment,
        &repo.path,
        &message,
        None,
    )
    .await
    .expect("an ambiguous amend must be recognised from repository state");

    assert_eq!(attempts, 1);
    assert_eq!(
        repo.commit_subjects().await,
        vec![message, "base".to_string()],
        "the amend replaced the WIP snapshot exactly once"
    );
    assert!(repo.head_file_names().await.contains("applied.txt"));
}

#[cfg_attr(windows, ignore)]
#[tokio::test]
async fn final_apply_commit_lock_ambiguous_success_rejects_historical_commit() {
    let repo = LockRepo::init().await;
    let message = expected_commit_message();
    // A commit with exactly the expected subject is already at HEAD, and the
    // worktree is clean, so HEAD never advances during finalization.
    repo.write_apply_output();
    git_ok(&repo.path, &["add", "-A"]).await;
    git_ok(&repo.path, &["commit", "-q", "--no-verify", "-m", &message]).await;
    let mut attempts = 0_u32;

    let error = run_final_commit_with_retry(
        |_attempt| {
            attempts += 1;
            let repo_path = repo.path.clone();
            async move { Err(fabricated_lock_error(&repo_path, "git add -A").await) }
        },
        &GitFinalCommitEnvironment,
        &repo.path,
        &message,
        None,
    )
    .await
    .expect_err("an unchanged HEAD is not this finalization's commit");

    assert_eq!(attempts, FINAL_COMMIT_MAX_ATTEMPTS);
    assert!(
        error.to_string().contains("did not clear"),
        "unexpected error: {}",
        error
    );
    assert_eq!(repo.final_commit_count().await, 1);
}

#[cfg_attr(windows, ignore)]
#[tokio::test]
async fn final_apply_commit_lock_ambiguous_success_rejects_mismatched_tree() {
    let repo = LockRepo::init().await;
    repo.snapshot_apply_output().await;
    let message = expected_commit_message();
    let mut attempts = 0_u32;

    let error = run_final_commit_with_retry(
        |_attempt| {
            attempts += 1;
            let repo_path = repo.path.clone();
            let message = message.clone();
            async move {
                if attempts == 1 {
                    // A commit with the expected subject and lineage lands, but
                    // it records content the amend would not have committed.
                    fs::write(repo_path.join("unexpected.txt"), "not apply output\n").unwrap();
                    run_git(&["add", "-A"], &repo_path).await?;
                    run_git(
                        &["commit", "-q", "--amend", "--allow-empty", "-m", &message],
                        &repo_path,
                    )
                    .await?;
                }
                Err(fabricated_lock_error(&repo_path, "git add -A").await)
            }
        },
        &GitFinalCommitEnvironment,
        &repo.path,
        &message,
        None,
    )
    .await
    .expect_err("a mismatched tree is not this finalization's commit");

    assert_eq!(attempts, FINAL_COMMIT_MAX_ATTEMPTS);
    assert!(error.to_string().contains("did not clear"));
}

#[cfg_attr(windows, ignore)]
#[tokio::test]
async fn final_apply_commit_lock_ambiguous_success_rejects_mismatched_tree_on_the_add_path() {
    let repo = LockRepo::init().await;
    repo.write_apply_output();
    let message = expected_commit_message();
    let mut attempts = 0_u32;

    let error = run_final_commit_with_retry(
        |_attempt| {
            attempts += 1;
            let repo_path = repo.path.clone();
            let message = message.clone();
            async move {
                if attempts == 1 {
                    // A commit lands on top of the captured HEAD carrying
                    // exactly this finalization's subject, and it leaves the
                    // worktree clean - but it records content the attempt
                    // never saw, so it is somebody else's commit.
                    fs::write(repo_path.join("applied.txt"), "tampered output\n").unwrap();
                    run_git(&["add", "-A"], &repo_path).await?;
                    run_git(&["commit", "-q", "--no-verify", "-m", &message], &repo_path).await?;
                }
                Err(fabricated_lock_error(&repo_path, "git add -A").await)
            }
        },
        &GitFinalCommitEnvironment,
        &repo.path,
        &message,
        None,
    )
    .await
    .expect_err("a same-subject, same-parent commit of other content is not this commit");

    assert_eq!(attempts, FINAL_COMMIT_MAX_ATTEMPTS);
    assert!(
        error.to_string().contains("did not clear"),
        "unexpected error: {}",
        error
    );
    assert_eq!(
        repo.commit_subjects().await,
        vec![message, "base".to_string()],
        "the foreign commit must be left exactly as it was found"
    );
    assert_eq!(
        fs::read_to_string(repo.path.join("applied.txt")).unwrap(),
        "tampered output\n",
        "the policy must not have committed on top of the foreign commit"
    );
}

/// The expected-tree proof must accept the real add-and-commit finalization it
/// is derived from: a tree captured from the worktree before the attempt has to
/// equal the tree `git add -A` plus `git commit` actually records.
#[cfg_attr(windows, ignore)]
#[tokio::test]
async fn final_apply_commit_lock_environment_workspace_tree_matches_the_committed_tree() {
    let repo = LockRepo::init().await;
    repo.write_apply_output();
    // Content Git treats specially still has to round-trip: a removal, a nested
    // addition, and an ignored file that must stay out of the tree.
    fs::remove_file(repo.path.join("base.txt")).unwrap();
    fs::create_dir_all(repo.path.join("nested")).unwrap();
    fs::write(repo.path.join("nested").join("deep.txt"), "deep\n").unwrap();
    fs::write(repo.path.join(".gitignore"), "ignored.txt\n").unwrap();
    fs::write(repo.path.join("ignored.txt"), "ignored\n").unwrap();

    let expected = GitFinalCommitEnvironment
        .workspace_tree(&repo.path)
        .await
        .expect("workspace tree");

    // Capturing the expected tree must be a pure observation of the worktree.
    assert!(
        run_git(&["diff", "--cached", "--name-only"], &repo.path)
            .await
            .expect("git diff --cached")
            .is_empty(),
        "computing the expected tree must not stage anything in the real index"
    );
    assert!(
        !repo.lock_path().exists(),
        "computing the expected tree must not take the real index.lock"
    );

    git_ok(&repo.path, &["add", "-A"]).await;
    git_ok(&repo.path, &["commit", "-q", "--no-verify", "-m", "staged"]).await;
    let committed = GitFinalCommitEnvironment
        .commit_tree(&repo.path, "HEAD")
        .await
        .expect("commit tree");

    assert_eq!(
        expected, committed,
        "the captured workspace tree must be the tree finalization records"
    );
    let tracked = run_git(&["ls-tree", "-r", "--name-only", "HEAD"], &repo.path)
        .await
        .expect("git ls-tree");
    assert!(
        tracked.contains("nested/deep.txt") && !tracked.contains("base.txt"),
        "the round-tripped tree must carry the addition and the removal: {tracked}"
    );
    assert!(
        !tracked.contains("ignored.txt"),
        "an ignored file must stay out of both trees: {tracked}"
    );
}

// ---- managed lock identity ----------------------------------------------

#[cfg_attr(windows, ignore)]
#[tokio::test]
async fn final_apply_commit_lock_environment_reports_managed_lock_identity() {
    let repo = LockRepo::init().await;

    let lock_paths = GitFinalCommitEnvironment
        .lock_paths(&repo.path)
        .await
        .expect("lock paths");

    assert!(!lock_paths.is_empty());
    assert!(lock_paths
        .iter()
        .all(|path| path.file_name().is_some_and(|name| name == "index.lock")));
    assert!(
        lock_paths.contains(&repo.lock_path()),
        "the workspace-derived lock path must be a candidate: {:?}",
        lock_paths
    );
}