Skip to content
Closed
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
255 changes: 255 additions & 0 deletions .github/scripts/refresh_flake_baseline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
#!/usr/bin/env python3
"""Maintain `flake-baseline.json`: what `main` itself does not reliably pass.

The merge queue has no way to tell "this PR broke it" from "main was already
broken", because every merge_group run tests `main` + up to three PRs and
nothing tests `main` on its own. This baseline is that missing statement.

Input is one outcome ledger from `classify_playwright_outcome.py`, produced by
the main lane (`playwright-main-lane.yml`). Each run contributes one
observation per test that failed its FIRST attempt — whether a retry rescued
it (`retryPassStableIds`) or not (`failureStableIds`). A test enters the
baseline only after `--threshold` observations inside a `--window` of runs, so
one unlucky run cannot excuse a test, and an entry ages out on its own once
`main` starts passing it again.

`stableIds` is the flat list `classify_playwright_outcome.py --retry-baseline`
reads, so the output is consumable without a translation step.

DELIBERATELY NOT A COMMITTED FILE. `refresh-timing-baseline` commits to `main`
and notes why that has to stay rare: every push to `main` resets the merge
queue. A baseline that refreshes on every main run cannot pay that cost, so it
lives in a `refs/heads/main`-scoped Actions cache instead — the one scope
merge_group and PR runs can restore from (#32605). A cache miss degrades to
today's behaviour: no baseline, no differential signal, nothing breaks.

Exits:
0 — baseline written (or unchanged)
1 — invalid input
3 — the ledger carries no usable observations (cancelled run, no results);
the caller skips the save rather than recording a false all-green
"""

from __future__ import annotations

import argparse
import json
from datetime import datetime, timezone
from pathlib import Path
from typing import Any

SCHEMA_VERSION = 1

# A run that never produced results tells us nothing about main. Recording it
# as "no test failed its first attempt" would silently decay real entries out
# of the window, which is exactly how a mask gets built.
UNUSABLE_CLASSIFICATIONS = frozenset({"cancelled", "missing_results"})


def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--ledger", type=Path, required=True,
help="outcome.json from classify_playwright_outcome.py")
parser.add_argument("--current", type=Path,
help="Existing baseline; absent or unreadable starts a "
"fresh window.")
parser.add_argument("--output", type=Path, required=True)
parser.add_argument("--window", type=int, default=10,
help="How many main-lane runs the baseline remembers.")
parser.add_argument("--threshold", type=int, default=2,
help="First-attempt failures inside the window before a "
"test counts as 'main does not pass this'.")
parser.add_argument("--run-id", default="")
parser.add_argument("--source-sha", default="")
parser.add_argument("--summary", type=Path,
help="Write a markdown summary here (job summary / "
"Slack body).")
return parser.parse_args()


def _load_json(path: Path | None) -> dict[str, Any] | None:
if path is None or not path.is_file():
return None
try:
value = json.loads(path.read_text(encoding="utf-8"))
except (OSError, UnicodeError, json.JSONDecodeError):
return None
return value if isinstance(value, dict) else None


def _string_list(value: Any) -> list[str]:
return [str(item) for item in value] if isinstance(value, list) else []


def _record_index(records: list[dict[str, Any]]) -> dict[str, dict[str, Any]]:
"""stableId -> the ledger's own record, for project/file/title metadata."""
index: dict[str, dict[str, Any]] = {}
for record in records:
if isinstance(record, dict) and record.get("stableId"):
index.setdefault(str(record["stableId"]), record)
return index


def refresh(
ledger: dict[str, Any],
current: dict[str, Any] | None,
window: int,
threshold: int,
run_id: str,
source_sha: str,
now: str,
) -> dict[str, Any] | None:
if str(ledger.get("classification", "")) in UNUSABLE_CLASSIFICATIONS:
return None

retry_passes = _string_list(ledger.get("retryPassStableIds"))
hard_failures = _string_list(ledger.get("failureStableIds"))
# Both mean the same thing for this baseline: the first attempt did not
# pass on main. The distinction is kept in the output so a later gate can
# treat "broken on main" differently from "flaky on main".
observed = sorted(set(retry_passes) | set(hard_failures))

metadata = _record_index(
list(ledger.get("retryPasses") or []) + list(ledger.get("failures") or [])
)

previous = current if isinstance(current, dict) else {}
runs = [run for run in _string_list_of_runs(previous.get("runs"))]
run_key = run_id or source_sha or now
runs = [run for run in runs if run.get("runId") != run_key]
runs.append({"runId": run_key, "sourceSha": source_sha, "recordedAt": now})
runs = runs[-window:]
live_run_ids = {run["runId"] for run in runs}

entries: dict[str, dict[str, Any]] = {}
for stable_id, entry in (previous.get("entries") or {}).items():
if not isinstance(entry, dict):
continue
kept = [
run for run in _string_list(entry.get("observations"))
if run in live_run_ids
]
if kept:
entries[str(stable_id)] = {**entry, "observations": kept}

for stable_id in observed:
entry = entries.setdefault(
stable_id,
{"observations": [], "project": "", "file": "", "title": ""},
)
if run_key not in entry["observations"]:
entry["observations"] = [*entry["observations"], run_key]
entry["lastSeenSha"] = source_sha
entry["hardFailure"] = stable_id in set(hard_failures)
record = metadata.get(stable_id, {})
for field in ("project", "file", "title"):
if record.get(field):
entry[field] = str(record[field])

baselined = sorted(
stable_id for stable_id, entry in entries.items()
if len(entry["observations"]) >= threshold
)

return {
"schemaVersion": SCHEMA_VERSION,
"generatedAt": now,
"window": window,
"threshold": threshold,
"runs": runs,
"entries": dict(sorted(entries.items())),
# Consumed verbatim by classify_playwright_outcome.py --retry-baseline.
"stableIds": baselined,
"hardFailureStableIds": sorted(
stable_id for stable_id in baselined
if entries[stable_id].get("hardFailure")
),
}


def _string_list_of_runs(value: Any) -> list[dict[str, Any]]:
if not isinstance(value, list):
return []
return [
{
"runId": str(run.get("runId", "")),
"sourceSha": str(run.get("sourceSha", "")),
"recordedAt": str(run.get("recordedAt", "")),
}
for run in value
if isinstance(run, dict) and run.get("runId")
]


def render_summary(baseline: dict[str, Any], previous: dict[str, Any] | None) -> str:
before = set(_string_list((previous or {}).get("stableIds")))
after = set(baseline["stableIds"])
added = sorted(after - before)
removed = sorted(before - after)
entries = baseline["entries"]

lines = [
"### Playwright flake baseline (main)",
"",
f"- Window: last {len(baseline['runs'])} of {baseline['window']} main-lane runs",
f"- Tests main does not reliably pass: **{len(after)}** "
f"({len(baseline['hardFailureStableIds'])} failing outright)",
f"- Entered this run: {len(added)} · left: {len(removed)}",
]
if added:
lines += ["", "**Entered the baseline**", ""]
lines += [
f"- `{stable_id}` — {len(entries[stable_id]['observations'])} "
f"of the last {len(baseline['runs'])} runs"
for stable_id in added[:20]
]
if removed:
lines += ["", "**Left the baseline** (main passes them again)", ""]
lines += [f"- `{stable_id}`" for stable_id in removed[:20]]
return "\n".join(lines) + "\n"


def main() -> int:
args = parse_args()

ledger = _load_json(args.ledger)
if ledger is None:
print(f"Unreadable or non-object ledger: {args.ledger}")
return 1
if args.window < 1 or args.threshold < 1:
print("--window and --threshold must both be >= 1")
return 1

previous = _load_json(args.current)
now = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
baseline = refresh(
ledger=ledger,
current=previous,
window=args.window,
threshold=args.threshold,
run_id=args.run_id,
source_sha=args.source_sha,
now=now,
)
if baseline is None:
print(
"Ledger classification "
f"'{ledger.get('classification')}' carries no usable observations; "
"leaving the baseline untouched."
)
return 3

args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(
json.dumps(baseline, indent=2, sort_keys=False) + "\n", encoding="utf-8"
)
summary = render_summary(baseline, previous)
if args.summary:
args.summary.parent.mkdir(parents=True, exist_ok=True)
args.summary.write_text(summary, encoding="utf-8")
print(summary)
return 0


if __name__ == "__main__":
raise SystemExit(main())
125 changes: 125 additions & 0 deletions .github/scripts/soft_budget_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/usr/bin/env python3
"""Soft time-budget check for CI jobs.

A hard ``timeout-minutes`` cancels the job and reports it red. Sometimes what
we actually want is a signal: this job took longer than it should have, but
completing is more valuable than failing. This script is that signal.

Given a job's wall-clock start (POSIX seconds) and a budget in minutes, it
writes a Slack ``chat.postMessage`` payload to ``--output`` if the observed
duration exceeded the budget, and nothing otherwise. It exits 0 either way;
the caller conditionally posts the file (``if hashFiles(...) != ''``).

Kept deliberately dependency-free so it can be invoked from any job without a
pip install.
"""

from __future__ import annotations

import argparse
import json
import os
import sys
import time
from pathlib import Path

DEFAULT_CHANNEL = "C0C008ZAK0V" # #pw-health


def _human_duration(seconds: int) -> str:
minutes, secs = divmod(max(0, seconds), 60)
if minutes < 60:
return f"{minutes}m {secs:02d}s"
hours, minutes = divmod(minutes, 60)
return f"{hours}h {minutes:02d}m"


def build_payload(
*,
job: str,
started_at: int,
now: int,
budget_minutes: int,
channel: str,
run_url: str,
commit_url: str,
commit_sha: str,
event_name: str,
ref: str,
pr_number: str,
) -> dict | None:
"""Return a Slack payload dict if over budget, else None."""
duration_s = max(0, now - started_at)
budget_s = budget_minutes * 60
if duration_s <= budget_s:
return None

over_by_s = duration_s - budget_s
sha = (commit_sha or "")[:7]

scope = f"PR #{pr_number}" if pr_number else f"`{ref or event_name or 'unknown'}`"
header = (
f":hourglass_flowing_sand: *{job}* on {scope} took"
f" *{_human_duration(duration_s)}*, budget *{budget_minutes}m*"
f" (over by {_human_duration(over_by_s)})."
)
location = (
f"<{commit_url}|{sha}>" if commit_url and sha else (commit_url or sha)
)
lines = [header]
if location:
lines.append(f"Commit: {location}")
if run_url:
lines.append(f"<{run_url}|run>")

return {"channel": channel, "text": "\n".join(lines)}


def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--job", required=True, help="Human-readable job label")
parser.add_argument("--started-at", required=True, type=int, help="Job start (POSIX seconds)")
parser.add_argument("--budget-minutes", required=True, type=int)
parser.add_argument("--now", type=int, default=None, help="POSIX seconds (default: time.time())")
parser.add_argument("--channel", default=DEFAULT_CHANNEL)
parser.add_argument("--run-url", default="")
parser.add_argument("--commit-url", default="")
parser.add_argument("--commit-sha", default="")
parser.add_argument("--event-name", default="")
parser.add_argument("--ref", default="")
parser.add_argument("--pr-number", default="")
parser.add_argument("--output", required=True, help="Path to write slack.json (only if over budget)")
args = parser.parse_args(argv)

now = args.now if args.now is not None else int(time.time())

payload = build_payload(
job=args.job,
started_at=args.started_at,
now=now,
budget_minutes=args.budget_minutes,
channel=args.channel,
run_url=args.run_url,
commit_url=args.commit_url,
commit_sha=args.commit_sha,
event_name=args.event_name,
ref=args.ref,
pr_number=args.pr_number,
)

out = Path(args.output)
if payload is None:
# Under budget: leave no file so the caller's hashFiles() gate skips posting.
if out.exists():
out.unlink()
print(f"under budget: observed <= {args.budget_minutes}m", file=sys.stderr)
return 0

out.parent.mkdir(parents=True, exist_ok=True)
out.write_text(json.dumps(payload))
print(f"over budget: wrote {out}", file=sys.stderr)
return 0


if __name__ == "__main__": # pragma: no cover
sys.exit(main())
Loading
Loading