ba3eb0c090
Phase 6: Eliminate Optional[T] returns - BATCH 2 of 7
Before: 7 more Optional[T] returns removed
After: 0 in command_palette.py, diff_viewer.py, fuzzy_anchor.py,
multi_agent_conductor.py, patch_modal.py, app_controller.py
Delta: -7 sites (cumulative: -15 of 30)
Specific changes:
- src/command_palette.py:50: CommandRegistry.get() returns Command (zero-init
sentinel: id="", title="", category="uncategorized", action=lambda: None)
- src/diff_viewer.py:117: get_line_color returns "" when no marker prefix
- src/fuzzy_anchor.py:40: FuzzyAnchor.resolve_slice returns (-1, -1) sentinel
(replaced 3x `return None` with `return (-1, -1)`)
- src/multi_agent_conductor.py:64: WorkerPool.spawn returns threading.Thread()
(empty sentinel, not started) when pool is full
- src/patch_modal.py:33: PatchModalManager.get_pending_patch returns
PendingPatch; class has EMPTY_PATCH sentinel; field type changed from
Optional[PendingPatch] to PendingPatch; 2x `= None` reset replaced with
`= EMPTY_PATCH`
- src/app_controller.py:4414: _confirm_and_run returns "" when not approved
(was Optional[str] returning None)
Test updates:
- tests/test_diff_viewer.py:95: get_line_color(" context") == ""
- tests/test_fuzzy_anchor.py:42,59: assert result == (-1, -1)
- tests/test_parallel_execution.py:31: t3 sentinel is now unstarted thread
(check via not t3.is_alive())
- tests/test_patch_modal.py:9,31,78: get_pending_patch() == "" sentinel check
Verification:
- audit_weak_types --strict: OK (107 <= 112 baseline)
- 22+ tests pass (test_diff_viewer, test_fuzzy_anchor,
test_parallel_execution, test_patch_modal, test_command_palette)
- py_check_syntax: OK on all changed files
REMAINING: ~15 Optional[T] returns in:
- src/external_editor.py (3)
- src/file_cache.py (7)
- src/diff_viewer.py: parse_hunk_header (1)
- src/models.py: ExternalEditorConfig.get_default (1)
- src/project_manager.py: load_track_state (1)
- src/session_logger.py: log_tool_call (1)
- src/app_controller.py: _pending_mma_spawn, _pending_mma_approval (2)
113 lines
3.2 KiB
Python
113 lines
3.2 KiB
Python
import threading
|
|
import time
|
|
import sys
|
|
import pytest
|
|
from unittest.mock import MagicMock
|
|
from src.multi_agent_conductor import WorkerPool
|
|
|
|
def test_worker_pool_limit():
|
|
max_workers = 2
|
|
pool = WorkerPool(max_workers=max_workers)
|
|
|
|
def slow_task(event):
|
|
event.set()
|
|
time.sleep(0.5)
|
|
|
|
event1 = threading.Event()
|
|
event2 = threading.Event()
|
|
event3 = threading.Event()
|
|
|
|
# Spawn 2 tasks
|
|
t1 = pool.spawn("t1", slow_task, (event1,))
|
|
t2 = pool.spawn("t2", slow_task, (event2,))
|
|
|
|
assert t1 is not None
|
|
assert t2 is not None
|
|
assert pool.get_active_count() == 2
|
|
assert pool.is_full() is True
|
|
|
|
# Try to spawn a 3rd task
|
|
t3 = pool.spawn("t3", slow_task, (event3,))
|
|
assert not t3.is_alive()
|
|
assert pool.get_active_count() == 2
|
|
|
|
# Wait for tasks to finish
|
|
event1.wait()
|
|
event2.wait()
|
|
pool.join_all()
|
|
|
|
assert pool.get_active_count() == 0
|
|
assert pool.is_full() is False
|
|
|
|
def test_worker_pool_tracking():
|
|
pool = WorkerPool(max_workers=4)
|
|
|
|
def task(ticket_id):
|
|
time.sleep(0.1)
|
|
|
|
pool.spawn("ticket_1", task, ("ticket_1",))
|
|
pool.spawn("ticket_2", task, ("ticket_2",))
|
|
|
|
assert "ticket_1" in pool._active
|
|
assert "ticket_2" in pool._active
|
|
|
|
pool.join_all()
|
|
assert len(pool._active) == 0
|
|
|
|
def test_worker_pool_completion_cleanup():
|
|
pool = WorkerPool(max_workers=4)
|
|
|
|
def fast_task():
|
|
pass
|
|
|
|
pool.spawn("t1", fast_task, ())
|
|
time.sleep(0.2) # Give it time to finish and run finally block
|
|
|
|
assert pool.get_active_count() == 0
|
|
assert "t1" not in pool._active
|
|
|
|
from unittest.mock import patch
|
|
from src.models import Track, Ticket
|
|
from src.multi_agent_conductor import ConductorEngine
|
|
|
|
@patch('src.multi_agent_conductor.run_worker_lifecycle')
|
|
def test_conductor_engine_pool_integration(mock_lifecycle):
|
|
# Create 4 independent tickets
|
|
tickets = [
|
|
Ticket(id=f"t{i}", description=f"task {i}", status="todo")
|
|
for i in range(4)
|
|
]
|
|
track = Track(id="test_track", description="test", tickets=tickets)
|
|
|
|
# Set up engine with auto_queue and explicit max_workers=2.
|
|
# ConductorEngine no longer reads config itself; the caller passes max_workers.
|
|
engine = ConductorEngine(track, auto_queue=True, max_workers=2)
|
|
sys.stderr.write(f"[TEST] engine.pool.max_workers = {engine.pool.max_workers}\n")
|
|
assert engine.pool.max_workers == 2
|
|
|
|
# Slow down lifecycle to capture parallel state
|
|
def slow_lifecycle(ticket, *args, **kwargs):
|
|
# Set to in_progress immediately to simulate the status change
|
|
# (The engine usually does this, but we want to be sure)
|
|
time.sleep(0.5)
|
|
ticket.status = "completed"
|
|
|
|
mock_lifecycle.side_effect = slow_lifecycle
|
|
|
|
# Run exactly 1 tick
|
|
engine.run(max_ticks=1)
|
|
|
|
# Verify only 2 were marked in_progress/spawned
|
|
# Because we only ran for one tick, and there were 4 ready tasks,
|
|
# it should have tried to spawn as many as possible (limit 2).
|
|
in_progress = [tk for tk in tickets if tk.status == "in_progress"]
|
|
# Also count those that already finished if the sleep was too short
|
|
completed = [tk for tk in tickets if tk.status == "completed"]
|
|
|
|
sys.stderr.write(f"[TEST] in_progress={len(in_progress)} completed={len(completed)}\n")
|
|
assert len(in_progress) + len(completed) == 2
|
|
assert engine.pool.get_active_count() <= 2
|
|
|
|
# Cleanup: wait for mock threads to finish or join_all
|
|
engine.pool.join_all()
|