diff --git a/heart/_workspace.py b/heart/_workspace.py index 5ef9d3cc..f4a53479 100644 --- a/heart/_workspace.py +++ b/heart/_workspace.py @@ -83,6 +83,16 @@ _shared_cache: object | None = None +def _check_context_brain() -> None: + """Reject two distinct Brain checkouts in this workspace context.""" + parent = HEART_HOME.parent + root = parent.parent if parent.name == "organs" else parent + flat, grouped = root / "PyAutoBrain", root / "organs" / "PyAutoBrain" + if ((flat / ".git").exists() and (grouped / ".git").exists() + and flat.resolve() != grouped.resolve()): + raise ValueError(f"PyAutoBrain: ambiguous checkouts: {flat}, {grouped}") + + def _shared(): """The Brain's resolver module, or None when no Brain checkout is in reach. @@ -92,6 +102,7 @@ def _shared(): vendored one inside it. """ global _shared_cache + _check_context_brain() if _shared_cache is not None: return _shared_cache or None for cand in ( @@ -164,6 +175,7 @@ def workspace_root() -> Path: def _repo_paths(): + _check_context_brain() for base in (os.environ.get("PYAUTO_BRAIN"), HEART_HOME.parent / "PyAutoBrain", HEART_HOME / "PyAutoBrain", workspace_root() / "PyAutoBrain", *(child / "PyAutoBrain" for child in workspace_root().iterdir() if child.is_dir() and not (child / ".git").exists())): if not base: continue @@ -228,7 +240,9 @@ def _canonical_from_git(start: Path) -> Path | None: common = Path(result.stdout.strip()) if not common.is_absolute(): common = start / common - return common.resolve().parent.parent + checkout = common.resolve().parent + marked = _marked_root(checkout) + return marked if marked is not None else checkout.parent def canonical_root_reason() -> tuple[Path, str]: diff --git a/heart/_workspace.sh b/heart/_workspace.sh index 11af4cb2..371e381e 100644 --- a/heart/_workspace.sh +++ b/heart/_workspace.sh @@ -116,9 +116,19 @@ else ""|/*) ;; *) _heart_common_dir="$_heart_workspace_home/$_heart_common_dir" ;; esac - # //.git -> + # Find the workspace marker above the main checkout; grouped organ + # checkouts add a directory between the repo and workspace root. if [ -n "$_heart_common_dir" ] && [ -e "$_heart_common_dir" ]; then - PYAUTO_MAIN_ROOT="$(cd "$(dirname "$(dirname "$_heart_common_dir")")" && pwd)" + _heart_main_checkout="$(cd "$(dirname "$_heart_common_dir")" && pwd)" + _heart_main_parent="$_heart_main_checkout" + while [ "$_heart_main_parent" != / ] && [ ! -f "$_heart_main_parent/$PYAUTO_ROOT_MARKER" ]; do + _heart_main_parent="$(dirname "$_heart_main_parent")" + done + if [ -f "$_heart_main_parent/$PYAUTO_ROOT_MARKER" ]; then + PYAUTO_MAIN_ROOT="$_heart_main_parent" + else + PYAUTO_MAIN_ROOT="$(dirname "$_heart_main_checkout")" + fi PYAUTO_MAIN_ROOT_REASON="main checkout (git --git-common-dir)" else PYAUTO_MAIN_ROOT="$PYAUTO_ROOT" diff --git a/scripts/health_release.sh b/scripts/health_release.sh index d0c76be2..6a3da5f4 100755 --- a/scripts/health_release.sh +++ b/scripts/health_release.sh @@ -34,10 +34,13 @@ # Sourced in a subshell so a script that lives in the user's ~/.bashrc does not # export PYAUTO_ROOT into every shell. _health_release_self="$(readlink -f "${BASH_SOURCE[0]}")" -PYAUTO_STATUS_FULL_DEFAULT="${PYAUTO_STATUS_FULL_DEFAULT:-$( - . "$(dirname "$_health_release_self")/../heart/_workspace.sh" - printf '%s' "$PYAUTO_MAIN_ROOT/PyAutoHands/run_logs/latest" -)}" +if [ -z "${PYAUTO_STATUS_FULL_DEFAULT:-}" ]; then + PYAUTO_STATUS_FULL_DEFAULT="$( + . "$(dirname "$_health_release_self")/../heart/_common.sh" + hands_home="$(heart_repo_path PyAutoHands)" || exit 1 + printf '%s' "$hands_home/run_logs/latest" + )" || return 1 +fi _health_release() { local run_dir="${1:-$PYAUTO_STATUS_FULL_DEFAULT}" diff --git a/scripts/health_sync.sh b/scripts/health_sync.sh index 07949d10..c1ee27bf 100644 --- a/scripts/health_sync.sh +++ b/scripts/health_sync.sh @@ -63,6 +63,11 @@ PYAUTO_STATUS_ROOT="${PYAUTO_STATUS_ROOT:-$( . "$(dirname "$_health_sync_self")/../heart/_workspace.sh"; printf '%s' "$PYAUTO_MAIN_ROOT" )}" +_health_sync_repo_path() { + local heart_home="$(dirname "$_health_sync_self")/.." + PYTHONPATH="$heart_home" python3 -c 'import sys; from pathlib import Path; from heart import _workspace; print(_workspace.repo_path(Path(sys.argv[1]), sys.argv[2]))' "$PYAUTO_STATUS_ROOT" "$1" +} + _health_sync() { local root="$PYAUTO_STATUS_ROOT" if [[ ! -d "$root" ]]; then @@ -232,7 +237,10 @@ _health_sync() { # failure (no PyAutoHeart checkout, no PyYAML, …) degrades to the raw # behaviour: classify_ok=false, NOISE column shows "?". PYAUTO_HEART_HOME # points at an alternative Heart checkout (e.g. a task worktree). - local heart_home="${PYAUTO_HEART_HOME:-$root/PyAutoHeart}" + local heart_home="${PYAUTO_HEART_HOME:-}" + if [[ -z "$heart_home" ]]; then + heart_home="$(_health_sync_repo_path PyAutoHeart)" || return 1 + fi local classify_ok=false if [[ -f "$heart_home/heart/noise.py" ]]; then if PYTHONPATH="$heart_home" python3 -m heart.noise \ @@ -376,12 +384,14 @@ for f in sorted(glob.glob(os.path.expanduser("~/.cache/pyauto/smoke/*.json"))): # reachable via PyAutoHands/run_logs/latest (written by the autohands # mega-run). Counts only — failure detail lives in the per-shard JSON / the # GitHub Actions run. - local pab_report="$HOME/Code/PyAutoLabs/PyAutoHands/run_logs/latest/report.json" + local hands_home + hands_home="$(_health_sync_repo_path PyAutoHands)" || return 1 + local pab_report="$hands_home/run_logs/latest/report.json" if [[ -f "$pab_report" ]]; then local pab_summary pab_summary=$(python3 -c ' -import json, os -p = os.path.expanduser("~/Code/PyAutoLabs/PyAutoHands/run_logs/latest/report.json") +import json, sys +p = sys.argv[1] try: d = json.load(open(p)) s = d.get("summary", {}) or {} @@ -394,11 +404,11 @@ try: print("|".join(str(x) for x in [label, njobs, n_proj, passed, failed, skipped])) except Exception: pass -' 2>/dev/null) +' "$pab_report" 2>/dev/null) if [[ -n "$pab_summary" ]]; then local pab_date njobs nproj pab_p pab_f pab_s sha IFS='|' read -r pab_date njobs nproj pab_p pab_f pab_s <<< "$pab_summary" - sha=$(git -C "$HOME/Code/PyAutoLabs/PyAutoHands" rev-parse --short HEAD 2>/dev/null) + sha=$(git -C "$hands_home" rev-parse --short HEAD 2>/dev/null) echo "" printf "Last autohands run: %s (PyAutoHands commit %s)\n" "$pab_date" "${sha:-?}" local color='\033[32m' diff --git a/tests/test_health_shell_repo_resolution.py b/tests/test_health_shell_repo_resolution.py new file mode 100644 index 00000000..a1f0e574 --- /dev/null +++ b/tests/test_health_shell_repo_resolution.py @@ -0,0 +1,36 @@ +"""Shell dashboards must surface ambiguous checkout placement.""" + +import os +from pathlib import Path +import subprocess + + +ROOT = Path(__file__).resolve().parents[1] + + +def test_release_default_rejects_ambiguous_hands(tmp_path): + (tmp_path / ".pyauto-root").touch() + for path in (tmp_path / "PyAutoHands", tmp_path / "organs" / "PyAutoHands"): + (path / ".git").mkdir(parents=True) + env = os.environ.copy() + env["PYAUTO_ROOT"] = str(tmp_path) + env.pop("PYAUTO_STATUS_FULL_DEFAULT", None) + result = subprocess.run( + ["bash", "-c", 'source "$1/scripts/health_release.sh"', "test", str(ROOT)], + env=env, text=True, capture_output=True, + ) + assert result.returncode != 0 + assert "ambiguous" in result.stderr + + +def test_sync_helper_rejects_ambiguous_hands(tmp_path): + for path in (tmp_path / "PyAutoHands", tmp_path / "organs" / "PyAutoHands"): + (path / ".git").mkdir(parents=True) + env = os.environ.copy() + env["PYAUTO_STATUS_ROOT"] = str(tmp_path) + result = subprocess.run( + ["bash", "-c", 'source "$1/scripts/health_sync.sh"; _health_sync_repo_path PyAutoHands', "test", str(ROOT)], + env=env, text=True, capture_output=True, + ) + assert result.returncode != 0 + assert "ambiguous" in result.stderr diff --git a/tests/test_repo_paths_grouped.py b/tests/test_repo_paths_grouped.py index 8cd53f0a..e13d2bbf 100644 --- a/tests/test_repo_paths_grouped.py +++ b/tests/test_repo_paths_grouped.py @@ -1,5 +1,6 @@ """Heart's checkout resolver works with the canonical family layout.""" from pathlib import Path +import subprocess from heart import _workspace @@ -11,3 +12,23 @@ def test_grouped_checkout_is_used_for_smoke_and_checks(tmp_path): (checkout / ".git").mkdir() assert _workspace.repo_path(tmp_path, "demo_workspace", required=True) == checkout assert checkout in _workspace.iter_checkouts(tmp_path) + + +def test_canonical_root_follows_marker_above_grouped_organ(tmp_path): + root = tmp_path / "workspace" + root.mkdir() + (root / ".pyauto-root").touch() + heart = root / "organs" / "PyAutoHeart" + heart.mkdir(parents=True) + subprocess.run(["git", "init", "-q", str(heart)], check=True) + assert _workspace._canonical_from_git(heart) == root +def test_distinct_flat_and_grouped_brain_checkouts_are_rejected(tmp_path, monkeypatch): + import pytest + + for path in (tmp_path / "PyAutoBrain", tmp_path / "organs" / "PyAutoBrain"): + (path / ".git").mkdir(parents=True) + monkeypatch.setattr(_workspace, "HEART_HOME", tmp_path / "organs" / "PyAutoHeart") + with pytest.raises(ValueError, match="ambiguous checkouts"): + _workspace._shared() + with pytest.raises(ValueError, match="ambiguous checkouts"): + _workspace._repo_paths()