1919import subprocess
2020import sys
2121import textwrap
22+ import time
2223
2324PRIMARY_BRANCH = 'main'
2425MAX_ATTEMPTS = 3
26+ # Let the jobs API converge after the workflow_run completion event.
27+ JOB_POLL_INTERVAL = 30
28+ JOB_POLL_BOUND = 10 * 60
2529# Exact entrypoints/events, independently checked in addition to workflow_run names.
2630ALLOWED_WORKFLOWS = {
2731 '.github/workflows/ci-post.yml' : frozenset ({'push' , 'schedule' }),
@@ -133,6 +137,14 @@ def post(self, path):
133137 self ._call (path , method = 'POST' )
134138
135139
140+ def _clock ():
141+ return time .monotonic ()
142+
143+
144+ def _sleep (seconds ):
145+ time .sleep (seconds )
146+
147+
136148def _gate (run , repo , expected_attempt ):
137149 if run ['repository' ]['full_name' ] != repo :
138150 return 'repository_mismatch'
@@ -580,6 +592,38 @@ def _job_evidence(api, repo, job, *, diagnostics=None):
580592 return reason
581593
582594
595+ def _jobs (api , endpoint , attempt ):
596+ return [job for page in api .pages (f'{ endpoint } /attempts/{ attempt } /jobs?per_page=100' )
597+ for job in page ['jobs' ]]
598+
599+
600+ def _run_changed (current , initial , repo , attempt ):
601+ identity = ('id' , 'run_attempt' , 'head_sha' , 'path' , 'event' , 'head_branch' )
602+ return _gate (current , repo , attempt ) or any (current [key ] != initial [key ] for key in identity )
603+
604+
605+ def _completed_jobs (api , endpoint , attempt , initial_run , repo ):
606+ jobs = _jobs (api , endpoint , attempt )
607+ if jobs and all (job ['status' ] == 'completed' for job in jobs ):
608+ return jobs , None , None
609+
610+ started = _clock ()
611+ deadline = started + JOB_POLL_BOUND
612+ while True :
613+ remaining = deadline - _clock ()
614+ if remaining <= 0 :
615+ waited = round (max (0.0 , _clock () - started ), 3 )
616+ return jobs , waited , 'missing_or_incomplete_jobs'
617+ _sleep (min (JOB_POLL_INTERVAL , remaining ))
618+ waited = round (max (0.0 , _clock () - started ), 3 )
619+ current = api .get (endpoint )
620+ if _run_changed (current , initial_run , repo , attempt ):
621+ return jobs , waited , 'run_changed_during_classification'
622+ jobs = _jobs (api , endpoint , attempt )
623+ if jobs and all (job ['status' ] == 'completed' for job in jobs ):
624+ return jobs , waited , None
625+
626+
583627def process (api , repo , run_id , * , dry_run = False , expected_attempt = None ):
584628 """Return a bounded decision; lookup ambiguity always refuses the POST."""
585629 result = {'run_id' : run_id , 'dry_run' : dry_run , 'rerun' : False ,
@@ -595,9 +639,11 @@ def process(api, repo, run_id, *, dry_run=False, expected_attempt=None):
595639 result ['reason' ] = reason
596640 return result
597641 attempt = int (run ['run_attempt' ])
598- jobs = [job for page in api .pages (f'{ endpoint } /attempts/{ attempt } /jobs?per_page=100' ) for job in page ['jobs' ]]
599- if not jobs or any (job ['status' ] != 'completed' for job in jobs ):
600- result ['reason' ] = 'missing_or_incomplete_jobs'
642+ jobs , waited , reason = _completed_jobs (api , endpoint , attempt , run , repo )
643+ if waited is not None :
644+ result ['waited_seconds' ] = waited
645+ if reason :
646+ result ['reason' ] = reason
601647 return result
602648 if any (job ['conclusion' ] not in {'success' , 'skipped' , 'neutral' , 'failure' } for job in jobs ):
603649 result ['reason' ] = 'ambiguous_unsuccessful_job'
@@ -611,7 +657,11 @@ def process(api, repo, run_id, *, dry_run=False, expected_attempt=None):
611657 specs = _workflow_jobs (api , repo , run )
612658 # Map failures first so an unresolvable failure names its blocker.
613659 mapped = _map_jobs (specs , failed )
614- mapped .update (_map_jobs (specs , [j for j in jobs if j not in failed ]))
660+ # A skipped matrix row may collapse to an aggregate display name
661+ # when a docs-only condition is evaluated before expansion. It
662+ # cannot be a failed DAG node, so only non-skipped jobs need aliases.
663+ mapped .update (_map_jobs (specs , [j for j in jobs
664+ if j not in failed and j ['conclusion' ] != 'skipped' ]))
615665 failed_needs = {
616666 int (job ['id' ]): {int (other ['id' ]) for other in failed
617667 if any (mapped [int (other ['id' ])] in specs [need ]['members' ]
@@ -658,8 +708,7 @@ def process(api, repo, run_id, *, dry_run=False, expected_attempt=None):
658708 return result
659709 # Re-read just before mutation; concurrency serializes controller runs too.
660710 current = api .get (endpoint )
661- identity = ('id' , 'run_attempt' , 'head_sha' , 'path' , 'event' , 'head_branch' )
662- if _gate (current , repo , attempt ) or any (current [key ] != run [key ] for key in identity ):
711+ if _run_changed (current , run , repo , attempt ):
663712 result ['reason' ] = 'run_changed_during_classification'
664713 return result
665714 if not dry_run :
0 commit comments