ariadne/ariadne/services/hermes_hung_builds.py
codex 1b29b56f50 feat(hermes): escalate builds that never reach a terminal result
Detection only ever considered builds that finished, so a build wedged on a
network call was invisible: no incident, no issue, no alert, while it held one
of the five Jenkins agent slots. Observed on metis build 272, which sat on
apt-get update for 75 minutes and starved the triage demo of an agent with
nothing anywhere saying so.

A build still running past ARIADNE_HERMES_HUNG_BUILD_MINUTES (default 45) now
files an issue and lands human_required. No model is consulted: the console is
still being written, so a root cause would be invented. Escalates once per
build, and a cap of zero disables the check.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 05:13:11 -03:00

86 lines
3.3 KiB
Python

"""Escalate Jenkins builds that never reach a terminal result.
Detection elsewhere in auto-triage only considers builds that finished, so a
build wedged on a network call is invisible: it produces no incident, no
issue, and no alert, while holding one of the finite Jenkins agent slots.
Observed on metis build 272, which sat on `apt-get update` for 75 minutes and
starved the demo of an agent without anything anywhere saying so.
No model call is made here. "This build has been running far longer than the
cap" is a fact Ariadne can establish on its own, and inventing a root cause
for a build whose console is still being written would be guesswork.
"""
from __future__ import annotations
import time
from typing import Any
HUNG_CLASSIFICATION = "build_exceeded_time_cap"
HUNG_REASON = "hung_build"
_MS_PER_SECOND = 1000.0
_SECONDS_PER_MINUTE = 60.0
def elapsed_minutes(last_build: dict[str, Any], now: float | None = None) -> float:
"""Return how long a build has been running, in minutes.
Inputs: a Jenkins lastBuild summary carrying `timestamp` in epoch
milliseconds, and an optional current epoch time in seconds for testing.
Outputs: elapsed minutes, or 0.0 when the timestamp is missing or unusable
so an unreadable build is never mistaken for a hung one.
"""
try:
started = float(last_build.get("timestamp") or 0.0) / _MS_PER_SECOND
except (TypeError, ValueError):
return 0.0
if started <= 0.0:
return 0.0
current = time.time() if now is None else now
return max(0.0, (current - started) / _SECONDS_PER_MINUTE)
def is_hung(last_build: dict[str, Any], cap_minutes: float, now: float | None = None) -> bool:
"""Report whether a running build has exceeded the configured cap.
Inputs: the lastBuild summary, the cap in minutes, and an optional clock.
Outputs: True only when the build is still running and has been running
longer than the cap. A cap of zero or less disables the check entirely.
"""
if cap_minutes <= 0 or not last_build.get("building"):
return False
return elapsed_minutes(last_build, now) > cap_minutes
def hung_bundle(job: str, last_build: dict[str, Any], cap_minutes: float) -> dict[str, Any]:
"""Build the evidence bundle describing a hung build.
Inputs: the job name, its lastBuild summary, and the configured cap.
Outputs: a bundle shaped like the console-evidence bundles the issue
renderer already accepts, carrying only facts Ariadne observed directly.
"""
minutes = elapsed_minutes(last_build)
number = last_build.get("number")
return {
"jenkins": {
"job": job,
"build_number": number,
"url": str(last_build.get("url") or ""),
"building": True,
"elapsed_minutes": round(minutes, 1),
"cap_minutes": cap_minutes,
"console_failures": [],
"console_tail": (
f"Build {job} #{number} has been running for {minutes:.1f} minutes, "
f"past the {cap_minutes:.0f} minute cap, and has not reported a result. "
"Ariadne did not diagnose a cause: the build is still running and its "
"console is incomplete. It is also still holding a Jenkins agent slot."
),
},
"log_evidence": {"records": []},
}