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
113 changes: 94 additions & 19 deletions scripts/Start-IssuePrLoop.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@
# 2. Runs the merged-worktree sweep and surveys open PRs and issues with `gh`. When
# the latest push CI run of any workflow on main failed, fixing main comes first.
# 3. Includes draft PRs, which need review processing and promotion. Skips `needs-human`
# and `on-hold` items, PRs whose checks are still running, and items whose GitHub
# state is unchanged since an agent last deferred them. Also skips issues that
# cannot be worked yet: issues with open native sub-issues or open `blocked_by`
# dependencies, issues that already have an open closing PR, and bot-owned tracking
# issues. Stranded branches follow the same rules. Ranks local `issue-*` branches
# with unpublished commits and no open PR above new issues, then parents whose
# sub-issues are all closed, then issues that block the most other issues. An item
# that a deferral named as its blocker goes first, so the next unit works the blocker
# instead of the blocked item.
# and `on-hold` items, PRs whose checks are still running (unless they conflict, have
# unresolved review threads, or have a changes-requested review newer than the last
# push), and items whose GitHub state is unchanged since an agent last deferred them.
# Also skips issues that cannot be worked yet: issues with open native sub-issues or
# open `blocked_by` dependencies, issues that already have an open closing PR, and
# bot-owned tracking issues. Stranded branches follow the same rules. Ranks local
# `issue-*` branches with unpublished commits and no open PR above new issues, then
# parents whose sub-issues are all closed, then issues that block the most other
# issues. Starts no new issue while -MaxOpenPrs or more of the loop's PRs are open and
# not parked, because every PR in flight is another chance of a conflict; existing PRs,
# stranded branches, and stale claims still run. An item that a deferral named as its
# blocker goes first, so the next unit works the blocker instead of the blocked item.
# 4. Acquires the item's Redis lock, moving on to the next candidate when another
# worker holds it.
# 5. Starts the agent on that one item, with lower reasoning effort for PRs that
Expand Down Expand Up @@ -60,7 +63,7 @@
#
# Stop: create the stop file printed at startup (checked between units), or Ctrl+C.
# Usage: pwsh scripts/Start-IssuePrLoop.ps1 [-Agent auto|claude|codex] [-IdleSeconds n] [-MaxUnits n]
# [-DeferSeconds n] [-MaxDeferSeconds n] [-LightEffort level] [-HeavyEffort level]
# [-DeferSeconds n] [-MaxDeferSeconds n] [-MaxOpenPrs n] [-LightEffort level] [-HeavyEffort level]
# [-TriageEvery n] [-TriageBatch n] [-AuditDays n] [-ParkedAuditHours n] [-DiscoveryHours n] [-DryRun]
# pwsh scripts/Start-IssuePrLoop.ps1 -Report [-ReportHours n]

Expand All @@ -79,6 +82,9 @@ param(
# Upper bound for a repeatedly deferred item's backoff.
[ValidateRange(60, 604800)]
[int]$MaxDeferSeconds = 259200,
# Open, unparked loop PRs at which no new issue is started. 0 means no limit.
[ValidateRange(0, 1000)]
[int]$MaxOpenPrs = 8,
# Reasoning effort for PRs that look ready to merge, and for all other units.
# An empty value keeps the agent's configured default.
[string]$LightEffort = 'medium',
Expand Down Expand Up @@ -260,6 +266,62 @@ function Get-PrFingerprint($Pr) {
Get-Hash ((@($Pr.headRefOid, $Pr.isDraft, $Pr.mergeable, ($labels -join ','), $review, $comment) + $checks + $codexReactions) -join '|')
}

function ConvertTo-Instant($Value) {
if ($Value -is [datetime]) { return [DateTimeOffset]$Value }
if ($Value) { return [DateTimeOffset]::Parse([string]$Value, [Globalization.CultureInfo]::InvariantCulture) }
return $null
}

# True when a PR has review feedback that no unit has dispositioned: an unresolved review
# thread (units resolve each thread they disposition), or a changes-requested review newer
# than both the last push and the loop's last reply. Such feedback can be handled while
# checks still run. Conversation comments do not count: a finished review with no
# findings is also a comment, and would send units to PRs with nothing to do.
function Test-HasNewFeedback($Pr, $Activity) {
if (-not $Activity) { return $false }
if ($Activity.UnresolvedThreads -gt 0) { return $true }
$since = ConvertTo-Instant $Activity.HeadCommittedAt
if (-not $since) { return $false }
$requested = [Collections.Generic.List[object]]::new()
foreach ($review in @($Pr.reviews)) {
if (-not $review -or -not $review.submittedAt) { continue }
$at = ConvertTo-Instant $review.submittedAt
# The loop replies to review threads as the PR author, which GitHub records as reviews.
if ($review.author.login -eq $Pr.author.login) { if ($at -gt $since) { $since = $at } }
elseif ($review.state -eq 'CHANGES_REQUESTED') { $requested.Add($at) }
}
foreach ($comment in @($Pr.comments)) {
if ($comment -and $comment.createdAt -and ([string]$comment.body).Contains($loopMarker)) {
$at = ConvertTo-Instant $comment.createdAt
if ($at -gt $since) { $since = $at }
}
}
@($requested | Where-Object { $_ -gt $since }).Count -gt 0
}

# Returns PR number -> head commit time and unresolved review thread count for open PRs.
# `gh pr list --json commits` asks for every commit's authors and exceeds GitHub's
# GraphQL node limit, so this asks only for the head commit.
function Get-PrActivity {
$query = 'query($owner: String!, $name: String!, $cursor: String) { repository(owner: $owner, name: $name) { ' +
'pullRequests(states: OPEN, first: 100, after: $cursor) { pageInfo { hasNextPage endCursor } ' +
'nodes { number commits(last: 1) { nodes { commit { committedDate } } } reviewThreads(first: 100) { nodes { isResolved } } } } } }'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Paginate review threads before counting unresolved feedback

For a PR with more than 100 review threads, this query ignores every thread after the first page; the surrounding cursor loop paginates only pullRequests. If the first 100 threads are resolved and a later thread is unresolved, Test-HasNewFeedback returns false unless a separate changes-requested review qualifies. Consequently, a pending PR is still skipped despite actionable feedback, and a clean PR can incorrectly receive light effort. Fetch the remaining thread pages before computing UnresolvedThreads, as the existing review gate in scripts/Assert-PrGreen.ps1 does.

AGENTS.md reference: AGENTS.md:L54-L55

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Later threads go unseen

reviewThreads(first: 100) reads only one page. If a PR has more than 100 threads, the first page can be fully resolved while a later thread remains open. UnresolvedThreads then becomes zero, so a comment-only review on that later thread does not wake the PR while checks run. Read the remaining thread pages before deciding that no unresolved feedback exists.

$activity = @{}
$cursor = $null
do {
$variables = if ($cursor) { @('-f', "cursor=$cursor") } else { @() }
$page = (Invoke-GraphQl $query $variables).pullRequests
foreach ($node in @($page.nodes)) {
$activity[[int]$node.number] = [pscustomobject]@{
HeadCommittedAt = @($node.commits.nodes)[0].commit.committedDate
UnresolvedThreads = @($node.reviewThreads.nodes | Where-Object { -not $_.isResolved }).Count
}
}
$cursor = $page.pageInfo.endCursor
} while ($page.pageInfo.hasNextPage)
return $activity
}

function Get-IssueFingerprint($Issue) {
$labels = @($Issue.labels.nodes | ForEach-Object { $_.name } | Sort-Object)
$assignees = @($Issue.assignees.nodes | ForEach-Object { $_.login } | Sort-Object)
Expand Down Expand Up @@ -326,18 +388,18 @@ $deferCategories = @('human', 'upstream', 'credentials', 'locked')
$surveyStats = [ordered]@{}

function Reset-SurveyStats {
foreach ($key in 'issues', 'issue-deferred', 'issue-locked', 'issue-excluded', 'issue-parent', 'issue-blocked', 'issue-has-pr', 'issue-claimed',
foreach ($key in 'issues', 'issue-wip', 'issue-deferred', 'issue-locked', 'issue-excluded', 'issue-parent', 'issue-blocked', 'issue-has-pr', 'issue-claimed',
'prs', 'pr-deferred', 'pr-locked', 'pr-pending', 'pr-excluded', 'pr-draft') {
$surveyStats[$key] = 0
}
}

function Format-SurveyStats {
$s = $surveyStats
('issues: {0} open, {1} deferred, {2} locked, {3} excluded by label/assignee/bot, {4} with open sub-issues, {5} blocked, {6} with open PR, {7} claimed; ' +
'PRs: {8} open, {9} deferred, {10} locked, {11} pending checks, {12} needs-human/on-hold, {13} draft') -f `
('issues: {0} open, {1} deferred, {2} locked, {3} excluded by label/assignee/bot, {4} with open sub-issues, {5} blocked, {6} with open PR, {7} claimed, ' +
'{14} held by the open-PR limit; PRs: {8} open, {9} deferred, {10} locked, {11} pending checks, {12} needs-human/on-hold, {13} draft') -f `
$s['issues'], $s['issue-deferred'], $s['issue-locked'], $s['issue-excluded'], $s['issue-parent'], $s['issue-blocked'], $s['issue-has-pr'], $s['issue-claimed'],
$s['prs'], $s['pr-deferred'], $s['pr-locked'], $s['pr-pending'], $s['pr-excluded'], $s['pr-draft']
$s['prs'], $s['pr-deferred'], $s['pr-locked'], $s['pr-pending'], $s['pr-excluded'], $s['pr-draft'], $s['issue-wip']
}

function Invoke-GraphQl([string]$Query, [string[]]$Variables) {
Expand Down Expand Up @@ -472,22 +534,31 @@ function Get-Candidates {
$prs = gh pr list --repo $repoSlug --author '@me' --state open --limit 500 --json $prFields | ConvertFrom-Json
if ($LASTEXITCODE -ne 0) { throw 'gh pr list failed.' }
$surveyStats['prs'] = @($prs).Count
$inFlight = 0
$prActivity = if (@($prs).Count -gt 0) { Get-PrActivity } else { @{} }
$ranked = foreach ($pr in $prs) {
if ($pr.isDraft) { $surveyStats['pr-draft']++ }
if (@($pr.labels | Where-Object { $_.name -in $excludedPrLabels }).Count -gt 0) { $surveyStats['pr-excluded']++; continue }
$inFlight++
$checks = @($pr.statusCheckRollup)
$pending = @($checks | Where-Object { (Get-CheckState $_) -in 'PENDING', 'EXPECTED', 'QUEUED', 'IN_PROGRESS', 'WAITING', 'REQUESTED', '' })
$failed = @($checks | Where-Object { (Get-CheckState $_) -in 'FAILURE', 'ERROR', 'TIMED_OUT', 'CANCELLED', 'ACTION_REQUIRED', 'STARTUP_FAILURE' })
$conflicting = $pr.mergeable -eq 'CONFLICTING'
# Running checks leave nothing to do yet; the next survey picks the PR up.
if ($pending.Count -gt 0 -and -not $conflicting) { $surveyStats['pr-pending']++; continue }
$feedback = Test-HasNewFeedback $pr $prActivity[[int]$pr.number]
# Running checks leave nothing to do yet, unless the PR conflicts or has new review
# feedback: finishing open PRs beats starting new ones. The next survey picks it up.
if ($pending.Count -gt 0 -and -not $conflicting -and -not $feedback) { $surveyStats['pr-pending']++; continue }
# A draft is never a merge candidate: it needs review processing and promotion first.
$rank = if (-not $pr.isDraft -and $pr.mergeStateStatus -eq 'CLEAN' -and $failed.Count -eq 0) { 0 }
$rank = if (-not $pr.isDraft -and $pr.mergeStateStatus -eq 'CLEAN' -and $failed.Count -eq 0 -and -not $feedback) { 0 }
elseif ($conflicting) { 1 }
elseif ($failed.Count -gt 0) { 2 }
elseif ($failed.Count -gt 0 -or $feedback) { 2 }
else { 3 }
$hint = if ($feedback -and $pending.Count -gt 0) {
"PR #$($pr.number) has review feedback without a disposition while checks are still running. Handle the feedback now instead of waiting for the checks."
}
else { '' }
# A green, clean PR usually needs only the merge gate or a disposition reply.
New-Candidate 'pr' ([int]$pr.number) $rank (Get-PrFingerprint $pr) ($rank -eq 0)
New-Candidate 'pr' ([int]$pr.number) $rank (Get-PrFingerprint $pr) ($rank -eq 0) $hint

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Reopened feedback stays deferred

When a reviewer reopens a resolved thread on a deferred PR, Test-HasNewFeedback makes it actionable. But Get-PrFingerprint leaves out thread state. If the head, checks, and other comments are unchanged, Test-Deferred still skips the PR until CI changes or the deferral expires. Include thread state in both survey and post-unit fingerprints so this feedback can reach a worker immediately.

}
foreach ($c in @($ranked | Sort-Object Rank, Number)) { $candidates.Add($c) }

Expand All @@ -496,6 +567,9 @@ function Get-Candidates {
$openIssues = @{}
foreach ($issue in $issues) { $openIssues[[int]$issue.number] = $issue }
$script:lastIssues = $issues
# Every open PR is another chance of a conflict, so finish existing work before
# starting more once enough PRs are in flight.
$atPrLimit = $MaxOpenPrs -gt 0 -and $inFlight -ge $MaxOpenPrs
$stranded = Get-StrandedBranches $openIssues
foreach ($number in @($stranded.Keys | Sort-Object)) {
$s = $stranded[$number]
Expand Down Expand Up @@ -547,6 +621,7 @@ function Get-Candidates {
New-Candidate 'issue' $number 5 $fingerprint $true $hint
continue
}
if ($atPrLimit) { $surveyStats['issue-wip']++; continue }
$candidate = New-Candidate 'issue' $number 6 $fingerprint $false
$candidate.Blocking = [int]$issue.issueDependenciesSummary.blocking
$candidate.Comments = [int]$issue.comments.totalCount
Expand Down
68 changes: 66 additions & 2 deletions scripts/Test-IssuePrLoop.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ $ast = [Management.Automation.Language.Parser]::ParseFile(
(Join-Path $PSScriptRoot 'Start-IssuePrLoop.ps1'), [ref]$null, [ref]$parseErrors)
if ($parseErrors.Count) { throw ($parseErrors -join "`n") }
foreach ($name in 'Get-Hash', 'Get-CheckState', 'Get-LatestOutsideComment', 'Get-PrFingerprint',
'Reset-SurveyStats', 'New-Candidate', 'Get-Candidates', 'Get-ReasonReferences') {
'Reset-SurveyStats', 'New-Candidate', 'Get-Candidates', 'Get-ReasonReferences',
'ConvertTo-Instant', 'Test-HasNewFeedback', 'Get-IssueFingerprint', 'Test-HasLabel', 'Test-HasOpenPr') {
$definition = $ast.Find({ param($node)
$node -is [Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq $name
}, $false)
Expand All @@ -21,17 +22,20 @@ function Assert-Equal($Actual, $Expected, [string]$Message) {
if ($actualJson -ne $expectedJson) { throw "$Message. Expected $expectedJson, got $actualJson." }
}

# The survey's issue half has its own sources; this test covers the PR half only.
# The survey's issue sources are stubbed; the open-PR limit test supplies issues.
function Get-MainCandidate { }
function Get-OpenIssues { }
function Get-StrandedBranches { @{} }
function Get-IssuePriority { 0 }
$script:activity = @{}
function Get-PrActivity { $script:activity }
function gh { $global:LASTEXITCODE = 0; ConvertTo-Json -InputObject $script:prs -Depth 20 }

$repoSlug = 'test/repo'
$prFields = ''
$excludedPrLabels = @('needs-human', 'on-hold')
$loopMarker = '<!-- issue-pr-loop -->'
$MaxOpenPrs = 8
$surveyStats = [ordered]@{}
$script:promoted = [Collections.Generic.HashSet[int]]::new()

Expand Down Expand Up @@ -69,6 +73,40 @@ foreach ($label in $excludedPrLabels) {
}
$draft.labels = @()

# Review feedback without a disposition is actionable while checks still run.
$draft.statusCheckRollup = @(@{ name = 'claude-review'; status = 'IN_PROGRESS' })
$script:activity = @{ 1 = [pscustomobject]@{ HeadCommittedAt = '2026-10-28T10:00:00Z'; UnresolvedThreads = 1 } }
Reset-SurveyStats
$feedbackCandidate = @(Get-Candidates) | Where-Object Number -eq 1
Assert-Equal $feedbackCandidate.Rank 2 'An unresolved review thread must make a pending PR actionable'
Assert-Equal ($feedbackCandidate.Hint -like '*review feedback*') $true 'A pending PR with feedback must say why it was chosen'
$script:activity[1].UnresolvedThreads = 0
$draft.reviews = @(@{ author = @{ login = 'reviewer' }; state = 'CHANGES_REQUESTED'; submittedAt = '2026-10-28T11:00:00Z' })
Reset-SurveyStats
Assert-Equal @((Get-Candidates).Number | Sort-Object) @(1, 2) 'A changes-requested review after the last push is feedback'
$draft.reviews += @{ author = @{ login = 'author' }; state = 'COMMENTED'; submittedAt = '2026-10-28T11:30:00Z' }
Reset-SurveyStats
Assert-Equal @((Get-Candidates).Number) @(2) 'Feedback the loop already answered must not wake a pending PR'
$draft.reviews = @(@{ author = @{ login = 'reviewer' }; state = 'CHANGES_REQUESTED'; submittedAt = '2026-10-28T11:00:00Z' })
$draft.comments = @([pscustomobject]@{ id = 'answer'; body = '<!-- issue-pr-loop --> Disposition'; createdAt = '2026-10-28T11:30:00Z'; reactionGroups = @() })
Reset-SurveyStats
Assert-Equal @((Get-Candidates).Number) @(2) 'A loop comment after the review must count as its disposition'
$draft.comments = @()
$draft.reviews = @(@{ author = @{ login = 'reviewer' }; state = 'CHANGES_REQUESTED'; submittedAt = '2026-10-28T09:00:00Z' })
Reset-SurveyStats
Assert-Equal @((Get-Candidates).Number) @(2) 'Feedback older than the last push must not wake a pending PR'
$draft.reviews = @(@{ author = @{ login = 'reviewer' }; state = 'COMMENTED'; submittedAt = '2026-10-28T11:00:00Z' })
$draft.comments = @([pscustomobject]@{ id = 'review'; body = 'Review: no blocking issues found.'; createdAt = '2026-10-28T11:00:00Z'; reactionGroups = @() })
Reset-SurveyStats
Assert-Equal @((Get-Candidates).Number) @(2) 'Comments and comment-only reviews without open threads must not wake a pending PR'
$script:activity[2] = [pscustomobject]@{ HeadCommittedAt = '2026-10-28T10:00:00Z'; UnresolvedThreads = 2 }
Reset-SurveyStats
Assert-Equal (@(Get-Candidates) | Where-Object Number -eq 2).Light $false 'A green PR with open threads needs full effort, not the merge gate'
$script:activity = @{}
$draft.comments = @()
$draft.reviews = @()
$draft.statusCheckRollup = @()

$before = Get-PrFingerprint $draft
$draft.isDraft = $false
Assert-Equal ((Get-PrFingerprint $draft) -ne $before) $true 'Marking a PR ready must invalidate a deferral'
Expand All @@ -95,4 +133,30 @@ Assert-Equal (Get-PrFingerprint $draft) $before 'A reaction on an older head''s

Assert-Equal @(Get-ReasonReferences 'Blocked on #12 and #4019; CI run #6494214847 failed, see #12' 4019) @(12) 'Deferral references must skip the item itself and ids too long for issue numbers'

# At the open-PR limit, existing PRs and claims still run but no new issue starts.
function New-TestIssue([int]$Number, [string[]]$Labels) {
[pscustomobject]@{
number = $Number; title = "Issue $Number"; lastEditedAt = $null; author = @{ login = 'author'; __typename = 'User' }
labels = @{ nodes = @($Labels | ForEach-Object { @{ name = $_ } }) }; assignees = @{ nodes = @() }
comments = @{ totalCount = 0; nodes = @() }; subIssuesSummary = @{ total = 0; completed = 0 }; subIssues = @{ nodes = @() }
issueDependenciesSummary = @{ blockedBy = 0; blocking = 0 }; closedByPullRequestsReferences = @{ nodes = @() }
}
}
$parkedLabels = @('needs-human', 'on-hold')
$excludedIssueLabels = @('wontfix', 'duplicate', 'question', 'needs-human', 'on-hold')
$script:issues = @((New-TestIssue 10 @()), (New-TestIssue 11 @()), (New-TestIssue 12 @('in-progress')))
function Get-OpenIssues { $script:issues }
$MaxOpenPrs = 2
$parked = $draft.PSObject.Copy(); $parked.number = 3; $parked.labels = @(@{ name = 'on-hold' })
$script:prs = @($draft, $parked)
Reset-SurveyStats
Assert-Equal @((Get-Candidates).Number) @(1, 10, 11, 12) 'Parked PRs must not count toward the open-PR limit'
$script:prs = @($draft, $ready)
Reset-SurveyStats
Assert-Equal @((Get-Candidates).Number) @(2, 1, 12) 'At the open-PR limit only existing work may be queued'
Assert-Equal $surveyStats['issue-wip'] 2 'Issues held by the open-PR limit must be counted'
$MaxOpenPrs = 0
Reset-SurveyStats
Assert-Equal @(Get-Candidates).Count 5 'A zero open-PR limit must not hold issues'

Write-Host 'Issue/PR loop survey and fingerprint tests passed.'
Loading