fix(prax): pytest detection immune to env-clearing — S304 F6/F43. Hooks tests patch.dict(os.environ, clear=True) stripped PYTEST_CURRENT_TEST, so loggers minted in that window froze prod-path handlers via the setup cache. get_system/module_logs_dir now also check _pytest in sys.modules. 1032 prax + 1154 hooks tests green. Residual: hooks-owned diagnostics writer bypasses prax resolution — handed to @hooks. DPLAN-0250 Track A, dispatched to @prax

This commit is contained in:
AIOSAI
2026-07-19 00:55:07 -07:00
parent 800acd079a
commit 48e25ae46e
2 changed files with 10 additions and 2 deletions
+8 -2
View File
@@ -31,6 +31,7 @@ import inspect
import json
import logging
import os
import sys
import tempfile
from pathlib import Path
from typing import Dict, Any, Optional
@@ -60,6 +61,11 @@ def _find_repo_root() -> Path:
return Path.cwd()
def _is_pytest_session() -> bool:
"""Detect pytest session via sys.modules (immune to patch.dict(os.environ, clear=True))."""
return "_pytest" in sys.modules
# Lazy SYSTEM_LOGS_DIR — resolved on first access, not at import time.
# Callers should use get_system_logs_dir() for guaranteed initialization.
_system_logs_dir_cache: Path | None = None
@@ -76,7 +82,7 @@ def get_system_logs_dir() -> Path:
p = Path(test_log_dir) / "system"
p.mkdir(parents=True, exist_ok=True)
return p
if os.environ.get("PYTEST_CURRENT_TEST"):
if os.environ.get("PYTEST_CURRENT_TEST") or _is_pytest_session():
p = Path(tempfile.gettempdir()) / "aipass_test_logs" / "system"
p.mkdir(parents=True, exist_ok=True)
return p
@@ -138,7 +144,7 @@ def get_module_logs_dir(module_name: Optional[str] = None) -> Path:
p = Path(test_log_dir) / module_name
p.mkdir(parents=True, exist_ok=True)
return p
if os.environ.get("PYTEST_CURRENT_TEST"):
if os.environ.get("PYTEST_CURRENT_TEST") or _is_pytest_session():
p = Path(tempfile.gettempdir()) / "aipass_test_logs" / module_name
p.mkdir(parents=True, exist_ok=True)
return p
+2
View File
@@ -50,6 +50,8 @@ def _fresh_import_load(monkeypatch, tmp_path):
# Clear test log redirects so tests exercise real path resolution
monkeypatch.delenv("AIPASS_TEST_LOG_DIR", raising=False)
monkeypatch.delenv("PYTEST_CURRENT_TEST", raising=False)
# Suppress sys.modules-based pytest detection so tests can verify prod paths
monkeypatch.setattr(load_mod, "_is_pytest_session", lambda: False)
return load_mod