Skip to main content

orch/engine/runner/
git_ops.rs

1//! Git operations — auto-commit, push, PR creation.
2//!
3//! These run after the agent completes to ensure all changes
4//! are committed, pushed, and a PR is created.
5//!
6//! All GitHub API operations use [`GhHttp`] exclusively. The `gh` CLI
7//! is not used as a fallback to ensure consistent behavior across
8//! environments and simplify testing.
9
10use crate::cmd::CommandErrorContext;
11use crate::github::http;
12use crate::github::http::GhHttp;
13use crate::github::token;
14use crate::security;
15use regex::Regex;
16use std::fmt::Write as _;
17use std::path::Path;
18use std::sync::LazyLock;
19use std::sync::atomic::{AtomicU64, Ordering};
20use std::time::Duration;
21
22use tokio::process::Command;
23use tokio::time;
24
25/// Format a task ID for display in PR/issue footers.
26///
27/// GitHub task IDs are displayed as `#123` (creates a GH issue hyperlink).
28/// Internal task IDs (`internal:13`) are displayed as `internal-13` (no `#`
29/// to avoid creating a false issue link).
30pub(crate) fn format_task_ref(task_id: &str) -> String {
31    if task_id.starts_with("internal:") {
32        task_id.replace(':', "-")
33    } else {
34        format!("#{task_id}")
35    }
36}
37
38/// Errors that can occur during PR creation.
39#[derive(Debug, thiserror::Error)]
40pub enum PrCreateError {
41    #[error("GitHub API error: {0}")]
42    ApiError(#[from] anyhow::Error),
43}
44
45/// Result type for PR creation operations.
46pub type PrCreateResult<T> = Result<T, PrCreateError>;
47
48/// Check if the current branch has any commits ahead of the default branch.
49pub async fn has_commits_ahead(dir: &Path, default_branch: &str) -> bool {
50    let output = Command::new("git")
51        .args([
52            "rev-list",
53            "--count",
54            &format!("origin/{default_branch}..HEAD"),
55        ])
56        .current_dir(dir)
57        .output()
58        .await;
59    match output {
60        Ok(o) if o.status.success() => {
61            let count: u32 = String::from_utf8_lossy(&o.stdout)
62                .trim()
63                .parse()
64                .unwrap_or(0);
65            count > 0
66        }
67        _ => {
68            tracing::debug!(
69                default_branch,
70                "git rev-list failed for compare ref, assuming commits ahead"
71            );
72            // If we can't determine, assume there ARE commits ahead so we
73            // attempt a push (better to try and fail loudly than silently
74            // skip push and lose the PR creation path).
75            true
76        }
77    }
78}
79
80/// Check if there are uncommitted changes in the working directory.
81pub async fn has_changes(dir: &Path) -> bool {
82    // Check for staged, unstaged, and untracked files
83    let diff = Command::new("git")
84        .args(["diff", "--quiet"])
85        .current_dir(dir)
86        .status()
87        .await;
88
89    let cached = Command::new("git")
90        .args(["diff", "--cached", "--quiet"])
91        .current_dir(dir)
92        .status()
93        .await;
94
95    let untracked = Command::new("git")
96        .args(["ls-files", "--others", "--exclude-standard"])
97        .current_dir(dir)
98        .output_with_context()
99        .await;
100
101    let has_diff = diff.is_ok_and(|s| !s.success());
102    let has_cached = cached.is_ok_and(|s| !s.success());
103    let has_untracked =
104        untracked.is_ok_and(|o| !String::from_utf8_lossy(&o.stdout).trim().is_empty());
105
106    has_diff || has_cached || has_untracked
107}
108
109pub(crate) fn find_stash_ref_by_hash(stash_list: &str, stash_hash: &str) -> Option<String> {
110    stash_list.lines().find_map(|line| {
111        let mut parts = line.split_whitespace();
112        match (parts.next(), parts.next()) {
113            (Some(hash), Some(reference)) if hash == stash_hash => Some(reference.to_string()),
114            _ => None,
115        }
116    })
117}
118
119/// Options for [`stash_rebase_restore`].
120pub(crate) struct StashRebaseOpts {
121    /// Abort the rebase (via `git rebase --abort`) and restore the stash when the
122    /// rebase fails.  Set to `false` when the caller intentionally leaves the working
123    /// tree in the in-progress-rebase state so an agent can resolve conflicts.
124    pub abort_on_failure: bool,
125}
126
127impl Default for StashRebaseOpts {
128    fn default() -> Self {
129        Self {
130            abort_on_failure: true,
131        }
132    }
133}
134
135/// Outcome returned by [`stash_rebase_restore`].
136#[derive(Debug)]
137pub(crate) enum RebaseOutcome {
138    /// Rebase completed successfully.
139    Succeeded,
140    /// Rebase failed.  When `abort_on_failure` is true the rebase was aborted and any
141    /// stash was restored; when false the working tree is left in the in-progress-rebase
142    /// state.
143    Failed(String),
144    /// A stash operation failed (push failed, or OID could not be captured); the
145    /// rebase was skipped to preserve the dirty working tree.
146    Skipped(String),
147}
148
149/// Stash uncommitted changes, rebase onto `target_ref`, then restore the stash.
150///
151/// This is the single canonical implementation of the stash → rebase → restore
152/// pattern used in multiple call sites.  Callers are responsible for any
153/// `git fetch` that should precede the rebase.
154///
155/// # Concurrency safety
156/// Git stashes are repo-global; multiple worktrees share the same stash stack.
157/// This function captures the stash OID immediately after pushing and applies by
158/// that exact ref — never via `stash pop` — to avoid restoring a stash that
159/// belongs to a different concurrently-running worktree.
160///
161/// # Stash failure handling
162/// If the stash push fails, or the OID cannot be captured after a successful push,
163/// the rebase is **skipped** and [`RebaseOutcome::Skipped`] is returned.  This
164/// preserves the dirty working tree intact rather than risking data loss.
165pub(crate) async fn stash_rebase_restore(
166    dir: &Path,
167    target_ref: &str,
168    opts: StashRebaseOpts,
169) -> anyhow::Result<RebaseOutcome> {
170    let stash_ref: Option<String> = if has_changes(dir).await {
171        let stash_out = Command::new("git")
172            .args(["stash", "push", "--include-untracked", "-m", "orch-rebase"])
173            .current_dir(dir)
174            .output_with_context()
175            .await;
176
177        match stash_out {
178            Ok(o) if o.status.success() => {
179                let stdout_preview = String::from_utf8_lossy(&o.stdout)
180                    .lines()
181                    .next()
182                    .unwrap_or("")
183                    .to_string();
184                tracing::debug!(
185                    dir = %dir.display(),
186                    stdout = %stdout_preview,
187                    "git stash push succeeded before rebase"
188                );
189
190                // Capture the OID of the stash object just created so we can
191                // apply it back by that exact ref, regardless of any stashes
192                // that other worktrees may push between now and then.
193                let ref_out = Command::new("git")
194                    .args(["rev-parse", "refs/stash@{0}"])
195                    .current_dir(dir)
196                    .output_with_context()
197                    .await;
198                let stash_hash = ref_out
199                    .ok()
200                    .filter(|o| o.status.success())
201                    .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
202                    .filter(|s| !s.is_empty());
203
204                if stash_hash.is_none() {
205                    // Stash was created but we can't track its ref.
206                    // Pop immediately to avoid orphaning the changes, then skip.
207                    tracing::warn!(
208                        dir = %dir.display(),
209                        "git stash succeeded but rev-parse failed — popping stash and skipping rebase"
210                    );
211                    let _ = Command::new("git")
212                        .args(["stash", "pop"])
213                        .current_dir(dir)
214                        .output_with_context()
215                        .await;
216                    return Ok(RebaseOutcome::Skipped(
217                        "stash OID capture failed".to_string(),
218                    ));
219                }
220                stash_hash
221            }
222            Ok(o) => {
223                let stderr = String::from_utf8_lossy(&o.stderr).trim().to_string();
224                tracing::warn!(
225                    dir = %dir.display(),
226                    code = ?o.status.code(),
227                    stderr = %stderr,
228                    "git stash push failed — skipping rebase to preserve dirty state"
229                );
230                return Ok(RebaseOutcome::Skipped(format!(
231                    "stash push failed: {stderr}"
232                )));
233            }
234            Err(e) => {
235                tracing::warn!(
236                    dir = %dir.display(),
237                    error = %e,
238                    "failed to run git stash push — skipping rebase to preserve dirty state"
239                );
240                return Ok(RebaseOutcome::Skipped(format!("stash push error: {e}")));
241            }
242        }
243    } else {
244        None
245    };
246
247    let output = git()
248        .await
249        .args(["rebase", target_ref])
250        .current_dir(dir)
251        .output_with_context()
252        .await;
253
254    match output {
255        Ok(o) if o.status.success() => {
256            tracing::debug!(dir = %dir.display(), target_ref, "rebase succeeded");
257            restore_stash_by_hash(dir, stash_ref.as_deref()).await;
258            Ok(RebaseOutcome::Succeeded)
259        }
260        Ok(o) => {
261            let stderr = String::from_utf8_lossy(&o.stderr).to_string();
262            tracing::warn!(dir = %dir.display(), target_ref, err = %stderr, "rebase failed");
263            if opts.abort_on_failure {
264                let abort = Command::new("git")
265                    .args(["rebase", "--abort"])
266                    .current_dir(dir)
267                    .output_with_context()
268                    .await;
269                match abort {
270                    Err(e) => {
271                        tracing::warn!(dir = %dir.display(), error = %e, "git rebase --abort failed — worktree may be in inconsistent state");
272                    }
273                    Ok(o) if !o.status.success() => {
274                        let stderr = String::from_utf8_lossy(&o.stderr);
275                        tracing::warn!(dir = %dir.display(), stderr = %stderr, "git rebase --abort returned non-zero — worktree may be in inconsistent state");
276                    }
277                    _ => {}
278                }
279            }
280            restore_stash_by_hash(dir, stash_ref.as_deref()).await;
281            Ok(RebaseOutcome::Failed(stderr))
282        }
283        Err(e) => {
284            if opts.abort_on_failure {
285                let abort = Command::new("git")
286                    .args(["rebase", "--abort"])
287                    .current_dir(dir)
288                    .output_with_context()
289                    .await;
290                match abort {
291                    Err(e) => {
292                        tracing::warn!(dir = %dir.display(), error = %e, "git rebase --abort failed — worktree may be in inconsistent state");
293                    }
294                    Ok(o) if !o.status.success() => {
295                        let stderr = String::from_utf8_lossy(&o.stderr);
296                        tracing::warn!(dir = %dir.display(), stderr = %stderr, "git rebase --abort returned non-zero — worktree may be in inconsistent state");
297                    }
298                    _ => {}
299                }
300            }
301            restore_stash_by_hash(dir, stash_ref.as_deref()).await;
302            Err(e)
303        }
304    }
305}
306
307/// Apply a stash entry by its OID and drop it from the stack on success.
308///
309/// On failure the entry is preserved so it can be recovered manually.
310async fn restore_stash_by_hash(dir: &Path, stash_hash: Option<&str>) {
311    let Some(stash_hash) = stash_hash else {
312        return;
313    };
314
315    let apply = Command::new("git")
316        .args(["stash", "apply", stash_hash])
317        .current_dir(dir)
318        .output_with_context()
319        .await;
320    if apply.is_ok_and(|o| o.status.success()) {
321        let list = Command::new("git")
322            .args(["stash", "list", "--format=%H %gd"])
323            .current_dir(dir)
324            .output_with_context()
325            .await;
326        if let Ok(list_out) = list
327            && list_out.status.success()
328        {
329            let list_str = String::from_utf8_lossy(&list_out.stdout);
330            if let Some(stash_ref) = find_stash_ref_by_hash(&list_str, stash_hash) {
331                match Command::new("git")
332                    .args(["stash", "drop", &stash_ref])
333                    .current_dir(dir)
334                    .output_with_context()
335                    .await
336                {
337                    Ok(drop_out) if !drop_out.status.success() => {
338                        let stderr = String::from_utf8_lossy(&drop_out.stderr);
339                        tracing::warn!(
340                            dir = %dir.display(),
341                            stash_ref = %stash_ref,
342                            err = %stderr,
343                            "stash drop failed after apply — entry may remain on stack"
344                        );
345                    }
346                    Err(e) => {
347                        tracing::warn!(
348                            dir = %dir.display(),
349                            stash_ref = %stash_ref,
350                            error = %e,
351                            "stash drop command failed after apply — entry may remain on stack"
352                        );
353                    }
354                    _ => {}
355                }
356            } else {
357                tracing::warn!(
358                    dir = %dir.display(),
359                    stash = %stash_hash,
360                    "stash applied but could not find ref by hash — stash entry may be orphaned on stack"
361                );
362            }
363        }
364    } else {
365        tracing::warn!(
366            dir = %dir.display(),
367            stash = %stash_hash,
368            "stash apply failed after rebase — stash preserved for manual recovery"
369        );
370    }
371}
372
373/// Commit what the agent left, with the tool the repository uses (see `commit`).
374pub async fn auto_commit(
375    dir: &Path,
376    task_id: &str,
377    title: &str,
378    agent: &str,
379    attempt: u32,
380    when: super::commit::When,
381) -> anyhow::Result<bool> {
382    if !has_changes(dir).await {
383        return Ok(false);
384    }
385
386    tracing::info!(task_id, "auto-committing uncommitted changes");
387    let task_ref = format_task_ref(task_id);
388    let message = format!("{title}\n\nTask {task_ref}\nAgent: {agent}\nAttempt: {attempt}");
389    match super::commit::commit_all(dir, &message, agent, when).await? {
390        super::commit::Outcome::Committed => {
391            tracing::info!(task_id, "auto-commit succeeded");
392            Ok(true)
393        }
394        super::commit::Outcome::Nothing => Ok(false),
395        super::commit::Outcome::Refused {
396            by: super::commit::Tool::Git,
397            reason,
398        } => {
399            tracing::warn!(task_id, err = %reason, "git commit failed");
400            Ok(false)
401        }
402        super::commit::Outcome::Refused { reason, .. } => anyhow::bail!(reason),
403    }
404}
405
406/// Maximum number of commits to replay during a rebase.
407///
408/// If a branch has diverged so far from the default branch that it would
409/// require replaying more than this many commits, we skip the rebase entirely.
410/// This prevents degenerate cases (e.g. a branch forked from the very first
411/// commit that now sits 300+ commits behind main) from producing hundreds of
412/// add/add conflicts that block the agent and leave the worktree unusable.
413const MAX_REBASE_COMMITS: usize = 50;
414
415/// Rebase the current branch on top of `origin/{branch}`.
416///
417/// Used for push-failure recovery: when another agent has pushed to the same
418/// branch, we fetch the latest remote state and rebase local commits on top.
419/// Returns `Ok(true)` if rebase succeeded, `Ok(false)` if there was nothing to
420/// rebase, and `Err` if rebase failed (conflicts).
421///
422/// Similar to `rebase_on_default` but targets the feature branch instead of
423/// the default branch, and is used after push failures rather than at startup.
424pub async fn rebase_on_branch(dir: &Path, branch: &str) -> anyhow::Result<bool> {
425    let origin_branch = format!("origin/{branch}");
426
427    // Fetch the remote branch to get latest state.
428    // 2-minute timeout mirrors the push timeout to prevent indefinite stalls.
429    let fetch = time::timeout(
430        Duration::from_secs(120),
431        git()
432            .await
433            .args(["fetch", "origin", branch])
434            .kill_on_drop(true)
435            .current_dir(dir)
436            .output(),
437    )
438    .await
439    .map_err(|_| anyhow::anyhow!("git fetch timed out after 120s"))
440    .and_then(|r| r.map_err(Into::into));
441
442    match fetch {
443        Ok(o) if o.status.success() => {}
444        Ok(o) => {
445            let stderr = String::from_utf8_lossy(&o.stderr);
446            anyhow::bail!("failed to fetch origin/{branch}: {stderr}");
447        }
448        Err(e) => anyhow::bail!("failed to fetch origin/{branch}: {e}"),
449    }
450
451    // Check if there are local commits to rebase.
452    let count_output = Command::new("git")
453        .args(["rev-list", "--count", &format!("{origin_branch}..HEAD")])
454        .current_dir(dir)
455        .output_with_context()
456        .await;
457
458    let commit_count: usize = match count_output {
459        Ok(o) if o.status.success() => {
460            let raw = String::from_utf8_lossy(&o.stdout);
461            raw.trim().parse().map_err(|e| {
462                anyhow::anyhow!(
463                    "failed to parse commit count from git rev-list output {:?}: {e}",
464                    raw.trim()
465                )
466            })?
467        }
468        Ok(o) => {
469            let stderr = String::from_utf8_lossy(&o.stderr);
470            anyhow::bail!("git rev-list --count failed: {stderr}");
471        }
472        Err(e) => anyhow::bail!("failed to run git rev-list --count: {e}"),
473    };
474
475    if commit_count == 0 {
476        // No local commits to rebase — nothing to do.
477        return Ok(false);
478    }
479
480    if commit_count > MAX_REBASE_COMMITS {
481        anyhow::bail!(
482            "refusing to rebase {commit_count} commits on origin/{branch} (max {MAX_REBASE_COMMITS})"
483        );
484    }
485
486    tracing::info!(
487        branch = branch,
488        commit_count,
489        "rebasing local commits on origin/{branch}"
490    );
491
492    match stash_rebase_restore(dir, &origin_branch, StashRebaseOpts::default()).await? {
493        RebaseOutcome::Succeeded => Ok(true),
494        RebaseOutcome::Failed(e) => anyhow::bail!("rebase failed: {e}"),
495        RebaseOutcome::Skipped(reason) => anyhow::bail!("rebase skipped: {reason}"),
496    }
497}
498
499/// Rebase the current branch on the default branch.
500///
501/// Run before the agent starts to ensure the worktree has the latest code.
502/// Fetches both the default branch and the current branch so agents never
503/// need to run `git fetch` themselves (important for sandboxed agents like
504/// codex whose workspace-write sandbox blocks writes outside the worktree).
505/// Non-fatal: if rebase fails (conflicts), the agent may still be able to work.
506pub async fn rebase_on_default(dir: &Path, default_branch: &str) {
507    // auto_merge leaves a conflicted rebase in place for the agent, aborting it here would undo that
508    if rebase_in_progress(dir).await {
509        tracing::info!("rebase already in progress, skipping pre-dispatch rebase");
510        return;
511    }
512
513    // A later force push leases this tip, so fetching someone else's commits cannot permit replacing them
514    let branch = get_current_branch(dir).await;
515    record_push_lease(dir, &branch, default_branch).await;
516
517    // Fetch current branch (for retries with existing remote commits)
518    // and default branch (for rebase) in one call.
519    let _ = git()
520        .await
521        .args(["fetch", "origin"])
522        .current_dir(dir)
523        .output_with_context()
524        .await;
525
526    // Count commits that would be replayed.  If the branch diverged too far
527    // back we skip the rebase: replaying hundreds of historical commits produces
528    // massive add/add conflicts (the branch adds files that main already has)
529    // and leaves the worktree stuck.
530    let commit_count = Command::new("git")
531        .args([
532            "rev-list",
533            "--count",
534            &format!("origin/{default_branch}..HEAD"),
535        ])
536        .current_dir(dir)
537        .output_with_context()
538        .await
539        .ok()
540        .and_then(|o| {
541            String::from_utf8_lossy(&o.stdout)
542                .trim()
543                .parse::<usize>()
544                .ok()
545        })
546        .unwrap_or(0);
547
548    if commit_count > MAX_REBASE_COMMITS {
549        tracing::warn!(
550            default_branch,
551            commit_count,
552            max = MAX_REBASE_COMMITS,
553            "skipping rebase: branch has too many commits to replay safely"
554        );
555        return;
556    }
557
558    match stash_rebase_restore(
559        dir,
560        &format!("origin/{default_branch}"),
561        StashRebaseOpts::default(),
562    )
563    .await
564    {
565        Ok(RebaseOutcome::Succeeded) => {
566            tracing::debug!(default_branch, "rebased worktree on default branch");
567        }
568        Ok(RebaseOutcome::Failed(e)) => {
569            tracing::warn!(default_branch, err = %e, "rebase failed, continuing with current state");
570        }
571        Ok(RebaseOutcome::Skipped(reason)) => {
572            tracing::warn!(default_branch, %reason, "stash failed before rebase, continuing with current state");
573        }
574        Err(e) => tracing::warn!(err = %e, "rebase error"),
575    }
576}
577
578pub(crate) async fn rebase_in_progress(dir: &Path) -> bool {
579    for name in ["rebase-merge", "rebase-apply"] {
580        let out = Command::new("git")
581            .args(["rev-parse", "--git-path", name])
582            .current_dir(dir)
583            .output_with_context()
584            .await;
585        if let Ok(o) = out {
586            let p = String::from_utf8_lossy(&o.stdout).trim().to_string();
587            if !p.is_empty() && dir.join(p).exists() {
588                return true;
589            }
590        }
591    }
592    false
593}
594
595/// Tool patterns no agent may run: a worktree shares `.git/config` with the operator's checkout,
596/// so a rewritten remote or URL rule would change the main repository
597pub(crate) fn remote_change_tools() -> Vec<String> {
598    [
599        "Bash(git remote set-url*)",
600        "Bash(git remote add*)",
601        "Bash(git remote remove*)",
602        "Bash(git remote rm*)",
603        "Bash(git remote rename*)",
604        "Bash(git config*remote.*)",
605        "Bash(git config*url.*)",
606    ]
607    .map(String::from)
608    .to_vec()
609}
610
611/// A `git` command for every call orch makes: GitHub over HTTPS with the gh token, never SSH,
612/// and no commit or tag signing, whatever the owner's git config says
613pub(crate) async fn git() -> Command {
614    let mut cmd = Command::new("git");
615    cmd.envs(build_git_auth_env().await).kill_on_drop(true);
616    cmd
617}
618
619/// Build git config environment variables that inject GitHub token credentials.
620///
621/// This handles two scenarios:
622/// - **SSH remotes** (`git@github.com:user/repo.git`): rewritten to
623///   `https://x-access-token:TOKEN@github.com/…` via `insteadOf`.
624/// - **HTTPS remotes** (`https://github.com/user/repo.git`): rewritten to
625///   `https://x-access-token:TOKEN@github.com/…` via a second `insteadOf`.
626///
627/// When no token is available, falls back to the legacy SSH→HTTPS conversion
628/// without credentials (works for repos that use credential helpers or SSH keys).
629///
630/// Returns a `Vec<(String, String)>` of environment variable key-value pairs.
631/// These are NOT visible in the process list (`ps`), unlike `-c` args.
632///
633/// Git environment variables:
634/// - `GIT_CONFIG_COUNT` — number of config entries
635/// - `GIT_CONFIG_KEY_N` — config key for entry N
636/// - `GIT_CONFIG_VALUE_N` — config value for entry N
637///
638/// Signing is switched off too, `commit.gpgsign=false` and `tag.gpgsign=false`
639pub(crate) async fn build_git_auth_env() -> Vec<(String, String)> {
640    let token = token::shared().get_token().await.ok().flatten();
641    build_git_auth_env_for_token(token)
642}
643
644fn build_git_auth_env_for_token(token: Option<String>) -> Vec<(String, String)> {
645    let mut config: Vec<(String, String)> = match token {
646        Some(t) if !t.is_empty() => {
647            let authed = format!("url.https://x-access-token:{t}@github.com/.insteadOf");
648            vec![
649                (authed.clone(), "https://github.com/".into()),
650                (authed, "git@github.com:".into()),
651            ]
652        }
653        _ => vec![(
654            "url.https://github.com/.insteadOf".into(),
655            "git@github.com:".into(),
656        )],
657    };
658    // orch's git never signs, a signing key would ask the owner for a passphrase or a touch
659    config.push(("commit.gpgsign".into(), "false".into()));
660    config.push(("tag.gpgsign".into(), "false".into()));
661
662    let mut env = vec![("GIT_CONFIG_COUNT".to_string(), config.len().to_string())];
663    for (n, (key, value)) in config.into_iter().enumerate() {
664        env.push((format!("GIT_CONFIG_KEY_{n}"), key));
665        env.push((format!("GIT_CONFIG_VALUE_{n}"), value));
666    }
667    env
668}
669
670/// Returns `true` if the push error indicates that the GitHub token lacks the
671/// `workflow` OAuth scope, which is required to push changes to
672/// `.github/workflows/` files.
673///
674/// GitHub rejects pushes with: "refusing to allow an OAuth App to create or
675/// update workflow `…` without `workflow` scope"
676pub(crate) fn is_workflow_scope_error(stderr: &str) -> bool {
677    stderr.contains("without `workflow` scope")
678        || stderr.contains("without 'workflow' scope")
679        || (stderr.contains("refusing to allow")
680            && stderr.contains("workflow")
681            && stderr.contains("scope"))
682}
683
684/// Remove `.github/workflows/` files from committed changes and amend the
685/// commits so the branch can be pushed without the `workflow` OAuth scope.
686///
687/// Strategy: find all commits ahead of `origin/{default_branch}`, identify
688/// which ones touch `.github/workflows/`, and rewrite them to exclude those
689/// files. Uses `git filter-branch`-style approach via interactive rebase
690/// with `exec` steps.
691///
692/// Returns `Ok(true)` if workflow files were removed and commits rewritten,
693/// `Ok(false)` if no workflow files were found in the commits.
694async fn strip_workflow_files(dir: &Path, default_branch: &str) -> anyhow::Result<bool> {
695    // Find workflow files in commits ahead of the default branch
696    let output = Command::new("git")
697        .args([
698            "diff",
699            "--name-only",
700            &format!("origin/{default_branch}...HEAD"),
701            "--",
702            ".github/workflows/",
703        ])
704        .current_dir(dir)
705        .output_with_context()
706        .await?;
707
708    let workflow_files: Vec<String> = String::from_utf8_lossy(&output.stdout)
709        .lines()
710        .filter(|l| !l.is_empty())
711        .map(ToString::to_string)
712        .collect();
713
714    if workflow_files.is_empty() {
715        return Ok(false);
716    }
717
718    tracing::info!(
719        files = ?workflow_files,
720        "stripping workflow files from commits to bypass missing workflow scope"
721    );
722
723    // Remove workflow files from the index and amend each commit.
724    // We use a soft reset → remove files → re-commit approach for simplicity.
725    //
726    // Count commits ahead of default branch
727    let count_output = Command::new("git")
728        .args([
729            "rev-list",
730            "--count",
731            &format!("origin/{default_branch}..HEAD"),
732        ])
733        .current_dir(dir)
734        .output_with_context()
735        .await?;
736
737    let commit_count: usize = String::from_utf8_lossy(&count_output.stdout)
738        .trim()
739        .parse()
740        .unwrap_or(0);
741
742    if commit_count == 0 {
743        return Ok(false);
744    }
745
746    // Save the commit messages before resetting.
747    // Use a null byte as the record prefix — null bytes cannot appear in git commit
748    // messages, so this separator is unambiguous (unlike a string like ---COMMIT_SEP---
749    // which an agent could write into a commit body, corrupting the parse).
750    let log_output = Command::new("git")
751        .args([
752            "log",
753            "--format=%x00%H%n%B",
754            &format!("origin/{default_branch}..HEAD"),
755        ])
756        .current_dir(dir)
757        .output_with_context()
758        .await?;
759    let log_text = String::from_utf8_lossy(&log_output.stdout).to_string();
760
761    // Parse commits in reverse order (oldest first).
762    // Each record starts with \0 (from %x00 in the format), so split on \0 and
763    // discard any leading empty fragment before the first record.
764    let commits: Vec<(String, String)> = log_text
765        .split('\0')
766        .filter(|s| !s.trim().is_empty())
767        .map(|block| {
768            let block = block.trim();
769            let first_nl = block.find('\n').unwrap_or(block.len());
770            let hash = block[..first_nl].to_string();
771            let msg = block[first_nl..].trim().to_string();
772            (hash, msg)
773        })
774        .collect::<Vec<_>>()
775        .into_iter()
776        .rev()
777        .collect();
778
779    if commits.is_empty() {
780        return Ok(false);
781    }
782
783    // Soft reset to the base (origin/default_branch)
784    let reset = Command::new("git")
785        .args(["reset", "--soft", &format!("origin/{default_branch}")])
786        .current_dir(dir)
787        .output_with_context()
788        .await?;
789
790    if !reset.status.success() {
791        let stderr = String::from_utf8_lossy(&reset.stderr);
792        anyhow::bail!("git reset --soft failed during workflow file stripping: {stderr}");
793    }
794
795    // Remove workflow files from the index
796    let mut rm_args = vec!["rm", "--cached", "--ignore-unmatch", "--"];
797    let file_refs: Vec<&str> = workflow_files.iter().map(String::as_str).collect();
798    rm_args.extend(file_refs);
799
800    let rm = Command::new("git")
801        .args(&rm_args)
802        .current_dir(dir)
803        .output_with_context()
804        .await?;
805
806    if !rm.status.success() {
807        let stderr = String::from_utf8_lossy(&rm.stderr);
808        tracing::warn!("git rm --cached for workflow files failed (non-fatal): {stderr}");
809    }
810
811    // Re-commit all remaining changes as a single commit preserving the first message.
812    // This squashes multiple commits but preserves the work minus workflow files.
813    let has_staged = Command::new("git")
814        .args(["diff", "--cached", "--quiet"])
815        .current_dir(dir)
816        .status()
817        .await
818        .is_ok_and(|s| !s.success());
819
820    if !has_staged {
821        tracing::warn!(
822            "after removing workflow files, no staged changes remain — \
823             the only changes were workflow files"
824        );
825        // Reset back to where we were — nothing to push
826        // Use the original HEAD from the first (newest) commit
827        if let Some((hash, _)) = commits.last() {
828            let _ = Command::new("git")
829                .args(["reset", "--soft", hash])
830                .current_dir(dir)
831                .output_with_context()
832                .await;
833        }
834        return Ok(false);
835    }
836
837    // Use the first commit's message (oldest commit)
838    let commit_msg = if commits.len() == 1 {
839        commits[0].1.clone()
840    } else {
841        // Combine messages when squashing
842        let mut combined = String::new();
843        for (i, (_, msg)) in commits.iter().enumerate() {
844            if i > 0 {
845                combined.push_str("\n\n");
846            }
847            combined.push_str(msg);
848        }
849        combined.push_str("\n\n[workflow files stripped: token lacks `workflow` scope]");
850        combined
851    };
852
853    // A repair of commits already made, so it lands through git even where shipcheck commits
854    let commit = super::commit::git_commit(dir, &commit_msg, "orchestrator").await?;
855
856    if !commit.status.success() {
857        let stderr = String::from_utf8_lossy(&commit.stderr);
858        // Recovery: restore the original commits so the worktree isn't stuck
859        if let Some((original_head, _)) = commits.last() {
860            let _ = Command::new("git")
861                .args(["reset", "--hard", original_head])
862                .current_dir(dir)
863                .output_with_context()
864                .await;
865        }
866        anyhow::bail!("git commit failed during workflow file stripping: {stderr}");
867    }
868
869    tracing::info!(
870        stripped_files = ?workflow_files,
871        original_commits = commit_count,
872        "successfully stripped workflow files from commits"
873    );
874
875    Ok(true)
876}
877
878const LEASE_ABSENT: &str = "absent";
879
880/// Force pushes that found no recorded lease and fell back to the fetched tip
881static LEASE_FALLBACKS: AtomicU64 = AtomicU64::new(0);
882
883async fn git_stdout(dir: &Path, args: &[&str]) -> Option<String> {
884    let out = Command::new("git")
885        .args(args)
886        .current_dir(dir)
887        .output_with_context()
888        .await
889        .ok()?;
890    if !out.status.success() {
891        return None;
892    }
893
894    Some(String::from_utf8_lossy(&out.stdout).trim().to_string())
895}
896
897fn lease_key(branch: &str) -> String {
898    format!("branch.{branch}.orchPushLease")
899}
900
901async fn read_push_lease(dir: &Path, branch: &str) -> Option<String> {
902    git_stdout(dir, &["config", "--get", &lease_key(branch)]).await
903}
904
905async fn write_push_lease(dir: &Path, branch: &str, value: &str) {
906    if git_stdout(dir, &["config", &lease_key(branch), value])
907        .await
908        .is_none()
909    {
910        tracing::warn!(branch, "failed to record push lease");
911    }
912}
913
914async fn remote_tracking_sha(dir: &Path, branch: &str) -> Option<String> {
915    let rev = format!("refs/remotes/origin/{branch}");
916    git_stdout(dir, &["rev-parse", "--verify", "--quiet", &rev])
917        .await
918        .filter(|sha| http::is_full_commit_sha(sha))
919}
920
921async fn is_ancestor(dir: &Path, ancestor: &str, descendant: &str) -> bool {
922    git_stdout(dir, &["merge-base", "--is-ancestor", ancestor, descendant])
923        .await
924        .is_some()
925}
926
927/// Record the remote tip this worktree may replace, before a fetch moves `origin/<branch>`. The lease
928/// moves only to a tip HEAD contains, such as the agent's own push, never to one only fetched
929pub(crate) async fn record_push_lease(dir: &Path, branch: &str, default_branch: &str) {
930    if branch.is_empty() || branch == default_branch {
931        return;
932    }
933
934    let tip = remote_tracking_sha(dir, branch).await;
935    match (read_push_lease(dir, branch).await, tip) {
936        (Some(lease), Some(tip)) if lease != tip && is_ancestor(dir, &tip, "HEAD").await => {
937            write_push_lease(dir, branch, &tip).await;
938        }
939        (Some(_), _) => {}
940        (None, tip) => {
941            let expect = tip.unwrap_or_else(|| LEASE_ABSENT.to_string());
942            write_push_lease(dir, branch, &expect).await;
943        }
944    }
945}
946
947/// Drop the lease of a branch whose remote was deleted, so the next dispatch records one for the recreated branch
948pub(crate) async fn clear_push_lease(dir: &Path, branch: &str) {
949    let _ = git_stdout(dir, &["config", "--unset", &lease_key(branch)]).await;
950}
951
952async fn note_pushed_head(dir: &Path, branch: &str) {
953    if let Some(sha) = git_stdout(dir, &["rev-parse", "--verify", "HEAD"]).await {
954        write_push_lease(dir, branch, &sha).await;
955    }
956}
957
958/// True when `tip` is GitHub's "Update branch" of `lease`: a merge with first parent `lease` and
959/// second parent on the default branch, or a rebase whose commits all have a twin in `lease`
960async fn is_github_branch_update(dir: &Path, tip: &str, lease: &str, default_branch: &str) -> bool {
961    let base = format!("origin/{default_branch}");
962    let Some(line) = git_stdout(dir, &["rev-list", "--parents", "-n", "1", tip]).await else {
963        return false;
964    };
965    let parents: Vec<&str> = line.split_whitespace().skip(1).collect();
966    if let [first, second] = parents[..] {
967        return first == lease && is_ancestor(dir, second, &base).await;
968    }
969
970    // `git cherry` marks each commit in base..tip with `-` when `lease` holds the same patch
971    let Some(cherry) = git_stdout(dir, &["cherry", lease, tip, &base]).await else {
972        return false;
973    };
974    is_ancestor(dir, &base, tip).await
975        && !cherry.is_empty()
976        && cherry.lines().all(|l| l.starts_with('-'))
977}
978
979async fn expected_force_sha(
980    dir: &Path,
981    branch: &str,
982    default_branch: &str,
983) -> anyhow::Result<String> {
984    let tip = remote_tracking_sha(dir, branch).await;
985    let base = format!("origin/{default_branch}");
986    if let Some(tip) = &tip {
987        // A tip the default branch already holds has no commits of its own, so a force push loses nothing
988        if is_ancestor(dir, tip, &base).await {
989            tracing::info!(branch, %tip, "remote tip is contained in the default branch, leasing it");
990            return Ok(tip.clone());
991        }
992    }
993
994    match read_push_lease(dir, branch).await {
995        Some(lease) if lease == LEASE_ABSENT => anyhow::bail!(
996            "force push rejected: remote branch {branch} was absent when the lease was recorded"
997        ),
998        Some(lease) => match tip {
999            Some(tip)
1000                if tip != lease
1001                    && is_github_branch_update(dir, &tip, &lease, default_branch).await =>
1002            {
1003                tracing::info!(branch, %lease, %tip, "remote tip is GitHub's update of the leased branch, leasing it");
1004                Ok(tip)
1005            }
1006            _ => Ok(lease),
1007        },
1008        None => {
1009            let Some(tip) = tip else {
1010                anyhow::bail!("force push rejected: no remote sha recorded for {branch}");
1011            };
1012            let fallbacks = LEASE_FALLBACKS.fetch_add(1, Ordering::Relaxed) + 1;
1013            tracing::warn!(
1014                branch,
1015                fallbacks,
1016                "no push lease recorded, using the fetched origin tip"
1017            );
1018            Ok(tip)
1019        }
1020    }
1021}
1022
1023/// Force-push `branch` only if the remote still holds the leased sha
1024pub(crate) async fn force_push_leased(
1025    dir: &Path,
1026    branch: &str,
1027    default_branch: &str,
1028) -> anyhow::Result<()> {
1029    let expected = expected_force_sha(dir, branch, default_branch).await?;
1030    let lease = format!("--force-with-lease={branch}:{expected}");
1031    tracing::info!(branch, expected_sha = %expected, "force-pushing with an expected remote sha");
1032    let output = git()
1033        .await
1034        .args([
1035            "-c",
1036            "lfs.locksverify=false",
1037            "push",
1038            &lease,
1039            "-u",
1040            "origin",
1041            branch,
1042        ])
1043        .current_dir(dir)
1044        .kill_on_drop(true)
1045        .output_with_context()
1046        .await?;
1047    if output.status.success() {
1048        note_pushed_head(dir, branch).await;
1049        return Ok(());
1050    }
1051
1052    let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
1053    anyhow::bail!("{}", sanitize_push_error(&stderr))
1054}
1055
1056/// Push the branch to origin.
1057pub async fn push_branch(dir: &Path, branch: &str, default_branch: &str) -> anyhow::Result<bool> {
1058    let current = get_current_branch(dir).await;
1059    let branch_to_push = if current.is_empty() { branch } else { &current };
1060
1061    // Guard against empty branch names (prevents git config corruption)
1062    if branch_to_push.is_empty() {
1063        anyhow::bail!("cannot push: branch name is empty");
1064    }
1065
1066    // Skip push for main/master
1067    if branch_to_push == "main" || branch_to_push == "master" {
1068        return Ok(false);
1069    }
1070
1071    // Check if there are commits to push and whether the remote branch exists
1072    let (remote_exists, has_unpushed) =
1073        check_push_needed(dir, branch_to_push, default_branch).await;
1074    if remote_exists && !has_unpushed {
1075        // Remote already has this branch and it's up to date — nothing to push.
1076        // Return true so callers know the branch IS on the remote and PR creation
1077        // is safe to attempt (e.g. a PR may already exist or can be created).
1078        tracing::debug!(
1079            branch = branch_to_push,
1080            "remote branch exists and is up to date, skipping push"
1081        );
1082        note_pushed_head(dir, branch_to_push).await;
1083        return Ok(true);
1084    }
1085    if !remote_exists && !has_unpushed {
1086        // Branch doesn't exist on the remote and has no commits ahead of the
1087        // default branch — push it anyway so the branch is created on the remote.
1088        // PR creation will then fail with "No commits between …" (422) which is
1089        // handled downstream, rather than with "head invalid" (422) which is
1090        // harder to distinguish from a real error.
1091        tracing::debug!(
1092            branch = branch_to_push,
1093            "remote branch missing with no new commits — pushing to create it on remote"
1094        );
1095    }
1096
1097    tracing::info!(branch = branch_to_push, "pushing branch");
1098
1099    // First attempt: normal push (no force).
1100    // lfs.locksverify=false skips the auxiliary LFS locks/verify roundtrip in
1101    // the pre-push hook; agents never hold LFS locks, and a transient timeout
1102    // on that endpoint must not fail the whole push.
1103    // A 2-minute timeout prevents an indefinitely-stalled push from blocking
1104    // post-processing and triggering a stuck-task re-dispatch race.
1105    let output = time::timeout(
1106        Duration::from_secs(120),
1107        git()
1108            .await
1109            .args([
1110                "-c",
1111                "lfs.locksverify=false",
1112                "push",
1113                "-u",
1114                "origin",
1115                branch_to_push,
1116            ])
1117            .kill_on_drop(true)
1118            .current_dir(dir)
1119            .output(),
1120    )
1121    .await
1122    .map_err(|_| anyhow::anyhow!("git push timed out after 120s"))??;
1123
1124    if output.status.success() {
1125        tracing::info!(branch = branch_to_push, "push succeeded");
1126        note_pushed_head(dir, branch_to_push).await;
1127        return Ok(true);
1128    }
1129
1130    let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
1131
1132    // Transient network blip (timeout, reset) rather than a ref rejection —
1133    // retry the identical push once before classifying the run as failed.
1134    if is_transient_network_error(&stderr) {
1135        tracing::warn!(
1136            branch = branch_to_push,
1137            stderr = %sanitize_push_error(&stderr),
1138            "push failed with transient network error — retrying once"
1139        );
1140        let retry_output = time::timeout(
1141            Duration::from_secs(120),
1142            git()
1143                .await
1144                .args([
1145                    "-c",
1146                    "lfs.locksverify=false",
1147                    "push",
1148                    "-u",
1149                    "origin",
1150                    branch_to_push,
1151                ])
1152                .kill_on_drop(true)
1153                .current_dir(dir)
1154                .output(),
1155        )
1156        .await
1157        .map_err(|_| anyhow::anyhow!("git push (network retry) timed out after 120s"))??;
1158
1159        if retry_output.status.success() {
1160            tracing::info!(branch = branch_to_push, "push succeeded on network retry");
1161            note_pushed_head(dir, branch_to_push).await;
1162            return Ok(true);
1163        }
1164
1165        let retry_stderr = String::from_utf8_lossy(&retry_output.stderr)
1166            .trim()
1167            .to_string();
1168        tracing::warn!(
1169            branch = branch_to_push,
1170            stderr = %sanitize_push_error(&retry_stderr),
1171            "push retry also failed"
1172        );
1173        anyhow::bail!("push failed: {}", sanitize_push_error(&retry_stderr));
1174    }
1175
1176    // Handle workflow scope error: the token lacks the `workflow` OAuth scope
1177    // required to push changes to `.github/workflows/`. Strip workflow files
1178    // from commits and retry the push.
1179    if is_workflow_scope_error(&stderr) {
1180        tracing::warn!(
1181            branch = branch_to_push,
1182            "push rejected: token lacks `workflow` scope — stripping workflow files and retrying"
1183        );
1184
1185        match strip_workflow_files(dir, default_branch).await {
1186            Ok(true) => {
1187                // Workflow files stripped, retry push
1188                let retry_output = time::timeout(
1189                    Duration::from_secs(120),
1190                    git()
1191                        .await
1192                        .args([
1193                            "-c",
1194                            "lfs.locksverify=false",
1195                            "push",
1196                            "-u",
1197                            "origin",
1198                            branch_to_push,
1199                        ])
1200                        .kill_on_drop(true)
1201                        .current_dir(dir)
1202                        .output(),
1203                )
1204                .await
1205                .map_err(|_| anyhow::anyhow!("git push (retry) timed out after 120s"))??;
1206
1207                if retry_output.status.success() {
1208                    tracing::info!(
1209                        branch = branch_to_push,
1210                        "push succeeded after stripping workflow files"
1211                    );
1212                    note_pushed_head(dir, branch_to_push).await;
1213                    return Ok(true);
1214                }
1215
1216                let retry_stderr = String::from_utf8_lossy(&retry_output.stderr)
1217                    .trim()
1218                    .to_string();
1219
1220                // If still failing with workflow scope, bail with clear message
1221                if is_workflow_scope_error(&retry_stderr) {
1222                    anyhow::bail!(
1223                        "push failed: token lacks `workflow` OAuth scope and workflow files \
1224                         could not be fully stripped — add `workflow` scope to your GitHub \
1225                         token or use a GitHub App for authentication"
1226                    );
1227                }
1228
1229                anyhow::bail!("push failed after stripping workflow files: {retry_stderr}");
1230            }
1231            Ok(false) => {
1232                // No workflow files found to strip (shouldn't happen if error was detected)
1233                anyhow::bail!(
1234                    "push failed: token lacks `workflow` OAuth scope — add `workflow` scope \
1235                     to your GitHub token or use a GitHub App for authentication. \
1236                     Error: {stderr}"
1237                );
1238            }
1239            Err(e) => {
1240                tracing::error!(error = %e, "failed to strip workflow files from commits");
1241                anyhow::bail!(
1242                    "push failed: token lacks `workflow` OAuth scope and automatic \
1243                     recovery failed ({e}) — add `workflow` scope to your GitHub token \
1244                     or use a GitHub App for authentication"
1245                );
1246            }
1247        }
1248    }
1249
1250    // Non-fast-forward after `rebase_on_default`. The lease was recorded before that fetch,
1251    // so this replaces only a tip this worktree already built on
1252    if push_needs_rebase(&stderr) && remote_exists {
1253        tracing::info!(
1254            branch = branch_to_push,
1255            "push rejected (non-fast-forward), force-pushing with lease"
1256        );
1257        let stderr = match force_push_leased(dir, branch_to_push, default_branch).await {
1258            Ok(()) => {
1259                tracing::info!(branch = branch_to_push, "force push succeeded");
1260                return Ok(true);
1261            }
1262            Err(e) => e.to_string(),
1263        };
1264
1265        // Check if force push also hit the workflow scope error
1266        if is_workflow_scope_error(&stderr) {
1267            tracing::warn!(
1268                branch = branch_to_push,
1269                "force push also rejected due to workflow scope — stripping and retrying"
1270            );
1271            match strip_workflow_files(dir, default_branch).await {
1272                Ok(true) => {
1273                    if let Err(e) = force_push_leased(dir, branch_to_push, default_branch).await {
1274                        anyhow::bail!("force push failed after stripping workflow files: {e}");
1275                    }
1276
1277                    tracing::info!(
1278                        branch = branch_to_push,
1279                        "force push succeeded after stripping workflow files"
1280                    );
1281                    return Ok(true);
1282                }
1283                Ok(false) => {
1284                    anyhow::bail!(
1285                        "force push failed: token lacks `workflow` OAuth scope — \
1286                         add `workflow` scope to your GitHub token or use a GitHub App. \
1287                         Error: {stderr}"
1288                    );
1289                }
1290                Err(e) => {
1291                    anyhow::bail!(
1292                        "force push failed: token lacks `workflow` scope and recovery \
1293                         failed ({e})"
1294                    );
1295                }
1296            }
1297        }
1298
1299        // The remote branch moved since the last fetch (GitHub's "Update branch", another
1300        // clone), so the lease no longer matches. Replay local commits on the new tip once
1301        if is_stale_lease_error(&stderr) && rebase_on_branch(dir, branch_to_push).await? {
1302            let output = git()
1303                .await
1304                .args([
1305                    "-c",
1306                    "lfs.locksverify=false",
1307                    "push",
1308                    "-u",
1309                    "origin",
1310                    branch_to_push,
1311                ])
1312                .current_dir(dir)
1313                .output_with_context()
1314                .await?;
1315            if output.status.success() {
1316                tracing::info!(
1317                    branch = branch_to_push,
1318                    "push succeeded after rebasing on the moved remote branch"
1319                );
1320                note_pushed_head(dir, branch_to_push).await;
1321                return Ok(true);
1322            }
1323
1324            let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
1325            anyhow::bail!("push failed after rebasing on the moved remote branch: {stderr}");
1326        }
1327
1328        if is_stale_lease_error(&stderr) {
1329            tracing::info!(
1330                branch = branch_to_push,
1331                "remote branch already holds every local commit, nothing to push"
1332            );
1333            return Ok(true);
1334        }
1335
1336        anyhow::bail!("force push failed: {stderr}");
1337    }
1338
1339    anyhow::bail!("push failed: {stderr}")
1340}
1341
1342pub fn is_missing_remote_ref_error(err: &anyhow::Error) -> bool {
1343    err.to_string().contains("couldn't find remote ref")
1344}
1345
1346fn push_needs_rebase(stderr: &str) -> bool {
1347    let lower = stderr.to_ascii_lowercase();
1348    lower.contains("non-fast-forward")
1349        || lower.contains("rejected")
1350        || lower.contains("fetch first")
1351        || lower.contains("behind")
1352}
1353
1354/// True when `--force-with-lease` refused because the remote ref is not the one last fetched
1355fn is_stale_lease_error(stderr: &str) -> bool {
1356    let lower = stderr.to_ascii_lowercase();
1357    lower.contains("stale info") || lower.contains("remote ref updated since checkout")
1358}
1359
1360/// True when a push failure looks like a transient network error (dial
1361/// timeout, i/o timeout, connection reset) rather than a ref rejection.
1362/// A refused ref must not be retried, but a blipped endpoint deserves one
1363/// immediate retry before the run is classified as `push_failed`.
1364fn is_transient_network_error(stderr: &str) -> bool {
1365    let lower = stderr.to_ascii_lowercase();
1366    lower.contains("i/o timeout")
1367        || lower.contains("dial tcp")
1368        || lower.contains("connection reset")
1369        || lower.contains("connection refused")
1370        || lower.contains("no route to host")
1371        || lower.contains("temporary failure in name resolution")
1372        || lower.contains("eof")
1373        || lower.contains("fatal error in commit_refs")
1374        || (lower.contains("[remote rejected]") && lower.contains("(failure)"))
1375}
1376
1377/// Strip credentials from push stderr before it is logged or returned:
1378/// URL userinfo (`https://x-access-token:TOKEN@…`) plus any secrets caught
1379/// by the leak patterns, so a failing push can never write a token to logs.
1380fn sanitize_push_error(stderr: &str) -> String {
1381    static URL_USERINFO: LazyLock<Regex> =
1382        LazyLock::new(|| Regex::new(r"://[^/@\s]+@").expect("valid url userinfo regex"));
1383    let no_userinfo = URL_USERINFO.replace_all(stderr, "://***@");
1384    security::redact(&no_userinfo)
1385}
1386
1387/// Drop sentences and list items about pushing or opening the PR
1388/// Agents report they could not push, orch already pushed, so those lines read as false on GitHub
1389fn strip_push_and_pr_creation(text: &str) -> String {
1390    let mut kept_lines: Vec<String> = Vec::new();
1391    for line in text.lines() {
1392        if line.trim().is_empty() {
1393            if kept_lines.last().is_some_and(|l| !l.is_empty()) {
1394                kept_lines.push(String::new());
1395            }
1396            continue;
1397        }
1398
1399        let indent_len = line.len() - line.trim_start().len();
1400        let indent = &line[..indent_len];
1401        let trimmed = line.trim();
1402        let (marker, rest) = match list_marker(trimmed) {
1403            Some((m, r)) => (m, r),
1404            None => ("", trimmed),
1405        };
1406
1407        let kept: Vec<&str> = split_statements(rest)
1408            .into_iter()
1409            .filter(|s| !is_push_or_pr_creation_statement(s))
1410            .collect();
1411        if kept.is_empty() {
1412            continue;
1413        }
1414        kept_lines.push(format!("{indent}{marker}{}", kept.join(" ")));
1415    }
1416    while kept_lines.last().is_some_and(String::is_empty) {
1417        kept_lines.pop();
1418    }
1419    kept_lines.join("\n")
1420}
1421
1422fn split_statements(text: &str) -> Vec<&str> {
1423    static SEP: LazyLock<Regex> =
1424        LazyLock::new(|| Regex::new(r"(\.{2,}|(?:[.!?]))\s+").expect("valid statement separator"));
1425    let mut statements = Vec::new();
1426    let mut last = 0;
1427    for cap in SEP.captures_iter(text) {
1428        let full = cap.get(0).expect("full separator match");
1429        let punct = cap.get(1).expect("punctuation capture");
1430        let ellipsis = punct.as_str().len() >= 2 && punct.as_str().bytes().all(|b| b == b'.');
1431        let end = if ellipsis { punct.start() } else { punct.end() };
1432        let stmt = text[last..end].trim();
1433        if !stmt.is_empty() {
1434            statements.push(stmt);
1435        }
1436        last = full.end();
1437    }
1438    let rest = text[last..].trim();
1439    if !rest.is_empty() {
1440        statements.push(rest);
1441    }
1442    statements
1443}
1444
1445fn list_marker(line: &str) -> Option<(&str, &str)> {
1446    for prefix in ["- ", "* ", "+ "] {
1447        if let Some(rest) = line.strip_prefix(prefix) {
1448            return Some((prefix, rest));
1449        }
1450    }
1451    let dot = line.find(". ")?;
1452    if !line[..dot].is_empty() && line[..dot].bytes().all(|b| b.is_ascii_digit()) {
1453        Some((&line[..dot + 2], &line[dot + 2..]))
1454    } else {
1455        None
1456    }
1457}
1458
1459fn is_push_or_pr_creation_statement(text: &str) -> bool {
1460    static PAT: LazyLock<Regex> = LazyLock::new(|| {
1461        Regex::new(concat!(
1462            r"(?i)",
1463            r"\bgit\s+push\b",
1464            r"|\bgh\s+pr\s+create\b",
1465            r"|\bgh\s+pr\s+open\b",
1466            r"|\bpush\s+(was\s+)?(denied|blocked|rejected)\b",
1467            r"|\bpush\s+permission\b",
1468            r"|\bcouldn['’]?t\s+push\b",
1469            r"|\bcould\s+not\s+push\b",
1470            r"|\bcan['’]?t\s+push\b",
1471            r"|\bcannot\s+push\b",
1472            r"|\bdidn['’]?t\s+push\b",
1473            r"|\bdid\s+not\s+push\b",
1474            r"|\bnothing\s+is\s+on\s+the\s+remote\b",
1475            r"|\bnot\s+on\s+the\s+remote\b",
1476            r"|\bopen(?:ing)?\s+(?:the|a)\s+pr\b",
1477            r"|\bcreat(?:e|ing)\s+(?:the|a)\s+pr\b",
1478            r"|\bopen(?:ing)?\s+(?:the|a)\s+pull\s+request\b",
1479            r"|\bcreat(?:e|ing)\s+(?:the|a)\s+pull\s+request\b",
1480        ))
1481        .expect("valid push/pr-creation regex")
1482    });
1483    PAT.is_match(text)
1484}
1485
1486fn cleaned_pr_items(items: &[String]) -> Vec<String> {
1487    items
1488        .iter()
1489        .filter_map(|item| {
1490            let cleaned = strip_push_and_pr_creation(item);
1491            (!cleaned.is_empty()).then_some(cleaned)
1492        })
1493        .collect()
1494}
1495
1496/// Format the GitHub PR description from the agent summary, stripping push and PR-creation statements
1497#[expect(
1498    clippy::too_many_arguments,
1499    reason = "each argument is one section of the PR body"
1500)]
1501fn build_pr_body(
1502    title: &str,
1503    summary: &str,
1504    accomplished: &[String],
1505    remaining: &[String],
1506    files: &[String],
1507    task_id: &str,
1508    agent: &str,
1509    model: Option<&str>,
1510) -> String {
1511    let cleaned_summary = strip_push_and_pr_creation(summary);
1512    let summary_text = if cleaned_summary.is_empty() {
1513        title
1514    } else {
1515        cleaned_summary.as_str()
1516    };
1517    let accomplished = cleaned_pr_items(accomplished);
1518    let remaining = cleaned_pr_items(remaining);
1519
1520    let mut body = format!("## Summary\n\n{summary_text}");
1521
1522    if !accomplished.is_empty() {
1523        body.push_str("\n\n### What was done\n\n");
1524        for item in &accomplished {
1525            let _ = writeln!(body, "- {item}");
1526        }
1527    }
1528
1529    if !remaining.is_empty() {
1530        body.push_str("\n\n### Remaining\n\n");
1531        for item in &remaining {
1532            let _ = writeln!(body, "- {item}");
1533        }
1534    }
1535
1536    if !files.is_empty() {
1537        body.push_str("\n\n### Files changed\n\n");
1538        for file in files {
1539            let _ = writeln!(body, "- `{file}`");
1540        }
1541    }
1542
1543    if !task_id.starts_with("internal:")
1544        && let Ok(issue_num) = task_id.parse::<u64>()
1545    {
1546        let _ = write!(body, "\n\nCloses #{issue_num}");
1547    }
1548
1549    let model_str = model.map(|m| format!(" using `{m}`")).unwrap_or_default();
1550    let task_ref = format_task_ref(task_id);
1551    let _ = write!(
1552        body,
1553        "\n\n---\n*Task {task_ref} · Created by {agent}[bot] via [Orch](https://github.com/gabrielkoerich/orch){model_str}*"
1554    );
1555    body
1556}
1557
1558/// Create a PR if one doesn't already exist.
1559///
1560/// Uses [`GhHttp`] exclusively for GitHub API operations. No `gh` CLI
1561/// fallback is used, ensuring consistent behavior across environments.
1562///
1563/// # Returns
1564/// - `Ok(url)` - PR URL (newly created or already existed)
1565/// - `Err(PrCreateError)` - API error or other failure
1566#[expect(
1567    clippy::too_many_arguments,
1568    reason = "the PR needs its branch, body sections, attribution and target together"
1569)]
1570pub async fn create_pr_if_needed(
1571    dir: &Path,
1572    branch: &str,
1573    title: &str,
1574    summary: &str,
1575    accomplished: &[String],
1576    remaining: &[String],
1577    files: &[String],
1578    task_id: &str,
1579    agent: &str,
1580    model: Option<&str>,
1581    repo: &str,
1582    base_branch: &str,
1583) -> PrCreateResult<String> {
1584    // Retries for transient 5xx errors on PR creation below
1585    const MAX_RETRIES: u32 = 3;
1586    let gh = GhHttp::new()?;
1587
1588    // Check if PR already exists using GhHttp API
1589    match gh.get_pr_number(repo, branch).await {
1590        Ok(Some(pr_number)) => {
1591            tracing::info!(task_id, pr = pr_number, "PR already exists");
1592            // Append attribution footer if the agent created the PR without one.
1593            append_pr_footer_if_missing(&gh, repo, pr_number, task_id, agent, model).await;
1594            // Fetch the existing PR to get its URL so the caller can store pr_number.
1595            let pr = match gh.get_pr(repo, pr_number).await {
1596                Ok(pr) => pr,
1597                Err(e) => {
1598                    tracing::warn!(task_id, error = %e, "get_pr API call failed after detecting existing PR");
1599                    return Err(PrCreateError::ApiError(e));
1600                }
1601            };
1602            return Ok(pr.html_url);
1603        }
1604        Ok(None) => {
1605            // No PR exists, proceed to create one
1606        }
1607        Err(e) => {
1608            tracing::warn!(task_id, error = %e, "get_pr_number API call failed");
1609            return Err(PrCreateError::ApiError(e));
1610        }
1611    }
1612
1613    // When the agent didn't self-report files, derive them from git.
1614    let git_files: Vec<String>;
1615    let effective_files: &[String] = if files.is_empty() {
1616        match Command::new("git")
1617            .args([
1618                "diff",
1619                &format!("origin/{base_branch}...HEAD"),
1620                "--name-only",
1621            ])
1622            .current_dir(dir)
1623            .output()
1624            .await
1625        {
1626            Ok(output) if output.status.success() => {
1627                git_files = String::from_utf8_lossy(&output.stdout)
1628                    .lines()
1629                    .filter(|l| !l.is_empty())
1630                    .map(ToString::to_string)
1631                    .collect();
1632                &git_files
1633            }
1634            _ => files,
1635        }
1636    } else {
1637        files
1638    };
1639
1640    let body = build_pr_body(
1641        title,
1642        summary,
1643        accomplished,
1644        remaining,
1645        effective_files,
1646        task_id,
1647        agent,
1648        model,
1649    );
1650
1651    // Always use the short task title for the PR title (summary goes in body)
1652    let pr_title = title;
1653
1654    // Create PR using GhHttp API with exponential backoff retry for transient 5xx errors.
1655    let mut last_error: Option<anyhow::Error> = None;
1656    let mut created_url: Option<String> = None;
1657    let mut had_transient_error = false;
1658    let mut effective_base = base_branch.to_string();
1659    let mut tried_api_fallback = false;
1660
1661    for attempt in 1..=MAX_RETRIES {
1662        match gh
1663            .create_pr(repo, pr_title, &body, branch, &effective_base)
1664            .await
1665        {
1666            Ok(u) => {
1667                created_url = Some(u);
1668                break;
1669            }
1670            Err(e) => {
1671                let err_str = format!("{e}");
1672                if is_transient_github_error(&err_str) {
1673                    had_transient_error = true;
1674                    if attempt < MAX_RETRIES {
1675                        let delay = Duration::from_secs(2u64.pow(attempt));
1676                        tracing::warn!(
1677                            task_id,
1678                            attempt,
1679                            delay_secs = delay.as_secs(),
1680                            error = %e,
1681                            "transient GitHub API error during PR creation — retrying"
1682                        );
1683                        time::sleep(delay).await;
1684                    }
1685                } else if is_invalid_base_error(&err_str) && !tried_api_fallback {
1686                    // Local base branch detection was wrong (e.g. "remotes/origin/main").
1687                    // Query GitHub API for the canonical default branch and retry once.
1688                    tried_api_fallback = true;
1689                    tracing::warn!(
1690                        task_id,
1691                        base_branch = %effective_base,
1692                        error = %e,
1693                        "PR creation failed with invalid base — fetching default branch from GitHub API"
1694                    );
1695                    match gh.get_default_branch(repo).await {
1696                        Ok(api_default) if api_default != effective_base => {
1697                            tracing::info!(
1698                                task_id,
1699                                old_base = %effective_base,
1700                                new_base = %api_default,
1701                                "retrying PR creation with GitHub API default branch"
1702                            );
1703                            effective_base = api_default;
1704                            continue;
1705                        }
1706                        Ok(api_default) => {
1707                            tracing::warn!(
1708                                task_id,
1709                                base_branch = %api_default,
1710                                "GitHub API default branch matches local — no fallback possible"
1711                            );
1712                        }
1713                        Err(api_err) => {
1714                            tracing::warn!(
1715                                task_id,
1716                                error = %api_err,
1717                                "failed to fetch default branch from GitHub API"
1718                            );
1719                        }
1720                    }
1721                }
1722                last_error = Some(e);
1723            }
1724        }
1725    }
1726
1727    let url = if let Some(u) = created_url {
1728        u
1729    } else {
1730        let e = last_error.unwrap_or_else(|| {
1731            anyhow::anyhow!("PR creation failed after retries (no error captured)")
1732        });
1733        let err_str = format!("{e}");
1734        // For transient 5xx errors, GitHub may have created the PR despite returning
1735        // an error (e.g. 502 after the write succeeded). Re-check for an existing PR
1736        // before propagating the failure so we don't orphan the PR from the task.
1737        // Also check when the final error is non-transient but a prior attempt had a
1738        // transient error — the PR may have been created on that earlier attempt (e.g.
1739        // attempt 1 returns 502, attempt 2 returns 422 "already exists").
1740        if is_transient_github_error(&err_str) || had_transient_error {
1741            tracing::warn!(
1742                task_id,
1743                error = %e,
1744                "transient GitHub API error after all retries — checking if PR was actually created"
1745            );
1746            match gh.get_pr_number(repo, branch).await {
1747                Ok(Some(pr_number)) => {
1748                    tracing::info!(
1749                        task_id,
1750                        pr = pr_number,
1751                        "PR was created despite transient errors — recovering"
1752                    );
1753                    append_pr_footer_if_missing(&gh, repo, pr_number, task_id, agent, model).await;
1754                    match gh.get_pr(repo, pr_number).await {
1755                        Ok(pr) => pr.html_url,
1756                        Err(get_err) => {
1757                            tracing::warn!(
1758                                task_id,
1759                                error = %get_err,
1760                                "failed to fetch PR URL after transient-error recovery"
1761                            );
1762                            return Err(PrCreateError::ApiError(e));
1763                        }
1764                    }
1765                }
1766                Ok(None) => {
1767                    tracing::warn!(
1768                        task_id,
1769                        "PR was not created after all retries — propagating original error"
1770                    );
1771                    return Err(PrCreateError::ApiError(e));
1772                }
1773                Err(check_err) => {
1774                    tracing::warn!(
1775                        task_id,
1776                        error = %check_err,
1777                        "failed to verify PR existence after transient error retries — propagating original error"
1778                    );
1779                    return Err(PrCreateError::ApiError(e));
1780                }
1781            }
1782        } else {
1783            return Err(PrCreateError::ApiError(e));
1784        }
1785    };
1786
1787    tracing::info!(task_id, pr_url = %url, "created PR via GhHttp API");
1788
1789    Ok(url)
1790}
1791
1792/// Returns true if the error string indicates a transient GitHub API failure
1793/// (HTTP 5xx, network errors, DNS failures, TLS issues, rate limits), where
1794/// the operation may succeed on retry.
1795pub(crate) fn is_transient_github_error(err_str: &str) -> bool {
1796    // Treat transport/send failures and explicit GhHttp server-error messages
1797    // as transient. Older callers relied on parsing the "failed (NNN)" pattern
1798    // to detect 5xx — keep that, but also recognize the newer wrappers.
1799    if err_str.contains("HTTP send failed") {
1800        return true;
1801    }
1802    if err_str.contains("GitHub API server error") {
1803        return true;
1804    }
1805
1806    // Check for HTTP 5xx status codes
1807    if extract_github_http_status(err_str).is_some_and(|s| (500..600).contains(&s)) {
1808        return true;
1809    }
1810
1811    // Check for common network-level transient errors
1812    let lower = err_str.to_lowercase();
1813
1814    // Connection and transport errors
1815    if lower.contains("connection reset")
1816        || lower.contains("broken pipe")
1817        || lower.contains("connection refused")
1818        || lower.contains("connection closed")
1819        || lower.contains("transport error")
1820        || lower.contains("failed to connect")
1821        || lower.contains("failed to connect to github.com port")
1822        || lower.contains("curl: (7) failed to connect")
1823        || lower.contains("couldn't connect to server")
1824        || lower.contains("could not connect to server")
1825    {
1826        return true;
1827    }
1828
1829    // Timeout errors
1830    if lower.contains("timeout") {
1831        return true;
1832    }
1833
1834    // DNS resolution errors
1835    if lower.contains("dns error")
1836        || lower.contains("resolve")
1837        || lower.contains("name resolution")
1838        || lower.contains("no such host")
1839    {
1840        return true;
1841    }
1842
1843    // TLS/SSL errors
1844    if lower.contains("tls handshake")
1845        || lower.contains("certificate")
1846        || lower.contains("ssl error")
1847    {
1848        return true;
1849    }
1850
1851    // Network unreachable errors
1852    if lower.contains("network is unreachable")
1853        || lower.contains("host unreachable")
1854        || lower.contains("temporary failure")
1855    {
1856        return true;
1857    }
1858
1859    // EOF errors (connection dropped mid-request)
1860    if lower.contains("unexpected eof") || lower.contains("unexpected end of file") {
1861        return true;
1862    }
1863
1864    // Internal circuit-breaker state (orch's own GitHub 5xx protection)
1865    if lower.contains("circuit-breaker") || lower.contains("circuit breaker") {
1866        return true;
1867    }
1868
1869    // Rate limit errors (transient with cooldown/retry)
1870    if lower.contains("rate limit") || lower.contains("too many requests") {
1871        return true;
1872    }
1873
1874    false
1875}
1876
1877/// Returns true when the error indicates the PR base branch is invalid.
1878///
1879/// GitHub returns HTTP 422 with `PullRequest.base invalid` when the base
1880/// branch name is not a valid ref in the repository. This is a terminal
1881/// configuration error — retrying with the same base will never succeed.
1882pub(crate) fn is_invalid_base_error(err_str: &str) -> bool {
1883    let lower = err_str.to_lowercase();
1884    lower.contains("422") && lower.contains("base") && lower.contains("invalid")
1885}
1886
1887/// Returns true when an error looks like a transient *GitHub API/CLI* failure.
1888///
1889/// This is stricter than [`is_transient_github_error`]: in addition to matching
1890/// a transient transport pattern, it requires GitHub-specific context in the
1891/// message. This prevents generic agent/runtime transport failures (for
1892/// example "broken pipe" from a model stream disconnect) from being treated as
1893/// GitHub retry signals in review reroute logic.
1894pub(crate) fn is_transient_github_api_error(err_str: &str) -> bool {
1895    if !is_transient_github_error(err_str) {
1896        return false;
1897    }
1898
1899    if extract_github_http_status(err_str).is_some() {
1900        return true;
1901    }
1902
1903    let lower = err_str.to_lowercase();
1904    lower.contains("github")
1905        || lower.contains("api.github.com")
1906        || lower.contains("gh api")
1907        || lower.contains("gh pr")
1908        || lower.contains("gh error")
1909        // `circuit-breaker` is orch's internal GitHub 5xx protection, set
1910        // exclusively by GhHttp.  It can never originate from agent/runtime
1911        // failures, so treating it as a GitHub-context marker is safe even
1912        // when upstream wrapping strips the "GitHub API" prefix.
1913        || lower.contains("circuit-breaker")
1914        || lower.contains("circuit breaker")
1915}
1916
1917/// Extract the HTTP status code from a GhHttp error string.
1918///
1919/// GhHttp formats errors as: `"GitHub API POST https://... failed (NNN): body"`
1920/// This parses the numeric status code from that format.
1921fn extract_github_http_status(err_str: &str) -> Option<u16> {
1922    let marker = "failed (";
1923    let start = err_str.find(marker)? + marker.len();
1924    let rest = &err_str[start..];
1925    let end = rest.find(')')?;
1926    rest[..end].parse().ok()
1927}
1928
1929/// Append the Orch attribution footer to an existing PR body if not already present.
1930///
1931/// Called when the agent created the PR directly (bypassing orch's
1932/// `create_pr_if_needed`), so the footer was never added by `build_pr_body`.
1933async fn append_pr_footer_if_missing(
1934    gh: &GhHttp,
1935    repo: &str,
1936    pr_number: u64,
1937    task_id: &str,
1938    agent: &str,
1939    model: Option<&str>,
1940) {
1941    let pr = match gh.get_pr(repo, pr_number).await {
1942        Ok(pr) => pr,
1943        Err(e) => {
1944            tracing::debug!(task_id, error = %e, "failed to fetch PR for footer check");
1945            return;
1946        }
1947    };
1948
1949    let body = pr.body.unwrap_or_default();
1950    if body.contains("via [Orch]") {
1951        return; // Footer already present
1952    }
1953
1954    let model_str = model.map(|m| format!(" using `{m}`")).unwrap_or_default();
1955    let task_ref = format_task_ref(task_id);
1956    let footer = format!(
1957        "\n\n---\n*Task {task_ref} · Created by {agent}[bot] via [Orch](https://github.com/gabrielkoerich/orch){model_str}*"
1958    );
1959    let new_body = format!("{body}{footer}");
1960
1961    match gh.update_pr_body(repo, pr_number, &new_body).await {
1962        Err(e) => {
1963            tracing::warn!(task_id, error = %e, "failed to append footer to PR (non-fatal)");
1964        }
1965        _ => {
1966            tracing::info!(
1967                task_id,
1968                pr = pr_number,
1969                "appended attribution footer to agent-created PR"
1970            );
1971        }
1972    }
1973}
1974
1975/// Get the current branch name.
1976pub(crate) async fn get_current_branch(dir: &Path) -> String {
1977    let output = Command::new("git")
1978        .args(["branch", "--show-current"])
1979        .current_dir(dir)
1980        .output_with_context()
1981        .await;
1982
1983    match output {
1984        Ok(o) if o.status.success() => String::from_utf8_lossy(&o.stdout).trim().to_string(),
1985        _ => String::new(),
1986    }
1987}
1988
1989/// Count the number of changed files in the working directory.
1990pub async fn count_changed_files(dir: &Path) -> anyhow::Result<usize> {
1991    // Count modified and new files (excluding deleted)
1992    let output = Command::new("git")
1993        .args(["status", "--porcelain"])
1994        .current_dir(dir)
1995        .output_with_context()
1996        .await?;
1997
1998    let status_output = String::from_utf8_lossy(&output.stdout);
1999    let count = status_output
2000        .lines()
2001        .filter(|line| {
2002            let prefix = line.get(0..2).unwrap_or("");
2003            // Count modified (M), added (A), renamed (R), copied (C), untracked (??)
2004            prefix.starts_with('M')
2005                || prefix.starts_with('A')
2006                || prefix.starts_with('R')
2007                || prefix.starts_with('C')
2008                || prefix.starts_with('?')
2009        })
2010        .count();
2011
2012    Ok(count)
2013}
2014
2015/// Check if there are unpushed commits.
2016/// Returns `(remote_exists, has_unpushed_commits)`.
2017///
2018/// - `remote_exists` — whether `origin/<branch>` is known to the local repo.
2019/// - `has_unpushed_commits` — whether there are local commits not yet on the
2020///   remote.  When the remote branch doesn't exist the comparison is against
2021///   `origin/<default_branch>` so we can still detect new work.
2022async fn check_push_needed(dir: &Path, branch: &str, default_branch: &str) -> (bool, bool) {
2023    // Check if remote tracking branch exists
2024    let remote_exists = Command::new("git")
2025        .args(["rev-parse", &format!("origin/{branch}")])
2026        .current_dir(dir)
2027        .output_with_context()
2028        .await
2029        .is_ok_and(|o| o.status.success());
2030
2031    let compare_ref = if remote_exists {
2032        format!("origin/{branch}..HEAD")
2033    } else {
2034        // Compare against default branch (passed in, NOT detected from
2035        // current HEAD — in a worktree HEAD is the feature branch itself)
2036        format!("origin/{default_branch}..HEAD")
2037    };
2038
2039    tracing::debug!(
2040        branch,
2041        compare_ref,
2042        remote_exists,
2043        "checking unpushed commits"
2044    );
2045
2046    let output = Command::new("git")
2047        .args(["log", &compare_ref, "--oneline"])
2048        .current_dir(dir)
2049        .output_with_context()
2050        .await;
2051
2052    let has_unpushed = match output {
2053        Ok(o) if o.status.success() => {
2054            let out = String::from_utf8_lossy(&o.stdout);
2055            let has = !out.trim().is_empty();
2056            tracing::debug!(branch, has_unpushed = has, "unpushed commits check result");
2057            has
2058        }
2059        _ => {
2060            tracing::debug!(branch, "git log failed for compare ref, assuming unpushed");
2061            // If we can't determine, assume there ARE commits to push
2062            // (better to attempt push and let it fail than silently skip)
2063            true
2064        }
2065    };
2066
2067    (remote_exists, has_unpushed)
2068}
2069
2070#[cfg(test)]
2071mod tests {
2072    use std::env;
2073    use std::fs;
2074    #[cfg(unix)]
2075    use std::os::unix::fs::PermissionsExt;
2076    use std::path::PathBuf;
2077    use std::process;
2078
2079    use super::*;
2080
2081    #[test]
2082    fn pr_create_error_display() {
2083        let err = PrCreateError::ApiError(anyhow::anyhow!("test error"));
2084        assert_eq!(format!("{err}"), "GitHub API error: test error");
2085    }
2086
2087    #[test]
2088    fn pr_create_error_from_anyhow() {
2089        let anyhow_err = anyhow::anyhow!("source error");
2090        let pr_err: PrCreateError = anyhow_err.into();
2091        assert!(matches!(pr_err, PrCreateError::ApiError(_)));
2092    }
2093
2094    #[test]
2095    fn git_env_never_signs_with_or_without_a_token() {
2096        for env in [
2097            build_git_auth_env_for_token(None),
2098            build_git_auth_env_for_token(Some("ghp_testtoken1234".into())),
2099        ] {
2100            let get = |key: &str| env.iter().find(|(k, _)| k == key).map(|(_, v)| v.clone());
2101            let count: usize = get("GIT_CONFIG_COUNT").unwrap().parse().unwrap();
2102            let config: Vec<(String, String)> = (0..count)
2103                .map(|n| {
2104                    (
2105                        get(&format!("GIT_CONFIG_KEY_{n}")).unwrap(),
2106                        get(&format!("GIT_CONFIG_VALUE_{n}")).unwrap(),
2107                    )
2108                })
2109                .collect();
2110            assert!(config.contains(&("commit.gpgsign".into(), "false".into())));
2111            assert!(config.contains(&("tag.gpgsign".into(), "false".into())));
2112        }
2113    }
2114
2115    #[test]
2116    fn build_git_auth_env_without_token_falls_back_to_ssh_https_conversion() {
2117        // Pass None directly to test the no-token path without relying on env vars
2118        // or the gh CLI, keeping the test deterministic regardless of local auth state.
2119        let env = build_git_auth_env_for_token(None);
2120
2121        // When no token is available the fallback must include the SSH insteadOf rule.
2122        // The SSH→HTTPS conversion sets the target URL (the rewrite destination) to git@github.com:.
2123        let has_ssh_rule = env
2124            .iter()
2125            .any(|(k, v)| k == "GIT_CONFIG_VALUE_0" && v == "git@github.com:");
2126        assert!(
2127            has_ssh_rule,
2128            "expected SSH insteadOf fallback (value is git@github.com:), got: {env:?}"
2129        );
2130    }
2131
2132    #[test]
2133    fn build_git_auth_env_with_token_covers_both_ssh_and_https() {
2134        // Pass a pre-built token directly to test the token path without touching env vars.
2135        let env = build_git_auth_env_for_token(Some("ghp_testtoken1234".into()));
2136
2137        // Must contain the token in the auth URL (the token is in the value of GIT_CONFIG_KEY_N).
2138        let has_token = env
2139            .iter()
2140            .any(|(_, v)| v.contains("x-access-token:ghp_testtoken1234@github.com"));
2141        assert!(has_token, "expected token in auth URL, got: {env:?}");
2142        // Must cover HTTPS origins (GIT_CONFIG_VALUE_0 is the HTTPS source URL).
2143        let has_https_rule = env
2144            .iter()
2145            .any(|(k, v)| k == "GIT_CONFIG_VALUE_0" && v == "https://github.com/");
2146        assert!(
2147            has_https_rule,
2148            "expected HTTPS insteadOf rule, got: {env:?}"
2149        );
2150        // Must cover SSH origins (GIT_CONFIG_VALUE_1 is the SSH source URL).
2151        let has_ssh_rule = env
2152            .iter()
2153            .any(|(k, v)| k == "GIT_CONFIG_VALUE_1" && v == "git@github.com:");
2154        assert!(has_ssh_rule, "expected SSH insteadOf rule, got: {env:?}");
2155    }
2156
2157    #[test]
2158    fn find_stash_ref_by_hash_returns_matching_reference() {
2159        let stash_list = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa stash@{1}\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb stash@{0}";
2160
2161        assert_eq!(
2162            find_stash_ref_by_hash(stash_list, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
2163            Some("stash@{1}".to_string())
2164        );
2165        assert_eq!(
2166            find_stash_ref_by_hash(stash_list, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"),
2167            Some("stash@{0}".to_string())
2168        );
2169    }
2170
2171    #[test]
2172    fn find_stash_ref_by_hash_returns_none_when_missing_or_malformed() {
2173        let stash_list = "malformed-line\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa stash@{2}";
2174
2175        assert_eq!(
2176            find_stash_ref_by_hash(stash_list, "cccccccccccccccccccccccccccccccccccccccc"),
2177            None
2178        );
2179        assert_eq!(find_stash_ref_by_hash("", "anything"), None);
2180    }
2181
2182    #[test]
2183    fn push_needs_rebase_detects_common_rejections() {
2184        assert!(push_needs_rebase(
2185            "! [rejected] branch -> branch (fetch first)"
2186        ));
2187        assert!(push_needs_rebase("non-fast-forward update was rejected"));
2188        assert!(push_needs_rebase(
2189            "push rejected because the remote contains work"
2190        ));
2191    }
2192
2193    #[test]
2194    fn is_transient_network_error_detects_lfs_locks_timeout() {
2195        let lfs_err = "Post \"https://x-access-token:***@github.com/o/r.git/info/lfs/locks/verify\": dial tcp 140.82.112.4:443: i/o timeout";
2196        assert!(is_transient_network_error(lfs_err));
2197        assert!(is_transient_network_error(
2198            "dial tcp 140.82.112.4:443: connect: connection refused"
2199        ));
2200        assert!(is_transient_network_error(
2201            "read tcp 10.0.0.1:51234->140.82.112.4:443: read: connection reset by peer"
2202        ));
2203    }
2204
2205    #[test]
2206    fn is_transient_network_error_rejects_ref_rejections() {
2207        assert!(!is_transient_network_error(
2208            "! [rejected] branch -> branch (fetch first)"
2209        ));
2210        assert!(!is_transient_network_error(
2211            "remote: error: GH006: Protected branch update failed"
2212        ));
2213    }
2214
2215    #[test]
2216    fn is_transient_network_error_detects_github_remote_failure() {
2217        assert!(is_transient_network_error(
2218            "remote: fatal error in commit_refs\n ! [remote rejected] b -> b (failure)"
2219        ));
2220        assert!(is_transient_network_error(
2221            "! [remote rejected] b -> b (failure)"
2222        ));
2223    }
2224
2225    #[test]
2226    fn missing_remote_ref_error_is_detected() {
2227        let err = anyhow::anyhow!("failed to fetch origin/b: fatal: couldn't find remote ref b");
2228        assert!(is_missing_remote_ref_error(&err));
2229        assert!(!is_missing_remote_ref_error(&anyhow::anyhow!("conflict")));
2230    }
2231
2232    #[test]
2233    fn sanitize_push_error_strips_url_credentials() {
2234        // Shape must not look like a real GitHub PAT so gitleaks stays clean
2235        let raw = "Post \"https://x-access-token:test-secret-1234567890abcdef@github.com/o/r.git/info/lfs/locks/verify\": dial tcp 140.82.112.4:443: i/o timeout";
2236        let sanitized = sanitize_push_error(raw);
2237        assert!(!sanitized.contains("test-secret-1234567890abcdef"));
2238        assert!(sanitized.contains("://***@github.com"));
2239        assert!(sanitized.contains("i/o timeout"));
2240    }
2241
2242    #[test]
2243    fn extract_github_http_status_parses_ghhttp_error_format() {
2244        let err =
2245            "GitHub API POST https://api.github.com/repos/foo/bar/pulls failed (502): Bad Gateway";
2246        assert_eq!(extract_github_http_status(err), Some(502));
2247
2248        let err500 = "GitHub API POST https://api.github.com/repos/foo/bar/pulls failed (500): Internal Server Error";
2249        assert_eq!(extract_github_http_status(err500), Some(500));
2250
2251        let err422 = "GitHub API POST https://api.github.com/repos/foo/bar/pulls failed (422): Unprocessable Entity";
2252        assert_eq!(extract_github_http_status(err422), Some(422));
2253
2254        let not_github = "some other error";
2255        assert_eq!(extract_github_http_status(not_github), None);
2256    }
2257
2258    #[test]
2259    fn is_transient_github_error_matches_5xx_only() {
2260        let err502 =
2261            "GitHub API POST https://api.github.com/repos/foo/bar/pulls failed (502): Bad Gateway";
2262        assert!(is_transient_github_error(err502));
2263
2264        let err503 = "GitHub API POST https://api.github.com/repos/foo/bar/pulls failed (503): Service Unavailable";
2265        assert!(is_transient_github_error(err503));
2266
2267        let err422 = "GitHub API POST https://api.github.com/repos/foo/bar/pulls failed (422): Unprocessable Entity";
2268        assert!(!is_transient_github_error(err422));
2269
2270        let err404 =
2271            "GitHub API POST https://api.github.com/repos/foo/bar/pulls failed (404): Not Found";
2272        assert!(!is_transient_github_error(err404));
2273    }
2274
2275    #[test]
2276    fn is_transient_github_error_matches_connection_errors() {
2277        // Connection-related transient errors
2278        assert!(is_transient_github_error("connection refused"));
2279        assert!(is_transient_github_error("Connection reset by peer"));
2280        assert!(is_transient_github_error("broken pipe"));
2281        assert!(is_transient_github_error("connection closed unexpectedly"));
2282        assert!(is_transient_github_error(
2283            "transport error: connection lost"
2284        ));
2285
2286        // Git/Curl connect-failure patterns (review push failures)
2287        assert!(is_transient_github_error(
2288            "fatal: unable to access 'https://github.com/owner/repo.git/': Failed to connect to github.com port 443 after 20990 ms: Couldn't connect to server"
2289        ));
2290        assert!(is_transient_github_error(
2291            "Failed to connect to github.com port 443: Connection timed out"
2292        ));
2293        assert!(is_transient_github_error(
2294            "Failed to connect to github.com port 443: Connection refused"
2295        ));
2296        assert!(is_transient_github_error(
2297            "curl: (7) Failed to connect to github.com port 443: Couldn't connect to server"
2298        ));
2299        assert!(is_transient_github_error(
2300            "fatal: unable to access 'https://github.com/owner/repo.git/': Couldn't connect to server"
2301        ));
2302    }
2303
2304    #[test]
2305    fn is_transient_github_error_matches_dns_errors() {
2306        // DNS resolution failures
2307        assert!(is_transient_github_error("dns error: failed to lookup"));
2308        assert!(is_transient_github_error("failed to resolve hostname"));
2309        assert!(is_transient_github_error("name resolution failed"));
2310        assert!(is_transient_github_error("no such host: api.github.com"));
2311    }
2312
2313    #[test]
2314    fn is_transient_github_error_matches_tls_errors() {
2315        // TLS/SSL handshake failures
2316        assert!(is_transient_github_error("tls handshake failed"));
2317        assert!(is_transient_github_error("certificate verification failed"));
2318        assert!(is_transient_github_error("ssl error: unknown ca"));
2319    }
2320
2321    #[test]
2322    fn is_transient_github_error_matches_network_unreachable() {
2323        // Network unreachable errors
2324        assert!(is_transient_github_error("network is unreachable"));
2325        assert!(is_transient_github_error("host unreachable"));
2326        assert!(is_transient_github_error(
2327            "temporary failure in name resolution"
2328        ));
2329    }
2330
2331    #[test]
2332    fn is_transient_github_error_matches_eof_errors() {
2333        // EOF/connection dropped errors
2334        assert!(is_transient_github_error("unexpected eof while reading"));
2335        assert!(is_transient_github_error("unexpected end of file"));
2336    }
2337
2338    #[test]
2339    fn is_transient_github_error_matches_rate_limit() {
2340        // Rate limit errors (transient with cooldown)
2341        assert!(is_transient_github_error("rate limit exceeded"));
2342        assert!(is_transient_github_error("429 Too Many Requests"));
2343        assert!(is_transient_github_error("too many requests, retry later"));
2344    }
2345
2346    #[test]
2347    fn is_transient_github_error_matches_timeout() {
2348        // Timeout errors
2349        assert!(is_transient_github_error("operation timeout"));
2350        assert!(is_transient_github_error("request timeout"));
2351    }
2352
2353    #[test]
2354    fn is_transient_github_error_rejects_non_transient() {
2355        // Non-transient errors should not match
2356        assert!(!is_transient_github_error("permission denied"));
2357        assert!(!is_transient_github_error("file not found"));
2358        assert!(!is_transient_github_error("invalid argument"));
2359        assert!(!is_transient_github_error("authentication failed"));
2360        assert!(!is_transient_github_error("bad credentials"));
2361    }
2362
2363    #[test]
2364    fn is_transient_github_api_error_requires_github_context() {
2365        assert!(is_transient_github_api_error(
2366            "GitHub API POST https://api.github.com/repos/foo/bar/pulls failed (502): Bad Gateway"
2367        ));
2368        assert!(is_transient_github_api_error(
2369            "gh error: transport error: connection reset by peer"
2370        ));
2371        assert!(is_transient_github_api_error(
2372            "push failed: fatal: unable to access 'https://github.com/owner/repo.git/': Failed to connect to github.com port 443 after 20990 ms: Couldn't connect to server"
2373        ));
2374        assert!(!is_transient_github_api_error(
2375            "codex failed: Reconnecting... (stream disconnected before completion: Broken pipe)"
2376        ));
2377        assert!(!is_transient_github_api_error(
2378            "agent stream failed: failed to connect to local relay"
2379        ));
2380    }
2381
2382    #[test]
2383    fn is_transient_github_api_error_matches_circuit_breaker() {
2384        // Full message from GhHttp
2385        assert!(is_transient_github_api_error(
2386            "GitHub API transient 5xx circuit-breaker active for 240s"
2387        ));
2388        // Wrapped through PrCreateError + response_handler
2389        assert!(is_transient_github_api_error(
2390            "create PR failed: GitHub API error: GitHub API transient 5xx circuit-breaker active"
2391        ));
2392        // Shortened form where upstream wrapping stripped the "GitHub API" prefix —
2393        // circuit-breaker alone must still be recognized as a GitHub signal.
2394        assert!(is_transient_github_api_error(
2395            "create PR failed: circuit-breaker"
2396        ));
2397        // Spaced variant
2398        assert!(is_transient_github_api_error(
2399            "create PR failed: circuit breaker tripped"
2400        ));
2401    }
2402
2403    #[tokio::test]
2404    async fn auto_commit_unstages_files_on_commit_failure() {
2405        // Create a temp git repo with one initial commit
2406        let dir = env::temp_dir().join(format!("orch_test_auto_commit_{}", process::id()));
2407        let _ = fs::remove_dir_all(&dir);
2408        fs::create_dir_all(&dir).unwrap();
2409
2410        // Init repo
2411        let _ = Command::new("git")
2412            .args(["init"])
2413            .current_dir(&dir)
2414            .output()
2415            .await;
2416        let _ = Command::new("git")
2417            .args(["config", "user.email", "test@test.com"])
2418            .current_dir(&dir)
2419            .output()
2420            .await;
2421        let _ = Command::new("git")
2422            .args(["config", "user.name", "Test"])
2423            .current_dir(&dir)
2424            .output()
2425            .await;
2426        // Create initial file and commit
2427        fs::write(dir.join("init.txt"), "init").unwrap();
2428        let _ = Command::new("git")
2429            .args(["add", "init.txt"])
2430            .current_dir(&dir)
2431            .output()
2432            .await;
2433        let _ = Command::new("git")
2434            .args(["-c", "commit.gpgsign=false", "commit", "-m", "init"])
2435            .current_dir(&dir)
2436            .output()
2437            .await;
2438
2439        // Create a new file (unstaged)
2440        fs::write(dir.join("new_file.txt"), "content").unwrap();
2441
2442        // Verify has_changes sees it
2443        assert!(has_changes(&dir).await);
2444
2445        // auto_commit should return false (no git user.email configured for this
2446        // test repo would cause commit to fail if we hadn't set it, but we did —
2447        // so this test verifies the happy path too).
2448        // For this test, the commit should succeed since we set user.email/name.
2449        let result = auto_commit(
2450            &dir,
2451            "1",
2452            "Test title",
2453            "test",
2454            1,
2455            super::super::commit::When::Always,
2456        )
2457        .await;
2458        assert!(result.unwrap());
2459
2460        // Verify no changes remain
2461        assert!(!has_changes(&dir).await);
2462
2463        // Cleanup
2464        let _ = fs::remove_dir_all(&dir);
2465    }
2466
2467    #[tokio::test]
2468    async fn auto_commit_ignores_repo_signing_config() {
2469        // Repo demands signing via a signer that always fails — auto_commit
2470        // must still commit (it passes -c commit.gpgsign=false)
2471        let dir = env::temp_dir().join(format!("orch_test_auto_commit_gpg_{}", process::id()));
2472        let _ = fs::remove_dir_all(&dir);
2473        fs::create_dir_all(&dir).unwrap();
2474
2475        for args in [
2476            vec!["init"],
2477            vec!["config", "user.email", "test@test.com"],
2478            vec!["config", "user.name", "Test"],
2479            vec!["config", "commit.gpgsign", "true"],
2480            vec!["config", "gpg.program", "/usr/bin/false"],
2481        ] {
2482            let _ = Command::new("git")
2483                .args(&args)
2484                .current_dir(&dir)
2485                .output()
2486                .await;
2487        }
2488        fs::write(dir.join("init.txt"), "init").unwrap();
2489        let _ = Command::new("git")
2490            .args(["add", "init.txt"])
2491            .current_dir(&dir)
2492            .output()
2493            .await;
2494        let _ = Command::new("git")
2495            .args(["-c", "commit.gpgsign=false", "commit", "-m", "init"])
2496            .current_dir(&dir)
2497            .output()
2498            .await;
2499
2500        fs::write(dir.join("signed_off.txt"), "content").unwrap();
2501        assert!(has_changes(&dir).await);
2502
2503        let result = auto_commit(
2504            &dir,
2505            "3",
2506            "Test title",
2507            "test",
2508            1,
2509            super::super::commit::When::Always,
2510        )
2511        .await;
2512        assert!(result.unwrap());
2513        assert!(!has_changes(&dir).await);
2514
2515        let _ = fs::remove_dir_all(&dir);
2516    }
2517
2518    #[tokio::test]
2519    async fn auto_commit_unstages_on_pre_commit_hook_failure() {
2520        // Create a temp git repo where commit always fails (via pre-commit hook)
2521        let dir = env::temp_dir().join(format!("orch_test_auto_commit_hook_{}", process::id()));
2522        let _ = fs::remove_dir_all(&dir);
2523        fs::create_dir_all(&dir).unwrap();
2524
2525        // Init repo
2526        let _ = Command::new("git")
2527            .args(["init"])
2528            .current_dir(&dir)
2529            .output()
2530            .await;
2531        let _ = Command::new("git")
2532            .args(["config", "user.email", "test@test.com"])
2533            .current_dir(&dir)
2534            .output()
2535            .await;
2536        let _ = Command::new("git")
2537            .args(["config", "user.name", "Test"])
2538            .current_dir(&dir)
2539            .output()
2540            .await;
2541        // Create initial commit
2542        fs::write(dir.join("init.txt"), "init").unwrap();
2543        let _ = Command::new("git")
2544            .args(["add", "init.txt"])
2545            .current_dir(&dir)
2546            .output()
2547            .await;
2548        let _ = Command::new("git")
2549            .args(["-c", "commit.gpgsign=false", "commit", "-m", "init"])
2550            .current_dir(&dir)
2551            .output()
2552            .await;
2553
2554        // Install a pre-commit hook that always fails
2555        let hooks_dir = dir.join(".git/hooks");
2556        fs::create_dir_all(&hooks_dir).unwrap();
2557        fs::write(hooks_dir.join("pre-commit"), "#!/bin/sh\nexit 1\n").unwrap();
2558        #[cfg(unix)]
2559        {
2560            let mut perms = fs::metadata(hooks_dir.join("pre-commit"))
2561                .unwrap()
2562                .permissions();
2563            perms.set_mode(0o755);
2564            fs::set_permissions(hooks_dir.join("pre-commit"), perms).unwrap();
2565        }
2566
2567        // Create a new file
2568        fs::write(dir.join("staged_file.txt"), "content").unwrap();
2569
2570        // Verify has_changes sees it
2571        assert!(has_changes(&dir).await);
2572
2573        // auto_commit should return false (pre-commit hook blocks it)
2574        let result = auto_commit(
2575            &dir,
2576            "2",
2577            "Test title",
2578            "test",
2579            1,
2580            super::super::commit::When::Always,
2581        )
2582        .await;
2583        assert!(!result.unwrap());
2584
2585        // After commit failure, files should be unstaged — has_changes should
2586        // still return true because the unstaged file is still there.
2587        assert!(has_changes(&dir).await);
2588
2589        // Verify the file is NOT staged (git diff --cached --quiet should succeed)
2590        let cached = Command::new("git")
2591            .args(["diff", "--cached", "--quiet"])
2592            .current_dir(&dir)
2593            .status()
2594            .await;
2595        assert!(
2596            cached.is_ok_and(|s| s.success()),
2597            "files should be unstaged after commit failure"
2598        );
2599
2600        // Cleanup
2601        let _ = fs::remove_dir_all(&dir);
2602    }
2603
2604    #[test]
2605    fn is_workflow_scope_error_detects_backtick_format() {
2606        let err = "! [remote rejected] branch -> branch (refusing to allow an OAuth App to create or update workflow `.github/workflows/ci.yml` without `workflow` scope)";
2607        assert!(is_workflow_scope_error(err));
2608    }
2609
2610    #[test]
2611    fn is_workflow_scope_error_detects_single_quote_format() {
2612        let err = "refusing to allow an OAuth App to create or update workflow '.github/workflows/ci.yml' without 'workflow' scope";
2613        assert!(is_workflow_scope_error(err));
2614    }
2615
2616    #[test]
2617    fn is_workflow_scope_error_detects_refusing_to_allow_variant() {
2618        let err = "remote: refusing to allow a Personal Access Token to create or update workflow `.github/workflows/scan.yml` without `workflow` scope";
2619        assert!(is_workflow_scope_error(err));
2620    }
2621
2622    #[test]
2623    fn is_workflow_scope_error_rejects_unrelated_errors() {
2624        assert!(!is_workflow_scope_error("non-fast-forward"));
2625        assert!(!is_workflow_scope_error("permission denied"));
2626        assert!(!is_workflow_scope_error("authentication failed"));
2627        assert!(!is_workflow_scope_error("rejected"));
2628    }
2629
2630    #[tokio::test]
2631    async fn strip_workflow_files_removes_workflow_from_commits() {
2632        // Create a temp git repo simulating the scenario
2633        let dir = env::temp_dir().join(format!("orch_test_strip_wf_{}", process::id()));
2634        let _ = fs::remove_dir_all(&dir);
2635        fs::create_dir_all(&dir).unwrap();
2636
2637        // Init repo with initial commit
2638        let _ = Command::new("git")
2639            .args(["init"])
2640            .current_dir(&dir)
2641            .output()
2642            .await;
2643        let _ = Command::new("git")
2644            .args(["config", "user.email", "test@test.com"])
2645            .current_dir(&dir)
2646            .output()
2647            .await;
2648        let _ = Command::new("git")
2649            .args(["config", "user.name", "Test"])
2650            .current_dir(&dir)
2651            .output()
2652            .await;
2653        fs::write(dir.join("init.txt"), "init").unwrap();
2654        let _ = Command::new("git")
2655            .args(["add", "."])
2656            .current_dir(&dir)
2657            .output()
2658            .await;
2659        let _ = Command::new("git")
2660            .args(["-c", "commit.gpgsign=false", "commit", "-m", "init"])
2661            .current_dir(&dir)
2662            .output()
2663            .await;
2664
2665        // Create a "remote" by adding the repo as its own origin (for rev-list)
2666        let _ = Command::new("git")
2667            .args(["remote", "add", "origin", dir.to_str().unwrap()])
2668            .current_dir(&dir)
2669            .output()
2670            .await;
2671        let _ = Command::new("git")
2672            .args(["fetch", "origin"])
2673            .current_dir(&dir)
2674            .output()
2675            .await;
2676
2677        // Create a branch off main
2678        let _ = Command::new("git")
2679            .args(["checkout", "-b", "feature"])
2680            .current_dir(&dir)
2681            .output()
2682            .await;
2683
2684        // Add both a normal file and a workflow file
2685        fs::write(dir.join("feature.txt"), "feature work").unwrap();
2686        let wf_dir = dir.join(".github/workflows");
2687        fs::create_dir_all(&wf_dir).unwrap();
2688        fs::write(wf_dir.join("ci.yml"), "name: CI\non: push").unwrap();
2689        let _ = Command::new("git")
2690            .args(["add", "."])
2691            .current_dir(&dir)
2692            .output()
2693            .await;
2694        let _ = Command::new("git")
2695            .args([
2696                "-c",
2697                "commit.gpgsign=false",
2698                "commit",
2699                "-m",
2700                "add feature and workflow",
2701            ])
2702            .current_dir(&dir)
2703            .output()
2704            .await;
2705
2706        // Get the default branch name
2707        let branch_out = Command::new("git")
2708            .args(["rev-parse", "--abbrev-ref", "origin/HEAD"])
2709            .current_dir(&dir)
2710            .output()
2711            .await;
2712        // Fallback to "main" if origin/HEAD is not set
2713        let default_branch = branch_out.ok().filter(|o| o.status.success()).map_or_else(
2714            || "main".to_string(),
2715            |o| {
2716                String::from_utf8_lossy(&o.stdout)
2717                    .trim()
2718                    .strip_prefix("origin/")
2719                    .unwrap_or("main")
2720                    .to_string()
2721            },
2722        );
2723
2724        // Strip workflow files
2725        let stripped = strip_workflow_files(&dir, &default_branch).await.unwrap();
2726        assert!(stripped, "should have stripped workflow files");
2727
2728        // Verify workflow file is no longer in the committed tree
2729        let ls_output = Command::new("git")
2730            .args(["ls-tree", "-r", "--name-only", "HEAD"])
2731            .current_dir(&dir)
2732            .output()
2733            .await
2734            .unwrap();
2735        let files = String::from_utf8_lossy(&ls_output.stdout);
2736        assert!(
2737            !files.contains(".github/workflows/ci.yml"),
2738            "workflow file should be removed from commits"
2739        );
2740        assert!(
2741            files.contains("feature.txt"),
2742            "non-workflow files should be preserved"
2743        );
2744
2745        // Cleanup
2746        let _ = fs::remove_dir_all(&dir);
2747    }
2748
2749    #[tokio::test]
2750    async fn strip_workflow_files_returns_false_when_no_workflows() {
2751        let dir = env::temp_dir().join(format!("orch_test_strip_wf_none_{}", process::id()));
2752        let _ = fs::remove_dir_all(&dir);
2753        fs::create_dir_all(&dir).unwrap();
2754
2755        // Init repo
2756        let _ = Command::new("git")
2757            .args(["init"])
2758            .current_dir(&dir)
2759            .output()
2760            .await;
2761        let _ = Command::new("git")
2762            .args(["config", "user.email", "test@test.com"])
2763            .current_dir(&dir)
2764            .output()
2765            .await;
2766        let _ = Command::new("git")
2767            .args(["config", "user.name", "Test"])
2768            .current_dir(&dir)
2769            .output()
2770            .await;
2771        fs::write(dir.join("init.txt"), "init").unwrap();
2772        let _ = Command::new("git")
2773            .args(["add", "."])
2774            .current_dir(&dir)
2775            .output()
2776            .await;
2777        let _ = Command::new("git")
2778            .args(["-c", "commit.gpgsign=false", "commit", "-m", "init"])
2779            .current_dir(&dir)
2780            .output()
2781            .await;
2782
2783        let _ = Command::new("git")
2784            .args(["remote", "add", "origin", dir.to_str().unwrap()])
2785            .current_dir(&dir)
2786            .output()
2787            .await;
2788        let _ = Command::new("git")
2789            .args(["fetch", "origin"])
2790            .current_dir(&dir)
2791            .output()
2792            .await;
2793
2794        let _ = Command::new("git")
2795            .args(["checkout", "-b", "feature2"])
2796            .current_dir(&dir)
2797            .output()
2798            .await;
2799
2800        // Only non-workflow files
2801        fs::write(dir.join("feature.txt"), "feature").unwrap();
2802        let _ = Command::new("git")
2803            .args(["add", "."])
2804            .current_dir(&dir)
2805            .output()
2806            .await;
2807        let _ = Command::new("git")
2808            .args([
2809                "-c",
2810                "commit.gpgsign=false",
2811                "commit",
2812                "-m",
2813                "add feature only",
2814            ])
2815            .current_dir(&dir)
2816            .output()
2817            .await;
2818
2819        let stripped = strip_workflow_files(&dir, "main").await.unwrap();
2820        assert!(!stripped, "should not strip when no workflow files exist");
2821
2822        // Cleanup
2823        let _ = fs::remove_dir_all(&dir);
2824    }
2825
2826    #[test]
2827    fn is_invalid_base_error_detects_base_invalid() {
2828        assert!(is_invalid_base_error(
2829            "GitHub API POST https://api.github.com/repos/foo/bar/pulls failed (422): {\"errors\":[{\"resource\":\"PullRequest\",\"field\":\"base\",\"code\":\"invalid\"}]}"
2830        ));
2831        assert!(is_invalid_base_error(
2832            "GitHub API POST .../pulls failed (422): Base field is invalid"
2833        ));
2834    }
2835
2836    #[test]
2837    fn is_invalid_base_error_rejects_non_base_errors() {
2838        // No-commits error should NOT match
2839        assert!(!is_invalid_base_error(
2840            "GitHub API POST .../pulls failed (422): No commits between main and branch"
2841        ));
2842        // Head invalid should NOT match
2843        assert!(!is_invalid_base_error(
2844            "GitHub API POST .../pulls failed (422): head is not a valid ref"
2845        ));
2846        // Transient 5xx should NOT match
2847        assert!(!is_invalid_base_error(
2848            "GitHub API POST .../pulls failed (502): Bad Gateway"
2849        ));
2850        // 404 should NOT match
2851        assert!(!is_invalid_base_error(
2852            "GitHub API POST .../pulls failed (404): Not Found"
2853        ));
2854    }
2855
2856    fn pr_summary(body: &str) -> &str {
2857        let rest = body
2858            .strip_prefix("## Summary\n\n")
2859            .expect("PR body starts with ## Summary");
2860        rest.split("\n\n").next().unwrap()
2861    }
2862
2863    fn assert_no_push_or_pr_creation(body: &str) {
2864        let lower = body.to_ascii_lowercase();
2865        for needle in [
2866            "git push",
2867            "gh pr create",
2868            "did not push",
2869            "couldn't push",
2870            "could not push",
2871            "push was denied",
2872            "push was blocked",
2873            "push permission",
2874            "nothing is on the remote",
2875            "open the pr",
2876        ] {
2877            assert!(
2878                !lower.contains(needle),
2879                "PR body still contains {needle:?}:\n{body}"
2880            );
2881        }
2882    }
2883
2884    #[test]
2885    fn build_pr_body_strips_push_and_pr_creation_statements() {
2886        let examples = [
2887            (
2888                concat!(
2889                    "Added the lesson list page. ",
2890                    "I did not push or open the PR, because the push was blocked on a permission prompt. ",
2891                    "Run `git push -u origin HEAD`, then `gh pr create`.",
2892                ),
2893                "Added the lesson list page.",
2894            ),
2895            (
2896                "It is committed locally ... The push was denied, so nothing is on the remote yet.",
2897                "It is committed locally",
2898            ),
2899            (
2900                concat!(
2901                    "Fixed the quiz scoring. ",
2902                    "I couldn't push it because the push permission was denied.",
2903                ),
2904                "Fixed the quiz scoring.",
2905            ),
2906        ];
2907        for (summary, work) in examples {
2908            let body = build_pr_body(
2909                "Fix the widget",
2910                summary,
2911                &[
2912                    "Wrote the lesson list".into(),
2913                    "Could not push because the push permission was denied".into(),
2914                ],
2915                &["Run `git push -u origin HEAD`".into()],
2916                &[],
2917                "16",
2918                "claude",
2919                None,
2920            );
2921            assert_eq!(pr_summary(&body), work);
2922            assert!(body.contains("Wrote the lesson list"));
2923            assert!(!body.contains("### Remaining"));
2924            assert_no_push_or_pr_creation(&body);
2925        }
2926    }
2927
2928    #[test]
2929    fn build_pr_body_keeps_summary_without_push_text() {
2930        let summary = "Added retry logic for GitHub 5xx errors.";
2931        let accomplished = vec!["Extracted the PR body builder".into()];
2932        let remaining = vec!["Watch CI after merge".into()];
2933        let body = build_pr_body(
2934            "Fix the widget",
2935            summary,
2936            &accomplished,
2937            &remaining,
2938            &["src/engine/runner/git_ops.rs".into()],
2939            "16",
2940            "claude",
2941            Some("sonnet"),
2942        );
2943        assert_eq!(pr_summary(&body), summary);
2944        assert!(body.contains("### What was done\n\n- Extracted the PR body builder\n"));
2945        assert!(body.contains("### Remaining\n\n- Watch CI after merge\n"));
2946        assert!(body.contains("- `src/engine/runner/git_ops.rs`"));
2947        assert!(body.contains(" using `sonnet`"));
2948    }
2949
2950    fn sh_git(dir: &Path, args: &[&str]) {
2951        let out = process::Command::new("git")
2952            .args([
2953                "-c",
2954                "commit.gpgsign=false",
2955                "-c",
2956                "user.name=T",
2957                "-c",
2958                "user.email=t@t",
2959            ])
2960            .args(args)
2961            .current_dir(dir)
2962            .output()
2963            .unwrap();
2964        assert!(
2965            out.status.success(),
2966            "git {args:?}: {}",
2967            String::from_utf8_lossy(&out.stderr)
2968        );
2969    }
2970
2971    fn commit_file(dir: &Path, name: &str) {
2972        fs::write(dir.join(name), name).unwrap();
2973        sh_git(dir, &["add", name]);
2974        sh_git(dir, &["commit", "-m", name]);
2975    }
2976
2977    /**
2978     * The review rebases its branch on main and force-pushes with lease. When GitHub's
2979     * "Update branch" moved the remote branch after the last fetch, the lease is stale and
2980     * the push was rejected, which blocked approved work (#3783)
2981     **/
2982    #[tokio::test]
2983    async fn push_branch_recovers_from_stale_lease_after_remote_moved() {
2984        let temp = tempfile::TempDir::new().unwrap();
2985        let bare = temp.path().join("origin.git");
2986        let a = temp.path().join("a");
2987        let b = temp.path().join("b");
2988        fs::create_dir_all(&bare).unwrap();
2989        sh_git(&bare, &["init", "--bare", "-b", "main"]);
2990        sh_git(temp.path(), &["clone", bare.to_str().unwrap(), "a"]);
2991        // push_branch's own rebase commits, and CI runners have no global identity
2992        sh_git(&a, &["config", "user.name", "T"]);
2993        sh_git(&a, &["config", "user.email", "t@t"]);
2994        commit_file(&a, "base.txt");
2995        sh_git(&a, &["push", "origin", "main"]);
2996        sh_git(&a, &["checkout", "-b", "feat"]);
2997        commit_file(&a, "feature.txt");
2998        sh_git(&a, &["push", "-u", "origin", "feat"]);
2999
3000        sh_git(temp.path(), &["clone", bare.to_str().unwrap(), "b"]);
3001        commit_file(&b, "main-change.txt");
3002        sh_git(&b, &["push", "origin", "main"]);
3003        sh_git(&b, &["checkout", "feat"]);
3004        commit_file(&b, "update-branch.txt");
3005        sh_git(&b, &["push", "origin", "feat"]);
3006
3007        // A only fetches main, so its view of origin/feat is stale when it rewrites feat
3008        sh_git(&a, &["fetch", "origin", "main"]);
3009        sh_git(&a, &["rebase", "origin/main"]);
3010
3011        assert!(push_branch(&a, "feat", "main").await.unwrap());
3012
3013        sh_git(&b, &["fetch", "origin", "feat"]);
3014        sh_git(&b, &["checkout", "origin/feat"]);
3015        for name in [
3016            "base.txt",
3017            "feature.txt",
3018            "main-change.txt",
3019            "update-branch.txt",
3020        ] {
3021            assert!(b.join(name).exists(), "remote feat lost {name}");
3022        }
3023    }
3024
3025    #[test]
3026    fn stale_lease_error_matches_git_messages() {
3027        assert!(is_stale_lease_error(
3028            "! [rejected] feat -> feat (stale info)"
3029        ));
3030        assert!(is_stale_lease_error(
3031            "! [rejected] feat -> feat (remote ref updated since checkout)"
3032        ));
3033        assert!(!is_stale_lease_error(
3034            "! [rejected] feat -> feat (fetch first)"
3035        ));
3036    }
3037
3038    fn git_ok(dir: &Path, args: &[&str]) {
3039        let out = process::Command::new("git")
3040            .args(args)
3041            .current_dir(dir)
3042            .output()
3043            .unwrap();
3044        assert!(
3045            out.status.success(),
3046            "git {} failed: {}",
3047            args.join(" "),
3048            String::from_utf8_lossy(&out.stderr)
3049        );
3050    }
3051
3052    fn git_sha(dir: &Path, rev: &str) -> String {
3053        let out = process::Command::new("git")
3054            .args(["rev-parse", rev])
3055            .current_dir(dir)
3056            .output()
3057            .unwrap();
3058        assert!(out.status.success(), "git rev-parse {rev} failed");
3059        String::from_utf8_lossy(&out.stdout).trim().to_string()
3060    }
3061
3062    fn init_identity(dir: &Path) {
3063        git_ok(dir, &["config", "user.email", "test@example.com"]);
3064        git_ok(dir, &["config", "user.name", "Test"]);
3065        git_ok(dir, &["config", "commit.gpgsign", "false"]);
3066    }
3067
3068    fn write_commit(dir: &Path, file: &str, body: &str, msg: &str) {
3069        fs::write(dir.join(file), body).unwrap();
3070        git_ok(dir, &["add", file]);
3071        git_ok(dir, &["commit", "-m", msg]);
3072    }
3073
3074    /// A bare origin with `main` and a pushed `feature`, and a clone `a` on `feature`
3075    fn publish_feature(root: &Path) -> (PathBuf, PathBuf) {
3076        let bare = root.join("bare.git");
3077        let a = root.join("a");
3078        git_ok(root, &["init", "-b", "main", a.to_str().unwrap()]);
3079        init_identity(&a);
3080        write_commit(&a, "README.md", "base\n", "base");
3081        git_ok(
3082            root,
3083            &["init", "--bare", "-b", "main", bare.to_str().unwrap()],
3084        );
3085        git_ok(&a, &["remote", "add", "origin", bare.to_str().unwrap()]);
3086        git_ok(&a, &["push", "-u", "origin", "main"]);
3087        git_ok(&a, &["checkout", "-b", "feature"]);
3088        write_commit(&a, "feature.txt", "feature\n", "feature");
3089        git_ok(&a, &["push", "-u", "origin", "feature"]);
3090        (bare, a)
3091    }
3092
3093    fn clone_on(root: &Path, bare: &Path, name: &str, branch: &str) -> PathBuf {
3094        let dir = root.join(name);
3095        git_ok(
3096            root,
3097            &["clone", bare.to_str().unwrap(), dir.to_str().unwrap()],
3098        );
3099        init_identity(&dir);
3100        if branch != "main" {
3101            git_ok(
3102                &dir,
3103                &["checkout", "-b", branch, &format!("origin/{branch}")],
3104            );
3105        }
3106
3107        dir
3108    }
3109
3110    #[tokio::test]
3111    async fn leased_force_push_does_not_replace_a_newer_remote_tip() {
3112        let root = tempfile::tempdir().unwrap();
3113        let (bare, a) = publish_feature(root.path());
3114        let b = clone_on(root.path(), &bare, "b", "feature");
3115        record_push_lease(&b, "feature", "main").await;
3116        let leased = git_sha(&b, "refs/remotes/origin/feature");
3117
3118        write_commit(&a, "feature.txt", "from a\n", "from a");
3119        git_ok(&a, &["push", "origin", "feature"]);
3120        let newer = git_sha(&a, "HEAD");
3121
3122        git_ok(&b, &["fetch", "origin"]);
3123        write_commit(&b, "feature.txt", "from b\n", "from b");
3124        let pushed = push_branch(&b, "feature", "main").await;
3125        assert!(
3126            pushed.is_err(),
3127            "force push should be rejected, got {pushed:?}"
3128        );
3129        assert_eq!(git_sha(&bare, "refs/heads/feature"), newer);
3130
3131        // A retry sees the fetched tip, which HEAD does not contain, so the lease stays
3132        record_push_lease(&b, "feature", "main").await;
3133        assert_eq!(read_push_lease(&b, "feature").await, Some(leased));
3134        let again = push_branch(&b, "feature", "main").await;
3135        assert!(
3136            again.is_err(),
3137            "second force push should stay rejected, got {again:?}"
3138        );
3139        assert_eq!(git_sha(&bare, "refs/heads/feature"), newer);
3140    }
3141
3142    #[tokio::test]
3143    async fn leased_force_push_allows_rewrite_of_the_recorded_tip() {
3144        let root = tempfile::tempdir().unwrap();
3145        let (bare, a) = publish_feature(root.path());
3146        record_push_lease(&a, "feature", "main").await;
3147        fs::write(a.join("feature.txt"), "rewritten\n").unwrap();
3148        git_ok(&a, &["commit", "-am", "--amend", "-m", "feature rewritten"]);
3149
3150        let pushed = push_branch(&a, "feature", "main").await;
3151        assert!(
3152            pushed.is_ok(),
3153            "rewrite of the leased tip should push, got {pushed:?}"
3154        );
3155        assert_eq!(git_sha(&bare, "refs/heads/feature"), git_sha(&a, "HEAD"));
3156        assert_eq!(
3157            read_push_lease(&a, "feature").await,
3158            Some(git_sha(&a, "HEAD"))
3159        );
3160    }
3161
3162    #[tokio::test]
3163    async fn push_after_update_branch_merge_force_pushes_the_rebased_branch() {
3164        let root = tempfile::tempdir().unwrap();
3165        let (bare, a) = publish_feature(root.path());
3166        record_push_lease(&a, "feature", "main").await;
3167
3168        // GitHub's "Update branch": main moves, then main is merged into feature on the remote
3169        let c = clone_on(root.path(), &bare, "c", "main");
3170        write_commit(&c, "main.txt", "main\n", "main moves");
3171        git_ok(&c, &["push", "origin", "main"]);
3172        git_ok(&c, &["checkout", "-b", "feature", "origin/feature"]);
3173        git_ok(
3174            &c,
3175            &[
3176                "merge",
3177                "--no-ff",
3178                "-m",
3179                "Merge main into feature",
3180                "origin/main",
3181            ],
3182        );
3183        git_ok(&c, &["push", "origin", "feature"]);
3184
3185        rebase_on_default(&a, "main").await;
3186        write_commit(&a, "feature.txt", "review fix\n", "review fix");
3187        let pushed = push_branch(&a, "feature", "main").await;
3188        assert!(
3189            pushed.is_ok(),
3190            "push after update-branch should succeed, got {pushed:?}"
3191        );
3192        assert_eq!(git_sha(&bare, "refs/heads/feature"), git_sha(&a, "HEAD"));
3193    }
3194
3195    #[tokio::test]
3196    async fn agent_push_moves_the_lease_to_a_tip_head_contains() {
3197        let root = tempfile::tempdir().unwrap();
3198        let (_bare, a) = publish_feature(root.path());
3199        record_push_lease(&a, "feature", "main").await;
3200
3201        write_commit(&a, "feature.txt", "agent step\n", "agent step");
3202        git_ok(&a, &["push", "origin", "feature"]);
3203        record_push_lease(&a, "feature", "main").await;
3204        assert_eq!(
3205            read_push_lease(&a, "feature").await,
3206            Some(git_sha(&a, "HEAD"))
3207        );
3208    }
3209
3210    #[tokio::test]
3211    async fn record_push_lease_skips_the_default_branch() {
3212        let root = tempfile::tempdir().unwrap();
3213        let (_bare, a) = publish_feature(root.path());
3214        git_ok(&a, &["checkout", "main"]);
3215        record_push_lease(&a, "main", "main").await;
3216        assert_eq!(read_push_lease(&a, "main").await, None);
3217    }
3218
3219    /// GitHub's "Update branch" by merge: main moves, then main is merged into feature
3220    fn github_update_by_merge(root: &Path, bare: &Path, name: &str) {
3221        let c = clone_on(root, bare, name, "main");
3222        write_commit(&c, "main.txt", "main\n", "main moves");
3223        git_ok(&c, &["push", "origin", "main"]);
3224        git_ok(&c, &["checkout", "-b", "feature", "origin/feature"]);
3225        git_ok(
3226            &c,
3227            &[
3228                "merge",
3229                "--no-ff",
3230                "-m",
3231                "Merge main into feature",
3232                "origin/main",
3233            ],
3234        );
3235        git_ok(&c, &["push", "origin", "feature"]);
3236    }
3237
3238    /// GitHub's "Update branch" by rebase: main moves, then feature is replayed on it
3239    fn github_update_by_rebase(root: &Path, bare: &Path, name: &str) -> PathBuf {
3240        let c = clone_on(root, bare, name, "main");
3241        write_commit(&c, "main.txt", "main\n", "main moves");
3242        git_ok(&c, &["push", "origin", "main"]);
3243        git_ok(&c, &["checkout", "-b", "feature", "origin/feature"]);
3244        git_ok(&c, &["rebase", "origin/main"]);
3245        git_ok(&c, &["push", "--force", "origin", "feature"]);
3246        c
3247    }
3248
3249    fn remote_has(root: &Path, bare: &Path, file: &str) -> bool {
3250        let check = root.join("check");
3251        if !check.exists() {
3252            git_ok(
3253                root,
3254                &["clone", bare.to_str().unwrap(), check.to_str().unwrap()],
3255            );
3256        }
3257
3258        git_ok(&check, &["fetch", "origin"]);
3259        git_ok(&check, &["checkout", "--detach", "origin/feature"]);
3260        check.join(file).exists()
3261    }
3262
3263    #[tokio::test]
3264    async fn push_after_update_branch_rebase_force_pushes_the_rebased_branch() {
3265        let root = tempfile::tempdir().unwrap();
3266        let (bare, a) = publish_feature(root.path());
3267        record_push_lease(&a, "feature", "main").await;
3268        github_update_by_rebase(root.path(), &bare, "c");
3269
3270        rebase_on_default(&a, "main").await;
3271        write_commit(&a, "fix.txt", "review fix\n", "review fix");
3272        let pushed = push_branch(&a, "feature", "main").await;
3273        assert!(
3274            pushed.is_ok(),
3275            "push after a rebase update should succeed, got {pushed:?}"
3276        );
3277        assert_eq!(git_sha(&bare, "refs/heads/feature"), git_sha(&a, "HEAD"));
3278    }
3279
3280    #[tokio::test]
3281    async fn rebase_update_with_a_foreign_commit_keeps_the_foreign_commit() {
3282        let root = tempfile::tempdir().unwrap();
3283        let (bare, a) = publish_feature(root.path());
3284        record_push_lease(&a, "feature", "main").await;
3285        let c = github_update_by_rebase(root.path(), &bare, "c");
3286        write_commit(&c, "other.txt", "other machine\n", "other machine");
3287        git_ok(&c, &["push", "origin", "feature"]);
3288
3289        rebase_on_default(&a, "main").await;
3290        write_commit(&a, "fix.txt", "review fix\n", "review fix");
3291        let _ = push_branch(&a, "feature", "main").await;
3292        assert!(
3293            remote_has(root.path(), &bare, "other.txt"),
3294            "the other machine's commit was dropped"
3295        );
3296    }
3297
3298    #[tokio::test]
3299    async fn merge_of_a_non_default_branch_is_not_a_github_update() {
3300        let root = tempfile::tempdir().unwrap();
3301        let (bare, a) = publish_feature(root.path());
3302        record_push_lease(&a, "feature", "main").await;
3303        let c = clone_on(root.path(), &bare, "c", "feature");
3304        git_ok(&c, &["checkout", "-b", "side", "origin/main"]);
3305        write_commit(&c, "side.txt", "side\n", "side work");
3306        git_ok(&c, &["checkout", "feature"]);
3307        git_ok(&c, &["merge", "--no-ff", "-m", "Merge side", "side"]);
3308        git_ok(&c, &["push", "origin", "feature"]);
3309
3310        rebase_on_default(&a, "main").await;
3311        write_commit(&a, "fix.txt", "review fix\n", "review fix");
3312        let _ = push_branch(&a, "feature", "main").await;
3313        assert!(
3314            remote_has(root.path(), &bare, "side.txt"),
3315            "the merged side branch was dropped"
3316        );
3317    }
3318
3319    #[tokio::test]
3320    async fn update_merge_on_top_of_a_foreign_push_keeps_the_foreign_commit() {
3321        let root = tempfile::tempdir().unwrap();
3322        let (bare, a) = publish_feature(root.path());
3323        record_push_lease(&a, "feature", "main").await;
3324        let c = clone_on(root.path(), &bare, "c", "feature");
3325        write_commit(&c, "other.txt", "other machine\n", "other machine");
3326        git_ok(&c, &["push", "origin", "feature"]);
3327        github_update_by_merge(root.path(), &bare, "d");
3328
3329        rebase_on_default(&a, "main").await;
3330        write_commit(&a, "fix.txt", "review fix\n", "review fix");
3331        let _ = push_branch(&a, "feature", "main").await;
3332        assert!(
3333            remote_has(root.path(), &bare, "other.txt"),
3334            "the other machine's commit was dropped"
3335        );
3336    }
3337
3338    #[tokio::test]
3339    async fn force_push_leased_accepts_github_updates() {
3340        for by_rebase in [false, true] {
3341            let root = tempfile::tempdir().unwrap();
3342            let (bare, a) = publish_feature(root.path());
3343            record_push_lease(&a, "feature", "main").await;
3344            if by_rebase {
3345                github_update_by_rebase(root.path(), &bare, "c");
3346            } else {
3347                github_update_by_merge(root.path(), &bare, "c");
3348            }
3349
3350            git_ok(&a, &["fetch", "origin"]);
3351            git_ok(&a, &["rebase", "origin/main"]);
3352            let pushed = force_push_leased(&a, "feature", "main").await;
3353            assert!(pushed.is_ok(), "by_rebase={by_rebase}: {pushed:?}");
3354            assert_eq!(git_sha(&bare, "refs/heads/feature"), git_sha(&a, "HEAD"));
3355        }
3356    }
3357
3358    #[tokio::test]
3359    async fn force_push_leased_accepts_a_branch_recreated_at_the_default_head() {
3360        let root = tempfile::tempdir().unwrap();
3361        let (bare, a) = publish_feature(root.path());
3362        record_push_lease(&a, "feature", "main").await;
3363        let c = clone_on(root.path(), &bare, "c", "main");
3364        write_commit(&c, "feature.txt", "main version\n", "main moves");
3365        git_ok(&c, &["push", "origin", "main"]);
3366        git_ok(
3367            &c,
3368            &["push", "origin", "main:refs/heads/feature", "--force"],
3369        );
3370
3371        git_ok(&a, &["fetch", "origin"]);
3372        record_push_lease(&a, "feature", "main").await;
3373        fs::write(a.join("feature.txt"), "feature rewritten\n").unwrap();
3374        git_ok(&a, &["commit", "-am", "rewrite"]);
3375        let pushed = force_push_leased(&a, "feature", "main").await;
3376        assert!(pushed.is_ok(), "{pushed:?}");
3377        assert_eq!(git_sha(&bare, "refs/heads/feature"), git_sha(&a, "HEAD"));
3378    }
3379
3380    #[tokio::test]
3381    async fn clear_push_lease_drops_the_recorded_lease() {
3382        let root = tempfile::tempdir().unwrap();
3383        let (_bare, a) = publish_feature(root.path());
3384        record_push_lease(&a, "feature", "main").await;
3385        assert!(read_push_lease(&a, "feature").await.is_some());
3386        clear_push_lease(&a, "feature").await;
3387        assert_eq!(read_push_lease(&a, "feature").await, None);
3388    }
3389
3390    #[tokio::test]
3391    async fn force_push_leased_refuses_a_foreign_push() {
3392        let root = tempfile::tempdir().unwrap();
3393        let (bare, a) = publish_feature(root.path());
3394        record_push_lease(&a, "feature", "main").await;
3395        let c = clone_on(root.path(), &bare, "c", "feature");
3396        write_commit(&c, "other.txt", "other machine\n", "other machine");
3397        git_ok(&c, &["push", "origin", "feature"]);
3398        let foreign = git_sha(&c, "HEAD");
3399
3400        git_ok(&a, &["fetch", "origin"]);
3401        record_push_lease(&a, "feature", "main").await;
3402        fs::write(a.join("feature.txt"), "rewritten\n").unwrap();
3403        git_ok(&a, &["commit", "-am", "--amend", "-m", "rewritten"]);
3404        assert!(force_push_leased(&a, "feature", "main").await.is_err());
3405        assert_eq!(git_sha(&bare, "refs/heads/feature"), foreign);
3406    }
3407
3408    #[tokio::test]
3409    async fn force_push_leased_refuses_a_branch_created_after_an_absent_lease() {
3410        let root = tempfile::tempdir().unwrap();
3411        let (bare, a) = publish_feature(root.path());
3412        git_ok(&a, &["checkout", "-b", "fresh", "main"]);
3413        record_push_lease(&a, "fresh", "main").await;
3414        assert_eq!(
3415            read_push_lease(&a, "fresh").await.as_deref(),
3416            Some(LEASE_ABSENT)
3417        );
3418
3419        let c = clone_on(root.path(), &bare, "c", "main");
3420        git_ok(&c, &["checkout", "-b", "fresh"]);
3421        write_commit(&c, "other.txt", "other machine\n", "other machine");
3422        git_ok(&c, &["push", "origin", "fresh"]);
3423        let foreign = git_sha(&c, "HEAD");
3424
3425        git_ok(&a, &["fetch", "origin"]);
3426        write_commit(&a, "mine.txt", "mine\n", "mine");
3427        assert!(force_push_leased(&a, "fresh", "main").await.is_err());
3428        assert_eq!(git_sha(&bare, "refs/heads/fresh"), foreign);
3429    }
3430
3431    #[tokio::test]
3432    async fn first_push_records_the_pushed_head_as_the_lease() {
3433        let root = tempfile::tempdir().unwrap();
3434        let (_bare, a) = publish_feature(root.path());
3435        git_ok(&a, &["checkout", "-b", "fresh", "main"]);
3436        write_commit(&a, "mine.txt", "mine\n", "mine");
3437        assert!(push_branch(&a, "fresh", "main").await.unwrap());
3438        assert_eq!(
3439            read_push_lease(&a, "fresh").await,
3440            Some(git_sha(&a, "HEAD"))
3441        );
3442    }
3443
3444    #[tokio::test]
3445    async fn missing_lease_falls_back_to_the_fetched_tip_and_counts_it() {
3446        let root = tempfile::tempdir().unwrap();
3447        let (_bare, a) = publish_feature(root.path());
3448        let before = LEASE_FALLBACKS.load(Ordering::Relaxed);
3449        let expected = expected_force_sha(&a, "feature", "main").await.unwrap();
3450        assert_eq!(expected, git_sha(&a, "refs/remotes/origin/feature"));
3451        assert!(LEASE_FALLBACKS.load(Ordering::Relaxed) > before);
3452    }
3453
3454    #[tokio::test]
3455    async fn agent_push_then_foreign_push_keeps_the_agent_tip_as_lease() {
3456        let root = tempfile::tempdir().unwrap();
3457        let (bare, a) = publish_feature(root.path());
3458        record_push_lease(&a, "feature", "main").await;
3459        write_commit(&a, "step.txt", "agent step\n", "agent step");
3460        git_ok(&a, &["push", "origin", "feature"]);
3461        let agent_tip = git_sha(&a, "HEAD");
3462
3463        let c = clone_on(root.path(), &bare, "c", "feature");
3464        write_commit(&c, "other.txt", "other machine\n", "other machine");
3465        git_ok(&c, &["push", "origin", "feature"]);
3466
3467        // Callers record before they fetch, so the lease moves to the agent's push and no further
3468        record_push_lease(&a, "feature", "main").await;
3469        git_ok(&a, &["fetch", "origin"]);
3470        record_push_lease(&a, "feature", "main").await;
3471        assert_eq!(read_push_lease(&a, "feature").await, Some(agent_tip));
3472        let foreign = git_sha(&bare, "refs/heads/feature");
3473        git_ok(&a, &["commit", "--amend", "-m", "agent step rewritten"]);
3474        assert!(force_push_leased(&a, "feature", "main").await.is_err());
3475        assert_eq!(git_sha(&bare, "refs/heads/feature"), foreign);
3476    }
3477
3478    #[tokio::test]
3479    async fn branch_without_remote_and_head_without_twins_are_not_github_updates() {
3480        let root = tempfile::tempdir().unwrap();
3481        let (_bare, a) = publish_feature(root.path());
3482        let lease = git_sha(&a, "HEAD");
3483        let main = git_sha(&a, "origin/main");
3484        // A tip equal to main has nothing in base..tip, which must not count as a rebase update
3485        assert!(!is_github_branch_update(&a, &main, &lease, "main").await);
3486        assert!(
3487            !is_github_branch_update(
3488                &a,
3489                "0000000000000000000000000000000000000000",
3490                &lease,
3491                "main"
3492            )
3493            .await
3494        );
3495    }
3496}