Repository: dotnet/runtime
File: .github/skills/vmr-codeflow-status/scripts/Get-CodeflowStatus.ps1 (PowerShell)

Code before:
# --- Step 2: Current State (independent assessment from primary signals) ---
Write-Section "Current State"

# Check for empty diff (0 changed files)
$isEmptyDiff = ($pr.changedFiles -eq 0 -and $pr.additions -eq 0 -and $pr.deletions -eq 0)
if ($isEmptyDiff) {
    Write-Host "  📭 Empty diff: 0 changed files, 0 additions, 0 deletions" -ForegroundColor Yellow
}

# Check PR timeline for force pushes
$forcePushEvents = @()
$owner, $repo = $Repository -split '/'
try {
    $timelineJson = gh api "repos/$owner/$repo/issues/$PRNumber/timeline" --paginate --slurp --jq 'map(.[] | select(.event == "head_ref_force_pushed"))' 2>$null
    if ($LASTEXITCODE -eq 0 -and $timelineJson) {
        $forcePushEvents = @($timelineJson | ConvertFrom-Json)
    }
}
catch {
    Write-Verbose "Failed to parse timeline JSON for force push events: $($_.Exception.Message)"
    $forcePushEvents = @()
}

if ($forcePushEvents.Count -gt 0) {
    foreach ($fp in $forcePushEvents) {
        $fpActor = if ($fp.actor) { $fp.actor.login } else { "unknown" }
        $fpTime = $fp.created_at
        $fpSha = if ($fp.commit_id) { Get-ShortSha $fp.commit_id } else { "unknown" }
        Write-Host "  🔄 Force push by @$fpActor at $fpTime (→ $fpSha)" -ForegroundColor Cyan
    }
    $lastForcePush = $forcePushEvents[-1]
    $lastForcePushTime = if ($lastForcePush.created_at) {
        [DateTimeOffset]::Parse($lastForcePush.created_at).UtcDateTime
    } else { $null }
    $lastForcePushActor = if ($lastForcePush.actor) { $lastForcePush.actor.login } else { "unknown" }
}

# Synthesize current state assessment
$prUpdatedTime = if ($pr.updatedAt) { [DateTimeOffset]::Parse($pr.updatedAt).UtcDateTime } else { $null }
$prAgeDays = if ($prUpdatedTime) { ([DateTime]::UtcNow - $prUpdatedTime).TotalDays } else { 0 }
$isClosed = $pr.state -eq "CLOSED"
$isMerged = $pr.state -eq "MERGED"
$currentState = if ($isMerged) {
    "MERGED"
} elseif ($isClosed) {
    "CLOSED"
} elseif ($isEmptyDiff -and $forcePushEvents.Count -gt 0) {
    "NO-OP"
} elseif ($isEmptyDiff) {
    "NO-OP"
} elseif ($forcePushEvents.Count -gt 0 -and $lastForcePushTime -and ([DateTime]::UtcNow - $lastForcePushTime).TotalHours -lt 24) {

Review comment (bug):
`gh api ... --paginate --jq '[...]'` is likely to emit one JSON array per page; concatenating pages produces invalid JSON and `ConvertFrom-Json` can throw (and terminate the script due to `$ErrorActionPreference = "Stop"`). Consider using `--slurp` with `--paginate` (so output is a single array) and/or wrap the JSON conversion in `try/catch` with a graceful fallback to `@()` when parsing fails.

Code after:

if ($isForwardFlow) {
    $sourceRepo = $Matches[1]
    Write-Status "Flow" "Forward ($sourceRepo → $Repository)" "Cyan"
}
elseif ($isBackflow) {
    Write-Status "Flow" "Backflow (dotnet/dotnet → $Repository)" "Cyan"
}

# --- Step 2: Current State (independent assessment from primary signals) ---
Write-Section "Current State"

# Check for empty diff (0 changed files)
$isEmptyDiff = ($pr.changedFiles -eq 0 -and $pr.additions -eq 0 -and $pr.deletions -eq 0)
if ($isEmptyDiff) {
    Write-Host "  📭 Empty diff: 0 changed files, 0 additions, 0 deletions" -ForegroundColor Yellow
}

# Check PR timeline for force pushes
$forcePushEvents = @()
$owner, $repo = $Repository -split '/'
$forcePushFetchSucceeded = $false
try {
    $timelineJson = gh api "repos/$owner/$repo/issues/$PRNumber/timeline" --paginate --slurp --jq 'map(.[] | select(.event == "head_ref_force_pushed"))' 2>$null
    if ($LASTEXITCODE -eq 0 -and $timelineJson) {
        $forcePushEvents = @($timelineJson | ConvertFrom-Json)
        $forcePushFetchSucceeded = $true
    } elseif ($LASTEXITCODE -ne 0) {
        Write-Warning "Could not fetch PR timeline for force push detection (gh api exit code $LASTEXITCODE). Current state assessment may be incomplete."
    } else {
        $forcePushFetchSucceeded = $true
    }
}
catch {
    Write-Warning "Failed to parse timeline JSON for force push events: $($_.Exception.Message)"
    $forcePushEvents = @()
}

if ($forcePushEvents.Count -gt 0) {
    foreach ($fp in $forcePushEvents) {
        $fpActor = if ($fp.actor) { $fp.actor.login } else { "unknown" }
        $fpTime = $fp.created_at
        $fpSha = if ($fp.commit_id) { Get-ShortSha $fp.commit_id } else { "unknown" }
        Write-Host "  🔄 Force push by @$fpActor at $fpTime (→ $fpSha)" -ForegroundColor Cyan
    }
    $lastForcePush = $forcePushEvents[-1]
    $lastForcePushTime = if ($lastForcePush.created_at) {
        [DateTimeOffset]::Parse($lastForcePush.created_at).UtcDateTime
    } else { $null }
    $lastForcePushActor = if ($lastForcePush.actor) { $lastForcePush.actor.login } else { "unknown" }
}