'
r'.*?',
re.DOTALL,
)
_details_re = re.compile(
r'(]*data-(?:prow-url|sippy-key)="([^"]*)"[^>]*>)\s*'
r'(.*?)'
r'(
)',
re.DOTALL,
)
patched_urls: set[str] = set()
patched_sippy_keys: set[str] = set()
def _replace_suggestion(m: re.Match) -> str:
nonlocal patched
prow_url = html_mod.unescape(m.group(1))
da = by_url.get(prow_url)
if da is None:
return m.group(0)
attempt_count = int(m.group(2))
patched += 1
patched_urls.add(prow_url)
return _render_analysis_card(da, attempt_count=attempt_count, job_name=da.get("job_name", ""))
content = _suggestion_re.sub(_replace_suggestion, content)
def _replace_sippy(m: re.Match) -> str:
nonlocal patched
key = html_mod.unescape(m.group(2))
da = by_sippy_job.get(key)
if da is None:
return m.group(0)
patched += 1
patched_sippy_keys.add(key)
card = _render_analysis_card(da, job_name=da.get("job_name", ""))
return f'{m.group(1)}{card}
'
content = re.sub(
r'(', '
', 1)
return row.removesuffix('
') + ''
content = re.sub(
r'(
]*data-sippy-key="([^"]*)"[^>]*>)'
r'.*?
',
_enable_sippy_row, content, flags=re.DOTALL,
)
def _insert_badge(m: re.Match) -> str:
if html_mod.unescape(m.group(2)) not in patched_urls | patched_sippy_keys:
return m.group(0)
def _mark_analyzed(status: re.Match) -> str:
values = status.group(1).split()
if "ai-analyzed" not in values:
values.append("ai-analyzed")
return f'data-status="{" ".join(values)}"'
details_tag = re.sub(r'data-status="([^"]*)"', _mark_analyzed, m.group(1))
details_tag = re.sub(r' hidden>$', '>', details_tag)
summary_content = m.group(3)
if "ai-analyzed" in summary_content:
return f'{details_tag}\n
{summary_content}{m.group(4)}'
sippy_name_end = ''
if sippy_name_end in summary_content:
summary_content = summary_content.replace(sippy_name_end, sippy_name_end + badge_html, 1)
return f'{details_tag}\n {summary_content}{m.group(4)}'
# Grid layout: insert badge inside the job cell before its closing
job_anchor = 'class="detail-cell-job"'
job_pos = summary_content.find(job_anchor)
if job_pos >= 0:
open_gt = summary_content.find('>', job_pos)
if open_gt >= 0:
close_pos = summary_content.find('', open_gt)
if close_pos >= 0:
summary_content = (
summary_content[:close_pos]
+ badge_html
+ summary_content[close_pos:]
)
return f'{details_tag}\n {summary_content}{m.group(4)}'
# Legacy flat layout fallback
for anchor in ('class="detail-date"', 'class="detail-version"'):
anchor_pos = summary_content.find(anchor)
if anchor_pos < 0:
continue
span_start = summary_content.rfind('', 0, span_start)
if last_close >= 0:
ins = last_close + 1
while ins < span_start and summary_content[ins] in ' \t\n':
ins += 1
summary_content = (
summary_content[:ins]
+ badge_html + '\n '
+ summary_content[ins:]
)
else:
summary_content = badge_html + ' ' + summary_content
break
else:
summary_content = summary_content + ' ' + badge_html
return f'{details_tag}\n {summary_content}{m.group(4)}'
content = _details_re.sub(_insert_badge, content)
# Update header: replace "Data only" status using data attribute
if patched > 0:
new_status = (
''
'● AI-enriched via Claude'
'
'
)
content = re.sub(
r']*data-enrichment-status="data-only"[^>]*>.*?
',
new_status,
content,
count=1,
flags=re.DOTALL,
)
html_path.write_text(content)
logger.info(f"Patched {patched} analysis card(s) into {html_path}")
def merge_analysis(report: MonitorReport, analysis_path: Path) -> None:
"""Merge a small analysis-only JSON into a MonitorReport.
Blocking results use ``by_prow_url``. Sippy job-health results use
``by_sippy_job`` keyed by Regression.analysis_key, with the same card fields:
{
"by_prow_url": {
"https://prow.ci.../123": {
"root_cause": "...",
"failure_type": "...",
"impact": "...",
"suspect_prs": [],
"recommendation": "..."
}
}
}
"""
data = _load_json(analysis_path, "Analysis JSON")
by_url = data.get("by_prow_url", {})
by_sippy_job = data.get("by_sippy_job", {})
merged = 0
for stream in report.streams:
for payload in stream.payloads:
for job in payload.jobs:
if job.prow_url in by_url:
job.deep_analysis = _build_deep_analysis(by_url[job.prow_url])
merged += 1
for regression in sippy_findings(report):
if regression.analysis_key in by_sippy_job:
regression.deep_analysis = _build_deep_analysis(by_sippy_job[regression.analysis_key])
merged += 1
logger.info(f"Merged deep analysis for {merged} job(s)")
def _build_deep_analysis(da_raw: dict) -> DeepAnalysis:
"""Build a DeepAnalysis from a raw dict, sanitizing URLs."""
return DeepAnalysis(
root_cause=da_raw.get("root_cause", ""),
failure_type=da_raw.get("failure_type", ""),
impact=da_raw.get("impact", ""),
suspect_prs=_safe_urls(da_raw.get("suspect_prs", [])),
recommendation=da_raw.get("recommendation", ""),
same_root_cause=da_raw.get("same_root_cause", True),
attempt_analyses=[
AttemptAnalysis(
prow_url=aa["prow_url"],
root_cause=aa.get("root_cause", ""),
failure_type=aa.get("failure_type", ""),
)
for aa in da_raw.get("attempt_analyses", [])
if aa.get("prow_url")
],
)
def _safe_dataclass_init(cls, data: dict):
"""Create a dataclass instance, ignoring unknown keys for forward compatibility."""
field_names = {f.name for f in dataclasses.fields(cls)}
filtered = {k: v for k, v in data.items() if k in field_names}
return cls(**filtered)
def load_json(json_path: Path) -> MonitorReport:
"""Load a MonitorReport from a JSON file (potentially enriched with deep analysis)."""
data = _load_json(json_path, "Report JSON")
streams = []
for s in data.get("streams", []):
payloads = []
for p in s.get("payloads", []):
jobs = []
for j in p.get("edge_jobs", []):
da_raw = j.get("deep_analysis")
deep_analysis = None
if da_raw:
deep_analysis = _build_deep_analysis(da_raw)
failing_tests = [
FailingTest(name=t.get("name", ""), error_message=t.get("error", ""))
for t in j.get("failing_tests", [])
]
prev_attempts = [
PreviousAttempt(
prow_url=pa.get("prow_url", ""),
result=JobResult(pa.get("result", "F")),
failing_tests=[
FailingTest(name=t.get("name", ""), error_message=t.get("error", ""))
for t in pa.get("failing_tests", [])
],
error_summary=pa.get("error_summary", ""),
)
for pa in j.get("previous_attempts", [])
]
jobs.append(JobRun(
name=j.get("name", ""),
prow_url=j.get("prow_url", ""),
result=JobResult(j.get("result", "U")),
job_type=JobType(j.get("job_type", "informing")),
topology=j.get("topology"),
failing_tests=failing_tests,
error_summary=j.get("error_summary", ""),
deep_analysis=deep_analysis,
retries=j.get("retries", 0),
previous_attempts=prev_attempts,
))
payloads.append(Payload(
tag=p.get("tag", ""),
stream=s.get("stream", ""),
version=s.get("version", ""),
status=PayloadStatus(p.get("status", "Pending")),
url=p.get("url", ""),
jobs=jobs,
))
regressions = []
for r in s.get("regressions", []):
regressions.append(Regression(
test_name=r.get("test_name", ""),
test_id=r.get("test_id", ""),
component=r.get("component", ""),
capability=r.get("capability", ""),
basis_pass_rate=r.get("basis_pass_rate", 0),
sample_pass_rate=r.get("sample_pass_rate", 0),
version=r.get("version", s.get("version", "")),
topology=r.get("topology", ""),
triage_url=r.get("triage_url", ""),
jira_bug=r.get("jira_bug", ""),
current_runs=r.get("current_runs", 0),
deep_analysis=_build_deep_analysis(r["deep_analysis"]) if r.get("deep_analysis") else None,
latest_failure_ms=r.get("latest_failure_ms", 0),
))
streams.append(StreamReport(
stream=s.get("stream", ""),
version=s.get("version", ""),
payloads=payloads,
regressions=regressions,
))
suggested_bugs = [
_safe_dataclass_init(SuggestedBug, b) for b in data.get("suggested_bugs", [])
]
comp_regressions = [
_safe_dataclass_init(ComponentRegression, cr) for cr in data.get("component_regressions", [])
]
# Deserialize escalation_risks
escalation_risks = [
_safe_dataclass_init(EscalationRisk, er) for er in data.get("escalation_risks", [])
]
return MonitorReport(
generated_at=data.get("generated_at", ""),
streams=streams,
suggested_bugs=suggested_bugs,
component_regressions=comp_regressions,
failure_counts=data.get("failure_counts", {}),
escalation_risks=escalation_risks,
cross_topology=data.get("cross_topology", {}),
recurring_threshold=data.get("recurring_threshold", 2),
persistent_threshold=data.get("persistent_threshold", 3),
)