function Get-WorkflowRunStatus { param( [Parameter(Mandatory)][string]$Repository, [string]$LogRoot, [Parameter(Mandatory)][string]$WorkflowId ) $latest = $null foreach ($file in @(Get-ChildItem -LiteralPath (Join-Path $Repository 'receipts') -Filter '*.json' -File -ErrorAction SilentlyContinue)) { try { $r = Get-Content -LiteralPath $file.FullName -Raw | ConvertFrom-Json if ($r.skill.name -ne $WorkflowId) { continue } $time = [DateTimeOffset]::Parse($r.finished_at) $outcome = 'unknown' if ($r.status -in @('failure','failed','blocked') -or @($r.failures | Where-Object { $null -ne $_ }).Count) { $outcome = 'failure' } elseif ($r.status -eq 'success' -and $r.validation.deterministic -eq 'pass' -and $r.validation.manual -eq 'pass' -and $r.artifacts.checkpoint_advanced -eq $true) { $base = [IO.Path]::GetFullPath($Repository).TrimEnd([IO.Path]::DirectorySeparatorChar) + [IO.Path]::DirectorySeparatorChar $report = [IO.Path]::GetFullPath((Join-Path $Repository $r.artifacts.report)) if (-not $report.StartsWith($base, [StringComparison]::OrdinalIgnoreCase)) { throw 'Report path escapes configured repository.' } if ((Test-Path -LiteralPath $report -PathType Leaf) -and (Get-Item -LiteralPath $report).Length -gt 0) { $outcome = 'success' } } if (-not $latest -or $time -gt $latest.time) { $latest = [pscustomobject]@{ time=$time; outcome=$outcome; detail="Validated workflow receipt: $($file.Name); outcome: $outcome."; source='workflow-run-receipt' } } } catch { Write-Warning "Unable to read workflow receipt $($file.Name)." } } # Startup failures occur before a task exists, so no workflow receipt can be written. # Look back two weekly cycles; retain the latest completed run as the baseline. $day = (Get-Date).Date.AddDays(-15) while ($LogRoot -and $day -le (Get-Date).Date) { $folder = Join-Path $LogRoot ($day.ToString('yyyy/MM/dd')) foreach ($file in @(Get-ChildItem -LiteralPath $folder -Filter '*.log' -File -ErrorAction SilentlyContinue)) { foreach ($hit in @(Select-String -LiteralPath $file.FullName -Pattern ('Scheduled run failed automationId=' + [regex]::Escape($WorkflowId) + '(?:\s|$)') -ErrorAction SilentlyContinue)) { $stamp = [regex]::Match($hit.Line, '^\S+') $time = [DateTimeOffset]::MinValue if ($stamp.Success -and [DateTimeOffset]::TryParse($stamp.Value, [ref]$time) -and (-not $latest -or $time -gt $latest.time)) { $message = [regex]::Match($hit.Line, 'errorMessage="([^"]+)"').Groups[1].Value $latest = [pscustomobject]@{ time=$time; outcome='failure'; detail="Codex scheduler could not start the run: $message. Evidence: $($file.Name):$($hit.LineNumber)."; source='codex-scheduler-log' } } } } $day = $day.AddDays(1) } return $latest }