fix(logging): stop pytest runs from writing to the live agent.log - #898
Open
hashbender wants to merge 1 commit into
Open
fix(logging): stop pytest runs from writing to the live agent.log#898hashbender wants to merge 1 commit into
hashbender wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Stops
pytest tests/from appending mock-provider log records to the real user'sagent.log(%LOCALAPPDATA%\hermes\logs\agent.logon native Windows,~/.hermes/logs/agent.logon POSIX).run_agent._hermes_homeis resolved once at import time. Under pytest, test modules are imported during collection — before the per-test_hermetic_environmentfixture redirectsHERMES_HOME— soagent_init'ssetup_logging(hermes_home=_ra()._hermes_home)attached root-logger file handlers to the real install'slogs/agent.log. Every mock-provider test in the process then wrote to the live log, producing entries likeprovider=xai-oauth base_url=https://api.x.ai/v1 model=grok-4.3 ... msg='Say OK'that look exactly like the agent silently calling real provider APIs.The fix is at the callsite (per the guidance already in the
tests/conftest.pydocstring) plus defense-in-depth in the conftest itself:agent_initnow callssetup_logging()with nohermes_home=override — the default resolvesget_hermes_home()at call time, which reads the env var live. Behavior for real CLI/gateway runs is unchanged (env doesn't change between import and agent init there).tests/conftest.pyexports a sandboxHERMES_HOMEat conftest import time, before pytest imports any test module, so every other import-timeget_hermes_home()consumer (cli._hermes_home, dotenv loading) freezes a sandbox path instead of the real install.Related Issue
Fixes NousResearch#57118
Type of Change
Changes Made
agent/agent_init.py— drop thehermes_home=_ra()._hermes_homeoverride on thesetup_logging()call; resolve the log directory from the live environment instead of run_agent's import-time frozen path.tests/conftest.py— setHERMES_HOMEto a session tempdir at module import time (unconditionally, so aHERMES_HOMEinherited from a developer shell or Docker deployment can't leak into tests either). The existing per-test fixture still re-points it per test.tests/run_agent/test_logging_home_isolation.py(new) — two regression tests:test_agent_logging_ignores_import_time_frozen_home: simulates a stalerun_agent._hermes_homeand assertsAIAgent.__init__attachesagent.logunder the liveHERMES_HOME, with nothing under the stale path. Fails if theagent_initfix is reverted.test_conftest_sandboxes_hermes_home_at_import_time: assertsrun_agent._hermes_homedid not freeze the platform-default (real) home. Fails if the conftest sandbox is reverted.How to Test
<hermes-home>/logs/agent.log.main, runpython -m pytest tests/run_agent/test_run_agent_codex_responses.py -q—grep 'api.x.ai' <hermes-home>/logs/agent.logshows new mock records (13 new lines in my repro, Windows 11 native).test_run_agent_codex_responses.py+test_auxiliary_client_xai_oauth_recovery.py, live-log signature count unchanged).python -m pytest tests/run_agent/test_logging_home_isolation.py -q— both regression tests pass; each fails with its half of the fix reverted.Checklist
Code
fix(scope):,feat(scope):, etc.)tests/cli/conftest.pyreload pollution but not this)pytest tests/ -qand all tests pass (per-file runs of the affected areas:tests/run_agent/test_run_agent.py414 passed,tests/run_agent/test_run_agent_codex_responses.py+ xai recovery 93 passed,tests/cron/test_cron_profile_isolation.py,tests/cron/test_cron_no_agent.py,tests/cli/test_cli_approval_ui.py,tests/agent/test_auxiliary_client.pyall green;tests/test_hermes_logging.py/tests/test_hermes_constants.pyhave pre-existing Windows-native chmod/symlink failures identical on unmodifiedmain)Documentation & Housekeeping
docs/, docstrings) — comments at both changed sites explain the import-time-freeze trapcli-config.yaml.exampleif I added/changed config keys — N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — N/Apytest tests/wrote to the real~/.hermes/logs/agent.logthe same way); fix uses onlytempfile/env, no platform branchesScreenshots / Logs
Pollution on
main(live%LOCALAPPDATA%\hermes\logs\agent.logafter running one test file):After this branch: same run adds zero records to the live log.
Mirror-of: NousResearch#57119
NousResearch#57119