| 1 | """Runner identity and machine-profile capture.
|
| 2 |
|
| 3 | Latency rows are only comparable inside a runner class (ADR-0007). This
|
| 4 | module keeps that policy behind a small interface: capture the local profile,
|
| 5 | classify it with ``config/runner_classes.json``, and stamp rows additively.
|
| 6 | """
|
| 7 |
|
| 8 | from __future__ import annotations
|
| 9 |
|
| 10 | import hashlib
|
| 11 | import json
|
| 12 | import os
|
| 13 | import platform
|
| 14 | import shutil
|
| 15 | import subprocess
|
| 16 | from pathlib import Path
|
| 17 | from typing import Any
|
| 18 |
|
| 19 | ROOT = Path(__file__).resolve().parents[2]
|
| 20 | DEFAULT_RUNNER_CLASSES = ROOT / "config" / "runner_classes.json"
|
| 21 |
|
| 22 |
|
| 23 | def _text_file(path: Path) -> str | None:
|
| 24 | try:
|
| 25 | text = path.read_text(errors="replace").strip()
|
| 26 | except OSError:
|
| 27 | return None
|
| 28 | return text or None
|
| 29 |
|
| 30 |
|
| 31 | def _command_text(command: list[str]) -> str | None:
|
| 32 | try:
|
| 33 | return subprocess.check_output(command, text=True, stderr=subprocess.DEVNULL, timeout=2).strip() or None
|
| 34 | except Exception:
|
| 35 | return None
|
| 36 |
|
| 37 |
|
| 38 | def cpu_model() -> str | None:
|
| 39 | if platform.system() == "Darwin":
|
| 40 | return _command_text(["sysctl", "-n", "machdep.cpu.brand_string"])
|
| 41 | linux = _text_file(Path("/proc/cpuinfo"))
|
| 42 | if linux:
|
| 43 | for line in linux.splitlines():
|
| 44 | if line.lower().startswith("model name"):
|
| 45 | return line.split(":", 1)[1].strip()
|
| 46 | if line.lower().startswith("hardware"):
|
| 47 | return line.split(":", 1)[1].strip()
|
| 48 | return platform.processor() or None
|
| 49 |
|
| 50 |
|
| 51 | def cpu_governor() -> str | None:
|
| 52 | governors = sorted(
|
| 53 | {
|
| 54 | value
|
| 55 | for path in Path("/sys/devices/system/cpu").glob("cpu*/cpufreq/scaling_governor")
|
| 56 | if (value := _text_file(path))
|
| 57 | }
|
| 58 | )
|
| 59 | return ",".join(governors) if governors else None
|
| 60 |
|
| 61 |
|
| 62 | def filesystem_type(path: Path) -> str | None:
|
| 63 | if platform.system() == "Darwin":
|
| 64 | output = _command_text(["/usr/sbin/diskutil", "info", str(mount_point(path))])
|
| 65 | return parse_diskutil_filesystem_type(output or "")
|
| 66 | if shutil.which("stat"):
|
| 67 | return _command_text(["stat", "-f", "-c", "%T", str(path)])
|
| 68 | return None
|
| 69 |
|
| 70 |
|
| 71 | def mount_point(path: Path) -> Path:
|
| 72 | candidate = path.resolve()
|
| 73 | if not candidate.exists():
|
| 74 | candidate = candidate.parent
|
| 75 | if candidate.is_file():
|
| 76 | candidate = candidate.parent
|
| 77 | while not os.path.ismount(candidate):
|
| 78 | parent = candidate.parent
|
| 79 | if parent == candidate:
|
| 80 | break
|
| 81 | candidate = parent
|
| 82 | return candidate
|
| 83 |
|
| 84 |
|
| 85 | def parse_diskutil_filesystem_type(output: str) -> str | None:
|
| 86 | preferred = ("File System Personality", "Type (Bundle)")
|
| 87 | values: dict[str, str] = {}
|
| 88 | for line in output.splitlines():
|
| 89 | if ":" not in line:
|
| 90 | continue
|
| 91 | key, value = line.split(":", 1)
|
| 92 | key = key.strip()
|
| 93 | value = value.strip()
|
| 94 | if key in preferred and value:
|
| 95 | values[key] = value
|
| 96 | for key in preferred:
|
| 97 | if key in values:
|
| 98 | return values[key]
|
| 99 | return None
|
| 100 |
|
| 101 |
|
| 102 | def machine_profile(path: Path | None = None) -> dict[str, Any]:
|
| 103 | probe_path = (path or Path.cwd()).resolve()
|
| 104 | return {
|
| 105 | "schema_version": 1,
|
| 106 | "system": platform.system(),
|
| 107 | "release": platform.release(),
|
| 108 | "machine": platform.machine(),
|
| 109 | "processor": platform.processor() or None,
|
| 110 | "cpu_model": cpu_model(),
|
| 111 | "cpu_count_logical": os.cpu_count(),
|
| 112 | "cpu_governor": cpu_governor(),
|
| 113 | "python_version": platform.python_version(),
|
| 114 | "filesystem_type": filesystem_type(probe_path),
|
| 115 | "probe_path": str(probe_path),
|
| 116 | }
|
| 117 |
|
| 118 |
|
| 119 | def stable_runner_fields(profile: dict[str, Any]) -> dict[str, Any]:
|
| 120 | return {
|
| 121 | key: profile.get(key)
|
| 122 | for key in (
|
| 123 | "system",
|
| 124 | "release",
|
| 125 | "machine",
|
| 126 | "processor",
|
| 127 | "cpu_model",
|
| 128 | "cpu_count_logical",
|
| 129 | "filesystem_type",
|
| 130 | )
|
| 131 | }
|
| 132 |
|
| 133 |
|
| 134 | def runner_id(profile: dict[str, Any]) -> str:
|
| 135 | payload = json.dumps(stable_runner_fields(profile), sort_keys=True, separators=(",", ":")).encode("utf-8")
|
| 136 | return hashlib.sha256(payload).hexdigest()[:16]
|
| 137 |
|
| 138 |
|
| 139 | def load_runner_classes(path: Path = DEFAULT_RUNNER_CLASSES) -> dict[str, Any]:
|
| 140 | return json.loads(path.read_text())
|
| 141 |
|
| 142 |
|
| 143 | def _matches(profile: dict[str, Any], match: dict[str, Any]) -> bool:
|
| 144 | for key, expected in match.items():
|
| 145 | if key.endswith("_prefix"):
|
| 146 | field = key[: -len("_prefix")]
|
| 147 | if not str(profile.get(field) or "").startswith(str(expected)):
|
| 148 | return False
|
| 149 | elif profile.get(key) != expected:
|
| 150 | return False
|
| 151 | return True
|
| 152 |
|
| 153 |
|
| 154 | def runner_class(profile: dict[str, Any], config: dict[str, Any] | None = None) -> str:
|
| 155 | data = config or load_runner_classes()
|
| 156 | default = "unknown"
|
| 157 | for item in data.get("classes", []):
|
| 158 | if item.get("default"):
|
| 159 | default = str(item["id"])
|
| 160 | continue
|
| 161 | if _matches(profile, item.get("match", {})):
|
| 162 | return str(item["id"])
|
| 163 | return default
|
| 164 |
|
| 165 |
|
| 166 | def runner_fields(profile: dict[str, Any] | None = None, config: dict[str, Any] | None = None) -> dict[str, Any]:
|
| 167 | prof = profile or machine_profile()
|
| 168 | return {
|
| 169 | "runner_id": runner_id(prof),
|
| 170 | "runner_class": runner_class(prof, config),
|
| 171 | "runner_profile": prof,
|
| 172 | }
|
| 173 |
|
| 174 |
|
| 175 | def stamp_row(row: dict[str, Any], fields: dict[str, Any]) -> dict[str, Any]:
|
| 176 | stamped = dict(row)
|
| 177 | stamped.update(fields)
|
| 178 | return stamped
|