@a2a @agents @memory_leak @agent_task_pruning
Feature: Agent Task Memory Leak Fix
  As a CleverAgents developer
  I want to ensure that completed tasks are removed from Agent._tasks
  So that long-lived agent instances do not accumulate unbounded memory

  Background:
    Given a test agent for memory leak testing

  @agent_task_pruning @memory_leak
  Scenario: Completed tasks are removed from _tasks list after processing
    When I send a message to the agent
    And I wait for the task to complete
    Then the _tasks list should be empty
    And the message should have been processed

  @agent_task_pruning @memory_leak
  Scenario: Multiple messages result in bounded _tasks list
    When I send 10 messages to the agent
    And I wait for all tasks to complete
    Then the _tasks list should be empty
    And all 10 messages should have been processed

  @agent_task_pruning @memory_leak
  Scenario: Failed tasks are also removed from _tasks list
    When I send a message that will fail to the agent
    And I wait for the task to complete
    Then the _tasks list should be empty
    And the error should have been raised

  @agent_task_pruning @memory_leak
  Scenario: _tasks list does not grow unboundedly during long-lived operation
    When I send 100 messages to the agent in rapid succession
    And I wait for all tasks to complete
    Then the _tasks list should be empty
    And the maximum _tasks list size should not exceed 10
    And all 100 messages should have been processed

  @agent_task_pruning @memory_leak
  Scenario: Task removal is thread-safe with concurrent messages
    When I send 50 messages to the agent concurrently
    And I wait for all tasks to complete
    Then the _tasks list should be empty
    And all 50 messages should have been processed
