fix(test_sandbox): redirect session logs to tests/artifacts via autouse fixture

Per FR1 of test_sandbox_hardening_20260619 spec, all writes must be under
<project_root>/tests/. Tests that create an AppController + call init_state()
trigger session_logger.open_session() at src/session_logger.py:85 which
writes to paths.get_logs_dir() - by default logs/ at project root, outside
tests/. This was triggered by tests/test_context_composition_decoupled.py
and surfaced in the latest batched test run.

Add a function-scoped autouse fixture in tests/conftest.py that monkeypatches
src.paths.get_logs_dir to return a per-run tests/-allowed path. Per-run
subdirectory prevents log_registry.toml collisions across test runs.

Skips test_paths.py, test_test_sandbox.py, and test_app_controller_offloading.py
which directly assert on paths.get_logs_dir() behavior or set up their own
session via tmp_session_dir (overriding get_logs_dir at the module level
breaks those tests' assertions). No production code is modified.
This commit is contained in:
ed
2026-06-21 11:59:51 -04:00
parent 69b7ab670d
commit e2411e5c54
7 changed files with 172 additions and 86 deletions
+34
View File
@@ -432,6 +432,40 @@ def reset_paths() -> Generator[None, None, None]:
from src import paths
yield
@pytest.fixture(autouse=True)
def _redirect_session_logs_to_tests_dir(monkeypatch, request) -> Generator[None, None, None]:
"""
Redirect session_logger output to tests/artifacts/ so the FR1 sandbox audit
allows the writes. Per conductor/code_styleguides/test_sandbox.md, all writes
must be under <project_root>/tests/. The session_logger writes to
paths.get_logs_dir() which defaults to logs/ at project root. Tests that
create an AppController + call init_state() trigger session_logger.open_session()
at src/session_logger.py:85 which would otherwise hit TEST_SANDBOX_VIOLATION.
Monkeypatches src.paths.get_logs_dir to return a per-run tests/-allowed path.
Per-run subdirectory prevents log_registry.toml collisions across test runs.
pytest's monkeypatch fixture auto-restores the original after each test.
Skips tests that explicitly test the paths module or have their own session
setup via tmp_session_dir (those tests call paths.get_logs_dir() directly or
set up the session via env var, and overriding get_logs_dir at the module
level breaks their assertions).
[C: src/session_logger.py:open_session, tests/conftest.py:_sandbox_audit_hook]
"""
test_file = str(request.node.fspath)
if any(name in test_file for name in (
"test_paths.py",
"test_test_sandbox.py",
"test_app_controller_offloading.py",
)):
yield
return
from src import paths as paths_module
test_logs_dir = Path("tests/artifacts/_test_session_logs") / f"run_{_RUN_ID}"
test_logs_dir.mkdir(parents=True, exist_ok=True)
monkeypatch.setattr(paths_module, "get_logs_dir", lambda: test_logs_dir)
yield
@pytest.fixture(autouse=True)
def reset_ai_client() -> Generator[None, None, None]:
"""