loopflow 0.12.28

Run steps and flows with coding agents
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
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
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::process::Command;

use crate::engine::git::{
    abort_merge, continue_merge, current_branch, fetch, get_default_branch, intervention_state,
    merge, rerere_remaining, rev_parse,
};

use crate::ops::error::{OpsError, OpsResult};
use crate::ops::git_operation::{
    authorize_sync_control, begin_sync_operation, GitOperationOwner, SyncOperation,
};
use crate::ops::progress::Progress;

#[derive(Debug, Clone)]
pub struct SyncOptions {
    pub onto: String,
    pub push: bool,
    /// The recorded parent tip used as the merge base after squash landing.
    pub fork_base: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SyncClass {
    StaleEmpty,
    ScratchOnly,
    GeneratedOnly,
    CleanAuthored,
    Protected,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SyncStrategy {
    Noop,
    ResetToBase,
    MergeTarget,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SyncPlan {
    pub branch: String,
    pub base_ref: String,
    /// The recorded stack base shared by planning, integration and the PR range.
    pub fork_base: Option<String>,
    pub class: SyncClass,
    pub strategy: SyncStrategy,
    pub unique_commits: usize,
    pub changed_files: Vec<PathBuf>,
    pub scratch_stashed: bool,
}

#[derive(Debug)]
struct SyncExpectation {
    strategy: SyncStrategy,
    tracked_dirty: BTreeSet<String>,
    stacked: bool,
    require_changes: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SyncVerification {
    pub branch: String,
    pub head: String,
    pub target_sha: String,
    pub unique_commits: usize,
}

#[derive(Debug)]
pub struct SyncRecovery {
    operation: SyncOperation,
    expected: SyncExpectation,
    push: bool,
}

pub fn plan_sync(
    repo: &Path,
    onto: Option<&str>,
    fork_base: Option<String>,
) -> OpsResult<SyncPlan> {
    let default_branch = get_default_branch(repo)?;
    let branch = current_branch(repo)?.unwrap_or_else(|| "HEAD".to_string());
    let base_ref = if let Some(onto) = onto {
        onto.to_string()
    } else {
        format!("origin/{default_branch}")
    };

    let comparison_base = fork_base.as_deref().unwrap_or(&base_ref);
    let unique_commits = count_unique_commits(repo, comparison_base)?;
    let mut changed_files = diff_names(repo, comparison_base)?;
    for path in dirty_paths(repo)? {
        if !changed_files.contains(&path) {
            changed_files.push(path);
        }
    }
    changed_files.sort();

    let protected = changed_files.iter().any(|path| is_protected_path(path));
    let scratch_only = !changed_files.is_empty()
        && changed_files
            .iter()
            .all(|path| path.starts_with(Path::new("scratch")));
    let has_non_scratch_changes = changed_files
        .iter()
        .any(|path| !path.starts_with(Path::new("scratch")));

    let (class, strategy) = if branch == default_branch {
        let strategy = if crate::engine::git::is_ancestor(repo, &base_ref, "HEAD")? {
            SyncStrategy::Noop
        } else {
            SyncStrategy::MergeTarget
        };
        (SyncClass::Protected, strategy)
    } else if protected {
        (SyncClass::Protected, SyncStrategy::MergeTarget)
    } else if unique_commits == 0 && scratch_only {
        (SyncClass::ScratchOnly, SyncStrategy::ResetToBase)
    } else if unique_commits == 0 && changed_files.is_empty() {
        (SyncClass::StaleEmpty, SyncStrategy::ResetToBase)
    } else if has_non_scratch_changes {
        (SyncClass::CleanAuthored, SyncStrategy::MergeTarget)
    } else if scratch_only || generated_only(repo, comparison_base)? {
        (SyncClass::GeneratedOnly, SyncStrategy::ResetToBase)
    } else {
        (SyncClass::CleanAuthored, SyncStrategy::MergeTarget)
    };
    // A stacked child's scratch deletion is intentional history. Resetting to
    // the parent would discard it and copy the parent's notes back into the child.
    let strategy = if fork_base.is_some() {
        if crate::engine::git::is_ancestor(repo, &base_ref, "HEAD")? {
            SyncStrategy::Noop
        } else {
            SyncStrategy::MergeTarget
        }
    } else {
        strategy
    };

    let scratch_stashed =
        matches!(strategy, SyncStrategy::ResetToBase) && repo.join("scratch").exists();

    Ok(SyncPlan {
        branch,
        base_ref,
        fork_base,
        class,
        strategy,
        unique_commits,
        changed_files,
        scratch_stashed,
    })
}

pub fn sync_with_recovery(
    repo: &Path,
    options: &SyncOptions,
    progress: &impl Progress,
) -> OpsResult<SyncVerification> {
    let default = get_default_branch(repo)?;
    if current_branch(repo)?.as_deref() == Some(&default)
        && options.onto == format!("origin/{default}")
    {
        crate::ops::checkout::refresh_main(repo, progress)?;
        let target_sha = rev_parse(repo, &options.onto)?;
        return Ok(SyncVerification {
            branch: default,
            head: rev_parse(repo, "HEAD")?,
            unique_commits: count_unique_commits(repo, &target_sha)?,
            target_sha,
        });
    }
    start_owned_sync(repo, options, false, progress)
}

/// Delivery must not publish an empty tree, even for an unmanaged branch.
pub(crate) fn sync_for_delivery(
    repo: &Path,
    options: &SyncOptions,
    progress: &impl Progress,
) -> OpsResult<SyncVerification> {
    start_owned_sync(repo, options, true, progress)
}

/// Continue a local sync after its conflict paths have been resolved.
pub fn continue_sync_for_resolution(repo: &Path, adopt: bool) -> OpsResult<()> {
    continue_sync_after_authorization(repo, adopt, || Ok(()))
}

pub(crate) fn continue_sync_after_authorization(
    repo: &Path,
    adopt: bool,
    before_mutation: impl FnOnce() -> OpsResult<()>,
) -> OpsResult<()> {
    let authorization = authorize_sync_control(repo, adopt)?;
    before_mutation()?;
    let result = continue_merge(repo, authorization.owner().target_sha.as_deref())?;
    if result.success {
        verify_control_completion(repo, authorization.owner())?;
        record_control_task_state(repo, authorization.owner())?;
        authorization.complete()?;
        return Ok(());
    }
    Err(OpsError::SyncConflict {
        onto: authorization.owner().target_ref.clone(),
        detail: conflict_detail(result.conflicts),
        recovery: None,
    })
}

/// Abort a local sync that was left open for inline resolution.
pub fn abort_sync_for_resolution(repo: &Path, adopt: bool) -> OpsResult<()> {
    abort_sync_after_authorization(repo, adopt, || Ok(()))
}

pub(crate) fn abort_sync_after_authorization(
    repo: &Path,
    adopt: bool,
    before_mutation: impl FnOnce() -> OpsResult<()>,
) -> OpsResult<()> {
    let authorization = authorize_sync_control(repo, adopt)?;
    before_mutation()?;
    abort_merge(repo)?;
    authorization.complete()?;
    Ok(())
}

pub fn recover_sync(
    recovery: SyncRecovery,
    launch: impl FnOnce(&BTreeMap<String, String>) -> OpsResult<()>,
) -> OpsResult<SyncVerification> {
    let worktree = recovery.operation.owner().worktree.clone();
    launch(&recovery.operation.scoped_env())?;
    finish_sync(
        &worktree,
        recovery.operation,
        &recovery.expected,
        recovery.push,
    )
}

fn start_owned_sync(
    repo: &Path,
    options: &SyncOptions,
    require_changes: bool,
    progress: &impl Progress,
) -> OpsResult<SyncVerification> {
    let mut operation = begin_sync_operation(repo, &options.onto)?;
    let tracked_dirty = tracked_dirty_state(repo)?;
    fetch_target(repo, &options.onto)?;
    let target_sha = rev_parse(repo, &options.onto)?;
    let plan = plan_sync(repo, Some(&options.onto), options.fork_base.clone())?;
    operation.pin_target(target_sha.clone())?;
    revalidate_start(repo, operation.owner())?;

    let expected = SyncExpectation {
        strategy: plan.strategy.clone(),
        tracked_dirty,
        stacked: options.fork_base.is_some(),
        require_changes,
    };
    validate_fork_base(repo, options.fork_base.as_deref())?;

    if matches!(plan.strategy, SyncStrategy::ResetToBase) {
        reset_to_base(repo, &plan, progress)?;
        return finish_sync(repo, operation, &expected, options.push);
    }
    if matches!(plan.strategy, SyncStrategy::Noop) {
        return finish_sync(repo, operation, &expected, false);
    }

    progress.status(&format!("Merging {}...", options.onto));
    let result = merge(repo, &target_sha, options.fork_base.as_deref())?;
    if result.success {
        return finish_sync(repo, operation, &expected, options.push);
    }
    if intervention_state(repo)? != Some("merge") {
        operation.complete()?;
        return Err(OpsError::Message(format!(
            "git merge of {} stopped before creating a recoverable sequencer",
            options.onto
        )));
    }

    if continue_reused_resolutions(repo, progress)? {
        progress.status("Reused recorded conflict resolution");
        return finish_sync(repo, operation, &expected, options.push);
    }

    Err(OpsError::SyncConflict {
        onto: options.onto.clone(),
        detail: conflict_detail(result.conflicts),
        recovery: Some(Box::new(SyncRecovery {
            operation,
            expected,
            push: options.push,
        })),
    })
}

/// Complete a merge when rerere populated every unresolved path.
fn continue_reused_resolutions(repo: &Path, progress: &impl Progress) -> OpsResult<bool> {
    let conflicts = git(repo, &["diff", "--diff-filter=U", "--name-only"])?;
    if conflicts.trim().is_empty() || !rerere_remaining(repo)?.is_empty() {
        return Ok(false);
    }
    progress.status("Applying recorded conflict resolution...");
    Ok(continue_merge(repo, None)?.success)
}

fn finish_sync(
    repo: &Path,
    operation: SyncOperation,
    expected: &SyncExpectation,
    push: bool,
) -> OpsResult<SyncVerification> {
    let verification = verify_sync(repo, operation.owner(), expected)?;
    if !expected.stacked {
        crate::ops::task::validate_task_pr_range_for_integration(
            repo,
            &operation.owner().target_ref,
            &verification.target_sha,
        )?;
    }
    if push {
        // Sync owns a force-push path rather than the ordinary commit helper,
        // but it must cross the same Task settlement fence first.
        let _mutation = crate::ops::task::lock_task_pr_mutation(repo)?;
        crate::ops::task::clear_task_pr_merge_before_head_mutation(repo, false)?;
        push_synced_branch(repo, &verification.branch)?;
        crate::ops::commit::verify_remote_branch_head(
            repo,
            &verification.branch,
            &verification.head,
        )?;
    }
    if !expected.stacked {
        crate::ops::task::record_task_pr_range_after_integration(
            repo,
            &operation.owner().target_ref,
            &verification.target_sha,
        )?;
    }
    operation.complete()?;
    Ok(verification)
}

fn verify_sync(
    repo: &Path,
    owner: &GitOperationOwner,
    expected: &SyncExpectation,
) -> OpsResult<SyncVerification> {
    if let Some(state) = intervention_state(repo)? {
        return Err(OpsError::Message(format!(
            "sync incomplete: Git still reports an active {state} operation"
        )));
    }
    let branch = current_branch(repo)?
        .ok_or_else(|| OpsError::Message("sync incomplete: HEAD is detached".to_string()))?;
    if branch != owner.branch {
        return Err(OpsError::Message(format!(
            "sync incomplete: expected branch {}, found {branch}",
            owner.branch
        )));
    }
    let head = rev_parse(repo, "HEAD")?;
    let target_sha = owner.target_sha.as_deref().ok_or_else(|| {
        OpsError::Message("sync verification has no pinned target commit".to_string())
    })?;
    if !crate::engine::git::is_ancestor(repo, target_sha, &head)? {
        return Err(OpsError::Message(format!(
            "sync incomplete: pinned target {target_sha} is not an ancestor of HEAD {head}"
        )));
    }
    let conflicts = git(repo, &["diff", "--diff-filter=U", "--name-only"])?;
    if !conflicts.trim().is_empty() {
        return Err(OpsError::Message(format!(
            "sync incomplete: unresolved paths remain: {}",
            conflicts.lines().collect::<Vec<_>>().join(", ")
        )));
    }
    let tracked_dirty = tracked_dirty_state(repo)?;
    let introduced = tracked_dirty
        .difference(&expected.tracked_dirty)
        .cloned()
        .collect::<Vec<_>>();
    if !introduced.is_empty() {
        return Err(OpsError::Message(format!(
            "sync incomplete: new tracked dirty state remains: {}",
            introduced.join(", ")
        )));
    }
    let unique_commits = count_unique_commits(repo, target_sha)?;
    if matches!(expected.strategy, SyncStrategy::MergeTarget)
        && !crate::engine::git::is_ancestor(repo, &owner.head, &head)?
    {
        return Err(OpsError::Message(
            "sync lost the original branch history".to_string(),
        ));
    }
    if expected.require_changes
        && rev_parse(repo, "HEAD^{tree}")? == rev_parse(repo, &format!("{target_sha}^{{tree}}"))?
    {
        return Err(OpsError::Message(
            "no authored changes remain after integration; refusing to publish an empty PR"
                .to_string(),
        ));
    }
    Ok(SyncVerification {
        branch,
        head,
        target_sha: target_sha.to_string(),
        unique_commits,
    })
}

fn verify_control_completion(repo: &Path, owner: &GitOperationOwner) -> OpsResult<()> {
    if let Some(state) = intervention_state(repo)? {
        return Err(OpsError::Message(format!(
            "sync incomplete: Git still reports an active {state} operation"
        )));
    }
    if owner.branch != "HEAD" {
        let branch = current_branch(repo)?
            .ok_or_else(|| OpsError::Message("sync incomplete: HEAD is detached".to_string()))?;
        if branch != owner.branch {
            return Err(OpsError::Message(format!(
                "sync incomplete: expected branch {}, found {branch}",
                owner.branch
            )));
        }
    }
    if let Some(target_sha) = owner.target_sha.as_deref() {
        if !crate::engine::git::is_ancestor(repo, target_sha, "HEAD")? {
            return Err(OpsError::Message(format!(
                "sync incomplete: pinned target {target_sha} is not an ancestor of HEAD"
            )));
        }
        let introduced = tracked_dirty_state(repo)?;
        if !introduced.is_empty() {
            return Err(OpsError::Message(format!(
                "sync incomplete: tracked dirty state remains: {}",
                introduced.into_iter().collect::<Vec<_>>().join(", ")
            )));
        }
        if !crate::engine::git::is_ancestor(repo, &owner.head, "HEAD")? {
            return Err(OpsError::Message(
                "sync lost the original branch history".to_string(),
            ));
        }
    }
    Ok(())
}

fn record_control_task_state(repo: &Path, owner: &GitOperationOwner) -> OpsResult<()> {
    let Some(target_sha) = owner.target_sha.as_deref() else {
        return Ok(());
    };
    if let Some(stacked) = crate::ops::task::task_stack(repo)? {
        let clear_parent = stacked.parent_branch.is_none();
        return crate::ops::task::record_stack_sync(&stacked, target_sha, clear_parent);
    }
    crate::ops::task::validate_task_pr_range_for_integration(repo, &owner.target_ref, target_sha)?;
    crate::ops::task::record_task_pr_range_after_integration(repo, &owner.target_ref, target_sha)
}

fn fetch_target(repo: &Path, target: &str) -> OpsResult<()> {
    if let Some(branch) = target.strip_prefix("origin/") {
        fetch(repo, "origin", branch)?;
    }
    Ok(())
}

fn revalidate_start(repo: &Path, owner: &GitOperationOwner) -> OpsResult<()> {
    if let Some(state) = intervention_state(repo)? {
        return Err(OpsError::Message(format!(
            "refusing to start the owned sync: a {state} operation appeared during preparation"
        )));
    }
    let branch = current_branch(repo)?.ok_or_else(|| {
        OpsError::Message("refusing to start the owned sync from detached HEAD".to_string())
    })?;
    let head = rev_parse(repo, "HEAD")?;
    if branch != owner.branch || head != owner.head {
        return Err(OpsError::Message(format!(
            "refusing to start the owned sync: branch/HEAD changed during preparation (expected {} at {}, found {branch} at {head})",
            owner.branch, owner.head
        )));
    }
    Ok(())
}

fn tracked_dirty_state(repo: &Path) -> OpsResult<BTreeSet<String>> {
    Ok(
        git(repo, &["status", "--porcelain", "--untracked-files=no"])?
            .lines()
            .map(str::to_string)
            .collect(),
    )
}

fn push_synced_branch(repo: &Path, branch: &str) -> OpsResult<()> {
    let reference = format!("refs/heads/{branch}");
    let remote = git(repo, &["ls-remote", "--heads", "origin", &reference])?;
    let lease = if remote.trim().is_empty() {
        // Deletion leaves a stale tracking ref. Require absence atomically so a
        // concurrently recreated branch cannot be overwritten.
        format!("--force-with-lease={reference}:")
    } else {
        "--force-with-lease".to_string()
    };
    git(repo, &["push", &lease, "-u", "origin", &reference]).map(|_| ())
}

/// Validate the recorded comparison base before merging a squash-landed parent.
fn validate_fork_base(repo: &Path, fork_base: Option<&str>) -> OpsResult<()> {
    let Some(base) = fork_base else {
        return Ok(());
    };
    if crate::engine::git::is_ancestor(repo, base, "HEAD")? {
        return Ok(());
    }
    let merge_base =
        crate::engine::git::merge_base(repo, base, "HEAD").unwrap_or_else(|_| base.to_string());
    let commits =
        git(repo, &["log", "--oneline", &format!("{merge_base}..HEAD")]).unwrap_or_default();
    Err(OpsError::UnsafeSyncBase {
        base: base.to_string(),
        commits,
    })
}

fn conflict_detail(conflicts: Option<Vec<PathBuf>>) -> String {
    conflicts
        .filter(|conflicts| !conflicts.is_empty())
        .map(|conflicts| {
            let conflict_paths = conflicts
                .iter()
                .map(|path| path.display().to_string())
                .collect::<Vec<_>>()
                .join(", ");
            format!("conflicts: {conflict_paths}")
        })
        .unwrap_or_else(|| "manual resolution required".to_string())
}

fn reset_to_base(repo: &Path, plan: &SyncPlan, progress: &impl Progress) -> OpsResult<()> {
    let stash_path = if repo.join("scratch").exists() {
        let path = scratch_stash_path(repo, &plan.branch);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        if path.exists() {
            std::fs::remove_dir_all(&path)?;
        }
        copy_dir(&repo.join("scratch"), &path)?;
        Some(path)
    } else {
        None
    };

    progress.status(&format!(
        "Resetting {} to {}...",
        plan.branch, plan.base_ref
    ));
    git(repo, &["reset", "--hard", &plan.base_ref])?;

    if let Some(path) = stash_path {
        let scratch = repo.join("scratch");
        if scratch.exists() {
            std::fs::remove_dir_all(&scratch)?;
        }
        copy_dir(&path, &scratch)?;
        progress.status(&format!("Restored scratch from {}", path.display()));
    }

    Ok(())
}

fn scratch_stash_path(repo: &Path, branch: &str) -> PathBuf {
    let ts = chrono::Utc::now().format("%Y%m%dT%H%M%SZ");
    let safe_branch = branch.replace(['/', '.'], "-");
    repo.join(".lf")
        .join("tmp")
        .join("scratch-stash")
        .join(format!("{safe_branch}-{ts}"))
}

fn copy_dir(from: &Path, to: &Path) -> std::io::Result<()> {
    std::fs::create_dir_all(to)?;
    for entry in std::fs::read_dir(from)? {
        let entry = entry?;
        let source = entry.path();
        let target = to.join(entry.file_name());
        if entry.file_type()?.is_dir() {
            copy_dir(&source, &target)?;
        } else {
            std::fs::copy(&source, &target)?;
        }
    }
    Ok(())
}

fn git(repo: &Path, args: &[&str]) -> OpsResult<String> {
    let output = Command::new("git")
        .arg("-C")
        .arg(repo)
        .args(args)
        .output()?;
    if !output.status.success() {
        return Err(OpsError::Git(crate::engine::GitError::CommandFailed {
            command: format!("git {}", args.join(" ")),
            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
        }));
    }
    Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}

fn count_unique_commits(repo: &Path, base_ref: &str) -> OpsResult<usize> {
    let stdout = git(repo, &["rev-list", "--count", &format!("{base_ref}..HEAD")])?;
    Ok(stdout.trim().parse().unwrap_or(0))
}

fn diff_names(repo: &Path, base_ref: &str) -> OpsResult<Vec<PathBuf>> {
    // Three-dot: diff from the merge-base, so we see only what this branch
    // authored — not files the base advanced past us. A stale branch (the whole
    // reason to sync) would otherwise report the base's new files as its own,
    // misclassifying a scratch-only branch as clean_authored.
    let stdout = git(
        repo,
        &["diff", "--name-only", &format!("{base_ref}...HEAD")],
    )?;
    Ok(stdout
        .lines()
        .filter(|line| !line.trim().is_empty())
        .map(PathBuf::from)
        .collect())
}

fn dirty_paths(repo: &Path) -> OpsResult<Vec<PathBuf>> {
    // Read raw stdout: porcelain lines carry a leading status column (e.g.
    // " M path" for a working-tree-only change), and the shared git() helper
    // would trim the first line's leading space, shifting the fixed 3-char
    // path offset and dropping the first character of that path.
    let output = Command::new("git")
        .arg("-C")
        .arg(repo)
        .args(["status", "--porcelain"])
        .output()?;
    if !output.status.success() {
        return Err(OpsError::Git(crate::engine::GitError::CommandFailed {
            command: "git status --porcelain".to_string(),
            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
        }));
    }
    let stdout = String::from_utf8_lossy(&output.stdout);
    Ok(stdout
        .lines()
        .filter_map(|line| {
            if line.len() < 4 {
                return None;
            }
            let path = &line[3..];
            let path = path
                .split_once(" -> ")
                .map(|(_, right)| right)
                .unwrap_or(path);
            Some(PathBuf::from(path))
        })
        .collect())
}

fn generated_only(repo: &Path, base_ref: &str) -> OpsResult<bool> {
    let stdout = git(repo, &["log", "--format=%s", &format!("{base_ref}..HEAD")])?;
    let subjects = stdout
        .lines()
        .filter(|line| !line.trim().is_empty())
        .collect::<Vec<_>>();
    Ok(!subjects.is_empty()
        && subjects.iter().all(|subject| {
            subject.starts_with("checkpoint:")
                || subject.starts_with("wip:")
                || subject.contains("generated")
        }))
}

fn is_protected_path(path: &Path) -> bool {
    path.starts_with(Path::new("wave"))
        || path.starts_with(Path::new(".lf/skills"))
        || path.starts_with(Path::new(".lf/flows"))
        || path == Path::new(".lf/config.yaml")
}

pub fn sync_class_name(class: &SyncClass) -> &'static str {
    match class {
        SyncClass::StaleEmpty => "stale_empty",
        SyncClass::ScratchOnly => "scratch_only",
        SyncClass::GeneratedOnly => "generated_only",
        SyncClass::CleanAuthored => "clean_authored",
        SyncClass::Protected => "protected",
    }
}

pub fn sync_strategy_name(strategy: &SyncStrategy) -> &'static str {
    match strategy {
        SyncStrategy::Noop => "noop",
        SyncStrategy::ResetToBase => "reset_to_base",
        SyncStrategy::MergeTarget => "merge_target",
    }
}

#[cfg(test)]
mod tests {
    use super::scratch_stash_path;
    use std::path::Path;

    #[test]
    fn scratch_stash_lands_under_the_ignored_tmp_prefix() {
        let path = scratch_stash_path(Path::new("/repo"), "jack/reconcile-out-of-band-merges");
        // .lf/tmp/ is gitignored; a sibling like .lf/scratch-stash/ would dirty
        // the worktree and block `lf task complete`.
        assert!(
            path.starts_with("/repo/.lf/tmp/scratch-stash"),
            "stash must sit under the ignored .lf/tmp prefix, got {}",
            path.display()
        );
        // Slashes and dots in the branch are flattened so the dir name is safe.
        let leaf = path.file_name().unwrap().to_string_lossy();
        assert!(leaf.starts_with("jack-reconcile-out-of-band-merges-"));
    }
}