fix: mark a standalone record from its loaded code

This commit is contained in:
Abdessamad Derraz committed 2026-11-24 00:20:03 +01:00
1 parent 83e810d1b3
commit 9b2957fbe4
3 files changed
+48 -13

No files matched your search

+4
View File
@@ -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."
}
}
},
+21 -8
View File
@@ -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)
+23 -5
View File
@@ -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):