Skip to content

Commit bdc612a

Browse files
committed
Keep baseline runtimes independent of temporary source and candidate launchers
Signed-off-by: tzhouam <tzhouam@connect.ust.hk>
1 parent 9e5be10 commit bdc612a

3 files changed

Lines changed: 144 additions & 6 deletions

File tree

‎doc/architecture/SPEC/engine/steps/rebase_v3.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ harness 会话通过 MCP tool bridge 拿到同一套工具面,plan gate 在派
132132
`upstream_commit` 重新确保 scratch。
133133
- baseline venv(`ensure_baseline_venv`):硬链接克隆 campaign venv 并换入
134134
baseline commit 的 wheel,让"omni main 对上次对齐的 wheel 是否也失败"可问;
135-
任何失败返回 None,保留保守的回归判定。
135+
使用发布 wheel(若配置 URL)或非 editable 安装,删除临时源码后再验证导入;
136+
克隆的 console/activation 脚本通过原子替换改为 baseline 路径,不能写穿硬链接
137+
修改 campaign。缓存复用也必须通过导入检查。任何失败返回 None,保留保守的回归判定。
136138

137139
## 操作者重跑与本地超时预算(2026-10-29)
138140

‎src/infermatrix_copilot/rebase_engine/wheel.py‎

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
752777
def _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

‎test/test_rebase_release_target.py‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,3 +437,92 @@ def clone(src, dst):
437437
run=run, clone=clone)
438438
assert out == tmp_path / ("venv-baseline-" + "a" * 12)
439439
assert any("worktree" in c and "remove" in c for c in calls)
440+
441+
442+
def test_hardlinked_baseline_launchers_use_clone_without_modifying_target(tmp_path):
443+
import venv
444+
445+
from infermatrix_copilot.rebase_engine.wheel import _clone_tree_hardlinked, _relocate_venv_scripts
446+
447+
target, baseline = tmp_path / "venv", tmp_path / "venv-baseline"
448+
venv.EnvBuilder(with_pip=False).create(target)
449+
launcher = target / "bin" / "runtime-probe"
450+
original = f"#!{target}/bin/python\nimport sys\nprint(sys.prefix)\n"
451+
launcher.write_text(original)
452+
launcher.chmod(0o755)
453+
assert _clone_tree_hardlinked(target, baseline)
454+
_relocate_venv_scripts(target, baseline) # Cached reuse must be idempotent.
455+
result = subprocess.run([str(baseline / "bin" / launcher.name)],
456+
capture_output=True, text=True, check=True)
457+
assert result.stdout.strip() == str(baseline)
458+
assert launcher.read_text() == original
459+
assert str(target) in (target / "bin" / "activate").read_text()
460+
assert str(baseline) in (baseline / "bin" / "activate").read_text()
461+
462+
463+
def test_baseline_package_survives_temporary_source_removal(tmp_path):
464+
import shutil
465+
import venv
466+
467+
from infermatrix_copilot.rebase_engine import wheel
468+
469+
target = tmp_path / "target"
470+
venv.EnvBuilder(with_pip=False).create(target)
471+
(tmp_path / ".git").mkdir()
472+
473+
def run(cmd, *, cwd=None, env=None, timeout=600):
474+
if cmd[0] == "git":
475+
source = tmp_path / ("target-baseline-" + "a" * 12 + "-src")
476+
if "add" in cmd:
477+
(source / "fixture").mkdir(parents=True)
478+
(source / "fixture" / "__init__.py").write_text("VALUE = 'baseline'\n")
479+
else:
480+
shutil.rmtree(source)
481+
return 0, "", ""
482+
if cmd[:3] == ["fixture-uv", "pip", "uninstall"]:
483+
return 0, "", ""
484+
if cmd[:3] == ["fixture-uv", "pip", "install"]:
485+
python = cmd[cmd.index("--python") + 1]
486+
site = subprocess.check_output(
487+
[python, "-c", "import sysconfig; print(sysconfig.get_path('purelib'))"],
488+
text=True).strip()
489+
# Simulate editable/non-editable installation without network or a
490+
# build backend. The former becomes unimportable after git cleanup.
491+
from pathlib import Path
492+
if "-e" in cmd:
493+
(Path(site) / "fixture.pth").write_text(str(cwd) + "\n")
494+
else:
495+
shutil.copytree(cwd / "fixture", Path(site) / "fixture")
496+
return 0, "", ""
497+
return wheel._run(cmd, cwd=cwd, env=env, timeout=timeout)
498+
499+
spec = WheelSpec(package="fixture", variant="cpu", arch="x86_64",
500+
index_url_template="https://example.invalid/{commit}")
501+
baseline = wheel.ensure_baseline_venv(target, spec, tmp_path, "a" * 40,
502+
uv="fixture-uv", run=run)
503+
assert baseline is not None
504+
assert not (tmp_path / (baseline.name + "-src")).exists()
505+
result = subprocess.run([str(baseline / "bin/python"), "-c",
506+
"import fixture; print(fixture.VALUE)"],
507+
cwd=tmp_path, capture_output=True, text=True, check=True)
508+
assert result.stdout.strip() == "baseline"
509+
510+
511+
def test_cached_baseline_with_matching_metadata_but_missing_code_is_rejected(tmp_path):
512+
from infermatrix_copilot.rebase_engine import wheel
513+
514+
target = tmp_path / "target"
515+
baseline = tmp_path / ("target-baseline-" + "a" * 12)
516+
baseline.mkdir()
517+
(baseline / "pyvenv.cfg").write_text("home=/usr\n")
518+
(tmp_path / ".git").mkdir()
519+
520+
def run(cmd, **kwargs):
521+
if len(cmd) > 3 and cmd[-1] == "fixture":
522+
return 0, "1.0+gaaaaaaaaa\n", ""
523+
return 1, "", "missing fixture source"
524+
525+
result = wheel.ensure_baseline_venv(target, _install_spec(), tmp_path,
526+
"a" * 40, run=run,
527+
clone=lambda src, dst: False)
528+
assert result is None

0 commit comments

Comments
 (0)