Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MobileChat/GlobalChatScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ struct GlobalChatScreen: View {
else { Text(verbatim: provider.title) }
}
.disabled(!snapshot.provider.canSelect(provider, ownerChoice: snapshot.providerChoice))
.accessibilityAddTraits(snapshot.provider == provider ? .isSelected : [])
}
}

Expand Down
76 changes: 49 additions & 27 deletions Sources/GitMetadataProber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ struct GitMetadataProber {
let branch: String?
let isDirty: Bool
let pullRequest: WorkspacePullRequestSnapshot
var pullRequestProbedAt: ContinuousClock.Instant? = nil
}

struct GitHubPullRequestProbeItem: Decodable, Equatable {
Expand All @@ -745,6 +746,7 @@ struct GitMetadataProber {
let updatedAt: String?
var headRefOid: String? = nil
var isCrossRepository: Bool? = nil
var isDraft: Bool? = nil
}

private struct GitHubPullRequestCheckItem: Decodable {
Expand All @@ -766,6 +768,8 @@ struct GitMetadataProber {
private nonisolated static let pullRequestLookupInitialBackoff: TimeInterval = 5
private nonisolated static let pullRequestLookupCacheLimit = 256

private typealias PullRequestLookup = (snapshot: WorkspacePullRequestSnapshot, probedAt: ContinuousClock.Instant?)

/// Wait before retrying `gh` after `failures` consecutive transient failures:
/// 5 s, 10 s, 20 s, ... capped at 10 minutes. Starts short so the startup retry burst
/// (TabManager's initial probe delays) can still recover from one slow first call.
Expand All @@ -778,7 +782,7 @@ struct GitMetadataProber {
private final class PullRequestLookupThrottle: @unchecked Sendable {
struct Entry {
let branch: String
var snapshot: WorkspacePullRequestSnapshot
let lookup: PullRequestLookup
var lastAttemptAt: Date
var consecutiveFailures: Int
}
Expand All @@ -787,18 +791,18 @@ struct GitMetadataProber {
private var entries: [String: Entry] = [:]

/// The cached answer when a fresh `gh` lookup is not due yet, else nil.
func cachedSnapshot(directory: String, branch: String, now: Date) -> WorkspacePullRequestSnapshot? {
func cachedLookup(directory: String, branch: String, now: Date) -> PullRequestLookup? {
lock.lock()
defer { lock.unlock() }
guard let entry = entries[directory], entry.branch == branch else { return nil }
let wait = entry.consecutiveFailures > 0
? GitMetadataProber.pullRequestLookupBackoff(failures: entry.consecutiveFailures)
: GitMetadataProber.pullRequestLookupRefreshInterval
return now.timeIntervalSince(entry.lastAttemptAt) < wait ? entry.snapshot : nil
return now.timeIntervalSince(entry.lastAttemptAt) < wait ? entry.lookup : nil
}

func record(
_ snapshot: WorkspacePullRequestSnapshot,
_ lookup: PullRequestLookup,
directory: String,
branch: String,
now: Date,
Expand All @@ -807,20 +811,16 @@ struct GitMetadataProber {
lock.lock()
defer { lock.unlock() }
let previous = entries[directory].flatMap { $0.branch == branch ? $0 : nil }
if case .transientFailure = snapshot {
if case .transientFailure = lookup.snapshot {
// Keep showing the last good answer during the backoff.
let lastGood = previous.map(\.snapshot).flatMap { cached -> WorkspacePullRequestSnapshot? in
if case .transientFailure = cached { return nil }
return cached
}
entries[directory] = Entry(
branch: branch,
snapshot: lastGood ?? .transientFailure,
lookup: previous?.lookup ?? lookup,
lastAttemptAt: now,
consecutiveFailures: (previous?.consecutiveFailures ?? 0) + 1
)
} else {
entries[directory] = Entry(branch: branch, snapshot: snapshot, lastAttemptAt: now, consecutiveFailures: 0)
entries[directory] = Entry(branch: branch, lookup: lookup, lastAttemptAt: now, consecutiveFailures: 0)
}
if entries.count > limit,
let oldest = entries.min(by: { $0.value.lastAttemptAt < $1.value.lastAttemptAt })?.key {
Expand Down Expand Up @@ -862,7 +862,9 @@ struct GitMetadataProber {
)
let isDirty = !(statusOutput?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ?? true)
let pullRequest = workspacePullRequestSnapshot(directory: directory, branch: branch)
return InitialWorkspaceGitMetadataSnapshot(branch: branch, isDirty: isDirty, pullRequest: pullRequest)
return InitialWorkspaceGitMetadataSnapshot(
branch: branch, isDirty: isDirty, pullRequest: pullRequest.snapshot, pullRequestProbedAt: pullRequest.probedAt
)
}

private nonisolated static func runGitCommand(directory: String, arguments: [String]) -> String? {
Expand All @@ -877,24 +879,26 @@ struct GitMetadataProber {
private nonisolated static func workspacePullRequestSnapshot(
directory: String,
branch: String
) -> WorkspacePullRequestSnapshot {
) -> PullRequestLookup {
guard !shouldSkipWorkspacePullRequestLookup(branch: branch) else {
return .notFound
return (.notFound, nil)
}

let now = Date()
if let cached = pullRequestLookupThrottle.cachedSnapshot(directory: directory, branch: branch, now: now) {
if let cached = pullRequestLookupThrottle.cachedLookup(directory: directory, branch: branch, now: now) {
return cached
}
let probedAt = ContinuousClock.now
let snapshot = uncachedWorkspacePullRequestSnapshot(directory: directory, branch: branch)
let lookup: PullRequestLookup = (snapshot, probedAt)
pullRequestLookupThrottle.record(
snapshot,
lookup,
directory: directory,
branch: branch,
now: now,
limit: pullRequestLookupCacheLimit
)
return snapshot
return lookup
}

private nonisolated static func uncachedWorkspacePullRequestSnapshot(
Expand Down Expand Up @@ -934,7 +938,7 @@ struct GitMetadataProber {
"--repo", repoSlug,
"--state", "all",
"--head", branch,
"--json", "number,state,url,updatedAt,headRefOid,isCrossRepository",
"--json", "number,state,url,updatedAt,headRefOid,isCrossRepository,isDraft",
],
timeout: workspacePullRequestProbeTimeout
)
Expand Down Expand Up @@ -1020,16 +1024,18 @@ struct GitMetadataProber {
status: status,
branch: branch,
checks: checks,
headSHA: shippableHead(pullRequest, directory: directory),
headSHA: shippableHead(pullRequest, checkedOut: checkedOutCommit(directory: directory)),
source: .probe(repoSlug: repoSlug, directory: directory)
)
)
}

enum ShipVerdict: Sendable {
enum ShipVerdict: Equatable, Sendable {
case ready
/// No longer open, or its head or the checkout moved off the commit.
case changed
case merged
case closed
case temporarilyUnavailable
case headMoved
case checksNotPassing
/// `gh` or `git` gave no usable answer.
case unverified
Expand All @@ -1044,7 +1050,9 @@ struct GitMetadataProber {
/// failing; and whether `directory` still has that commit checked out. Blocks; call off-main.
nonisolated static func shipVerdict(number: Int, repoSlug: String, directory: String, headSHA: String) -> ShipVerdict {
guard let checkedOut = checkedOutCommit(directory: directory) else { return .unverified }
guard checkedOut == headSHA else { return .changed }
guard checkedOut == headSHA else {
return classifyShip(headSHA: headSHA, checkedOut: checkedOut, pullRequestHead: nil, readiness: nil)
}
let result = runCommandResult(
directory: directory,
executable: "gh",
Expand All @@ -1056,7 +1064,21 @@ struct GitMetadataProber {
let readiness = ReviewMergeReadinessProber.parse(pullRequestJSON: output, reviewThreadsJSON: nil) else {
return .unverified
}
guard readiness.state == .open, head.headRefOid == headSHA else { return .changed }
return classifyShip(headSHA: headSHA, checkedOut: checkedOut, pullRequestHead: head.headRefOid, readiness: readiness)
}

nonisolated static func classifyShip(
headSHA: String, checkedOut: String, pullRequestHead: String?, readiness: ReviewMergeReadiness?
) -> ShipVerdict {
guard checkedOut == headSHA else { return .temporarilyUnavailable }
guard let readiness else { return .unverified }
switch readiness.state {
case .merged: return .merged
case .closed: return .closed
case .open, .draft: break
}
guard pullRequestHead == headSHA else { return .headMoved }
guard readiness.state == .open else { return .temporarilyUnavailable }
guard readiness.failingCheckNames.isEmpty, readiness.pendingCheckCount == 0, readiness.passingCheckCount > 0 else {
return .checksNotPassing
}
Expand All @@ -1066,9 +1088,9 @@ struct GitMetadataProber {
/// The head commit Ship may pin, or nil. `--head` matches the branch name in every fork, so a
/// cross-repository pull request (or one whose origin `gh` did not report) is never shippable,
/// though the sidebar still shows it; nor is one whose head is not the checked-out commit.
private nonisolated static func shippableHead(_ pullRequest: GitHubPullRequestProbeItem, directory: String) -> String? {
guard pullRequest.isCrossRepository == false, let head = pullRequest.headRefOid,
head == checkedOutCommit(directory: directory) else { return nil }
nonisolated static func shippableHead(_ pullRequest: GitHubPullRequestProbeItem, checkedOut: @autoclosure () -> String?) -> String? {
guard pullRequest.isDraft != true, pullRequest.isCrossRepository == false,
let head = pullRequest.headRefOid, head == checkedOut() else { return nil }
return head
}

Expand Down
Loading
Loading