Skip to content

Commit 20b9c31

Browse files
committed
Fail-fast simplifications in build-prompt.py and build-pr-body.py
1 parent 5921bf5 commit 20b9c31

4 files changed

Lines changed: 41 additions & 89 deletions

File tree

.github/scripts/flaky-test-fix/build-pr-body.py

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22
"""Render the PR body for an automated flaky-test fix.
33
44
Inputs:
5-
--selected selected.json from find-flaky-test.py (or build-override.py)
6-
--copilot-log copilot-output.jsonl produced by `copilot ... --output-format json`
7-
(optional)
5+
--selected selected.json from find-flaky-test.py
86
--diagnosis build/flaky-fix/diagnosis.md written by Copilot (optional)
97
--output path to write the PR body to
108
119
The body always includes Develocity scan links, a per-day flake breakdown,
1210
and the captured failure stack. If a Copilot diagnosis file is present, it
13-
is included verbatim. Otherwise the script falls back to extracting the
14-
final assistant message from the Copilot JSONL log.
11+
is included verbatim.
1512
"""
1613

1714
from __future__ import annotations
@@ -23,26 +20,6 @@
2320
from pathlib import Path
2421

2522

26-
def _last_assistant_message(jsonl_path: Path) -> str:
27-
"""Return the last non-empty content of an ``assistant.message`` event."""
28-
last = ""
29-
with jsonl_path.open("r", encoding="utf-8", errors="replace") as f:
30-
for line in f:
31-
line = line.strip()
32-
if not line:
33-
continue
34-
try:
35-
d = json.loads(line)
36-
except json.JSONDecodeError:
37-
continue
38-
if d.get("type") != "assistant.message":
39-
continue
40-
content = (d.get("data") or {}).get("content") or ""
41-
if isinstance(content, str) and content.strip():
42-
last = content
43-
return last.strip()
44-
45-
4623
def _format_per_day(rows: list) -> list[str]:
4724
if not rows:
4825
return []
@@ -51,11 +28,10 @@ def _format_per_day(rows: list) -> list[str]:
5128
"| Day | flaky | failed | passed |",
5229
"| --- | ---: | ---: | ---: |"]
5330
for r in rows:
54-
day = dt.datetime.fromtimestamp((r.get("start_ms") or 0) / 1000,
55-
tz=dt.timezone.utc)
31+
day = dt.datetime.fromtimestamp(r["start_ms"] / 1000, tz=dt.timezone.utc)
5632
out.append(
5733
f"| {day.strftime('%Y-%m-%d')} | "
58-
f"{r.get('flaky', 0)} | {r.get('failed', 0)} | {r.get('passed', 0)} |"
34+
f"{r['flaky']} | {r['failed']} | {r['passed']} |"
5935
)
6036
out.append("")
6137
return out
@@ -66,12 +42,9 @@ def _format_recent_scans(scans: list) -> list[str]:
6642
return []
6743
out = ["### Recent failed/flaky scans", ""]
6844
for s in scans[:5]:
69-
url = s.get("scan_url", "")
70-
outcome = s.get("outcome", "")
71-
wu = s.get("work_unit", "")
72-
bullet = f"- [{s.get('build_id', '')[:13]}]({url}) ({outcome}"
73-
if wu:
74-
bullet += f", `{wu}`"
45+
bullet = f"- [{s['build_id'][:13]}]({s['scan_url']}) ({s['outcome']}"
46+
if s["work_unit"]:
47+
bullet += f", `{s['work_unit']}`"
7548
bullet += ")"
7649
out.append(bullet)
7750
out.append("")
@@ -81,56 +54,45 @@ def _format_recent_scans(scans: list) -> list[str]:
8154
def main() -> int:
8255
ap = argparse.ArgumentParser()
8356
ap.add_argument("--selected", type=Path, required=True)
84-
ap.add_argument("--copilot-log", type=Path, default=None)
8557
ap.add_argument("--diagnosis", type=Path, default=None)
8658
ap.add_argument("--output", type=Path, required=True)
8759
args = ap.parse_args()
8860

8961
selected = json.loads(args.selected.read_text(encoding="utf-8"))
90-
cls = selected.get("class", "")
91-
method = selected.get("method", "")
92-
fq = selected.get("fully_qualified") or f"{cls}.{method}"
93-
source_file = selected.get("source_file", "")
94-
flaky_count = selected.get("flaky_count", 0)
95-
container_flaky = selected.get("container_flaky_count", 0)
96-
window_days = selected.get("window_days", 0)
97-
sample_url = selected.get("sample_scan_url") or ""
98-
sample_failure = (selected.get("sample_failure") or "").rstrip()
62+
fq = selected["fully_qualified"]
63+
source_file = selected["source_file"]
64+
window_days = selected["window_days"]
65+
sample_url = selected["sample_scan_url"]
66+
sample_failure = selected["sample_failure"].rstrip()
9967

10068
lines: list[str] = [
10169
f"Automated attempt at fixing flakiness in `{fq}`.",
10270
"",
10371
f"- Source: [`{source_file}`]({source_file})",
104-
f"- Flaky executions in last {window_days}d (this test): **{flaky_count}**",
72+
f"- Flaky executions in last {window_days}d (this test): "
73+
f"**{selected['flaky_count']}**",
10574
f"- Flaky executions in last {window_days}d (test container): "
106-
f"**{container_flaky}**",
75+
f"**{selected['container_flaky_count']}**",
10776
]
10877
if sample_url:
10978
lines.append(f"- Primary failed scan: {sample_url}")
11079
lines.append("")
11180

112-
lines += _format_recent_scans(selected.get("recent_flaky_scans") or [])
113-
lines += _format_per_day(selected.get("per_day_breakdown") or [])
81+
lines += _format_recent_scans(selected["recent_flaky_scans"])
82+
lines += _format_per_day(selected["per_day_breakdown"])
11483

11584
lines += ["### Sample failure (from Develocity)", "", "```"]
116-
lines.append(sample_failure if sample_failure else "(no failure message captured)")
85+
lines.append(sample_failure or "(no failure message captured)")
11786
lines += ["```", ""]
11887

119-
diagnosis_text = ""
12088
if args.diagnosis and args.diagnosis.exists():
121-
diagnosis_text = args.diagnosis.read_text(
122-
encoding="utf-8", errors="replace"
123-
).strip()
124-
if not diagnosis_text and args.copilot_log and args.copilot_log.exists():
125-
diagnosis_text = _last_assistant_message(args.copilot_log)
126-
127-
if diagnosis_text:
128-
lines += ["## Copilot diagnosis", "", diagnosis_text, ""]
89+
diagnosis_text = args.diagnosis.read_text(encoding="utf-8").strip()
90+
if diagnosis_text:
91+
lines += ["## Copilot diagnosis", "", diagnosis_text, ""]
12992

13093
lines += [
13194
"---",
13295
"",
133-
"Generated locally by `.github/scripts/flaky-test-fix/run-local.sh`. "
13496
"Review the diagnosis and the diff carefully before merging - "
13597
"automated fixes can mask flakiness instead of addressing the root cause.",
13698
]

.github/scripts/flaky-test-fix/build-prompt.py

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#!/usr/bin/env python3
22
"""Render the Copilot CLI prompt for fixing one flaky test.
33
4-
Inputs the `selected.json` produced by `find-flaky-test.py` (or
5-
`build-override.py`) and writes a plain-text prompt suitable for `copilot -p`.
4+
Inputs the `selected.json` produced by `find-flaky-test.py` and writes a
5+
plain-text prompt suitable for `copilot -p`.
66
"""
77

88
from __future__ import annotations
99

1010
import argparse
11+
import datetime as dt
1112
import json
1213
import sys
1314
from pathlib import Path
@@ -73,18 +74,13 @@ def _format_recent_scans(scans: list) -> str:
7374
return ""
7475
lines = ["Other recent flaky/failed scans for this test:"]
7576
for s in scans[:5]:
76-
bid = s.get("build_id", "")
77-
url = s.get("scan_url", "")
78-
outcome = s.get("outcome", "")
79-
wu = s.get("work_unit", "")
80-
excerpt = (s.get("failure_excerpt") or "").splitlines()
81-
first = excerpt[0] if excerpt else ""
82-
bullet = f"- {url} ({outcome}"
83-
if wu:
84-
bullet += f", {wu}"
77+
bullet = f"- {s['scan_url']} ({s['outcome']}"
78+
if s["work_unit"]:
79+
bullet += f", {s['work_unit']}"
8580
bullet += ")"
86-
if first:
87-
bullet += f"\n first line: `{first[:160]}`"
81+
excerpt = s["failure_excerpt"].splitlines()
82+
if excerpt:
83+
bullet += f"\n first line: `{excerpt[0][:160]}`"
8884
lines.append(bullet)
8985
lines.append("")
9086
return "\n".join(lines) + "\n"
@@ -93,16 +89,14 @@ def _format_recent_scans(scans: list) -> str:
9389
def _format_per_day(rows: list) -> str:
9490
if not rows:
9591
return ""
96-
import datetime as _dt
9792
lines = ["Per-day outcome breakdown for this test:", "",
9893
"| Day (UTC) | flaky | failed | passed |",
9994
"| --- | ---: | ---: | ---: |"]
10095
for r in rows:
101-
day = _dt.datetime.fromtimestamp((r.get("start_ms") or 0) / 1000,
102-
tz=_dt.timezone.utc)
96+
day = dt.datetime.fromtimestamp(r["start_ms"] / 1000, tz=dt.timezone.utc)
10397
lines.append(
10498
f"| {day.strftime('%Y-%m-%d')} | "
105-
f"{r.get('flaky', 0)} | {r.get('failed', 0)} | {r.get('passed', 0)} |"
99+
f"{r['flaky']} | {r['failed']} | {r['passed']} |"
106100
)
107101
lines.append("")
108102
return "\n".join(lines) + "\n"
@@ -115,23 +109,21 @@ def main() -> int:
115109
args = ap.parse_args()
116110

117111
selected = json.loads(args.selected.read_text(encoding="utf-8"))
118-
failure = selected.get("sample_failure") or "(no failure message captured)"
112+
failure = (selected["sample_failure"] or "(no failure message captured)").strip()
119113
# Trim aggressively to keep the prompt within token limits.
120-
failure = failure.strip()
121114
if len(failure) > 4000:
122115
failure = failure[:4000] + "\n... [truncated]"
123116

124117
prompt = PROMPT_TEMPLATE.format(
125-
fq=selected.get("fully_qualified")
126-
or f"{selected.get('class', '')}.{selected.get('method', '')}",
127-
source=selected.get("source_file", ""),
128-
window_days=selected.get("window_days", 0),
129-
flaky_count=selected.get("flaky_count", 0),
130-
container_flaky_count=selected.get("container_flaky_count", 0),
131-
scan_url=selected.get("sample_scan_url") or "(none)",
118+
fq=selected["fully_qualified"],
119+
source=selected["source_file"],
120+
window_days=selected["window_days"],
121+
flaky_count=selected["flaky_count"],
122+
container_flaky_count=selected["container_flaky_count"],
123+
scan_url=selected["sample_scan_url"] or "(none)",
132124
failure=failure,
133-
recent_section=_format_recent_scans(selected.get("recent_flaky_scans") or []),
134-
per_day_section=_format_per_day(selected.get("per_day_breakdown") or []),
125+
recent_section=_format_recent_scans(selected["recent_flaky_scans"]),
126+
per_day_section=_format_per_day(selected["per_day_breakdown"]),
135127
)
136128

137129
args.output.parent.mkdir(parents=True, exist_ok=True)

.github/scripts/flaky-test-fix/run-local.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ git push -u "$REMOTE" "$BRANCH"
226226
PR_BODY="$OUT_DIR/pr-body.md"
227227
python .github/scripts/flaky-test-fix/build-pr-body.py \
228228
--selected "$SELECTED" \
229-
--copilot-log "$COPILOT_LOG" \
230229
--diagnosis "$DIAGNOSIS" \
231230
--output "$PR_BODY"
232231

.github/workflows/flaky-test-fix.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ jobs:
220220
mkdir -p "$(dirname "$body_file")"
221221
python .github/scripts/flaky-test-fix/build-pr-body.py \
222222
--selected "$COPILOT_ROOT/selected.json" \
223-
--copilot-log "$COPILOT_ROOT/run/copilot-output.jsonl" \
224223
--diagnosis build/flaky-fix/diagnosis.md \
225224
--output "$body_file"
226225
if [[ -n "${ARTIFACT_URL:-}" ]]; then

0 commit comments

Comments
 (0)