52673c9cad
Extracts all Honcho-specific code from run_agent.py, model_tools.py, toolsets.py, and gateway/run.py. Honcho is now exclusively available as a memory provider plugin (plugins/honcho-memory/). Removed from run_agent.py (-457 lines): - Honcho init block (session manager creation, activation, config) - 8 Honcho methods: _honcho_should_activate, _strip_honcho_tools, _activate_honcho, _register_honcho_exit_hook, _queue_honcho_prefetch, _honcho_prefetch, _honcho_save_user_observation, _honcho_sync - _inject_honcho_turn_context module-level function - Honcho system prompt block (tool descriptions, CLI commands) - Honcho context injection in api_messages building - Honcho params from __init__ (honcho_session_key, honcho_manager, honcho_config) - HONCHO_TOOL_NAMES constant - All honcho-specific tool dispatch forwarding Removed from other files: - model_tools.py: honcho_tools import, honcho params from handle_function_call - toolsets.py: honcho toolset definition, honcho tools from core tools list - gateway/run.py: honcho params from AIAgent constructor calls Removed tests (-339 lines): - 9 Honcho-specific test methods from test_run_agent.py - TestHonchoAtexitFlush class from test_exit_cleanup_interrupt.py Restored two regex constants (_SURROGATE_RE, _BUDGET_WARNING_RE) that were accidentally removed during the honcho function extraction. The honcho_integration/ package is kept intact — the plugin delegates to it. tools/honcho_tools.py registry entries are now dead code (import commented out in model_tools.py) but the file is preserved for reference. Full suite: 7207 passed, 4 pre-existing failures. Zero regressions.
74 lines
2.8 KiB
Python
74 lines
2.8 KiB
Python
"""Tests for KeyboardInterrupt handling in exit cleanup paths.
|
|
|
|
``except Exception`` does not catch ``KeyboardInterrupt`` (which inherits
|
|
from ``BaseException``). A second Ctrl+C during exit cleanup must not
|
|
abort remaining cleanup steps. These tests exercise the actual production
|
|
code paths — not a copy of the try/except pattern.
|
|
"""
|
|
|
|
import atexit
|
|
import weakref
|
|
from unittest.mock import MagicMock, patch, call
|
|
|
|
import pytest
|
|
|
|
|
|
class TestCronJobCleanup:
|
|
"""cron/scheduler.py — end_session + close in the finally block."""
|
|
|
|
def test_keyboard_interrupt_in_end_session_does_not_skip_close(self):
|
|
"""If end_session raises KeyboardInterrupt, close() must still run."""
|
|
mock_db = MagicMock()
|
|
mock_db.end_session.side_effect = KeyboardInterrupt
|
|
|
|
from cron import scheduler
|
|
|
|
job = {
|
|
"id": "test-job-1",
|
|
"name": "test cleanup",
|
|
"prompt": "hello",
|
|
"schedule": "0 9 * * *",
|
|
"model": "test/model",
|
|
}
|
|
|
|
with patch("hermes_state.SessionDB", return_value=mock_db), \
|
|
patch.object(scheduler, "_build_job_prompt", return_value="hello"), \
|
|
patch.object(scheduler, "_resolve_origin", return_value=None), \
|
|
patch.object(scheduler, "_resolve_delivery_target", return_value=None), \
|
|
patch("dotenv.load_dotenv", return_value=None), \
|
|
patch("run_agent.AIAgent") as MockAgent:
|
|
# Make the agent raise immediately so we hit the finally block
|
|
MockAgent.return_value.run_conversation.side_effect = RuntimeError("boom")
|
|
scheduler.run_job(job)
|
|
|
|
mock_db.end_session.assert_called_once()
|
|
mock_db.close.assert_called_once()
|
|
|
|
def test_keyboard_interrupt_in_close_does_not_propagate(self):
|
|
"""If close() raises KeyboardInterrupt, it must not escape run_job."""
|
|
mock_db = MagicMock()
|
|
mock_db.close.side_effect = KeyboardInterrupt
|
|
|
|
from cron import scheduler
|
|
|
|
job = {
|
|
"id": "test-job-2",
|
|
"name": "test close interrupt",
|
|
"prompt": "hello",
|
|
"schedule": "0 9 * * *",
|
|
"model": "test/model",
|
|
}
|
|
|
|
with patch("hermes_state.SessionDB", return_value=mock_db), \
|
|
patch.object(scheduler, "_build_job_prompt", return_value="hello"), \
|
|
patch.object(scheduler, "_resolve_origin", return_value=None), \
|
|
patch.object(scheduler, "_resolve_delivery_target", return_value=None), \
|
|
patch("dotenv.load_dotenv", return_value=None), \
|
|
patch("run_agent.AIAgent") as MockAgent:
|
|
MockAgent.return_value.run_conversation.side_effect = RuntimeError("boom")
|
|
# Must not raise
|
|
scheduler.run_job(job)
|
|
|
|
mock_db.end_session.assert_called_once()
|
|
mock_db.close.assert_called_once()
|