From 5eb62d0d192ab96ee077dd5c2bc3665048f39eec Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Thu, 12 Nov 2026 15:40:42 +0000 Subject: [PATCH] fix(verify_install): check F re-runs the Colab setup cell once after a restart request When the bootstrap raises ColabRestartRequired (it upgraded a package this process had already loaded), do what a user does: run the cell again in a fresh process. Exit 4 marks the request; a second request or any other failure is still a FAIL. Matched by class name so older autonerves releases are unaffected. Refs PyAutoLabs/PyAutoNerves#217 Co-Authored-By: Claude Opus 5.5 --- heart/checks/verify_install.sh | 20 +++++++++- tests/test_verify_install_script.py | 60 +++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/heart/checks/verify_install.sh b/heart/checks/verify_install.sh index a4f8f58a..380c8a83 100755 --- a/heart/checks/verify_install.sh +++ b/heart/checks/verify_install.sh @@ -926,7 +926,16 @@ if not hasattr(_setup_colab, "setup"): ) sys.exit(3) -_setup_colab.setup("autolens", raise_error_if_not_gpu=False, workspace_dir=WS_DIR) +try: + _setup_colab.setup("autolens", raise_error_if_not_gpu=False, workspace_dir=WS_DIR) +except Exception as exc: + # A user restarts the runtime and re-runs the cell (PyAutoNerves#217); the + # shell below does the same in a fresh process. Matched by name so older + # autonerves releases without the class are unaffected. + if type(exc).__name__ == "ColabRestartRequired": + print(f"RESTART REQUESTED: {exc}") + sys.exit(4) + raise # --- assertions: clone happened, cwd moved into the workspace --- assert os.getcwd() == WS_DIR, f"cwd is {os.getcwd()}, expected {WS_DIR}" @@ -937,6 +946,15 @@ PYEOF local setup_rc=0 COLAB_SIM_WORKSPACE_DIR="$ws_dir" python /tmp/F_driver_setup.py |& tee /tmp/F_driver.log setup_rc=${PIPESTATUS[0]} + # Exit 4 = the bootstrap upgraded a package this process had already loaded + # and asked for a runtime restart. Do what a user does: run the cell once + # more in a fresh process. A second request (or any failure) is a FAIL. + if [ "$setup_rc" -eq 4 ]; then + step "setup cell asked for a runtime restart; re-running it in a fresh process" + COLAB_SIM_WORKSPACE_DIR="$ws_dir" python /tmp/F_driver_setup.py |& tee -a /tmp/F_driver.log + setup_rc=${PIPESTATUS[0]} + [ "$setup_rc" -eq 4 ] && setup_rc=41 + fi if [ "$setup_rc" -eq 3 ]; then RESULTS+=("F|SKIP|installed autonerves predates setup_colab registry (next release)") diff --git a/tests/test_verify_install_script.py b/tests/test_verify_install_script.py index 308344fc..495f1fa9 100644 --- a/tests/test_verify_install_script.py +++ b/tests/test_verify_install_script.py @@ -668,3 +668,63 @@ def test_sidecar_warn_row_keeps_ready_true_and_readiness_not_red(tmp_path): verdict = readiness.compute({"ts": sidecar["ts"], "verify_install": sidecar}) assert not any("install" in reason for reason in verdict["red_reasons"]) assert not any("install" in reason for reason in verdict["yellow_reasons"]) + + +def _setup_driver_source(): + body = SCRIPT.read_text() + start = body.index("cat > /tmp/F_driver_setup.py <<'PYEOF'\n") + len( + "cat > /tmp/F_driver_setup.py <<'PYEOF'\n" + ) + return body[start : body.index("PYEOF\n", start)] + + +def _run_setup_driver(tmp_path, setup_body): + """Run check F's real setup-cell driver against a fake autonerves.setup_colab.""" + import os + import sys + + pkgs = tmp_path / "pkgs" + (pkgs / "google" / "colab").mkdir(parents=True) + (pkgs / "google" / "__init__.py").write_text("") + (pkgs / "google" / "colab" / "__init__.py").write_text("") + (pkgs / "autonerves").mkdir() + (pkgs / "autonerves" / "__init__.py").write_text("") + (pkgs / "autonerves" / "setup_colab.py").write_text( + "class ColabRestartRequired(RuntimeError):\n pass\n\n" + "def setup(*args, **kwargs):\n" + setup_body + ) + driver = tmp_path / "driver.py" + # The cell's `pip install autonerves --no-deps` is replaced by a no-op. + driver.write_text( + _setup_driver_source().replace( + 'subprocess.check_call(\n [sys.executable, "-m", "pip", "install", "autonerves", "--no-deps"]\n )', + "pass", + ) + ) + ws = tmp_path / "ws" + env = {**os.environ, "PYTHONPATH": str(pkgs), "COLAB_SIM_WORKSPACE_DIR": str(ws)} + return subprocess.run( + [sys.executable, str(driver)], env=env, capture_output=True, text=True + ) + + +def test_check_f_driver_exits_4_when_the_bootstrap_asks_for_a_restart(tmp_path): + """PyAutoNerves#217: a restart request is what a user meets on Colab; the + shell re-runs the cell in a fresh process instead of failing check F.""" + res = _run_setup_driver(tmp_path, " raise ColabRestartRequired('restart please')\n") + assert res.returncode == 4, res.stdout + res.stderr + assert "RESTART REQUESTED" in res.stdout + + +def test_check_f_driver_still_fails_on_any_other_error(tmp_path): + res = _run_setup_driver(tmp_path, " raise ValueError('broken bootstrap')\n") + assert res.returncode not in (0, 4) + assert "broken bootstrap" in res.stderr + + +def test_check_f_reruns_the_cell_once_after_a_restart_request(): + body = SCRIPT.read_text() + block = body[body.index('if [ "$setup_rc" -eq 4 ]; then') :] + block = block[: block.index("\n fi\n")] + assert block.count("python /tmp/F_driver_setup.py") == 1 + assert '[ "$setup_rc" -eq 4 ] && setup_rc=41' in block