From 1e3fe9868de4e155d8771d4f05dce927361414eb Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Fri, 18 Sep 2026 20:41:28 +0100 Subject: [PATCH] maintenance: resolve the workspace root through the shared contract MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit board.py's `_workspace_root()` was a byte-identical twin of Heart's and carried the same bug: it fell back to ~/Code, not ~/Code/PyAutoLabs, so `root/"PyAutoBrain"` named a path that does not exist. It was masked only because it is the last candidate in a chain an earlier candidate satisfies. run_all.py and pre_build.sh derived the root by fixed depth. autohands/_workspace.py is now the single door, reaching PyAutoBrain's resolver the same way board.theme() already finds the Brain and degrading the same way when none is in reach — so importing autohands still never requires a Brain. pre_build.sh adopts it only when the resolver is actually present, because its test harness runs the real script in a throwaway base. ADMIN_JAMMY in bin/autohands had no consumer anywhere in the repo; admin_jammy is not part of the PyAutoLabs ecosystem. Removing it left PYAUTOBASE with no consumer either (the file is executed, never sourced), so both are gone. Hands has no grading path — board.py's root is theme discovery and run_all.py runs the tree you are in — so unlike Heart it needs no canonical/workspace split. Suite 460 passed (baseline 460). Part of PyAutoBrain#391 (phase 1a). No directory layout changes. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_0161NLKVzbBMzhaBfjpGogor --- autohands/_workspace.py | 125 ++++++++++++++++++++++++++++++++++++++++ autohands/board.py | 20 +++++-- autohands/run_all.py | 10 +++- bin/autohands | 2 - pre_build.sh | 16 ++++- 5 files changed, 161 insertions(+), 12 deletions(-) create mode 100644 autohands/_workspace.py diff --git a/autohands/_workspace.py b/autohands/_workspace.py new file mode 100644 index 00000000..acaf61bd --- /dev/null +++ b/autohands/_workspace.py @@ -0,0 +1,125 @@ +"""autohands/_workspace.py — where the workspace root is, asked in one place. + +The answer belongs to the organism, not to this organ: it lives in +``PyAutoBrain/agents/_pyauto_root.py`` (mirrored by ``bin/_pyauto_root.sh``). +This module is the Hands' door to it — everything here that needs the root +imports from here, so the rule can move once, in the Brain, and the Hands +follow. + +The Hands used to roll their own answer in three places: ``board.py`` fell back +to a ``$HOME``-relative path naming the directory ABOVE the workspace (so +``root / "PyAutoBrain"`` named a path that has never existed), while +``run_all.py`` and ``pre_build.sh`` counted a fixed number of directories up +from a file. Counting is right until the checkouts are grouped; a spelled-out +path is an instance fact either way. + +**No hard dependency on PyAutoBrain.** The Hands run with no Brain in reach — +which is why ``board.theme()`` degrades rather than importing unconditionally. +So does this: when the Brain's resolver cannot be found, the rule below is +applied locally and the reason string says so, rather than a wrong root passing +for a right one. The local copy is deliberate duplication of a documented rule, +kept as short as it can be; the Brain's module is the one that explains it. + +Stdlib only; the Brain module is loaded by file path rather than by pushing a +directory onto ``sys.path``, so importing this never changes what any other +``import`` in the process resolves to. +""" + +from __future__ import annotations + +import importlib.util +import os +from pathlib import Path + +__all__ = ["workspace_root", "workspace_root_reason", "ROOT_MARKER"] + +HANDS_HOME = Path(__file__).resolve().parents[1] + +# Mirrors PyAutoBrain/agents/_pyauto_root.py. Only the degraded path below +# reads these; when the Brain is in reach its own values are used. +ROOT_MARKER = ".pyauto-root" +_SIBLING_ORGANS = ( + "PyAutoMind", + "PyAutoCortex", + "PyAutoMemory", + "PyAutoHeart", + "PyAutoHands", + "PyAutoNerves", + "PyAutoGut", +) + +_DEGRADED = " (no PyAutoBrain in reach)" + +_shared_cache: object | None = None + + +def _shared(): + """The Brain's resolver module, or None when no Brain checkout is in reach. + + Candidate order mirrors ``board.theme()``: the explicit ``$PYAUTO_BRAIN`` + first, then a Brain beside this checkout (where CI puts it — tests.yml and + release_board.yml check PyAutoBrain out next to PyAutoHands), then a + vendored one inside it. + """ + global _shared_cache + if _shared_cache is not None: + return _shared_cache or None + for cand in ( + os.environ.get("PYAUTO_BRAIN"), + HANDS_HOME.parent / "PyAutoBrain", + HANDS_HOME / "PyAutoBrain", + ): + if not cand: + continue + module_path = Path(cand) / "agents" / "_pyauto_root.py" + if not module_path.is_file(): + continue + try: + spec = importlib.util.spec_from_file_location("_pyauto_root", module_path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + except Exception: # a broken/partial checkout must not break the Hands + continue + if not callable(getattr(module, "workspace_root_reason", None)): + continue # a resolver older than this API: degrade, do not raise + _shared_cache = module + return module + _shared_cache = False + return None + + +def _marked_root(start: Path) -> Path | None: + """The nearest ANCESTOR of `start` holding the marker, or None.""" + for candidate in start.parents: + if (candidate / ROOT_MARKER).is_file(): + return candidate + return None + + +def workspace_root_reason() -> tuple[Path, str]: + """``(root, why)`` — the workspace root and the rule that produced it.""" + shared = _shared() + if shared is not None: + return shared.workspace_root_reason() + # Degraded: the same order as the Brain's module, applied here. The + # environment override comes first, so an operator's word still wins. + env = os.environ.get("PYAUTO_ROOT") + if env: + if (Path(env) / ROOT_MARKER).is_file(): + return Path(env), "PYAUTO_ROOT" + _DEGRADED + return ( + Path(env), + f"PYAUTO_ROOT (unverified - no {ROOT_MARKER} marker)" + _DEGRADED, + ) + marked = _marked_root(HANDS_HOME) + if marked is not None: + return marked, f"{ROOT_MARKER} marker" + _DEGRADED + parent = HANDS_HOME.parent + if any((parent / organ).is_dir() for organ in _SIBLING_ORGANS): + return parent, "beside this checkout" + _DEGRADED + return parent, "unverified (no sibling organ beside this checkout)" + _DEGRADED + + +def workspace_root() -> Path: + """The workspace root — the directory holding the organ checkouts.""" + return Path(workspace_root_reason()[0]) diff --git a/autohands/board.py b/autohands/board.py index 1ca7d735..324df6b0 100644 --- a/autohands/board.py +++ b/autohands/board.py @@ -43,6 +43,11 @@ import urllib.request from pathlib import Path +try: # `python -m autohands.board` + from autohands import _workspace +except ImportError: # imported flat, with autohands/ itself on sys.path + import _workspace + HANDS_HOME = Path(__file__).resolve().parents[1] # The family look lives once, in the Brain (``board/_theme.py``): the @@ -55,13 +60,16 @@ def _workspace_root() -> Path: - """Where the sibling PyAuto checkouts live: `$PYAUTO_ROOT`, else `~/Code`. - - The org's own directory name is an instance fact, so it is never written - here — a workspace that does not follow the default sets `$PYAUTO_ROOT` - (the same variable the dev-flow doors read). + """Where the sibling PyAuto checkouts live — via `autohands/_workspace.py`. + + The workspace directory's own name is an instance fact, so it is never + written here; neither is the `$HOME`-relative path this used to fall back + to. That fallback named the directory ABOVE the workspace, so `_workspace_root() / + "PyAutoBrain"` pointed at a path that has never existed — masked only + because `theme()` tries it last, after the sibling checkout that answers. + `$PYAUTO_ROOT` still wins, as the resolver's first rule. """ - return Path(os.environ.get("PYAUTO_ROOT") or Path.home() / "Code") + return _workspace.workspace_root() def theme(): diff --git a/autohands/run_all.py b/autohands/run_all.py index 6176bef4..bf821c2f 100644 --- a/autohands/run_all.py +++ b/autohands/run_all.py @@ -33,8 +33,16 @@ from argparse import ArgumentParser from pathlib import Path +try: # imported flat, with autohands/ itself on sys.path (the usual case) + import _workspace +except ImportError: # `python -m autohands.run_all` + from autohands import _workspace + AUTOHANDS_DIR = Path(__file__).parent -PYAUTOBASE = AUTOHANDS_DIR.parent.parent # PyAutoLabs/ +# The workspace root holding the workspace checkouts, via the one shared +# resolver (autohands/_workspace.py). Counting two directories up from this +# file is right only while PyAutoHands sits directly under the root. +PYAUTOBASE = _workspace.workspace_root() RESULTS_BASE = AUTOHANDS_DIR.parent / "run_logs" DEFAULT_RUN_TYPE = "smoke" diff --git a/bin/autohands b/bin/autohands index db25aa01..f5ea228a 100755 --- a/bin/autohands +++ b/bin/autohands @@ -11,9 +11,7 @@ set -u SELF="$(readlink -f "$0")" REPO="$(cd "$(dirname "$SELF")/.." && pwd)" -PYAUTOBASE="$(cd "$REPO/.." && pwd)" AUTOHANDS_DIR="$REPO/autohands" -ADMIN_JAMMY="$PYAUTOBASE/admin_jammy" # ----- subcommand registration ----- # diff --git a/pre_build.sh b/pre_build.sh index 95f124b5..78bf5bc7 100644 --- a/pre_build.sh +++ b/pre_build.sh @@ -14,10 +14,20 @@ set -e MINOR_VERSION="${1:-1}" -# Resolve PYAUTOBASE from this script's location (same idiom as bin/autohands) -# so pre_build.sh works from any checkout — Linux, WSL, anywhere. +# Resolve PYAUTOBASE — the workspace root holding PyAutoHands and every +# workspace repo — through the one shared resolver when a PyAutoBrain checkout +# is in reach, so this script and the agents that drive it agree on the tree +# (and $PYAUTO_ROOT still points them all at another workspace). With no Brain +# beside us, fall back to this script's own location, exactly as before: +# pre_build.sh must keep working from a bare PyAutoHands checkout. SELF="$(readlink -f "$0")" -PYAUTOBASE="$(cd "$(dirname "$SELF")/.." && pwd)" +HANDS_HOME="$(cd "$(dirname "$SELF")" && pwd)" +if [ -f "$HANDS_HOME/../PyAutoBrain/bin/_pyauto_root.sh" ]; then + . "$HANDS_HOME/../PyAutoBrain/bin/_pyauto_root.sh" + PYAUTOBASE="$PYAUTO_ROOT" +else + PYAUTOBASE="$(cd "$HANDS_HOME/.." && pwd)" +fi AUTOHANDS="$PYAUTOBASE/PyAutoHands/autohands" PYTHONPATH_EXTRA="$AUTOHANDS"