Class: Ace::Git::Worktree::Molecules::CleanupPrResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/git/worktree/molecules/cleanup_pr_resolver.rb

Overview

Resolves PR evidence for candidate branches to prove safe squash-merge cleanup.

Instance Method Summary collapse

Constructor Details

#initialize(target:, target_sha:, offline: false) ⇒ CleanupPrResolver

Returns a new instance of CleanupPrResolver.



13
14
15
16
17
18
19
20
21
# File 'lib/ace/git/worktree/molecules/cleanup_pr_resolver.rb', line 13

def initialize(target:, target_sha:, offline: false)
  @target = target
  @target_sha = target_sha
  @offline = offline
  
  # Cache gh availability to avoid repeated checks
  @gh_available = Ace::Git::Molecules::PrMetadataFetcher.gh_installed? && 
                  Ace::Git::Molecules::PrMetadataFetcher.gh_authenticated?
end

Instance Method Details

#classify(branch, candidate_sha) ⇒ Hash

Classify a candidate branch with GitHub PR evidence.

Parameters:

  • branch (String)

    The branch name (e.g. "feature")

  • candidate_sha (String)

    The commit SHA of the candidate

Returns:

  • (Hash)

    Proof classification result



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ace/git/worktree/molecules/cleanup_pr_resolver.rb', line 28

def classify(branch, candidate_sha)
  return offline_result unless @gh_available && !@offline

  prs = fetch_merged_prs_for_branch(branch)
  return unproven_result if prs.nil? || prs.empty?

  # Find a PR that targets the configured base and is actually reachable
  valid_pr = prs.find do |pr|
    pr["baseRefName"] == @target && 
    pr["state"] == "MERGED" && 
    pr["mergeCommit"] && 
    pr["mergeCommit"]["oid"] &&
    ancestor?(pr["mergeCommit"]["oid"], @target_sha)
  end

  return ambiguous_result(prs) if prs.length > 1 && !valid_pr
  return unproven_result unless valid_pr

  merge_commit_sha = valid_pr["mergeCommit"]["oid"]
  pr_head_sha = valid_pr["headRefOid"]

  if candidate_sha == pr_head_sha
    {
      proof: "exact_merged_pr_head",
      pr: valid_pr["number"],
      candidate_head: candidate_sha,
      merged_head: pr_head_sha,
      merge_commit: merge_commit_sha,
      target_reachable: true,
      path_type_mode_match: true,
      provider_status: "available",
      action: "remove"
    }
  elsif patch_equivalent?(candidate_sha, merge_commit_sha)
    {
      proof: "stable_patch_equivalence",
      pr: valid_pr["number"],
      candidate_head: candidate_sha,
      merged_head: pr_head_sha,
      merge_commit: merge_commit_sha,
      target_reachable: true,
      path_type_mode_match: true,
      provider_status: "available",
      action: "remove"
    }
  else
    {
      proof: "none",
      pr: valid_pr["number"],
      candidate_head: candidate_sha,
      merged_head: pr_head_sha,
      merge_commit: merge_commit_sha,
      target_reachable: true,
      path_type_mode_match: false,
      provider_status: "available",
      action: "retain",
      retention_reason: "patch_mismatch"
    }
  end
rescue Ace::Git::GhNotInstalledError, Ace::Git::GhAuthenticationError, Ace::Git::TimeoutError
  # Provider failure degradation
  offline_result("unavailable")
end