# AIVA: Implement Downscale Actions Execution

This PRD defines the implementation of executing downscale actions within the AIVA framework, building upon the definition of default downscale actions.

### GPM-070: Create Action Executor Interface
**Goal:** To define a standard interface for executing different downscale actions.
**Acceptance Criteria:**
- An abstract class or interface named `DownscaleActionExecutor` exists.
- The `DownscaleActionExecutor` interface defines a method `execute(context: dict) -> bool`, which returns True if successful, False otherwise.
- The `execute` method accepts a context dictionary containing relevant information for the action.
**Files:** `aiva/actions/action_executor.py`
**Spawns:** Implement concrete action executors for specific downscale actions.

### GPM-071: Implement Stop Instance Action Executor
**Goal:** To create an action executor that stops a specified instance.
**Acceptance Criteria:**
- A class `StopInstanceActionExecutor` exists, inheriting from `DownscaleActionExecutor`.
- The `StopInstanceActionExecutor` implements the `execute` method to stop an instance based on the instance ID in the context.
- The `execute` method returns True if the instance is successfully stopped, False otherwise.
- The context dictionary should contain an `instance_id` key.
**Files:** `aiva/actions/stop_instance_action.py`
**Spawns:** Implement error handling and logging within the stop instance action executor.

### GPM-072: Implement Scale Down Instance Type Action Executor
**Goal:** To create an action executor that changes the instance type to a smaller size.
**Acceptance Criteria:**
- A class `ScaleDownInstanceTypeActionExecutor` exists, inheriting from `DownscaleActionExecutor`.
- The `ScaleDownInstanceTypeActionExecutor` implements the `execute` method to change the instance type of a specified instance based on the instance ID and a target instance type in the context.
- The `execute` method returns True if the instance type is successfully changed, False otherwise.
- The context dictionary should contain `instance_id` and `target_instance_type` keys.
**Files:** `aiva/actions/scale_down_instance_type_action.py`
**Spawns:** Implement logic to determine a suitable smaller instance type automatically.

### GPM-073: Implement Send Notification Action Executor
**Goal:** To create an action executor that sends a notification (e.g., email, Slack message) about the downscale event.
**Acceptance Criteria:**
- A class `SendNotificationActionExecutor` exists, inheriting from `DownscaleActionExecutor`.
- The `SendNotificationActionExecutor` implements the `execute` method to send a notification based on the notification channel and message content in the context.
- The `execute` method returns True if the notification is successfully sent, False otherwise.
- The context dictionary should contain `notification_channel` and `message_content` keys.  Supported channels could be 'email', 'slack', etc.
**Files:** `aiva/actions/send_notification_action.py`
**Spawns:** Implement different notification channel integrations (e.g., email via SMTP, Slack API).

### GPM-074: Integrate Action Executors into Downscale Detection Logic
**Goal:** To integrate the action executors into the downscale detection logic to trigger actions when a downscale pattern is detected.
**Acceptance Criteria:**
- The downscale detection logic now iterates through a list of configured actions for a detected downscale pattern.
- For each action, the corresponding action executor is instantiated and its `execute` method is called with the appropriate context.
- The execution results are logged.
**Files:** `aiva/core/downscale_detector.py`, `aiva/actions/__init__.py` (to register actions)
**Spawns:** Implement a configuration mechanism to define which actions should be triggered for specific downscale patterns.

### GPM-075: Implement Retry Mechanism for Action Execution
**Goal:** To implement a retry mechanism for action execution in case of failures.
**Acceptance Criteria:**
- If an action execution fails, the system attempts to retry the action a configurable number of times.
- A backoff strategy is implemented between retries (e.g., exponential backoff).
- The retry attempts and their results are logged.
**Files:** `aiva/actions/action_executor.py` (modify base class or add helper function)
**Spawns:** Implement circuit breaker pattern to prevent cascading failures during action execution.

### GPM-076: Add Logging to Action Executors
**Goal:** Add detailed logging to all action executors.
**Acceptance Criteria:**
- Each action executor logs:
    - The start of the execution.
    - The context data used for execution.
    - Any errors encountered during execution.
    - The result of the execution (success/failure).
**Files:** `aiva/actions/stop_instance_action.py`, `aiva/actions/scale_down_instance_type_action.py`, `aiva/actions/send_notification_action.py`
**Spawns:** Centralize logging configuration and add monitoring dashboards for action execution.
