# Implementation Plan: Test Suite Performance & Flakiness (test_suite_performance_and_flakiness_20260302)

## Phase 1: Audit & Polling Primitives
- [ ] Task: Initialize MMA Environment `activate_skill mma-orchestrator`
- [ ] Task: Create Deterministic Polling Primitives
    - [ ] WHERE: `tests/conftest.py`
    - [ ] WHAT: Implement a `wait_until(predicate_fn, timeout=5.0, interval=0.05)` utility.
    - [ ] HOW: Standard while loop that evaluates `predicate_fn()`.
    - [ ] SAFETY: Ensure it raises a clear `TimeoutError` if it fails.
- [ ] Task: Conductor - User Manual Verification 'Phase 1: Polling Primitives' (Protocol in workflow.md)

## Phase 2: Refactoring Integration Tests
- [ ] Task: Refactor `test_spawn_interception.py`
    - [ ] WHERE: `tests/test_spawn_interception.py`
    - [ ] WHAT: Replace hardcoded sleeps with `wait_until` checking the `event_queue` or internal state.
    - [ ] HOW: Use the new `conftest.py` utility.
    - [ ] SAFETY: Prevent event loop deadlocks.
- [ ] Task: Refactor Simulation Waits
    - [ ] WHERE: `simulation/*.py` and `tests/test_live_gui_integration.py`
    - [ ] WHAT: Replace `time.sleep()` blocks with `ApiHookClient.wait_for_event` or `client.wait_until_value_equals`.
    - [ ] HOW: Expand `ApiHookClient` polling capabilities if necessary.
    - [ ] SAFETY: Ensure the GUI hook server remains responsive during rapid polling.
- [ ] Task: Conductor - User Manual Verification 'Phase 2: Refactoring Sleeps' (Protocol in workflow.md)

## Phase 3: Test Marking & Final Validation
- [ ] Task: Apply Slow Test Marks
    - [ ] WHERE: Across all `tests/`
    - [ ] WHAT: Add `@pytest.mark.slow` to any test requiring a live GUI boot or API mocking that takes >2 seconds.
    - [ ] HOW: Import pytest and apply the decorator.
    - [ ] SAFETY: Update `pyproject.toml` to register the `slow` marker.
- [ ] Task: Full Suite Performance Validation
    - [ ] WHERE: Project root
    - [ ] WHAT: Run `uv run pytest -m "not slow"` and verify execution time < 10 seconds. Run `uv run pytest` to ensure total suite passes.
    - [ ] HOW: Time the terminal command.
    - [ ] SAFETY: None.
- [ ] Task: Conductor - User Manual Verification 'Phase 3: Final Validation' (Protocol in workflow.md)