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
6 changes: 6 additions & 0 deletions docs/performance/agent-iteration.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ The reader requires passed cases, matching summaries, and exact case identities.
PowerShell command still returns the same case and run objects to the acceptance runner and host
collector. PowerShell owns process execution, artifact provenance, and receipt validation.

The frozen player inventory uses the same locked Python worker for declared files, built player
payloads, and replay project inputs. It checks the tool checkout before and after each request and
detects persistent changes to files it has hashed. On Windows, read handles prevent writes and
replacement through the final inventory recheck. PowerShell still produces the provenance receipts
and supplies their ordered path, kind, length, and SHA-256 rows.

The approved organization policy requires a static version axis to prove matching editor setup
and license return. The workflow retains that literal universe and uses
[matrix exclusions](https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-syntax#jobsjob_idstrategymatrixexclude)
Expand Down
86 changes: 86 additions & 0 deletions scripts/tests/test_frozen_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,3 +682,89 @@ def literal(path: Path) -> str:
assert reply["third"] == reply["second"]
assert reply["stopped"] is True
assert reply["workerExited"] is True


def test_player_inventory_keeps_sorted_rows_and_declared_refusal(
tmp_path: Path,
) -> None:
digest = hashlib.sha256(b"content").hexdigest()
write(tmp_path / "one" / "entry.txt", b"content")
(tmp_path / "empty").mkdir()
result = frozen_files.frozen_player_inventory(tmp_path, None, 4096)
assert result == {
"rows": [
{"Path": "empty", "Kind": "directory", "Length": None, "Sha256": None},
{"Path": "one", "Kind": "directory", "Length": None, "Sha256": None},
{"Path": "one/entry.txt", "Kind": "file", "Length": 7, "Sha256": digest},
],
"error": None,
}
declared = [{"Path": "one/entry.txt", "Sha256": "0" * 64}]
outcome = frozen_files.frozen_player_inventory(tmp_path, declared, 1)
assert outcome["rows"] == result["rows"][2:]
assert outcome["error"] == "Frozen declared file hash does not match: one/entry.txt"


@pytest.mark.parametrize("declared", [False, True])
def test_player_inventory_refuses_persistent_same_length_change(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, declared: bool
) -> None:
first = write(tmp_path / "01.txt", b"AAAA")
second = write(tmp_path / "02.txt", b"CCCC")
rows = [
{"Path": path.name, "Sha256": hashlib.sha256(path.read_bytes()).hexdigest()}
for path in (first, second)
]
original_hash = frozen_files.file_hash

def hash_then_change(path: Path, observed: stat_result | None = None) -> tuple[str, int]:
if path == second:
if sys.platform == "win32":
with pytest.raises(PermissionError):
first.write_bytes(b"BBBB")
else:
first.write_bytes(b"BBBB")
return original_hash(path, observed)

monkeypatch.setattr(frozen_files, "file_hash", hash_then_change)
if sys.platform == "win32":
result = frozen_files.frozen_player_inventory(tmp_path, rows if declared else None, 2)
assert result["error"] is None
assert first.read_bytes() == b"AAAA"
else:
with pytest.raises(ValueError, match="changed during inventory"):
frozen_files.frozen_player_inventory(tmp_path, rows if declared else None, 2)
assert first.read_bytes() == b"BBBB"


def test_player_inventory_request_rechecks_the_checkout(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
write(tmp_path / "file", b"data")
original_git = frozen_files.git_output
head_reads = 0

def git_with_changed_head(root: Path, arguments: list[str]) -> bytes:
nonlocal head_reads
if arguments == ["rev-parse", "--verify", "HEAD"]:
head_reads += 1
if head_reads == 2:
return b"0" * 40 + b"\n"
return original_git(root, arguments)

monkeypatch.setattr(frozen_files, "git_output", git_with_changed_head)
request = {
"protocol": 1,
"requestId": "1" * 32,
"operation": "inventory",
"root": str(tmp_path),
"repository": str(ROOT),
"declaredFiles": None,
"maximumFiles": 4096,
}
with pytest.raises(ValueError, match="checkout changed during the request"):
frozen_files.execute_request(request)
assert head_reads == 2
request["repository"] = str(ROOT / "scripts")
with pytest.raises((ValueError, FileNotFoundError)):
frozen_files.execute_request(request)
176 changes: 167 additions & 9 deletions scripts/unity/frozen_files.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Inventory exact WProto source bytes without following filesystem redirects."""
"""Inventory WProto sources and frozen player inputs without following filesystem redirects."""

from __future__ import annotations

Expand All @@ -13,7 +13,7 @@
import subprocess
import sys
import uuid
from contextlib import contextmanager
from contextlib import ExitStack, contextmanager
from functools import cache
from pathlib import Path
from typing import TYPE_CHECKING, TypedDict, cast
Expand Down Expand Up @@ -143,9 +143,20 @@ def windows_kernel32() -> ctypes.WinDLL:

def _open_windows_handle(path: Path) -> int:
kernel = windows_kernel32()
generic_read, share_read, open_existing, ordinary_attributes = 0x80000000, 1, 3, 0x80
generic_read, share_read, open_existing, ordinary_attributes = (
0x80000000,
1,
3,
0x80,
)
handle: int | None = kernel.CreateFileW(
str(path), generic_read, share_read, None, open_existing, ordinary_attributes, None
str(path),
generic_read,
share_read,
None,
open_existing,
ordinary_attributes,
None,
)
if handle is None or handle == ctypes.c_void_p(-1).value:
raise ctypes.WinError(ctypes.get_last_error())
Expand Down Expand Up @@ -285,7 +296,15 @@ def host_source_files(repository: Path) -> list[HostSourceFile]:
raise ValueError("Host source census requires the Git repository root.")
listing = git_output(
root,
["ls-files", "--cached", "--others", "--exclude-standard", "-z", "--", *HOST_GIT_INPUTS],
[
"ls-files",
"--cached",
"--others",
"--exclude-standard",
"-z",
"--",
*HOST_GIT_INPUTS,
],
)
paths = {
path.decode("utf-8", errors="surrogateescape") for path in listing.split(b"\0") if path
Expand All @@ -311,29 +330,168 @@ def host_source_files(repository: Path) -> list[HostSourceFile]:
]


class InventoryRow(TypedDict):
Path: str
Kind: str
Length: int | None
Sha256: str | None


class InventoryResult(TypedDict):
rows: list[InventoryRow]
error: str | None


def _inventory_checkout(repository: Path) -> tuple[Path, bytes]:
root = regular_path(repository, directory=True)
tool = regular_path(root / "scripts/unity/frozen_files.py", directory=False)
actual_root = Path(
os.fsdecode(git_output(root, ["rev-parse", "--show-toplevel"]).rstrip(b"\r\n"))
)
if actual_root != root or Path(os.path.abspath(__file__)) != tool: # noqa: PTH100
raise ValueError("Frozen inventory tool does not belong to the requested checkout.")
revision = git_output(root, ["rev-parse", "--verify", "HEAD"]).strip()
if not re.fullmatch(rb"[a-f0-9]{40}", revision):
raise ValueError("Frozen inventory checkout has no valid commit identity.")
return root, revision


def _declared_hashes(declaration: object, maximum: int) -> dict[str, str]:
if not isinstance(declaration, list):
raise ValueError("Frozen input lists must be arrays.")
declarations = cast("list[object]", declaration)
if not 1 <= len(declarations) <= maximum:
raise ValueError(f"Frozen input lists require between 1 and {maximum} files.")
result: dict[str, str] = {}
for row in declarations:
if not isinstance(row, dict):
raise ValueError("Frozen input paths and hashes must be scalar strings.")
entry = cast("dict[str, object]", row)
relative, digest = entry.get("Path"), entry.get("Sha256")
if not isinstance(relative, str) or not isinstance(digest, str):
raise ValueError("Frozen input paths and hashes must be scalar strings.")
if (
not relative.strip()
or len(ordinal_key(relative)) > 2048
or re.search(r'[\\:<>"|?*\x00-\x1f]', relative)
or any(
part in ("", ".", "..") or part.endswith((".", " ")) for part in relative.split("/")
)
or not re.fullmatch(r"[a-f0-9]{64}", digest)
or relative in result
Comment thread
wallstop marked this conversation as resolved.
Resolved
):
raise ValueError(f"Invalid or colliding frozen input path/hash: {relative}")
result[relative] = digest
return result


def _payload_states(root: Path) -> dict[str, tuple[bool, os.stat_result]]:
states = {
path.relative_to(root).as_posix(): (directory, info) for path, directory, info in walk(root)
}
if any("\\" in name for name in states):
raise ValueError("Frozen payload contains a backslash in a relative path.")
if not any(not directory for directory, _ in states.values()):
raise ValueError("Frozen payload contains no files.")
return states


def frozen_player_inventory(root_path: Path, declaration: object, maximum: int) -> InventoryResult:
root = regular_path(root_path, directory=True)
before_root = ordinary(root, directory=True)
declared = None if declaration is None else _declared_hashes(declaration, maximum)
payload = _payload_states(root) if declared is None else {}
rows: list[InventoryRow] = []
observed: dict[Path, os.stat_result] = {}
problem: str | None = None
with ExitStack() as retained:
for relative in sorted(payload if declared is None else declared, key=ordinal_key):
path = root / relative
try:
if declared is None:
directory, info = payload[relative]
else:
info = path.lstat()
if stat.S_ISDIR(info.st_mode):
raise ValueError(f"Frozen input is not a file: {relative}")
regular_path(path, directory=False)
directory = False
if directory:
rows.append(
InventoryRow(Path=relative, Kind="directory", Length=None, Sha256=None)
)
continue
if sys.platform == "win32":
retained.enter_context(retain_source(path))
digest, size = file_hash(path, info)
observed[path] = info
rows.append(InventoryRow(Path=relative, Kind="file", Length=size, Sha256=digest))
if declared is not None and declared[relative] != digest:
raise ValueError(f"Frozen declared file hash does not match: {relative}")
except (OSError, ValueError) as error:
problem = str(error)
break
regular_path(root, directory=True)
after_root = ordinary(root, directory=True)
if declared is None:
current = _payload_states(root)
if (
current.keys() != payload.keys()
or identity(before_root) != identity(after_root)
or any(
identity(state) != identity(current[name][1])
for name, (_, state) in payload.items()
)
):
raise ValueError("Frozen player payload changed during inventory.")
elif (before_root.st_dev, before_root.st_ino) != (
after_root.st_dev,
after_root.st_ino,
):
raise ValueError("Frozen player inventory root changed.")
for path, info in observed.items():
if declared is not None:
regular_path(path, directory=False)
if identity(info) != identity(ordinary(path, directory=False)):
raise ValueError(f"Frozen file changed during inventory: {path.relative_to(root)}")
return InventoryResult(rows=rows, error=problem)


def execute_request(value: object) -> object:
if not isinstance(value, dict):
raise ValueError("Frozen inventory requests must be objects.")
request = cast("dict[str, object]", value)
if set(request) - {"protocol", "requestId", "operation", "root"}:
operation = request.get("operation")
allowed = {"protocol", "requestId", "operation", "root"}
if operation == "inventory":
allowed.update(("repository", "declaredFiles", "maximumFiles"))
if set(request) - allowed:
raise ValueError("Source census request has unknown fields.")
protocol = request.get("protocol")
identifier = request.get("requestId")
operation = request.get("operation")
root = request.get("root")
if (
type(protocol) is not int
or protocol != 1
or not isinstance(identifier, str)
or not re.fullmatch(r"[a-f0-9]{32}", identifier)
or operation not in ("sources", "host-sources")
or operation not in ("sources", "host-sources", "inventory")
or not isinstance(root, str)
or not root.strip()
):
raise ValueError("Frozen inventory request has invalid fields.")
if operation == "sources":
return source_files(Path(root))
return host_source_files(Path(root))
if operation == "host-sources":
return host_source_files(Path(root))
repository, maximum = request.get("repository"), request.get("maximumFiles")
if not isinstance(repository, str) or not repository.strip() or type(maximum) is not int:
raise ValueError("Frozen inventory checkout or file bound is invalid.")
checkout = _inventory_checkout(Path(repository))
result = frozen_player_inventory(Path(root), request.get("declaredFiles"), maximum)
if _inventory_checkout(Path(repository)) != checkout:
raise ValueError("Frozen inventory checkout changed during the request.")
return result


def request_identifier(value: object) -> object:
Expand Down
Loading
Loading