@@ -749,6 +749,31 @@ def import_ok() -> bool:
749749
750750# -- baseline runtime ----------------------------------------------------------
751751
752+ def _relocate_venv_scripts (src : Path , dst : Path ) -> None :
753+ """Point cloned launchers/activation scripts at the clone, without writing
754+ through hardlinks into the campaign runtime."""
755+ import stat
756+
757+ source = os .fsencode (str (src ))
758+ destination = os .fsencode (str (dst ))
759+ # The baseline directory name starts with the campaign name. Match the
760+ # complete path so a healthy cloned launcher is not relocated twice.
761+ source_path = re .compile (re .escape (source ) + rb"(?=[/\x22\x27\s]|$)" )
762+ for script in (dst / "bin" ).iterdir ():
763+ if script .is_symlink () or not script .is_file ():
764+ continue
765+ data = script .read_bytes ()
766+ if b"\0 " in data or not source_path .search (data ):
767+ continue
768+ replacement = script .with_name (f".{ script .name } .relocate-{ os .getpid ()} " )
769+ try :
770+ replacement .write_bytes (source_path .sub (lambda _ : destination , data ))
771+ replacement .chmod (stat .S_IMODE (script .stat ().st_mode ))
772+ os .replace (replacement , script )
773+ finally :
774+ replacement .unlink (missing_ok = True )
775+
776+
752777def _clone_tree_hardlinked (src : Path , dst : Path ) -> bool :
753778 """Hardlink clone (near-zero disk, same filesystem). uv replaces files
754779 by rename, which breaks the links — the campaign venv is never modified
@@ -757,6 +782,7 @@ def _clone_tree_hardlinked(src: Path, dst: Path) -> bool:
757782 if rc != 0 :
758783 _log (f"baseline-venv clone failed: { err .strip ()} " )
759784 return False
785+ _relocate_venv_scripts (src , dst )
760786 return True
761787
762788
@@ -778,7 +804,8 @@ def ensure_baseline_venv(
778804 against the wheel main was LAST aligned to"; this builds exactly that
779805 runtime: hardlink-clone the campaign venv (identical deps), swap in
780806 the baseline wheel from a throwaway worktree of the upstream at the
781- baseline commit. Returns None on any failure — a missing baseline
807+ baseline commit. Installs are non-editable so cleanup cannot remove code
808+ the runtime needs; console scripts are relocated to the clone. Returns None on any failure — a missing baseline
782809 runtime keeps the loop's regression-preserving default and must never
783810 read as pre-existing."""
784811 import shutil
@@ -791,9 +818,16 @@ def ensure_baseline_venv(
791818 python = str (bv / "bin" / "python" )
792819 if (bv / "pyvenv.cfg" ).is_file ():
793820 installed = read_installed_version (spec , python = python , run = run )
794- if installed and version_matches_commit (installed , baseline_commit ):
795- log (f"baseline venv already healthy at { bv } " )
796- return bv
821+ matches = (installed_wheel_matches (
822+ spec , baseline_commit , installed , python = python , run = run )
823+ if spec .install_url_template else
824+ bool (installed and version_matches_commit (installed , baseline_commit )))
825+ if matches :
826+ rc , _ , _ = run ([python , "-c" , build_import_check_snippet (spec )], cwd = bv )
827+ if rc == 0 :
828+ _relocate_venv_scripts (target_venv , bv )
829+ log (f"baseline venv already healthy at { bv } " )
830+ return bv
797831 if not (Path (upstream ) / ".git" ).exists ():
798832 log (f"baseline venv: upstream { upstream } is not a git checkout" )
799833 return None
@@ -817,8 +851,14 @@ def ensure_baseline_venv(
817851 env .update ({key : value .replace ("{commit}" , baseline_commit )
818852 .replace ("{variant}" , spec .variant )
819853 for key , value in spec .install_env .items ()})
854+ # The worktree is deleted below. An editable install would retain a
855+ # finder pointing into that deleted tree and pass only the import check
856+ # run from inside it. Prefer the frozen published wheel when available.
857+ requirement = (spec .install_url_template .format (
858+ commit = baseline_commit , variant = spec .variant , arch = spec .arch ,
859+ package = spec .package ) if spec .install_url_template else "." )
820860 rc , _ , err = run ([uv , "pip" , "install" , "--python" , python ,
821- "-e" , "." ], cwd = wt , env = env , timeout = 3600 )
861+ requirement ], cwd = wt , env = env , timeout = 3600 )
822862 if rc != 0 :
823863 log (f"baseline wheel install failed: { err .strip ()[- 300 :]} " )
824864 shutil .rmtree (bv , ignore_errors = True )
@@ -832,6 +872,13 @@ def ensure_baseline_venv(
832872 finally :
833873 run (["git" , "-C" , str (upstream ), "worktree" , "remove" ,
834874 "--force" , str (wt )], timeout = 600 )
875+ # Probe after removal, away from both source trees: cwd must not rescue a
876+ # broken install. This also protects baseline classification on later reuse.
877+ rc , _ , err = run ([python , "-c" , build_import_check_snippet (spec )], cwd = bv )
878+ if rc != 0 :
879+ log (f"baseline venv import failed after source cleanup: { err .strip ()[- 200 :]} " )
880+ shutil .rmtree (bv , ignore_errors = True )
881+ return None
835882 log (f"baseline venv ready at { bv } (wheel { baseline_commit [:12 ]} )" )
836883 return bv
837884
0 commit comments