From 9b2957fbe43975f9e3ef5a2a8543cd3db3913033 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Mon, 23 Nov 2026 23:38:31 +0100 Subject: [PATCH] fix: mark a standalone record from its loaded code --- schemas/core-probe-record.schema.json | 4 ++++ scripts/probes/standalonerecord.py | 29 +++++++++++++++++++-------- tests/test_standalone_probe.py | 28 +++++++++++++++++++++----- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/schemas/core-probe-record.schema.json b/schemas/core-probe-record.schema.json index 9a6593280..d1232c048 100644 --- a/schemas/core-probe-record.schema.json +++ b/schemas/core-probe-record.schema.json @@ -170,6 +170,10 @@ }, "script_sha256": { "$ref": "#/$defs/sha256" + }, + "core_probe_sha256": { + "$ref": "#/$defs/sha256", + "description": "standalone_probe.py: the sha256 of scripts/core_probe.py, whose code judges the run." } } }, diff --git a/scripts/probes/standalonerecord.py b/scripts/probes/standalonerecord.py index 059428423..3ba61cf1a 100644 --- a/scripts/probes/standalonerecord.py +++ b/scripts/probes/standalonerecord.py @@ -23,11 +23,8 @@ RECORD_VERSION = 4 OBSERVABLES = ("verdict", "screens", "frame_hashes", "uniform_frames") SCRIPT = "scripts/standalone_probe.py" SCRIPT_PATH = REPO_ROOT / SCRIPT -# The files that hold the code of a run: a record from a checkout where one -# of them differs from the commit says so. -CODE_FILES = (SCRIPT, "scripts/probes/standaloneemulators.py", "scripts/probes/appimage.py", - "scripts/probes/xvfb.py", "scripts/probes/standalonerun.py", - "scripts/probes/standalonerecord.py", "scripts/probes/standalonemodes.py") +SCRIPTS_DIR = REPO_ROOT / "scripts" +CORE_PROBE = "scripts/core_probe.py" class RunFailed(Exception): @@ -39,13 +36,29 @@ class Busy(Exception): """The host's load is past the threshold a recorded run accepts.""" +def code_files() -> list[str]: + """The files that hold the code of a run: every module of the checkout's + scripts/ the process has loaded, as paths from the checkout root.""" + files = {SCRIPT, CORE_PROBE} + for module in list(sys.modules.values()): + name = getattr(module, "__file__", None) + if not name: + continue + path = Path(name).resolve() + if path.is_relative_to(SCRIPTS_DIR): + files.add(path.relative_to(REPO_ROOT).as_posix()) + return sorted(files) + + def frontend_identity() -> dict: - """core_probe's frontend identity of the command, modified when any file - that holds the run's code differs from the commit.""" + """core_probe's frontend identity of the command, with core_probe's own + sha256, modified when any file that holds the run's code differs from the + commit.""" identity = cp.frontend_identity(SCRIPT_PATH, SCRIPT) + identity["core_probe_sha256"] = cp.file_digest(cp.SCRIPT_PATH, "sha256") if identity["script_modified"] is False: try: - status = subprocess.run(["git", "status", "--porcelain", "--", *CODE_FILES], cwd=REPO_ROOT, + status = subprocess.run(["git", "status", "--porcelain", "--", *code_files()], cwd=REPO_ROOT, capture_output=True, text=True, check=True).stdout except (OSError, subprocess.CalledProcessError) as exc: print(f"WARNING: frontend state unknown, git failed: {exc}", file=sys.stderr) diff --git a/tests/test_standalone_probe.py b/tests/test_standalone_probe.py index f3474356a..aaa13799b 100644 --- a/tests/test_standalone_probe.py +++ b/tests/test_standalone_probe.py @@ -185,14 +185,32 @@ class Frontend(unittest.TestCase): changed = mock.Mock(stdout=" M scripts/probes/xvfb.py\n") with mock.patch.object(cp, "frontend_identity", return_value=dict(clean)), \ mock.patch.object(standalonerecord.subprocess, "run", return_value=changed) as run: - self.assertTrue(standalonerecord.frontend_identity()["script_modified"]) - self.assertEqual(run.call_args.args[0][4:], list(standalonerecord.CODE_FILES)) + identity = standalonerecord.frontend_identity() + self.assertTrue(identity["script_modified"]) + self.assertEqual(identity["core_probe_sha256"], cp.file_digest(ROOT / "scripts/core_probe.py", "sha256")) + self.assertEqual(run.call_args.args[0][4:], standalonerecord.code_files()) + + def test_the_code_files_are_every_module_the_command_imports(self) -> None: + import json + import subprocess + import sys - def test_the_code_files_are_the_modules_of_the_command(self) -> None: from tests.sources import script_paths - self.assertEqual({ROOT / name for name in standalonerecord.CODE_FILES}, - set(script_paths("standalone_probe.py"))) + probe = ("import json, standalone_probe\n" + "from probes.standalonerecord import SCRIPTS_DIR, code_files\n" + "from pathlib import Path\n" + "loaded = sorted(str(Path(m.__file__).resolve()) for m in list(sys.modules.values())\n" + " if getattr(m, '__file__', None)\n" + " and Path(m.__file__).resolve().is_relative_to(SCRIPTS_DIR))\n" + "print(json.dumps([code_files(), loaded]))\n") + out = subprocess.run([sys.executable, "-c", "import sys\n" + probe], cwd=ROOT / "scripts", + capture_output=True, text=True, check=True).stdout + files, loaded = json.loads(out) + named = {ROOT / name for name in files} + self.assertLessEqual({Path(path) for path in loaded}, named) + self.assertLessEqual(set(script_paths("standalone_probe.py")), named) + self.assertLessEqual({ROOT / "scripts/core_probe.py", ROOT / "scripts/probes/host.py"}, named) class HostLock(unittest.TestCase):