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
242 changes: 242 additions & 0 deletions .github/scripts/ratchet-limits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
"""Fails a pull request that raises a limit which only goes down.

`xtask/row-uses.txt` (`cargo xtask rows`) and `xtask/memory-limits.txt` (`cargo xtask memory`) are
committed limits that AGENTS.md says only come down: the xtask checks fail when the code is over
its limit, but nothing stopped a pull request from editing the limit up to make them pass. This
check compares each limit with the one on the base commit (the pull request's base branch, or the
merge queue entry's parent) and fails when one is higher or has been removed. A new line (a new
memory scenario) is fine. Lowering a limit is what the files are for.

A limit may need to rise for a reason a maintainer accepts. A maintainer overrides the check for
one change by commenting on the pull request, or reviewing it, with a line of its own that the
failure prints for that change, such as
`<!-- ratchet-raise-approved: xtask/row-uses.txt count=790 -->`. The line names the file, the
limit and the value it was approved at, so a later push that raises the limit further, or another
limit, needs its own approval. A removed limit is approved the same
way (`... memory-limits.txt save removed -->`), as is a deleted file. A maintainer here is the
repository's owner, a member of its organization or a collaborator, as GitHub labels the message;
the pull request's author may be that maintainer: the override is a deliberate, recorded decision.

The check compares the two data files with their base. It does not look at the code that reads
them (`xtask/src/rows.rs`, `xtask/src/memory.rs`), and it runs from the pull request's own
checkout, like the other checks here, so a pull request that loosens those is for review to catch.

Only these two files are checked. The other hand-kept lists (the commands the hostile tests skip,
the known-silent reader cases, the cancellation exemptions) grow for good reasons, and AGENTS.md
doesn't say they only go down.

CI runs this on pull requests and in the merge queue. It needs BASE_SHA (the pull request's base
commit, or the queue entry's parent), GH_TOKEN (read access to pull requests, used only when a
limit has risen), GITHUB_REPOSITORY, and PR_NUMBER or, in the merge queue, MERGE_GROUP_REF.
"""

import hashlib
import json
import math
import os
import re
import subprocess
import sys

ROWS_FILE = "xtask/row-uses.txt"
LIMIT_FILES = [ROWS_FILE, "xtask/memory-limits.txt"]
APPROVE = "ratchet-raise-approved"
MAINTAINERS = {"OWNER", "MEMBER", "COLLABORATOR"}
# What Rust's `str::trim` removes (Unicode White_Space). Python's `strip` also removes U+001C to
# U+001F, so a line that starts with one would read as a different key here than in the xtask readers.
RUST_WHITESPACE = "\t\n\x0b\x0c\r \x85\xa0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000"
QUEUE_REF = re.compile(r"/pr-(\d+)-[0-9a-f]{40}$")


def parse(text, rows_file=False):
"""The `name = number` lines of a limits file, a `#` starting a comment; the first line of a name counts.

The xtask readers take the first line of a name too, so a later one can't move the limit.
`rows_file` reads `xtask/row-uses.txt` as `cargo xtask rows` does: only `count` matters, and a
line without an `=` is ignored. `cargo xtask memory` rejects a line without one.
"""
limits = {}
# Lines end at LF or CRLF only, as in Rust's `str::lines`: `splitlines` would also split at a
# vertical tab, form feed or bare CR, which the xtask readers keep inside the line.
for n, raw in enumerate(text.split("\n"), 1):
line = raw.removesuffix("\r").split("#", 1)[0].strip(RUST_WHITESPACE)
if not line:
continue
name, sep, value = line.partition("=")
name = name.strip(RUST_WHITESPACE)
if rows_file and (not sep or name != "count"):
continue
if not sep:
raise ValueError(f"line {n}: expected `name = number`")
value = value.strip(RUST_WHITESPACE)
try:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A raised limit can pass unseen: str.strip() removes more than the xtask readers' trim().

Python's strip() also removes U+001C to U+001F (they count as whitespace in Python), but Rust's trim() (Unicode White_Space) does not. The xtask readers call trim(): rows.rs (allowed, k.trim() == "count") and memory.rs (parse_limits, after split('#')). So for a head file like this, with base count = 782:

\x1ccount = 782
count = 999

this check reads the first count as 782 and reports nothing, while xtask rows takes the first line whose key is exactly count, which is count = 999. That raise to 999 needs no approval and passes the check. The same works for a memory limit with a duplicate line. The job's purpose is to stop exactly this.

Fix: strip with the characters Rust trims. Define this next to QUEUE_REF:

RUST_WHITESPACE = " \t\n\x0b\x0c\r\x85\xa0            

   "

and use it here (the suggestion replaces this line only), and in the .strip() calls on the name and value (lines 63, 67, 69, 72).

Add a regression test to test_ratchet_limits.py, next to the line-separator test: self.problems(ROWS, "\x1ccount = 782\ncount = 999\n") should give one problem containing rose from 782 to 999. It fails on the current code. I checked this by reading the code, not by running it.

Suggested change
try:
line = line.removesuffix("\r").split("#", 1)[0].strip(RUST_WHITESPACE)

Category: security

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in da169c7: keys, values and lines are trimmed with the characters Rust's trim removes, not Python's strip. The new test runs \x1c to \x1f before the first count and expects one problem, rose from 782 to 999; it fails with strip() put back.

number = float(value)
except ValueError:
raise ValueError(f"line {n}: `{value}` is not a number") from None
if not math.isfinite(number):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parse() is stricter than the row reader, so it refuses a file xtask accepts, and an unreadable base skips the comparison.

xtask/src/rows.rs allowed() ignores every line that is not #-prefixed and has no = (find_map over split_once('=')), so a plain note such as see AGENTS.md in xtask/row-uses.txt is valid for cargo xtask rows. This raise ValueError turns that line into a head error with no approval token (always refused, "fix the file"). The fix is only to add a #, which is a surprise for a file the xtask accepts.

The reverse is the fail-open case: compare() returns only a notice when the base can't be parsed. If the base holds such a line and the same pull request removes it while raising count, the raise is never compared and passes with no approval.

Fix: for row-uses.txt, skip lines without = as rows.rs does (memory-limits.txt can keep the error, since parse_limits rejects those lines too). Add a test with a non-# note line on both base and head.

Confidence: high on the mechanics, low on likelihood (no such line exists today).

Category: bug

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 7137508. xtask/row-uses.txt is read as cargo xtask rows reads it: only count counts, and a line without an = is ignored. A limits file that can't be read on the base is no longer skipped: it is a problem that needs a maintainer's approval line, so a raise can't hide behind it. Tests cover a note line on both sides and an unreadable base.

raise ValueError(f"line {n}: `{value}` is not a finite number")
limits.setdefault(name, (number, value))
return limits


def marker(token):
"""The line a maintainer's message carries to approve the change named by `token`."""
return f"<!-- {APPROVE}: {token} -->"


def compare(path, base_text, head_text):
"""The raised or removed limits of one file, as (message, approval token) pairs.

`base_text` is None when the file didn't exist on the base (nothing to compare), `head_text` None
when the change deletes it. A token of None means nothing can approve the problem: fix the file.
"""
if base_text is None:
return []
head = None
if head_text is not None:
try:
head = parse(head_text, path == ROWS_FILE)
except ValueError as error:
return [(f"{path}: {error}", None)]
try:
base = parse(base_text, path == ROWS_FILE)
except ValueError as error:
# The limits can't be compared, so a raise could hide behind this: it needs a maintainer's say,
# for the head as it is (its digest is in the token), not for whatever is pushed next.
digest = hashlib.sha256((head_text or "").encode("utf-8")).hexdigest()[:12]
return [(f"{path}: can't be read on the base ({error}), so its limits can't be compared", f"{path} unreadable on base, head {digest}")]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A deleted limits file whose base can't be read is approved by the "unreadable on base" line, not by a "deleted" line. This re-raises an earlier thread on this spot: the code has changed since (the digest was added), and the problem is still there.

compare() parses the base before it looks at head_text. When the head was deleted (head_text is None) and the base doesn't parse, this return gives the token … unreadable on base, head e3b0c44298fc (the digest of ""). The deleted token on line 109 is never reached. A maintainer who approves the line the job prints for this change approves the deletion without the line naming it, which contradicts the docstring and the PR text ("a deleted file … its own line"). Impact is limited, since a base that doesn't parse already fails cargo xtask, but the approval should name what it covers.

Suggested fix: answer the deletion first in that branch, and add a test.

Suggested change
return [(f"{path}: can't be read on the base ({error}), so its limits can't be compared", f"{path} unreadable on base, head {digest}")]
if head_text is None:
return [(f"{path}: deleted, and its base can't be read ({error})", f"{path} deleted")]
return [(f"{path}: can't be read on the base ({error}), so its limits can't be compared", f"{path} unreadable on base, head {digest}")]

Test to add beside test_a_file_that_cannot_be_read_fails_on_the_head_and_is_skipped_on_the_base: self.assertEqual(self.tokens("count = many\n", None), ["xtask/x.txt deleted"]).

Category: bug

if head is None:
return [(f"{path}: deleted, but it holds {len(base)} limit(s) that only go down", f"{path} deleted")]
problems = []
for name, (old, old_text) in base.items():
if name not in head:
problems.append((f"{path}: the limit `{name}` ({old_text}) was removed", f"{path} {name} removed"))
elif head[name][0] > old:
problems.append((f"{path}: `{name}` rose from {old_text} to {head[name][1]}", f"{path} {name}={head[name][1]}"))
return problems


def run(args):
"""Runs a command and returns its standard output as it is, line endings included; raises if it fails."""
return subprocess.run(args, check=True, stdout=subprocess.PIPE, timeout=120).stdout.decode("utf-8", "replace")


def base_mode(base_sha, path):
"""The file's git mode on the base commit (`120000` is a symbolic link), or None when it has no such file."""
listing = run(["git", "ls-tree", base_sha, "--", path]).split()
return listing[0] if listing else None


def base_file(base_sha, path):
"""The file's text on the base commit, or None when the base has no such file."""
if base_mode(base_sha, path) is None:
return None
return run(["git", "show", f"{base_sha}:{path}"])
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved


def is_link(path):
"""Whether the checkout has a symbolic link at the path."""
return os.path.islink(path)


def head_file(path):
"""The file's text in the checkout, with its line endings, or None when it isn't there."""
try:
with open(path, encoding="utf-8", newline="") as f:
return f.read()
except FileNotFoundError:
return None


def objects(text):
"""Every JSON value in `text`, whether `gh` prints them one to a line or indented."""
decoder, at, found = json.JSONDecoder(), 0, []
while True:
while at < len(text) and text[at].isspace():
at += 1
if at >= len(text):
return found
value, at = decoder.raw_decode(text, at)
found.append(value)


def messages(repo, number):
"""Every comment and review on the pull request, as (body, author association, is a bot)."""
found = []
for endpoint in (f"repos/{repo}/issues/{number}/comments", f"repos/{repo}/pulls/{number}/reviews"):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits, grouped (quality, left for the release quality pass):

  • messages() reads issue comments and review bodies only. An approval line written as an inline line comment inside a review (pulls/{n}/comments) is ignored, and the failure text says "comments on the pull request, or reviewing it" without saying so. This fails closed, but a maintainer who posts it that way gets refused. Either read the review comments too or name the limitation in the failure message.
  • head_file() can raise UnicodeDecodeError or IsADirectoryError, which escape as a traceback instead of a ::error:: line that names the file. Exit 1 still fails the job, so this is style only.
  • In test_a_symbolic_link_at_a_limit_path_is_never_accepted, the approvals use the tokens None and count=782. The None marker can never match, so that part of the test checks nothing new.

Category: quality

out = run(
["gh", "api", "--paginate", endpoint, "--jq",
".[] | {body, association: .author_association, bot: (.user.type == \"Bot\")}"]
)
found += objects(out)
return found


def approved(found, token):
"""Whether a maintainer's message has the approval line for `token` on a line of its own."""
line = marker(token)
return any(
not m.get("bot")
and m.get("association") in MAINTAINERS
and any(row.strip() == line for row in (m.get("body") or "").split("\n"))
for m in found
)


def pr_number():
"""The pull request to check, from PR_NUMBER or the merge queue's branch name."""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits, grouped:

  • approved() splits the body with splitlines(), but parse() deliberately splits only at LF and CRLF. Use split("\n") here too so a marker can't be found on a line the reader would not see as one.
  • Tests don't reach messages(), run(), head_file() or the PR_NUMBER branch of pr_number(). A test with a stubbed gh output in the one-object-per-line format would cover the API shape.
  • Lines 12–13 of the docstring run past 100 columns; the wrap is uneven.

Category: quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 7137508: approved() splits at LF only, the docstring wrap is corrected, and tests cover messages() (one object per line, comments and reviews) and pr_number() with PR_NUMBER set and with neither it nor a queue branch.

if os.environ.get("PR_NUMBER"):
return int(os.environ["PR_NUMBER"])
match = QUEUE_REF.search(os.environ.get("MERGE_GROUP_REF", ""))
if match is None:
raise SystemExit("::error::No PR_NUMBER, and MERGE_GROUP_REF names no pull request.")
return int(match.group(1))


def escape(text):
"""Text for a workflow command line: a newline must not start a command of its own."""
return text.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A")


def main():
"""Checks the limits against the base and exits 1 if one rose or was removed without a maintainer's say."""
base_sha = os.environ.get("BASE_SHA", "")
if not re.fullmatch(r"[0-9a-f]{40}", base_sha):
raise SystemExit("::error::BASE_SHA is not a commit hash, so there is nothing to compare the limits with.")
run(["git", "fetch", "--no-tags", "--depth=1", "origin", base_sha])
problems = []
for path in LIMIT_FILES:
# A link reads as its target here and as its own text in git, so the two sides would not
# be the same file: neither is accepted.
if is_link(path) or base_mode(base_sha, path) == "120000":
problems.append((f"{path}: is a symbolic link, but the limits must be in a regular file", None))
continue
problems += compare(path, base_file(base_sha, path), head_file(path))
if not problems:
print("No limit that only goes down has risen.")
return 0
number = pr_number()
found = messages(os.environ["GITHUB_REPOSITORY"], number)
refused = []
for message, token in problems:
if token is not None and approved(found, token):
print(f"::notice::{escape(message)}: approved by a maintainer on #{number}")
else:
refused.append((message, token))
print(f"::error::{escape(message)}")
if not refused:
return 0
print(
f"#{number} raises {len(refused)} limit(s) that only go down. Fix the code so the old limit holds, "
"or, if the change is deliberate, a maintainer comments on the pull request with each of these "
"lines on a line of its own, and the check is run again (re-run the job, or queue the pull request "
"again). A removed memory scenario needs its line too."
)
for _, token in refused:
if token is not None:
print(marker(token))
return 1


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