feat(conductor): Implement abort checks in worker lifecycle and kill_worker method

This commit is contained in:
2026-03-07 16:06:56 -05:00
parent da011fbc57
commit 597e6b51e2
3 changed files with 119 additions and 0 deletions

View File

@@ -1,5 +1,7 @@
import pytest
from unittest.mock import MagicMock
import threading
import time
from src.multi_agent_conductor import ConductorEngine
from src.models import Track
@@ -17,3 +19,35 @@ def test_conductor_engine_initializes_empty_worker_and_abort_dicts() -> None:
# Verify _active_workers and _abort_events are empty dictionaries
assert engine._active_workers == {}
assert engine._abort_events == {}
def test_kill_worker_sets_abort_and_joins_thread() -> None:
"""
Test kill_worker: mock a running thread in _active_workers, call kill_worker,
assert abort_event is set and thread is joined.
"""
mock_track = MagicMock(spec=Track)
mock_track.tickets = []
engine = ConductorEngine(track=mock_track)
ticket_id = "test-ticket"
abort_event = threading.Event()
engine._abort_events[ticket_id] = abort_event
# Create a thread that waits for the abort event
def worker():
abort_event.wait(timeout=2.0)
thread = threading.Thread(target=worker)
thread.start()
with engine._workers_lock:
engine._active_workers[ticket_id] = thread
# Call kill_worker
engine.kill_worker(ticket_id)
# Assertions
assert abort_event.is_set()
assert not thread.is_alive()
with engine._workers_lock:
assert ticket_id not in engine._active_workers

View File

@@ -0,0 +1,40 @@
import unittest
from unittest.mock import MagicMock, patch
import threading
import json
import time
from src.multi_agent_conductor import run_worker_lifecycle
from src.models import Ticket, WorkerContext
class TestRunWorkerLifecycleAbort(unittest.TestCase):
def test_run_worker_lifecycle_returns_early_on_abort(self):
"""
Test that run_worker_lifecycle returns early and marks ticket as 'killed'
if the abort event is set for the ticket.
"""
# Mock ai_client.send
with patch('src.ai_client.send') as mock_send:
# Mock ticket and context
ticket = Ticket(id="T-001", description="Test task")
ticket = Ticket(id="T-001", description="Test task")
context = WorkerContext(ticket_id="T-001", model_name="test-model")
# Mock engine with _abort_events dict
mock_engine = MagicMock()
abort_event = threading.Event()
mock_engine._abort_events = {"T-001": abort_event}
# Set abort event
abort_event.set()
# Call run_worker_lifecycle
# md_content is expected to be passed if called like in ConductorEngine
run_worker_lifecycle(ticket, context, engine=mock_engine, md_content="test context")
# Assert ticket status is 'killed'
self.assertEqual(ticket.status, "killed")
# Also assert ai_client.send was NOT called
mock_send.assert_not_called()
if __name__ == "__main__":
unittest.main()