# Implementation Plan: Expanded Hook API & Headless Orchestration

## Phase 1: WebSocket Infrastructure & Event Streaming
- [x] Task: Implement the WebSocket gateway.
    - [x] Integrate a lightweight WebSocket library (e.g., `websockets` or `simple-websocket`).
    - [x] Create a dedicated `WebSocketServer` class in `src/api_hooks.py` that runs on a separate port (e.g., 9000).
    - [x] Implement a basic subscription mechanism for different event channels.
- [x] Task: Connect the event queue to the WebSocket stream.
    - [x] Update `AsyncEventQueue` to broadcast events to connected WebSocket clients.
    - [x] Add high-frequency telemetry (FPS, CPU) to the event stream.
- [x] Task: Write unit tests for WebSocket connection and event broadcasting.
- [x] Task: Conductor - User Manual Verification 'Phase 1: WebSocket Infrastructure' (Protocol in workflow.md)

## Phase 2: Expanded Read Endpoints (GET)
- [x] Task: Implement detailed state exposure endpoints.
    - [x] Add `/api/mma/workers` to return the status, logs, and traces of all active sub-agents.
    - [x] Add `/api/context/state` to expose AST cache metadata and file aggregation status.
    - [x] Add `/api/metrics/financial` to return track-specific token usage and cost data.
    - [x] Add `/api/system/telemetry` to expose internal thread and queue metrics.
- [x] Task: Enhance `/api/gui/state` to provide a truly exhaustive JSON dump of all internal managers.
- [x] Task: Update `api_hook_client.py` with corresponding methods for all new GET endpoints.
- [x] Task: Write integration tests for all new GET endpoints using `live_gui`.
- [x] Task: Conductor - User Manual Verification 'Phase 2: Expanded Read Endpoints' (Protocol in workflow.md)

## Phase 3: Comprehensive Control Endpoints (POST)
- [x] Task: Implement worker and pipeline control.
    - [x] Add `/api/mma/workers/spawn` to manually initiate sub-agent execution via the API.
    - [x] Add `/api/mma/workers/kill` to programmatically abort running workers.
    - [x] Add `/api/mma/pipeline/pause` and `/api/mma/pipeline/resume` to control the global MMA loop.
- [x] Task: Implement context and DAG mutation.
    - [x] Add `/api/context/inject` to allow programmatic context injection (files/skeletons).
    - [x] Add `/api/mma/dag/mutate` to allow modifying task dependencies through the API.
- [x] Task: Update `api_hook_client.py` with corresponding methods for all new POST endpoints.
- [x] Task: Write integration tests for all new control endpoints using `live_gui`.
- [x] Task: Conductor - User Manual Verification 'Phase 3: Comprehensive Control Endpoints' (Protocol in workflow.md)

## Phase 4: Headless Refinement & Verification
- [x] Task: Improve error reporting.
    - [x] Refactor `HookHandler` to catch and wrap all internal exceptions in JSON error responses.
- [x] Task: Conduct a full headless simulation.
    - [x] Create a specialized simulation script that replicates a full MMA track lifecycle (planning, worker spawn, DAG mutation, completion) using ONLY the Hook API.
- [x] Task: Final performance audit.
    - [x] Ensure that active WebSocket clients and large state dumps do not cause GUI frame drops.
- [x] Task: Conductor - User Manual Verification 'Phase 4: Headless Refinement & Verification' (Protocol in workflow.md)
