"""tests/regression/conftest.py

iso-2629-dev6 worktree 전용 sys.path 보정.
tests/conftest.py 의 WORKSPACE_ROOT 기본값이 live main workspace(/home/jay/workspace)를
가리키는 문제를 방지한다.

이 conftest 는 tests/conftest.py 이후에 로드되지만,
sys.path[0] 에 worktree root 를 삽입하고 utils 패키지 캐시를 교체한다.
"""

import sys
from pathlib import Path

_WORKTREE_ROOT = Path(__file__).resolve().parents[2]

# sys.path[0] 에 worktree root 를 보장 (live workspace 보다 우선)
if str(_WORKTREE_ROOT) not in sys.path:
    sys.path.insert(0, str(_WORKTREE_ROOT))
elif sys.path[0] != str(_WORKTREE_ROOT):
    sys.path.remove(str(_WORKTREE_ROOT))
    sys.path.insert(0, str(_WORKTREE_ROOT))

# utils 패키지가 이미 sys.modules 에 live workspace 것으로 캐싱된 경우 교체.
# ★ 부작용 최소화(Gemini medium): 전체 utils.* 무차별 삭제 대신, 본 test 가
#   worktree 본을 필요로 하는 모듈(utils 패키지 + callback_lifecycle_*) 로 blast
#   radius 를 한정한다. 무관한 utils.* 캐시(다른 test 가 참조 중일 수 있음)는 보존하여
#   상태 불일치/isinstance 실패 위험을 줄인다.
if "utils" in sys.modules:
    _cached_utils_path = getattr(sys.modules["utils"], "__file__", None) or ""
    if str(_WORKTREE_ROOT) not in _cached_utils_path:
        # worktree 본 사용을 보장해야 하는 최소 모듈만 캐시에서 제거
        _refresh = ("utils", "utils.callback_lifecycle_states",
                    "utils.callback_lifecycle_classifier")
        for k in _refresh:
            sys.modules.pop(k, None)
