Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion bin/pyauto-brain
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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")
55 changes: 55 additions & 0 deletions tests/test_session_bootstrap_dispatch.py
Original file line number Diff line number Diff line change
@@ -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()
Loading