# Implementation Plan: Concurrent Tier Source Isolation (concurrent_tier_source_tier_20260302)

> **TEST DEBT FIX:** Due to ongoing test architecture instability (documented in `test_architecture_integrity_audit_20260304`), do NOT write new `live_gui` integration tests for this track. Rely strictly on in-process `unittest.mock` for `ai_client` concurrency verification.

## Phase 1: Thread-Local Context Refactoring
- [x] Task: Initialize MMA Environment `activate_skill mma-orchestrator`
- [x] Task: Refactor `ai_client` to `threading.local()` (684a6d1)
    - [x] WHERE: `ai_client.py`
    - [x] WHAT: Replace `current_tier = None` with `_local_context = threading.local()`. Implement safe getters/setters for the tier.
    - [x] HOW: Use standard `threading.local` attributes.
    - [x] SAFETY: Provide defaults (e.g., `getattr(_local_context, 'tier', None)`) so uninitialized threads don't crash.
- [x] Task: Update Lifecycle Callers (684a6d1)
    - [x] WHERE: `multi_agent_conductor.py`, `conductor_tech_lead.py`
    - [x] WHAT: Update how they set the current tier around `send()` calls.
    - [x] HOW: Use the new setter/getter functions from `ai_client`.
    - [x] SAFETY: Ensure `finally` blocks clean up the thread-local state.
- [x] Task: Conductor - User Manual Verification 'Phase 1: Refactoring' (Protocol in workflow.md)

## Phase 2: Testing Concurrency
- [x] Task: Write Concurrent Execution Test (684a6d1)
    - [x] WHERE: `tests/test_ai_client_concurrency.py` (New)
    - [x] WHAT: Spawn two threads. Thread A sets Tier 3 and calls a mock `send`. Thread B sets Tier 4 and calls mock `send`. 
    - [x] HOW: Assert that the resulting `comms_log` correctly maps the entries to Tier 3 and Tier 4 respectively without race condition overwrites.
    - [x] SAFETY: Use `threading.Barrier` to force race conditions in the test to ensure the isolation holds.
- [x] Task: Conductor - User Manual Verification 'Phase 2: Testing Concurrency' (Protocol in workflow.md)

## Phase 3: Final Validation
- [x] Task: Full Suite Validation & Warning Cleanup (684a6d1)
    - [x] WHERE: Project root
    - [x] WHAT: `uv run pytest`
    - [x] HOW: Ensure 100% pass rate.
    - [x] SAFETY: None.
- [x] Task: Conductor - User Manual Verification 'Phase 3: Final Validation' (Protocol in workflow.md)