test(profiles): keep profile module reloads isolated

This commit is contained in:
starship-s
2026-05-15 04:14:09 -06:00
parent 4ffecdd7c9
commit abb6057304
4 changed files with 22 additions and 10 deletions

View File

@@ -30,7 +30,6 @@ from api.agent_sessions import (
read_session_lineage_report,
)
from api.compression_anchor import visible_messages_for_anchor
from api.profiles import get_active_profile_name as _get_active_profile_name, profile_env_for_background_worker
logger = logging.getLogger(__name__)
@@ -5444,9 +5443,11 @@ def handle_post(handler, parsed) -> bool:
target = body.get("target") if isinstance(body, dict) else None
def _llm_update_summary(system_prompt: str, user_prompt: str) -> str:
active_profile = _get_active_profile_name() or "default"
from api import profiles as profiles_api
with profile_env_for_background_worker(
active_profile = profiles_api.get_active_profile_name() or "default"
with profiles_api.profile_env_for_background_worker(
active_profile,
"update summary",
logger_override=logger,
@@ -8281,7 +8282,9 @@ def _run_manual_compression_job(sid, body):
except KeyError:
session = None
if session is not None:
with profile_env_for_background_worker(session, "manual compression", logger_override=logger):
from api import profiles as profiles_api
with profiles_api.profile_env_for_background_worker(session, "manual compression", logger_override=logger):
_handle_session_compress(memory_handler, body)
else:
_handle_session_compress(memory_handler, body)

View File

@@ -34,7 +34,6 @@ from api.config import (
)
from api.helpers import redact_session_data, _redact_text
from api.compression_anchor import visible_messages_for_anchor
from api.profiles import profile_env_for_background_worker
from api.metering import meter
from api.turn_journal import append_turn_journal_event_for_stream
@@ -1469,7 +1468,9 @@ def _run_background_title_update(session_id: str, user_text: str, assistant_text
if not still_auto:
_put_title_status(put_event, session_id, 'skipped', 'manual_title', current)
return
with profile_env_for_background_worker(s, "background title", logger_override=logger):
from api import profiles as profiles_api
with profiles_api.profile_env_for_background_worker(s, "background title", logger_override=logger):
aux_title_configured = _aux_title_configured()
if agent and not aux_title_configured:
next_title, llm_status, raw_preview = _generate_llm_session_title_for_agent(agent, user_text, assistant_text)
@@ -1550,7 +1551,9 @@ def _run_background_title_refresh(session_id: str, user_text: str, assistant_tex
return
if not effective or effective in ('Untitled', 'New Chat'):
return
with profile_env_for_background_worker(s, "background title", logger_override=logger):
from api import profiles as profiles_api
with profiles_api.profile_env_for_background_worker(s, "background title", logger_override=logger):
aux_title_configured = _aux_title_configured()
if agent and not aux_title_configured:
next_title, llm_status, raw_preview = _generate_llm_session_title_for_agent(agent, user_text, assistant_text)

View File

@@ -27,8 +27,13 @@ def _reload_profiles_module(base_home: Path):
profiles = importlib.import_module("api.profiles")
# Restore original modules so the cache stays consistent for the rest of the suite.
# Restore original modules and package attributes so the cache stays
# consistent for the rest of the suite.
sys.modules.update(_saved)
api_pkg = sys.modules.get("api")
if api_pkg is not None:
for name, module in _saved.items():
setattr(api_pkg, name.rsplit(".", 1)[-1], module)
return profiles

View File

@@ -387,8 +387,9 @@ class TestBackgroundTitleProfileRouting(unittest.TestCase):
session = types.SimpleNamespace(profile='work')
captured = {}
with patch(
'api.profiles.get_hermes_home_for_profile',
with patch.object(
profiles,
'get_hermes_home_for_profile',
side_effect=RuntimeError('profile lookup failed'),
):
with patch.dict(os.environ, {'HERMES_HOME': 'default-home'}, clear=False):