Merge origin/main into ci/linux-runner

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-26 01:23:12 +01:00
47 changed files with 2179 additions and 64 deletions
+56 -13
View File
@@ -45,6 +45,11 @@ import subprocess
import sys
import time
try:
from . import parity_reach
except ImportError:
import parity_reach
ROOT = Path(__file__).resolve().parents[1]
LOCAL = ROOT / ".local"
STATE_DIR = LOCAL / "parity-runner"
@@ -652,6 +657,9 @@ def prebuild(artifacts, jobs):
LARGE_OUTPUT = 64 << 20
# Vanilla outputs above this are deleted after a pass too: rerunning those few oracles (12 harnesses on the Mac
# mini's runner) costs less than the 26 GB they held in every checkout; smaller ones stay for oracle reuse.
KEPT_ORACLE_OUTPUT = 256 << 20
OWN_DIRECTORIES = {"parity-target", "parity-runner", "rea", "logs", "gate"}
@@ -667,11 +675,12 @@ def clear_large_outputs(name, trace):
"""After a pass, delete the large files a harness regenerates on every run.
Harness outputs reach gigabytes and used to stay in every worktree. Vanilla
oracle outputs are kept, since reusing an oracle run needs them in place;
everything else large under the harness's .local directories is rebuilt on
its next run."""
oracle outputs under KEPT_ORACLE_OUTPUT are kept, since reusing an oracle
run needs them in place; everything else large under the harness's .local
directories (`<name>...` or `rust-<name>-parity`) is rebuilt on its next run."""
local = LOCAL.resolve()
keep = {os.path.realpath(line) for line in outputs_listing(name).read_text(encoding="utf-8").splitlines() if line}
own = name.split("#")[0].replace("_", "-")
directories = set()
for command in trace["commands"]:
for argument in command[1:]:
@@ -683,12 +692,13 @@ def clear_large_outputs(name, trace):
# Only the harness's own directory: another harness running beside it may be writing
# large files in a directory this one merely read (decorated_region compiles its oracle
# into chunk-generation-parity while chunk_generation writes its dumps there).
if top not in OWN_DIRECTORIES and top.startswith(name.replace("_", "-")):
if top not in OWN_DIRECTORIES and (top.startswith(own) or top == f"rust-{own}-parity"):
directories.add(local / top)
for directory in directories:
for file in (directory.rglob("*") if directory.is_dir() else []):
try:
if file.is_file() and not file.is_symlink() and file.stat().st_size >= LARGE_OUTPUT and str(file) not in keep:
size = file.stat().st_size if file.is_file() and not file.is_symlink() else 0
if size >= LARGE_OUTPUT and (str(file) not in keep or size > KEPT_ORACLE_OUTPUT):
file.unlink()
except OSError:
pass
@@ -823,8 +833,11 @@ class LocalExecutor:
environment.pop("PARITY_SHARD", None)
if shard:
environment["PARITY_SHARD"] = shard
# stdin from /dev/null: tools/parity_shim.py never reuses an oracle run that reads a pipe, and CI runners
# (and --worker's ssh) give the runner one, which kept oracle reuse off on CI.
process = subprocess.Popen([sys.executable, str(Path(__file__)), "--trace", str(trace), str(script)],
cwd=ROOT, env=environment, stdout=log, stderr=subprocess.STDOUT, start_new_session=True)
cwd=ROOT, env=environment, stdin=subprocess.DEVNULL, stdout=log,
stderr=subprocess.STDOUT, start_new_session=True)
self.running[name] = {"process": process, "started": time.monotonic(), "log": log, "expected": expected,
"slot": slot, "script": script}
return True
@@ -1224,20 +1237,44 @@ def changed_harnesses(found, records, ref):
return found_changed
def affected_harnesses(found, records, changed):
def affected_harnesses(found, records, changed, reach=True, base=None):
"""Harnesses the changed files can affect: their script or a recorded dependency (here, or in the shared
dependency lists) changed, a file every harness depends on changed, or nothing is recorded about them yet."""
dependency lists) changed, a file every harness depends on changed, or nothing is recorded about them yet.
With reach, a harness's workspace Rust sources count only if its probes can reach them (tools/parity_reach.py);
a harness whose probes can't be placed keeps them all. A changed file counts unless, compared with base (a
commit), only declarations of modules the harness doesn't reach changed (tools/parity_reach.py)."""
if changed & SHARED_INPUTS:
return set(found)
graph = None
if reach and any(path.startswith("crates/") for path in changed):
cache_path = STATE_DIR / "reach-cache.json"
try:
cache = json.loads(cache_path.read_text(encoding="utf-8"))
except (OSError, ValueError):
cache = {}
graph = parity_reach.SourceGraph(ROOT, rust_code, cache)
affected = set()
for name, script in found.items():
dependencies = records.get(name, {}).get("dependencies")
record = records.get(name) or {}
dependencies = record.get("dependencies")
template = SHARED / "dependencies" / f"{name}.json"
if dependencies is None and template.exists():
dependencies = json.loads(template.read_text(encoding="utf-8")).get("dependencies")
if dependencies is None or Path(script).relative_to(ROOT).as_posix() in changed or \
any(path.split(KEEP)[0] in changed for path in dependencies):
record = json.loads(template.read_text(encoding="utf-8"))
dependencies = record.get("dependencies")
if dependencies is None or Path(script).relative_to(ROOT).as_posix() in changed:
affected.add(name)
continue
plain = {path.split(KEEP)[0] for path in dependencies}
if graph is not None and plain & changed:
narrowed = parity_reach.narrow(dependencies, recorded_artifacts(record), graph, changed)
if any(path.split(KEEP)[0] in changed and (children is None or parity_reach.declarations_changed(
ROOT, path.split(KEEP)[0], children, base, whole)) for path, (whole, children) in narrowed.items()):
affected.add(name)
elif plain & changed:
affected.add(name)
if graph is not None:
STATE_DIR.mkdir(parents=True, exist_ok=True)
cache_path.write_text(json.dumps(graph.saved_cache()), encoding="utf-8")
return affected
@@ -1280,6 +1317,8 @@ def main():
parser.add_argument("--flake-rounds", type=int, default=3, help="runs required by --flake-check (default 3)")
parser.add_argument("--changed", metavar="REF", help="only harnesses the changes since the merge base with REF can "
"affect (and that have not passed with their current inputs): the pull-request check")
parser.add_argument("--by-crate", action="store_true", help="with --changed: count every Rust source a probe "
"links, not only those it can reach (tools/parity_reach.py)")
parser.add_argument("--changed-files", metavar="FILE", help="like --changed, for the changed paths listed in FILE, "
"one per line: for a copy without Git history (tools/pr_ready.py on another machine)")
args = parser.parse_args()
@@ -1316,7 +1355,11 @@ def main():
changed, since = changed_files(args.changed), f" since the merge base with {args.changed}"
if changed is None:
parser.error(f"no merge base with {args.changed}")
affected = affected_harnesses(found, records, changed)
base = None
if args.changed:
merge_base = subprocess.run(["git", "merge-base", "HEAD", args.changed], cwd=ROOT, capture_output=True, text=True)
base = merge_base.stdout.strip() or None
affected = affected_harnesses(found, records, changed, reach=not args.by_crate, base=base)
chosen = [name for name in found if name in affected and stale(name, state, hasher, environment)]
print(f"{len(changed)} files changed{since}: {len(affected)} harnesses "
f"depend on them, {len(chosen)} without a pass for their current inputs.", flush=True)
+129
View File
@@ -0,0 +1,129 @@
#!/usr/bin/env python3
"""parity-main (.github/workflows/parity-main.yml): the vanilla harnesses run on main, a batch of merges at a time.
python3 tools/parity_main.py base # the commit this run checks from: main's last green parity-main
python3 tools/parity_main.py bisect BASE # after a failure: the first merge since BASE that broke a harness
`base` prints the newest commit on main whose parity-main run passed and that HEAD contains, else HEAD's first parent,
so a run tests every merge since the last green one (`tools/parity.py --changed <base>`), however many landed while
the previous run went on. `bisect` reads the harnesses that failed (.local/parity-runner/last-run.json), checks out
the first-parent merges between BASE and HEAD by halves and runs only those harnesses, then names the first merge they
fail on (written to $GITHUB_STEP_SUMMARY and, with --comment, on its pull request). HEAD is checked out again at the end.
"""
import argparse
import json
import os
from pathlib import Path
import re
import subprocess
import sys
ROOT = Path(__file__).resolve().parents[1]
REPO = os.environ.get("GITHUB_REPOSITORY", "ludovic111/horizon")
LAST_RUN = ROOT / ".local" / "parity-runner" / "last-run.json"
def git(*arguments, check=True):
result = subprocess.run(["git", *arguments], cwd=ROOT, capture_output=True, text=True)
if check and result.returncode:
sys.exit(f"git {' '.join(arguments)}: {result.stderr.strip()}")
return result
def green_runs():
"""Head commits of main's passing parity-main runs, newest first."""
result = subprocess.run(["gh", "run", "list", "--repo", REPO, "--workflow", "parity-main", "--branch", "main",
"--status", "success", "--limit", "30", "--json", "headSha"], capture_output=True, text=True)
if result.returncode:
print(f"note: no parity-main history ({result.stderr.strip()})", file=sys.stderr)
return []
return [run["headSha"] for run in json.loads(result.stdout or "[]")]
def base(head="HEAD", runs=None):
for sha in green_runs() if runs is None else runs:
if git("merge-base", "--is-ancestor", sha, head, check=False).returncode == 0:
return git("rev-parse", sha).stdout.strip()
return git("rev-parse", f"{head}^1").stdout.strip()
def first_bad(commits, fails):
"""The first of commits (oldest first, the last known to fail; the one before them known to pass) where fails()."""
good, bad = -1, len(commits) - 1
while bad - good > 1:
middle = (good + bad) // 2
if fails(commits[middle]):
bad = middle
else:
good = middle
return commits[bad]
def harness_exists(name):
return (ROOT / "tools" / f"test_{name.split('#')[0]}_parity.py").exists()
def run_harnesses(names, timeout):
"""Whether any of the harnesses fails at the checked-out commit (those it doesn't have yet can't)."""
present = [name for name in names if harness_exists(name)]
if not present:
return False
return subprocess.run([sys.executable, "tools/parity.py", *present, "--timeout", str(timeout)], cwd=ROOT).returncode != 0
def bisect(since, timeout, comment):
failed = []
if LAST_RUN.exists():
failed = json.loads(LAST_RUN.read_text(encoding="utf-8")).get("failed") or []
if not failed:
print("No failed harness recorded: nothing to bisect.")
return 0
head = git("rev-parse", "HEAD").stdout.strip()
commits = git("rev-list", "--first-parent", "--reverse", f"{since}..{head}").stdout.split()
if not commits:
print("No merge since the base.")
return 0
def fails(commit):
print(f"--- {commit[:9]} {git('log', '-1', '--format=%s', commit).stdout.strip()}", flush=True)
git("checkout", "-q", "--detach", commit)
return run_harnesses(failed, timeout)
try:
culprit = first_bad(commits, fails) if len(commits) > 1 else commits[0]
finally:
git("checkout", "-q", "--detach", head)
subject = git("log", "-1", "--format=%s", culprit).stdout.strip()
number = re.search(r"Merge pull request #(\d+)", subject)
line = (f"parity-main: {', '.join(failed)} first fail at {culprit[:9]} ({subject}), "
f"of {len(commits)} merges since {since[:9]}.")
print(line)
if os.environ.get("GITHUB_STEP_SUMMARY"):
with open(os.environ["GITHUB_STEP_SUMMARY"], "a", encoding="utf-8") as summary:
summary.write(f"### {line}\n")
if comment and number:
url = f"{os.environ.get('GITHUB_SERVER_URL', 'https://github.com')}/{REPO}/actions/runs/{os.environ.get('GITHUB_RUN_ID', '')}"
body = (f"🤖 Agent: parity-main found that this merge breaks {', '.join(f'`{name}`' for name in failed)} on main "
f"(bisected over {len(commits)} merges: {url}). Releases wait until parity-main passes again.")
subprocess.run(["gh", "pr", "comment", number.group(1), "--repo", REPO, "--body", body])
return 0
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
commands = parser.add_subparsers(dest="command", required=True)
commands.add_parser("base")
bisecting = commands.add_parser("bisect")
bisecting.add_argument("since", help="the last commit known to pass (the run's base)")
bisecting.add_argument("--timeout", type=int, default=2400, help="seconds per harness")
bisecting.add_argument("--comment", action="store_true", help="comment on the breaking merge's pull request")
args = parser.parse_args()
if args.command == "base":
print(base())
return 0
return bisect(args.since, args.timeout, args.comment)
if __name__ == "__main__":
sys.exit(main())
+768
View File
@@ -0,0 +1,768 @@
"""Which of a harness's Rust sources its probes can reach, for `parity.py --changed`.
Cargo's dep-info names every source file of every crate a probe links, so a change anywhere in horizon-client
selected every harness whose probe links horizon-client. This module narrows a harness's recorded dependencies to
the files holding an item its entry points (the probe modules it ran, the binaries it built) can reach, over a graph
of the workspace's current sources whose nodes are items:
- everything in an entry is reached, and so is everything in a Rust file the module tree doesn't place (an
`include!`d source);
- a module is open when it is a crate root, holds reached code, or is named in a path (`name::`, `use ...name`) by
reached code while its parent is open;
- a free item (fn, struct, enum, union, trait, type, const, static, an item-level macro call's items) is reached when
reached code in its own module or below it mentions its name, or when its module is open and reached code names
it in a path or an import, or when its module is glob-imported (`use m::*`) and its name is mentioned;
- an inherent impl's methods and constants are reached one by one, by name, once their type is; a trait impl once
its trait and its type are (a std or outside name always is, a generic parameter needs nothing); a macro once
invoked; a `pub use` like a free item named by everything it imports;
- "a crate's reached code" covers the crates it names (`horizon_world::`), directly or through one that does, and
code under `#[cfg(test)]` or `#[test]` is left out (probes are never test builds).
Names match by spelling, so a file can be kept for a name it never meant: the graph only over-approximates. A
reached file's parent modules count through their `mod` declarations of it (parity.py compares those with the merge
base). Everything that isn't a workspace Rust source stays a dependency, except a crate's non-Rust files (shaders,
assets, fonts) that only unreached sources name; a dependency that no longer exists counts as changed, and Rust files
a change adds take part.
"""
import hashlib
from pathlib import Path, PurePosixPath
import re
import subprocess
IDENT = r"[A-Za-z_]\w*"
WORD = re.compile(rf"\b({IDENT})\b")
MOD_DECLARATION = re.compile(rf"\bmod\s+({IDENT})\s*;")
INLINE_MOD = re.compile(rf"\bmod\s+({IDENT})\s*\{{")
PATH_ATTRIBUTE = re.compile(r'#\[\s*path\s*=\s*"([^"]+)"\s*\]\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+\w+\s*;$')
BRACE = re.compile(r"[{}]")
TEST_ONLY = re.compile(r"#\[\s*(?:cfg\s*\(\s*test\s*\)|test)\s*\]")
TEST_MODULE = re.compile(r"#\[\s*cfg\s*\(\s*test\s*\)\s*\]\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+\w+\s*;$")
SPLIT = re.compile(r"[{}()\[\];]")
ITEM = re.compile(rf"(?:pub(?:\s*\([^)]*\))?\s+)?(?:(?:unsafe|async|const|extern|default|auto)\s+)*"
rf"(fn|struct|enum|union|trait|type|const|static|impl|mod|use|macro_rules\s*!|extern\s+crate)"
rf"(?!\w)\s*(?:mut\s+)?({IDENT})?")
PATH_BEFORE = re.compile(rf"\b({IDENT})\s*::")
PATH_AFTER = re.compile(rf"::\s*({IDENT})")
MACRO_USE = re.compile(rf"\b({IDENT})\s*!\s*[(\[{{]")
MACRO_CALL = re.compile(rf"({IDENT}(?:\s*::\s*{IDENT})*)\s*!")
DEFINED_INSIDE = re.compile(rf"\b(?:fn|struct|enum|union|trait|type|const|static)\s+({IDENT})")
NESTED_IMPL = re.compile(r"^[ \t]*(?:unsafe\s+)?impl\b([^{;]*)\{", re.MULTILINE)
QUALIFIED = re.compile(rf"{IDENT}(?:\s*::\s*{IDENT})*")
GLOB_IMPORT = re.compile(r"\buse\s[^;]*::\s*\*")
# File names a source may name in a literal (include_str!, include_bytes!, a path it reads). A file of another kind
# is never "named", so it always counts.
FILE_NAME = re.compile(r"[\w-]+\.(?:wgsl|glsl|json|png|ttf|otf|svg|txt|toml|ogg|wav|nbt|bin|dat|csv|mcmeta|wit|wasm)\b")
TYPE_KINDS = {"struct", "enum", "union", "trait", "type", "macro_call"}
# Names an impl header can take from outside the workspace: std's prelude and primitive types, and the std and serde
# traits a glob import may bring. A glob-imported name outside this list is taken to be the workspace's.
STD_NAMES = {"Display", "Error", "Read", "Write", "Seek", "BufRead", "Deref", "DerefMut", "Index", "IndexMut", "Add",
"Sub", "Mul", "Div", "Rem", "Neg", "Not", "BitAnd", "BitOr", "BitXor", "Shl", "Shr", "AddAssign",
"SubAssign", "MulAssign", "DivAssign", "RemAssign", "BitAndAssign", "BitOrAssign", "BitXorAssign",
"Borrow", "BorrowMut", "Future", "Any", "FromStr", "Hasher", "BuildHasher", "Formatter", "Ordering",
"Serialize", "Deserialize", "Serializer", "Deserializer", "Visitor", "Termination", "Sum", "Product",
"Wake", "Cow", "Rc", "Arc", "Mutex", "RwLock", "Cell", "RefCell", "HashMap", "HashSet", "BTreeMap",
"BTreeSet", "VecDeque"}
PRELUDE = {"Option", "Some", "None", "Result", "Ok", "Err", "Vec", "String", "Box", "ToString", "ToOwned", "Clone",
"Copy", "Send", "Sync", "Sized", "Unpin", "Drop", "Fn", "FnMut", "FnOnce", "From", "Into", "TryFrom",
"TryInto", "AsRef", "AsMut", "Default", "Eq", "PartialEq", "Ord", "PartialOrd", "Iterator", "Extend",
"IntoIterator", "DoubleEndedIterator", "ExactSizeIterator", "FromIterator", "Debug", "Hash",
"bool", "char", "str", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128",
"isize", "f32", "f64"}
STD_NAMES |= PRELUDE
KEYWORDS = {"crate", "self", "super", "Self", "pub", "in", "as", "use", "for", "where", "dyn", "mut", "const", "fn",
"unsafe", "impl", "std", "core", "alloc", "let", "if", "else", "match", "return", "struct", "enum", "mod",
"type", "static", "trait", "true", "false", "loop", "while", "move", "ref", "break", "continue", "async",
"await", "extern", "union"}
def segments(code, start, end):
"""The item-level pieces of code[start:end]: each ends at a `;` or a `}` outside any bracket."""
found, begin, depth = [], start, 0
for match in SPLIT.finditer(code, start, end):
character = match.group()
if character in "{([":
depth += 1
elif character in "})]":
depth -= 1
if character == "}" and depth == 0:
found.append((begin, match.end()))
begin = match.end()
elif depth == 0:
found.append((begin, match.end()))
begin = match.end()
if code[begin:end].strip():
found.append((begin, end))
return found
def skip_attributes(code, index, end):
"""The offset of the first non-space character after the attributes (`#[...]`, `#![...]`) starting at index."""
while True:
while index < end and code[index].isspace():
index += 1
if not code.startswith("#", index) or not re.match(r"#!?\[", code[index:index + 3]):
return index
depth = 0
for position in range(code.index("[", index), end):
depth += {"[": 1, "]": -1}.get(code[position], 0)
if depth == 0:
index = position + 1
break
else:
return end
def body_of(code, start, end):
"""The range inside an item's outermost `{ }` block, or None."""
opening = code.find("{", start, end)
closing = code.rfind("}", start, end)
return (opening + 1, closing) if 0 <= opening < closing else None
def words(text):
return set(WORD.findall(text)) - KEYWORDS
def qualified_words(text):
"""Names a piece of code reaches through a path (`a::b`: b), the way items of other modules are named."""
return set(PATH_AFTER.findall(text)) - KEYWORDS
def path_words(text):
"""Names a piece of code uses as path segments (`a::`, `::b`), where module names appear."""
return set(PATH_BEFORE.findall(text)) | set(PATH_AFTER.findall(text))
def parse_items(code, start, end, items):
"""Append the items of code[start:end] (a file or an inline module's body) to items, as dicts."""
for begin, finish in segments(code, start, end):
head = skip_attributes(code, begin, finish)
text = code[head:finish]
if not text.strip(" \n\t;") or TEST_ONLY.search(code[begin:head]):
continue # compiled into tests only, never into a probe
match = ITEM.match(text)
kind = re.sub(r"\s+", "", match.group(1)) if match else None
name = match.group(2) if match else None
if kind == "mod":
body = body_of(code, head, finish)
if body:
parse_items(code, body[0], body[1], items)
continue
if kind in ("externcrate", None) and not MACRO_CALL.match(text):
if kind is None:
items.append({"kind": "glue", "mentions": sorted(words(text)), "paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))})
continue
if kind is None: # an item-level macro call: it may define items
items.append({"kind": "macro_call", "names": sorted(set(DEFINED_INSIDE.findall(text))),
"macro": MACRO_CALL.match(text).group(1).split("::")[-1].strip(),
"mentions": sorted(words(text)), "paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))})
elif kind == "use":
head_word = re.match(rf"\s*(?:::\s*)?({IDENT})", text[match.end(1):])
items.append({"kind": "use", "public": text.startswith("pub"), "names": sorted(words(text)),
"head": head_word.group(1) if head_word else "", "glob": bool(re.search(r"::\s*\*", text)),
"mentions": sorted(words(text)), "paths": sorted(words(text)), "qualified": sorted(words(text)),
"invokes": []})
elif kind == "impl":
body = body_of(code, head, finish)
subject = code[head + match.end(1) - match.start(): body[0] - 1 if body else finish]
inherent = not re.search(r"\bfor\b", impl_subject(subject)[1])
if inherent and body:
members = []
for member_begin, member_end in segments(code, body[0], body[1]):
member = code[skip_attributes(code, member_begin, member_end):member_end]
found = ITEM.match(member)
members.append({"name": found.group(2) if found else None, "mentions": sorted(words(member)),
"paths": sorted(path_words(member)), "qualified": sorted(qualified_words(member)), "invokes": sorted(set(MACRO_USE.findall(member)))})
items.append({"kind": "impl", "subject": subject, "inherent": True, "members": members,
"mentions": sorted(words(subject)), "paths": sorted(path_words(subject)), "qualified": sorted(qualified_words(subject)), "invokes": sorted(set(MACRO_USE.findall(subject)))})
else:
items.append({"kind": "impl", "subject": subject, "inherent": False, "mentions": sorted(words(text)),
"paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))})
else:
kind = "macro" if kind.startswith("macro_rules") else kind
items.append({"kind": kind, "names": [name] if name else [], "mentions": sorted(words(text)),
"defines": sorted(set(DEFINED_INSIDE.findall(text))) if kind == "macro" else [],
"paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))})
if kind in ("fn", "const", "static"):
# An impl inside a function body still holds for the whole program.
for header in NESTED_IMPL.findall(text):
items.append({"kind": "impl", "subject": header, "inherent": False,
"mentions": sorted(words(text)), "paths": sorted(path_words(text)), "qualified": sorted(qualified_words(text)), "invokes": sorted(set(MACRO_USE.findall(text)))})
def crate_roots(crate_dir):
"""A crate's root files: its library, binaries and examples."""
roots = [crate_dir / "src" / "lib.rs", crate_dir / "src" / "main.rs"]
for folder in (crate_dir / "src" / "bin", crate_dir / "examples"):
if folder.is_dir():
roots += sorted(folder.glob("*.rs")) + sorted(folder.glob("*/main.rs"))
return [root for root in roots if root.is_file()]
class SourceGraph:
"""The workspace's Rust sources, parsed once: what each file names, defines and declares."""
def __init__(self, root, rust_code, cache=None):
"""cache: a dict of earlier parses by content digest, which the graph reuses and adds to (parity.py keeps it
in .local/parity-runner/reach-cache.json)."""
self.root = Path(root)
self.rust_code = rust_code
self.cache = {} if cache is None else cache
self.used = set()
self.files = {}
self.parent = {} # module file -> the file declaring it
self.module_name = {}
self.roots = set()
self.target_cache = {}
self.workspace_heads = None
self.test_only = set() # modules compiled into tests only
self.tables = None
self.root_of = {} # source -> the root of its target (lib.rs, a bin's or an example's main file)
self.results = {} # (entries, universe) -> reachable files
for manifest in sorted((self.root / "crates").glob("*/Cargo.toml")):
for crate_root in crate_roots(manifest.parent):
path = self.relative(crate_root)
self.roots.add(path)
self.root_of[path] = path
self.place(path, crate_root.parent, set())
def entries(self, artifacts, rust):
"""The root files of the probes and binaries a harness built (its record's artifacts), among its Rust
dependencies: for a crate's probe binary, the probe modules it ran (parity.only_probes_run kept only those).
None when an artifact can't be placed."""
found = set()
for kind, name, *rest in artifacts:
selection = rest[1] if len(rest) > 1 else []
packages = [value for flag, value in zip(selection, selection[1:]) if flag == "-p"] or \
[manifest.parent.name for manifest in sorted((self.root / "crates").glob("*/Cargo.toml"))]
roots = set()
for package in packages:
crate = self.root / "crates" / package
targets = self.targets(crate)
if kind in ("example", "examples"):
candidates = [path for target, (target_kind, path) in targets.items()
if target_kind == "example" and (not name or target == name)]
else:
candidates = [path for target, (target_kind, path) in targets.items()
if target_kind == "bin" and (not name or target == name)]
for path in candidates:
if path.endswith("/examples/probe/main.rs"):
# The probes it ran, and the dispatch and support modules they use (only_probes_run kept
# those); the binary's root, which only declares them, so their own submodules are placed.
folder = path.removesuffix("main.rs")
roots |= {dependency for dependency in rust if dependency.startswith(folder)
and "/" not in dependency.removeprefix(folder).removeprefix("support/")}
if roots:
roots.add(path)
elif path in rust:
roots.add(path)
if not roots:
return None
found |= roots
return found or None
def targets(self, crate):
"""A crate's bins and examples: {target name: (kind, root file)}, from its Cargo.toml and Cargo's defaults."""
if crate in self.target_cache:
return self.target_cache[crate]
found = {}
manifest = (crate / "Cargo.toml").read_text(encoding="utf-8") if (crate / "Cargo.toml").is_file() else ""
package = re.search(r'^\[package\][^\[]*?^name\s*=\s*"([^"]+)"', manifest, re.MULTILINE | re.DOTALL)
if (crate / "src" / "main.rs").is_file() and package:
found[package.group(1)] = ("bin", self.relative(crate / "src" / "main.rs"))
for kind, folder in (("bin", crate / "src" / "bin"), ("example", crate / "examples")):
for path in sorted(folder.glob("*.rs")) + sorted(folder.glob("*/main.rs")):
found[path.stem if path.name != "main.rs" else path.parent.name] = (kind, self.relative(path))
for kind, table in re.findall(r"^\[\[(bin|example)\]\]([^\[]*)", manifest, re.MULTILINE):
name = re.search(r'^name\s*=\s*"([^"]+)"', table, re.MULTILINE)
path = re.search(r'^path\s*=\s*"([^"]+)"', table, re.MULTILINE)
if name and path and (crate / path.group(1)).is_file():
found[name.group(1)] = (kind, self.relative(crate / path.group(1)))
self.target_cache[crate] = found
return found
def target(self, path):
"""The Cargo target a source belongs to: the root file of the library, bin or example whose module tree
holds it (a file nothing places is its own)."""
return self.root_of.get(path, path)
def is_library(self, path):
return self.target(path).endswith("/src/lib.rs")
def relative(self, path):
return Path(path).resolve().relative_to(self.root.resolve()).as_posix()
def place(self, path, module_dir, seen):
"""Walk the module tree from a file: record each declared module's file, parent and name."""
if path in seen:
return
seen.add(path)
parsed = self.parse(path)
if parsed is None:
return
for name, nesting, attribute, test in parsed["declarations"]:
if attribute is not None:
if nesting:
continue # a #[path] inside an inline module: unplaced, so always kept
candidates = [(self.root / path).parent / attribute]
else:
folder = module_dir.joinpath(*nesting)
candidates = [folder / f"{name}.rs", folder / name / "mod.rs"]
for candidate in candidates:
if candidate.is_file():
child = self.relative(candidate)
self.parent.setdefault(child, path)
self.module_name.setdefault(child, name)
self.root_of.setdefault(child, self.root_of[path])
if test or path in self.test_only:
self.test_only.add(child)
child_dir = candidate.parent if candidate.name == "mod.rs" or attribute is not None \
else candidate.parent / candidate.stem
self.place(child, child_dir, seen)
break
def parse(self, path):
if path in self.files:
return self.files[path]
try:
data = (self.root / path).read_bytes()
except OSError:
self.files[path] = None
return None
digest = hashlib.sha256(data).hexdigest()
self.used.add(digest)
if digest in self.cache:
self.files[path] = parsed = self.cache[digest]
return parsed
raw = data.decode("utf-8", errors="replace")
code = self.rust_code(raw) # same length as raw: literals and comments blanked
opening = {match.end() - 1: match.group(1) for match in INLINE_MOD.finditer(code)}
events = sorted([(match.start(), match) for match in BRACE.finditer(code)]
+ [(match.start(), match) for match in MOD_DECLARATION.finditer(code)], key=lambda event: event[0])
declarations, stack, depth = [], [], 0
for index, match in events:
if match.group() == "{":
depth += 1
if index in opening:
stack.append((opening[index], depth))
elif match.group() == "}":
if stack and stack[-1][1] == depth:
stack.pop()
depth -= 1
else:
window = " ".join(raw[max(0, index - 300):match.end()].split())
attribute = PATH_ATTRIBUTE.search(window)
declarations.append([match.group(1), [name for name, _ in stack], attribute.group(1) if attribute else None,
bool(TEST_MODULE.search(window))])
items = []
parse_items(code, 0, len(code), items)
self.files[path] = parsed = {
"declarations": declarations,
"items": items,
"glob": bool(GLOB_IMPORT.search(code)),
"file_names": sorted(set(FILE_NAME.findall(raw))),
}
self.cache[digest] = parsed
return parsed
def saved_cache(self):
"""The parses of the files this graph read, to keep for the next run."""
return {digest: self.cache[digest] for digest in self.used if digest in self.cache}
def requirement(self, path, item):
"""For an impl: for its trait and for its type, [names the file defines, other workspace names], one of
which must be a reached type or trait (of this file, of a crate it sees) before the impl can matter; both
empty: nothing of the workspace is needed. A name is the workspace's when the path naming it starts in the
workspace, or the file defines it, imports it from the workspace or may glob-import it; otherwise (std's,
another crate's, the prelude's) it needs nothing."""
if "requires" in item:
return item["requires"]
if self.workspace_heads is None:
self.workspace_heads = {"crate", "self", "super"} | set(self.module_name.values()) | {
manifest.parent.name.replace("-", "_") for manifest in (self.root / "crates").glob("*/Cargo.toml")}
parsed = self.parse(path)
defined = {name for other in parsed["items"] for name in other.get("names", ()) if other["kind"] != "use"}
workspace_imports = {name for other in parsed["items"] if other["kind"] == "use"
and other["head"] in self.workspace_heads for name in other["names"]}
parameters, subject = impl_subject(item["subject"])
parts = []
for part in re.split(r"\bfor\b", subject, maxsplit=1):
names, own = set(), set()
for match in QUALIFIED.finditer(part):
segments_ = [segment.strip() for segment in match.group().split("::")]
name = segments_[-1]
if name in parameters or name in KEYWORDS:
continue
if len(segments_) > 1:
if segments_[0] in self.workspace_heads:
names.add(name)
elif name in defined:
own.add(name)
elif name in workspace_imports or (parsed["glob"] and name not in STD_NAMES):
names.add(name)
parts.append([sorted(own), sorted(names)])
item["requires"] = parts
return parts
def index(self):
"""Lookup tables over every placed source, built once: which crates each crate sees, the nodes (items, an
inherent impl's members) by the names they define and the types they need, the modules by name."""
if self.tables is not None:
return self.tables
idents = {manifest.parent.name.replace("-", "_"): manifest.parent.name
for manifest in (self.root / "crates").glob("*/Cargo.toml")}
paths = [path for path in self.files if self.files[path] is not None and path not in self.test_only]
sees = {}
for path in paths:
named = {word for item in self.files[path]["items"] for word in item["mentions"]} & idents.keys()
sees.setdefault(crate_of(path), {crate_of(path)}).update(idents[name] for name in named)
changed = True
while changed:
changed = False
for crate, seen in sees.items():
wider = set().union(*(sees.get(other, {other}) for other in seen))
if not wider <= seen:
seen |= wider
changed = True
seen_by = {} # crate -> the crates that see it
for crate, seen in sees.items():
for other in seen:
seen_by.setdefault(other, set()).add(crate)
# What a call of a workspace macro defines: what its definition's body defines.
expands = {}
for path in paths:
for item in self.files[path]["items"]:
if item["kind"] == "macro":
for name in item["names"]:
expands.setdefault(name, set()).update(item["defines"])
names_of, by_name, by_file_name, by_type, by_module, children, seeds = {}, {}, {}, {}, {}, {}, []
for path in paths:
crate = crate_of(path)
if path in self.module_name:
by_module.setdefault((crate, self.module_name[path]), []).append(path)
children.setdefault(self.parent[path], []).append(path)
for index, item in enumerate(self.files[path]["items"]):
node = (path, index, None)
names = set(item.get("names", ()))
if item["kind"] == "macro_call":
names |= expands.get(item["macro"], set())
names_of[node] = names
if item["kind"] == "glue":
seeds.append(node)
elif item["kind"] == "macro_call" and not names:
# A macro call defining nothing by name may still expand to impls: it counts once a type or
# trait it mentions is reached.
for name in item["mentions"]:
by_type.setdefault(name, []).append(node)
elif item["kind"] == "impl":
required = {name for own, foreign in self.requirement(path, item) for name in own + foreign}
members = [(path, index, member) for member in range(len(item["members"]))] if item["inherent"] else []
for member_node in members:
name = item["members"][member_node[2]]["name"]
names_of[member_node] = set()
if name:
by_name.setdefault(name, []).append(member_node)
for name in required:
for other in [node] + members:
by_type.setdefault(name, []).append(other)
if not required:
seeds.append(node)
seeds += [member_node for member_node in members if not item["members"][member_node[2]]["name"]]
else:
for name in names:
by_name.setdefault(name, []).append(node)
by_file_name.setdefault((path, name), []).append(node)
self.tables = dict(idents=idents, sees=sees, seen_by=seen_by, names_of=names_of, by_name=by_name,
by_file_name=by_file_name, by_type=by_type, by_module=by_module, children=children,
seeds=seeds, node_sets={})
return self.tables
def reachable(self, entries, universe, reasons=None):
"""The files of universe (workspace Rust sources) holding an item reachable from entries (by the rules in
this module's doc), and the entries. reasons, a dict, gets why each file was reached (for debugging)."""
# Searching every source of the crates involved (not only those Cargo listed) only adds reach, and lets
# harnesses with the same entries (the game binary's) share one search.
crates = frozenset(crate_of(path) for path in universe)
key = (frozenset(entries), crates)
if reasons is None and key in self.results:
return self.results[key] & set(universe)
everything = {path for path in self.files if crate_of(path) in crates} | set(universe)
files = self.search(entries, everything, reasons)
self.results[key] = files
return files & set(universe)
def search(self, entries, universe, reasons):
targets = {self.target(path) for path in entries}
universe = {path for path in universe if (self.is_library(path) or self.target(path) in targets)
and path not in self.test_only}
tables = self.index()
idents, sees, seen_by, names_of = tables["idents"], tables["sees"], tables["seen_by"], tables["names_of"]
node_sets = tables["node_sets"]
by_name, by_file_name, by_type = tables["by_name"], tables["by_file_name"], tables["by_type"]
by_module, children = tables["by_module"], tables["children"]
seeds = [node for node in tables["seeds"] if node[0] in universe]
for path in universe:
if (path in entries or (path not in self.parent and path not in self.roots)) and self.parse(path):
seeds += [(path, index, None) for index in range(len(self.parse(path)["items"]))]
def names(node):
return names_of.get(node, set())
def sets_of(node, item):
"""A node's mentions, path words, qualified names and macro calls, as sets (parses keep lists)."""
if node not in node_sets:
source = item["members"][node[2]] if node[2] is not None else item
node_sets[node] = {key: set(source.get(key, ())) for key in ("mentions", "paths", "qualified", "invokes")}
return node_sets[node]
def item_of(node):
return self.parse(node[0])["items"][node[1]]
reached, opened, globbed, files, queue = set(), set(), set(), set(), []
# name -> the crates whose reached code mentions it plainly, names it in a path or an import, invokes it.
mentioned, qualified, invoked = {}, {}, {}
done = {"mentions": {}, "qualified": {}, "invokes": {}, "paths": {}} # crate -> names already handled
path_names, types = {}, {} # by crate
local = {} # file -> plain mentions by reached code in it or below it (child modules may `use super::*`)
local_types = {} # file -> its reached types and traits
def heard(table, name, crate):
"""Whether a crate that can see crate's items has name in table."""
sources = table.get(name)
return bool(sources) and not sources.isdisjoint(seen_by.get(crate, {crate}))
def ready(node):
path, index, member = node
item = item_of(node)
crate = crate_of(path)
if item["kind"] == "impl":
visible = visible_types(crate)
here = local_types.get(path, set())
if not all((not own and not foreign) or here.intersection(own) or visible.intersection(foreign)
for own, foreign in self.requirement(path, item)):
return False
name = item["members"][member]["name"] if member is not None else None
return name is None or heard(mentioned, name, crate) # a method: called by its name
if item["kind"] == "macro_call" and not names(node):
return not visible_types(crate).isdisjoint(item["mentions"])
names_ = names(node)
if item["kind"] == "macro":
return any(heard(invoked, name, crate) for name in names_)
if item["kind"] == "use" and item["glob"]:
return path in opened
nearby = local.get(path, ())
if any(name in nearby for name in names_):
return True # named in its own module, or below it
if item["kind"] == "use" and not item["public"]:
return False
if path in opened and any(heard(qualified, name, crate) for name in names_):
return True # named through a path, or imported
return path in globbed and any(heard(mentioned, name, crate) for name in names_)
visible_cache = {}
def visible_types(crate):
"""The reached types and traits of the crates a crate sees (cached until a type is reached)."""
if crate not in visible_cache:
visible_cache[crate] = set().union(*(types.get(other, set()) for other in sees.get(crate, {crate})))
return visible_cache[crate]
def wake(nodes, why):
for other in nodes:
if other not in reached and other[0] in universe and ready(other):
reach(other, why)
def open_module(path, why):
pending = [path]
while pending:
current = pending.pop()
if current in opened:
continue
opened.add(current)
crate = crate_of(current)
pending += [child for child in children.get(current, ())
if child in universe and self.module_name[child] in path_names.get(crate, ())]
parsed = self.parse(current)
wake([(current, index, None) for index, item in enumerate(parsed["items"] if parsed else ())
if item["kind"] != "impl"], f"{current} opened ({why})")
def reach(node, why):
if node in reached:
return
reached.add(node)
queue.append(node)
path, _, member = node
item = item_of(node)
if (item["kind"] == "use" and not item["public"]) or (item["kind"] == "impl" and item["inherent"] and member is None):
return
if path not in files:
files.add(path)
if reasons is not None:
reasons[path] = why
for path in universe:
if path.endswith("/src/lib.rs") or path in entries or path not in self.parent:
open_module(path, "crate root or entry")
for node in seeds:
reach(node, "entry" if node[0] in entries else "always counted")
for path in entries & universe:
files.add(path)
if reasons is not None:
reasons.setdefault(path, "entry")
while queue:
node = queue.pop()
path, index, member = node
item = item_of(node)
crate = crate_of(path)
label = (item.get("names") or [item["kind"]])[0] if member is None else item["members"][member]["name"]
source = sets_of(node, item)
why = f"{path} ({label})"
# Plain mentions reach items of this file and of the modules above it.
ancestor, new = path, source["mentions"]
while ancestor is not None and new:
known = local.setdefault(ancestor, set())
new = new - known
known |= new
for name in new:
wake(by_file_name.get((ancestor, name), ()), f"{why} mentions {name}")
ancestor = self.parent.get(ancestor)
if item["kind"] == "use" and item["glob"]:
for name in item["names"]:
for target in sees.get(crate, {crate}):
for module in list(by_module.get((target, name), ())) + \
([f"crates/{idents[name]}/src/lib.rs"] if name in idents else []):
if module not in globbed and module in universe:
globbed.add(module)
wake([(module, index, None) for index in range(len(self.parse(module)["items"]))]
if self.parse(module) else [], f"{why} glob-imports {name}")
visible_to = sees.get(crate, {crate})
new = source["paths"] - done["paths"].setdefault(crate, set())
done["paths"][crate] |= new
for name in new:
for target in visible_to:
path_names.setdefault(target, set()).add(name)
for module in by_module.get((target, name), ()):
if module in universe and self.parent[module] in opened:
open_module(module, f"{why} names {name}")
for key, table, wanted in (("qualified", qualified, lambda other: True),
("invokes", invoked, lambda other: item_of(other)["kind"] == "macro"),
("mentions", mentioned, lambda other: other[2] is not None or other[0] in globbed)):
new = source[key] - done[key].setdefault(crate, set())
done[key][crate] |= new
for name in new:
table.setdefault(name, set()).add(crate)
wake([other for other in by_name.get(name, ()) if crate_of(other[0]) in visible_to and wanted(other)],
f"{why} {key} {name}")
if item["kind"] in TYPE_KINDS and member is None:
own = types.setdefault(crate, set())
local_types.setdefault(path, set()).update(names(node))
if not names(node) <= own:
own |= names(node)
visible_cache.clear()
seeing = seen_by.get(crate, {crate})
for name in names(node): # also when the crate has one by that name already: impls of this one
wake([other for other in by_type.get(name, ()) if crate_of(other[0]) in seeing],
f"implements for {name}, defined in {path}")
return files
def impl_subject(header):
"""An impl header (the text after `impl`): its generic parameters' names, and the rest without its where clause
(the trait and the type it is for)."""
header, parameters = header.strip(), set()
if header.startswith("<"):
depth = 0
for index, character in enumerate(header):
depth += {"<": 1, ">": -1}.get(character, 0)
if depth == 0:
generics, header = header[1:index], header[index + 1:]
parameters = {match.group(1) for match in re.finditer(rf"(?:^|,)\s*(?:const\s+)?({IDENT})", generics)}
break
return parameters, re.split(r"\bwhere\b", header)[0]
def crate_of(path):
return path.split("/", 2)[1]
def is_workspace_rust(path):
parts = PurePosixPath(path).parts
return len(parts) > 2 and parts[0] == "crates" and path.endswith(".rs")
DECLARATION_LINE = re.compile(rf"\s*(?:pub(?:\([^)]*\))?\s+)?mod\s+({IDENT})\s*;")
def declarations(text, names, keep_rest):
"""A Rust file's `mod` declarations of names, each with the attribute lines just above it; with keep_rest, the
whole text except the declarations of other modules instead."""
lines, found, attributes = text.splitlines(), [], []
for line in lines:
if line.strip().startswith("#[") and not line.strip().startswith("#!["):
attributes.append(line.strip())
continue
match = DECLARATION_LINE.match(line)
if match and match.group(1) in names:
found += attributes + [line.strip()]
elif keep_rest and not match:
found += attributes + [line]
attributes = []
return "\n".join(found + attributes)
def declarations_changed(root, path, children, base, whole):
"""Whether a changed file differs from the commit base in what a harness depends on: for a module parent, its
`mod` declarations of the reached children; for a whole file (whole), everything but its declarations of other
modules (adding a module beside reached ones changes nothing). True without a base, or if either side is
missing."""
if base is None:
return True
try:
head = (Path(root) / path).read_text(encoding="utf-8", errors="replace")
before = subprocess.run(["git", "show", f"{base}:{path}"], cwd=root, capture_output=True, text=True, check=True).stdout
except (OSError, subprocess.CalledProcessError):
return True
return declarations(head, children, whole) != declarations(before, children, whole)
def narrow(dependencies, artifacts, graph, changed=()):
"""A harness's dependencies narrowed to what its entry points can reach: {dependency: (whole, children)}, where
whole says the file counts for its own content (else only through its `mod` declarations of children, as a
module ancestor of a reached file) and children are its reached child modules. Without known entry points (a
probe or binary the harness built that isn't found here), every dependency is kept whole."""
plain = [dependency.split("#keep:")[0] for dependency in dependencies]
crates = {PurePosixPath(path).parts[1] for path in plain if path.startswith("crates/")}
rust = {path for path in plain if is_workspace_rust(path)}
added = {path for path in changed if is_workspace_rust(path) and PurePosixPath(path).parts[1] in crates
and path not in rust and (graph.root / path).is_file()}
entries = graph.entries(artifacts, rust)
if entries is None:
return {dependency: (True, None) for dependency in dependencies}
missing = {path for path in rust if graph.parse(path) is None}
reached = graph.reachable(entries, rust | added | entries) | missing
named, named_in_crate = set(), {}
for path in rust | added:
parsed = graph.parse(path)
file_names = set(parsed["file_names"]) if parsed else set()
if path in reached:
named |= file_names
named_in_crate.setdefault(PurePosixPath(path).parts[1], set()).update(file_names)
kept = {}
for dependency, path in zip(dependencies, plain):
if is_workspace_rust(path):
if path in reached:
kept[dependency] = (True, set())
elif not path.startswith("crates/") or path.endswith(("Cargo.toml", "build.rs")) \
or PurePosixPath(path).name in named \
or PurePosixPath(path).name not in named_in_crate.get(PurePosixPath(path).parts[1], ()):
kept[dependency] = (True, None)
for path in sorted(added & reached):
kept[path] = (True, set())
for path in sorted(reached):
child = path
while child in graph.parent:
parent = graph.parent[child]
kept.setdefault(parent, (False, set()))[1].add(graph.module_name[child])
child = parent
return kept
+176
View File
@@ -0,0 +1,176 @@
#!/usr/bin/env python3
"""The prepared output's version: what keeps `OUTPUT_VERSION` (crates/horizon-prepare/src/lib.rs) honest.
The launcher copies a prepared version's resources (`.local/horizon-client/<version>`) to a newly installed
one when both pin the same Minecraft jars and their preparers declare the same `OUTPUT_VERSION`, so a release
that changes the preparer's code without changing what it makes asks nobody to prepare again. The number is
bumped by hand; this tool catches a change to the output without a bump.
crates/horizon-prepare/output-versions.txt lists every `OUTPUT_VERSION` with the digest of what it prepares.
It only grows: the last line is the current version, and a change to the output appends a line.
python3 tools/prepare_output.py digest [RESOURCES] the digest of a preparation
python3 tools/prepare_output.py check [--base SHA] [--cache] [RESOURCES]
fails when RESOURCES (default: this checkout's .local/horizon-client/<version>, as CI prepares it) is
not what the last line says, or when the file changed other than by appending since SHA. --cache keeps
the digest beside RESOURCES (CI: a preparation never changes once made; it takes ~30 s to compute)
The digest covers every file of the preparation, canonicalised where the preparer is not deterministic between
runs (verified across the CI runners): JSON is compared as data (key order, the order of each `tags` list of
creative-inventory.json, and the addresses in `$$Lambda/0x...` class names don't count); logs, compiled classes,
the sounds (copied from the player's launcher by another step) and the manifest's file hashes (raw bytes) don't
count. The manifest's own scalar fields (schema, data version, jar hashes, file count) do.
"""
from __future__ import annotations
import argparse
import hashlib
import json
import os
import re
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
VERSIONS = 'crates/horizon-prepare/output-versions.txt'
LIB = 'crates/horizon-prepare/src/lib.rs'
MANIFEST = 'horizon-resources.json'
LAMBDA = re.compile(r'\$\$Lambda/0x[0-9a-fA-F]+')
CONSTANT = re.compile(r'^pub const OUTPUT_VERSION: u32 = (\d+);', re.M)
def canonical_json(value, key=''):
if isinstance(value, dict):
return {k: canonical_json(v, k) for k, v in value.items()}
if isinstance(value, list):
items = [canonical_json(v, key) for v in value]
if key == 'tags' and all(isinstance(x, list) and all(isinstance(t, str) for t in x) for x in items):
items = [sorted(x) for x in items]
return items
if isinstance(value, str):
return LAMBDA.sub('$$Lambda', value)
return value
def canonical(rel: str, data: bytes) -> bytes:
if rel == MANIFEST:
manifest = json.loads(data)
scalars = {k: v for k, v in manifest.items() if not isinstance(v, (dict, list))}
return json.dumps(scalars, sort_keys=True).encode()
if rel.endswith('.json'):
try:
value = json.loads(data)
except ValueError:
return data
return json.dumps(canonical_json(value), sort_keys=True).encode()
return data
def skipped(rel: str) -> bool:
parts = rel.split('/')
return rel.endswith(('.log', '.class')) or parts[:3] == ['assets', 'minecraft', 'sounds'] \
or rel == 'assets/minecraft/sounds.json'
def digest(resources: Path) -> tuple[str, int]:
"""The digest of a preparation, and how many files it covers."""
if not (resources / MANIFEST).is_file():
raise SystemExit(f'{resources} is not a preparation: no {MANIFEST}')
files = []
for dirpath, dirs, names in os.walk(resources):
dirs.sort()
for name in names:
path = Path(dirpath) / name
rel = path.relative_to(resources).as_posix()
if path.is_symlink() or skipped(rel):
continue
files.append(rel)
total = hashlib.sha256()
for rel in sorted(files):
data = hashlib.sha256(canonical(rel, (resources / rel).read_bytes())).hexdigest()
total.update(f'{rel}\0{data}\n'.encode())
return total.hexdigest(), len(files)
def parse_versions(text: str) -> list[tuple[int, str]]:
out = []
for line in text.splitlines():
line = line.strip()
if not line or line.startswith('#'):
continue
number, value = line.split()
out.append((int(number), value))
return out
def problems(versions: list[tuple[int, str]], constant: int | None) -> list[str]:
found = []
if not versions:
return [f'{VERSIONS} lists no version']
if [n for n, _ in versions] != list(range(1, len(versions) + 1)):
found.append(f'{VERSIONS}: the versions must be 1, 2, 3... in order')
if len({d for _, d in versions}) != len(versions):
found.append(f'{VERSIONS}: two versions with the same digest')
if constant != versions[-1][0]:
found.append(f'{LIB} says OUTPUT_VERSION = {constant}, {VERSIONS} ends at {versions[-1][0]}')
return found
def default_resources() -> Path:
lock = json.loads((ROOT / 'research-lock.json').read_text())
return ROOT / '.local/horizon-client' / lock['minecraft']['version']
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__.split('\n\n')[0])
parser.add_argument('command', choices=['digest', 'check'])
parser.add_argument('resources', nargs='?', type=Path)
parser.add_argument('--base', help='the commit the change is based on (check: the file only grew since)')
parser.add_argument('--cache', action='store_true', help='keep the digest beside the preparation')
args = parser.parse_args()
resources = (args.resources or default_resources()).resolve()
tool = hashlib.sha256(Path(__file__).read_bytes()).hexdigest()[:16]
cache = resources.parent / f'.output-digest-{resources.name}-{tool}'
if args.cache and cache.is_file():
value, count = cache.read_text().split()
else:
value, count = digest(resources)
if args.cache:
cache.write_text(f'{value} {count}\n')
if args.command == 'digest':
print(value)
return 0
versions = parse_versions((ROOT / VERSIONS).read_text())
match = CONSTANT.search((ROOT / LIB).read_text())
found = problems(versions, int(match.group(1)) if match else None)
if args.base:
shown = subprocess.run(['git', 'show', f'{args.base}:{VERSIONS}'], cwd=ROOT, capture_output=True, text=True)
if shown.returncode == 0:
before = parse_versions(shown.stdout)
if versions[:len(before)] != before:
found.append(f'{VERSIONS} changed lines it already had (since {args.base[:12]}): '
'a new output gets a new version, appended')
current = versions[-1] if versions else (0, '')
if value != current[1]:
known = [n for n, d in versions if d == value]
if known:
found.append(f'the preparation is OUTPUT_VERSION {known[0]}\'s output, but OUTPUT_VERSION is {current[0]}')
else:
found.append(
f'the prepared output changed ({count} files, digest {value}) but OUTPUT_VERSION is still '
f'{current[0]}. Players would keep resources prepared by the old preparer. Bump it:\n'
f' set `pub const OUTPUT_VERSION: u32 = {current[0] + 1};` in {LIB}\n'
f' append `{current[0] + 1} {value}` to {VERSIONS}\n'
' (if the change is only a nondeterminism this tool should ignore, teach canonical() instead)')
for problem in found:
print(f'::error::{problem}' if os.environ.get('GITHUB_ACTIONS') else f'error: {problem}', file=sys.stderr)
if not found:
print(f'prepared output: OUTPUT_VERSION {current[0]} ({count} files, {value[:16]})')
return 1 if found else 0
if __name__ == '__main__':
sys.exit(main())
+26 -8
View File
@@ -414,13 +414,26 @@ def gather(kind, head, releases=None):
return last, prs
def green_main_head():
"""The newest commit on main whose checks passed: what an automatic release builds."""
runs = gh_json("run", "list", "--repo", REPO, "--workflow", "checks", "--branch", "main", "--event", "push",
def passed_on(workflow, sha):
"""Whether the newest finished push run of workflow on main for commit sha passed."""
runs = gh_json("run", "list", "--repo", REPO, "--workflow", workflow, "--branch", "main", "--event", "push",
"--commit", sha, "--limit", "5", "--json", "status,conclusion")
done = [entry for entry in runs if entry["status"] == "completed" and entry["conclusion"] != "cancelled"]
return bool(done) and done[0]["conclusion"] == "success"
def green_main_head(parity=False):
"""The newest commit on main whose checks passed: what an automatic release builds. With parity (game
releases), the newest commit parity-main passed on, if its checks passed too: parity-main runs on batches of
merges, so the commits a run skipped are covered by the next one, and a superseded run counts for nothing."""
workflow = "parity-main" if parity else "checks"
runs = gh_json("run", "list", "--repo", REPO, "--workflow", workflow, "--branch", "main", "--event", "push",
"--limit", "10", "--json", "headSha,status,conclusion")
for entry in runs:
if entry["status"] == "completed":
return entry["headSha"] if entry["conclusion"] == "success" else None
if entry["status"] == "completed" and entry["conclusion"] != "cancelled":
if entry["conclusion"] != "success":
return None
return entry["headSha"] if not parity or passed_on("checks", entry["headSha"]) else None
return None
@@ -437,7 +450,7 @@ def main():
parser.add_argument("--kind", choices=["game", "launcher"], required=True)
parser.add_argument("--head", help="the commit to release (default: the newest commit on main whose checks passed)")
parser.add_argument("--tag", help="notes: the release's tag")
parser.add_argument("--force", action="store_true", help="release even without player-facing changes, before merges settle or while paused")
parser.add_argument("--force", action="store_true", help="release even without player-facing changes, before merges settle, while paused or before parity-main passed")
parser.add_argument("--out", type=Path, help="notes: write title.txt and body.md here")
parser.add_argument("--now", help="the time to decide at (ISO 8601; default now)")
parser.add_argument("--smoke", type=Path, help="gate: the release smoke test's report (smoke-test.json)")
@@ -456,9 +469,12 @@ def main():
summary.write(f"### {line}\n")
return 1 if problem else 0
head = args.head or green_main_head()
# Game releases ship only a commit the vanilla harnesses passed on (parity-main, docs/release-flow.md).
parity = args.kind == "game" and args.command == "decide" and not args.force
head = args.head or green_main_head(parity)
if not head:
result = {"release": False, "reason": "main's latest checks did not pass"}
result = {"release": False, "reason": "main's latest checks did not pass" + (
" or parity-main did not pass on them" if parity else "")}
print(json.dumps(result, indent=1))
write_outputs({"release": "false", "reason": result["reason"]})
return 0
@@ -482,6 +498,8 @@ def main():
blockers = open_blockers() if args.kind == "game" else []
release, reason = decision(args.kind, prs, now, os.environ.get("RELEASES_PAUSED") == "1", args.force, new_line,
blockers)
if release and args.head and parity and not (passed_on("parity-main", head) and passed_on("checks", head)):
release, reason = False, f"waiting for checks and parity-main to pass on {head[:9]}"
minor = any("feature" in pr["labels"] for pr in prs if pr["paths"])
tag = next_game_tag(tags, now.date(), line) if args.kind == "game" else next_launcher_tag(tags, minor, line)
result = {
+3
View File
@@ -5,6 +5,7 @@ import json
import os
from pathlib import Path
import platform
import shutil
import subprocess
import tempfile
@@ -37,6 +38,8 @@ def main():
if args.prepare:
lab.prepare()
lock, jars, libraries = test_rust_parity.verify_artifacts()
for earlier in directory.glob("run-*"): # one run per run used to stay behind: 5 GB here after a few weeks
shutil.rmtree(earlier, ignore_errors=True)
base = Path(tempfile.mkdtemp(prefix="run-", dir=directory))
oracle = lab.ROOT / "mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCollisionOracle.java"
sources = [oracle, Path(__file__).resolve(), lab.ROOT / "tools/test_rust_parity.py", lab.ROOT / "Cargo.toml", lab.ROOT / "Cargo.lock", lab.ROOT / "crates/horizon-core/Cargo.toml", lab.ROOT / "crates/horizon-world/Cargo.toml", *sorted((lab.ROOT / "crates/horizon-core/src").rglob("*.rs")), *sorted((lab.ROOT / "crates/horizon-world/src").rglob("*.rs")), lab.ROOT / "crates/horizon-world/examples/probe/collision_fixture.rs"]
+3
View File
@@ -9,6 +9,7 @@ from pathlib import Path
import platform
import random
import struct
import shutil
import subprocess
import tempfile
@@ -273,6 +274,8 @@ def main():
if args.prepare:
lab.prepare()
lock, jars, libraries = test_rust_parity.verify_artifacts()
for earlier in directory.glob("run-*"): # one run per run used to stay behind: 5 GB here after a few weeks
shutil.rmtree(earlier, ignore_errors=True)
base = Path(tempfile.mkdtemp(prefix="run-", dir=directory))
commands = list(corpus())
inputs = base / "inputs.txt"
+3
View File
@@ -5,6 +5,7 @@ import json
import os
from pathlib import Path
import platform
import shutil
import subprocess
import tempfile
@@ -35,6 +36,8 @@ def main():
if args.prepare:
lab.prepare()
lock, jars, libraries = test_rust_parity.verify_artifacts()
for earlier in directory.glob("run-*"): # one run per run used to stay behind: 5 GB here after a few weeks
shutil.rmtree(earlier, ignore_errors=True)
base = Path(tempfile.mkdtemp(prefix="run-", dir=directory))
classes = base / "classes"
classes.mkdir()