diff --git a/bin/pyauto-brain b/bin/pyauto-brain index d144923d..d54eb95d 100755 --- a/bin/pyauto-brain +++ b/bin/pyauto-brain @@ -181,7 +181,12 @@ ensure_session_environment() { [[ -z "${PYAUTO_SKIP_BOOTSTRAP:-}" ]] || return 0 local boot local mind_checkout - mind_checkout="$(resolve_mind)" || return 0 + # resolve_mind lives in agents/_common.sh, which this dispatcher does not + # source (the agents do). Resolve in a subshell so the shared helpers stay + # out of the dispatcher's namespace; an unresolvable Mind just skips the + # bootstrap, as before. + mind_checkout="$(. "$BRAIN_HOME/agents/_common.sh" && resolve_mind 2>/dev/null)" \ + || return 0 boot="$mind_checkout/scripts/session_bootstrap.sh" [[ -x "$boot" ]] || return 0 bash "$boot" >/dev/null 2>&1 || true diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..9b8d3c7d --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,19 @@ +"""Suite-wide isolation for tests that drive bin/pyauto-brain. + +In a remote session (CLAUDE_CODE_REMOTE=true) the dispatcher runs PyAutoMind's +session bootstrap before every verb. That bootstrap honours a declared +PYAUTO_ROOT and registers a `.claude/` hook there, so a test pointing +PYAUTO_ROOT at a tmp fixture would have the fixture rewritten under it (an +extra `.claude` sibling) and pay the bootstrap's cost on every subprocess. +Tests never want the real bootstrap; tests/test_session_bootstrap_dispatch.py +removes this variable explicitly to exercise it against a stub. +""" + +from __future__ import annotations + +import pytest + + +@pytest.fixture(autouse=True) +def _no_session_bootstrap(monkeypatch): + monkeypatch.setenv("PYAUTO_SKIP_BOOTSTRAP", "1") diff --git a/tests/test_session_bootstrap_dispatch.py b/tests/test_session_bootstrap_dispatch.py new file mode 100644 index 00000000..c97917cc --- /dev/null +++ b/tests/test_session_bootstrap_dispatch.py @@ -0,0 +1,55 @@ +"""bin/pyauto-brain runs the Mind's session bootstrap in remote sessions. + +Regression: ensure_session_environment called resolve_mind, which is defined +in agents/_common.sh and was never sourced by the dispatcher, so every verb in +a remote session printed "resolve_mind: command not found" and silently +skipped the bootstrap. +""" + +from __future__ import annotations + +import os +import subprocess +from pathlib import Path + +BRAIN = Path(__file__).resolve().parents[1] + + +def _run(tmp_path: Path, **env_overrides: str) -> tuple[subprocess.CompletedProcess, Path]: + mind = tmp_path / "PyAutoMind" + (mind / "scripts").mkdir(parents=True) + marker = tmp_path / "bootstrapped" + boot = mind / "scripts" / "session_bootstrap.sh" + boot.write_text(f"#!/usr/bin/env bash\ntouch {marker}\n") + boot.chmod(0o755) + env = { + **os.environ, + "CLAUDE_CODE_REMOTE": "true", + "PYAUTO_MIND": str(mind), + } + env.pop("PYAUTO_SKIP_BOOTSTRAP", None) + env.update(env_overrides) + # An unknown agent still passes through ensure_session_environment, then + # exits 2 without running anything else. + result = subprocess.run( + ["bash", str(BRAIN / "bin" / "pyauto-brain"), "no-such-agent"], + capture_output=True, + text=True, + env=env, + timeout=60, + ) + return result, marker + + +def test_remote_session_runs_mind_bootstrap(tmp_path): + result, marker = _run(tmp_path) + assert result.returncode == 2 + assert "command not found" not in result.stderr + assert marker.exists() + + +def test_skip_bootstrap_is_honoured(tmp_path): + result, marker = _run(tmp_path, PYAUTO_SKIP_BOOTSTRAP="1") + assert result.returncode == 2 + assert "command not found" not in result.stderr + assert not marker.exists()