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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# why: this verdict stays I/O-free (no subprocess, no environment); ci.py owns the gh call

_FAILED_STATES = ("FAILURE", "TIMED_OUT", "ACTION_REQUIRED")
_FAILED_STATES = ("FAILURE", "TIMED_OUT", "ACTION_REQUIRED", "CANCELLED", "STALE")
_PENDING_STATES = ("IN_PROGRESS", "QUEUED", "PENDING")


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"record_routing retire_baseline_block route_failure"
),
"sandbox_executor.flow.review.ci_inspection": "CiInspection CiInspectionInputs inspect_ci",
"sandbox_executor.flow.review.ci_noise_rerun": "rerun_noise_once",
"sandbox_executor.flow.review.ci_noise_rerun": "rerun_noise_once defect_check_names",
"sandbox_executor.flow.review.ci_outcome": "describe_ci_outcome gate_ci_flags",
"sandbox_executor.flow.review.cli": "run_review_loop_cli",
"sandbox_executor.flow.review.consensus_withdrawal": (
Expand All @@ -52,7 +52,9 @@
"_ENGINE_PASSES_PER_DISPATCH _dispatch_review_ensemble _ensemble_iteration _resolve_reviewed_target "
"_review_base_ref _semantic_pr_text"
),
"sandbox_executor.flow.review.fixed_run_failures": "fixed_run_handover mark_handed_over settle_fixed_run",
"sandbox_executor.flow.review.fixed_run_failures": (
"fixed_run_handover gh_failed_log_lookup mark_handed_over settle_fixed_run"
),
"sandbox_executor.flow.review.git_identity": "REVIEW_GIT_ENV",
"sandbox_executor.flow.review.ids": "build_review_id",
"sandbox_executor.flow.review.local_test_finding": "parked_record_from_ci_status",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class CiInspectionInputs:
ci_wait: CiWaitResult | None = None
detail_lookup: DetailLookup | None = None
rerun_failed: Callable[[str], bool] | None = None
log_lookup: Callable[[dict[str, Any]], str | None] | None = None


class CiInspection(NamedTuple):
Expand Down
179 changes: 147 additions & 32 deletions apps/sandbox-executor/src/sandbox_executor/flow/review/ci_noise_rerun.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,91 @@
from __future__ import annotations

from collections.abc import Sequence
from dataclasses import dataclass
from typing import Any

from sandbox_executor.flow.context import FlowContext
from sandbox_executor.flow.github.ci import resolve_ci_registration_timeout
from sandbox_executor.flow.github.ci_failure_detail import gh_detail_lookup, short_sha
from sandbox_executor.flow.github.ci_failure_detail import DetailLookup, gh_detail_lookup, short_sha
from sandbox_executor.flow.github.ci_head_checks import CANCELLED_CONCLUSIONS
from sandbox_executor.flow.review.ci_failure_routing import (
CLASS_LABELS,
DEFECT,
NOISE_CLASSES,
RERUN,
RESOLVE,
FailedRun,
_failed_runs_from_status,
_routing_state,
classify_ci_failure,
note_rerun,
record_routing,
retire_baseline_block,
route_failure,
)
from sandbox_executor.flow.review.ci_inspection import CiInspection, CiInspectionInputs, inspect_ci
from sandbox_executor.flow.review.ci_timing_finding import MAX_JOBS_DESCRIBED
from sandbox_executor.flow.review.main_baseline_guard import BaselineSeams, inherited_failure, record_baseline_block

RERUN_START_POLL_SECONDS = 5.0
NOISE_RERUN_KEY = "noise_rerun"
ClassifiedRun = tuple[FailedRun, str]


@dataclass(frozen=True)
class _Routing:
head: str
cls: str
runs: list[FailedRun]
labels: str
baseline: BaselineSeams


def _classes_by_run(runs: Sequence[FailedRun], lookup: DetailLookup) -> list[ClassifiedRun]:
# why: fail safe as classify_ci_failure is: too many or unidentified runs are never assumed noise
if len(runs) > MAX_JOBS_DESCRIBED or any(run.run_id is None for run in runs):
return [(run, DEFECT) for run in runs]
return [(run, classify_ci_failure([run], lookup)) for run in runs]


def _noise_runs(classes: Sequence[ClassifiedRun]) -> list[ClassifiedRun]:
# why: rerunning the failed jobs of a run would also rerun a defect job in it and hide the defect
defect_run_ids = {run.run_id for run, cls in classes if cls not in NOISE_CLASSES}
return [(run, cls) for run, cls in classes if cls in NOISE_CLASSES and run.run_id not in defect_run_ids]


def _noise_routing_class(classes: Sequence[ClassifiedRun]) -> str:
kinds = {cls for _run, cls in classes}
return min(kinds) if kinds and kinds <= NOISE_CLASSES else DEFECT


def _class_labels(classes: Sequence[ClassifiedRun]) -> str:
return ", ".join(CLASS_LABELS[cls] for cls in sorted({cls for _run, cls in classes}) if cls in CLASS_LABELS)


def _note_noise_rerun(context: FlowContext, head: str, runs: Sequence[FailedRun]) -> None:
names = list(dict.fromkeys(run.name for run in runs if run.name))
_routing_state(context)[NOISE_RERUN_KEY] = {"head_sha": head, "names": names}


def _recorded_noise_rerun(context: FlowContext) -> dict[str, Any] | None:
state = context.review_state.get("ci_routing")
recorded = state.get(NOISE_RERUN_KEY) if isinstance(state, dict) else None
return recorded if isinstance(recorded, dict) else None


def _clear_noise_rerun(context: FlowContext) -> None:
if _recorded_noise_rerun(context) is not None:
del context.review_state["ci_routing"][NOISE_RERUN_KEY]


def defect_check_names(context: FlowContext, head: str | None, names: Sequence[str]) -> list[str]:
recorded = _recorded_noise_rerun(context)
if recorded is None or not head or recorded.get("head_sha") != head:
return list(names)
rerun = recorded.get("names")
rerun_names = {str(name) for name in rerun} if isinstance(rerun, list) else set()
return [name for name in names if name not in rerun_names]


def _eligible(inputs: CiInspectionInputs, ci: CiInspection, head: str | None) -> bool:
Expand Down Expand Up @@ -90,32 +154,68 @@ def _without_inherited_finding(
return ci._replace(ci_finding=False)


def rerun_noise_once(
context: FlowContext,
inputs: CiInspectionInputs,
ci: CiInspection,
head: str | None,
*,
baseline: BaselineSeams | None = None,
) -> CiInspection:
runs = _failed_runs_from_status(ci.ci_status) if head and _eligible(inputs, ci, head) else []
if not head or not runs:
retire_baseline_block(context)
return ci
baseline = baseline or BaselineSeams(proxy_env=inputs.proxy_env)
if inputs.reviewer_objected:
retire_baseline_block(context)
return _without_inherited_finding(context, ci, head, runs, baseline)
cls = classify_ci_failure(runs, inputs.detail_lookup or gh_detail_lookup(inputs.repo_dir, inputs.proxy_env))
def _record_beside_defects(context: FlowContext, head: str, cls: str, runs: Sequence[FailedRun]) -> None:
# why: the defect's routing stays the last one, so the gate and the resolver still judge the defect
state = _routing_state(context)
prior = state.get("last")
record_routing(context, head, cls, RERUN, runs)
if prior is None:
del state["last"]
else:
state["last"] = prior


def _rerun_noise_beside_defects(
context: FlowContext, inputs: CiInspectionInputs, head: str, noise: Sequence[ClassifiedRun]
) -> bool:
runs, cls = [run for run, _cls in noise], _noise_routing_class(noise)
if route_failure(context, head, cls, runs) != RERUN:
return False
ok, dispatched = _dispatch_all(inputs, runs)
if not ok:
note_rerun(context, head, dispatched)
context.log(f"[Review Loop] CI noise rerun on {short_sha(head)} could not be dispatched; resolving.")
return False
note_rerun(context, head, dispatched)
_record_beside_defects(context, head, cls, runs)
_note_noise_rerun(context, head, runs)
context.log(
f"[Review Loop] CI noise ({_class_labels(noise)}) on {short_sha(head)}: rerunning run "
f"{', '.join(dispatched)} once; only the defect checks go to the resolver."
)
return True


def _inspect_rerun(context: FlowContext, inputs: CiInspectionInputs, routing: _Routing) -> CiInspection:
head, runs = routing.head, routing.runs
_wait_for_rerun_start(inputs, {run.name for run in runs})
after = inspect_ci(inputs, context)
still_red = after.ci_green is False
if still_red:
_clear_noise_rerun(context)
red_runs = _failed_runs_from_status(after.ci_status) or runs
blocked = _baseline_block(context, after, head, red_runs, routing.baseline) if still_red else None
if blocked is not None:
return blocked
if still_red:
# why: the ledger records the hand-off to the resolver, as Stage 5 does
record_routing(context, head, routing.cls, RESOLVE, red_runs)
context.log(f"[Review Loop] CI noise on {short_sha(head)} still red after its rerun: sent to the resolver.")
return after


def _route(context: FlowContext, inputs: CiInspectionInputs, ci: CiInspection, routing: _Routing) -> CiInspection:
head, cls, runs = routing.head, routing.cls, routing.runs
decision, dispatched = route_failure(context, head, cls, runs), []
if decision == RERUN:
ok, dispatched = _dispatch_all(inputs, runs)
if ok:
note_rerun(context, head, dispatched)
_note_noise_rerun(context, head, runs)
else:
context.log(f"[Review Loop] CI noise rerun on {short_sha(head)} could not be dispatched; resolving.")
decision = RESOLVE
blocked = None if decision == RERUN else _baseline_block(context, ci, head, runs, baseline)
blocked = None if decision == RERUN else _baseline_block(context, ci, head, runs, routing.baseline)
if blocked is not None:
note_rerun(context, head, dispatched)
return blocked
Expand All @@ -126,18 +226,33 @@ def rerun_noise_once(
context.log(f"[Review Loop] CI failure on {short_sha(head)} classified {cls}: sent to the resolver.")
return ci
context.log(
f"[Review Loop] CI noise ({CLASS_LABELS[cls]}) on {short_sha(head)}: rerunning failed jobs of run "
f"[Review Loop] CI noise ({routing.labels}) on {short_sha(head)}: rerunning failed jobs of run "
f"{', '.join(last['run_ids'])} once; the diff approval stands."
)
_wait_for_rerun_start(inputs, {run.name for run in runs})
after = inspect_ci(inputs, context)
still_red = after.ci_green is False
red_runs = _failed_runs_from_status(after.ci_status) or runs
blocked = _baseline_block(context, after, head, red_runs, baseline) if still_red else None
if blocked is not None:
return blocked
if still_red:
# why: the ledger records the hand-off to the resolver, as Stage 5 does
record_routing(context, head, cls, RESOLVE, _failed_runs_from_status(after.ci_status) or runs)
context.log(f"[Review Loop] CI noise on {short_sha(head)} still red after its rerun: sent to the resolver.")
return after
return _inspect_rerun(context, inputs, routing)


def rerun_noise_once(
context: FlowContext,
inputs: CiInspectionInputs,
ci: CiInspection,
head: str | None,
*,
baseline: BaselineSeams | None = None,
) -> CiInspection:
_clear_noise_rerun(context)
runs = _failed_runs_from_status(ci.ci_status) if head and _eligible(inputs, ci, head) else []
if not head or not runs:
retire_baseline_block(context)
return ci
baseline = baseline or BaselineSeams(proxy_env=inputs.proxy_env)
if inputs.reviewer_objected:
retire_baseline_block(context)
return _without_inherited_finding(context, ci, head, runs, baseline)
classes = _classes_by_run(runs, inputs.detail_lookup or gh_detail_lookup(inputs.repo_dir, inputs.proxy_env))
noise = _noise_runs(classes)
if noise and len(noise) < len(classes) and _rerun_noise_beside_defects(context, inputs, head, noise):
classes = [entry for entry in classes if entry not in noise]
cls = _noise_routing_class(classes)
routing = _Routing(head, cls, [run for run, _cls in classes], _class_labels(classes), baseline)
return _route(context, inputs, ci, routing)
Loading
Loading