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
57 changes: 57 additions & 0 deletions .github/workflows/train-content-gate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# A merge train's members keep their content, read from the content (#20406).
#
# Train PR #20271 merged a member with an empty diff: its commits were reachable and the PR read
# MERGED, while its files were not on main. On a train PR this judges each member at its own merge
# commit, so a loss stops the train before it lands. When a train lands it checks that what the
# train changed reached main.
#
# Only `train-*` heads from this repo run it. `pull_request`, not `pull_request_target`, and a
# read-only token. Making `train-content` a required status on main is a branch-protection setting
# (maintainer), recorded on #20406.

name: train-content-gate

on:
pull_request:
branches: [ main ]
types: [ opened, reopened, synchronize, ready_for_review, closed ]

concurrency:
group: train-content-gate-${{ github.event.pull_request.number }}-${{ github.event.action == 'closed' }}
cancel-in-progress: true

permissions: {}

jobs:
train-content:
name: train-content
if: >-
startsWith(github.head_ref, 'train-') &&
github.event.pull_request.head.repo.full_name == github.repository &&
(github.event.action != 'closed' || github.event.pull_request.merged == true)
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: read
steps:
- name: Check out the full history
uses: actions/checkout@v7
with:
fetch-depth: 0
persist-credentials: false
ref: ${{ github.event.pull_request.merged == true && github.event.pull_request.merge_commit_sha || github.event.pull_request.head.sha }}

- name: Every member's content is present at its merge
if: github.event.action != 'closed'
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
TITLE: ${{ github.event.pull_request.title }}
BODY: ${{ github.event.pull_request.body }}
run: python3 pipeline-scripts/train_content_gate.py --range "$(git merge-base "$BASE_SHA" "$HEAD_SHA")..$HEAD_SHA" --title="$TITLE" --body="$BODY"

- name: What the train changed reached main
if: github.event.action == 'closed'
env:
MERGE_SHA: ${{ github.event.pull_request.merge_commit_sha }}
run: python3 pipeline-scripts/train_content_gate.py --landed "$MERGE_SHA"
209 changes: 209 additions & 0 deletions pipeline-scripts/train_content_gate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
#!/usr/bin/env python3
# Copyright 2025-2026 mrveiss
# SPDX-License-Identifier: Apache-2.0
# AutoBot - AI-Powered Automation Platform
# Author: mrveiss
"""A merge train's members must keep their content, read from the content (#20406).

Train PR #20271 merged #20179 with an empty diff: its commits were reachable, the PR read MERGED,
and five of its files were not on main. The cause is unknown, so this keys on the result and not
on how a train is built.

``--range BASE..TIP`` runs before landing. Each ``Merge #N @ sha`` is judged at its own merge
commit, because at the tip a deliberate in-train fix reads as a loss, and a member merged twice is
judged at its last merge against the base of its first, so a drop in the first still counts. A member
fails when a file it added or deleted did not take effect there, or when under half of a file's added
lines are present. ``--title`` names the members the train declares, so one with no merge at all
fails too, as does a merge the gate cannot account for. A head already on the train's base has landed
and passes with a note; a head reachable only through the train leaves nothing to compare and fails.
``--body`` may name a deliberate drop: ``train-content-drop: #N path reason``.

``--landed MERGE`` runs after landing. A file the train changed and main did not must arrive
byte-identical.

Blind spots: a file above half can still have lost lines, moved or split code reads as lost, and a
modified binary has no lines to compare. A file gone by the tip but present at its member's merge is
only a note, because a deliberate in-train fix looks the same. Files that trains recompute are
skipped (``RECOMPUTED``), so a loss in one of those goes unseen.
"""

from __future__ import annotations

import argparse
import re
import subprocess
import sys
from dataclasses import dataclass, field
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from autobot_shared.git_probe import run_git # noqa: E402

MEMBER_MERGE = re.compile(r"^Merge #(\d+) @ [0-9a-f]{7,40}\b")
#: Bringing main into the train adds no member: "Merge main into ...", "Merge branch 'main' into ...".
BASE_MERGE = re.compile(r"^Merge (?:branch |remote-tracking branch )?['\"]?(?:origin/)?main\b")
#: The member list of a train title: "chore(merge-train): #1 #2". A "(#3)" later in it is no member.
DECLARED_MEMBERS = re.compile(r"^chore\(merge-train\):\s*((?:#\d+\s*)+)")
DELIBERATE_DROP = re.compile(r"^train-content-drop:\s*#(\d+)\s+(\S+)\s+\S", re.MULTILINE)
#: Files a train recomputes on purpose, so the member's own value is not expected to survive.
RECOMPUTED = re.compile(r"(_known_large\.py|_baseline\.py|_ratchet_test\.py|_ratchet_\d+_test\.py)$")
#: A line this short matches by chance; a floor pin is recomputed by the train.
MIN_LINE = 12
FLOOR_LINE = "floor="
MIN_SURVIVAL = 0.5


@dataclass
class ContentReport:
members: int = 0
files: int = 0
lines: int = 0
findings: list[str] = field(default_factory=list)
notes: list[str] = field(default_factory=list)


def _git(*args: str) -> str:
return run_git(list(args), capture_output=True, check=True, errors="replace").stdout


def member_merges(rng: str, report: ContentReport) -> dict[str, tuple[str, str]]:
"""``{pr: (last merge, first merge)}``; a merge that is neither member nor main is a finding."""
found: dict[str, tuple[str, str]] = {}
for line in _git("log", "--first-parent", "--merges", "--format=%H %s", rng).splitlines(): # newest first
sha, _, subject = line.partition(" ")
match = MEMBER_MERGE.match(subject)
if match:
found[match.group(1)] = (found.get(match.group(1), (sha,))[0], sha)
elif not BASE_MERGE.match(subject):
report.findings.append(f"{sha[:10]} is a merge this gate cannot account for: {subject!r}")
return found


def declared_members(title: str) -> set[str]:
match = DECLARED_MEMBERS.match(title)
return set(re.findall(r"\d+", match.group(1))) if match else set()


def _is_ancestor(commit: str, of: str) -> bool:
return run_git(["merge-base", "--is-ancestor", commit, of], capture_output=True).returncode == 0


def changed(base: str, head: str) -> list[tuple[str, str]]:
"""``(status, path)`` per file; NUL-separated, so no path is quoted or split."""
fields = _git("diff", "--name-status", "--no-renames", "-z", base, head).split("\0")
return list(zip(fields[0::2], fields[1::2]))


def _exists_at(commit: str, path: str) -> bool:
return bool(_git("ls-tree", commit, "--", path).strip())


def _added_lines(base: str, head: str, path: str) -> list[str]:
diff = _git("diff", "--unified=0", "--no-renames", base, head, "--", path).splitlines()
added = (line[1:].strip() for line in diff if line.startswith("+") and not line.startswith("+++"))
return [line for line in added if len(line) >= MIN_LINE and FLOOR_LINE not in line]


def _judge_file(status: str, path: str, base: str, head: str, merge: str, report: ContentReport) -> str | None:
"""Why *path* did not take effect at *merge*, or None."""
if status == "D":
return "deleted by the member, still present" if _exists_at(merge, path) else None
if not _exists_at(merge, path):
return "missing"
added = _added_lines(base, head, path)
if not added:
return None
present = {line.strip() for line in _git("show", f"{merge}:{path}").splitlines()}
kept = sum(line in present for line in added)
report.lines += len(added)
return f"only {kept}/{len(added)} added lines present" if kept < len(added) * MIN_SURVIVAL else None


def _check_member(pr: str, last: str, first: str, rng: str, drops: set[tuple[str, str]], report: ContentReport) -> None:
start, tip = rng.split("..")
head = f"{last}^2"
base = _git("merge-base", f"{first}^1", f"{first}^2").strip()
if _git("rev-parse", head).strip() == base:
if _is_ancestor(head, start):
report.notes.append(f"#{pr}: its head is already on {start[:10]}, so it has landed")
else:
report.findings.append(f"#{pr}: its head is already in the train at {last[:10]}: nothing was measured")
return
for status, path in changed(base, head):
if RECOMPUTED.search(path):
continue
report.files += 1
why = _judge_file(status, path, base, head, last, report)
if why and (pr, path) in drops:
report.notes.append(f"#{pr} {path}: {why}, recorded as a deliberate drop")
elif why:
report.findings.append(f"#{pr} {path}: {why} at {last[:10]}")
elif status != "D" and not _exists_at(tip, path):
report.notes.append(f"#{pr} {path}: present at its merge, gone by the tip")
if not _git("diff", "--name-only", f"{last}^1", last).strip():
report.notes.append(f"#{pr}: its merge changed nothing; its content was checked above")


def check_range(rng: str, title: str = "", body: str = "") -> ContentReport:
"""Judge every member of the train in *rng*."""
report = ContentReport()
merges = member_merges(rng, report)
report.members = len(merges)
if not merges:
report.findings.append(f"no 'Merge #N @ sha' commit in {rng}: nothing was measured")
for pr in sorted(declared_members(title) - set(merges), key=int):
report.findings.append(f"#{pr} is declared by the train and has no merge in {rng}")
drops = set(DELIBERATE_DROP.findall(body))
for pr, (last, first) in sorted(merges.items(), key=lambda item: int(item[0])):
_check_member(pr, last, first, rng, drops, report)
return report


def check_landed(merge: str) -> ContentReport:
"""A file the train changed and main did not must reach main byte-identical."""
report = ContentReport(members=1)
if len(_git("rev-list", "--parents", "-n", "1", merge).split()) != 3:
report.findings.append(f"{merge[:10]} is not a two-parent merge, so it cannot be compared to the train's tip")
return report
main_before, tip = f"{merge}^1", f"{merge}^2"
base = _git("merge-base", main_before, tip).strip()
main_changed = {path for _, path in changed(base, main_before)}
for _, path in changed(base, tip):
if path in main_changed:
continue
report.files += 1
if _git("diff", "--name-only", tip, merge, "--", path).strip():
report.findings.append(f"{path}: differs from the train's tip at {merge[:10]}")
if not report.files:
report.findings.append(f"{merge[:10]}: no file to compare, so nothing was measured")
return report


def _say(line: str) -> None:
sys.stdout.write(f"{line}\n")


def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
mode = parser.add_mutually_exclusive_group(required=True)
mode.add_argument("--range", help="BASE..TIP of a train branch")
mode.add_argument("--landed", help="the merge commit that landed a train")
parser.add_argument("--title", default="", help="the train PR's title, which lists its members")
parser.add_argument("--body", default="", help="the train PR's body, which may name a deliberate drop")
args = parser.parse_args(argv)
try:
report = check_range(args.range, args.title, args.body) if args.range else check_landed(args.landed)
except subprocess.CalledProcessError as exc:
_say(f"UNMEASURED: {' '.join(map(str, exc.cmd))}: {(exc.stderr or '').strip()}")
return 2
_say(f"examined {report.members} member(s), {report.files} file(s), {report.lines} added line(s)")
for note in report.notes:
_say(f"note: {note}")
for finding in report.findings:
_say(f"LOST: {finding}")
return 1 if report.findings else 0


if __name__ == "__main__":
sys.exit(main())
Loading