"""The cp1-agents scripts Shakedown reuses, imported by path from a clone (plan ยง3: no change to cp1-agents). `kab_run_workflow.py` gives the run-context helpers (load_context, new_ids, context_uploads, upload_type, customer_problem, with_run_metadata, check_engagement) and `evaluate_case.py` judges a run against a case. The clone's commit is recorded with every run, so a result can be reproduced. """ from __future__ import annotations import importlib.util import os import subprocess import sys from pathlib import Path from types import ModuleType DEFAULT_DIR = Path(__file__).resolve().parents[2] / "cp1-agents" def clone_dir() -> Path: path = Path(os.getenv("CP1_AGENTS_DIR") or DEFAULT_DIR) if not (path / "scripts" / "kab_run_workflow.py").is_file(): raise RuntimeError(f"no cp1-agents clone at {path}: set CP1_AGENTS_DIR") return path def _load(name: str) -> ModuleType: scripts = clone_dir() / "scripts" if str(scripts) not in sys.path: sys.path.insert(0, str(scripts)) # evaluate_case imports check_data_contract beside it spec = importlib.util.spec_from_file_location(f"cp1_agents_{name}", scripts / f"{name}.py") module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module _cache: dict[str, ModuleType] = {} def module(name: str) -> ModuleType: if name not in _cache: _cache[name] = _load(name) return _cache[name] def run_workflow() -> ModuleType: return module("kab_run_workflow") def evaluate_case() -> ModuleType: return module("evaluate_case") def commit() -> str | None: try: return subprocess.run(["git", "-C", str(clone_dir()), "rev-parse", "HEAD"], capture_output=True, text=True, check=True).stdout.strip() except (subprocess.CalledProcessError, FileNotFoundError, RuntimeError): return None