Skip to content

The agent that grows with you

ci: show PM cache restore results and write policy

ethernetf3ea0290e2f0
diff --git a/.github/actions/setup-pm/action.yml b/.github/actions/setup-pm/action.yml
index 9604a340747..450ec875b75 100644
--- a/.github/actions/setup-pm/action.yml
+++ b/.github/actions/setup-pm/action.yml
@@ -222,3 +222,14 @@ runs:
if [ "$_PM_TEST_ENVIRONMENT" = true ]; then args+=(--test-environment); fi
"$HERMES_PYTHON" -S "$_PM_ACTION/../../../scripts/ci/setup_toolchain.py" dependencies \
--toolchain "$_PM_TOOLCHAIN" --extras "$_PM_EXTRAS" --home "$HERMES_HOME" "${args[@]}"
+
+ - name: Log PM cache restore results and save policy
+ if: always()
+ shell: bash
+ env:
+ _PM_ACTION: ${{ github.action_path }}
+ _PM_CACHE_INPUTS: ${{ toJSON(inputs) }}
+ _PM_CACHE_STEPS: ${{ toJSON(steps) }}
+ run: |
+ if [ "$RUNNER_OS" = Windows ]; then bootstrap=python; else bootstrap=python3; fi
+ "$bootstrap" -S "$_PM_ACTION/../../../scripts/ci/pm_cache_log.py"
diff --git a/scripts/ci/pm_cache_log.py b/scripts/ci/pm_cache_log.py
new file mode 100644
index 00000000000..1ad033c66df
--- /dev/null
+++ b/scripts/ci/pm_cache_log.py
@@ -0,0 +1,30 @@
+"""Log setup-pm's restore results and write policy without traversing cache trees."""
+from __future__ import annotations
+
+import json
+import os
+
+
+def log_caches(inputs: dict, steps: dict, scope: str) -> None:
+ for family, name, producer, output in [
+ ("tools", "tools", "prepare", "store"),
+ ("uv", "python", "install", "uv-cache-path"),
+ ("npm", "node", "install", "npm-cache-path"),
+ ]:
+ candidates = [steps.get(f"{name}-cache", {}), steps.get(f"{name}-cache-restore", {})]
+ selected = next((step for step in candidates if step.get("outcome") not in (None, "skipped")), {})
+ outcome = selected.get("outcome", "skipped")
+ hit = selected.get("outputs", {}).get("cache-hit", "")
+ policy = "disabled" if inputs.get(f"save-{name}-cache") == "false" else "on-success"
+ if not selected:
+ policy = "not-registered"
+ path = steps.get(producer, {}).get("outputs", {}).get(output, "")
+ # This is policy, not a save receipt: the native post action reports
+ # exact-hit skips, successful archive sizes and upload failures later.
+ print(f"[setup-pm cache] {family}: restore={outcome}, cache-hit={hit!r}, "
+ f"save={policy}, path={path!r}, scope={scope}", flush=True)
+
+
+if __name__ == "__main__":
+ log_caches(json.loads(os.environ["_PM_CACHE_INPUTS"]), json.loads(os.environ["_PM_CACHE_STEPS"]),
+ os.environ.get("GITHUB_REF", "unknown"))
diff --git a/tests/ci/test_pm_cache_log.py b/tests/ci/test_pm_cache_log.py
new file mode 100644
index 00000000000..f71b8e3b266
--- /dev/null
+++ b/tests/ci/test_pm_cache_log.py
@@ -0,0 +1,53 @@
+"""PM cache diagnostics describe the action's actual selected branches."""
+from __future__ import annotations
+
+import json
+import os
+from pathlib import Path
+import subprocess
+import sys
+
+import pytest
+
+
[email protected]("hit,save", [("true", "true"), ("false", "true"), ("", "false")])
+def test_cache_log_reports_selected_restore_and_save_policy(tmp_path, hit, save):
+ script = Path(__file__).resolve().parents[2] / "scripts/ci/pm_cache_log.py"
+ steps: dict[str, dict] = {"prepare": {"outputs": {"store": str(tmp_path / "tools")}},
+ "install": {"outputs": {"uv-cache-path": str(tmp_path / "uv"),
+ "npm-cache-path": str(tmp_path / "npm")}}}
+ inputs = {}
+ for name in ["tools", "python", "node"]:
+ inputs[f"save-{name}-cache"] = save
+ selected = f"{name}-cache" + ("-restore" if save == "false" else "")
+ steps[selected] = {"outcome": "success", "outputs": {"cache-hit": hit}}
+ result = subprocess.run(
+ [sys.executable, "-S", str(script)],
+ env={**os.environ, "_PM_CACHE_INPUTS": json.dumps(inputs), "_PM_CACHE_STEPS": json.dumps(steps)},
+ capture_output=True, text=True, encoding="utf-8", timeout=10, check=False,
+ )
+ assert result.returncode == 0, result.stderr
+ lines = result.stdout.splitlines()
+ assert len(lines) == 3
+ for family, line in zip(["tools", "uv", "npm"], lines):
+ assert f"[setup-pm cache] {family}:" in line
+ assert f"cache-hit={hit!r}" in line
+ assert ("save=on-success" if save == "true" else "save=disabled") in line
+ assert str(tmp_path / family) in line
+
+
+def test_cache_log_does_not_call_a_skipped_restore_a_miss(tmp_path):
+ script = Path(__file__).resolve().parents[2] / "scripts/ci/pm_cache_log.py"
+ result = subprocess.run(
+ [sys.executable, "-S", str(script)],
+ env={**os.environ, "_PM_CACHE_INPUTS": "{}", "_PM_CACHE_STEPS": json.dumps({
+ "tools-cache": {"outcome": "skipped", "outputs": {}},
+ "python-cache-restore": {"outcome": "failure", "outputs": {}},
+ })},
+ capture_output=True, text=True, encoding="utf-8", timeout=10, check=False,
+ )
+ assert result.returncode == 0, result.stderr
+ lines = result.stdout.splitlines()
+ assert "tools: restore=skipped" in lines[0]
+ assert "uv: restore=failure" in lines[1]
+ assert "npm: restore=skipped" in lines[2]